my-server
← Wiki Redirected from Ioctl(2)

Ioctl

In computing, <code>ioctl</code> (an abbreviation of input/output control) is a system call for device-specific input/output operations and other operations which cannot be expressed by read/write/seek regular file semantics. It takes a parameter specifying a request code; the effect of a call depends completely on the request code. Request codes are often device-specific. For instance, a CD-ROM device driver which can instruct a physical device to eject a disc would provide an <code>ioctl</code> request code to do so. Device-independent request codes are sometimes used to give userspace access to kernel functions which are only used by core system software or still under development.

The <code>ioctl</code> system call first appeared in Version 7 of Unix under that name. It is supported by most Unix and Unix-like systems, including Linux and macOS, though the available request codes differ from system to system. Microsoft Windows provides a similar function, named "<code>DeviceIoControl</code>", in its Win32 API.

Background

Conventional operating systems can be divided into two layers, userspace and the kernel. Application code such as a text editor resides in userspace, while the underlying facilities of the operating system, such as the network stack, reside in the kernel. Kernel code handles sensitive resources and implements the security and reliability barriers between applications; for this reason, user mode applications are prevented by the operating system from directly accessing kernel resources.

Userspace applications typically make requests to the kernel by means of system calls, whose code lies in the kernel layer. A system call usually takes the form of a "system call vector", in which the desired system call is indicated with an index number. For instance, <code>exit()</code> might be system call number 1, and <code>write()</code> number 4. The system call vector is then used to find the desired kernel function for the request. In this way, conventional operating systems typically provide several hundred system calls to the userspace.

Though an expedient design for accessing standard kernel facilities, system calls are sometimes inappropriate for accessing non-standard hardware peripherals. By necessity, most hardware peripherals (aka devices) are directly addressable only within the kernel. But user code may need to communicate directly with devices; for instance, an administrator might configure the media type on an Ethernet interface. Modern operating systems support diverse devices, many of which offer a large collection of facilities. Some of these facilities may not be foreseen by the kernel designer, and as a consequence it is difficult for a kernel to provide system calls for using the devices.

To solve this problem, the kernel is designed to be extensible, and may accept an extra module called a device driver which runs in kernel space and can directly address the device. An <code>ioctl</code> interface is a single system call by which userspace may communicate with device drivers. Requests on a device driver are vectored with respect to this <code>ioctl</code> system call, typically by a handle to the device and a request number. The basic kernel can thus allow the userspace to access a device driver without knowing anything about the facilities supported by the device, and without needing an unmanageably large collection of system calls.

Uses

Hardware device configuration

A common use of <code>ioctl</code> is to control hardware devices.

For example, on Win32 systems, <code>ioctl</code> calls can communicate with USB devices, or they can discover drive-geometry information of the attached storage-devices.

On OpenBSD and NetBSD, <code>ioctl</code> is used by the <code></code> pseudo-device driver and the <code>bioctl</code> utility to implement RAID volume management in a unified vendor-agnostic interface similar to <code>ifconfig</code>.

On NetBSD, <code>ioctl</code> is also used by the <code>sysmon</code> framework.

Terminals

One use of <code>ioctl</code> in code exposed to end-user applications is terminal I/O.

Unix operating systems have traditionally made heavy use of command-line interfaces, originally with hardware text terminals such as VT100s attached to serial ports, and later with terminal emulators and remote login servers using pseudoterminals. Serial port devices and pseudoterminals are both controlled and configured using <code>ioctl</code> calls. For instance, the display size is set using the <code>TIOCSWINSZ</code> call. The TIOCSTI (terminal I/O control, simulate terminal input) ioctl function can push a character into a device stream.

Kernel extensions

When applications need to extend the kernel, for instance to accelerate network processing, <code>ioctl</code> calls provide a convenient way to bridge userspace code to kernel extensions. Kernel extensions can provide a location in the filesystem that can be opened by name, through which an arbitrary number of <code>ioctl</code> calls can be dispatched, allowing the extension to be programmed without adding system calls to the operating system.

sysctl alternative

According to an OpenBSD developer, <code>ioctl</code> and <code>sysctl</code> are the two system calls for extending the kernel, with <code>sysctl</code> possibly being the simpler of the two.

In NetBSD, the <code>sysmon_envsys</code> framework for hardware monitoring uses <code>ioctl</code> through <code>proplib</code>; whereas OpenBSD and DragonFly BSD instead use <code>sysctl</code> for their corresponding <code>hw.sensors</code> framework. The original revision of <code>envsys</code> in NetBSD was implemented with <code>ioctl</code> before <code>proplib</code> was available, and had a message suggesting that the framework is experimental, and should be replaced by a <code>sysctl(8)</code> interface, should one be developed, which potentially explains the choice of <code>sysctl</code> in OpenBSD with its subsequent introduction of <code>hw.sensors</code> in 2003. However, when the <code>envsys</code> framework was redesigned in 2007 around <code>proplib</code>, the system call remained as <code>ioctl</code>, and the message was removed.

Implementations

Unix

The <code>ioctl</code> system call first appeared in Version 7 Unix, as a replacement for the <code>stty</code> and <code>gtty</code> system calls, with an additional request code argument. An <code>ioctl</code> call takes as parameters:

  1. an open file descriptor
  2. a request code number
  3. an untyped pointer to data (either going to the driver, coming back from the driver, or both).

The kernel generally dispatches an <code>ioctl</code> call straight to the device driver, which can interpret the request number and data in whatever way required. The writers of each driver document request numbers for that particular driver and provide them as constants in a header file.

Request numbers usually combine a code identifying the device or class of devices for which the request is intended and a number indicating the particular request; the code identifying the device or class of devices is usually a single ASCII character. Some Unix systems, including 4.2BSD and later BSD releases, operating systems derived from those releases, and Linux, have conventions that also encode within the request number the size of the data to be transferred to/from the device driver and the direction of the data transfer. Regardless of whether any such conventions are followed, the kernel and the driver collaborate to deliver a uniform error code (denoted by the symbolic constant <code>ENOTTY</code>) to an application which makes a request of a driver which does not recognise it.

The mnemonic <code>ENOTTY</code> (traditionally associated with the textual message "Not a typewriter") derives from the earliest systems that incorporated an <code>ioctl</code> call, where only the teletype (<code>tty</code>) device raised this error. Though the symbolic mnemonic is fixed by compatibility requirements, some modern systems more helpfully render a more general message such as "Inappropriate device control operation" (or a localization thereof).

<code>TCSETS</code> exemplifies an <code>ioctl</code> call on a serial port. The normal read and write calls on a serial port receive and send data bytes. An <code>ioctl(fd,TCSETS,data)</code> call, separate from such normal I/O, controls various driver options like handling of special characters, or the output signals on the port (such as the DTR signal).

Win32

A Win32 <code>DeviceIoControl</code> takes as parameters:

  1. an open object handle (the Win32 equivalent of a file descriptor)
  2. a request code number (the "control code")
  3. a buffer for input parameters
  4. length of the input buffer
  5. a buffer for output results
  6. length of the output buffer
  7. an <code>OVERLAPPED</code> structure, if overlapped I/O is being used.

The Win32 device control code takes into consideration the mode of the operation being performed.

There are 4 defined modes of operation, impacting the security of the device driver -

  1. <code>METHOD_IN_DIRECT</code>: The buffer address is verified to be readable by the user mode caller.
  2. <code>METHOD_OUT_DIRECT</code>: The buffer address is verified to be writable by the user mode caller.
  3. <code>METHOD_NEITHER</code>: User mode virtual addresses are passed to the driver without mapping or validation.
  4. <code>METHOD_BUFFERED</code>: IO Manager controlled shared buffers are used to move data to and from user mode.

Alternatives

Other vectored call interfaces

Devices and kernel extensions may be linked to userspace using additional new system calls, although this approach is rarely taken, because operating system developers try to keep the system call interface focused and efficient.

On Unix operating systems, two other vectored call interfaces are popular: the <code>fcntl</code> ("file control") system call configures open files, and is used in situations such as enabling non-blocking I/O; and the <code>setsockopt</code> ("set socket option") system call configures open network sockets, a facility used to configure the <code>ipfw</code> packet firewall on BSD Unix systems.

Memory mapping

Unix: Device interfaces and input/output capabilities are sometimes provided using memory-mapped files. Applications that interact with devices open a location on the filesystem corresponding to the device, as they would for an <code>ioctl</code> call, but then use memory mapping system calls to tie a portion of their address space to that of the kernel. This interface is a far more efficient way to provide bulk data transfer between a device and a userspace application; individual <code>ioctl</code> or read/write system calls inflict overhead due to repeated userspace-to-kernel transitions, where access to a memory-mapped range of addresses incurs no such overhead.
Win32: Buffered IO methods or named file mapping objects can be used; however, for simple device drivers the standard <code>DeviceIoControl METHOD_</code> accesses are sufficient.

Netlink

Netlink is a socket-like mechanism for inter-process communication (IPC), designed to be a more flexible successor to <code>ioctl</code>.

Implications

Complexity

<code>ioctl</code> calls minimize the complexity of the kernel's system call interface. However, by providing a place for developers to "stash" bits and pieces of kernel programming interfaces, <code>ioctl</code> calls complicate the overall user-to-kernel API. A kernel that provides several hundred system calls may provide several thousand ioctl calls.

Though the interface to <code>ioctl</code> calls appears somewhat different from conventional system calls, there is in practice little difference between an <code>ioctl</code> call and a system call; an <code>ioctl</code> call is simply a system call with a different dispatching mechanism. Many of the arguments against expanding the kernel system call interface could therefore be applied to <code>ioctl</code> interfaces.

To application developers, system calls appear no different from application subroutines; they are simply function calls that take arguments and return values. The core libraries (e.g. <code>libc</code>) mask the complexity involved in invoking system calls. The same is true for <code>ioctl</code>s, where driver interfaces usually come with a user space library. (E.g. Mesa for the Direct Rendering Infrastructure of graphics drivers.)

Libpcap and libdnet are two examples of third-party wrapper Unix libraries designed to mask the complexity of <code>ioctl</code> interfaces, for packet capture and packet I/O, respectively.

Security

In traditional design, kernels resided in ring 0, separated from device drivers in ring 1, and in microkernels, also from each other. This has largely been given up due adding the same overhead of transitioning between rings to driver/kernel interfaces, that syscalls impose on kernel/user space interfaces. This has led to the difficult-in-practice requirement that all drivers, which now reside in ring 0 as well, must uphold the same level of security as the kernel core.

While the user-to-kernel interfaces of mainstream operating systems are often audited heavily for code flaws and security vulnerabilities prior to release, these audits typically focus on the well-documented system call interfaces. For instance, auditors might ensure that sensitive security calls such as changing user IDs are only available to administrative users.<br/> Because the handler for an <code>ioctl</code> call also resides directly in ring 0, the input from userspace should be validated just as carefully. As vulnerabilities in device drivers can be exploited by local users, e.g. by passing invalid buffers to <code>ioctl</code> calls.<br/> In practice, this is not the case. <code>ioctl</code> interfaces are larger, more diverse, and less well defined, and thus harder to audit than system calls. Furthermore, because <code>ioctl</code> calls can be provided by third-party developers, often after the core operating system has been released, <code>ioctl</code> call implementations may generally receive less scrutiny and thus harbor more vulnerabilities. Finally, some <code>ioctl</code> calls, particularly for third-party device drivers, can be entirely undocumented.

Varying fixes for this have been created, with the goal of achieving an equivalent to the former security, while keeping the gained speed.<br/> Win32 and Unix operating systems can protect a userspace device name from access by applications with specific access controls applied to the device. Security problems can arise when device driver developers do not apply appropriate access controls to the userspace accessible object.<br/> Some modern operating systems protect the kernel from hostile userspace code (such as applications that have been infected by buffer overflow exploits) using system call wrappers. System call wrappers implement role-based access control by specifying which system calls can be invoked by which applications; wrappers can, for instance, be used to "revoke" the right of a mail program to spawn other programs. <code>ioctl</code> interfaces complicate system call wrappers because there are large numbers of them, each taking different arguments, some of which may be required by normal programs. Furthermore, such solutions negate the gained reduction of overhead.

References

Further reading