xref: /netbsd-src/share/man/man9/kauth.9 (revision 5b239d0be1d6682ecf6030411841b40cc91e8b34)
1.\" $NetBSD: kauth.9,v 1.32 2006/11/04 10:47:37 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. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"      This product includes software developed by Elad Efrat.
17.\" 4. The name of the author may not be used to endorse or promote products
18.\"    derived from this software without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30.\"
31.Dd November 4, 2006
32.Dt KAUTH 9
33.Os
34.Sh NAME
35.Nm kauth
36.Nd kernel authorization framework
37.Sh SYNOPSIS
38.In sys/kauth.h
39.Sh DESCRIPTION
40.Nm ,
41or kernel authorization, is the subsystem managing all authorization requests
42inside the kernel.
43It manages user credentials and rights, and can be used
44to implement a system-wide security policy.
45It allows external modules to plug-in the authorization process.
46.Pp
47.Nm
48introduces some new concepts, namely
49.Dq scopes
50and
51.Dq listeners ,
52which will be detailed together with other useful information for kernel
53developers in this document.
54.Ss Types
55Some
56.Nm
57types include the following:
58.Bl -tag
59.It kauth_cred_t
60Representing credentials that can be associated with an object.
61Includes user- and group-ids (real, effective, and save) as well as group
62membership information.
63.It kauth_scope_t
64Describes a scope.
65.It kauth_listener_t
66Describes a listener.
67.El
68.Ss Terminology
69.Nm
70operates in various
71.Dq scopes ,
72each scope holding a group of
73.Dq listeners .
74.Pp
75Each listener works as a callback for when an authorization request within the
76scope is made.
77When such a request is made, all listeners on the scope are passed common
78information such as the credentials of the request context, an identifier for
79the requested operation, and possibly other information as well.
80.Pp
81Every listener examines the passed information and returns its decision
82regarding the requested operation.
83It can either allow, deny, or defer the operation -- in which case, the
84decision is left to the other listeners.
85.Pp
86For an operation to be allowed, all listeners must not return any deny
87or defer decisions.
88.Pp
89Scopes manage listeners that operate in the same aspect of the system.
90.Ss Kernel Programming Interface
91.Nm
92exports a KPI that allows developers both of
93.Nx
94and third-party products to authorize requests, access and modify credentials,
95create and remove scopes and listeners, and perform other miscellaneous operations on
96credentials.
97.Ss Authorization Requests
98.Nm
99provides a single authorization request routine, which all authorization
100requests go through.
101This routine dispatches the request to the listeners of the appropriate scope,
102together with four optional user-data variables, and returns the augmented
103result.
104.Pp
105It is declared as
106.Pp
107.Ft int Fn kauth_authorize_action "kauth_scope_t scope" "kauth_cred_t cred" \
108"kauth_action_t op" "void *arg0" "void *arg1" "void *arg2" "void *arg3"
109.Pp
110An authorization request can return one of two possible values.
111Zero indicates success -- the operation is allowed;
112.Er EPERM
113(see
114.Xr errno 2 )
115indicates failure -- the operation is denied.
116.Pp
117Each scope has its own authorization wrapper, to make it easy to call from various
118places by eliminating the need to specify the scope and/or cast values.
119The authorization wrappers are detailed in each scope's section.
120.Ss Generic Scope
121The generic scope,
122.Dq org.netbsd.kauth.generic ,
123manages generic authorization requests in the kernel.
124.Pp
125The authorization wrapper for this scope is declared as
126.Pp
127.Ft int Fn kauth_authorize_generic "kauth_cred_t cred" "kauth_action_t op" \
128"void *arg0"
129.Pp
130The following operations are available for this scope:
131.Bl -tag
132.It Dv KAUTH_GENERIC_ISSUSER
133Checks whether the credentials belong to the super-user.
134.Pp
135Using this request is strongly discouraged and should only be done as a
136temporary place-holder, as it is breaking the separation between the
137interface for authorization requests from the back-end implementation.
138.It Dv KAUTH_GENERIC_CANSEE
139Checks whether an object with one set of credentials can access
140information about another object, possibly with a different set of
141credentials.
142.Pp
143.Ar arg0
144contains the credentials of the object looked at.
145.Pp
146This request should be issued only in cases where generic credentials
147check is required; otherwise it is recommended to use the object-specific
148routines.
149.El
150.Ss System Scope
151The system scope,
152.Dq org.netbsd.kauth.system ,
153manages authorization requests affecting the entire system.
154.Pp
155The authorization wrapper for this scope is declared as
156.Pp
157.Ft int Fn kauth_authorize_system "kauth_cred_t cred" \
158"kauth_action_t op" "enum kauth_system_req req" "void *arg1" "void *arg2" \
159"void *arg3"
160.Pp
161The following requests are available for this scope:
162.Bl -tag
163.It Dv KAUTH_SYSTEM_ACCOUNTING
164Check if enabling/disabling accounting allowed.
165.It Dv KAUTH_SYSTEM_CHROOT
166.Ar req
167can be any of the following:
168.Bl -tag
169.It Dv KAUTH_REQ_SYSTEM_CHROOT_CHROOT
170Check if calling
171.Xr chroot 2
172is allowed.
173.It Dv KAUTH_REQ_SYSTEM_CHROOT_FCHROOT
174Check if calling
175.Xr fchroot 2
176is allowed.
177.El
178.It Dv KAUTH_SYSTEM_DEBUG
179This request concentrates several debugging-related operations.
180.Ar req
181can be any of the following:
182.Bl -tag
183.It Dv KAUTH_REQ_SYSTEM_DEBUG_IPKDB
184Check if using
185.Xr ipkdb 4
186is allowed.
187.El
188.It Dv KAUTH_SYSTEM_FILEHANDLE
189Check if filehandle operations allowed.
190.It Dv KAUTH_SYSTEM_LKM
191Check if an LKM request is allowed.
192.Pp
193.Ar arg1
194is the command.
195.It Dv KAUTH_SYSTEM_MKNOD
196Check if creating devices is allowed.
197.It Dv KAUTH_SYSTEM_REBOOT
198Check if rebooting is allowed.
199.It Dv KAUTH_SYSTEM_SETIDCORE
200Check if changing coredump settings for set-id processes is allowed.
201.It Dv KAUTH_SYSTEM_SWAPCTL
202Check if privileged
203.Xr swapctl 2
204requests are allowed.
205.It Dv KAUTH_SYSTEM_SYSCTL
206This requests operations related to
207.Xr sysctl 9 .
208.Ar req
209indicates the specific request and can be one of the following:
210.Bl -tag
211.It Dv KAUTH_REQ_SYSTEM_SYSCTL_ADD
212Check if adding a
213.Xr sysctl 9
214node is allowed.
215.It Dv KAUTH_REQ_SYSTEM_SYSCTL_DELETE
216Check if deleting a
217.Xr sysctl 9
218node is allowed.
219.It Dv KAUTH_REQ_SYSTEM_SYSCTL_DESC
220Check if adding description to a
221.Xr sysctl 9
222node is allowed.
223.It Dv KAUTH_REQ_SYSTEM_SYSCTL_PRVT
224Check if accessing private
225.Xr sysctl 9
226nodes is allowed.
227.El
228.It Dv KAUTH_SYSTEM_TIME
229This request groups time-related operations.
230.Ar req
231can be any of the following:
232.Bl -tag
233.It Dv KAUTH_REQ_SYSTEM_TIME_ADJTIME
234Check if changing the time using
235.Xr adjtime 2
236is allowed.
237.It Dv KAUTH_REQ_SYSTEM_TIME_BACKWARDS
238Check if setting the time backwards is allowed.
239.It Dv KAUTH_REQ_SYSTEM_TIME_NTPADJTIME
240Check if setting the time using
241.Xr ntp_adjtime 2
242is allowed.
243.It Dv KAUTH_REQ_SYSTEM_TIME_SYSTEM
244Check if changing the time (usually via
245.Xr settimeofday 2 )
246is allowed.
247.It Dv KAUTH_REQ_SYSTEM_TIME_RTCOFFSET
248Check if changing the RTC offset is allowed.
249.El
250.El
251.Ss Process Scope
252The process scope,
253.Dq org.netbsd.kauth.process ,
254manages authorization requests related to processes in the system.
255.Pp
256The authorization wrapper for this scope is declared as
257.Pp
258.Ft int Fn kauth_authorize_process "kauth_cred_t cred" \
259"kauth_action_t op" "struct proc *p" "void *arg1" "void *arg2" \
260"void *arg3"
261.Pp
262The following operations are available for this scope:
263.Bl -tag
264.It Dv KAUTH_PROCESS_CANSIGNAL
265Checks whether an object with one set of credentials can post signals
266to another process.
267.Pp
268.Ar p
269is the process the signal is being posted to, and
270.Ar arg1
271is the signal number.
272.It Dv KAUTH_PROCESS_CANSEE
273Checks whether an object with one set of credentials can access
274information about another process, possibly with a different set of
275credentials.
276.It Dv KAUTH_PROCESS_CORENAME
277Checks whether the coredump name for the process
278.Ar p
279can be changed.
280.It Dv KAUTH_PROCESS_RESOURCE
281Groups authorization requests related to resource management.
282.Ar arg0
283indicates the sub-action, and can be one of the following:
284.Bl -tag
285.It Dv KAUTH_REQ_PROCESS_RESOURCE_NICE
286Checks whether the
287.Em nice
288value of
289.Ar p
290can be changed to
291.Ar arg2 .
292.It Dv KAUTH_REQ_PROCESS_RESOURCE_RLIMIT
293Checks whether the
294.Em rlimit
295value for
296.Ar arg3
297in
298.Ar p
299can be set to
300.Ar arg2 .
301.El
302.It Dv KAUTH_PROCESS_SETID
303Check if changing the user- or group-ids, groups, or login-name for
304.Ar p
305is allowed.
306.El
307.Ss Network Scope
308The network scope,
309.Dq org.netbsd.kauth.network ,
310manages networking-related authorization requests in the kernel.
311.Pp
312The authorization wrapper for this scope is declared as
313.Pp
314.Ft int Fn kauth_authorize_network "kauth_cred_t cred" "kauth_action_t op" \
315"enum kauth_network_req req" "void *arg1" "void *arg2" "void *arg3"
316.Pp
317The following operations are available for this scope:
318.Bl -tag
319.It Dv KAUTH_NETWORK_ALTQ
320Checks if an ALTQ operation is allowed.
321.Pp
322.Ar req
323indicates the ALTQ subsystem in question, and can be one of the following:
324.Pp
325.Bl -tag -compact
326.It Dv KAUTH_REQ_NETWORK_ALTQ_AFMAP
327.It Dv KAUTH_REQ_NETWORK_ALTQ_BLUE
328.It Dv KAUTH_REQ_NETWORK_ALTQ_CBQ
329.It Dv KAUTH_REQ_NETWORK_ALTQ_CDNR
330.It Dv KAUTH_REQ_NETWORK_ALTQ_CONF
331.It Dv KAUTH_REQ_NETWORK_ALTQ_FIFOQ
332.It Dv KAUTH_REQ_NETWORK_ALTQ_HFSC
333.It Dv KAUTH_REQ_NETWORK_ALTQ_JOBS
334.It Dv KAUTH_REQ_NETWORK_ALTQ_PRIQ
335.It Dv KAUTH_REQ_NETWORK_ALTQ_RED
336.It Dv KAUTH_REQ_NETWORK_ALTQ_RIO
337.It Dv KAUTH_REQ_NETWORK_ALTQ_WFQ
338.El
339.It Dv KAUTH_NETWORK_BIND
340Checks if a
341.Xr bind 2
342request is allowed.
343.Pp
344.Ar req
345allows to indicate the type of the request to structure listeners and callers
346easier.
347Supported request types:
348.Bl -tag
349.It Dv KAUTH_REQ_NETWORK_BIND_PRIVPORT
350Checks if binding to a privileged/reserved port is allowed.
351.El
352.It Dv KAUTH_NETWORK_FIREWALL
353Checks if firewall-related operations are allowed.
354.Pp
355.Ar req
356indicates the sub-action, and can be one of the following:
357.Bl -tag
358.It Dv KAUTH_REQ_NETWORK_FIREWALL_FW
359Modification of packet filtering rules.
360.It Dv KAUTH_REQ_NETWORK_FIREWALL_NAT
361Modification of NAT rules.
362.El
363.It Dv KAUTH_NETWORK_INTERFACE
364Checks if network interface-related operations are allowed.
365.Pp
366.Ar arg1
367is (optionally) the
368.Ft struct ifnet *
369associated with the interface.
370.Ar arg2
371is (optionally) an
372.Ft int
373describing the interface-specific operation.
374.Ar arg3
375is (optionally) a pointer to the interface-specific request structure.
376.Ar req
377indicates the sub-action, and can be one of the following:
378.Bl -tag
379.It Dv KAUTH_REQ_NETWORK_INTERFACE_GET
380Check if retrieving information from the device is allowed.
381.It Dv KAUTH_REQ_NETWORK_INTERFACE_GETPRIV
382Check if retrieving privileged information from the device is allowed.
383.It Dv KAUTH_REQ_NETWORK_INTERFACE_SET
384Check if setting parameters on the device is allowed.
385.It Dv KAUTH_REQ_NETWORK_INTERFACE_SETPRIV
386Check if setting privileged parameters on the device is allowed.
387.El
388.Pp
389Note that unless the
390.Ft struct ifnet *
391for the interface was passed in
392.Ar arg1 ,
393there's no way to tell what structure
394.Ar arg3
395is.
396.It Dv KAUTH_NETWORK_FORWSRCRT
397Checks whether status of forwarding of source-routed packets can be modified
398or not.
399.It Dv KAUTH_NETWORK_ROUTE
400Checks if a routing-related request is allowed.
401.Pp
402.Ar arg1
403is the
404.Ft struct rt_msghdr *
405for the request.
406.It Dv KAUTH_NETWORK_SOCKET
407Checks if a socket related operation is allowed.
408.Pp
409.Ar req
410allows to indicate the type of the request to structure listeners and callers
411easier.
412Supported request types:
413.Bl -tag
414.It Dv KAUTH_REQ_NETWORK_SOCKET_RAWSOCK
415Checks if opening a raw socket is allowed.
416.It Dv KAUTH_REQ_NETWORK_SOCKET_OPEN
417Checks if opening a socket is allowed.
418.Ar arg1 , arg2 ,
419and
420.Ar arg3
421are all
422.Ft int
423parameters describing the domain, socket type, and protocol,
424respectively.
425.It Dv KAUTH_REQ_NETWORK_SOCKET_CANSEE
426Checks if looking at the socket passed is allowed.
427.Pp
428.Ar arg1
429is a
430.Ft struct socket *
431describing the socket.
432.El
433.El
434.Ss Machine-dependent Scope
435The machine-dependent (machdep) scope,
436.Dq org.netbsd.kauth.machdep ,
437manages machine-dependent authorization requests in the kernel.
438.Pp
439The authorization wrapper for this scope is declared as
440.Pp
441.Ft int Fn kauth_authorize_machdep "kauth_cred_t cred" "kauth_action_t op" \
442"enum kauth_machdep_req req" "void *arg1" "void *arg2" "void *arg3"
443.Pp
444In this scope,
445.Ar req
446always indicates the machine for the request.
447Below is the list of available request hierarchy.
448.Bl -tag
449.It Dv KAUTH_MACHDEP_X86
450The request is x86 specific.
451.Pp
452Available requests as
453.Ar arg1
454are:
455.Bl -tag
456.It Dv KAUTH_REQ_MACHDEP_X86_IOPL
457Checks if IOPL is allowed to be modified.
458.It Dv KAUTH_REQ_MACHDEP_X86_IOPERM
459Checks if IOPERM is allowed to be modified.
460.It Dv KAUTH_REQ_MACHDEP_X86_MTRR_SET
461Checks if the MTRR can be set.
462.El
463.It Dv KAUTH_MACHDEP_X86_64
464The request is x86-64 specific.
465.Pp
466Available requests as
467.Ar arg1
468are:
469.Bl -tag
470.It Dv KAUTH_REQ_MACHDEP_X86_64_MTRR_GET
471Check if MTRR values can be retrieved.
472.El
473.El
474.Ss Device Scope
475The device scope,
476.Dq org.netbsd.kauth.device ,
477manages authorization requests related to devices on the system.
478Devices can be, for example, terminals, tape drives, and any other hardware.
479Network devices specifically are handled by the
480.Em network
481scope.
482.Pp
483This scope provides authorization wrappers for various device types.
484.Pp
485.Ft int Fn kauth_authorize_device_tty "kauth_cred_t cred" "kauth_action_t op" \
486"struct tty *tty"
487.Pp
488Authorizes requests for
489.Em terminal devices
490on the system.
491The third argument,
492.Ar tty ,
493is the terminal device in question.
494It is passed to the listener as
495.Ar arg0 .
496The second argument,
497.Ar op ,
498is the action and can be one of the following:
499.Bl -tag
500.It Dv KAUTH_DEVICE_TTY_OPEN
501Open the terminal device pointed to by
502.Ar tty .
503.It Dv KAUTH_DEVICE_TTY_PRIVSET
504Set privileged settings on the terminal device pointed to by
505.Ar tty .
506.El
507.Pp
508.Ft int Fn kauth_authorize_device_spec "kauth_cred_t cred" \
509"enum kauth_device_req req" "struct vnode *vp"
510.Pp
511Authorizes requests for
512.Em special files ,
513usually disk devices, but also direct memory access, on the system.
514.Pp
515It passes
516.Dq KAUTH_DEVICE_RAWIO_SPEC
517as the action to the listener, and accepts two arguments.
518.Ar req ,
519passed to the listener as
520.Ar arg0 ,
521is access requested, and can be one of
522.Dq KAUTH_REQ_DEVICE_RAWIO_SPEC_READ ,
523.Dq KAUTH_REQ_DEVICE_RAWIO_SPEC_WRITE ,
524or
525.Dq KAUTH_REQ_DEVICE_RAWIO_SPEC_RW ,
526representing read, write, or both read/write access respectively.
527.Ar vp
528is the vnode of the special file in question, and is passed to the listener as
529.Ar arg1 .
530.Pp
531Keep in mind that it is the responsibility of the security model developer to
532check whether the underlying device is a disk or the system memory, using
533.Fn iskmemdev :
534.Bd -literal -offset indent
535if ((vp-\*[Gt]v_type == VCHR) \*[Am]\*[Am]
536    iskmemdev(vp-\*[Gt]v_un.vu_specinfo-\*[Gt]si_rdev))
537	/* system memory access */
538.Ed
539.Pp
540.Ft int Fn kauth_authorize_device_passthru "kauth_cred_t cred" "dev_t dev" \
541"void *data"
542.Pp
543Authorizes hardware
544.Em passthru
545requests, or user commands passed directly to the hardware.
546These have the potential of resulting in direct disk and/or memory access.
547.Pp
548It passes
549.Dq KAUTH_DEVICE_RAWIO_PASSTHRU
550as the action to the listener, and accepts two arguments.
551.Ar dev ,
552passed as
553.Ar arg1
554to the listener, is the device for which the request is made, and
555.Ar data ,
556passed as
557.Ar arg2
558to the listener, is device-specific data that may be associated with the
559request.
560.El
561.Ss Credentials Accessors and Mutators
562.Nm
563has a variety of accessor and mutator routines to handle
564.Ft kauth_cred_t
565objects.
566.Pp
567The following routines can be used to access and modify the user- and
568group-ids in a
569.Ft kauth_cred_t :
570.Bl -tag
571.It Ft uid_t Fn kauth_cred_getuid "kauth_cred_t cred"
572Returns the real user-id from
573.Ar cred .
574.It Ft uid_t Fn kauth_cred_geteuid "kauth_cred_t cred"
575Returns the effective user-id from
576.Ar cred .
577.It Ft uid_t Fn kauth_cred_getsvuid "kauth_cred_t cred"
578Returns the saved user-id from
579.Ar cred .
580.It Ft void Fn kauth_cred_setuid "kauth_cred_t cred" "uid_t uid"
581Sets the real user-id in
582.Ar cred
583to
584.Ar uid .
585.It Ft void Fn kauth_cred_seteuid "kauth_cred_t cred" "uid_t uid"
586Sets the effective user-id in
587.Ar cred
588to
589.Ar uid .
590.It Ft void Fn kauth_cred_setsvuid "kauth_cred_t cred" "uid_t uid"
591Sets the saved user-id in
592.Ar cred
593to
594.Ar uid .
595.It Ft gid_t Fn kauth_cred_getgid "kauth_cred_t cred"
596Returns the real group-id from
597.Ar cred .
598.It Ft gid_t Fn kauth_cred_getegid "kauth_cred_t cred"
599Returns the effective group-id from
600.Ar cred .
601.It Ft gid_t Fn kauth_cred_getsvgid "kauth_cred_t cred"
602Returns the saved group-id from
603.Ar cred .
604.It Ft void Fn kauth_cred_setgid "kauth_cred_t cred" "gid_t gid"
605Sets the real group-id in
606.Ar cred
607to
608.Ar gid .
609.It Ft void Fn kauth_cred_setegid "kauth_cred_t cred" "gid_t gid"
610Sets the effective group-id in
611.Ar cred
612to
613.Ar gid .
614.It Ft void Fn kauth_cred_setsvgid "kauth_cred_t cred" "gid_t gid"
615Sets the saved group-id in
616.Ar cred
617to
618.Ar gid .
619.It Ft u_int Fn kauth_cred_getrefcnt "kauth_cred_t cred"
620Return the reference count for
621.Ar cred .
622.El
623.Pp
624The following routines can be used to access and modify the group
625list in a
626.Ft kauth_cred_t :
627.Bl -tag
628.It Ft int Fn kauth_cred_ismember_gid "kauth_cred_t cred" "gid_t gid" \
629"int *resultp"
630Checks if the group-id
631.Ar gid
632is a member in the group list of
633.Ar cred .
634.Pp
635If it is,
636.Ar resultp
637will be set to one, otherwise, to zero.
638.Pp
639The return value is an error code, or zero for success.
640.It Ft u_int Fn kauth_cred_ngroups "kauth_cred_t cred"
641Return the number of groups in the group list of
642.Ar cred .
643.It Ft int Fn kauth_cred_group "kauth_cred_t cred" "u_int idx"
644Return the group-id of the group at index
645.Ar idx
646in the group list of
647.Ar cred .
648.It Ft int Fn kauth_cred_setgroups "kauth_cred_t cred" "gid_t *groups" \
649"size_t ngroups" "uid_t gmuid"
650Copy
651.Ar ngroups
652groups from array pointed to by
653.Ar groups
654to the group list in
655.Ar cred ,
656adjusting the number of groups in
657.Ar cred
658appropriately.
659.Pp
660Any groups remaining will be set to an invalid value.
661.Pp
662.Ar gmuid
663is unused for now, and to maintain interface compatibility with the Darwin
664KPI.
665.It Ft int Fn kauth_cred_getgroups "kauth_cred_t cred" "gid_t *groups" \
666"size_t ngroups"
667Copy
668.Ar ngroups
669groups from the group list in
670.Ar cred
671to the buffer pointed to by
672.Ar groups .
673.Pp
674The number of groups in
675.Ar cred
676will be returned.
677.El
678.Ss Credentials Inheritance and Reference Counting
679.Nm
680provides a KPI for handling a
681.Ft kauth_cred_t
682in shared credentials situations and credential inheritance.
683.Pp
684When a
685.Ft kauth_cred_t
686is first allocated, its reference count is set to 1.
687However, with time, its reference count can grow as more objects (processes,
688LWPs, files, etc.) reference it.
689One such case is during a
690.Xr fork 2
691where the child process and its LWPs inherit the credentials of the parent.
692.Pp
693To prevent freeing a
694.Ft kauth_cred_t
695while it is still referenced, the following routines are available to maintain
696its reference count:
697.Bl -tag
698.It Ft void Fn kauth_cred_hold "kauth_cred_t cred"
699Increases reference count to
700.Ar cred
701by one.
702.It Ft void Fn kauth_cred_free "kauth_cred_t cred"
703Decreases the reference count to
704.Ar cred
705by one.
706.Pp
707If the reference count dropped to zero, the memory used by
708.Ar cred
709will be returned back to the memory pool.
710.El
711.Ss Credentials Memory Management
712Data-structures for credentials, listeners, and scopes are allocated from
713memory pools managed by the
714.Xr pool 9
715subsystem.
716.Pp
717The
718.Ft kauth_cred_t
719objects have their own memory management routines:
720.Bl -tag
721.It Ft kauth_cred_t Fn kauth_cred_alloc "void"
722Allocates a new
723.Ft kauth_cred_t ,
724initializes its lock, and sets its reference count to one.
725.El
726.Ss Conversion Routines
727Sometimes it might be necessary to convert a
728.Ft kauth_cred_t
729to userland's view of credentials, a
730.Ft struct uucred ,
731or vice versa.
732.Pp
733The following routines are available for these cases:
734.Bl -tag
735.It Ft void Fn kauth_uucred_to_cred "kauth_cred_t cred" "const struct uucred *uucred"
736Convert userland's view of credentials to a
737.Ft kauth_cred_t .
738.Pp
739This includes effective user- and group-ids, a number of groups, and a group
740list.
741The reference count is set to one.
742.Pp
743Note that
744.Nm
745will try to copy as many groups as can be held inside a
746.Ft kauth_cred_t .
747.It Ft void Fn kauth_cred_to_uucred "struct uucred *uucred" "const kauth_cred_t cred"
748Convert
749.Ft kauth_cred_t
750to userland's view of credentials.
751.Pp
752This includes effective user- and group-ids, a number of groups, and a group
753list.
754.Pp
755Note that
756.Nm
757will try to copy as many groups as can be held inside a
758.Ft struct uucred .
759.It Ft int Fn kauth_cred_uucmp "kauth_cred_t cred" "struct uucred *uucred"
760Compares
761.Ar cred
762with the userland credentials in
763.Ar uucred .
764.Pp
765Common values that will be compared are effective user- and group-ids, and
766the group list.
767.El
768.Ss Miscellaneous Routines
769Other routines provided by
770.Nm
771are:
772.Bl -tag
773.It Ft void Fn kauth_cred_clone "kauth_cred_t cred1" "kauth_cred_t cred2"
774Clone credentials from
775.Ar cred1
776to
777.Ar cred2 ,
778except for the lock and reference count.
779.Pp
780.It Ft kauth_cred_t Fn kauth_cred_dup "kauth_cred_t cred"
781Duplicate
782.Ar cred .
783.Pp
784What this routine does is call
785.Fn kauth_cred_alloc
786followed by a call to
787.Fn kauth_cred_clone .
788.It Ft kauth_cred_t Fn kauth_cred_copy "kauth_cred_t cred"
789Works like
790.Fn kauth_cred_dup ,
791except for a few differences.
792.Pp
793If
794.Ar cred
795already has a reference count of one, it will be returned.
796Otherwise, a new
797.Ft kauth_cred_t
798will be allocated and the credentials from
799.Ar cred
800will be cloned to it.
801Last, a call to
802.Fn kauth_cred_free
803for
804.Ar cred
805will be done.
806.It Ft kauth_cred_t Fn kauth_cred_get "void"
807Return the credentials associated with the current LWP.
808.El
809.Ss Scope Management
810.Nm
811provides routines to manage the creation and deletion of scopes on the
812system.
813.Pp
814Note that the built-in scopes, the
815.Dq generic
816scope and the
817.Dq process
818scope, can't be deleted.
819.Bl -tag
820.It Ft kauth_scope_t Fn kauth_register_scope "const char *id" \
821"kauth_scope_callback_t cb" "void *cookie"
822Register a new scope on the system.
823.Ar id
824is the name of the scope, usually in reverse DNS-like notation.
825For example,
826.Dq org.netbsd.kauth.myscope .
827.Ar cb
828is the default listener, to which authorization requests for this scope
829will be dispatched to.
830.Ar cookie
831is optional user-data that will be passed to all listeners
832during authorization on the scope.
833.It Ft void Fn kauth_deregister_scope "kauth_scope_t scope"
834Deregister
835.Ar scope
836from the scopes available on the system.
837.El
838.Ss Listener Management
839Listeners in
840.Nm
841are authorization callbacks that are called during an authorization
842request in the scope which they belong to.
843.Pp
844When an authorization request is made, all listeners associated with
845a scope are called to allow, deny, or defer the request.
846.Pp
847It is enough for one listener to deny the request in order for the
848request to be denied; but all listeners are called during an authorization
849process none-the-less.
850All listeners are required to allow the request for it to be granted,
851and in a case where all listeners defer the request -- leaving the decision
852for other listeners -- the request is denied.
853.Pp
854The following KPI is provided for the management of listeners:
855.Bl -tag
856.It Ft kauth_listener_t Fn kauth_listen_scope "const char *id" \
857"kauth_scope_callback_t cb" "void *cookie"
858Create a new listener on the scope with the id
859.Ar id ,
860setting the default listener to
861.Ar cb .
862.\".Ar cookie
863.\"is optional user-data that will be passed to the listener when called
864.\"during an authorization request.
865.It Ft void Fn kauth_unlisten_scope "kauth_listener_t listener"
866Remove
867.Ar listener
868from the scope which it belongs to.
869.Pp
870Effectively what this does is is remove the callback from the chain of
871functions to be called when an authorization request is made, preventing
872from the listener from being entered in the future.
873.El
874.Pp
875.Nm
876provides no means for synchronization within listeners.
877It is the the programmer's responsibility to make sure data used by the
878listener is properly locked during its use, as it can be accessed
879simultaneously from the same listener called multiple times.
880It is also the programmer's responsibility to do garbage collection after
881the listener, possibly freeing any allocated data it used.
882.Pp
883The common method to do the above is by having a reference count to
884each listener.
885On entry to the listener, this reference count should be raised, and
886on exit -- lowered.
887.Pp
888During the removal of a listener, first
889.Fn kauth_scope_unlisten
890should be called to make sure the listener code will not be entered in
891the future.
892Then, the code should wait (possibly sleeping) until the reference count
893drops to zero.
894When that happens, it is safe to do the final cleanup.
895.Pp
896Listeners might sleep, so no locks can be held when calling an authorization
897wrapper.
898.\".Sh EXAMPLES
899.Sh SEE ALSO
900.Xr secmodel 9
901.Sh HISTORY
902The kernel authorization framework first appeared in Mac OS X 10.4.
903.Pp
904The kernel authorization framework in
905.Nx
906first appeared in
907.Nx 4.0 ,
908and is a clean-room implementation based on Apple TN2127, available at
909http://developer.apple.com/technotes/tn2005/tn2127.html
910.Sh AUTHORS
911.An Elad Efrat Aq elad@NetBSD.org
912implemented the kernel authorization framework in
913.Nx .
914.Pp
915.An Jason R. Thorpe Aq thorpej@NetBSD.org
916provided guidance and answered questions about the Darwin implementation.
917.Sh ONE MORE THING
918The
919.Nm
920framework is dedicated to Brian Mitchell, one of the most talented people
921I know.
922Thanks for everything.
923