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