man.bsd.lv manual page server

Manual Page Search Parameters

KAUTH(9) Kernel Developer's Manual KAUTH(9)

kauthkernel authorization framework

#include <sys/kauth.h>

kauth, or kernel authorization, is the subsystem managing all authorization requests inside the kernel. It manages user credentials and rights, and can be used to implement a system-wide security policy. It allows external modules to plug-in the authorization process.

kauth introduces some new concepts, namely “scopes” and “listeners”, which will be detailed together with other useful information for kernel developers in this document.

Some kauth types include the following:

kauth_cred_t
Representing credentials that can be associated with an object. Includes user- and group-ids (real, effective, and save) as well as group membership information.
kauth_scope_t
Describes a scope.
kauth_listener_t
Describes a listener.

kauth operates in various “scopes”, each scope holding a group of “listeners”.

Each listener works as a callback for when an authorization request within the scope is made. When such a request is made, all listeners on the scope are passed common information such as the credentials of the request context, an identifier for the requested operation, and possibly other information as well.

Every listener examines the passed information and returns its decision regarding the requested operation. It can either return:

The listener allows the operation.
The listener denies the operation.
The listener defers the decision to other listeners.

For an operation to be allowed, at least one listener has to return KAUTH_RESULT_ALLOW while no other listener returned KAUTH_RESULT_DENY.

Scopes manage listeners that operate in the same aspect of the system.

kauth exports a KPI that allows developers both of NetBSD and third-party products to authorize requests, access and modify credentials, create and remove scopes and listeners, and perform other miscellaneous operations on credentials.

kauth provides a single authorization request routine, which all authorization requests go through. This routine dispatches the request to the listeners of the appropriate scope, together with four optional user-data variables, and returns the augmented result.

It is declared as

int (kauth_scope_t scope, kauth_cred_t cred, kauth_action_t op, void *arg0, void *arg1, void *arg2, void *arg3)

An authorization request can return one of two possible values:

(zero)
indicates success; operation is allowed.
indicates failure; operation is denied. See errno(2).

Each scope has its own authorization wrapper, to make it easy to call from various places by eliminating the need to specify the scope and/or cast values. The authorization wrappers are detailed in each scope's section.

() has several special cases, when it will always allow the request. These are for when the request is issued by the kernel itself (indicated by the credentials being either NOCRED or FSCRED), or when there was no definitive decision from any of the listeners (i.e., it was not explicitly allowed or denied) and no security model was loaded.

The generic scope, “org.netbsd.kauth.generic”, manages generic authorization requests in the kernel.

The authorization wrapper for this scope is declared as

int (kauth_cred_t cred, kauth_action_t op, void *arg0)

The following operations are available for this scope:

Checks whether the credentials belong to the super-user.

Using this request is strongly discouraged and should only be done as a temporary place-holder, as it is breaking the separation between the interface for authorization requests from the back-end implementation.

The system scope, “org.netbsd.kauth.system”, manages authorization requests affecting the entire system.

The authorization wrapper for this scope is declared as

int (kauth_cred_t cred, kauth_action_t op, enum kauth_system_req req, void *arg1, void *arg2, void *arg3)

The following requests are available for this scope:

Check if enabling/disabling accounting allowed.
req can be any of the following:
Check if calling chroot(2) is allowed.
Check if calling fchroot(2) is allowed.
Check CPU-manipulation access.

req can be any of the following:

Set CPU state, including setting it online or offline.
This request concentrates several debugging-related operations.
Check if operations on the device mapper dm(4) device are allowed.
Check if file handle operations allowed.
Check if starting, stopping, enabling, or disabling extended attributes is allowed. arg1 is a struct mount * of the mount-point on which the operation is performed.
Check if setting up a file system snapshot is allowed. arg1 is a struct mount * of the mount-point of which the snapshot is taken, and arg2 is a struct vnode * of the vnode where the snapshot is expected to be.
Check if file system quota operations are allowed.

arg1 is a struct mount * describing the file system mount in question. req can be one of the following:

Check if retrieving quota information is allowed.

arg2 is a uid_t with the user-id of the user whose quota information is to be retrieved.

Check if turning quota on/off is allowed.
Check if managing the quota by setting the quota/quota use is allowed.

arg2 is a uid_t with the user-id of the user whose quota/quota use is to be set.

Check if bypassing the quota (not enforcing it) is allowed.
Check if using the file system reserved space is allowed.
Check if LFS-related operations are allowed. req can be one of the following:
Check if calling lfs_markv(2) is allowed.
Check if calling lfs_bmapv(2) is allowed.
Check if calling lfs_segclean(2) is allowed.
Check if calling lfs_segwait(2) is allowed.
Check if operations on LFS through fcntl(2) are allowed.
Check if changing the status of memory mapping of virtual address zero is allowed.
Check if a module request is allowed.

arg1 is the command.

Check if creating devices is allowed.
Check if mount-related operations are allowed.

req can be any of the following:

Check if mounting a device is allowed. arg1 is a vnode_t * of the device, arg2 is a struct mount * with the mount-point, and arg3 is a mode_t with the desired access mode.
Check if retrieving information about a mount is allowed. arg1 is a struct mount * with the mount structure in question, arg2 is a void * with file system specific data, if any.
Check if mounting a new file system is allowed.

arg1 is the struct vnode * on which the file system is to be mounted, arg2 is an int with the mount flags, and arg3 is a void * with file system specific data, if any.

Checks if unmounting a file system is allowed.

arg1 is a struct mount * with the mount in question.

Checks if updating an existing mount is allowed.

arg1 is the struct mount * of the existing mount, arg2 is an int with the new mount flags, and arg3 is a void * with file system specific data, if any.

Check if mounting the user and group id remapping file system. See mount_umap(8).
Check if bypassing permissions on a message queue object are allowed. arg1 is a mqueue_t * describing the message queue.
Check processor-set manipulation.

req can be any of the following:

Change processor-set processor assignment.
Bind an LWP to a processor-set.
Create a processor-set.
Destroy a processor-set.
Check if rebooting is allowed.
Check if changing coredump settings for set-id processes is allowed.
Check if access to a kernel semaphore is allowed. arg1 is a ksem_t * describing the semaphore.
Check if privileged swapctl(2) requests are allowed.
This requests operations related to sysctl(9). req indicates the specific request and can be one of the following:
Check if adding a sysctl(9) node is allowed.
Check if deleting a sysctl(9) node is allowed.
Check if adding description to a sysctl(9) node is allowed.
Check if modifying a sysctl(9) node variable that doesn't have a custom sysctl helper function is allowed.

This request might be deprecated in the future.

Check if accessing private sysctl(9) nodes is allowed.
Check SysV IPC related operations. req indicates the specific request and can be one of the following:
Check if bypassing a SysV IPC object's permissions is allowed. arg1 is a struct ipc_perm * with the object's permissions and arg2 is a mode_t indicating the requested access mode.
Check if shared memory locking is allowed.
Check if shared memory unlocking is allowed.
Check if oversizing a message queue is allowed. arg1 is a msglen_t indicating the size of the message buffer, and arg2 is a msglen_t indicating the size of the message queue.
This request groups time-related operations. req can be any of the following:
Check if changing the time using adjtime(2) is allowed.
Check if setting the time using ntp_adjtime(2) is allowed.
Check if changing the time (usually via settimeofday(2)) is allowed.

arg1 is a struct timespec * with the new time, arg2 is a struct timeval * with the delta from the current time, arg3 is a bool indicating whether the caller is a device context (e.g. /dev/clockctl) or not.

Check if changing the RTC offset is allowed.
Check if manipulating timecounters is allowed.
Check if operations on the veriexec(8) subsystem are allowed. req can be one of the following:
Check if access to the veriexec(8) subsystem is allowed.
Check if modifications to the state of veriexec(8) are allowed.

The process scope, “org.netbsd.kauth.process”, manages authorization requests related to processes in the system.

The authorization wrapper for this scope is declared as

int (kauth_cred_t cred, kauth_action_t op, struct proc *p, void *arg1, void *arg2, void *arg3)

The following operations are available for this scope:

Checks whether an object with one set of credentials can ktrace(1) another process p, possibly with a different set of credentials.

If arg1 is KAUTH_REQ_PROCESS_KTRACE_PERSISTENT, this checks if persistent tracing can be done. Persistent tracing maintains the trace across a set-user-id/set-group-id exec(3), and normally requires privileged credentials.

Checks whether object with passed credentials can use to access process p.

arg1 is the struct pfsnode * for the target element in the target process, and arg2 is the access type, which can be either KAUTH_REQ_PROCESS_PROCFS_READ, KAUTH_REQ_PROCESS_PROCFS_RW, or KAUTH_REQ_PROCESS_PROCFS_WRITE, indicating , , , or access respectively.

Checks whether object with passed credentials can use ptrace(2) to access process p.

arg1 is the ptrace(2) command.

Checks whether an object with one set of credentials can access information about another process, possibly with a different set of credentials.

arg1 indicates the class of information being viewed, and can be either of KAUTH_REQ_PROCESS_CANSEE_ARGS, KAUTH_REQ_PROCESS_CANSEE_ENTRY, KAUTH_REQ_PROCESS_CANSEE_ENV, or KAUTH_REQ_PROCESS_CANSEE_OPENFILES.

Checks whether viewing the scheduler affinity is allowed.
Checks whether setting the scheduler affinity is allowed.
Checks whether viewing the scheduler policy and parameters is allowed.
Checks whether modifying the scheduler policy and parameters is allowed.
Checks whether an object with one set of credentials can post signals to another process.

p is the process the signal is being posted to, and arg1 is the signal number.

Controls access to process corename.

arg1 can be KAUTH_REQ_PROCESS_CORENAME_GET or KAUTH_REQ_PROCESS_CORENAME_SET, indicating access to read or write the process' corename, respectively.

When modifying the corename, arg2 holds the new corename to be used.

Checks if the process can fork. arg1 is an int indicating how many processes exist on the system at the time of the check.
Checks whether setting a process kevent(2) filter is allowed.
Checks whether the value of p can be changed to arg1.
Controls access to process resource limits.

arg1 can be KAUTH_REQ_PROCESS_RLIMIT_GET or KAUTH_REQ_PROCESS_RLIMIT_SET, indicating access to read or write the process' resource limits, respectively, or KAUTH_REQ_PROCESS_RLIMIT_BYPASS to check if the limit enforcement can be bypassed.

When modifying resource limits, arg2 is the new value to be used and arg3 indicates which resource limit is to be modified.

Check if changing the user- or group-ids, groups, or login-name for p is allowed.
Check if setting the stop flags for exec(3), exit(3), and fork(2) is allowed.

arg1 indicates the flag, and can be either P_STOPEXEC, P_STOPEXIT, or P_STOPFORK respectively.

The network scope, “org.netbsd.kauth.network”, manages networking-related authorization requests in the kernel.

The authorization wrapper for this scope is declared as

int (kauth_cred_t cred, kauth_action_t op, enum kauth_network_req req, void *arg1, void *arg2, void *arg3)

The following operations are available for this scope:

Checks if an ALTQ operation is allowed.

req indicates the ALTQ subsystem in question, and can be one of the following:

 
 
 
 
 
 
 
 
 
 
 
 
Checks if a bind(2) request is allowed.

req allows to indicate the type of the request to structure listeners and callers easier. Supported request types:

Checks if binding to a non-privileged/reserved port is allowed.
Checks if binding to a privileged/reserved port is allowed.
Checks if firewall-related operations are allowed.

req indicates the sub-action, and can be one of the following:

Modification of packet filtering rules.
Modification of NAT rules.
Checks if network interface-related operations are allowed.

arg1 is (optionally) the struct ifnet * associated with the interface. arg2 is (optionally) an int describing the interface-specific operation. arg3 is (optionally) a pointer to the interface-specific request structure. req indicates the sub-action, and can be one of the following:

Check if retrieving information from the device is allowed.
Check if retrieving privileged information from the device is allowed.
Check if setting parameters on the device is allowed.
Check if setting privileged parameters on the device is allowed.
Check if manipulating the firmware on a network interface device is allowed.

Note that unless the struct ifnet * for the interface was passed in arg1, there's no way to tell what structure arg3 is.

Check if operations performed on the bridge(4) network interface are allowed.

req can be one of the following:

Check if getting privileges parameters is allowed.
Check if setting privileges parameters is allowed.
Checks if operations performed on the ppp(4) network interface are allowed.

req can be one of the following:

Checks if adding and enabling a ppp(4) interface to the system is allowed.
Check if operations performed on a PVC device (e.g. en(4)) are allowed. req can be one of the following:
Check if adding a PVC device is allowed.
Checks if operations performed on the sl(4) network interface are allowed.

req can be one of the following:

Checks if adding and enabling a sl(4) interface to the system is allowed.
Checks if operations performed on the strip(4) network interface are allowed.

req can be one of the following:

Check if adding and enabling a strip(4) interface to the system is allowed.
Checks if operations performed on the tun(4) network interface are allowed.

req can be one of the following:

Checks if adding and enabling a tun(4) interface to the system is allowed.
Check if operations related to ipsec(4) connections are allowed. req can be one of the following:
Check if bypassing ipsec(4) policy is allowed.
Check if IPv6-specific operations are allowed. req can be one of the following:
Check if setting hop-by-hop packet options is allowed.
Check if joining a multicast network is allowed.
Checks whether status of forwarding of source-routed packets can be modified or not.
Check if an NFS related operation is allowed.

req can be any of the following:

Check if modifying the NFS export table is allowed.
Check if access to the NFS nfssvc(2) syscall is allowed.
Checks if a routing-related request is allowed.

arg1 is the struct rt_msghdr * for the request.

Check if operations related to SMB are allowed.

req can be one of the following:

Check if accessing an SMB share is allowed.

arg1 is a struct smb_share * describing the SMB share, and arg2 is a mode_t with the desired access mode.

Check if creating an SMB share is allowed.

arg1 is a struct smb_sharespec * describing the share to be created.

Check if accessing an SMB VC is allowed.

arg1 is a struct smb_vc * describing the SMB VC, and arg2 is a mode_t with the desired access mode.

Check if creating an SMB VC is allowed.

arg1 is a struct smb_vcspec * describing the VC to be created.

Checks if a socket related operation is allowed.

req allows to indicate the type of the request to structure listeners and callers easier. Supported request types:

Checks if opening a raw socket is allowed.
Checks if opening a socket is allowed. arg1, arg2, and arg3 are all int parameters describing the domain, socket type, and protocol, respectively.
Checks if looking at the socket passed is allowed.

arg1 is a struct socket * describing the socket.

Checks if a connection can be dropped.

arg1 is a struct socket * describing the socket.

Checks if setting privileged socket options is allowed.

arg1 is a struct socket * describing the socket, arg2 is a u_long describing the socket option.

The machine-dependent (machdep) scope, “org.netbsd.kauth.machdep”, manages machine-dependent authorization requests in the kernel.

The authorization wrapper for this scope is declared as

int (kauth_cred_t cred, kauth_action_t op, void *arg0, void *arg1, void *arg2, void *arg3)

The actions on this scope provide a set that may or may not affect all platforms. Below is a list of available actions, along with which platforms are affected by each.

Request to flush the whole CPU cache. Affects Linux emulation.
Request to apply a CPU microcode to a CPU. This is related to , see options(4). Affects i386 and xen.
Request to get the I/O permission level. Affects amd64, i386, xen.
Request to set the I/O permission level. Affects amd64, i386, xen.
Request to set the I/O privilege level. Affects amd64, i386, xen.
Request to get the LDT (local descriptor table). Affects amd64, i386, xen.
Request to set the LDT (local descriptor table). Affects amd64, i386, xen.
Request to get the MTRR (memory type range registers). Affects amd64, i386, xen.
Request to set the MTRR (memory type range registers). Affects amd64, i386, xen.
Request to access (read/write) the NVRAM. Affects i386.
Request to start or stop the pxg(4) CPU. arg0 is true or false, respectively. Affects .
Request to access unmanaged memory. Affects , amd64, , i386, , , , xen.

The device scope, “org.netbsd.kauth.device”, manages authorization requests related to devices on the system. Devices can be, for example, terminals, tape drives, Bluetooth accessories, and any other hardware. Network devices specifically are handled by the scope.

In addition to the standard authorization wrapper:

int (kauth_cred_t cred, kauth_action_t op, void *arg0, void *arg1, void *arg2, void *arg3)

this scope provides authorization wrappers for various device types.

int (kauth_cred_t cred, kauth_action_t op, struct tty *tty)

Authorizes requests for on the system. The third argument, tty, is the terminal device in question. It is passed to the listener as arg0. The second argument, op, is the action and can be one of the following:

Open the terminal device pointed to by tty.
Set privileged settings on the terminal device pointed to by tty.
Use the “TIOCSTI” device ioctl(2), allowing to inject characters into the terminal buffer, simulating terminal input.
Control the virtual console. tty is the current console tty(4).

int (kauth_cred_t cred, enum kauth_device_req req, struct vnode *vp)

Authorizes requests for , usually disk devices, but also direct memory access, on the system.

It passes KAUTH_DEVICE_RAWIO_SPEC as the action to the listener, and accepts two arguments. req, passed to the listener as arg0, is access requested, and can be one of KAUTH_REQ_DEVICE_RAWIO_SPEC_READ, KAUTH_REQ_DEVICE_RAWIO_SPEC_WRITE, or KAUTH_REQ_DEVICE_RAWIO_SPEC_RW, representing read, write, or both read/write access respectively. vp is the vnode of the special file in question, and is passed to the listener as arg1.

Keep in mind that it is the responsibility of the security model developer to check whether the underlying device is a disk or the system memory, using ():

if ((vp->v_type == VCHR) &&
    iskmemdev(vp->v_un.vu_specinfo->si_rdev))
	/* system memory access */

int (kauth_cred_t cred, dev_t dev, u_long mode, void *data)

Authorizes hardware requests, or user commands passed directly to the hardware. These have the potential of resulting in direct disk and/or memory access.

It passes KAUTH_DEVICE_RAWIO_PASSTHRU as the action to the listener, and accepts three arguments. dev, passed as arg1 to the listener, is the device for which the request is made. mode, passed as arg0 to the listener, is a generic representation of the access mode requested. It can be one or more (binary-OR'd) of the following:

KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READ
 
KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF
 
KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITE
 
KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF
 

data, passed as arg2 to the listener, is device-specific data that may be associated with the request.

Authorizing actions relevant to Bluetooth devices is done using the standard authorization wrapper, with the following actions:

KAUTH_DEVICE_BLUETOOTH_BCSP
Check if operations on a bcsp(4) device are allowed.

arg0 is an enum kauth_device_req with one of the following values:

Check if adding and enabling a bcsp(4) device is allowed.
KAUTH_DEVICE_BLUETOOTH_BTUART
Check if operations on a btuart(4) device are allowed.

arg0 is an enum kauth_device_req with one of the following values:

Check if adding and enabling a btuart(4) device is allowed.
KAUTH_DEVICE_BLUETOOTH_RECV
Check if a packet can be received from the device.

arg0 is the packet type. For HCI_CMD_PKT packets, arg1 is the opcode, for HCI_EVENT_PKT packets, arg1 is the event ID, and for HCI_ACLDATA_PKT or HCI_SCODATA_PKT packets, arg1 is the connection handle.

KAUTH_DEVICE_BLUETOOTH_SEND
Check if a packet can be sent to the device.

arg0 is a struct hci_unit * describing the HCI unit, arg1 is a hci_cmd_hdr_t * describing the packet header.

KAUTH_DEVICE_BLUETOOTH_SETPRIV
Check if privileged settings can be changed.

arg0 is a struct hci_unit * describing the HCI unit, arg1 is a struct btreq * describing the request, and arg2 is a u_long describing the command.

Authorization actions relevant to the kernel random device, rnd(4), is done using the standard authorization wrapper, with the following actions:

KAUTH_DEVICE_RND_ADDDATA
Check if adding data to the entropy pool is allowed.
KAUTH_DEVICE_RND_GETPRIV
Check if privileged settings and information can be retrieved.
KAUTH_DEVICE_RND_SETPRIV
Check if privileged settings can be changed.

Authorization actions relevant to wscons(4) are done using the standard authorization wrapper, with the following actions:

KAUTH_DEVICE_WSCONS_KEYBOARD_BELL
Check if setting the default bell is allowed.
KAUTH_DEVICE_WSCONS_KEYBOARD_KEYREPEAT
Check if setting the default key-repeat is allowed.

The vnode scope, “org.netbsd.kauth.vnode”, authorizes operations made on vnodes representing file system objects.

The authorization wrapper for this scope is declared as

int (kauth_cred_t cred, kauth_action_t action, vnode_t *vp, vnode_t *dvp, int fs_decision)

This scope is heavily used in file system code and can potentially affect system-wide performance. Therefore, there are several things developers should know when using it.

First, the action parameter is a bit-mask and multiple actions can be binary-OR'd and authorized in a single call. Two helper functions help generate the action value for a couple of common cases: translating file system access to a kauth action and checking access to a vnode.

The first, (mode_t access_mode), and returns a kauth_action_t representing the desired access modes. Another function, (mode_t access_mode, enum vtype v_type, mode_t file_mode), returns a kauth_action_t suitable for use in many file system access(2) implementations. It calls the aforementioned kauth_mode_to_action(), but before returning also adds the KAUTH_VNODE_IS_EXEC flag if needed. See below for the meaning of this flag and how its necessity is determined.

Second, it is recommended to be very careful with adding listeners on this scope. A special parameter, fs_decision, allows different file systems to instrument different policies without adding their own listener. This parameter is special because it also serves as a fall-back decision when no secmodel(9) is present to prevent a fail-open scenario. It can take either an errno(2) value or “KAUTH_VNODE_REMOTEFS”, indicating that the file system on which the authorization is made is remote and cannot provide us with a fall-back decision. In this case, kauth can only short-circuit the request but the file system will have the last word if there is no definitive allow or deny decision.

The value of fs_decision can be hard-coded or determined by calling an internal function implementing a policy. For the latter case, genfs(9) provides a set of helper functions that implement common policies that file systems can use. The calling convention is as follows:

int error;

error = kauth_authorize_vnode(..., genfs_can_foo(...));

Actions on the vnode scope are of two types: operations and flags. An operation is similar in concept to actions on other scopes in the sense that it represents an operation desired by the caller. A flag is an indicator of additional information about the vnode that a file system can set in order to allow the listener to make a more informed decision.

Actions include the following:

KAUTH_VNODE_READ_DATA
Read file data.
KAUTH_VNODE_LIST_DIRECTORY
Read directory listing. Identical to the above.
KAUTH_VNODE_WRITE_DATA
Write file data.
KAUTH_VNODE_ADD_FILE
Add a file to a directory. Identical to the above.
KAUTH_VNODE_EXECUTE
Execute a file.
KAUTH_VNODE_SEARCH
Search (enter) a directory. Identical to the above.
KAUTH_VNODE_DELETE
Delete a file.
KAUTH_VNODE_APPEND_DATA
Append data to a file.
KAUTH_VNODE_ADD_SUBDIRECTORY
Add a subdirectory to a directory. Identical to the above.
KAUTH_VNODE_READ_TIMES
Read the created, last accessed, and last modified times of a file.
KAUTH_VNODE_WRITE_TIMES
Modify the created, last accessed, or last modified times of a file.
KAUTH_VNODE_READ_FLAGS
Read file flags.
KAUTH_VNODE_WRITE_FLAGS
Modify file flags.
KAUTH_VNODE_READ_SYSFLAGS
Read file system flags.
KAUTH_VNODE_WRITE_SYSFLAGS
Modify file system flags.
KAUTH_VNODE_RENAME
Rename a file.
KAUTH_VNODE_CHANGE_OWNERSHIP
Change ownership of a file.
KAUTH_VNODE_READ_SECURITY
Read the permissions of a file.
KAUTH_VNODE_WRITE_SECURITY
Change the permissions of a file, for example by using chmod(2).
KAUTH_VNODE_READ_ATTRIBUTES
Read attributes of a file.
KAUTH_VNODE_WRITE_ATTRIBUTES
Modify attributes of a file.
KAUTH_VNODE_READ_EXTATTRIBUTES
Read extended attributes of a file.
KAUTH_VNODE_WRITE_EXTATTRIBUTES
Modify extended attributes of a file.
KAUTH_VNODE_RETAIN_SUID
Check if retaining the set-user-id bit on files after chown(2) is allowed.
KAUTH_VNODE_RETAIN_SGID
Check if retaining the set-group-id bit on files after chown(2) is allowed.
KAUTH_VNODE_REVOKE
Revoke a file.

Flags include the following:

KAUTH_VNODE_IS_EXEC
The vnode is executable.

The macro () can be used to help determine if this flag should be set. This macro determines a file system object to be executable if it is a directory (in which case we say it is searchable) or if it has at least one executable bit set in its mode.

Setting this flag helps a listener know that a vnode is executable and is used in implementing privileged access to files and directories while maintaining semantics that prevent execution until a file is marked as an executable. An example for using this in listener code is:

if (privileged) {
	/* Always allow read/write; execute only if executable. */
	if ((action & KAUTH_VNODE_EXECUTE) == 0 ||
	    (action & KAUTH_VNODE_IS_EXEC))
		result = KAUTH_RESULT_ALLOW;
}

Finally, the vnode scope authorization wrapper returns EACCES in case of an error, to maintain file system semantics. File systems can override this value if needed.

KAUTH_VNODE_HAS_SYSFLAGS
The file system object represented by the vnode has system flags set.
KAUTH_VNODE_ACCESS
The authorization is advisory only and no actual operation is to be performed. This is not implemented.

The credentials scope, “org.netbsd.kauth.cred”, is a special scope used internally by the kauth framework to provide hooking to credential-related operations.

It is a “notify-only” scope, allowing hooking operations such as initialization of new credentials, credential inheritance during a fork, and copying and freeing of credentials. The main purpose for this scope is to give a security model a way to control the aforementioned operations, especially in cases where the credentials hold security model-private data.

Notifications are made using the following function, which is internal to kauth:

int (kauth_cred_t cred, kauth_action_t action, void *arg0, void *arg1)

With the following actions:

The credentials are being copied. cred are the credentials of the lwp context doing the copy, and arg0 and arg1 are both kauth_cred_t representing the “from” and “to” credentials, respectively.
The credentials are being inherited from a parent to a child process during a fork.

cred are the credentials of the lwp context doing the fork, and arg0 and arg1 are both struct proc * of the parent and child processes, respectively.

The credentials in cred belong to a process whose root directory is changed through () (see vfs(9) ).

Arg0 is the new struct cwdinfo * of the process.

The credentials in cred are being freed.
The credentials in cred are being initialized.

Since this is a notify-only scope, all listeners are required to return KAUTH_RESULT_ALLOW.

kauth has a variety of accessor and mutator routines to handle kauth_cred_t objects.

The following routines can be used to access and modify the user- and group-ids in a kauth_cred_t:

uid_t (kauth_cred_t cred)
Returns the real user-id from cred.
uid_t (kauth_cred_t cred)
Returns the effective user-id from cred.
uid_t (kauth_cred_t cred)
Returns the saved user-id from cred.
void (kauth_cred_t cred, uid_t uid)
Sets the real user-id in cred to uid.
void (kauth_cred_t cred, uid_t uid)
Sets the effective user-id in cred to uid.
void (kauth_cred_t cred, uid_t uid)
Sets the saved user-id in cred to uid.
gid_t (kauth_cred_t cred)
Returns the real group-id from cred.
gid_t (kauth_cred_t cred)
Returns the effective group-id from cred.
gid_t (kauth_cred_t cred)
Returns the saved group-id from cred.
void (kauth_cred_t cred, gid_t gid)
Sets the real group-id in cred to gid.
void (kauth_cred_t cred, gid_t gid)
Sets the effective group-id in cred to gid.
void (kauth_cred_t cred, gid_t gid)
Sets the saved group-id in cred to gid.
u_int (kauth_cred_t cred)
Return the reference count for cred.

The following routines can be used to access and modify the group list in a kauth_cred_t:

int (kauth_cred_t cred, gid_t gid, int *resultp)
Checks if the group-id gid is a member in the group list of cred.

If it is, resultp will be set to one, otherwise, to zero.

The return value is an error code, or zero for success.

u_int (kauth_cred_t cred)
Return the number of groups in the group list of cred.
gid_t (kauth_cred_t cred, u_int idx)
Return the group-id of the group at index idx in the group list of cred.
int (kauth_cred_t cred, const gid_t *groups, size_t ngroups, uid_t gmuid, enum uio_seg seg)
Copy ngroups groups from array pointed to by groups to the group list in cred, adjusting the number of groups in cred appropriately. seg should be either UIO_USERSPACE or UIO_SYSSPACE indicating whether groups is a user or kernel space address.

Any groups remaining will be set to an invalid value.

gmuid is unused for now, and to maintain interface compatibility with the Darwin KPI.

The return value is an error code, or zero for success.

int (kauth_cred_t cred, gid_t *groups, size_t ngroups, enum uio_seg seg)
Copy ngroups groups from the group list in cred to the buffer pointed to by groups. seg should be either UIO_USERSPACE or UIO_SYSSPACE indicating whether groups is a user or kernel space address.

The return value is an error code, or zero for success.

kauth provides an interface to allow attaching security-model private data to credentials.

The use of this interface has two parts that can be divided to direct and indirect control of the private-data. Directly controlling the private data is done by using the below routines, while the indirect control is often dictated by events such as process fork, and is handled by listening on the credentials scope (see above).

Attaching private data to credentials works by registering a key to serve as a unique identifier, distinguishing various sets of private data that may be associated with the credentials. Registering, and deregistering, a key is done by using these routines:

int (secmodel_t sm, kauth_key_t *keyp)
Register new key for private data for security model sm. keyp will be used to return the key to be used in further calls.

The function returns 0 on success and an error code (see errno(2)) on failure.

int (kauth_key_t key)
Deregister private data key key.

Once registered, private data may be manipulated by the following routines:

void (kauth_cred_t cred, kauth_key_t key, void *data)
Set private data for key in cred to be data.
void * kauth_cred_getdata(kauth_cred_t cred, kauth_key_t key)
Retrieve private data for key in cred.

Note that it is required to use the above routines every time the private data is changed, i.e., using () and later modifying the private data should be accompanied by a call to kauth_cred_setdata() with the “new” private data.

kauth provides an interface for handling shared credentials.

When a kauth_cred_t is first allocated, its reference count is set to 1. However, with time, its reference count can grow as more objects (processes, LWPs, files, etc.) reference it.

The following routines are available for managing credentials reference counting:

void (kauth_cred_t cred)
Increases reference count to cred by one.
void kauth_cred_free(kauth_cred_t cred)
Decreases the reference count to cred by one.

If the reference count dropped to zero, the memory used by cred will be freed.

Credential inheritance happens during a fork(2), and is handled by the following function:

void (struct proc *parent, struct proc *child)

When called, it references the parent's credentials from the child, and calls the credentials scope's hook with the KAUTH_CRED_FORK action to allow security model-specific handling of the inheritance to take place.

Data-structures for credentials, listeners, and scopes are allocated from memory pools managed by the pool(9) subsystem.

The kauth_cred_t objects have their own memory management routines:

kauth_cred_t (void)
Allocates a new kauth_cred_t, initializes its lock, and sets its reference count to one.

Sometimes it might be necessary to convert a kauth_cred_t to userland's view of credentials, a struct uucred, or vice versa.

The following routines are available for these cases:

void (kauth_cred_t cred, const struct uucred *uucred)
Convert userland's view of credentials to a kauth_cred_t.

This includes effective user- and group-ids, a number of groups, and a group list. The reference count is set to one.

Note that kauth will try to copy as many groups as can be held inside a kauth_cred_t.

void (struct uucred *uucred, const kauth_cred_t cred)
Convert kauth_cred_t to userland's view of credentials.

This includes effective user- and group-ids, a number of groups, and a group list.

Note that kauth will try to copy as many groups as can be held inside a struct uucred.

int (kauth_cred_t cred, struct uucred *uucred)
Compares cred with the userland credentials in uucred.

Common values that will be compared are effective user- and group-ids, and the group list.

Other routines provided by kauth are:

void (kauth_cred_t cred1, kauth_cred_t cred2)
Clone credentials from cred1 to cred2, except for the lock and reference count.
kauth_cred_t (kauth_cred_t cred)
Duplicate cred.

What this routine does is call () followed by a call to kauth_cred_clone().

kauth_cred_t (kauth_cred_t cred)
Works like kauth_cred_dup(), except for a few differences.

If cred already has a reference count of one, it will be returned. Otherwise, a new kauth_cred_t will be allocated and the credentials from cred will be cloned to it. Last, a call to () for cred will be done.

kauth_cred_t (void)
Return the credentials associated with the current LWP. This does not change the reference count of the resulting kauth_cred_t object.

kauth provides routines to manage the creation and deletion of scopes on the system.

Note that the built-in scopes, the “generic” scope and the “process” scope, can't be deleted.

kauth_scope_t (const char *id, kauth_scope_callback_t cb, void *cookie)
Register a new scope on the system. id is the name of the scope, usually in reverse DNS-like notation. For example, “org.netbsd.kauth.myscope”. cb is the default listener, to which authorization requests for this scope will be dispatched to. cookie is optional user-data that will be passed to all listeners during authorization on the scope.
void (kauth_scope_t scope)
Deregister scope from the scopes available on the system, and free the kauth_scope_t object scope.

Listeners in kauth are authorization callbacks that are called during an authorization request in the scope which they belong to.

When an authorization request is made, all listeners associated with a scope are called to allow, deny, or defer the request.

It is enough for one listener to deny the request in order for the request to be denied; but all listeners are called during an authorization process none-the-less. All listeners are required to allow the request for it to be granted, and in a case where all listeners defer the request — leaving the decision for other listeners — the request is denied.

The following KPI is provided for the management of listeners:

kauth_listener_t (const char *id, kauth_scope_callback_t cb, void *cookie)
Create a new listener on the scope with the id id, setting the default listener to cb. cookie is optional user-data that will be passed to the listener when called during an authorization request.
void (kauth_listener_t listener)
Removes listener from the scope which it belongs to, ensuring it won't be called again, and frees the kauth_listener_t object listener.

kauth provides no means for synchronization within listeners. It is the programmer's responsibility to make sure data used by the listener is properly locked during its use, as it can be accessed simultaneously from the same listener called multiple times. It is also the programmer's responsibility to do garbage collection after the listener, possibly freeing any allocated data it used.

The common method to do the above is by having a reference count to each listener. On entry to the listener, this reference count should be raised; on exit, lowered.

During the removal of a listener, first () should be called to make sure the listener code will not be entered in the future. Then, the code should wait (possibly sleeping) until the reference count drops to zero. When that happens, it is safe to do the final cleanup.

Listeners might sleep, so no locks can be held when calling an authorization wrapper.

Older code had no abstraction of the security model, so most privilege checks looked like this:

if (suser(cred, &acflag) == 0)
	/* allow privileged operation */

Using the new interface, you must ask for a specific privilege explicitly. For example, checking whether it is possible to open a socket would look something like this:

if (kauth_authorize_network(cred, KAUTH_NETWORK_SOCKET,
    KAUTH_REQ_NETWORK_SOCKET_OPEN, PF_INET, SOCK_STREAM,
    IPPROTO_TCP) == 0)
	/* allow opening the socket */

Note that the implications were also integrated into the kauth framework so you don't have to note anything special in the call to the authorization wrapper, but rather just have to make sure the security model handles the request as you expect it to.

To do that you can just grep(1) in the relevant security model directory and have a look at the code.

Although kauth provides a large set of both detailed and more or less generic requests, it might be needed eventually to introduce more scopes, actions, or requests.

Adding a new scope should happen only when an entire subsystem is introduced and it is assumed other parts of the kernel may want to interfere with its inner-workings. When a subsystem that has the potential of impacting the security of the system is introduced, existing security modules must be updated to also handle actions on the newly added scope.

New actions should be added when sets of operations not covered at all belong in an already existing scope.

Requests (or sub-actions) can be added as subsets of existing actions when an operation that belongs in an already covered area is introduced.

Note that all additions should include updates to this manual, the security models shipped with NetBSD, and the example skeleton security model.

secmodel(9)

The kernel authorization framework first appeared in Mac OS X 10.4.

The kernel authorization framework in NetBSD first appeared in NetBSD 4.0, and is a clean-room implementation based on Apple TN2127, available at http://developer.apple.com/technotes/tn2005/tn2127.html

As kauth in NetBSD is still under active development, it is likely that the ABI, and possibly the API, will differ between NetBSD versions. Developers are to take notice of this fact in order to avoid building code that expects one version of the ABI and running it in a system with a different one.

Elad Efrat <elad@NetBSD.org> implemented the kernel authorization framework in NetBSD.


Jason R. Thorpe <thorpej@NetBSD.org> provided guidance and answered questions about the Darwin implementation.

July 14, 2018 NetBSD-9.2