KAUTH(9)
๐ฆ KAUTH(9) โ kernel authorization framework ๐ฆ
Kernel Developer's Manual
๐ธ NAME ๐ธ
kauthโ kernel authorization framework
โจ SYNOPSIS โจ
#include <sys/kauth.h>
๐ DESCRIPTION ๐
Okay bestie, so kauth โ kernel authorization, iconic name โ is the subsystem that runs all authorization requests inside the kernel. It manages user credentials and rights, and it can be used to implement a system-wide security policy. It also lets external modules plug into the authorization process. No cap.
kauth introduces some new concepts, namely โscopesโ and โlistenersโ, which get detailed together with other useful information for kernel developers in this document. Periodt.
๐งธ Types ๐งธ
Some kauth types include the following:
kauth_cred_tโ represents 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.
๐ Terminology ๐
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:
KAUTH_RESULT_ALLOWโ the listener allows the operation.KAUTH_RESULT_DENYโ the listener denies the operation.KAUTH_RESULT_DEFERโ 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.
๐ Kernel Programming Interface ๐
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. Slay.
๐ฅ Authorization Requests ๐ฅ
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_authorize_action(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:
0(zero) โ indicates success; operation is allowed.EPERMโ indicates failure; operation is denied. Seeerrno(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.
kauth_authorize_action() 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.
๐ซถ Generic Scope ๐ซถ
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_authorize_generic(kauth_cred_t cred, kauth_action_t op, void *arg0)
The following operations are available for this scope:
KAUTH_GENERIC_ISSUSERโ 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. Like, don't make it your whole personality, bestie.
๐ System Scope ๐
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_authorize_system(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:
KAUTH_SYSTEM_ACCOUNTINGโ check if enabling/disabling accounting allowed.KAUTH_SYSTEM_CHROOTโreqcan be any of the following:KAUTH_REQ_SYSTEM_CHROOT_CHROOTโ check if callingchroot(2)is allowed.KAUTH_REQ_SYSTEM_CHROOT_FCHROOTโ check if callingfchroot(2)is allowed.
KAUTH_SYSTEM_CPUโ check CPU-manipulation access.reqcan be any of the following:KAUTH_REQ_SYSTEM_CPU_SETSTATEโ set CPU state, including setting it online or offline.
KAUTH_SYSTEM_DEBUGโ this request concentrates several debugging-related operations.KAUTH_SYSTEM_DEVMAPPERโ check if operations on the device mapperdm(4)device are allowed.KAUTH_SYSTEM_FILEHANDLEโ check if file handle operations allowed.KAUTH_SYSTEM_FS_EXTATTRโ check if starting, stopping, enabling, or disabling extended attributes is allowed.arg1is astruct mount *of the mount-point on which the operation is performed.KAUTH_SYSTEM_FS_SNAPSHOTโ check if setting up a file system snapshot is allowed.arg1is astruct mount *of the mount-point of which the snapshot is taken, andarg2is astruct vnode *of the vnode where the snapshot is expected to be.KAUTH_SYSTEM_FS_QUOTAโ 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:
KAUTH_REQ_SYSTEM_FS_QUOTA_GETโ check if retrieving quota information is allowed.arg2is auid_twith the user-id of the user whose quota information is to be retrieved.KAUTH_REQ_SYSTEM_FS_QUOTA_ONOFFโ check if turning quota on/off is allowed.KAUTH_REQ_SYSTEM_FS_QUOTA_MANAGEโ check if managing the quota by setting the quota/quota use is allowed.arg2is auid_twith the user-id of the user whose quota/quota use is to be set.KAUTH_REQ_SYSTEM_FS_QUOTA_NOLIMITโ check if bypassing the quota (not enforcing it) is allowed.
KAUTH_SYSTEM_FS_RESERVEDSPACEโ check if using the file system reserved space is allowed.KAUTH_SYSTEM_LFSโ check if LFS-related operations are allowed.reqcan be one of the following:KAUTH_REQ_SYSTEM_LFS_MARKVโ check if callinglfs_markv(2)is allowed.KAUTH_REQ_SYSTEM_LFS_BMAPVโ check if callinglfs_bmapv(2)is allowed.KAUTH_REQ_SYSTEM_LFS_SEGCLEANโ check if callinglfs_segclean(2)is allowed.KAUTH_REQ_SYSTEM_LFS_SEGWAITโ check if callinglfs_segwait(2)is allowed.KAUTH_REQ_SYSTEM_LFS_FCNTLโ check if operations on LFS throughfcntl(2)are allowed.
KAUTH_SYSTEM_MAP_VA_ZEROโ check if changing the status of memory mapping of virtual address zero is allowed.KAUTH_SYSTEM_MODULEโ check if a module request is allowed.arg1is the command.KAUTH_SYSTEM_MKNODโ check if creating devices is allowed.KAUTH_SYSTEM_MOUNTโ check if mount-related operations are allowed.reqcan be any of the following:KAUTH_REQ_SYSTEM_MOUNT_DEVICEโ check if mounting a device is allowed.arg1is avnode_t *of the device,arg2is astruct mount *with the mount-point, andarg3is amode_twith the desired access mode.KAUTH_REQ_SYSTEM_MOUNT_GETโ check if retrieving information about a mount is allowed.arg1is astruct mount *with the mount structure in question,arg2is avoid *with file system specific data, if any.KAUTH_REQ_SYSTEM_MOUNT_NEWโ check if mounting a new file system is allowed.arg1is thestruct vnode *on which the file system is to be mounted,arg2is anintwith the mount flags, andarg3is avoid *with file system specific data, if any.KAUTH_REQ_SYSTEM_MOUNT_UNMOUNTโ checks if unmounting a file system is allowed.arg1is astruct mount *with the mount in question.KAUTH_REQ_SYSTEM_MOUNT_UPDATEโ checks if updating an existing mount is allowed.arg1is thestruct mount *of the existing mount,arg2is anintwith the new mount flags, andarg3is avoid *with file system specific data, if any.KAUTH_REQ_SYSTEM_MOUNT_UMAPโ check if mounting the user and group id remapping file system. Seemount_umap(8).
KAUTH_SYSTEM_MQUEUEโ check if bypassing permissions on a message queue object are allowed.arg1is amqueue_t *describing the message queue.KAUTH_SYSTEM_PSETโ check processor-set manipulation.reqcan be any of the following:KAUTH_REQ_SYSTEM_PSET_ASSIGNโ change processor-set processor assignment.KAUTH_REQ_SYSTEM_PSET_BINDโ bind an LWP to a processor-set.KAUTH_REQ_SYSTEM_PSET_CREATEโ create a processor-set.KAUTH_REQ_SYSTEM_PSET_DESTROYโ destroy a processor-set.
KAUTH_SYSTEM_REBOOTโ check if rebooting is allowed.KAUTH_SYSTEM_SETIDCOREโ check if changing coredump settings for set-id processes is allowed.KAUTH_SYSTEM_SEMAPHOREโ check if access to a kernel semaphore is allowed.arg1is aksem_t *describing the semaphore.KAUTH_SYSTEM_SWAPCTLโ check if privilegedswapctl(2)requests are allowed.
KAUTH_SYSTEM_SYSCTL
okay so this one handles everything sysctl(9)-flavored ๐ โ req tells you which specific request it is, and it's one of these:
KAUTH_REQ_SYSTEM_SYSCTL_ADDโ vibe check on whether adding asysctl(9)node is allowed โจKAUTH_REQ_SYSTEM_SYSCTL_DELETEโ can we delete asysctl(9)node? that's the question ๐KAUTH_REQ_SYSTEM_SYSCTL_DESCโ is adding a description to asysctl(9)node allowed? ๐KAUTH_REQ_SYSTEM_SYSCTL_MODIFYโ checks if modifying asysctl(9)node variable is allowed when that node doesn't have a custom sysctl helper function. no helper? then this gatekeeper steps in ๐ซถKAUTH_REQ_SYSTEM_SYSCTL_PRVTโ is accessing privatesysctl(9)nodes allowed? ๐
heads up bestie: KAUTH_REQ_SYSTEM_SYSCTL_MODIFY might be deprecated in the future ๐ญ
KAUTH_SYSTEM_SYSVIPC
this one checks SysV IPC related operations. req = the specific request, and it's one of:
KAUTH_REQ_SYSTEM_SYSVIPC_BYPASSโ checks if bypassing a SysV IPC object's permissions is allowed.arg1is astruct ipc_perm *holding the object's permissions, andarg2is amode_tindicating the requested access mode ๐KAUTH_REQ_SYSTEM_SYSVIPC_SHM_LOCKโ is shared memory locking allowed? ๐KAUTH_REQ_SYSTEM_SYSVIPC_SHM_UNLOCKโ is shared memory unlocking allowed? ๐KAUTH_REQ_SYSTEM_SYSVIPC_MSGQ_OVERSIZEโ is oversizing a message queue allowed?arg1is amsglen_tindicating the size of the message buffer, andarg2is amsglen_tindicating the size of the message queue ๐ฌ
KAUTH_SYSTEM_TIME
the time-ops group chat โฐ โ req can be any of:
KAUTH_REQ_SYSTEM_TIME_ADJTIMEโ checks if changing the time usingadjtime(2)is allowed.KAUTH_REQ_SYSTEM_TIME_NTPADJTIMEโ checks if setting the time usingntp_adjtime(2)is allowed.KAUTH_REQ_SYSTEM_TIME_SYSTEMโ checks if changing the time (usually viasettimeofday(2)) is allowed.arg1is astruct timespec *with the new time,arg2is astruct timeval *with the delta from the current time, andarg3is aboolindicating whether the caller is a device context (e.g./dev/clockctl) or not. literally all the deets ๐KAUTH_REQ_SYSTEM_TIME_RTCOFFSETโ checks if changing the RTC offset is allowed.KAUTH_REQ_SYSTEM_TIME_TIMECOUNTERSโ checks if manipulating timecounters is allowed.
KAUTH_SYSTEM_VERIEXEC
checks whether operations on the veriexec(8) subsystem are allowed. req can be one of:
KAUTH_REQ_SYSTEM_VERIEXEC_ACCESSโ is access to theveriexec(8)subsystem allowed? ๐ฆKAUTH_REQ_SYSTEM_VERIEXEC_MODIFYโ are modifications to the state ofveriexec(8)allowed? โจ
๐ Process Scope ๐
the process scope, โorg.netbsd.kauth.processโ, manages all the authorization requests related to processes in the system. it's giving main character energy ngl ๐
the authorization wrapper for this scope is declared as:
int kauth_authorize_process(kauth_cred_t cred, kauth_action_t op, struct proc *p, void *arg1, void *arg2, void *arg3)
and here are all the operations available for this scope ๐
KAUTH_PROCESS_KTRACE
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. so yeah, fancy tracing = fancy creds ๐ฅ
KAUTH_PROCESS_PROCFS
checks whether the object with the passed credentials can use procfs 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 control, read, read-write, or write access respectively. yes bestie, the order in that sentence is exactly how it's written, don't come for me ๐ญ
KAUTH_PROCESS_PTRACE
checks whether the object with the passed credentials can use ptrace(2) to access process p. arg1 is the ptrace(2) command ๐
KAUTH_PROCESS_CANSEE
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.
KAUTH_PROCESS_SCHEDULER_GETAFFINITY
checks whether viewing the scheduler affinity is allowed ๐
KAUTH_PROCESS_SCHEDULER_SETAFFINITY
checks whether setting the scheduler affinity is allowed โจ
KAUTH_PROCESS_SCHEDULER_GETPARAM
checks whether viewing the scheduler policy and parameters is allowed ๐ธ
KAUTH_PROCESS_SCHEDULER_SETPARAM
checks whether modifying the scheduler policy and parameters is allowed ๐
KAUTH_PROCESS_SIGNAL
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.
KAUTH_PROCESS_CORENAME
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 ๐ฆ
KAUTH_PROCESS_FORK
checks if the process can fork. arg1 is an int indicating how many processes exist on the system at the time of the check ๐ด
KAUTH_PROCESS_KEVENT_FILTER
checks whether setting a process kevent(2) filter is allowed ๐ง
KAUTH_PROCESS_NICE
checks whether the nice value of p can be changed to arg1 ๐
KAUTH_PROCESS_RLIMIT
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.
KAUTH_PROCESS_SETID
check if changing the user- or group-ids, groups, or login-name for p is allowed ๐ชช
KAUTH_PROCESS_STOPFLAG
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.
๐ฆ Network Scope ๐ฆ
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_authorize_network(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 โญ
KAUTH_NETWORK_ALTQ
checks if an ALTQ operation is allowed ๐ฆ
req indicates the ALTQ subsystem in question, and can be one of the following:
KAUTH_REQ_NETWORK_ALTQ_AFMAP
KAUTH_REQ_NETWORK_ALTQ_BLUE
KAUTH_REQ_NETWORK_ALTQ_CBQ
KAUTH_REQ_NETWORK_ALTQ_CDNR
KAUTH_REQ_NETWORK_ALTQ_CONF
KAUTH_REQ_NETWORK_ALTQ_FIFOQ
KAUTH_REQ_NETWORK_ALTQ_HFSC
KAUTH_REQ_NETWORK_ALTQ_JOBS
KAUTH_REQ_NETWORK_ALTQ_PRIQ
KAUTH_REQ_NETWORK_ALTQ_RED
KAUTH_REQ_NETWORK_ALTQ_RIO
KAUTH_REQ_NETWORK_ALTQ_WFQ
KAUTH_NETWORK_BIND
checks if a bind(2) request is allowed ๐ชข
req lets you indicate the type of the request so structuring listeners and callers is easier. supported request types:
KAUTH_REQ_NETWORK_BIND_PORTโ checks if binding to a non-privileged/reserved port is allowed.KAUTH_REQ_NETWORK_BIND_PRIVPORTโ checks if binding to a privileged/reserved port is allowed. fancy port, fancy permission ๐
KAUTH_NETWORK_FIREWALL
checks if firewall-related operations are allowed ๐ฅ
req indicates the sub-action, and can be one of the following:
KAUTH_REQ_NETWORK_FIREWALL_FWโ modification of packet filtering rules.KAUTH_REQ_NETWORK_FIREWALL_NATโ modification of NAT rules.
KAUTH_NETWORK_INTERFACE
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:
KAUTH_REQ_NETWORK_INTERFACE_GETโ check if retrieving information from the device is allowed.KAUTH_REQ_NETWORK_INTERFACE_GETPRIVโ check if retrieving privileged information from the device is allowed. ๐KAUTH_REQ_NETWORK_INTERFACE_SETโ check if setting parameters on the device is allowed.KAUTH_REQ_NETWORK_INTERFACE_SETPRIVโ check if setting privileged parameters on the device is allowed. ๐ฅKAUTH_REQ_NETWORK_INTERFACE_FIRMWAREโ check if manipulating the firmware on a network interface device is allowed. ๐ง
ngl this one's important: unless thestruct ifnet *for the interface was passed inarg1, there's no way to tell what structurearg3is. so likeโฆ passarg1, bestie ๐ญ๐
๐ KAUTH_NETWORK_INTERFACE_BRIDGE
Okay so this one checks if operations on the bridge(4) network interface are allowed. Slay.
req can be one of these besties:
KAUTH_REQ_NETWORK_INTERFACE_BRIDGE_GETPRIVโ checks if getting privileges parameters is allowed.KAUTH_REQ_NETWORK_INTERFACE_BRIDGE_SETPRIVโ checks if setting privileges parameters is allowed.
๐ง KAUTH_NETWORK_INTERFACE_PPP
Checks if operations performed on the ppp(4) network interface are allowed.
req can be one of the following:
KAUTH_REQ_NETWORK_INTERFACE_PPP_ADDโ checks if adding and enabling appp(4)interface to the system is allowed.
๐ธ KAUTH_NETWORK_INTERFACE_PVC
Check if operations performed on a PVC device (e.g. en(4)) are allowed. req can be one of the following:
KAUTH_REQ_NETWORK_INTERFACE_PVC_ADDโ check if adding a PVC device is allowed.
๐ฆ KAUTH_NETWORK_INTERFACE_SLIP
Checks if operations performed on the sl(4) network interface are allowed.
req can be one of the following:
KAUTH_REQ_NETWORK_INTERFACE_SLIP_ADDโ checks if adding and enabling asl(4)interface to the system is allowed.
๐ KAUTH_NETWORK_INTERFACE_STRIP
Checks if operations performed on the strip(4) network interface are allowed.
req can be one of the following:
KAUTH_REQ_NETWORK_INTERFACE_STRIP_ADDโ check if adding and enabling astrip(4)interface to the system is allowed.
โญ KAUTH_NETWORK_INTERFACE_TUN
Checks if operations performed on the tun(4) network interface are allowed.
req can be one of the following:
KAUTH_REQ_NETWORK_INTERFACE_TUN_ADDโ checks if adding and enabling atun(4)interface to the system is allowed.
๐ KAUTH_NETWORK_IPSEC
Check if operations related to ipsec(4) connections are allowed. req can be one of the following:
KAUTH_REQ_NETWORK_IPSEC_BYPASSโ check if bypassingipsec(4)policy is allowed.
๐ KAUTH_NETWORK_IPV6
Check if IPv6-specific operations are allowed. req can be one of the following:
KAUTH_REQ_NETWORK_IPV6_HOPBYHOPโ check if setting hop-by-hop packet options is allowed.KAUTH_REQ_NETWORK_IPV6_JOIN_MULTICASTโ check if joining a multicast network is allowed.
๐ KAUTH_NETWORK_FORWSRCRT
Checks whether status of forwarding of source-routed packets can be modified or not. (And yes bestie, the constant really is spelled FORWSRCRT โ we do not fix it, we respect it. ๐ซถ)
๐ KAUTH_NETWORK_NFS
Check if an NFS related operation is allowed.
req can be any of the following:
KAUTH_REQ_NETWORK_NFS_EXPORTโ check if modifying the NFS export table is allowed.KAUTH_REQ_NETWORK_NFS_SVCโ check if access to the NFSnfssvc(2)syscall is allowed.
๐ KAUTH_NETWORK_ROUTE
Checks if a routing-related request is allowed.
arg1 is the struct rt_msghdr * for the request.
๐ง KAUTH_NETWORK_SMB
Check if operations related to SMB are allowed.
req can be one of the following:
KAUTH_REQ_NETWORK_SMB_SHARE_ACCESSโ check if accessing an SMB share is allowed.arg1is astruct smb_share *describing the SMB share, andarg2is amode_twith the desired access mode.KAUTH_REQ_NETWORK_SMB_SHARE_CREATEโ check if creating an SMB share is allowed.arg1is astruct smb_sharespec *describing the share to be created.KAUTH_REQ_NETWORK_SMB_VC_ACCESSโ check if accessing an SMB VC is allowed.arg1is astruct smb_vc *describing the SMB VC, andarg2is amode_twith the desired access mode.KAUTH_REQ_NETWORK_SMB_VC_CREATEโ check if creating an SMB VC is allowed.arg1is astruct smb_vcspec *describing the VC to be created.
๐ฅ KAUTH_NETWORK_SOCKET
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 are giving:
KAUTH_REQ_NETWORK_SOCKET_RAWSOCKโ checks if opening a raw socket is allowed.KAUTH_REQ_NETWORK_SOCKET_OPENโ checks if opening a socket is allowed.arg1,arg2, andarg3are allintparameters describing the domain, socket type, and protocol, respectively.KAUTH_REQ_NETWORK_SOCKET_CANSEEโ checks if looking at the socket passed is allowed.arg1is astruct socket *describing the socket.KAUTH_REQ_NETWORK_SOCKET_DROPโ checks if a connection can be dropped.arg1is astruct socket *describing the socket.KAUTH_REQ_NETWORK_SOCKET_SETPRIVโ checks if setting privileged socket options is allowed.arg1is astruct socket *describing the socket,arg2is au_longdescribing the socket option.
๐ฅ๏ธ Machine-dependent Scope โจ
The machine-dependent (machdep) scope, โorg.netbsd.kauth.machdepโ, manages machine-dependent authorization requests in the kernel. Very demure, very platform-specific.
The authorization wrapper for this scope is declared as:
int kauth_authorize_machdep(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 โ iykyk, don't assume your arch has your back. ๐
KAUTH_MACHDEP_CACHEFLUSHโ request to flush the whole CPU cache. Affects m68k Linux emulation.KAUTH_MACHDEP_CPU_UCODE_APPLYโ request to apply a CPU microcode to a CPU. This is related toCPU_UCODE, seeoptions(4). Affects i386 and xen.KAUTH_MACHDEP_IOPERM_GETโ request to get the I/O permission level. Affects amd64, i386, xen.KAUTH_MACHDEP_IOPERM_SETโ request to set the I/O permission level. Affects amd64, i386, xen.KAUTH_MACHDEP_IOPLโ request to set the I/O privilege level. Affects amd64, i386, xen.KAUTH_MACHDEP_LDT_GETโ request to get the LDT (local descriptor table). Affects amd64, i386, xen.KAUTH_MACHDEP_LDT_SETโ request to set the LDT (local descriptor table). Affects amd64, i386, xen.KAUTH_MACHDEP_MTRR_GETโ request to get the MTRR (memory type range registers). Affects amd64, i386, xen.KAUTH_MACHDEP_MTRR_SETโ request to set the MTRR (memory type range registers). Affects amd64, i386, xen.KAUTH_MACHDEP_NVRAMโ request to access (read/write) the NVRAM. Affects i386.KAUTH_MACHDEP_PXGโ request to start or stop thepxg(4)CPU.arg0is true or false, respectively. Affects pmax.KAUTH_MACHDEP_UNMANAGEDMEMโ request to access unmanaged memory. Affects alpha, amd64, arm, i386, powerpc, sh3, vax, x68k, xen.
๐ฎ Device Scope ๐
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 network scope โ different scope, different vibe.
In addition to the standard authorization wrapper:
int kauth_authorize_device(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. Obsessed, honestly.
int kauth_authorize_device_tty(kauth_cred_t cred, kauth_action_t op,
struct tty *tty)
Authorizes requests for terminal devices 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:
KAUTH_DEVICE_TTY_OPENโ open the terminal device pointed to bytty.KAUTH_DEVICE_TTY_PRIVSETโ set privileged settings on the terminal device pointed to bytty.KAUTH_DEVICE_TTY_STIโ use the โTIOCSTIโ deviceioctl(2), allowing to inject characters into the terminal buffer, simulating terminal input. (Yes, that one. It's giving "typing for you while you're not looking". ๐)KAUTH_DEVICE_TTY_VIRTUALโ control the virtual console.ttyis the current consoletty(4).
int kauth_authorize_device_spec(kauth_cred_t cred, enum kauth_device_req
req, struct vnode *vp)
Authorizes requests for special files, 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.
โ ๏ธ Bestie, 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 iskmemdev(). Do NOT skip this, fr fr:
if ((vp->v_type == VCHR) &&
iskmemdev(vp->v_un.vu_specinfo->si_rdev))
/* system memory access */
int kauth_authorize_device_passthru(kauth_cred_t cred, dev_t dev, u_long
mode, void *data)
Authorizes hardware passthru requests, or user commands passed directly to the hardware. These have the potential of resulting in direct disk and/or memory access. ๐จ Highkey important, no cap.
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.
๐ฆ Bluetooth Devices ๐ฆ
okay so authorizing stuff for Bluetooth devices? same standard authorization wrapper as always, just with these actions ๐
KAUTH_DEVICE_BLUETOOTH_BCSPโ check if operations on abcsp(4)device are even allowed, bestie.
arg0 is an enum kauth_device_req and it's one of these:
KAUTH_REQ_DEVICE_BLUETOOTH_BCSP_ADDโ checks if adding and enabling abcsp(4)device is allowed โจ
KAUTH_DEVICE_BLUETOOTH_BTUARTโ check if operations on abtuart(4)device are allowed.
arg0 is an enum kauth_device_req, one of:
KAUTH_REQ_DEVICE_BLUETOOTH_BTUART_ADDโ checks if adding and enabling abtuart(4)device is allowed ๐
KAUTH_DEVICE_BLUETOOTH_RECVโ can we even receive a packet 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. iykyk ๐
KAUTH_DEVICE_BLUETOOTH_SENDโ can we send a packet to the device?
arg0 is a struct hci_unit * describing the HCI unit, and arg1 is a hci_cmd_hdr_t * describing the packet header.
KAUTH_DEVICE_BLUETOOTH_SETPRIVโ can privileged settings 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.
๐ฒ Kernel random device ๐ฒ
authorization actions for the kernel random device, rnd(4) โ again, standard authorization wrapper, with these 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 retrievedKAUTH_DEVICE_RND_SETPRIVโ check if privileged settings can be changed
โจ๏ธ Wscons devices โจ๏ธ
authorization actions relevant to wscons(4), standard wrapper, these 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
๐๏ธ Vnode Scope ๐๏ธ
The vnode scope, "org.netbsd.kauth.vnode", authorizes operations made on vnodes representing file system objects. literally its whole personality ๐
The authorization wrapper for this scope is declared as:
int kauth_authorize_vnode(kauth_cred_t cred, kauth_action_t action,
vnode_t *vp, vnode_t *dvp, int fs_decision)
okay so warning time ๐จ this scope is heavily used in file system code, which means it can potentially affect system-wide performance. so there are a few things developers should know before using it, no cap.
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 one, kauth_mode_to_action(mode_t access_mode), returns a kauth_action_t representing the desired access modes. The other one, kauth_access_action(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 it also adds the KAUTH_VNODE_IS_EXEC flag if needed. (Scroll down for what this flag means and how its necessity is determined ๐)
Second: be very careful with adding listeners on this scope, fr fr. A special parameter, fs_decision, lets different file systems instrument different policies without adding their own listener. It's special because it also serves as a fall-back decision when no secmodel(9) is present โ which is what prevents a fail-open scenario ๐ญ It can take either an errno(2) value or "KAUTH_VNODE_REMOTEFS", which means the file system on which the authorization is made is remote and can't give us a fall-back decision. In that case, kauth can only short-circuit the request โ but the file system gets the last word if there's no definitive allow or deny decision. main character energy tbh ๐
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 implementing common policies that file systems can use. Calling convention goes like this:
int error;
error = kauth_authorize_vnode(..., genfs_can_foo(...));
Actions on the vnode scope come in two types: operations and flags ๐ซถ An operation is similar in concept to actions on other scopes โ 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, so the listener can make a more informed decision.
Actions include the following:
KAUTH_VNODE_READ_DATAโ read file dataKAUTH_VNODE_LIST_DIRECTORYโ read directory listing. identical to the above ๐ฏKAUTH_VNODE_WRITE_DATAโ write file dataKAUTH_VNODE_ADD_FILEโ add a file to a directory. identical to the aboveKAUTH_VNODE_EXECUTEโ execute a fileKAUTH_VNODE_SEARCHโ search (enter) a directory. identical to the aboveKAUTH_VNODE_DELETEโ delete a file ๐KAUTH_VNODE_APPEND_DATAโ append data to a fileKAUTH_VNODE_ADD_SUBDIRECTORYโ add a subdirectory to a directory. identical to the aboveKAUTH_VNODE_READ_TIMESโ read the created, last accessed, and last modified times of a fileKAUTH_VNODE_WRITE_TIMESโ modify the created, last accessed, or last modified times of a fileKAUTH_VNODE_READ_FLAGSโ read file flagsKAUTH_VNODE_WRITE_FLAGSโ modify file flagsKAUTH_VNODE_READ_SYSFLAGSโ read file system flagsKAUTH_VNODE_WRITE_SYSFLAGSโ modify file system flagsKAUTH_VNODE_RENAMEโ rename a fileKAUTH_VNODE_CHANGE_OWNERSHIPโ change ownership of a fileKAUTH_VNODE_READ_SECURITYโ read the permissions of a fileKAUTH_VNODE_WRITE_SECURITYโ change the permissions of a file, for example by usingchmod(2)KAUTH_VNODE_READ_ATTRIBUTESโ read attributes of a fileKAUTH_VNODE_WRITE_ATTRIBUTESโ modify attributes of a fileKAUTH_VNODE_READ_EXTATTRIBUTESโ read extended attributes of a fileKAUTH_VNODE_WRITE_EXTATTRIBUTESโ modify extended attributes of a fileKAUTH_VNODE_RETAIN_SUIDโ check if retaining the set-user-id bit on files afterchown(2)is allowedKAUTH_VNODE_RETAIN_SGIDโ check if retaining the set-group-id bit on files afterchown(2)is allowedKAUTH_VNODE_REVOKEโ revoke a file
Flags include the following:
KAUTH_VNODE_IS_EXECโ the vnode is executable.
The macro FS_OBJECT_CAN_EXEC() can be used to help determine if this flag should be set. This macro determines a file system object to be executable if it's a directory (in which case we say it's 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 it's 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 of 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 setKAUTH_VNODE_ACCESSโ the authorization is advisory only and no actual operation is to be performed. this is not implemented ๐ฉ
๐ Credentials Scope ๐
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's a "notify-only" scope โ it allows 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 those aforementioned operations, especially when the credentials hold security model-private data ๐ซถ
Notifications are made using the following function, which is internal to kauth:
int kauth_cred_hook(kauth_cred_t cred, kauth_action_t action, void
*arg0, void *arg1)
with the following actions:
KAUTH_CRED_COPYโcredare the credentials that are being copied.arg0is akauth_cred_trepresenting the destination credentials. That is, the credentials are copied fromcredtoarg0.KAUTH_CRED_FORKโ 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.
KAUTH_CRED_CHROOTโ the credentials incredbelong to a process whose root directory is changed throughchange_root()(seevfs(9)).
Arg0 is the new struct cwdinfo * of the process.
KAUTH_CRED_FREEโ the credentials incredare being freed. Bye bestie, we hardly knew ye ๐ญKAUTH_CRED_INITโ the credentials incredare being initialized. Fresh start energy โจ
Since this is a notify-only scope, every single listener is required to return KAUTH_RESULT_ALLOW. No cap, no exceptions โ itโs giving mandatory group-chat agreement ๐
๐ธ Credentials Accessors and Mutators ๐ธ
Okay so kauth has a whole little toolkit of accessor and mutator routines to handle kauth_cred_t objects. Iconic, honestly.
The following routines can be used to access and modify the user- and group-ids in a kauth_cred_t:
uid_t kauth_cred_getuid(kauth_cred_t cred)โ returns the real user-id fromcred. The actual real one, not the fake one ๐ซถuid_t kauth_cred_geteuid(kauth_cred_t cred)โ returns the effective user-id fromcred. Effective as in โwhat youโre actually acting asโ ๐uid_t kauth_cred_getsvuid(kauth_cred_t cred)โ returns the saved user-id fromcred. The little backup copy, iykyk ๐void kauth_cred_setuid(kauth_cred_t cred, uid_t uid)โ sets the real user-id incredtouid.void kauth_cred_seteuid(kauth_cred_t cred, uid_t uid)โ sets the effective user-id incredtouid.void kauth_cred_setsvuid(kauth_cred_t cred, uid_t uid)โ sets the saved user-id incredtouid.gid_t kauth_cred_getgid(kauth_cred_t cred)โ returns the real group-id fromcred.gid_t kauth_cred_getegid(kauth_cred_t cred)โ returns the effective group-id fromcred.gid_t kauth_cred_getsvgid(kauth_cred_t cred)โ returns the saved group-id fromcred.void kauth_cred_setgid(kauth_cred_t cred, gid_t gid)โ sets the real group-id incredtogid.void kauth_cred_setegid(kauth_cred_t cred, gid_t gid)โ sets the effective group-id incredtogid.void kauth_cred_setsvgid(kauth_cred_t cred, gid_t gid)โ sets the saved group-id incredtogid.u_int kauth_cred_getrefcnt(kauth_cred_t cred)โ returns the reference count forcred. The โhow many besties are holding onto thisโ number ๐ฅ
The following routines can be used to access and modify the group list in a kauth_cred_t:
int kauth_cred_ismember_gid(kauth_cred_t cred, gid_t gid, int *resultp)โ checks if the group-idgidis a member in the group list ofcred.
If it is, resultp will be set to one, otherwise, to zero.
The return value is an error code, or zero for success. Very โvibe check: are you in the group chat?โ energy ๐
u_int kauth_cred_ngroups(kauth_cred_t cred)โ returns the number of groups in the group list ofcred.gid_t kauth_cred_group(kauth_cred_t cred, u_int idx)โ returns the group-id of the group at indexidxin the group list ofcred.int kauth_cred_setgroups(kauth_cred_t cred, const gid_t *groups, size_t ngroups, uid_t gmuid, enum uio_seg seg)โ copiesngroupsgroups from the array pointed to bygroupsto the group list incred, adjusting the number of groups incredappropriately.segshould be eitherUIO_USERSPACEorUIO_SYSSPACE, indicating whethergroupsis 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_getgroups(kauth_cred_t cred, gid_t *groups, size_t ngroups, enum uio_seg seg)โ copiesngroupsgroups from the group list incredto the buffer pointed to bygroups.segshould be eitherUIO_USERSPACEorUIO_SYSSPACE, indicating whethergroupsis a user or kernel space address.
The return value is an error code, or zero for success. Slay, but check your error codes ๐
๐ฆ Credential Private Data ๐ฆ
kauth provides an interface to allow attaching security-model private data to credentials. Itโs giving โyour creds can have little secretsโ โจ
The use of this interface has two parts that can be divided into 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 kauth_register_key(secmodel_t sm, kauth_key_t *keyp)โ registers a new key for private data for security modelsm.keypwill 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. Periodt, check your return value ๐
int kauth_deregister_key(kauth_key_t key)โ deregisters private data keykey. Bye key, thanks for your service ๐
Once registered, private data may be manipulated by the following routines:
void kauth_cred_setdata(kauth_cred_t cred, kauth_key_t key, void *data)โ sets private data forkeyincredto bedata.void * kauth_cred_getdata(kauth_cred_t cred, kauth_key_t key)โ retrieves private data forkeyincred.
Note that it is required to use the above routines every time the private data is changed, i.e., usingkauth_cred_getdata()and later modifying the private data should be accompanied by a call tokauth_cred_setdata()with the โnewโ private data. Fr fr, donโt skip the setter โ thatโs the whole plot twist ๐ญ
๐ Credential Inheritance and Reference Counting ๐
kauth provides an interface for handling shared credentials. Sharing is caring, but the refcount has to stay correct ๐ซถ
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. Main character cred energy: everyone wants a piece ๐
The following routines are available for managing credentials reference counting:
kauth_cred_t kauth_cred_hold(kauth_cred_t cred)โ increases the reference count tocredby one and returnscredverbatim. Same cred, extra hold, no copies ๐void kauth_cred_free(kauth_cred_t cred)โ decreases the reference count tocredby one.
If the reference count dropped to zero, the memory used by cred will be freed. Last one out turns off the lights ๐
Credential inheritance happens during a fork(2), and is handled by the following function:
void kauth_proc_fork(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. Itโs giving โpass the aux, but make it secureโ ๐ฅ
๐ Credentials Memory Management ๐
Data-structures for credentials, listeners, and scopes are allocated from memory pools managed by the pool(9) subsystem. Pool party, but make it kernel-safe ๐ง
The kauth_cred_t objects have their own memory management routines:
kauth_cred_t kauth_cred_alloc(void)โ allocates a newkauth_cred_t, initializes its lock, and sets its reference count to one. Fresh cred, fresh lock, refcount1โ iconic starter pack โจ
โญ Conversion Routines โญ
Sometimes it might be necessary to convert a kauth_cred_t to userland's view of credentials, a struct uucred, or vice versa. Translating between kernel and userland, the bilingual bestie moment ๐ฆ
The following routines are available for these cases:
void kauth_uucred_to_cred(kauth_cred_t cred, const struct uucred *uucred)โ converts userland's view of credentials to akauth_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. Itโs giving โweโll fit what we can, bestieโ ๐ซถ
void kauth_cred_to_uucred(struct uucred *uucred, const kauth_cred_t cred)โ convertskauth_cred_tto 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. Same energy, opposite direction ๐
int kauth_cred_uucmp(kauth_cred_t cred, struct uucred *uucred)โ comparescredwith the userland credentials inuucred.
Common values that will be compared are effective user- and group-ids, and the group list. Itโs a vibe check, but for credentials ๐ธ
๐ Miscellaneous Routines ๐
Other routines provided by kauth are:
void kauth_cred_clone(kauth_cred_t cred1, kauth_cred_t cred2)โ clones credentials fromcred1tocred2, except for the lock and reference count. Copy the fit, not the lock ๐ฅkauth_cred_t kauth_cred_dup(kauth_cred_t cred)โ duplicatescred.
What this routine does is call kauth_cred_alloc() followed by a call to kauth_cred_clone(). Alloc then clone, the classic duo ๐
kauth_cred_t kauth_cred_copy(kauth_cred_t cred)โ works likekauth_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 kauth_cred_free() for cred will be done. Itโs giving โif youโre solo, stay solo; otherwise make a copy and free the old oneโ ๐ญ
kauth_cred_t kauth_cred_get(void)โ returns the credentials associated with the current LWP. This does not change the reference count of the resultingkauth_cred_tobject. Look, donโt touch the refcount โ just vibes ๐
๐ Scope Management ๐
kauth provides routines to manage the creation and deletion of scopes on the system. Scope admin, but make it cute ๐
Note that the built-in scopes, the โgenericโ scope and the โprocessโ scope, can't be deleted. Theyโre permanent residents, no eviction ๐
kauth_scope_t kauth_register_scope(const char *id, kauth_scope_callback_t cb, void *cookie)โ registers a new scope on the system.idis the name of the scope, usually in reverse DNS-like notation. For example,โorg.netbsd.kauth.myscopeโ.cbis the default listener, to which authorization requests for this scope will be dispatched to.cookieis optional user-data that will be passed to all listeners during authorization on the scope. Very โnew group chat, hereโs the invite linkโ energy โจvoid kauth_deregister_scope(kauth_scope_t scope)โ deregistersscopefrom the scopes available on the system, and frees thekauth_scope_tobjectscope. Archive the chat and free the object, periodt ๐
๐ง Listener Management ๐ง
Listeners in kauth are authorization callbacks that are called during an authorization request in the scope which they belong to. Theyโre the group-chat members who get a vote on every request ๐ณ๏ธ
When an authorization request is made, all listeners associated with a scope are called to allow, deny, or defer the request. Itโs giving full council, no skipping anyone.
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. So: one deny = denied, everyone allow = granted, all defer = denied. No cap, thatโs the rule ๐ญ
The following KPI is provided for the management of listeners:
kauth_listener_t kauth_listen_scope(const char *id, kauth_scope_callback_t cb, void *cookie)โ creates a new listener on the scope with the idid, setting the default listener tocb.cookieis optional user-data that will be passed to the listener when called during an authorization request. New listener just dropped, and yes she brought snacks ๐void kauth_unlisten_scope(kauth_listener_t listener)โ bestie this one removeslistenerfrom its scope, guarantees it will not be called ever again, and frees thekauth_listener_tobjectlistener. it's giving closure, periodt ๐ซถ
okay so here's the tea on synchronization ๐ญ kauth literally 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, because it can be accessed simultaneously from the same listener called multiple times. also on you: garbage collection after the listener, possibly freeing any allocated data it used. no cap, that's your job ๐
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. iconic little pattern ngl โจ
during the removal of a listener, first kauth_scope_unlisten() 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. slay ๐ธ
โ ๏ธ listen up: listeners might sleep, so no locks can be held when calling an authorization wrapper. literally do not do it ๐
๐ EXAMPLES ๐
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's 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 */
not me being obsessed with how clean that is ๐ญโจ
note that the securelevel implications were also integrated into the kauth framework so you don't have to note anything special in the call to the authorization wrapper โ you just have to make sure the security model handles the request as you expect it to. the framework ate and left no crumbs ๐
to do that you can just grep(1) in the relevant security model directory and have a look at the code. iykyk ๐
๐ฆ EXTENDING KAUTH ๐ฆ
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. growth era fr fr ๐ฑ
- 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. no skipping that step bestie ๐
- new actions should be added when sets of operations not covered at all belong in an already existing scope. fills the gap, main character energy โจ
- requests (or sub-actions) can be added as subsets of existing actions when an operation that belongs in an already covered area is introduced. nesting dolls but make it kernel ๐
๐ note that all additions should include updates to this manual, the security models shipped with NetBSD, and the example skeleton security model. don't leave the docs hanging, that's so not slay ๐ค
๐ SEE ALSO ๐
๐ HISTORY ๐
the kernel authorization framework first appeared in Mac OS X 10.4. a legend was born ๐ซ
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 โ go read it, it's giving lore ๐โจ
๐ NOTES ๐
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. fr fr do not do this, it will not end well ๐ญ๐
๐ AUTHORS ๐
- Elad Efrat
<elad@NetBSD.org>implemented the kernel authorization framework in NetBSD. the blueprint herself ๐ - Jason R. Thorpe
<thorpej@NetBSD.org>provided guidance and answered questions about the Darwin implementation. the supportive bestie we all need ๐ซถ
and that's the whole page babes!! ๐โจ this one's from the Debian fam, dated Jul 1, 2025 โ go forth and authorize securely, mwah ๐ ๐
๐ฆ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 2005, 2006 Elad Efrat <elad@NetBSD.org>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms