Incomputer programming,ahandleis an abstractreferenceto aresourcethat is used whenapplication softwarereferencesblocksofmemoryor objects that are managed by another system like adatabaseor anoperating system.

A resource handle can be anopaqueidentifier,in which case it is often anintegernumber (often anarray indexin an array or "table" that is used to manage that type of resource), or it can be apointerthat allows access to further information. Common resource handles includefile descriptors,network sockets,database connections,process identifiers(PIDs), andjob IDs.PIDs and job IDs are explicitly visible integers; while file descriptors and sockets (which are often implemented as a form of file descriptor) are represented as integers, they are typically considered opaque. In traditional implementations, file descriptors are indices into a (per-process)file descriptor table,thence a (system-wide)file table.

Comparison to pointers

edit

While apointercontains theaddressof the item to which it refers, a handle is anabstractionof a reference which is managed externally; its opacity allows the referent to be relocated in memory by the system without invalidating the handle, making it similar tovirtual memoryfor pointers, but even more abstracted. Similarly, the extra layer ofindirectionalso increases the control that the managing system has over the operations performed on the referent. Typically the handle is an index or a pointer into a global array oftombstones.

Ahandle leakis a type ofsoftware bugthat occurs when a computer program does not free a handle that it previously allocated. This is a form ofresource leak,analogous to amemory leakfor previously allocated memory.

Security

edit

Insecure computingterms, because access to a resource via a handle is mediated by another system, a handle functions as acapability:it not only identifies an object, but also associatesaccess rights.For example, while a filename is forgeable (it is just a guessable identifier), a handle isgivento a user by an external system, and thus represents not just identity, but alsograntedaccess.

For example, if a program wishes to read the system password file (/etc/passwd) in read/write mode (O_RDWR), it could try to open the file via the following call:

intfd=open("/etc/passwd",O_RDWR);

This call asks the operating system to open the specified file with the specified access rights. If the OS allows this, then it opens the file (creates an entry in the per-processfile descriptor table) and returns a handle (file descriptor, index into this table) to the user: the actual access is controlled by the OS, and the handle is atokenof that. Conversely, the OS may deny access, and thus neither open the file nor return a handle.

In a capability-based system, handles can be passed between processes, with associated access rights. Note that in these cases the handle must be something other than a systemwide-unique small integer, otherwise it is forgeable. Such an integer may nevertheless be used to identify a capability inside a process; e.g., file descriptor in Linux is unforgeable because its numerical value alone is meaningless, and only in the process context may refer to anything. Transferring such a handle requires special care though, as its value often has to be different in the sending and receiving processes.

In non-capability-based systems, on the other hand, each process must acquire its own separate handle, by specifying the identity of the resource and the desired access rights (e.g., each process must open a file itself, by giving the filename and access mode). Such usage is more common even in modern systems that do support passing handles, but it is subject to vulnerabilities like theconfused deputy problem.

Examples

edit

Handles were a popular solution tomemory managementin operating systems of the 1990s, such asMac OS[1]andWindows.The FILE data structure in theC standard I/O libraryis afile handle,abstracting from the underlying file representation (onUnixthese arefile descriptors). Like otherdesktop environments,theWindows APIheavily uses handles to represent objects in the system and to provide a communication pathway between the operating system anduser space.For example, a window on thedesktopis represented by a handle of typeHWND(handle, window).

Doubly indirecthandles (where the handle is not necessarily a pointer but might be, for example, an integer) have fallen out of favor in recent times, as increases in available memory and improvedvirtual memoryalgorithms have made the use of the simpler pointer more attractive. However, many operating systems still apply the term to pointers to opaque, "private"data structuresopaque pointers—or to indexes into internal arrays passed from oneprocessto itsclient.

See also

edit

References

edit
  1. ^Hertzfeld, Andy(January 1982),The Original Macintosh: Hungarian,retrieved2010-05-10
edit