1.\" $NetBSD: kauth.9,v 1.68 2008/02/16 16:39:34 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 February 16, 2008 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_CPU 186Check CPU-manipulation access. 187.Pp 188.Ar req 189can be any of the following: 190.Bl -tag 191.It Dv KAUTH_REQ_SYSTEM_CPU_SETSTATE 192Set CPU state, including setting it online or offline. 193.El 194.It Dv KAUTH_SYSTEM_DEBUG 195This request concentrates several debugging-related operations. 196.Ar req 197can be any of the following: 198.Bl -tag 199.It Dv KAUTH_REQ_SYSTEM_DEBUG_IPKDB 200Check if using 201.Xr ipkdb 4 202is allowed. 203.El 204.It Dv KAUTH_SYSTEM_FILEHANDLE 205Check if filehandle operations allowed. 206.It Dv KAUTH_SYSTEM_LKM 207Check if an LKM request is allowed. 208.Pp 209.Ar arg1 210is the command. 211.It Dv KAUTH_SYSTEM_MKNOD 212Check if creating devices is allowed. 213.It Dv KAUTH_SYSTEM_MOUNT 214Check if mount-related operations are allowed. 215.Pp 216.Ar req 217can be any of the following: 218.Bl -tag 219.It Dv KAUTH_REQ_SYSTEM_MOUNT_GET 220Check if retrieving information about a mount is allowed. 221.Ar arg1 222is a 223.Ft struct mount * 224with the mount structure in question, 225.Ar arg2 226is a 227.Ft void * 228with file-system specific data, if any. 229.It Dv KAUTH_REQ_SYSTEM_MOUNT_NEW 230Check if mounting a new file-system is allowed. 231.Pp 232.Ar arg1 233is the 234.Ft struct vnode * 235on which the file-system is to be mounted, 236.Ar arg2 237is an 238.Ft int 239with the mount flags, and 240.Ar arg3 241is a 242.Ft void * 243with file-system specific data, if any. 244.It Dv KAUTH_REQ_SYSTEM_MOUNT_UNMOUNT 245Checks if unmounting a file-system is allowed. 246.Pp 247.Ar arg1 248is a 249.Ft struct mount * 250with the mount in question. 251.It Dv KAUTH_REQ_SYSTEM_MOUNT_UPDATE 252Checks if updating an existing mount is allowed. 253.Pp 254.Ar arg1 255is the 256.Ft struct mount * 257of the existing mount, 258.Ar arg2 259is an 260.Ft int 261with the new mount flags, and 262.Ar arg3 263is a 264.Ft void * 265with file-system specific data, if any. 266.El 267.It Dv KAUTH_SYSTEM_PSET 268Check processor-set manipulation. 269.Pp 270.Ar req 271can be any of the following: 272.Bl -tag 273.It Dv KAUTH_REQ_SYSTEM_PSET_ASSIGN 274Change processor-set processor assignment. 275.It Dv KAUTH_REQ_SYSTEM_PSET_BIND 276Bind an LWP to a processor-set. 277.It Dv KAUTH_REQ_SYSTEM_PSET_CREATE 278Create a processor-set. 279.It Dv KAUTH_REQ_SYSTEM_PSET_DESTROY 280Destroy a processor-set. 281.El 282.It Dv KAUTH_SYSTEM_REBOOT 283Check if rebooting is allowed. 284.It Dv KAUTH_SYSTEM_SETIDCORE 285Check if changing coredump settings for set-id processes is allowed. 286.It Dv KAUTH_SYSTEM_SWAPCTL 287Check if privileged 288.Xr swapctl 2 289requests are allowed. 290.It Dv KAUTH_SYSTEM_SYSCTL 291This requests operations related to 292.Xr sysctl 9 . 293.Ar req 294indicates the specific request and can be one of the following: 295.Bl -tag 296.It Dv KAUTH_REQ_SYSTEM_SYSCTL_ADD 297Check if adding a 298.Xr sysctl 9 299node is allowed. 300.It Dv KAUTH_REQ_SYSTEM_SYSCTL_DELETE 301Check if deleting a 302.Xr sysctl 9 303node is allowed. 304.It Dv KAUTH_REQ_SYSTEM_SYSCTL_DESC 305Check if adding description to a 306.Xr sysctl 9 307node is allowed. 308.It Dv KAUTH_REQ_SYSTEM_SYSCTL_PRVT 309Check if accessing private 310.Xr sysctl 9 311nodes is allowed. 312.El 313.It Dv KAUTH_SYSTEM_TIME 314This request groups time-related operations. 315.Ar req 316can be any of the following: 317.Bl -tag 318.It Dv KAUTH_REQ_SYSTEM_TIME_ADJTIME 319Check if changing the time using 320.Xr adjtime 2 321is allowed. 322.It Dv KAUTH_REQ_SYSTEM_TIME_NTPADJTIME 323Check if setting the time using 324.Xr ntp_adjtime 2 325is allowed. 326.It Dv KAUTH_REQ_SYSTEM_TIME_SYSTEM 327Check if changing the time (usually via 328.Xr settimeofday 2 ) 329is allowed. 330.Pp 331.Ar arg1 332is a 333.Ft struct timespec * 334with the new time, 335.Ar arg2 336is a 337.Ft struct timeval * 338with the delta from the current time, 339.Ar arg3 340is a 341.Ft bool 342indicating whether the caller is a device context (eg. 343.Pa /dev/clockctl ) 344or not. 345.It Dv KAUTH_REQ_SYSTEM_TIME_RTCOFFSET 346Check if changing the RTC offset is allowed. 347.It Dv KAUTH_REQ_SYSTEM_TIME_TIMECOUNTERS 348Check if manipulating timecounters is allowed. 349.El 350.El 351.Ss Process Scope 352The process scope, 353.Dq org.netbsd.kauth.process , 354manages authorization requests related to processes in the system. 355.Pp 356The authorization wrapper for this scope is declared as 357.Pp 358.Ft int Fn kauth_authorize_process "kauth_cred_t cred" \ 359"kauth_action_t op" "struct proc *p" "void *arg1" "void *arg2" \ 360"void *arg3" 361.Pp 362The following operations are available for this scope: 363.Bl -tag 364.It Dv KAUTH_PROCESS_KTRACE 365Checks whether an object with one set of credentials can 366.Xr ktrace 1 367another process 368.Ar p , 369possibly with a different set of credentials. 370.Pp 371If 372.Ar arg1 373is 374.Dv KAUTH_REQ_PROCESS_KTRACE_PERSISTENT , 375this checks if persistent tracing can be done. 376Persistent tracing maintains the trace across a set-user-id/set-group-id 377.Xr exec 2 , 378and normally requires privileged credentials. 379.It Dv KAUTH_PROCESS_PROCFS 380Checks whether object with passed credentials can use 381.Em procfs 382to access process 383.Ar p . 384.Pp 385.Ar arg1 386is the 387.Ft struct pfsnode * 388for the target element in the target process, and 389.Ar arg2 390is the access type, which can be either 391.Dv KAUTH_REQ_PROCESS_PROCFS_CTL , 392.Dv KAUTH_REQ_PROCESS_PROCFS_READ , 393.Dv KAUTH_REQ_PROCESS_PROCFS_RW , 394or 395.Dv KAUTH_REQ_PROCESS_PROCFS_WRITE , 396indicating 397.Em control , 398.Em read , 399.Em read-write , 400or 401.Em write 402access respectively. 403.It Dv KAUTH_PROCESS_PTRACE 404Checks whether object with passed credentials can use 405.Xr ptrace 2 406to access process 407.Ar p . 408.Pp 409.Ar arg1 410is the 411.Xr ptrace 2 412command. 413.It Dv KAUTH_PROCESS_CANSEE 414Checks whether an object with one set of credentials can access 415information about another process, possibly with a different set of 416credentials. 417.Pp 418.Ar arg1 419indicates the class of information being viewed, and can either of 420.Dv KAUTH_REQ_PROCESS_CANSEE_ARGS , 421.Dv KAUTH_REQ_PROCESS_CANSEE_ENTRY , 422.Dv KAUTH_REQ_PROCESS_CANSEE_ENV , 423or 424.Dv KAUTH_REQ_PROCESS_CANSEE_OPENFILES . 425.It Dv KAUTH_PROCESS_SCHEDULER_GET 426Checks whether viewing the scheduler policy is allowed. 427.It Dv KAUTH_PROCESS_SCHEDULER_SET 428Checks whether setting the scheduler policy (class) is allowed. 429.It Dv KAUTH_PROCESS_SCHEDULER_GETAFFINITY 430Checks whether viewing the scheduler affinity is allowed. 431.It Dv KAUTH_PROCESS_SCHEDULER_SETAFFINITY 432Checks whether setting the scheduler affinity is allowed. 433.It Dv KAUTH_PROCESS_SCHEDULER_GETPARAMS 434Checks whether viewing scheduler parameters is allowed. 435.It Dv KAUTH_PROCESS_SCHEDULER_SETPARAMS 436Checks whether modifying scheduler parameters is allowed. 437.It Dv KAUTH_PROCESS_SIGNAL 438Checks whether an object with one set of credentials can post signals 439to another process. 440.Pp 441.Ar p 442is the process the signal is being posted to, and 443.Ar arg1 444is the signal number. 445.It Dv KAUTH_PROCESS_CORENAME 446Controls access to process corename. 447.Pp 448.Ar arg1 449can be 450.Dv KAUTH_REQ_PROCESS_CORENAME_GET 451or 452.Dv KAUTH_REQ_PROCESS_CORENAME_SET , 453indicating access to read or write the process' corename, respectively. 454.Pp 455When modifying the corename, 456.Ar arg2 457holds the new corename to be used. 458.It Dv KAUTH_PROCESS_FORK 459Checks if the process can fork. 460.Ar arg1 461is an 462.Ft int 463indicating how many processes exist on the system at the time of the check. 464.It Dv KAUTH_PROCESS_KEVENT_FILTER 465Checks whether setting a process 466.Xr kevent 2 467filter is allowed. 468.Pp 469.It Dv KAUTH_PROCESS_NICE 470Checks whether the 471.Em nice 472value of 473.Ar p 474can be changed to 475.Ar arg1 . 476.It Dv KAUTH_PROCESS_RLIMIT 477Controls access to process resource limits. 478.Pp 479.Ar arg1 480can be 481.Dv KAUTH_REQ_PROCESS_RLIMIT_GET 482or 483.Dv KAUTH_REQ_PROCESS_RLIMIT_SET , 484indicating access to read or write the process' resource limits, respectively. 485.Pp 486When modifying resource limits, 487.Ar arg2 488is the new value to be used and 489.Ar arg3 490indicates which resource limit is to be modified. 491.It Dv KAUTH_PROCESS_SETID 492Check if changing the user- or group-ids, groups, or login-name for 493.Ar p 494is allowed. 495.It Dv KAUTH_PROCESS_STOPFLAG 496Check if setting the stop flags for 497.Xr exec 3 , 498.Xr exit 3 , 499and 500.Xr fork 2 501is allowed. 502.Pp 503.Ar arg1 504indicates the flag, and can be either 505.Dv P_STOPEXEC , 506.Dv P_STOPEXIT , 507or 508.Dv P_STOPFORK 509respectively. 510.El 511.Ss Network Scope 512The network scope, 513.Dq org.netbsd.kauth.network , 514manages networking-related authorization requests in the kernel. 515.Pp 516The authorization wrapper for this scope is declared as 517.Pp 518.Ft int Fn kauth_authorize_network "kauth_cred_t cred" "kauth_action_t op" \ 519"enum kauth_network_req req" "void *arg1" "void *arg2" "void *arg3" 520.Pp 521The following operations are available for this scope: 522.Bl -tag 523.It Dv KAUTH_NETWORK_ALTQ 524Checks if an ALTQ operation is allowed. 525.Pp 526.Ar req 527indicates the ALTQ subsystem in question, and can be one of the following: 528.Pp 529.Bl -tag -compact 530.It Dv KAUTH_REQ_NETWORK_ALTQ_AFMAP 531.It Dv KAUTH_REQ_NETWORK_ALTQ_BLUE 532.It Dv KAUTH_REQ_NETWORK_ALTQ_CBQ 533.It Dv KAUTH_REQ_NETWORK_ALTQ_CDNR 534.It Dv KAUTH_REQ_NETWORK_ALTQ_CONF 535.It Dv KAUTH_REQ_NETWORK_ALTQ_FIFOQ 536.It Dv KAUTH_REQ_NETWORK_ALTQ_HFSC 537.It Dv KAUTH_REQ_NETWORK_ALTQ_JOBS 538.It Dv KAUTH_REQ_NETWORK_ALTQ_PRIQ 539.It Dv KAUTH_REQ_NETWORK_ALTQ_RED 540.It Dv KAUTH_REQ_NETWORK_ALTQ_RIO 541.It Dv KAUTH_REQ_NETWORK_ALTQ_WFQ 542.El 543.It Dv KAUTH_NETWORK_BIND 544Checks if a 545.Xr bind 2 546request is allowed. 547.Pp 548.Ar req 549allows to indicate the type of the request to structure listeners and callers 550easier. 551Supported request types: 552.Bl -tag 553.It Dv KAUTH_REQ_NETWORK_BIND_PRIVPORT 554Checks if binding to a privileged/reserved port is allowed. 555.El 556.It Dv KAUTH_NETWORK_FIREWALL 557Checks if firewall-related operations are allowed. 558.Pp 559.Ar req 560indicates the sub-action, and can be one of the following: 561.Bl -tag 562.It Dv KAUTH_REQ_NETWORK_FIREWALL_FW 563Modification of packet filtering rules. 564.It Dv KAUTH_REQ_NETWORK_FIREWALL_NAT 565Modification of NAT rules. 566.El 567.It Dv KAUTH_NETWORK_INTERFACE 568Checks if network interface-related operations are allowed. 569.Pp 570.Ar arg1 571is (optionally) the 572.Ft struct ifnet * 573associated with the interface. 574.Ar arg2 575is (optionally) an 576.Ft int 577describing the interface-specific operation. 578.Ar arg3 579is (optionally) a pointer to the interface-specific request structure. 580.Ar req 581indicates the sub-action, and can be one of the following: 582.Bl -tag 583.It Dv KAUTH_REQ_NETWORK_INTERFACE_GET 584Check if retrieving information from the device is allowed. 585.It Dv KAUTH_REQ_NETWORK_INTERFACE_GETPRIV 586Check if retrieving privileged information from the device is allowed. 587.It Dv KAUTH_REQ_NETWORK_INTERFACE_SET 588Check if setting parameters on the device is allowed. 589.It Dv KAUTH_REQ_NETWORK_INTERFACE_SETPRIV 590Check if setting privileged parameters on the device is allowed. 591.El 592.Pp 593Note that unless the 594.Ft struct ifnet * 595for the interface was passed in 596.Ar arg1 , 597there's no way to tell what structure 598.Ar arg3 599is. 600.It Dv KAUTH_NETWORK_FORWSRCRT 601Checks whether status of forwarding of source-routed packets can be modified 602or not. 603.It Dv KAUTH_NETWORK_ROUTE 604Checks if a routing-related request is allowed. 605.Pp 606.Ar arg1 607is the 608.Ft struct rt_msghdr * 609for the request. 610.It Dv KAUTH_NETWORK_SOCKET 611Checks if a socket related operation is allowed. 612.Pp 613.Ar req 614allows to indicate the type of the request to structure listeners and callers 615easier. 616Supported request types: 617.Bl -tag 618.It Dv KAUTH_REQ_NETWORK_SOCKET_RAWSOCK 619Checks if opening a raw socket is allowed. 620.It Dv KAUTH_REQ_NETWORK_SOCKET_OPEN 621Checks if opening a socket is allowed. 622.Ar arg1 , arg2 , 623and 624.Ar arg3 625are all 626.Ft int 627parameters describing the domain, socket type, and protocol, 628respectively. 629.It Dv KAUTH_REQ_NETWORK_SOCKET_CANSEE 630Checks if looking at the socket passed is allowed. 631.Pp 632.Ar arg1 633is a 634.Ft struct socket * 635describing the socket. 636.El 637.El 638.Ss Machine-dependent Scope 639The machine-dependent (machdep) scope, 640.Dq org.netbsd.kauth.machdep , 641manages machine-dependent authorization requests in the kernel. 642.Pp 643The authorization wrapper for this scope is declared as 644.Pp 645.Ft int Fn kauth_authorize_machdep "kauth_cred_t cred" "kauth_action_t op" \ 646"void *arg0" "void *arg1" "void *arg2" "void *arg3" 647.Pp 648The actions on this scope provide a set that may or may not affect all 649platforms. 650Below is a list of available actions, along with which platforms are affected 651by each. 652.Bl -tag 653.It Dv KAUTH_MACHDEP_IOPERM_GET 654Request to get the I/O permission level. 655Affects 656.Em amd64 , 657.Em i386 , 658.Em xen . 659.It Dv KAUTH_MACHDEP_IOPERM_SET 660Request to set the I/O permission level. 661Affects 662.Em amd64 , 663.Em i386 , 664.Em xen . 665.It Dv KAUTH_MACHDEP_IOPL 666Request to set the I/O privilege level. 667Affects 668.Em amd64 , 669.Em i386 , 670.Em xen . 671.It Dv KAUTH_MACHDEP_LDT_GET 672Request to get the LDT (local descriptor table). 673Affects 674.Em amd64 , 675.Em i386 , 676.Em xen . 677.It Dv KAUTH_MACHDEP_LDT_SET 678Request to set the LDT (local descriptor table). 679Affects 680.Em amd64 , 681.Em i386 , 682.Em xen . 683.It Dv KAUTH_MACHDEP_MTRR_GET 684Request to get the MTRR (memory type range registers). 685Affects 686.Em amd64 , 687.Em i386 , 688.Em xen . 689.It Dv KAUTH_MACHDEP_MTRR_SET 690Request to set the MTRR (memory type range registers). 691Affects 692.Em amd64 , 693.Em i386 , 694.Em xen . 695.It Dv KAUTH_MACHDEP_UNMANAGEDMEM 696Request to access unmanaged memory. 697Affects 698.Em alpha , 699.Em amd64 , 700.Em arm , 701.Em i386 , 702.Em powerpc , 703.Em sh3 , 704.Em vax , 705.Em xen . 706.El 707.Ss Device Scope 708The device scope, 709.Dq org.netbsd.kauth.device , 710manages authorization requests related to devices on the system. 711Devices can be, for example, terminals, tape drives, and any other hardware. 712Network devices specifically are handled by the 713.Em network 714scope. 715.Pp 716In addition to the standard authorization wrapper: 717.Pp 718.Ft int Fn kauth_authorize_device "kauth_cred_t cred" "kauth_action_t op" \ 719"void *arg0" "void *arg1" "void *arg2" "void *arg3" 720.Pp 721this scope provides authorization wrappers for various device types. 722.Pp 723.Ft int Fn kauth_authorize_device_tty "kauth_cred_t cred" "kauth_action_t op" \ 724"struct tty *tty" 725.Pp 726Authorizes requests for 727.Em terminal devices 728on the system. 729The third argument, 730.Ar tty , 731is the terminal device in question. 732It is passed to the listener as 733.Ar arg0 . 734The second argument, 735.Ar op , 736is the action and can be one of the following: 737.Bl -tag 738.It Dv KAUTH_DEVICE_TTY_OPEN 739Open the terminal device pointed to by 740.Ar tty . 741.It Dv KAUTH_DEVICE_TTY_PRIVSET 742Set privileged settings on the terminal device pointed to by 743.Ar tty . 744.It Dv KAUTH_DEVICE_TTY_STI 745Use the 746.Dq TIOCSTI 747device 748.Xr ioctl 2 , 749allowing to inject characters into the terminal buffer, simulating terminal 750input. 751.El 752.Pp 753.Ft int Fn kauth_authorize_device_spec "kauth_cred_t cred" \ 754"enum kauth_device_req req" "struct vnode *vp" 755.Pp 756Authorizes requests for 757.Em special files , 758usually disk devices, but also direct memory access, on the system. 759.Pp 760It passes 761.Dv KAUTH_DEVICE_RAWIO_SPEC 762as the action to the listener, and accepts two arguments. 763.Ar req , 764passed to the listener as 765.Ar arg0 , 766is access requested, and can be one of 767.Dv KAUTH_REQ_DEVICE_RAWIO_SPEC_READ , 768.Dv KAUTH_REQ_DEVICE_RAWIO_SPEC_WRITE , 769or 770.Dv KAUTH_REQ_DEVICE_RAWIO_SPEC_RW , 771representing read, write, or both read/write access respectively. 772.Ar vp 773is the vnode of the special file in question, and is passed to the listener as 774.Ar arg1 . 775.Pp 776Keep in mind that it is the responsibility of the security model developer to 777check whether the underlying device is a disk or the system memory, using 778.Fn iskmemdev : 779.Bd -literal -offset indent 780if ((vp-\*[Gt]v_type == VCHR) \*[Am]\*[Am] 781 iskmemdev(vp-\*[Gt]v_un.vu_specinfo-\*[Gt]si_rdev)) 782 /* system memory access */ 783.Ed 784.Pp 785.Ft int Fn kauth_authorize_device_passthru "kauth_cred_t cred" "dev_t dev" \ 786"u_long mode" "void *data" 787.Pp 788Authorizes hardware 789.Em passthru 790requests, or user commands passed directly to the hardware. 791These have the potential of resulting in direct disk and/or memory access. 792.Pp 793It passes 794.Dv KAUTH_DEVICE_RAWIO_PASSTHRU 795as the action to the listener, and accepts three arguments. 796.Ar dev , 797passed as 798.Ar arg1 799to the listener, is the device for which the request is made. 800.Ar mode , 801passed as 802.Ar arg0 803to the listener, is a generic representation of the access mode requested. 804It can be one or more (binary-OR'd) of the following: 805.Pp 806.Bl -tag -offset indent -compact 807.It KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READ 808.It KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF 809.It KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITE 810.It KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF 811.El 812.Pp 813.Ar data , 814passed as 815.Ar arg2 816to the listener, is device-specific data that may be associated with the 817request. 818.Ss Credentials Scope 819The credentials scope, 820.Dq org.netbsd.kauth.cred , 821is a special scope used internally by the 822.Nm 823framework to provide hooking to credential-related operations. 824.Pp 825It is a 826.Dq notify-only 827scope, allowing hooking operations such as initialization of new credentials, 828credential inheritance during a fork, and copying and freeing of credentials. 829The main purpose for this scope is to give a security model a way to control 830the aforementioned operations, especially in cases where the credentials 831hold security model-private data. 832.Pp 833Notifications are made using the following function, which is internal to 834.Nm : 835.Pp 836.Ft int Fn kauth_cred_hook "kauth_cred_t cred" "kauth_action_t action" \ 837"void *arg0" "void *arg1" 838.Pp 839With the following actions: 840.Bl -tag 841.It Dv KAUTH_CRED_COPY 842The credentials are being copied. 843.Ar cred 844are the credentials of the lwp context doing the copy, and 845.Ar arg0 846and 847.Ar arg1 848are both 849.Ft kauth_cred_t 850representing the 851.Dq from 852and 853.Dq to 854credentials, respectively. 855.It Dv KAUTH_CRED_FORK 856The credentials are being inherited from a parent to a child process during a 857fork. 858.Pp 859.Ar cred 860are the credentials of the lwp context doing the fork, and 861.Ar arg0 862and 863.Ar arg1 864are both 865.Ft struct proc * 866of the parent and child processes, respectively. 867.It Dv KAUTH_CRED_FREE 868The credentials in 869.Ar cred 870are being freed. 871.It Dv KAUTH_CRED_INIT 872The credentials in 873.Ar cred 874are being initialized. 875.El 876.Pp 877Since this is a notify-only scope, all listeners are required to return 878.Dv KAUTH_RESULT_ALLOW . 879.Ss Credentials Accessors and Mutators 880.Nm 881has a variety of accessor and mutator routines to handle 882.Ft kauth_cred_t 883objects. 884.Pp 885The following routines can be used to access and modify the user- and 886group-ids in a 887.Ft kauth_cred_t : 888.Bl -tag 889.It Ft uid_t Fn kauth_cred_getuid "kauth_cred_t cred" 890Returns the real user-id from 891.Ar cred . 892.It Ft uid_t Fn kauth_cred_geteuid "kauth_cred_t cred" 893Returns the effective user-id from 894.Ar cred . 895.It Ft uid_t Fn kauth_cred_getsvuid "kauth_cred_t cred" 896Returns the saved user-id from 897.Ar cred . 898.It Ft void Fn kauth_cred_setuid "kauth_cred_t cred" "uid_t uid" 899Sets the real user-id in 900.Ar cred 901to 902.Ar uid . 903.It Ft void Fn kauth_cred_seteuid "kauth_cred_t cred" "uid_t uid" 904Sets the effective user-id in 905.Ar cred 906to 907.Ar uid . 908.It Ft void Fn kauth_cred_setsvuid "kauth_cred_t cred" "uid_t uid" 909Sets the saved user-id in 910.Ar cred 911to 912.Ar uid . 913.It Ft gid_t Fn kauth_cred_getgid "kauth_cred_t cred" 914Returns the real group-id from 915.Ar cred . 916.It Ft gid_t Fn kauth_cred_getegid "kauth_cred_t cred" 917Returns the effective group-id from 918.Ar cred . 919.It Ft gid_t Fn kauth_cred_getsvgid "kauth_cred_t cred" 920Returns the saved group-id from 921.Ar cred . 922.It Ft void Fn kauth_cred_setgid "kauth_cred_t cred" "gid_t gid" 923Sets the real group-id in 924.Ar cred 925to 926.Ar gid . 927.It Ft void Fn kauth_cred_setegid "kauth_cred_t cred" "gid_t gid" 928Sets the effective group-id in 929.Ar cred 930to 931.Ar gid . 932.It Ft void Fn kauth_cred_setsvgid "kauth_cred_t cred" "gid_t gid" 933Sets the saved group-id in 934.Ar cred 935to 936.Ar gid . 937.It Ft u_int Fn kauth_cred_getrefcnt "kauth_cred_t cred" 938Return the reference count for 939.Ar cred . 940.El 941.Pp 942The following routines can be used to access and modify the group 943list in a 944.Ft kauth_cred_t : 945.Bl -tag 946.It Ft int Fn kauth_cred_ismember_gid "kauth_cred_t cred" "gid_t gid" \ 947"int *resultp" 948Checks if the group-id 949.Ar gid 950is a member in the group list of 951.Ar cred . 952.Pp 953If it is, 954.Ar resultp 955will be set to one, otherwise, to zero. 956.Pp 957The return value is an error code, or zero for success. 958.It Ft u_int Fn kauth_cred_ngroups "kauth_cred_t cred" 959Return the number of groups in the group list of 960.Ar cred . 961.It Ft gid_t Fn kauth_cred_group "kauth_cred_t cred" "u_int idx" 962Return the group-id of the group at index 963.Ar idx 964in the group list of 965.Ar cred . 966.It Ft int Fn kauth_cred_setgroups "kauth_cred_t cred" "gid_t *groups" \ 967"size_t ngroups" "uid_t gmuid" "enum uio_seg seg" 968Copy 969.Ar ngroups 970groups from array pointed to by 971.Ar groups 972to the group list in 973.Ar cred , 974adjusting the number of groups in 975.Ar cred 976appropriately. 977.Ar seg 978should be either 979.Dv UIO_USERSPACE 980or 981.Dv UIO_SYSSPACE 982indicating whether 983.Ar groups 984is a user or kernel space address. 985.Pp 986Any groups remaining will be set to an invalid value. 987.Pp 988.Ar gmuid 989is unused for now, and to maintain interface compatibility with the Darwin 990KPI. 991.Pp 992The return value is an error code, or zero for success. 993.It Ft int Fn kauth_cred_getgroups "kauth_cred_t cred" "gid_t *groups" \ 994"size_t ngroups" "enum uio_seg seg" 995Copy 996.Ar ngroups 997groups from the group list in 998.Ar cred 999to the buffer pointed to by 1000.Ar groups . 1001.Ar seg 1002should be either 1003.Dv UIO_USERSPACE 1004or 1005.Dv UIO_SYSSPACE 1006indicating whether 1007.Ar groups 1008is a user or kernel space address. 1009.Pp 1010The return value is an error code, or zero for success. 1011.El 1012.Ss Credential Private Data 1013.Nm 1014provides an interface to allow attaching security-model private data to 1015credentials. 1016.Pp 1017The use of this interface has two parts that can be divided to direct and 1018indirect control of the private-data. 1019Directly controlling the private data is done by using the below routines, 1020while the indirect control is often dictated by events such as process 1021fork, and is handled by listening on the credentials scope (see above). 1022.Pp 1023Attaching private data to credentials works by registering a key to serve 1024as a unique identifier, distinguishing various sets of private data that 1025may be associated with the credentials. 1026Registering, and deregistering, a key is done by using these routines: 1027.Pp 1028.Bl -tag 1029.It Ft int Fn kauth_register_key "const char *name" "kauth_key_t *keyp" 1030Register new key for private data for 1031.Ar name 1032(usually, the security model name). 1033.Ar keyp 1034will be used to return the key to be used in further calls. 1035.Pp 1036The function returns 0 on success and an error code (see 1037.Xr errno 2 ) 1038on failure. 1039.It Ft int Fn kauth_deregister_key "kauth_key_t key" 1040Deregister private data key 1041.Ar key . 1042.El 1043.Pp 1044Once registered, private data may be manipulated by the following routines: 1045.Bl -tag 1046.It Ft void Fn kauth_cred_setdata "kauth_cred_t cred" "kauth_key_t key" \ 1047"void *data" 1048Set private data for 1049.Ar key 1050in 1051.Ar cred 1052to be 1053.Ar data . 1054.It Ft void * Fn kauth_cred_getdata "kauth_cred_t cred" "kauth_key_t key" 1055Retrieve private data for 1056.Ar key 1057in 1058.Ar cred . 1059.El 1060.Pp 1061Note that it is required to use the above routines every time the private 1062data is changed, i.e., using 1063.Fn kauth_cred_getdata 1064and later modifying the private data should be accompanied by a call to 1065.Fn kauth_cred_setdata 1066with the 1067.Dq new 1068private data. 1069.Ss Credential Inheritance and Reference Counting 1070.Nm 1071provides an interface for handling shared credentials. 1072.Pp 1073When a 1074.Ft kauth_cred_t 1075is first allocated, its reference count is set to 1. 1076However, with time, its reference count can grow as more objects (processes, 1077LWPs, files, etc.) reference it. 1078.Pp 1079The following routines are available for managing credentials reference 1080counting: 1081.Bl -tag 1082.It Ft void Fn kauth_cred_hold "kauth_cred_t cred" 1083Increases reference count to 1084.Ar cred 1085by one. 1086.It Ft void Fn kauth_cred_free "kauth_cred_t cred" 1087Decreases the reference count to 1088.Ar cred 1089by one. 1090.Pp 1091If the reference count dropped to zero, the memory used by 1092.Ar cred 1093will be freed. 1094.El 1095.Pp 1096Credential inheritance happens during a 1097.Xr fork 2 , 1098and is handled by the following function: 1099.Pp 1100.Ft void Fn kauth_proc_fork "struct proc *parent" "struct proc *child" 1101.Pp 1102When called, it references the parent's credentials from the child, 1103and calls the credentials scope's hook with the 1104.Dv KAUTH_CRED_FORK 1105action to allow security model-specific handling of the inheritance 1106to take place. 1107.Ss Credentials Memory Management 1108Data-structures for credentials, listeners, and scopes are allocated from 1109memory pools managed by the 1110.Xr pool 9 1111subsystem. 1112.Pp 1113The 1114.Ft kauth_cred_t 1115objects have their own memory management routines: 1116.Bl -tag 1117.It Ft kauth_cred_t Fn kauth_cred_alloc "void" 1118Allocates a new 1119.Ft kauth_cred_t , 1120initializes its lock, and sets its reference count to one. 1121.El 1122.Ss Conversion Routines 1123Sometimes it might be necessary to convert a 1124.Ft kauth_cred_t 1125to userland's view of credentials, a 1126.Ft struct uucred , 1127or vice versa. 1128.Pp 1129The following routines are available for these cases: 1130.Bl -tag 1131.It Ft void Fn kauth_uucred_to_cred "kauth_cred_t cred" "const struct uucred *uucred" 1132Convert userland's view of credentials to a 1133.Ft kauth_cred_t . 1134.Pp 1135This includes effective user- and group-ids, a number of groups, and a group 1136list. 1137The reference count is set to one. 1138.Pp 1139Note that 1140.Nm 1141will try to copy as many groups as can be held inside a 1142.Ft kauth_cred_t . 1143.It Ft void Fn kauth_cred_to_uucred "struct uucred *uucred" "const kauth_cred_t cred" 1144Convert 1145.Ft kauth_cred_t 1146to userland's view of credentials. 1147.Pp 1148This includes effective user- and group-ids, a number of groups, and a group 1149list. 1150.Pp 1151Note that 1152.Nm 1153will try to copy as many groups as can be held inside a 1154.Ft struct uucred . 1155.It Ft int Fn kauth_cred_uucmp "kauth_cred_t cred" "struct uucred *uucred" 1156Compares 1157.Ar cred 1158with the userland credentials in 1159.Ar uucred . 1160.Pp 1161Common values that will be compared are effective user- and group-ids, and 1162the group list. 1163.El 1164.Ss Miscellaneous Routines 1165Other routines provided by 1166.Nm 1167are: 1168.Bl -tag 1169.It Ft void Fn kauth_cred_clone "kauth_cred_t cred1" "kauth_cred_t cred2" 1170Clone credentials from 1171.Ar cred1 1172to 1173.Ar cred2 , 1174except for the lock and reference count. 1175.Pp 1176.It Ft kauth_cred_t Fn kauth_cred_dup "kauth_cred_t cred" 1177Duplicate 1178.Ar cred . 1179.Pp 1180What this routine does is call 1181.Fn kauth_cred_alloc 1182followed by a call to 1183.Fn kauth_cred_clone . 1184.It Ft kauth_cred_t Fn kauth_cred_copy "kauth_cred_t cred" 1185Works like 1186.Fn kauth_cred_dup , 1187except for a few differences. 1188.Pp 1189If 1190.Ar cred 1191already has a reference count of one, it will be returned. 1192Otherwise, a new 1193.Ft kauth_cred_t 1194will be allocated and the credentials from 1195.Ar cred 1196will be cloned to it. 1197Last, a call to 1198.Fn kauth_cred_free 1199for 1200.Ar cred 1201will be done. 1202.It Ft kauth_cred_t Fn kauth_cred_get "void" 1203Return the credentials associated with the current LWP. 1204.El 1205.Ss Scope Management 1206.Nm 1207provides routines to manage the creation and deletion of scopes on the 1208system. 1209.Pp 1210Note that the built-in scopes, the 1211.Dq generic 1212scope and the 1213.Dq process 1214scope, can't be deleted. 1215.Bl -tag 1216.It Ft kauth_scope_t Fn kauth_register_scope "const char *id" \ 1217"kauth_scope_callback_t cb" "void *cookie" 1218Register a new scope on the system. 1219.Ar id 1220is the name of the scope, usually in reverse DNS-like notation. 1221For example, 1222.Dq org.netbsd.kauth.myscope . 1223.Ar cb 1224is the default listener, to which authorization requests for this scope 1225will be dispatched to. 1226.Ar cookie 1227is optional user-data that will be passed to all listeners 1228during authorization on the scope. 1229.It Ft void Fn kauth_deregister_scope "kauth_scope_t scope" 1230Deregister 1231.Ar scope 1232from the scopes available on the system, and free the 1233.Ft kauth_scope_t 1234object 1235.Ar scope . 1236.El 1237.Ss Listener Management 1238Listeners in 1239.Nm 1240are authorization callbacks that are called during an authorization 1241request in the scope which they belong to. 1242.Pp 1243When an authorization request is made, all listeners associated with 1244a scope are called to allow, deny, or defer the request. 1245.Pp 1246It is enough for one listener to deny the request in order for the 1247request to be denied; but all listeners are called during an authorization 1248process none-the-less. 1249All listeners are required to allow the request for it to be granted, 1250and in a case where all listeners defer the request -- leaving the decision 1251for other listeners -- the request is denied. 1252.Pp 1253The following KPI is provided for the management of listeners: 1254.Bl -tag 1255.It Ft kauth_listener_t Fn kauth_listen_scope "const char *id" \ 1256"kauth_scope_callback_t cb" "void *cookie" 1257Create a new listener on the scope with the id 1258.Ar id , 1259setting the default listener to 1260.Ar cb . 1261.Ar cookie 1262is optional user-data that will be passed to the listener when called 1263during an authorization request. 1264.It Ft void Fn kauth_unlisten_scope "kauth_listener_t listener" 1265Removes 1266.Ar listener 1267from the scope which it belongs to, ensuring it won't be called again, 1268and frees the 1269.Ft kauth_listener_t 1270object 1271.Ar listener . 1272.El 1273.Pp 1274.Nm 1275provides no means for synchronization within listeners. 1276It is the the programmer's responsibility to make sure data used by the 1277listener is properly locked during its use, as it can be accessed 1278simultaneously from the same listener called multiple times. 1279It is also the programmer's responsibility to do garbage collection after 1280the listener, possibly freeing any allocated data it used. 1281.Pp 1282The common method to do the above is by having a reference count to 1283each listener. 1284On entry to the listener, this reference count should be raised, and 1285on exit -- lowered. 1286.Pp 1287During the removal of a listener, first 1288.Fn kauth_scope_unlisten 1289should be called to make sure the listener code will not be entered in 1290the future. 1291Then, the code should wait (possibly sleeping) until the reference count 1292drops to zero. 1293When that happens, it is safe to do the final cleanup. 1294.Pp 1295Listeners might sleep, so no locks can be held when calling an authorization 1296wrapper. 1297.Sh EXAMPLES 1298Older code had no abstraction of the security model, so most privilege 1299checks looked like this: 1300.Bd -literal -offset indent 1301if (suser(cred, \*[Am]acflag) == 0) 1302 /* allow privileged operation */ 1303.Ed 1304.Pp 1305Using the new interface, you must ask for a specific privilege explicitly. 1306For example, checking whether it is possible to open a socket would look 1307something like this: 1308.Bd -literal -offset indent 1309if (kauth_authorize_network(cred, KAUTH_NETWORK_SOCKET, 1310 KAUTH_REQ_NETWORK_SOCKET_OPEN, PF_INET, SOCK_STREAM, 1311 IPPROTO_TCP) == 0) 1312 /* allow opening the socket */ 1313.Ed 1314.Pp 1315Note that the 1316.Em securelevel 1317implications were also integrated into the 1318.Nm 1319framework so you don't have to note anything special in the call to the 1320authorization wrapper, but rather just have to make sure the security 1321model handles the request as you expect it to. 1322.Pp 1323To do that you can just 1324.Xr grep 1 1325in the relevant security model directory and have a look at the code. 1326.Sh EXTENDING KAUTH 1327Although 1328.Nm 1329provides a large set of both detailed and more or less generic requests, 1330it might be needed eventually to introduce more scopes, actions, or 1331requests. 1332.Pp 1333Adding a new scope should happen only when an entire subsystem is 1334introduced and it is assumed other parts of the kernel may want to 1335interfere with its inner-workings. 1336When a subsystem that has the potential of impacting the security 1337if the system is introduced, existing security modules must be updated 1338to also handle actions on the newly added scope. 1339.Pp 1340New actions should be added when sets of operations not covered at all 1341belong in an already existing scope. 1342.Pp 1343Requests (or sub-actions) can be added as subsets of existing actions 1344when an operation that belongs in an already covered area is introduced. 1345.Pp 1346Note that all additions should include updates to this manual, the 1347security models shipped with 1348.Nx , 1349and the example skeleton security model. 1350.Sh SEE ALSO 1351.Xr secmodel 9 1352.Sh HISTORY 1353The kernel authorization framework first appeared in Mac OS X 10.4. 1354.Pp 1355The kernel authorization framework in 1356.Nx 1357first appeared in 1358.Nx 4.0 , 1359and is a clean-room implementation based on Apple TN2127, available at 1360http://developer.apple.com/technotes/tn2005/tn2127.html 1361.Sh NOTES 1362As 1363.Nm 1364in 1365.Nx 1366is still under active development, it is likely that the ABI, and possibly the 1367API, will differ between 1368.Nx 1369versions. 1370Developers are to take notice of this fact in order to avoid building code 1371that expects one version of the ABI and running it in a system with a different 1372one. 1373.Sh AUTHORS 1374.An Elad Efrat Aq elad@NetBSD.org 1375implemented the kernel authorization framework in 1376.Nx . 1377.Pp 1378.An Jason R. Thorpe Aq thorpej@NetBSD.org 1379provided guidance and answered questions about the Darwin implementation. 1380.Sh ONE MORE THING 1381The 1382.Nm 1383framework is dedicated to Brian Mitchell, one of the most talented people 1384I know. 1385Thanks for everything. 1386