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