1.\" $NetBSD: kauth.9,v 1.58 2007/11/27 10:50:09 elad Exp $ 2.\" 3.\" Copyright (c) 2005, 2006 Elad Efrat <elad@NetBSD.org> 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. The name of the author may not be used to endorse or promote products 15.\" derived from this software without specific prior written permission. 16.\" 17.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27.\" 28.Dd November 27, 2007 29.Dt KAUTH 9 30.Os 31.Sh NAME 32.Nm kauth 33.Nd kernel authorization framework 34.Sh SYNOPSIS 35.In sys/kauth.h 36.Sh DESCRIPTION 37.Nm , 38or kernel authorization, is the subsystem managing all authorization requests 39inside the kernel. 40It manages user credentials and rights, and can be used 41to implement a system-wide security policy. 42It allows external modules to plug-in the authorization process. 43.Pp 44.Nm 45introduces some new concepts, namely 46.Dq scopes 47and 48.Dq listeners , 49which will be detailed together with other useful information for kernel 50developers in this document. 51.Ss Types 52Some 53.Nm 54types include the following: 55.Bl -tag 56.It kauth_cred_t 57Representing credentials that can be associated with an object. 58Includes user- and group-ids (real, effective, and save) as well as group 59membership information. 60.It kauth_scope_t 61Describes a scope. 62.It kauth_listener_t 63Describes a listener. 64.El 65.Ss Terminology 66.Nm 67operates in various 68.Dq scopes , 69each scope holding a group of 70.Dq listeners . 71.Pp 72Each listener works as a callback for when an authorization request within the 73scope is made. 74When such a request is made, all listeners on the scope are passed common 75information such as the credentials of the request context, an identifier for 76the requested operation, and possibly other information as well. 77.Pp 78Every listener examines the passed information and returns its decision 79regarding the requested operation. 80It can either allow, deny, or defer the operation -- in which case, the 81decision is left to the other listeners. 82.Pp 83For an operation to be allowed, all listeners must not return any deny 84or defer decisions. 85.Pp 86Scopes manage listeners that operate in the same aspect of the system. 87.Ss Kernel Programming Interface 88.Nm 89exports a KPI that allows developers both of 90.Nx 91and third-party products to authorize requests, access and modify credentials, 92create and remove scopes and listeners, and perform other miscellaneous operations on 93credentials. 94.Ss Authorization Requests 95.Nm 96provides a single authorization request routine, which all authorization 97requests go through. 98This routine dispatches the request to the listeners of the appropriate scope, 99together with four optional user-data variables, and returns the augmented 100result. 101.Pp 102It is declared as 103.Pp 104.Ft int Fn kauth_authorize_action "kauth_scope_t scope" "kauth_cred_t cred" \ 105"kauth_action_t op" "void *arg0" "void *arg1" "void *arg2" "void *arg3" 106.Pp 107An authorization request can return one of two possible values. 108Zero indicates success -- the operation is allowed; 109.Er EPERM 110(see 111.Xr errno 2 ) 112indicates failure -- the operation is denied. 113.Pp 114Each scope has its own authorization wrapper, to make it easy to call from various 115places by eliminating the need to specify the scope and/or cast values. 116The authorization wrappers are detailed in each scope's section. 117.Pp 118.Fn kauth_authorize_action 119has several special cases, when it will always allow the request. 120These are for when the request is issued by the kernel itself (indicated by the 121credentials being either 122.Dv NOCRED 123or 124.Dv FSCRED ) , 125or when there was no definitive decision from any of the listeners (i.e., it 126was not explicitly allowed or denied) and no security model was loaded. 127.Ss Generic Scope 128The generic scope, 129.Dq org.netbsd.kauth.generic , 130manages generic authorization requests in the kernel. 131.Pp 132The authorization wrapper for this scope is declared as 133.Pp 134.Ft int Fn kauth_authorize_generic "kauth_cred_t cred" "kauth_action_t op" \ 135"void *arg0" 136.Pp 137The following operations are available for this scope: 138.Bl -tag 139.It Dv KAUTH_GENERIC_ISSUSER 140Checks whether the credentials belong to the super-user. 141.Pp 142Using this request is strongly discouraged and should only be done as a 143temporary place-holder, as it is breaking the separation between the 144interface for authorization requests from the back-end implementation. 145.It Dv KAUTH_GENERIC_CANSEE 146Checks whether an object with one set of credentials can access 147information about another object, possibly with a different set of 148credentials. 149.Pp 150.Ar arg0 151contains the credentials of the object looked at. 152.Pp 153This request should be issued only in cases where generic credentials 154check is required; otherwise it is recommended to use the object-specific 155routines. 156.El 157.Ss System Scope 158The system scope, 159.Dq org.netbsd.kauth.system , 160manages authorization requests affecting the entire system. 161.Pp 162The authorization wrapper for this scope is declared as 163.Pp 164.Ft int Fn kauth_authorize_system "kauth_cred_t cred" \ 165"kauth_action_t op" "enum kauth_system_req req" "void *arg1" "void *arg2" \ 166"void *arg3" 167.Pp 168The following requests are available for this scope: 169.Bl -tag 170.It Dv KAUTH_SYSTEM_ACCOUNTING 171Check if enabling/disabling accounting allowed. 172.It Dv KAUTH_SYSTEM_CHROOT 173.Ar req 174can be any of the following: 175.Bl -tag 176.It Dv KAUTH_REQ_SYSTEM_CHROOT_CHROOT 177Check if calling 178.Xr chroot 2 179is allowed. 180.It Dv KAUTH_REQ_SYSTEM_CHROOT_FCHROOT 181Check if calling 182.Xr fchroot 2 183is allowed. 184.El 185.It Dv KAUTH_SYSTEM_DEBUG 186This request concentrates several debugging-related operations. 187.Ar req 188can be any of the following: 189.Bl -tag 190.It Dv KAUTH_REQ_SYSTEM_DEBUG_IPKDB 191Check if using 192.Xr ipkdb 4 193is allowed. 194.El 195.It Dv KAUTH_SYSTEM_FILEHANDLE 196Check if filehandle operations allowed. 197.It Dv KAUTH_SYSTEM_LKM 198Check if an LKM request is allowed. 199.Pp 200.Ar arg1 201is the command. 202.It Dv KAUTH_SYSTEM_MKNOD 203Check if creating devices is allowed. 204.It Dv KAUTH_SYSTEM_MOUNT 205Check if mount-related operations are allowed. 206.Pp 207.Ar req 208can be any of the following: 209.Bl -tag 210.It Dv KAUTH_REQ_SYSTEM_MOUNT_GET 211Check if retrieving information about a mount is allowed. 212.Ar arg1 213is a 214.Ft struct mount * 215with the mount structure in question, 216.Ar arg2 217is a 218.Ft void * 219with file-system specific data, if any. 220.It Dv KAUTH_REQ_SYSTEM_MOUNT_NEW 221Check if mounting a new file-system is allowed. 222.Pp 223.Ar arg1 224is the 225.Ft struct vnode * 226on which the file-system is to be mounted, 227.Ar arg2 228is an 229.Ft int 230with the mount flags, and 231.Ar arg3 232is a 233.Ft void * 234with file-system specific data, if any. 235.It Dv KAUTH_REQ_SYSTEM_MOUNT_UNMOUNT 236Checks if unmounting a file-system is allowed. 237.Pp 238.Ar arg1 239is a 240.Ft struct mount * 241with the mount in question. 242.It Dv KAUTH_REQ_SYSTEM_MOUNT_UPDATE 243Checks if updating an existing mount is allowed. 244.Pp 245.Ar arg1 246is the 247.Ft struct mount * 248of the existing mount, 249.Ar arg2 250is an 251.Ft int 252with the new mount flags, and 253.Ar arg3 254is a 255.Ft void * 256with file-system specific data, if any. 257.El 258.It Dv KAUTH_SYSTEM_REBOOT 259Check if rebooting is allowed. 260.It Dv KAUTH_SYSTEM_SETIDCORE 261Check if changing coredump settings for set-id processes is allowed. 262.It Dv KAUTH_SYSTEM_SWAPCTL 263Check if privileged 264.Xr swapctl 2 265requests are allowed. 266.It Dv KAUTH_SYSTEM_SYSCTL 267This requests operations related to 268.Xr sysctl 9 . 269.Ar req 270indicates the specific request and can be one of the following: 271.Bl -tag 272.It Dv KAUTH_REQ_SYSTEM_SYSCTL_ADD 273Check if adding a 274.Xr sysctl 9 275node is allowed. 276.It Dv KAUTH_REQ_SYSTEM_SYSCTL_DELETE 277Check if deleting a 278.Xr sysctl 9 279node is allowed. 280.It Dv KAUTH_REQ_SYSTEM_SYSCTL_DESC 281Check if adding description to a 282.Xr sysctl 9 283node is allowed. 284.It Dv KAUTH_REQ_SYSTEM_SYSCTL_PRVT 285Check if accessing private 286.Xr sysctl 9 287nodes is allowed. 288.El 289.It Dv KAUTH_SYSTEM_TIME 290This request groups time-related operations. 291.Ar req 292can be any of the following: 293.Bl -tag 294.It Dv KAUTH_REQ_SYSTEM_TIME_ADJTIME 295Check if changing the time using 296.Xr adjtime 2 297is allowed. 298.It Dv KAUTH_REQ_SYSTEM_TIME_NTPADJTIME 299Check if setting the time using 300.Xr ntp_adjtime 2 301is allowed. 302.It Dv KAUTH_REQ_SYSTEM_TIME_SYSTEM 303Check if changing the time (usually via 304.Xr settimeofday 2 ) 305is allowed. 306.Pp 307.Ar arg1 308is a 309.Ft struct timespec * 310with the new time, 311.Ar arg2 312is a 313.Ft struct timeval * 314with the delta from the current time, 315.Ar arg3 316is a 317.Ft bool 318indicating whether the caller is a device context (eg. 319.Pa /dev/clockctl ) 320or not. 321.It Dv KAUTH_REQ_SYSTEM_TIME_RTCOFFSET 322Check if changing the RTC offset is allowed. 323.It Dv KAUTH_REQ_SYSTEM_TIME_TIMECOUNTERS 324Check if manipulating timecounters is allowed. 325.El 326.El 327.Ss Process Scope 328The process scope, 329.Dq org.netbsd.kauth.process , 330manages authorization requests related to processes in the system. 331.Pp 332The authorization wrapper for this scope is declared as 333.Pp 334.Ft int Fn kauth_authorize_process "kauth_cred_t cred" \ 335"kauth_action_t op" "struct proc *p" "void *arg1" "void *arg2" \ 336"void *arg3" 337.Pp 338The following operations are available for this scope: 339.Bl -tag 340.It Dv KAUTH_PROCESS_CANKTRACE 341Checks whether an object with one set of credentials can 342.Xr ktrace 1 343another process 344.Ar p , 345possibly with a different set of credentials. 346.It Dv KAUTH_PROCESS_CANPROCFS 347Checks whether object with passed credentials can use 348.Em procfs 349to access process 350.Ar p . 351.Pp 352.Ar arg1 353is the 354.Ft struct pfsnode * 355for the target element in the target process, and 356.Ar arg2 357is the access type, which can be either 358.Dv KAUTH_REQ_PROCESS_CANPROCFS_CTL , 359.Dv KAUTH_REQ_PROCESS_CANPROCFS_READ , 360.Dv KAUTH_REQ_PROCESS_CANPROCFS_RW , 361or 362.Dv KAUTH_REQ_PROCESS_CANPROCFS_WRITE , 363indicating 364.Em control , 365.Em read , 366.Em read-write , 367or 368.Em write 369access respectively. 370.It Dv KAUTH_PROCESS_CANPTRACE 371Checks whether object with passed credentials can use 372.Xr ptrace 2 373to access process 374.Ar p . 375.Pp 376.Ar arg1 377is the 378.Xr ptrace 2 379command. 380.It Dv KAUTH_PROCESS_CANSEE 381Checks whether an object with one set of credentials can access 382information about another process, possibly with a different set of 383credentials. 384.It Dv KAUTH_PROCESS_CANSIGNAL 385Checks whether an object with one set of credentials can post signals 386to another process. 387.Pp 388.Ar p 389is the process the signal is being posted to, and 390.Ar arg1 391is the signal number. 392.It Dv KAUTH_PROCESS_CANSYSTRACE 393Checks whether object with passed credentials can use 394.Xr systrace 4 395on process 396.Ar p . 397.It Dv KAUTH_PROCESS_CORENAME 398Checks whether the coredump name for the process 399.Ar p 400can be changed. 401.Pp 402.Ar arg1 403is the new corename. 404.It Dv KAUTH_PROCESS_NICE 405Checks whether the 406.Em nice 407value of 408.Ar p 409can be changed to 410.Ar arg1 . 411.It Dv KAUTH_PROCESS_RLIMIT 412Checks whether the 413.Em rlimit 414value for 415.Ar arg2 416in 417.Ar p 418can be set to 419.Ar arg1 . 420.It Dv KAUTH_PROCESS_SETID 421Check if changing the user- or group-ids, groups, or login-name for 422.Ar p 423is allowed. 424.It Dv KAUTH_PROCESS_STOPFLAG 425Check if setting the stop flags for 426.Xr exec 3 , 427.Xr exit 3 , 428and 429.Xr fork 2 430is allowed. 431.Pp 432.Ar arg1 433indicates the flag, and can be either 434.Dv P_STOPEXEC , 435.Dv P_STOPEXIT , 436or 437.Dv P_STOPFORK 438respectively. 439.El 440.Ss Network Scope 441The network scope, 442.Dq org.netbsd.kauth.network , 443manages networking-related authorization requests in the kernel. 444.Pp 445The authorization wrapper for this scope is declared as 446.Pp 447.Ft int Fn kauth_authorize_network "kauth_cred_t cred" "kauth_action_t op" \ 448"enum kauth_network_req req" "void *arg1" "void *arg2" "void *arg3" 449.Pp 450The following operations are available for this scope: 451.Bl -tag 452.It Dv KAUTH_NETWORK_ALTQ 453Checks if an ALTQ operation is allowed. 454.Pp 455.Ar req 456indicates the ALTQ subsystem in question, and can be one of the following: 457.Pp 458.Bl -tag -compact 459.It Dv KAUTH_REQ_NETWORK_ALTQ_AFMAP 460.It Dv KAUTH_REQ_NETWORK_ALTQ_BLUE 461.It Dv KAUTH_REQ_NETWORK_ALTQ_CBQ 462.It Dv KAUTH_REQ_NETWORK_ALTQ_CDNR 463.It Dv KAUTH_REQ_NETWORK_ALTQ_CONF 464.It Dv KAUTH_REQ_NETWORK_ALTQ_FIFOQ 465.It Dv KAUTH_REQ_NETWORK_ALTQ_HFSC 466.It Dv KAUTH_REQ_NETWORK_ALTQ_JOBS 467.It Dv KAUTH_REQ_NETWORK_ALTQ_PRIQ 468.It Dv KAUTH_REQ_NETWORK_ALTQ_RED 469.It Dv KAUTH_REQ_NETWORK_ALTQ_RIO 470.It Dv KAUTH_REQ_NETWORK_ALTQ_WFQ 471.El 472.It Dv KAUTH_NETWORK_BIND 473Checks if a 474.Xr bind 2 475request is allowed. 476.Pp 477.Ar req 478allows to indicate the type of the request to structure listeners and callers 479easier. 480Supported request types: 481.Bl -tag 482.It Dv KAUTH_REQ_NETWORK_BIND_PRIVPORT 483Checks if binding to a privileged/reserved port is allowed. 484.El 485.It Dv KAUTH_NETWORK_FIREWALL 486Checks if firewall-related operations are allowed. 487.Pp 488.Ar req 489indicates the sub-action, and can be one of the following: 490.Bl -tag 491.It Dv KAUTH_REQ_NETWORK_FIREWALL_FW 492Modification of packet filtering rules. 493.It Dv KAUTH_REQ_NETWORK_FIREWALL_NAT 494Modification of NAT rules. 495.El 496.It Dv KAUTH_NETWORK_INTERFACE 497Checks if network interface-related operations are allowed. 498.Pp 499.Ar arg1 500is (optionally) the 501.Ft struct ifnet * 502associated with the interface. 503.Ar arg2 504is (optionally) an 505.Ft int 506describing the interface-specific operation. 507.Ar arg3 508is (optionally) a pointer to the interface-specific request structure. 509.Ar req 510indicates the sub-action, and can be one of the following: 511.Bl -tag 512.It Dv KAUTH_REQ_NETWORK_INTERFACE_GET 513Check if retrieving information from the device is allowed. 514.It Dv KAUTH_REQ_NETWORK_INTERFACE_GETPRIV 515Check if retrieving privileged information from the device is allowed. 516.It Dv KAUTH_REQ_NETWORK_INTERFACE_SET 517Check if setting parameters on the device is allowed. 518.It Dv KAUTH_REQ_NETWORK_INTERFACE_SETPRIV 519Check if setting privileged parameters on the device is allowed. 520.El 521.Pp 522Note that unless the 523.Ft struct ifnet * 524for the interface was passed in 525.Ar arg1 , 526there's no way to tell what structure 527.Ar arg3 528is. 529.It Dv KAUTH_NETWORK_FORWSRCRT 530Checks whether status of forwarding of source-routed packets can be modified 531or not. 532.It Dv KAUTH_NETWORK_ROUTE 533Checks if a routing-related request is allowed. 534.Pp 535.Ar arg1 536is the 537.Ft struct rt_msghdr * 538for the request. 539.It Dv KAUTH_NETWORK_SOCKET 540Checks if a socket related operation is allowed. 541.Pp 542.Ar req 543allows to indicate the type of the request to structure listeners and callers 544easier. 545Supported request types: 546.Bl -tag 547.It Dv KAUTH_REQ_NETWORK_SOCKET_RAWSOCK 548Checks if opening a raw socket is allowed. 549.It Dv KAUTH_REQ_NETWORK_SOCKET_OPEN 550Checks if opening a socket is allowed. 551.Ar arg1 , arg2 , 552and 553.Ar arg3 554are all 555.Ft int 556parameters describing the domain, socket type, and protocol, 557respectively. 558.It Dv KAUTH_REQ_NETWORK_SOCKET_CANSEE 559Checks if looking at the socket passed is allowed. 560.Pp 561.Ar arg1 562is a 563.Ft struct socket * 564describing the socket. 565.El 566.El 567.Ss Machine-dependent Scope 568The machine-dependent (machdep) scope, 569.Dq org.netbsd.kauth.machdep , 570manages machine-dependent authorization requests in the kernel. 571.Pp 572The authorization wrapper for this scope is declared as 573.Pp 574.Ft int Fn kauth_authorize_machdep "kauth_cred_t cred" "kauth_action_t op" \ 575"void *arg0" "void *arg1" "void *arg2" "void *arg3" 576.Pp 577The actions on this scope provide a set that may or may not affect all 578platforms. 579Below is a list of available actions, along with which platforms are affected 580by each. 581.Bl -tag 582.It Dv KAUTH_MACHDEP_IOPERM_GET 583Request to get the I/O permission level. 584Affects 585.Em amd64 , 586.Em i386 , 587.Em xen . 588.It Dv KAUTH_MACHDEP_IOPERM_SET 589Request to set the I/O permission level. 590Affects 591.Em amd64 , 592.Em i386 , 593.Em xen . 594.It Dv KAUTH_MACHDEP_IOPL 595Request to set the I/O privilege level. 596Affects 597.Em amd64 , 598.Em i386 , 599.Em xen . 600.It Dv KAUTH_MACHDEP_LDT_GET 601Request to get the LDT (local descriptor table). 602Affects 603.Em amd64 , 604.Em i386 , 605.Em xen . 606.It Dv KAUTH_MACHDEP_LDT_SET 607Request to set the LDT (local descriptor table). 608Affects 609.Em amd64 , 610.Em i386 , 611.Em xen . 612.It Dv KAUTH_MACHDEP_MTRR_GET 613Request to get the MTRR (memory type range registers). 614Affects 615.Em amd64 , 616.Em i386 , 617.Em xen . 618.It Dv KAUTH_MACHDEP_MTRR_SET 619Request to set the MTRR (memory type range registers). 620Affects 621.Em amd64 , 622.Em i386 , 623.Em xen . 624.It Dv KAUTH_MACHDEP_UNMANAGEDMEM 625Request to access unmanaged memory. 626Affects 627.Em alpha , 628.Em amd64 , 629.Em arm , 630.Em i386 , 631.Em pc532 , 632.Em powerpc , 633.Em sh3 , 634.Em sh5 , 635.Em vax , 636.Em xen . 637.El 638.Ss Device Scope 639The device scope, 640.Dq org.netbsd.kauth.device , 641manages authorization requests related to devices on the system. 642Devices can be, for example, terminals, tape drives, and any other hardware. 643Network devices specifically are handled by the 644.Em network 645scope. 646.Pp 647In addition to the standard authorization wrapper: 648.Pp 649.Ft int Fn kauth_authorize_device "kauth_cred_t cred" "kauth_action_t op" \ 650"void *arg0" "void *arg1" "void *arg2" "void *arg3" 651.Pp 652this scope provides authorization wrappers for various device types. 653.Pp 654.Ft int Fn kauth_authorize_device_tty "kauth_cred_t cred" "kauth_action_t op" \ 655"struct tty *tty" 656.Pp 657Authorizes requests for 658.Em terminal devices 659on the system. 660The third argument, 661.Ar tty , 662is the terminal device in question. 663It is passed to the listener as 664.Ar arg0 . 665The second argument, 666.Ar op , 667is the action and can be one of the following: 668.Bl -tag 669.It Dv KAUTH_DEVICE_TTY_OPEN 670Open the terminal device pointed to by 671.Ar tty . 672.It Dv KAUTH_DEVICE_TTY_PRIVSET 673Set privileged settings on the terminal device pointed to by 674.Ar tty . 675.El 676.Pp 677.Ft int Fn kauth_authorize_device_spec "kauth_cred_t cred" \ 678"enum kauth_device_req req" "struct vnode *vp" 679.Pp 680Authorizes requests for 681.Em special files , 682usually disk devices, but also direct memory access, on the system. 683.Pp 684It passes 685.Dv KAUTH_DEVICE_RAWIO_SPEC 686as the action to the listener, and accepts two arguments. 687.Ar req , 688passed to the listener as 689.Ar arg0 , 690is access requested, and can be one of 691.Dv KAUTH_REQ_DEVICE_RAWIO_SPEC_READ , 692.Dv KAUTH_REQ_DEVICE_RAWIO_SPEC_WRITE , 693or 694.Dv KAUTH_REQ_DEVICE_RAWIO_SPEC_RW , 695representing read, write, or both read/write access respectively. 696.Ar vp 697is the vnode of the special file in question, and is passed to the listener as 698.Ar arg1 . 699.Pp 700Keep in mind that it is the responsibility of the security model developer to 701check whether the underlying device is a disk or the system memory, using 702.Fn iskmemdev : 703.Bd -literal -offset indent 704if ((vp-\*[Gt]v_type == VCHR) \*[Am]\*[Am] 705 iskmemdev(vp-\*[Gt]v_un.vu_specinfo-\*[Gt]si_rdev)) 706 /* system memory access */ 707.Ed 708.Pp 709.Ft int Fn kauth_authorize_device_passthru "kauth_cred_t cred" "dev_t dev" \ 710"u_long mode" "void *data" 711.Pp 712Authorizes hardware 713.Em passthru 714requests, or user commands passed directly to the hardware. 715These have the potential of resulting in direct disk and/or memory access. 716.Pp 717It passes 718.Dv KAUTH_DEVICE_RAWIO_PASSTHRU 719as the action to the listener, and accepts three arguments. 720.Ar dev , 721passed as 722.Ar arg1 723to the listener, is the device for which the request is made. 724.Ar mode , 725passed as 726.Ar arg0 727to the listener, is a generic representation of the access mode requested. 728It can be one or more (binary-OR'd) of the following: 729.Pp 730.Bl -tag -offset indent -compact 731.It KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READ 732.It KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF 733.It KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITE 734.It KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF 735.El 736.Pp 737.Ar data , 738passed as 739.Ar arg2 740to the listener, is device-specific data that may be associated with the 741request. 742.Ss Credentials Scope 743The credentials scope, 744.Dq org.netbsd.kauth.cred , 745is a special scope used internally by the 746.Nm 747framework to provide hooking to credential-related operations. 748.Pp 749It is a 750.Dq notify-only 751scope, allowing hooking operations such as initialization of new credentials, 752credential inheritance during a fork, and copying and freeing of credentials. 753The main purpose for this scope is to give a security model a way to control 754the aforementioned operations, especially in cases where the credentials 755hold security model-private data. 756.Pp 757Notifications are made using the following function, which is internal to 758.Nm : 759.Pp 760.Ft int Fn kauth_cred_hook "kauth_cred_t cred" "kauth_action_t action" \ 761"void *arg0" "void *arg1" 762.Pp 763With the following actions: 764.Bl -tag 765.It Dv KAUTH_CRED_COPY 766The credentials are being copied. 767.Ar cred 768are the credentials of the lwp context doing the copy, and 769.Ar arg0 770and 771.Ar arg1 772are both 773.Ft kauth_cred_t 774representing the 775.Dq from 776and 777.Dq to 778credentials, respectively. 779.It Dv KAUTH_CRED_FORK 780The credentials are being inherited from a parent to a child process during a 781fork. 782.Pp 783.Ar cred 784are the credentials of the lwp context doing the fork, and 785.Ar arg0 786and 787.Ar arg1 788are both 789.Ft struct proc * 790of the parent and child processes, respectively. 791.It Dv KAUTH_CRED_FREE 792The credentials in 793.Ar cred 794are being freed. 795.It Dv KAUTH_CRED_INIT 796The credentials in 797.Ar cred 798are being initialized. 799.El 800.Pp 801Since this is a notify-only scope, all listeners are required to return 802.Dv KAUTH_RESULT_ALLOW . 803.Ss Credentials Accessors and Mutators 804.Nm 805has a variety of accessor and mutator routines to handle 806.Ft kauth_cred_t 807objects. 808.Pp 809The following routines can be used to access and modify the user- and 810group-ids in a 811.Ft kauth_cred_t : 812.Bl -tag 813.It Ft uid_t Fn kauth_cred_getuid "kauth_cred_t cred" 814Returns the real user-id from 815.Ar cred . 816.It Ft uid_t Fn kauth_cred_geteuid "kauth_cred_t cred" 817Returns the effective user-id from 818.Ar cred . 819.It Ft uid_t Fn kauth_cred_getsvuid "kauth_cred_t cred" 820Returns the saved user-id from 821.Ar cred . 822.It Ft void Fn kauth_cred_setuid "kauth_cred_t cred" "uid_t uid" 823Sets the real user-id in 824.Ar cred 825to 826.Ar uid . 827.It Ft void Fn kauth_cred_seteuid "kauth_cred_t cred" "uid_t uid" 828Sets the effective user-id in 829.Ar cred 830to 831.Ar uid . 832.It Ft void Fn kauth_cred_setsvuid "kauth_cred_t cred" "uid_t uid" 833Sets the saved user-id in 834.Ar cred 835to 836.Ar uid . 837.It Ft gid_t Fn kauth_cred_getgid "kauth_cred_t cred" 838Returns the real group-id from 839.Ar cred . 840.It Ft gid_t Fn kauth_cred_getegid "kauth_cred_t cred" 841Returns the effective group-id from 842.Ar cred . 843.It Ft gid_t Fn kauth_cred_getsvgid "kauth_cred_t cred" 844Returns the saved group-id from 845.Ar cred . 846.It Ft void Fn kauth_cred_setgid "kauth_cred_t cred" "gid_t gid" 847Sets the real group-id in 848.Ar cred 849to 850.Ar gid . 851.It Ft void Fn kauth_cred_setegid "kauth_cred_t cred" "gid_t gid" 852Sets the effective group-id in 853.Ar cred 854to 855.Ar gid . 856.It Ft void Fn kauth_cred_setsvgid "kauth_cred_t cred" "gid_t gid" 857Sets the saved group-id in 858.Ar cred 859to 860.Ar gid . 861.It Ft u_int Fn kauth_cred_getrefcnt "kauth_cred_t cred" 862Return the reference count for 863.Ar cred . 864.El 865.Pp 866The following routines can be used to access and modify the group 867list in a 868.Ft kauth_cred_t : 869.Bl -tag 870.It Ft int Fn kauth_cred_ismember_gid "kauth_cred_t cred" "gid_t gid" \ 871"int *resultp" 872Checks if the group-id 873.Ar gid 874is a member in the group list of 875.Ar cred . 876.Pp 877If it is, 878.Ar resultp 879will be set to one, otherwise, to zero. 880.Pp 881The return value is an error code, or zero for success. 882.It Ft u_int Fn kauth_cred_ngroups "kauth_cred_t cred" 883Return the number of groups in the group list of 884.Ar cred . 885.It Ft gid_t Fn kauth_cred_group "kauth_cred_t cred" "u_int idx" 886Return the group-id of the group at index 887.Ar idx 888in the group list of 889.Ar cred . 890.It Ft int Fn kauth_cred_setgroups "kauth_cred_t cred" "gid_t *groups" \ 891"size_t ngroups" "uid_t gmuid" "enum uio_seg seg" 892Copy 893.Ar ngroups 894groups from array pointed to by 895.Ar groups 896to the group list in 897.Ar cred , 898adjusting the number of groups in 899.Ar cred 900appropriately. 901.Ar seg 902should be either 903.Dv UIO_USERSPACE 904or 905.Dv UIO_SYSSPACE 906indicating whether 907.Ar groups 908is a user or kernel space address. 909.Pp 910Any groups remaining will be set to an invalid value. 911.Pp 912.Ar gmuid 913is unused for now, and to maintain interface compatibility with the Darwin 914KPI. 915.Pp 916The return value is an error code, or zero for success. 917.It Ft int Fn kauth_cred_getgroups "kauth_cred_t cred" "gid_t *groups" \ 918"size_t ngroups" "enum uio_seg seg" 919Copy 920.Ar ngroups 921groups from the group list in 922.Ar cred 923to the buffer pointed to by 924.Ar groups . 925.Ar seg 926should be either 927.Dv UIO_USERSPACE 928or 929.Dv UIO_SYSSPACE 930indicating whether 931.Ar groups 932is a user or kernel space address. 933.Pp 934The return value is an error code, or zero for success. 935.El 936.Ss Credential Private Data 937.Nm 938provides an interface to allow attaching security-model private data to 939credentials. 940.Pp 941The use of this interface has two parts that can be divided to direct and 942indirect control of the private-data. 943Directly controlling the private data is done by using the below routines, 944while the indirect control is often dictated by events such as process 945fork, and is handled by listening on the credentials scope (see above). 946.Pp 947Attaching private data to credentials works by registering a key to serve 948as a unique identifier, distinguishing various sets of private data that 949may be associated with the credentials. 950Registering, and deregistering, a key is done by using these routines: 951.Pp 952.Bl -tag 953.It Ft int Fn kauth_register_key "const char *name" "kauth_key_t *keyp" 954Register new key for private data for 955.Ar name 956(usually, the security model name). 957.Ar keyp 958will be used to return the key to be used in further calls. 959.Pp 960The function returns 0 on success and an error code (see 961.Xr errno 2 ) 962on failure. 963.It Ft int Fn kauth_deregister_key "kauth_key_t key" 964Deregister private data key 965.Ar key . 966.El 967.Pp 968Once registered, private data may be manipulated by the following routines: 969.Bl -tag 970.It Ft void Fn kauth_cred_setdata "kauth_cred_t cred" "kauth_key_t key" \ 971"void *data" 972Set private data for 973.Ar key 974in 975.Ar cred 976to be 977.Ar data . 978.It Ft void * Fn kauth_cred_getdata "kauth_cred_t cred" "kauth_key_t key" 979Retrieve private data for 980.Ar key 981in 982.Ar cred . 983.El 984.Pp 985Note that it is required to use the above routines every time the private 986data is changed, i.e., using 987.Fn kauth_cred_getdata 988and later modifying the private data should be accompanied by a call to 989.Fn kauth_cred_setdata 990with the 991.Dq new 992private data. 993.Ss Credential Inheritance and Reference Counting 994.Nm 995provides an interface for handling shared credentials. 996.Pp 997When a 998.Ft kauth_cred_t 999is first allocated, its reference count is set to 1. 1000However, with time, its reference count can grow as more objects (processes, 1001LWPs, files, etc.) reference it. 1002.Pp 1003The following routines are available for managing credentials reference 1004counting: 1005.Bl -tag 1006.It Ft void Fn kauth_cred_hold "kauth_cred_t cred" 1007Increases reference count to 1008.Ar cred 1009by one. 1010.It Ft void Fn kauth_cred_free "kauth_cred_t cred" 1011Decreases the reference count to 1012.Ar cred 1013by one. 1014.Pp 1015If the reference count dropped to zero, the memory used by 1016.Ar cred 1017will be freed. 1018.El 1019.Pp 1020Credential inheritance happens during a 1021.Xr fork 2 , 1022and is handled by the following function: 1023.Pp 1024.Ft void Fn kauth_proc_fork "struct proc *parent" "struct proc *child" 1025.Pp 1026When called, it references the parent's credentials from the child, 1027and calls the credentials scope's hook with the 1028.Dv KAUTH_CRED_FORK 1029action to allow security model-specific handling of the inheritance 1030to take place. 1031.Ss Credentials Memory Management 1032Data-structures for credentials, listeners, and scopes are allocated from 1033memory pools managed by the 1034.Xr pool 9 1035subsystem. 1036.Pp 1037The 1038.Ft kauth_cred_t 1039objects have their own memory management routines: 1040.Bl -tag 1041.It Ft kauth_cred_t Fn kauth_cred_alloc "void" 1042Allocates a new 1043.Ft kauth_cred_t , 1044initializes its lock, and sets its reference count to one. 1045.El 1046.Ss Conversion Routines 1047Sometimes it might be necessary to convert a 1048.Ft kauth_cred_t 1049to userland's view of credentials, a 1050.Ft struct uucred , 1051or vice versa. 1052.Pp 1053The following routines are available for these cases: 1054.Bl -tag 1055.It Ft void Fn kauth_uucred_to_cred "kauth_cred_t cred" "const struct uucred *uucred" 1056Convert userland's view of credentials to a 1057.Ft kauth_cred_t . 1058.Pp 1059This includes effective user- and group-ids, a number of groups, and a group 1060list. 1061The reference count is set to one. 1062.Pp 1063Note that 1064.Nm 1065will try to copy as many groups as can be held inside a 1066.Ft kauth_cred_t . 1067.It Ft void Fn kauth_cred_to_uucred "struct uucred *uucred" "const kauth_cred_t cred" 1068Convert 1069.Ft kauth_cred_t 1070to userland's view of credentials. 1071.Pp 1072This includes effective user- and group-ids, a number of groups, and a group 1073list. 1074.Pp 1075Note that 1076.Nm 1077will try to copy as many groups as can be held inside a 1078.Ft struct uucred . 1079.It Ft int Fn kauth_cred_uucmp "kauth_cred_t cred" "struct uucred *uucred" 1080Compares 1081.Ar cred 1082with the userland credentials in 1083.Ar uucred . 1084.Pp 1085Common values that will be compared are effective user- and group-ids, and 1086the group list. 1087.El 1088.Ss Miscellaneous Routines 1089Other routines provided by 1090.Nm 1091are: 1092.Bl -tag 1093.It Ft void Fn kauth_cred_clone "kauth_cred_t cred1" "kauth_cred_t cred2" 1094Clone credentials from 1095.Ar cred1 1096to 1097.Ar cred2 , 1098except for the lock and reference count. 1099.Pp 1100.It Ft kauth_cred_t Fn kauth_cred_dup "kauth_cred_t cred" 1101Duplicate 1102.Ar cred . 1103.Pp 1104What this routine does is call 1105.Fn kauth_cred_alloc 1106followed by a call to 1107.Fn kauth_cred_clone . 1108.It Ft kauth_cred_t Fn kauth_cred_copy "kauth_cred_t cred" 1109Works like 1110.Fn kauth_cred_dup , 1111except for a few differences. 1112.Pp 1113If 1114.Ar cred 1115already has a reference count of one, it will be returned. 1116Otherwise, a new 1117.Ft kauth_cred_t 1118will be allocated and the credentials from 1119.Ar cred 1120will be cloned to it. 1121Last, a call to 1122.Fn kauth_cred_free 1123for 1124.Ar cred 1125will be done. 1126.It Ft kauth_cred_t Fn kauth_cred_get "void" 1127Return the credentials associated with the current LWP. 1128.El 1129.Ss Scope Management 1130.Nm 1131provides routines to manage the creation and deletion of scopes on the 1132system. 1133.Pp 1134Note that the built-in scopes, the 1135.Dq generic 1136scope and the 1137.Dq process 1138scope, can't be deleted. 1139.Bl -tag 1140.It Ft kauth_scope_t Fn kauth_register_scope "const char *id" \ 1141"kauth_scope_callback_t cb" "void *cookie" 1142Register a new scope on the system. 1143.Ar id 1144is the name of the scope, usually in reverse DNS-like notation. 1145For example, 1146.Dq org.netbsd.kauth.myscope . 1147.Ar cb 1148is the default listener, to which authorization requests for this scope 1149will be dispatched to. 1150.Ar cookie 1151is optional user-data that will be passed to all listeners 1152during authorization on the scope. 1153.It Ft void Fn kauth_deregister_scope "kauth_scope_t scope" 1154Deregister 1155.Ar scope 1156from the scopes available on the system, and free the 1157.Ft kauth_scope_t 1158object 1159.Ar scope . 1160.El 1161.Ss Listener Management 1162Listeners in 1163.Nm 1164are authorization callbacks that are called during an authorization 1165request in the scope which they belong to. 1166.Pp 1167When an authorization request is made, all listeners associated with 1168a scope are called to allow, deny, or defer the request. 1169.Pp 1170It is enough for one listener to deny the request in order for the 1171request to be denied; but all listeners are called during an authorization 1172process none-the-less. 1173All listeners are required to allow the request for it to be granted, 1174and in a case where all listeners defer the request -- leaving the decision 1175for other listeners -- the request is denied. 1176.Pp 1177The following KPI is provided for the management of listeners: 1178.Bl -tag 1179.It Ft kauth_listener_t Fn kauth_listen_scope "const char *id" \ 1180"kauth_scope_callback_t cb" "void *cookie" 1181Create a new listener on the scope with the id 1182.Ar id , 1183setting the default listener to 1184.Ar cb . 1185.Ar cookie 1186is optional user-data that will be passed to the listener when called 1187during an authorization request. 1188.It Ft void Fn kauth_unlisten_scope "kauth_listener_t listener" 1189Removes 1190.Ar listener 1191from the scope which it belongs to, ensuring it won't be called again, 1192and frees the 1193.Ft kauth_listener_t 1194object 1195.Ar listener . 1196.El 1197.Pp 1198.Nm 1199provides no means for synchronization within listeners. 1200It is the the programmer's responsibility to make sure data used by the 1201listener is properly locked during its use, as it can be accessed 1202simultaneously from the same listener called multiple times. 1203It is also the programmer's responsibility to do garbage collection after 1204the listener, possibly freeing any allocated data it used. 1205.Pp 1206The common method to do the above is by having a reference count to 1207each listener. 1208On entry to the listener, this reference count should be raised, and 1209on exit -- lowered. 1210.Pp 1211During the removal of a listener, first 1212.Fn kauth_scope_unlisten 1213should be called to make sure the listener code will not be entered in 1214the future. 1215Then, the code should wait (possibly sleeping) until the reference count 1216drops to zero. 1217When that happens, it is safe to do the final cleanup. 1218.Pp 1219Listeners might sleep, so no locks can be held when calling an authorization 1220wrapper. 1221.Sh EXAMPLES 1222Older code had no abstraction of the security model, so most privilege 1223checks looked like this: 1224.Bd -literal -offset indent 1225if (suser(cred, \*[Am]acflag) == 0) 1226 /* allow privileged operation */ 1227.Ed 1228.Pp 1229Using the new interface, you must ask for a specific privilege explicitly. 1230For example, checking whether it is possible to open a socket would look 1231something like this: 1232.Bd -literal -offset indent 1233if (kauth_authorize_network(cred, KAUTH_NETWORK_SOCKET, 1234 KAUTH_REQ_NETWORK_SOCKET_OPEN, PF_INET, SOCK_STREAM, 1235 IPPROTO_TCP) == 0) 1236 /* allow opening the socket */ 1237.Ed 1238.Pp 1239Note that the 1240.Em securelevel 1241implications were also integrated into the 1242.Nm 1243framework so you don't have to note anything special in the call to the 1244authorization wrapper, but rather just have to make sure the security 1245model handles the request as you expect it to. 1246.Pp 1247To do that you can just 1248.Xr grep 1 1249in the relevant security model directory and have a look at the code. 1250.Sh EXTENDING KAUTH 1251Although 1252.Nm 1253provides a large set of both detailed and more or less generic requests, 1254it might be needed eventually to introduce more scopes, actions, or 1255requests. 1256.Pp 1257Adding a new scope should happen only when an entire subsystem is 1258introduced and it is assumed other parts of the kernel may want to 1259interfere with its inner-workings. 1260When a subsystem that has the potential of impacting the security 1261if the system is introduced, existing security modules must be updated 1262to also handle actions on the newly added scope. 1263.Pp 1264New actions should be added when sets of operations not covered at all 1265belong in an already existing scope. 1266.Pp 1267Requests (or sub-actions) can be added as subsets of existing actions 1268when an operation that belongs in an already covered area is introduced. 1269.Pp 1270Note that all additions should include updates to this manual, the 1271security models shipped with 1272.Nx , 1273and the example skeleton security model. 1274.Sh SEE ALSO 1275.Xr secmodel 9 1276.Sh HISTORY 1277The kernel authorization framework first appeared in Mac OS X 10.4. 1278.Pp 1279The kernel authorization framework in 1280.Nx 1281first appeared in 1282.Nx 4.0 , 1283and is a clean-room implementation based on Apple TN2127, available at 1284http://developer.apple.com/technotes/tn2005/tn2127.html 1285.Sh NOTES 1286As 1287.Nm 1288in 1289.Nx 1290is still under active development, it is likely that the ABI, and possibly the 1291API, will differ between 1292.Nx 1293versions. 1294Developers are to take notice of this fact in order to avoid building code 1295that expects one version of the ABI and running it in a system with a different 1296one. 1297.Sh AUTHORS 1298.An Elad Efrat Aq elad@NetBSD.org 1299implemented the kernel authorization framework in 1300.Nx . 1301.Pp 1302.An Jason R. Thorpe Aq thorpej@NetBSD.org 1303provided guidance and answered questions about the Darwin implementation. 1304.Sh ONE MORE THING 1305The 1306.Nm 1307framework is dedicated to Brian Mitchell, one of the most talented people 1308I know. 1309Thanks for everything. 1310