xref: /netbsd-src/share/man/man9/kauth.9 (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1.\" $NetBSD: kauth.9,v 1.112 2018/07/15 05:16:41 maxv 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 July 14, 2018
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 return:
81.Pp
82.Bl -tag -width KAUTH_RESULT_ALLOW -compact
83.It Dv KAUTH_RESULT_ALLOW
84The listener allows the operation.
85.It Dv KAUTH_RESULT_DENY
86The listener denies the operation.
87.It Dv KAUTH_RESULT_DEFER
88The listener defers the decision to other listeners.
89.El
90.Pp
91For an operation to be allowed, at least one listener has to return
92.Dv KAUTH_RESULT_ALLOW
93while no other listener returned
94.Dv KAUTH_RESULT_DENY .
95.Pp
96Scopes manage listeners that operate in the same aspect of the system.
97.Ss Kernel Programming Interface
98.Nm
99exports a KPI that allows developers both of
100.Nx
101and third-party products to authorize requests, access and modify credentials,
102create and remove scopes and listeners, and perform other miscellaneous operations on
103credentials.
104.Ss Authorization Requests
105.Nm
106provides a single authorization request routine, which all authorization
107requests go through.
108This routine dispatches the request to the listeners of the appropriate scope,
109together with four optional user-data variables, and returns the augmented
110result.
111.Pp
112It is declared as
113.Pp
114.Ft int Fn kauth_authorize_action "kauth_scope_t scope" "kauth_cred_t cred" \
115"kauth_action_t op" "void *arg0" "void *arg1" "void *arg2" "void *arg3"
116.Pp
117An authorization request can return one of two possible values:
118.Bl -tag -width ".It Dv 0 Po zero Pc" -compact
119.It Dv 0 Po zero Pc
120indicates success; operation is allowed.
121.It Dv EPERM
122indicates failure; operation is denied.
123See
124.Xr errno 2 .
125.El
126.Pp
127Each scope has its own authorization wrapper, to make it easy to call from various
128places by eliminating the need to specify the scope and/or cast values.
129The authorization wrappers are detailed in each scope's section.
130.Pp
131.Fn kauth_authorize_action
132has several special cases, when it will always allow the request.
133These are for when the request is issued by the kernel itself (indicated by the
134credentials being either
135.Dv NOCRED
136or
137.Dv FSCRED ) ,
138or when there was no definitive decision from any of the listeners (i.e., it
139was not explicitly allowed or denied) and no security model was loaded.
140.Ss Generic Scope
141The generic scope,
142.Dq org.netbsd.kauth.generic ,
143manages generic authorization requests in the kernel.
144.Pp
145The authorization wrapper for this scope is declared as
146.Pp
147.Ft int Fn kauth_authorize_generic "kauth_cred_t cred" "kauth_action_t op" \
148"void *arg0"
149.Pp
150The following operations are available for this scope:
151.Bl -tag -width compact
152.It Dv KAUTH_GENERIC_ISSUSER
153Checks whether the credentials belong to the super-user.
154.Pp
155Using this request is strongly discouraged and should only be done as a
156temporary place-holder, as it is breaking the separation between the
157interface for authorization requests from the back-end implementation.
158.El
159.Ss System Scope
160The system scope,
161.Dq org.netbsd.kauth.system ,
162manages authorization requests affecting the entire system.
163.Pp
164The authorization wrapper for this scope is declared as
165.Pp
166.Ft int Fn kauth_authorize_system "kauth_cred_t cred" \
167"kauth_action_t op" "enum kauth_system_req req" "void *arg1" "void *arg2" \
168"void *arg3"
169.Pp
170The following requests are available for this scope:
171.Bl -tag -width compact
172.It Dv KAUTH_SYSTEM_ACCOUNTING
173Check if enabling/disabling accounting allowed.
174.It Dv KAUTH_SYSTEM_CHROOT
175.Ar req
176can be any of the following:
177.Bl -tag -width compact
178.It Dv KAUTH_REQ_SYSTEM_CHROOT_CHROOT
179Check if calling
180.Xr chroot 2
181is allowed.
182.It Dv KAUTH_REQ_SYSTEM_CHROOT_FCHROOT
183Check if calling
184.Xr fchroot 2
185is allowed.
186.El
187.It Dv KAUTH_SYSTEM_CPU
188Check CPU-manipulation access.
189.Pp
190.Ar req
191can be any of the following:
192.Bl -tag -width compact
193.It Dv KAUTH_REQ_SYSTEM_CPU_SETSTATE
194Set CPU state, including setting it online or offline.
195.El
196.It Dv KAUTH_SYSTEM_DEBUG
197This request concentrates several debugging-related operations.
198.It Dv KAUTH_SYSTEM_DEVMAPPER
199Check if operations on the device mapper
200.Xr dm 4
201device are allowed.
202.It Dv KAUTH_SYSTEM_FILEHANDLE
203Check if file handle operations allowed.
204.It Dv KAUTH_SYSTEM_FS_EXTATTR
205Check if starting, stopping, enabling, or disabling extended attributes
206is allowed.
207.Ar arg1
208is a
209.Ft struct mount *
210of the mount-point on which the operation is performed.
211.It Dv KAUTH_SYSTEM_FS_SNAPSHOT
212Check if setting up a file system snapshot is allowed.
213.Ar arg1
214is a
215.Ft struct mount *
216of the mount-point of which the snapshot is taken, and
217.Ar arg2
218is a
219.Ft struct vnode *
220of the vnode where the snapshot is expected to be.
221.It Dv KAUTH_SYSTEM_FS_QUOTA
222Check if file system quota operations are allowed.
223.Pp
224.Ar arg1
225is a
226.Ft struct mount *
227describing the file system mount in question.
228.Ar req
229can be one of the following:
230.Bl -tag -width compact
231.It Dv KAUTH_REQ_SYSTEM_FS_QUOTA_GET
232Check if retrieving quota information is allowed.
233.Pp
234.Ar arg2
235is a
236.Ft uid_t
237with the user-id of the user whose quota information is to be retrieved.
238.It Dv KAUTH_REQ_SYSTEM_FS_QUOTA_ONOFF
239Check if turning quota on/off is allowed.
240.It Dv KAUTH_REQ_SYSTEM_FS_QUOTA_MANAGE
241Check if managing the quota by setting the quota/quota use is allowed.
242.Pp
243.Ar arg2
244is a
245.Ft uid_t
246with the user-id of the user whose quota/quota use is to be set.
247.It Dv KAUTH_REQ_SYSTEM_FS_QUOTA_NOLIMIT
248Check if bypassing the quota (not enforcing it) is allowed.
249.El
250.It Dv KAUTH_SYSTEM_FS_RESERVEDSPACE
251Check if using the file system reserved space is allowed.
252.It Dv KAUTH_SYSTEM_LFS
253Check if LFS-related operations are allowed.
254.Ar req
255can be one of the following:
256.Bl -tag -width compact
257.It Dv KAUTH_REQ_SYSTEM_LFS_MARKV
258Check if calling
259.Xr lfs_markv 2
260is allowed.
261.It Dv KAUTH_REQ_SYSTEM_LFS_BMAPV
262Check if calling
263.Xr lfs_bmapv 2
264is allowed.
265.It Dv KAUTH_REQ_SYSTEM_LFS_SEGCLEAN
266Check if calling
267.Xr lfs_segclean 2
268is allowed.
269.It Dv KAUTH_REQ_SYSTEM_LFS_SEGWAIT
270Check if calling
271.Xr lfs_segwait 2
272is allowed.
273.It Dv KAUTH_REQ_SYSTEM_LFS_FCNTL
274Check if operations on LFS through
275.Xr fcntl 2
276are allowed.
277.El
278.It Dv KAUTH_SYSTEM_MAP_VA_ZERO
279Check if changing the status of memory mapping of virtual address zero
280is allowed.
281.It Dv KAUTH_SYSTEM_MODULE
282Check if a module request is allowed.
283.Pp
284.Ar arg1
285is the command.
286.It Dv KAUTH_SYSTEM_MKNOD
287Check if creating devices is allowed.
288.It Dv KAUTH_SYSTEM_MOUNT
289Check if mount-related operations are allowed.
290.Pp
291.Ar req
292can be any of the following:
293.Bl -tag -width compact
294.It Dv KAUTH_REQ_SYSTEM_MOUNT_DEVICE
295Check if mounting a device is allowed.
296.Ar arg1
297is a
298.Ft vnode_t *
299of the device,
300.Ar arg2
301is a
302.Ft struct mount *
303with the mount-point, and
304.Ar arg3
305is a
306.Ft mode_t
307with the desired access mode.
308.It Dv KAUTH_REQ_SYSTEM_MOUNT_GET
309Check if retrieving information about a mount is allowed.
310.Ar arg1
311is a
312.Ft struct mount *
313with the mount structure in question,
314.Ar arg2
315is a
316.Ft void *
317with file system specific data, if any.
318.It Dv KAUTH_REQ_SYSTEM_MOUNT_NEW
319Check if mounting a new file system is allowed.
320.Pp
321.Ar arg1
322is the
323.Ft struct vnode *
324on which the file system is to be mounted,
325.Ar arg2
326is an
327.Ft int
328with the mount flags, and
329.Ar arg3
330is a
331.Ft void *
332with file system specific data, if any.
333.It Dv KAUTH_REQ_SYSTEM_MOUNT_UNMOUNT
334Checks if unmounting a file system is allowed.
335.Pp
336.Ar arg1
337is a
338.Ft struct mount *
339with the mount in question.
340.It Dv KAUTH_REQ_SYSTEM_MOUNT_UPDATE
341Checks if updating an existing mount is allowed.
342.Pp
343.Ar arg1
344is the
345.Ft struct mount *
346of the existing mount,
347.Ar arg2
348is an
349.Ft int
350with the new mount flags, and
351.Ar arg3
352is a
353.Ft void *
354with file system specific data, if any.
355.It Dv KAUTH_REQ_SYSTEM_MOUNT_UMAP
356Check if mounting the user and group id remapping file system.
357See
358.Xr mount_umap 8 .
359.El
360.It Dv KAUTH_SYSTEM_MQUEUE
361Check if bypassing permissions on a message queue object are allowed.
362.Ar arg1
363is a
364.Ft mqueue_t *
365describing the message queue.
366.It Dv KAUTH_SYSTEM_PSET
367Check processor-set manipulation.
368.Pp
369.Ar req
370can be any of the following:
371.Bl -tag -width compact
372.It Dv KAUTH_REQ_SYSTEM_PSET_ASSIGN
373Change processor-set processor assignment.
374.It Dv KAUTH_REQ_SYSTEM_PSET_BIND
375Bind an LWP to a processor-set.
376.It Dv KAUTH_REQ_SYSTEM_PSET_CREATE
377Create a processor-set.
378.It Dv KAUTH_REQ_SYSTEM_PSET_DESTROY
379Destroy a processor-set.
380.El
381.It Dv KAUTH_SYSTEM_REBOOT
382Check if rebooting is allowed.
383.It Dv KAUTH_SYSTEM_SETIDCORE
384Check if changing coredump settings for set-id processes is allowed.
385.It Dv KAUTH_SYSTEM_SEMAPHORE
386Check if access to a kernel semaphore is allowed.
387.Ar arg1
388is a
389.Ft ksem_t *
390describing the semaphore.
391.It Dv KAUTH_SYSTEM_SWAPCTL
392Check if privileged
393.Xr swapctl 2
394requests are allowed.
395.It Dv KAUTH_SYSTEM_SYSCTL
396This requests operations related to
397.Xr sysctl 9 .
398.Ar req
399indicates the specific request and can be one of the following:
400.Bl -tag -width compact
401.It Dv KAUTH_REQ_SYSTEM_SYSCTL_ADD
402Check if adding a
403.Xr sysctl 9
404node is allowed.
405.It Dv KAUTH_REQ_SYSTEM_SYSCTL_DELETE
406Check if deleting a
407.Xr sysctl 9
408node is allowed.
409.It Dv KAUTH_REQ_SYSTEM_SYSCTL_DESC
410Check if adding description to a
411.Xr sysctl 9
412node is allowed.
413.It Dv KAUTH_REQ_SYSTEM_SYSCTL_MODIFY
414Check if modifying a
415.Xr sysctl 9
416node variable that doesn't have a custom sysctl helper function is allowed.
417.Pp
418This request might be deprecated in the future.
419.It Dv KAUTH_REQ_SYSTEM_SYSCTL_PRVT
420Check if accessing private
421.Xr sysctl 9
422nodes is allowed.
423.El
424.It Dv KAUTH_SYSTEM_SYSVIPC
425Check SysV IPC related operations.
426.Ar req
427indicates the specific request and can be one of the following:
428.Bl -tag -width compact
429.It Dv KAUTH_REQ_SYSTEM_SYSVIPC_BYPASS
430Check if bypassing a SysV IPC object's permissions is allowed.
431.Ar arg1
432is a
433.Ft struct ipc_perm *
434with the object's permissions and
435.Ar arg2
436is a
437.Ft mode_t
438indicating the requested access mode.
439.It Dv KAUTH_REQ_SYSTEM_SYSVIPC_SHM_LOCK
440Check if shared memory locking is allowed.
441.It Dv KAUTH_REQ_SYSTEM_SYSVIPC_SHM_UNLOCK
442Check if shared memory unlocking is allowed.
443.It Dv KAUTH_REQ_SYSTEM_SYSVIPC_MSGQ_OVERSIZE
444Check if oversizing a message queue is allowed.
445.Ar arg1
446is a
447.Ft msglen_t
448indicating the size of the message buffer, and
449.Ar arg2
450is a
451.Ft msglen_t
452indicating the size of the message queue.
453.El
454.It Dv KAUTH_SYSTEM_TIME
455This request groups time-related operations.
456.Ar req
457can be any of the following:
458.Bl -tag -width compact
459.It Dv KAUTH_REQ_SYSTEM_TIME_ADJTIME
460Check if changing the time using
461.Xr adjtime 2
462is allowed.
463.It Dv KAUTH_REQ_SYSTEM_TIME_NTPADJTIME
464Check if setting the time using
465.Xr ntp_adjtime 2
466is allowed.
467.It Dv KAUTH_REQ_SYSTEM_TIME_SYSTEM
468Check if changing the time (usually via
469.Xr settimeofday 2 )
470is allowed.
471.Pp
472.Ar arg1
473is a
474.Ft struct timespec *
475with the new time,
476.Ar arg2
477is a
478.Ft struct timeval *
479with the delta from the current time,
480.Ar arg3
481is a
482.Ft bool
483indicating whether the caller is a device context (e.g.
484.Pa /dev/clockctl )
485or not.
486.It Dv KAUTH_REQ_SYSTEM_TIME_RTCOFFSET
487Check if changing the RTC offset is allowed.
488.It Dv KAUTH_REQ_SYSTEM_TIME_TIMECOUNTERS
489Check if manipulating timecounters is allowed.
490.El
491.It Dv KAUTH_SYSTEM_VERIEXEC
492Check if operations on the
493.Xr veriexec 8
494subsystem are allowed.
495.Ar req
496can be one of the following:
497.Bl -tag -width compact
498.It Dv KAUTH_REQ_SYSTEM_VERIEXEC_ACCESS
499Check if access to the
500.Xr veriexec 8
501subsystem is allowed.
502.It Dv KAUTH_REQ_SYSTEM_VERIEXEC_MODIFY
503Check if modifications to the state of
504.Xr veriexec 8
505are allowed.
506.El
507.El
508.Ss Process Scope
509The process scope,
510.Dq org.netbsd.kauth.process ,
511manages authorization requests related to processes in the system.
512.Pp
513The authorization wrapper for this scope is declared as
514.Pp
515.Ft int Fn kauth_authorize_process "kauth_cred_t cred" \
516"kauth_action_t op" "struct proc *p" "void *arg1" "void *arg2" \
517"void *arg3"
518.Pp
519The following operations are available for this scope:
520.Bl -tag -width compact
521.It Dv KAUTH_PROCESS_KTRACE
522Checks whether an object with one set of credentials can
523.Xr ktrace 1
524another process
525.Ar p ,
526possibly with a different set of credentials.
527.Pp
528If
529.Ar arg1
530is
531.Dv KAUTH_REQ_PROCESS_KTRACE_PERSISTENT ,
532this checks if persistent tracing can be done.
533Persistent tracing maintains the trace across a set-user-id/set-group-id
534.Xr exec 3 ,
535and normally requires privileged credentials.
536.It Dv KAUTH_PROCESS_PROCFS
537Checks whether object with passed credentials can use
538.Em procfs
539to access process
540.Ar p .
541.Pp
542.Ar arg1
543is the
544.Ft struct pfsnode *
545for the target element in the target process, and
546.Ar arg2
547is the access type, which can be either
548.Dv KAUTH_REQ_PROCESS_PROCFS_READ ,
549.Dv KAUTH_REQ_PROCESS_PROCFS_RW ,
550or
551.Dv KAUTH_REQ_PROCESS_PROCFS_WRITE ,
552indicating
553.Em control ,
554.Em read ,
555.Em read-write ,
556or
557.Em write
558access respectively.
559.It Dv KAUTH_PROCESS_PTRACE
560Checks whether object with passed credentials can use
561.Xr ptrace 2
562to access process
563.Ar p .
564.Pp
565.Ar arg1
566is the
567.Xr ptrace 2
568command.
569.It Dv KAUTH_PROCESS_CANSEE
570Checks whether an object with one set of credentials can access
571information about another process, possibly with a different set of
572credentials.
573.Pp
574.Ar arg1
575indicates the class of information being viewed, and can be either of
576.Dv KAUTH_REQ_PROCESS_CANSEE_ARGS ,
577.Dv KAUTH_REQ_PROCESS_CANSEE_ENTRY ,
578.Dv KAUTH_REQ_PROCESS_CANSEE_ENV ,
579or
580.Dv KAUTH_REQ_PROCESS_CANSEE_OPENFILES .
581.It Dv KAUTH_PROCESS_SCHEDULER_GETAFFINITY
582Checks whether viewing the scheduler affinity is allowed.
583.It Dv KAUTH_PROCESS_SCHEDULER_SETAFFINITY
584Checks whether setting the scheduler affinity is allowed.
585.It Dv KAUTH_PROCESS_SCHEDULER_GETPARAM
586Checks whether viewing the scheduler policy and parameters is allowed.
587.It Dv KAUTH_PROCESS_SCHEDULER_SETPARAM
588Checks whether modifying the scheduler policy and parameters is allowed.
589.It Dv KAUTH_PROCESS_SIGNAL
590Checks whether an object with one set of credentials can post signals
591to another process.
592.Pp
593.Ar p
594is the process the signal is being posted to, and
595.Ar arg1
596is the signal number.
597.It Dv KAUTH_PROCESS_CORENAME
598Controls access to process corename.
599.Pp
600.Ar arg1
601can be
602.Dv KAUTH_REQ_PROCESS_CORENAME_GET
603or
604.Dv KAUTH_REQ_PROCESS_CORENAME_SET ,
605indicating access to read or write the process' corename, respectively.
606.Pp
607When modifying the corename,
608.Ar arg2
609holds the new corename to be used.
610.It Dv KAUTH_PROCESS_FORK
611Checks if the process can fork.
612.Ar arg1
613is an
614.Ft int
615indicating how many processes exist on the system at the time of the check.
616.It Dv KAUTH_PROCESS_KEVENT_FILTER
617Checks whether setting a process
618.Xr kevent 2
619filter is allowed.
620.It Dv KAUTH_PROCESS_NICE
621Checks whether the
622.Em nice
623value of
624.Ar p
625can be changed to
626.Ar arg1 .
627.It Dv KAUTH_PROCESS_RLIMIT
628Controls access to process resource limits.
629.Pp
630.Ar arg1
631can be
632.Dv KAUTH_REQ_PROCESS_RLIMIT_GET
633or
634.Dv KAUTH_REQ_PROCESS_RLIMIT_SET ,
635indicating access to read or write the process' resource limits, respectively, or
636.Dv KAUTH_REQ_PROCESS_RLIMIT_BYPASS
637to check if the limit enforcement can be bypassed.
638.Pp
639When modifying resource limits,
640.Ar arg2
641is the new value to be used and
642.Ar arg3
643indicates which resource limit is to be modified.
644.It Dv KAUTH_PROCESS_SETID
645Check if changing the user- or group-ids, groups, or login-name for
646.Ar p
647is allowed.
648.It Dv KAUTH_PROCESS_STOPFLAG
649Check if setting the stop flags for
650.Xr exec 3 ,
651.Xr exit 3 ,
652and
653.Xr fork 2
654is allowed.
655.Pp
656.Ar arg1
657indicates the flag, and can be either
658.Dv P_STOPEXEC ,
659.Dv P_STOPEXIT ,
660or
661.Dv P_STOPFORK
662respectively.
663.El
664.Ss Network Scope
665The network scope,
666.Dq org.netbsd.kauth.network ,
667manages networking-related authorization requests in the kernel.
668.Pp
669The authorization wrapper for this scope is declared as
670.Pp
671.Ft int Fn kauth_authorize_network "kauth_cred_t cred" "kauth_action_t op" \
672"enum kauth_network_req req" "void *arg1" "void *arg2" "void *arg3"
673.Pp
674The following operations are available for this scope:
675.Bl -tag -width compact
676.It Dv KAUTH_NETWORK_ALTQ
677Checks if an ALTQ operation is allowed.
678.Pp
679.Ar req
680indicates the ALTQ subsystem in question, and can be one of the following:
681.Pp
682.Bl -tag -compact -width compact
683.It Dv KAUTH_REQ_NETWORK_ALTQ_AFMAP
684.It Dv KAUTH_REQ_NETWORK_ALTQ_BLUE
685.It Dv KAUTH_REQ_NETWORK_ALTQ_CBQ
686.It Dv KAUTH_REQ_NETWORK_ALTQ_CDNR
687.It Dv KAUTH_REQ_NETWORK_ALTQ_CONF
688.It Dv KAUTH_REQ_NETWORK_ALTQ_FIFOQ
689.It Dv KAUTH_REQ_NETWORK_ALTQ_HFSC
690.It Dv KAUTH_REQ_NETWORK_ALTQ_JOBS
691.It Dv KAUTH_REQ_NETWORK_ALTQ_PRIQ
692.It Dv KAUTH_REQ_NETWORK_ALTQ_RED
693.It Dv KAUTH_REQ_NETWORK_ALTQ_RIO
694.It Dv KAUTH_REQ_NETWORK_ALTQ_WFQ
695.El
696.It Dv KAUTH_NETWORK_BIND
697Checks if a
698.Xr bind 2
699request is allowed.
700.Pp
701.Ar req
702allows to indicate the type of the request to structure listeners and callers
703easier.
704Supported request types:
705.Bl -tag -width compact
706.It Dv KAUTH_REQ_NETWORK_BIND_PORT
707Checks if binding to a non-privileged/reserved port is allowed.
708.It Dv KAUTH_REQ_NETWORK_BIND_PRIVPORT
709Checks if binding to a privileged/reserved port is allowed.
710.El
711.It Dv KAUTH_NETWORK_FIREWALL
712Checks if firewall-related operations are allowed.
713.Pp
714.Ar req
715indicates the sub-action, and can be one of the following:
716.Bl -tag -width compact
717.It Dv KAUTH_REQ_NETWORK_FIREWALL_FW
718Modification of packet filtering rules.
719.It Dv KAUTH_REQ_NETWORK_FIREWALL_NAT
720Modification of NAT rules.
721.El
722.It Dv KAUTH_NETWORK_INTERFACE
723Checks if network interface-related operations are allowed.
724.Pp
725.Ar arg1
726is (optionally) the
727.Ft struct ifnet *
728associated with the interface.
729.Ar arg2
730is (optionally) an
731.Ft int
732describing the interface-specific operation.
733.Ar arg3
734is (optionally) a pointer to the interface-specific request structure.
735.Ar req
736indicates the sub-action, and can be one of the following:
737.Bl -tag -width compact
738.It Dv KAUTH_REQ_NETWORK_INTERFACE_GET
739Check if retrieving information from the device is allowed.
740.It Dv KAUTH_REQ_NETWORK_INTERFACE_GETPRIV
741Check if retrieving privileged information from the device is allowed.
742.It Dv KAUTH_REQ_NETWORK_INTERFACE_SET
743Check if setting parameters on the device is allowed.
744.It Dv KAUTH_REQ_NETWORK_INTERFACE_SETPRIV
745Check if setting privileged parameters on the device is allowed.
746.It Dv KAUTH_REQ_NETWORK_INTERFACE_FIRMWARE
747Check if manipulating the firmware on a network interface device is allowed.
748.El
749.Pp
750Note that unless the
751.Ft struct ifnet *
752for the interface was passed in
753.Ar arg1 ,
754there's no way to tell what structure
755.Ar arg3
756is.
757.It Dv KAUTH_NETWORK_INTERFACE_BRIDGE
758Check if operations performed on the
759.Xr bridge 4
760network interface are allowed.
761.Pp
762.Ar req
763can be one of the following:
764.Bl -tag -width compact
765.It Dv KAUTH_REQ_NETWORK_INTERFACE_BRIDGE_GETPRIV
766Check if getting privileges parameters is allowed.
767.It Dv KAUTH_REQ_NETWORK_INTERFACE_BRIDGE_SETPRIV
768Check if setting privileges parameters is allowed.
769.El
770.It Dv KAUTH_NETWORK_INTERFACE_PPP
771Checks if operations performed on the
772.Xr ppp 4
773network interface are allowed.
774.Pp
775.Ar req
776can be one of the following:
777.Bl -tag -width compact
778.It Dv KAUTH_REQ_NETWORK_INTERFACE_PPP_ADD
779Checks if adding and enabling a
780.Xr ppp 4
781interface to the system is allowed.
782.El
783.It Dv KAUTH_NETWORK_INTERFACE_PVC
784Check if operations performed on a PVC device (e.g.
785.Xr en 4 )
786are allowed.
787.Ar req
788can be one of the following:
789.Bl -tag -width compact
790.It Dv KAUTH_REQ_NETWORK_INTERFACE_PVC_ADD
791Check if adding a PVC device is allowed.
792.El
793.It Dv KAUTH_NETWORK_INTERFACE_SLIP
794Checks if operations performed on the
795.Xr sl 4
796network interface are allowed.
797.Pp
798.Ar req
799can be one of the following:
800.Bl -tag -width compact
801.It Dv KAUTH_REQ_NETWORK_INTERFACE_SLIP_ADD
802Checks if adding and enabling a
803.Xr sl 4
804interface to the system is allowed.
805.El
806.It Dv KAUTH_NETWORK_INTERFACE_STRIP
807Checks if operations performed on the
808.Xr strip 4
809network interface are allowed.
810.Pp
811.Ar req
812can be one of the following:
813.Bl -tag -width compact
814.It Dv KAUTH_REQ_NETWORK_INTERFACE_STRIP_ADD
815Check if adding and enabling a
816.Xr strip 4
817interface to the system is allowed.
818.El
819.It Dv KAUTH_NETWORK_INTERFACE_TUN
820Checks if operations performed on the
821.Xr tun 4
822network interface are allowed.
823.Pp
824.Ar req
825can be one of the following:
826.Bl -tag -width compact
827.It Dv KAUTH_REQ_NETWORK_INTERFACE_TUN_ADD
828Checks if adding and enabling a
829.Xr tun 4
830interface to the system is allowed.
831.El
832.It Dv KAUTH_NETWORK_IPSEC
833Check if operations related to
834.Xr ipsec 4
835connections are allowed.
836.Ar req
837can be one of the following:
838.Bl -tag -width compact
839.It Dv KAUTH_REQ_NETWORK_IPSEC_BYPASS
840Check if bypassing
841.Xr ipsec 4
842policy is allowed.
843.El
844.It Dv KAUTH_NETWORK_IPV6
845Check if IPv6-specific operations are allowed.
846.Ar req
847can be one of the following:
848.Bl -tag -width compact
849.It Dv KAUTH_REQ_NETWORK_IPV6_HOPBYHOP
850Check if setting hop-by-hop packet options is allowed.
851.It Dv KAUTH_REQ_NETWORK_IPV6_JOIN_MULTICAST
852Check if joining a multicast network is allowed.
853.El
854.It Dv KAUTH_NETWORK_FORWSRCRT
855Checks whether status of forwarding of source-routed packets can be modified
856or not.
857.It Dv KAUTH_NETWORK_NFS
858Check if an NFS related operation is allowed.
859.Pp
860.Ar req
861can be any of the following:
862.Bl -tag -width compact
863.It Dv KAUTH_REQ_NETWORK_NFS_EXPORT
864Check if modifying the NFS export table is allowed.
865.It Dv KAUTH_REQ_NETWORK_NFS_SVC
866Check if access to the NFS
867.Xr nfssvc 2
868syscall is allowed.
869.El
870.It Dv KAUTH_NETWORK_ROUTE
871Checks if a routing-related request is allowed.
872.Pp
873.Ar arg1
874is the
875.Ft struct rt_msghdr *
876for the request.
877.It Dv KAUTH_NETWORK_SMB
878Check if operations related to SMB are allowed.
879.Pp
880.Ar req
881can be one of the following:
882.Bl -tag -width compact
883.It Dv KAUTH_REQ_NETWORK_SMB_SHARE_ACCESS
884Check if accessing an SMB share is allowed.
885.Pp
886.Ar arg1
887is a
888.Ft struct smb_share *
889describing the SMB share, and
890.Ar arg2
891is a
892.Ft mode_t
893with the desired access mode.
894.It Dv KAUTH_REQ_NETWORK_SMB_SHARE_CREATE
895Check if creating an SMB share is allowed.
896.Pp
897.Ar arg1
898is a
899.Ft struct smb_sharespec *
900describing the share to be created.
901.It Dv KAUTH_REQ_NETWORK_SMB_VC_ACCESS
902Check if accessing an SMB VC is allowed.
903.Pp
904.Ar arg1
905is a
906.Ft struct smb_vc *
907describing the SMB VC, and
908.Ar arg2
909is a
910.Ft mode_t
911with the desired access mode.
912.It Dv KAUTH_REQ_NETWORK_SMB_VC_CREATE
913Check if creating an SMB VC is allowed.
914.Pp
915.Ar arg1
916is a
917.Ft struct smb_vcspec *
918describing the VC to be created.
919.El
920.It Dv KAUTH_NETWORK_SOCKET
921Checks if a socket related operation is allowed.
922.Pp
923.Ar req
924allows to indicate the type of the request to structure listeners and callers
925easier.
926Supported request types:
927.Bl -tag -width compact
928.It Dv KAUTH_REQ_NETWORK_SOCKET_RAWSOCK
929Checks if opening a raw socket is allowed.
930.It Dv KAUTH_REQ_NETWORK_SOCKET_OPEN
931Checks if opening a socket is allowed.
932.Ar arg1 , arg2 ,
933and
934.Ar arg3
935are all
936.Ft int
937parameters describing the domain, socket type, and protocol,
938respectively.
939.It Dv KAUTH_REQ_NETWORK_SOCKET_CANSEE
940Checks if looking at the socket passed is allowed.
941.Pp
942.Ar arg1
943is a
944.Ft struct socket *
945describing the socket.
946.It Dv KAUTH_REQ_NETWORK_SOCKET_DROP
947Checks if a connection can be dropped.
948.Pp
949.Ar arg1
950is a
951.Ft struct socket *
952describing the socket.
953.It Dv KAUTH_REQ_NETWORK_SOCKET_SETPRIV
954Checks if setting privileged socket options is allowed.
955.Pp
956.Ar arg1
957is a
958.Ft struct socket *
959describing the socket,
960.Ar arg2
961is a
962.Ft u_long
963describing the socket option.
964.El
965.El
966.Ss Machine-dependent Scope
967The machine-dependent (machdep) scope,
968.Dq org.netbsd.kauth.machdep ,
969manages machine-dependent authorization requests in the kernel.
970.Pp
971The authorization wrapper for this scope is declared as
972.Pp
973.Ft int Fn kauth_authorize_machdep "kauth_cred_t cred" "kauth_action_t op" \
974"void *arg0" "void *arg1" "void *arg2" "void *arg3"
975.Pp
976The actions on this scope provide a set that may or may not affect all
977platforms.
978Below is a list of available actions, along with which platforms are affected
979by each.
980.Bl -tag -width compact
981.It Dv KAUTH_MACHDEP_CACHEFLUSH
982Request to flush the whole CPU cache.
983Affects
984.Em m68k
985Linux emulation.
986.It Dv KAUTH_MACHDEP_CPU_UCODE_APPLY
987Request to apply a CPU microcode to a CPU.
988This is related to
989.Em CPU_UCODE ,
990see
991.Xr options 4 .
992Affects
993.Em i386
994and
995.Em xen .
996.It Dv KAUTH_MACHDEP_IOPERM_GET
997Request to get the I/O permission level.
998Affects
999.Em amd64 ,
1000.Em i386 ,
1001.Em xen .
1002.It Dv KAUTH_MACHDEP_IOPERM_SET
1003Request to set the I/O permission level.
1004Affects
1005.Em amd64 ,
1006.Em i386 ,
1007.Em xen .
1008.It Dv KAUTH_MACHDEP_IOPL
1009Request to set the I/O privilege level.
1010Affects
1011.Em amd64 ,
1012.Em i386 ,
1013.Em xen .
1014.It Dv KAUTH_MACHDEP_LDT_GET
1015Request to get the LDT (local descriptor table).
1016Affects
1017.Em amd64 ,
1018.Em i386 ,
1019.Em xen .
1020.It Dv KAUTH_MACHDEP_LDT_SET
1021Request to set the LDT (local descriptor table).
1022Affects
1023.Em amd64 ,
1024.Em i386 ,
1025.Em xen .
1026.It Dv KAUTH_MACHDEP_MTRR_GET
1027Request to get the MTRR (memory type range registers).
1028Affects
1029.Em amd64 ,
1030.Em i386 ,
1031.Em xen .
1032.It Dv KAUTH_MACHDEP_MTRR_SET
1033Request to set the MTRR (memory type range registers).
1034Affects
1035.Em amd64 ,
1036.Em i386 ,
1037.Em xen .
1038.It Dv KAUTH_MACHDEP_NVRAM
1039Request to access (read/write) the NVRAM.
1040Affects
1041.Em i386 .
1042.It Dv KAUTH_MACHDEP_PXG
1043Request to start or stop the
1044.Xr pxg 4
1045CPU.
1046.Ar arg0
1047is
1048.Ft true
1049or
1050.Ft false ,
1051respectively.
1052Affects
1053.Em pmax .
1054.It Dv KAUTH_MACHDEP_UNMANAGEDMEM
1055Request to access unmanaged memory.
1056Affects
1057.Em alpha ,
1058.Em amd64 ,
1059.Em arm ,
1060.Em i386 ,
1061.Em powerpc ,
1062.Em sh3 ,
1063.Em vax ,
1064.Em xen .
1065.El
1066.Ss Device Scope
1067The device scope,
1068.Dq org.netbsd.kauth.device ,
1069manages authorization requests related to devices on the system.
1070Devices can be, for example, terminals, tape drives, Bluetooth accessories, and
1071any other hardware.
1072Network devices specifically are handled by the
1073.Em network
1074scope.
1075.Pp
1076In addition to the standard authorization wrapper:
1077.Pp
1078.Ft int Fn kauth_authorize_device "kauth_cred_t cred" "kauth_action_t op" \
1079"void *arg0" "void *arg1" "void *arg2" "void *arg3"
1080.Pp
1081this scope provides authorization wrappers for various device types.
1082.Pp
1083.Ft int Fn kauth_authorize_device_tty "kauth_cred_t cred" "kauth_action_t op" \
1084"struct tty *tty"
1085.Pp
1086Authorizes requests for
1087.Em terminal devices
1088on the system.
1089The third argument,
1090.Ar tty ,
1091is the terminal device in question.
1092It is passed to the listener as
1093.Ar arg0 .
1094The second argument,
1095.Ar op ,
1096is the action and can be one of the following:
1097.Bl -tag -width compact
1098.It Dv KAUTH_DEVICE_TTY_OPEN
1099Open the terminal device pointed to by
1100.Ar tty .
1101.It Dv KAUTH_DEVICE_TTY_PRIVSET
1102Set privileged settings on the terminal device pointed to by
1103.Ar tty .
1104.It Dv KAUTH_DEVICE_TTY_STI
1105Use the
1106.Dq TIOCSTI
1107device
1108.Xr ioctl 2 ,
1109allowing to inject characters into the terminal buffer, simulating terminal
1110input.
1111.It Dv KAUTH_DEVICE_TTY_VIRTUAL
1112Control the virtual console.
1113.Ar tty
1114is the current console
1115.Xr tty 4 .
1116.El
1117.Pp
1118.Ft int Fn kauth_authorize_device_spec "kauth_cred_t cred" \
1119"enum kauth_device_req req" "struct vnode *vp"
1120.Pp
1121Authorizes requests for
1122.Em special files ,
1123usually disk devices, but also direct memory access, on the system.
1124.Pp
1125It passes
1126.Dv KAUTH_DEVICE_RAWIO_SPEC
1127as the action to the listener, and accepts two arguments.
1128.Ar req ,
1129passed to the listener as
1130.Ar arg0 ,
1131is access requested, and can be one of
1132.Dv KAUTH_REQ_DEVICE_RAWIO_SPEC_READ ,
1133.Dv KAUTH_REQ_DEVICE_RAWIO_SPEC_WRITE ,
1134or
1135.Dv KAUTH_REQ_DEVICE_RAWIO_SPEC_RW ,
1136representing read, write, or both read/write access respectively.
1137.Ar vp
1138is the vnode of the special file in question, and is passed to the listener as
1139.Ar arg1 .
1140.Pp
1141Keep in mind that it is the responsibility of the security model developer to
1142check whether the underlying device is a disk or the system memory, using
1143.Fn iskmemdev :
1144.Bd -literal -offset indent
1145if ((vp->v_type == VCHR) &&
1146    iskmemdev(vp->v_un.vu_specinfo->si_rdev))
1147	/* system memory access */
1148.Ed
1149.Pp
1150.Ft int Fn kauth_authorize_device_passthru "kauth_cred_t cred" "dev_t dev" \
1151"u_long mode" "void *data"
1152.Pp
1153Authorizes hardware
1154.Em passthru
1155requests, or user commands passed directly to the hardware.
1156These have the potential of resulting in direct disk and/or memory access.
1157.Pp
1158It passes
1159.Dv KAUTH_DEVICE_RAWIO_PASSTHRU
1160as the action to the listener, and accepts three arguments.
1161.Ar dev ,
1162passed as
1163.Ar arg1
1164to the listener, is the device for which the request is made.
1165.Ar mode ,
1166passed as
1167.Ar arg0
1168to the listener, is a generic representation of the access mode requested.
1169It can be one or more (binary-OR'd) of the following:
1170.Pp
1171.Bl -tag -width compact -offset indent -compact
1172.It KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READ
1173.It KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF
1174.It KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITE
1175.It KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF
1176.El
1177.Pp
1178.Ar data ,
1179passed as
1180.Ar arg2
1181to the listener, is device-specific data that may be associated with the
1182request.
1183.Ss Bluetooth Devices
1184Authorizing actions relevant to Bluetooth devices is done using the standard
1185authorization wrapper, with the following actions:
1186.Bl -tag -width compact
1187.It KAUTH_DEVICE_BLUETOOTH_BCSP
1188Check if operations on a
1189.Xr bcsp 4
1190device are allowed.
1191.Pp
1192.Ar arg0
1193is an
1194.Ft enum kauth_device_req
1195with one of the following values:
1196.Bl -tag -width compact
1197.It Dv KAUTH_REQ_DEVICE_BLUETOOTH_BCSP_ADD
1198Check if adding and enabling a
1199.Xr bcsp 4
1200device is allowed.
1201.El
1202.It KAUTH_DEVICE_BLUETOOTH_BTUART
1203Check if operations on a
1204.Xr btuart 4
1205device are allowed.
1206.Pp
1207.Ar arg0
1208is an
1209.Ft enum kauth_device_req
1210with one of the following values:
1211.Bl -tag -width compact
1212.It Dv KAUTH_REQ_DEVICE_BLUETOOTH_BTUART_ADD
1213Check if adding and enabling a
1214.Xr btuart 4
1215device is allowed.
1216.El
1217.It KAUTH_DEVICE_BLUETOOTH_RECV
1218Check if a packet can be received from the device.
1219.Pp
1220.Ar arg0
1221is the packet type.
1222For
1223.Dv HCI_CMD_PKT
1224packets,
1225.Ar arg1
1226is the opcode, for
1227.Dv HCI_EVENT_PKT
1228packets,
1229.Ar arg1
1230is the event ID, and for
1231.Dv HCI_ACLDATA_PKT
1232or
1233.Dv HCI_SCODATA_PKT
1234packets,
1235.Ar arg1
1236is the connection handle.
1237.It KAUTH_DEVICE_BLUETOOTH_SEND
1238Check if a packet can be sent to the device.
1239.Pp
1240.Ar arg0
1241is a
1242.Ft struct hci_unit *
1243describing the HCI unit,
1244.Ar arg1
1245is a
1246.Ft hci_cmd_hdr_t *
1247describing the packet header.
1248.It KAUTH_DEVICE_BLUETOOTH_SETPRIV
1249Check if privileged settings can be changed.
1250.Pp
1251.Ar arg0
1252is a
1253.Ft struct hci_unit *
1254describing the HCI unit,
1255.Ar arg1
1256is a
1257.Ft struct btreq *
1258describing the request, and
1259.Ar arg2
1260is a
1261.Ft u_long
1262describing the command.
1263.El
1264.Ss Kernel random device
1265Authorization actions relevant to the kernel random device,
1266.Xr rnd 4 ,
1267is done using the standard authorization wrapper, with the following actions:
1268.Bl -tag -width compact
1269.It KAUTH_DEVICE_RND_ADDDATA
1270Check if adding data to the entropy pool is allowed.
1271.It KAUTH_DEVICE_RND_GETPRIV
1272Check if privileged settings and information can be retrieved.
1273.It KAUTH_DEVICE_RND_SETPRIV
1274Check if privileged settings can be changed.
1275.El
1276.Ss Wscons devices
1277Authorization actions relevant to
1278.Xr wscons 4
1279are done using the standard authorization wrapper, with the following actions:
1280.Bl -tag -width compact
1281.It KAUTH_DEVICE_WSCONS_KEYBOARD_BELL
1282Check if setting the default bell is allowed.
1283.It KAUTH_DEVICE_WSCONS_KEYBOARD_KEYREPEAT
1284Check if setting the default key-repeat is allowed.
1285.El
1286.Ss Vnode Scope
1287The vnode scope,
1288.Dq org.netbsd.kauth.vnode ,
1289authorizes operations made on vnodes representing file system objects.
1290.Pp
1291The authorization wrapper for this scope is declared as
1292.Pp
1293.Ft int Fn kauth_authorize_vnode "kauth_cred_t cred" "kauth_action_t action" \
1294"vnode_t *vp" "vnode_t *dvp" "int fs_decision"
1295.Pp
1296This scope is heavily used in file system code and can potentially affect
1297system-wide performance.
1298Therefore, there are several things developers should know when using it.
1299.Pp
1300First, the
1301.Ar action
1302parameter is a bit-mask and multiple actions can be binary-OR'd and authorized
1303in a single call.
1304Two helper functions help generate the
1305.Ar action
1306value for a couple of common cases: translating file system access to a
1307.Nm
1308action and checking access to a vnode.
1309.Pp
1310The first,
1311.Fn kauth_mode_to_action "mode_t access_mode" ,
1312and returns a
1313.Ft kauth_action_t
1314representing the desired access modes.
1315Another function,
1316.Fn kauth_access_action "mode_t access_mode" "enum vtype v_type" \
1317"mode_t file_mode" ,
1318returns a
1319.Ft kauth_action_t
1320suitable for use in many file system
1321.Xr access 2
1322implementations.
1323It calls the aforementioned
1324.Fn kauth_mode_to_action ,
1325but before returning also adds the
1326.Dv KAUTH_VNODE_IS_EXEC
1327flag if needed.
1328See below for the meaning of this flag and how its necessity is
1329determined.
1330.Pp
1331Second, it is recommended to be very careful with adding listeners on this
1332scope.
1333A special parameter,
1334.Ar fs_decision ,
1335allows different file systems to instrument different policies without adding
1336their own listener.
1337This parameter is special because it also serves as a fall-back decision when
1338no
1339.Xr secmodel 9
1340is present to prevent a fail-open scenario.
1341It can take either an
1342.Xr errno 2
1343value or
1344.Dq KAUTH_VNODE_REMOTEFS ,
1345indicating that the file system on which the authorization is made is remote
1346and cannot provide us with a fall-back decision.
1347In this case,
1348.Nm
1349can only short-circuit the request but the file system will have the last
1350word if there is no definitive allow or deny decision.
1351.Pp
1352The value of
1353.Ar fs_decision
1354can be hard-coded or determined by calling an internal function implementing a
1355policy.
1356For the latter case,
1357.Xr genfs 9
1358provides a set of helper functions that implement common policies that
1359file systems can use.
1360The calling convention is as follows:
1361.Bd -literal -offset indent
1362int error;
1363
1364error = kauth_authorize_vnode(..., genfs_can_foo(...));
1365.Ed
1366.Pp
1367Actions on the vnode scope are of two types: operations and flags.
1368An operation is similar in concept to actions on other scopes in the sense
1369that it represents an operation desired by the caller.
1370A flag is an indicator of additional information about the vnode that
1371a file system can set in order to allow the listener to make a more
1372informed decision.
1373.Pp
1374Actions include the following:
1375.Bl -tag -width compact -offset indent
1376.It KAUTH_VNODE_READ_DATA
1377Read file data.
1378.It KAUTH_VNODE_LIST_DIRECTORY
1379Read directory listing.
1380Identical to the above.
1381.It KAUTH_VNODE_WRITE_DATA
1382Write file data.
1383.It KAUTH_VNODE_ADD_FILE
1384Add a file to a directory.
1385Identical to the above.
1386.It KAUTH_VNODE_EXECUTE
1387Execute a file.
1388.It KAUTH_VNODE_SEARCH
1389Search (enter) a directory.
1390Identical to the above.
1391.It KAUTH_VNODE_DELETE
1392Delete a file.
1393.It KAUTH_VNODE_APPEND_DATA
1394Append data to a file.
1395.It KAUTH_VNODE_ADD_SUBDIRECTORY
1396Add a subdirectory to a directory.
1397Identical to the above.
1398.It KAUTH_VNODE_READ_TIMES
1399Read the created, last accessed, and last modified times of a file.
1400.It KAUTH_VNODE_WRITE_TIMES
1401Modify the created, last accessed, or last modified times of a file.
1402.It KAUTH_VNODE_READ_FLAGS
1403Read file flags.
1404.It KAUTH_VNODE_WRITE_FLAGS
1405Modify file flags.
1406.It KAUTH_VNODE_READ_SYSFLAGS
1407Read file system flags.
1408.It KAUTH_VNODE_WRITE_SYSFLAGS
1409Modify file system flags.
1410.It KAUTH_VNODE_RENAME
1411Rename a file.
1412.It KAUTH_VNODE_CHANGE_OWNERSHIP
1413Change ownership of a file.
1414.It KAUTH_VNODE_READ_SECURITY
1415Read the permissions of a file.
1416.It KAUTH_VNODE_WRITE_SECURITY
1417Change the permissions of a file, for example by using
1418.Xr chmod 2 .
1419.It KAUTH_VNODE_READ_ATTRIBUTES
1420Read attributes of a file.
1421.It KAUTH_VNODE_WRITE_ATTRIBUTES
1422Modify attributes of a file.
1423.It KAUTH_VNODE_READ_EXTATTRIBUTES
1424Read extended attributes of a file.
1425.It KAUTH_VNODE_WRITE_EXTATTRIBUTES
1426Modify extended attributes of a file.
1427.It KAUTH_VNODE_RETAIN_SUID
1428Check if retaining the set-user-id bit on files after
1429.Xr chown 2
1430is allowed.
1431.It KAUTH_VNODE_RETAIN_SGID
1432Check if retaining the set-group-id bit on files after
1433.Xr chown 2
1434is allowed.
1435.It KAUTH_VNODE_REVOKE
1436Revoke a file.
1437.El
1438.Pp
1439Flags include the following:
1440.Bl -tag -width compact -offset indent
1441.It KAUTH_VNODE_IS_EXEC
1442The vnode is executable.
1443.Pp
1444The macro
1445.Fn FS_OBJECT_CAN_EXEC
1446can be used to help determine if this flag should be set.
1447This macro determines a file system object to be executable if it is a
1448directory (in which case we say it is searchable) or if it has at least one
1449executable bit set in its mode.
1450.Pp
1451Setting this flag helps a listener know that a vnode is executable and is used
1452in implementing privileged access to files and directories while maintaining
1453semantics that prevent execution until a file is marked as an executable.
1454An example for using this in listener code is:
1455.Bd -literal -offset indent
1456if (privileged) {
1457	/* Always allow read/write; execute only if executable. */
1458	if ((action & KAUTH_VNODE_EXECUTE) == 0 ||
1459	    (action & KAUTH_VNODE_IS_EXEC))
1460		result = KAUTH_RESULT_ALLOW;
1461}
1462.Ed
1463.Pp
1464Finally, the vnode scope authorization wrapper returns
1465.Er EACCES
1466in case of an error, to maintain file system semantics.
1467File systems can override this value if needed.
1468.It KAUTH_VNODE_HAS_SYSFLAGS
1469The file system object represented by the vnode has system flags set.
1470.It KAUTH_VNODE_ACCESS
1471The authorization is advisory only and no actual operation is to be
1472performed.
1473This is not implemented.
1474.El
1475.Ss Credentials Scope
1476The credentials scope,
1477.Dq org.netbsd.kauth.cred ,
1478is a special scope used internally by the
1479.Nm
1480framework to provide hooking to credential-related operations.
1481.Pp
1482It is a
1483.Dq notify-only
1484scope, allowing hooking operations such as initialization of new credentials,
1485credential inheritance during a fork, and copying and freeing of credentials.
1486The main purpose for this scope is to give a security model a way to control
1487the aforementioned operations, especially in cases where the credentials
1488hold security model-private data.
1489.Pp
1490Notifications are made using the following function, which is internal to
1491.Nm :
1492.Pp
1493.Ft int Fn kauth_cred_hook "kauth_cred_t cred" "kauth_action_t action" \
1494"void *arg0" "void *arg1"
1495.Pp
1496With the following actions:
1497.Bl -tag -width compact
1498.It Dv KAUTH_CRED_COPY
1499The credentials are being copied.
1500.Ar cred
1501are the credentials of the lwp context doing the copy, and
1502.Ar arg0
1503and
1504.Ar arg1
1505are both
1506.Ft kauth_cred_t
1507representing the
1508.Dq from
1509and
1510.Dq to
1511credentials, respectively.
1512.It Dv KAUTH_CRED_FORK
1513The credentials are being inherited from a parent to a child process during a
1514fork.
1515.Pp
1516.Ar cred
1517are the credentials of the lwp context doing the fork, and
1518.Ar arg0
1519and
1520.Ar arg1
1521are both
1522.Ft struct proc *
1523of the parent and child processes, respectively.
1524.It Dv KAUTH_CRED_CHROOT
1525The credentials in cred belong to a process whose root directory is
1526changed through
1527.Fn change_root
1528(see
1529.Xr vfs 9 ).
1530.Pp
1531.Ar Arg0
1532is the new
1533.Ft struct cwdinfo *
1534of the process.
1535.It Dv KAUTH_CRED_FREE
1536The credentials in
1537.Ar cred
1538are being freed.
1539.It Dv KAUTH_CRED_INIT
1540The credentials in
1541.Ar cred
1542are being initialized.
1543.El
1544.Pp
1545Since this is a notify-only scope, all listeners are required to return
1546.Dv KAUTH_RESULT_ALLOW .
1547.Ss Credentials Accessors and Mutators
1548.Nm
1549has a variety of accessor and mutator routines to handle
1550.Ft kauth_cred_t
1551objects.
1552.Pp
1553The following routines can be used to access and modify the user- and
1554group-ids in a
1555.Ft kauth_cred_t :
1556.Bl -tag -width compact
1557.It Ft uid_t Fn kauth_cred_getuid "kauth_cred_t cred"
1558Returns the real user-id from
1559.Ar cred .
1560.It Ft uid_t Fn kauth_cred_geteuid "kauth_cred_t cred"
1561Returns the effective user-id from
1562.Ar cred .
1563.It Ft uid_t Fn kauth_cred_getsvuid "kauth_cred_t cred"
1564Returns the saved user-id from
1565.Ar cred .
1566.It Ft void Fn kauth_cred_setuid "kauth_cred_t cred" "uid_t uid"
1567Sets the real user-id in
1568.Ar cred
1569to
1570.Ar uid .
1571.It Ft void Fn kauth_cred_seteuid "kauth_cred_t cred" "uid_t uid"
1572Sets the effective user-id in
1573.Ar cred
1574to
1575.Ar uid .
1576.It Ft void Fn kauth_cred_setsvuid "kauth_cred_t cred" "uid_t uid"
1577Sets the saved user-id in
1578.Ar cred
1579to
1580.Ar uid .
1581.It Ft gid_t Fn kauth_cred_getgid "kauth_cred_t cred"
1582Returns the real group-id from
1583.Ar cred .
1584.It Ft gid_t Fn kauth_cred_getegid "kauth_cred_t cred"
1585Returns the effective group-id from
1586.Ar cred .
1587.It Ft gid_t Fn kauth_cred_getsvgid "kauth_cred_t cred"
1588Returns the saved group-id from
1589.Ar cred .
1590.It Ft void Fn kauth_cred_setgid "kauth_cred_t cred" "gid_t gid"
1591Sets the real group-id in
1592.Ar cred
1593to
1594.Ar gid .
1595.It Ft void Fn kauth_cred_setegid "kauth_cred_t cred" "gid_t gid"
1596Sets the effective group-id in
1597.Ar cred
1598to
1599.Ar gid .
1600.It Ft void Fn kauth_cred_setsvgid "kauth_cred_t cred" "gid_t gid"
1601Sets the saved group-id in
1602.Ar cred
1603to
1604.Ar gid .
1605.It Ft u_int Fn kauth_cred_getrefcnt "kauth_cred_t cred"
1606Return the reference count for
1607.Ar cred .
1608.El
1609.Pp
1610The following routines can be used to access and modify the group
1611list in a
1612.Ft kauth_cred_t :
1613.Bl -tag -width compact
1614.It Ft int Fn kauth_cred_ismember_gid "kauth_cred_t cred" "gid_t gid" \
1615"int *resultp"
1616Checks if the group-id
1617.Ar gid
1618is a member in the group list of
1619.Ar cred .
1620.Pp
1621If it is,
1622.Ar resultp
1623will be set to one, otherwise, to zero.
1624.Pp
1625The return value is an error code, or zero for success.
1626.It Ft u_int Fn kauth_cred_ngroups "kauth_cred_t cred"
1627Return the number of groups in the group list of
1628.Ar cred .
1629.It Ft gid_t Fn kauth_cred_group "kauth_cred_t cred" "u_int idx"
1630Return the group-id of the group at index
1631.Ar idx
1632in the group list of
1633.Ar cred .
1634.It Ft int Fn kauth_cred_setgroups "kauth_cred_t cred" "const gid_t *groups" \
1635"size_t ngroups" "uid_t gmuid" "enum uio_seg seg"
1636Copy
1637.Ar ngroups
1638groups from array pointed to by
1639.Ar groups
1640to the group list in
1641.Ar cred ,
1642adjusting the number of groups in
1643.Ar cred
1644appropriately.
1645.Ar seg
1646should be either
1647.Dv UIO_USERSPACE
1648or
1649.Dv UIO_SYSSPACE
1650indicating whether
1651.Ar groups
1652is a user or kernel space address.
1653.Pp
1654Any groups remaining will be set to an invalid value.
1655.Pp
1656.Ar gmuid
1657is unused for now, and to maintain interface compatibility with the Darwin
1658KPI.
1659.Pp
1660The return value is an error code, or zero for success.
1661.It Ft int Fn kauth_cred_getgroups "kauth_cred_t cred" "gid_t *groups" \
1662"size_t ngroups" "enum uio_seg seg"
1663Copy
1664.Ar ngroups
1665groups from the group list in
1666.Ar cred
1667to the buffer pointed to by
1668.Ar groups .
1669.Ar seg
1670should be either
1671.Dv UIO_USERSPACE
1672or
1673.Dv UIO_SYSSPACE
1674indicating whether
1675.Ar groups
1676is a user or kernel space address.
1677.Pp
1678The return value is an error code, or zero for success.
1679.El
1680.Ss Credential Private Data
1681.Nm
1682provides an interface to allow attaching security-model private data to
1683credentials.
1684.Pp
1685The use of this interface has two parts that can be divided to direct and
1686indirect control of the private-data.
1687Directly controlling the private data is done by using the below routines,
1688while the indirect control is often dictated by events such as process
1689fork, and is handled by listening on the credentials scope (see above).
1690.Pp
1691Attaching private data to credentials works by registering a key to serve
1692as a unique identifier, distinguishing various sets of private data that
1693may be associated with the credentials.
1694Registering, and deregistering, a key is done by using these routines:
1695.Bl -tag -width compact
1696.It Ft int Fn kauth_register_key "secmodel_t sm" "kauth_key_t *keyp"
1697Register new key for private data for security model
1698.Ar sm .
1699.Ar keyp
1700will be used to return the key to be used in further calls.
1701.Pp
1702The function returns 0 on success and an error code (see
1703.Xr errno 2 )
1704on failure.
1705.It Ft int Fn kauth_deregister_key "kauth_key_t key"
1706Deregister private data key
1707.Ar key .
1708.El
1709.Pp
1710Once registered, private data may be manipulated by the following routines:
1711.Bl -tag -width compact
1712.It Ft void Fn kauth_cred_setdata "kauth_cred_t cred" "kauth_key_t key" \
1713"void *data"
1714Set private data for
1715.Ar key
1716in
1717.Ar cred
1718to be
1719.Ar data .
1720.It Ft void * Fn kauth_cred_getdata "kauth_cred_t cred" "kauth_key_t key"
1721Retrieve private data for
1722.Ar key
1723in
1724.Ar cred .
1725.El
1726.Pp
1727Note that it is required to use the above routines every time the private
1728data is changed, i.e., using
1729.Fn kauth_cred_getdata
1730and later modifying the private data should be accompanied by a call to
1731.Fn kauth_cred_setdata
1732with the
1733.Dq new
1734private data.
1735.Ss Credential Inheritance and Reference Counting
1736.Nm
1737provides an interface for handling shared credentials.
1738.Pp
1739When a
1740.Ft kauth_cred_t
1741is first allocated, its reference count is set to 1.
1742However, with time, its reference count can grow as more objects (processes,
1743LWPs, files, etc.) reference it.
1744.Pp
1745The following routines are available for managing credentials reference
1746counting:
1747.Bl -tag -width compact
1748.It Ft void Fn kauth_cred_hold "kauth_cred_t cred"
1749Increases reference count to
1750.Ar cred
1751by one.
1752.It Ft void Fn kauth_cred_free "kauth_cred_t cred"
1753Decreases the reference count to
1754.Ar cred
1755by one.
1756.Pp
1757If the reference count dropped to zero, the memory used by
1758.Ar cred
1759will be freed.
1760.El
1761.Pp
1762Credential inheritance happens during a
1763.Xr fork 2 ,
1764and is handled by the following function:
1765.Pp
1766.Ft void Fn kauth_proc_fork "struct proc *parent" "struct proc *child"
1767.Pp
1768When called, it references the parent's credentials from the child,
1769and calls the credentials scope's hook with the
1770.Dv KAUTH_CRED_FORK
1771action to allow security model-specific handling of the inheritance
1772to take place.
1773.Ss Credentials Memory Management
1774Data-structures for credentials, listeners, and scopes are allocated from
1775memory pools managed by the
1776.Xr pool 9
1777subsystem.
1778.Pp
1779The
1780.Ft kauth_cred_t
1781objects have their own memory management routines:
1782.Bl -tag -width compact
1783.It Ft kauth_cred_t Fn kauth_cred_alloc "void"
1784Allocates a new
1785.Ft kauth_cred_t ,
1786initializes its lock, and sets its reference count to one.
1787.El
1788.Ss Conversion Routines
1789Sometimes it might be necessary to convert a
1790.Ft kauth_cred_t
1791to userland's view of credentials, a
1792.Ft struct uucred ,
1793or vice versa.
1794.Pp
1795The following routines are available for these cases:
1796.Bl -tag -width compact
1797.It Ft void Fn kauth_uucred_to_cred "kauth_cred_t cred" "const struct uucred *uucred"
1798Convert userland's view of credentials to a
1799.Ft kauth_cred_t .
1800.Pp
1801This includes effective user- and group-ids, a number of groups, and a group
1802list.
1803The reference count is set to one.
1804.Pp
1805Note that
1806.Nm
1807will try to copy as many groups as can be held inside a
1808.Ft kauth_cred_t .
1809.It Ft void Fn kauth_cred_to_uucred "struct uucred *uucred" "const kauth_cred_t cred"
1810Convert
1811.Ft kauth_cred_t
1812to userland's view of credentials.
1813.Pp
1814This includes effective user- and group-ids, a number of groups, and a group
1815list.
1816.Pp
1817Note that
1818.Nm
1819will try to copy as many groups as can be held inside a
1820.Ft struct uucred .
1821.It Ft int Fn kauth_cred_uucmp "kauth_cred_t cred" "struct uucred *uucred"
1822Compares
1823.Ar cred
1824with the userland credentials in
1825.Ar uucred .
1826.Pp
1827Common values that will be compared are effective user- and group-ids, and
1828the group list.
1829.El
1830.Ss Miscellaneous Routines
1831Other routines provided by
1832.Nm
1833are:
1834.Bl -tag -width compact
1835.It Ft void Fn kauth_cred_clone "kauth_cred_t cred1" "kauth_cred_t cred2"
1836Clone credentials from
1837.Ar cred1
1838to
1839.Ar cred2 ,
1840except for the lock and reference count.
1841.It Ft kauth_cred_t Fn kauth_cred_dup "kauth_cred_t cred"
1842Duplicate
1843.Ar cred .
1844.Pp
1845What this routine does is call
1846.Fn kauth_cred_alloc
1847followed by a call to
1848.Fn kauth_cred_clone .
1849.It Ft kauth_cred_t Fn kauth_cred_copy "kauth_cred_t cred"
1850Works like
1851.Fn kauth_cred_dup ,
1852except for a few differences.
1853.Pp
1854If
1855.Ar cred
1856already has a reference count of one, it will be returned.
1857Otherwise, a new
1858.Ft kauth_cred_t
1859will be allocated and the credentials from
1860.Ar cred
1861will be cloned to it.
1862Last, a call to
1863.Fn kauth_cred_free
1864for
1865.Ar cred
1866will be done.
1867.It Ft kauth_cred_t Fn kauth_cred_get "void"
1868Return the credentials associated with the current LWP.
1869This does not change the reference count of the resulting
1870.Ft kauth_cred_t
1871object.
1872.El
1873.Ss Scope Management
1874.Nm
1875provides routines to manage the creation and deletion of scopes on the
1876system.
1877.Pp
1878Note that the built-in scopes, the
1879.Dq generic
1880scope and the
1881.Dq process
1882scope, can't be deleted.
1883.Bl -tag -width compact
1884.It Ft kauth_scope_t Fn kauth_register_scope "const char *id" \
1885"kauth_scope_callback_t cb" "void *cookie"
1886Register a new scope on the system.
1887.Ar id
1888is the name of the scope, usually in reverse DNS-like notation.
1889For example,
1890.Dq org.netbsd.kauth.myscope .
1891.Ar cb
1892is the default listener, to which authorization requests for this scope
1893will be dispatched to.
1894.Ar cookie
1895is optional user-data that will be passed to all listeners
1896during authorization on the scope.
1897.It Ft void Fn kauth_deregister_scope "kauth_scope_t scope"
1898Deregister
1899.Ar scope
1900from the scopes available on the system, and free the
1901.Ft kauth_scope_t
1902object
1903.Ar scope .
1904.El
1905.Ss Listener Management
1906Listeners in
1907.Nm
1908are authorization callbacks that are called during an authorization
1909request in the scope which they belong to.
1910.Pp
1911When an authorization request is made, all listeners associated with
1912a scope are called to allow, deny, or defer the request.
1913.Pp
1914It is enough for one listener to deny the request in order for the
1915request to be denied; but all listeners are called during an authorization
1916process none-the-less.
1917All listeners are required to allow the request for it to be granted,
1918and in a case where all listeners defer the request \(em leaving the
1919decision for other listeners \(em the request is denied.
1920.Pp
1921The following KPI is provided for the management of listeners:
1922.Bl -tag -width compact
1923.It Ft kauth_listener_t Fn kauth_listen_scope "const char *id" \
1924"kauth_scope_callback_t cb" "void *cookie"
1925Create a new listener on the scope with the id
1926.Ar id ,
1927setting the default listener to
1928.Ar cb .
1929.Ar cookie
1930is optional user-data that will be passed to the listener when called
1931during an authorization request.
1932.It Ft void Fn kauth_unlisten_scope "kauth_listener_t listener"
1933Removes
1934.Ar listener
1935from the scope which it belongs to, ensuring it won't be called again,
1936and frees the
1937.Ft kauth_listener_t
1938object
1939.Ar listener .
1940.El
1941.Pp
1942.Nm
1943provides no means for synchronization within listeners.
1944It is the programmer's responsibility to make sure data used by the
1945listener is properly locked during its use, as it can be accessed
1946simultaneously from the same listener called multiple times.
1947It is also the programmer's responsibility to do garbage collection after
1948the listener, possibly freeing any allocated data it used.
1949.Pp
1950The common method to do the above is by having a reference count to
1951each listener.
1952On entry to the listener, this reference count should be raised; on
1953exit, lowered.
1954.Pp
1955During the removal of a listener, first
1956.Fn kauth_scope_unlisten
1957should be called to make sure the listener code will not be entered in
1958the future.
1959Then, the code should wait (possibly sleeping) until the reference count
1960drops to zero.
1961When that happens, it is safe to do the final cleanup.
1962.Pp
1963Listeners might sleep, so no locks can be held when calling an authorization
1964wrapper.
1965.Sh EXAMPLES
1966Older code had no abstraction of the security model, so most privilege
1967checks looked like this:
1968.Bd -literal -offset indent
1969if (suser(cred, &acflag) == 0)
1970	/* allow privileged operation */
1971.Ed
1972.Pp
1973Using the new interface, you must ask for a specific privilege explicitly.
1974For example, checking whether it is possible to open a socket would look
1975something like this:
1976.Bd -literal -offset indent
1977if (kauth_authorize_network(cred, KAUTH_NETWORK_SOCKET,
1978    KAUTH_REQ_NETWORK_SOCKET_OPEN, PF_INET, SOCK_STREAM,
1979    IPPROTO_TCP) == 0)
1980	/* allow opening the socket */
1981.Ed
1982.Pp
1983Note that the
1984.Em securelevel
1985implications were also integrated into the
1986.Nm
1987framework so you don't have to note anything special in the call to the
1988authorization wrapper, but rather just have to make sure the security
1989model handles the request as you expect it to.
1990.Pp
1991To do that you can just
1992.Xr grep 1
1993in the relevant security model directory and have a look at the code.
1994.Sh EXTENDING KAUTH
1995Although
1996.Nm
1997provides a large set of both detailed and more or less generic requests,
1998it might be needed eventually to introduce more scopes, actions, or
1999requests.
2000.Pp
2001Adding a new scope should happen only when an entire subsystem is
2002introduced and it is assumed other parts of the kernel may want to
2003interfere with its inner-workings.
2004When a subsystem that has the potential of impacting the security
2005of the system is introduced, existing security modules must be updated
2006to also handle actions on the newly added scope.
2007.Pp
2008New actions should be added when sets of operations not covered at all
2009belong in an already existing scope.
2010.Pp
2011Requests (or sub-actions) can be added as subsets of existing actions
2012when an operation that belongs in an already covered area is introduced.
2013.Pp
2014Note that all additions should include updates to this manual, the
2015security models shipped with
2016.Nx ,
2017and the example skeleton security model.
2018.Sh SEE ALSO
2019.Xr secmodel 9
2020.Sh HISTORY
2021The kernel authorization framework first appeared in Mac OS X 10.4.
2022.Pp
2023The kernel authorization framework in
2024.Nx
2025first appeared in
2026.Nx 4.0 ,
2027and is a clean-room implementation based on Apple TN2127, available at
2028.Lk http://developer.apple.com/technotes/tn2005/tn2127.html
2029.Sh NOTES
2030As
2031.Nm
2032in
2033.Nx
2034is still under active development, it is likely that the ABI, and possibly the
2035API, will differ between
2036.Nx
2037versions.
2038Developers are to take notice of this fact in order to avoid building code
2039that expects one version of the ABI and running it in a system with a different
2040one.
2041.Sh AUTHORS
2042.An Elad Efrat Aq Mt elad@NetBSD.org
2043implemented the kernel authorization framework in
2044.Nx .
2045.Pp
2046.An Jason R. Thorpe Aq Mt thorpej@NetBSD.org
2047provided guidance and answered questions about the Darwin implementation.
2048