Ptrace
   HOME

TheInfoList



OR:

ptrace is a
system call In computing, a system call (syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, accessing a hard disk drive ...
found in
Unix Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
and several
Unix-like A Unix-like (sometimes referred to as UN*X, *nix or *NIX) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Uni ...
operating system An operating system (OS) is system software that manages computer hardware and software resources, and provides common daemon (computing), services for computer programs. Time-sharing operating systems scheduler (computing), schedule tasks for ...
s. By using ptrace (an abbreviation of "process trace") one
process A process is a series or set of activities that interact to produce a result; it may occur once-only or be recurrent or periodic. Things called a process include: Business and management * Business process, activities that produce a specific s ...
can control another, enabling the controller to inspect and manipulate the internal state of its target. ptrace is used by
debugger A debugger is a computer program used to test and debug other programs (the "target" programs). Common features of debuggers include the ability to run or halt the target program using breakpoints, step through code line by line, and display ...
s and other code-analysis tools, mostly as aids to software development.


Uses

ptrace is used by debuggers (such as gdb and
dbx dbx or DBX may refer to: * dbx (debugger), a Unix source-level debugger * dbx (company), a professional audio recording equipment company ** dbx (noise reduction), a noise reduction system invented by dbx, Inc. * .dbx, the file extension for Micros ...
), by tracing tools like strace and ltrace, and by
code coverage In software engineering, code coverage, also called test coverage, is a percentage measure of the degree to which the source code of a program is executed when a particular test suite is run. A program with high code coverage has more of its ...
tools. ptrace is also used by specialized programs to patch running programs, to avoid unfixed bugs or to overcome security features. It can further be used as a
sandbox A sandbox is a sandpit, a wide, shallow playground construction to hold sand, often made of wood or plastic. Sandbox or sand box may also refer to: Arts, entertainment, and media * Sandbox (band), a Canadian rock music group * Sandbox (Gu ...
sydbox
/ref>PRoot
/ref> and as a run-time environment simulator (like emulating root access for non-root software). By attaching to another process using the ptrace call, a tool has extensive control over the operation of its target. This includes manipulation of its
file descriptor In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket. File descriptors typically h ...
s, memory, and registers. It can single-step through the target's code, can observe and intercept system calls and their results, and can manipulate the target's
signal A signal is both the process and the result of transmission of data over some media accomplished by embedding some variation. Signals are important in multiple subject fields including signal processing, information theory and biology. In ...
handlers and both receive and send signals on its behalf. The ability to write into the target's memory allows not only its data store to be changed, but also the application's own
code segment In computing, a code segment, also known as a text segment or simply as text, is a portion of an object file or the corresponding section of the program's virtual address space that contains executable instructions. Segment The term "segment" c ...
, allowing the controller to install
breakpoint In software development, a breakpoint is an intentional stopping or pausing place in a computer program, program, put in place for debugging purposes. It is also sometimes simply referred to as a pause. More generally, a breakpoint is a means o ...
s and patch the running code of the target. As the ability to inspect and alter another process is very powerful, ptrace can attach only to processes that the owner can send signals to (typically only their own processes); the
superuser In computing, the superuser is a special user account used for system administration. Depending on the operating system (OS), the actual name of this account might be root, administrator, admin or supervisor. In some cases, the actual name of the ...
account can ptrace almost any process (except
init In Unix-based computer operating systems, init (short for ''initialization'') is the first process started during booting of the operating system. Init is a daemon process that continues running until the system is shut down. It is the direc ...
on kernels before 2.6.26). In Linux systems where
POSIX capabilities The Portable Operating System Interface (POSIX; ) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines application programming interfaces (APIs), along with comma ...
are used, the ability to ptrace is further limited by the CAP_SYS_PTRACE capability or by the YAMA Linux Security Module. In
FreeBSD FreeBSD is a free-software Unix-like operating system descended from the Berkeley Software Distribution (BSD). The first version was released in 1993 developed from 386BSD, one of the first fully functional and free Unix clones on affordable ...
, it is limited by
FreeBSD jail The jail mechanism is an implementation of FreeBSD's OS-level virtualisation that allows system administrators to partition a FreeBSD-derived computer system into several independent mini-systems called ''jails'', all sharing the same kernel, with ...
s and
Mandatory Access Control In computer security, mandatory access control (MAC) refers to a type of access control by which a secured environment (e.g., an operating system or a database) constrains the ability of a ''subject'' or ''initiator'' to access or modify on an ' ...
policies.


Limitations

Communications between the controller and target take place using repeated calls of ptrace, passing a small fixed-size block of memory between the two (necessitating two
context switch In computing, a context switch is the process of storing the state of a process or thread, so that it can be restored and resume execution at a later point, and then restoring a different, previously saved, state. This allows multiple processes ...
es per call); this is acutely inefficient when accessing large amounts of the target's memory, as this can only be done in
word A word is a basic element of language that carries semantics, meaning, can be used on its own, and is uninterruptible. Despite the fact that language speakers often have an intuitive grasp of what a word is, there is no consensus among linguist ...
sized blocks (with a ptrace call for each word). For this reason the 8th edition of Unix introduced
procfs The proc filesystem (procfs) is a special filesystem in Unix-like operating systems that presents information about processes and other system information in a hierarchical file-like structure, providing a more convenient and standardized metho ...
, which allows permitted processes direct access to the memory of another process - 4.4BSD followed, and the use of /proc for debugger support was inherited by Solaris, BSD, and AIX, and mostly copied by Linux. Some, such as
Solaris Solaris is the Latin word for sun. It may refer to: Arts and entertainment Literature, television and film * ''Solaris'' (novel), a 1961 science fiction novel by Stanisław Lem ** ''Solaris'' (1968 film), directed by Boris Nirenburg ** ''Sol ...
, have removed ptrace as a system call altogether, retaining it as a library call that reinterprets calls to ptrace in terms of the platform's procfs. Such systems use
ioctl In computing, ioctl (an abbreviation of input/output control) is a system call for device-specific input/output operations and other operations which cannot be expressed by regular file semantics. It takes a parameter specifying a request code; ...
s on the
file descriptor In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket. File descriptors typically h ...
of the opened /proc file to issue commands to the controlled process.
FreeBSD FreeBSD is a free-software Unix-like operating system descended from the Berkeley Software Distribution (BSD). The first version was released in 1993 developed from 386BSD, one of the first fully functional and free Unix clones on affordable ...
, on the other hand, extended ptrace to remove mentioned problems, and declared
procfs The proc filesystem (procfs) is a special filesystem in Unix-like operating systems that presents information about processes and other system information in a hierarchical file-like structure, providing a more convenient and standardized metho ...
obsolete due to its inherent design problems. ptrace only provides the most basic interface necessary to support debuggers and similar tools. Programs using it must have intimate knowledge of the specifics of the OS and architecture, including stack layout,
application binary interface An application binary interface (ABI) is an interface exposed by software that is defined for in-process machine code access. Often, the exposing software is a library, and the consumer is a program. An ABI is at a relatively low-level of a ...
,
system call In computing, a system call (syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, accessing a hard disk drive ...
mechanism,
name mangling In compiler construction, name mangling (also called name decoration) is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages. It provides means to e ...
, the format of any debug data, and are responsible for understanding and disassembling
machine code In computer programming, machine code is computer code consisting of machine language instructions, which are used to control a computer's central processing unit (CPU). For conventional binary computers, machine code is the binaryOn nonb ...
themselves. Further, programs that inject executable code into the target process or (like gdb) allow the user to enter commands that are executed in the context of the target must generate and load that code themselves, generally without the help of the program loader.


Support


Unix and BSD

ptrace was first implemented in
Version 6 Unix Sixth Edition Unix, also called Version 6 Unix or just V6 is a version of the Unix operating system first released in May 1975 and the first version of the Unix operating system to see wide release outside Bell Labs. Like its direct predecessor, t ...
, and was present in both the
SVr4 Unix System V (pronounced: "System Five") is one of the first commercial versions of the Unix operating system. It was originally developed by AT&T and first released in 1983. Four major versions of System V were released, numbered 1, 2, 3, an ...
and
4.3BSD The history of the Berkeley Software Distribution began in the 1970s when University of California, Berkeley received a copy of Unix. Professors and students at the university began adding software to the operating system and released it as BSD to ...
branches of Unix. ptrace is available as a system call on
IRIX IRIX (, ) is a discontinued operating system developed by Silicon Graphics (SGI) to run on the company's proprietary MIPS architecture, MIPS workstations and servers. It is based on UNIX System V with Berkeley Software Distribution, BSD extensio ...
,
IBM AIX AIX (pronounced ) is a series of Proprietary software, proprietary Unix operating systems developed and sold by IBM since 1986. The name stands for "Advanced Interactive eXecutive". Current versions are designed to work with Power ISA based ...
,
NetBSD NetBSD is a free and open-source Unix-like operating system based on the Berkeley Software Distribution (BSD). It was the first open-source BSD descendant officially released after 386BSD was fork (software development), forked. It continues to ...
,
FreeBSD FreeBSD is a free-software Unix-like operating system descended from the Berkeley Software Distribution (BSD). The first version was released in 1993 developed from 386BSD, one of the first fully functional and free Unix clones on affordable ...
,
OpenBSD OpenBSD is a security-focused operating system, security-focused, free software, Unix-like operating system based on the Berkeley Software Distribution (BSD). Theo de Raadt created OpenBSD in 1995 by fork (software development), forking NetBSD ...
, and
Linux Linux ( ) is a family of open source Unix-like operating systems based on the Linux kernel, an kernel (operating system), operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically package manager, pac ...
. ptrace is implemented as a library call on Solaris, built on the Solaris kernel's procfs filesystem; Sun notes that ptrace on Solaris is intended for compatibility, and recommends that new implementations use the richer interface that proc supplies instead.
UnixWare UnixWare is a Unix operating system. It was originally released by Univel, a jointly owned venture of AT&T's Unix System Laboratories (USL) and Novell. It was then taken over by Novell. Via Santa Cruz Operation (SCO), it went on to Caldera Syst ...
also features a limited ptrace but like Sun, SCO recommends implementers use the underlying procfs features instead.
HP-UX HP-UX (from "Hewlett Packard Unix") is a proprietary software, proprietary implementation of the Unix operating system developed by Hewlett Packard Enterprise; current versions support HPE Integrity Servers, based on Intel's Itanium architect ...
supported ptrace until release 11i v3 (it was deprecated in favour o
ttrace
a similar OS-specific call, in 11i v1).


macOS

Apple's
macOS macOS, previously OS X and originally Mac OS X, is a Unix, Unix-based operating system developed and marketed by Apple Inc., Apple since 2001. It is the current operating system for Apple's Mac (computer), Mac computers. With ...
also implements ptrace as a system call. Apple's version adds a special option PT_DENY_ATTACH – if a process invokes this option on itself, subsequent attempts to ptrace the process will fail. Apple uses this feature to limit the use of debuggers on programs that manipulate
DRM DRM may refer to: Government, military and politics * Defense reform movement, U.S. campaign inspired by Col. John Boyd * Democratic Republic of Madagascar, a former socialist state (1975–1992) on Madagascar * Direction du renseignement militair ...
-ed content, including
iTunes iTunes is a media player, media library, and mobile device management (MDM) utility developed by Apple. It is used to purchase, play, download and organize digital multimedia on personal computers running the macOS and Windows operating s ...
. PT_DENY_ATTACH on also disables
DTrace DTrace is a comprehensive dynamic tracing framework originally created by Sun Microsystems for troubleshooting kernel and application problems on production systems in real time. Originally developed for Solaris, it has since been released un ...
's ability to monitor the process. Debuggers on OS X typically use a combination of ptrace and the
Mach The Mach number (M or Ma), often only Mach, (; ) is a dimensionless quantity in fluid dynamics representing the ratio of flow velocity past a Boundary (thermodynamic), boundary to the local speed of sound. It is named after the Austrian physi ...
VM and thread APIs. ptrace (again with PT_DENY_ATTACH) is available to developers for the Apple
iPhone The iPhone is a line of smartphones developed and marketed by Apple that run iOS, the company's own mobile operating system. The first-generation iPhone was announced by then–Apple CEO and co-founder Steve Jobs on January 9, 2007, at ...
.


Linux

Linux also gives processes the ability to prevent other processes from attaching to them. Processes can call the prctl syscall and clear their PR_SET_DUMPABLE flag; in later kernels this prevents non-root processes from ptracing the calling process; the OpenSSH authentication agent uses this mechanism to prevent ssh session hijacking via ptrace. Later Ubuntu versions ship with a Linux kernel configured to prevent ptrace attaches from processes other than the traced process' parent; this allows gdb and strace to continue to work when running a target process, but prevents them from attaching to an unrelated running process. Control of this feature is performed via the /proc/sys/kernel/yama/ptrace_scope setting. On systems where this feature is enabled, commands like "gdb --attach" and "strace -p" will not work. Starting in Ubuntu 10.10, ptrace is only allowed to be called on child processes.


Android

For some Android phones with a locked boot loader, ptrace is used to gain control over the init process to enable a '2nd boot' and replace the system files.


References

{{reflist, 30em, refs= "ptrace(2)"
IRIX 6.5 manual, section 2, SGI techpubs library

IBM AIX Technical Reference: Base Operating System and Extensions, Volume 1

netbsd manual, section 2

FreeBSD manual, section 2
"ptrace(2)"
OpenBSD manual, section 2
"ptrace(2) manpage"
Linux manual section 2
"ptrace() Request Values"
''Solaris Transition Guide'',
Sun Microsystems Sun Microsystems, Inc., often known as Sun for short, was an American technology company that existed from 1982 to 2010 which developed and sold computers, computer components, software, and information technology services. Sun contributed sig ...
, 2000
ptrace(2)
SCO UnixWare 7 manual, section 2
For exampl
retty
uses ptrace to alter another process' file descriptors, and to inject executable code into the target's text segment
''The Design and Implementation of the 4.4 BSD Operating System'',
Marshall Kirk McKusick Marshall Kirk McKusick (born January 19, 1954) is an American computer scientist, known for his extensive work on BSD UNIX, from the 1980s to FreeBSD in the present day. He served on the board of the USENIX Association from 1986 to 1992 and aga ...
, Keith Bostic, Michael J. Karels,
John Quarterman John S. Quarterman (born April 27, 1954) is an American author and longtime Internet participant. He wrote one of the classic books about Computer network, networking prior to the commercialization of the Internet. He has also written about risk ...
, Addison-Wesley, April 1996, {{ISBN, 0-201-54979-4
"ptrace(2)"
BSD System Calls Manual, Apple iPhone OS Reference Library
"Apple 'breaks' Sun developer app"
Matthew Broersma, '' Computerworld UK'', 24 January 2008
Chapter 9, ''Mac OS X internals: a systems approach'', Amit Singh, {{ISBN, 978-0-321-27854-8, Addison Wesley, 2006 "ptrace() System Call (Obsolete)"
HP-UX 11i Version 3 Release Notes: HP 9000 and HP Integrity Servers,
Hewlett Packard The Hewlett-Packard Company, commonly shortened to Hewlett-Packard ( ) or HP, was an American multinational information technology company. It was founded by Bill Hewlett and David Packard in 1939 in a one-car garage in Palo Alto, Californi ...
, February 2007
"System call compatibility notes"
{{webarchive, url=https://web.archive.org/web/20110716015312/http://uw714doc.sco.com/en/SDK_porting/syscall_compat_notes_top.html , date=2011-07-16 , UnixWare 7 Documentation

Apple Darwin/OS-X manual
"Owning the Fanboys : Hacking Mac OS X"
Charlie Miller,
Black Hat Briefings Black Hat Briefings (commonly referred to as Black Hat) is a computer security conference that provides security consulting, training, and briefings to hackers, corporations, and government agencies around the world. Black Hat brings together ...
conference 2008
"KernelHardening"
Ubuntu security team roadmap
"prctl(2)"
Linux programmer's manual, section 2
"PATCH ptrace: allow restriction of ptrace scope"
posting by Canonical Ltd. engineer Kees Cook, Linux Kernel mailing list, June 16, 2010


External links


Article from Linux Gazette about ptrace

Article about ptrace in linux journal
Unix Debugging System calls