2 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
3 * Copyright (c) 2001, 2002 Networks Associates Technology, Inc.
6 * This software was developed by Robert Watson for the TrustedBSD Project.
8 * This software was developed for the FreeBSD Project in part by Network
9 * Associates Laboratories, the Security Research Division of Network
10 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
11 * as part of the DARPA CHATS research program.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * Kernel interface for MAC policy modules.
39 #ifndef _SYS_MAC_POLICY_H
40 #define _SYS_MAC_POLICY_H
43 * Pluggable access control policy definition structure.
45 * List of operations that are performed as part of the implementation
46 * of a MAC policy. Policy implementors declare operations with a
47 * mac_policy_ops structure, and using the MAC_POLICY_SET() macro.
48 * If an entry point is not declared, then then the policy will be ignored
49 * during evaluation of that event or check.
51 * Operations are sorted first by general class of operation, then
54 struct mac_policy_conf;
55 struct mac_policy_ops {
57 * Policy module operations.
59 void (*mpo_destroy)(struct mac_policy_conf *mpc);
60 void (*mpo_init)(struct mac_policy_conf *mpc);
63 * General policy-directed security system call so that policies
64 * may implement new services without reserving explicit
65 * system call numbers.
67 int (*mpo_syscall)(struct thread *td, int call, void *arg);
72 void (*mpo_init_bpfdesc_label)(struct label *label);
73 void (*mpo_init_cred_label)(struct label *label);
74 void (*mpo_init_devfsdirent_label)(struct label *label);
75 void (*mpo_init_ifnet_label)(struct label *label);
76 void (*mpo_init_ipq_label)(struct label *label);
77 int (*mpo_init_mbuf_label)(struct label *label, int flag);
78 void (*mpo_init_mount_label)(struct label *label);
79 void (*mpo_init_mount_fs_label)(struct label *label);
80 int (*mpo_init_socket_label)(struct label *label, int flag);
81 int (*mpo_init_socket_peer_label)(struct label *label, int flag);
82 void (*mpo_init_pipe_label)(struct label *label);
83 void (*mpo_init_vnode_label)(struct label *label);
84 void (*mpo_destroy_bpfdesc_label)(struct label *label);
85 void (*mpo_destroy_cred_label)(struct label *label);
86 void (*mpo_destroy_devfsdirent_label)(struct label *label);
87 void (*mpo_destroy_ifnet_label)(struct label *label);
88 void (*mpo_destroy_ipq_label)(struct label *label);
89 void (*mpo_destroy_mbuf_label)(struct label *label);
90 void (*mpo_destroy_mount_label)(struct label *label);
91 void (*mpo_destroy_mount_fs_label)(struct label *label);
92 void (*mpo_destroy_socket_label)(struct label *label);
93 void (*mpo_destroy_socket_peer_label)(struct label *label);
94 void (*mpo_destroy_pipe_label)(struct label *label);
95 void (*mpo_destroy_vnode_label)(struct label *label);
96 void (*mpo_copy_pipe_label)(struct label *src,
98 void (*mpo_copy_vnode_label)(struct label *src,
100 int (*mpo_externalize_cred_label)(struct label *label,
101 char *element_name, char *buffer, size_t buflen,
102 size_t *len, int *claimed);
103 int (*mpo_externalize_ifnet_label)(struct label *label,
104 char *element_name, char *buffer, size_t buflen,
105 size_t *len, int *claimed);
106 int (*mpo_externalize_pipe_label)(struct label *label,
107 char *element_name, char *buffer, size_t buflen,
108 size_t *len, int *claimed);
109 int (*mpo_externalize_socket_label)(struct label *label,
110 char *element_name, char *buffer, size_t buflen,
111 size_t *len, int *claimed);
112 int (*mpo_externalize_socket_peer_label)(struct label *label,
113 char *element_name, char *buffer, size_t buflen,
114 size_t *len, int *claimed);
115 int (*mpo_externalize_vnode_label)(struct label *label,
116 char *element_name, char *buffer, size_t buflen,
117 size_t *len, int *claimed);
118 int (*mpo_internalize_cred_label)(struct label *label,
119 char *element_name, char *element_data, int *claimed);
120 int (*mpo_internalize_ifnet_label)(struct label *label,
121 char *element_name, char *element_data, int *claimed);
122 int (*mpo_internalize_pipe_label)(struct label *label,
123 char *element_name, char *element_data, int *claimed);
124 int (*mpo_internalize_socket_label)(struct label *label,
125 char *element_name, char *element_data, int *claimed);
126 int (*mpo_internalize_vnode_label)(struct label *label,
127 char *element_name, char *element_data, int *claimed);
130 * Labeling event operations: file system objects, and things that
131 * look a lot like file system objects.
133 void (*mpo_associate_vnode_devfs)(struct mount *mp,
134 struct label *fslabel, struct devfs_dirent *de,
135 struct label *delabel, struct vnode *vp,
136 struct label *vlabel);
137 int (*mpo_associate_vnode_extattr)(struct mount *mp,
138 struct label *fslabel, struct vnode *vp,
139 struct label *vlabel);
140 void (*mpo_associate_vnode_singlelabel)(struct mount *mp,
141 struct label *fslabel, struct vnode *vp,
142 struct label *vlabel);
143 void (*mpo_create_devfs_device)(dev_t dev, struct devfs_dirent *de,
144 struct label *label);
145 void (*mpo_create_devfs_directory)(char *dirname, int dirnamelen,
146 struct devfs_dirent *de, struct label *label);
147 void (*mpo_create_devfs_symlink)(struct ucred *cred,
148 struct devfs_dirent *dd, struct label *ddlabel,
149 struct devfs_dirent *de, struct label *delabel);
150 void (*mpo_create_devfs_vnode)(struct devfs_dirent *de,
151 struct label *direntlabel, struct vnode *vp,
152 struct label *vnodelabel);
153 int (*mpo_create_vnode_extattr)(struct ucred *cred,
154 struct mount *mp, struct label *fslabel,
155 struct vnode *dvp, struct label *dlabel,
156 struct vnode *vp, struct label *vlabel,
157 struct componentname *cnp);
158 void (*mpo_create_mount)(struct ucred *cred, struct mount *mp,
159 struct label *mntlabel, struct label *fslabel);
160 void (*mpo_create_root_mount)(struct ucred *cred, struct mount *mp,
161 struct label *mountlabel, struct label *fslabel);
162 void (*mpo_relabel_vnode)(struct ucred *cred, struct vnode *vp,
163 struct label *vnodelabel, struct label *label);
164 int (*mpo_setlabel_vnode_extattr)(struct ucred *cred,
165 struct vnode *vp, struct label *vlabel,
166 struct label *intlabel);
167 void (*mpo_update_devfsdirent)(struct devfs_dirent *devfs_dirent,
168 struct label *direntlabel, struct vnode *vp,
169 struct label *vnodelabel);
172 * Labeling event operations: IPC objects.
174 void (*mpo_create_mbuf_from_socket)(struct socket *so,
175 struct label *socketlabel, struct mbuf *m,
176 struct label *mbuflabel);
177 void (*mpo_create_socket)(struct ucred *cred, struct socket *so,
178 struct label *socketlabel);
179 void (*mpo_create_socket_from_socket)(struct socket *oldsocket,
180 struct label *oldsocketlabel, struct socket *newsocket,
181 struct label *newsocketlabel);
182 void (*mpo_relabel_socket)(struct ucred *cred, struct socket *so,
183 struct label *oldlabel, struct label *newlabel);
184 void (*mpo_relabel_pipe)(struct ucred *cred, struct pipe *pipe,
185 struct label *oldlabel, struct label *newlabel);
186 void (*mpo_set_socket_peer_from_mbuf)(struct mbuf *mbuf,
187 struct label *mbuflabel, struct socket *so,
188 struct label *socketpeerlabel);
189 void (*mpo_set_socket_peer_from_socket)(struct socket *oldsocket,
190 struct label *oldsocketlabel, struct socket *newsocket,
191 struct label *newsocketpeerlabel);
192 void (*mpo_create_pipe)(struct ucred *cred, struct pipe *pipe,
193 struct label *pipelabel);
196 * Labeling event operations: network objects.
198 void (*mpo_create_bpfdesc)(struct ucred *cred, struct bpf_d *bpf_d,
199 struct label *bpflabel);
200 void (*mpo_create_ifnet)(struct ifnet *ifnet,
201 struct label *ifnetlabel);
202 void (*mpo_create_ipq)(struct mbuf *fragment,
203 struct label *fragmentlabel, struct ipq *ipq,
204 struct label *ipqlabel);
205 void (*mpo_create_datagram_from_ipq)
206 (struct ipq *ipq, struct label *ipqlabel,
207 struct mbuf *datagram, struct label *datagramlabel);
208 void (*mpo_create_fragment)(struct mbuf *datagram,
209 struct label *datagramlabel, struct mbuf *fragment,
210 struct label *fragmentlabel);
211 void (*mpo_create_mbuf_from_mbuf)(struct mbuf *oldmbuf,
212 struct label *oldlabel, struct mbuf *newmbuf,
213 struct label *newlabel);
214 void (*mpo_create_mbuf_linklayer)(struct ifnet *ifnet,
215 struct label *ifnetlabel, struct mbuf *mbuf,
216 struct label *mbuflabel);
217 void (*mpo_create_mbuf_from_bpfdesc)(struct bpf_d *bpf_d,
218 struct label *bpflabel, struct mbuf *mbuf,
219 struct label *mbuflabel);
220 void (*mpo_create_mbuf_from_ifnet)(struct ifnet *ifnet,
221 struct label *ifnetlabel, struct mbuf *mbuf,
222 struct label *mbuflabel);
223 void (*mpo_create_mbuf_multicast_encap)(struct mbuf *oldmbuf,
224 struct label *oldmbuflabel, struct ifnet *ifnet,
225 struct label *ifnetlabel, struct mbuf *newmbuf,
226 struct label *newmbuflabel);
227 void (*mpo_create_mbuf_netlayer)(struct mbuf *oldmbuf,
228 struct label *oldmbuflabel, struct mbuf *newmbuf,
229 struct label *newmbuflabel);
230 int (*mpo_fragment_match)(struct mbuf *fragment,
231 struct label *fragmentlabel, struct ipq *ipq,
232 struct label *ipqlabel);
233 void (*mpo_relabel_ifnet)(struct ucred *cred, struct ifnet *ifnet,
234 struct label *ifnetlabel, struct label *newlabel);
235 void (*mpo_update_ipq)(struct mbuf *fragment,
236 struct label *fragmentlabel, struct ipq *ipq,
237 struct label *ipqlabel);
240 * Labeling event operations: processes.
242 void (*mpo_create_cred)(struct ucred *parent_cred,
243 struct ucred *child_cred);
244 void (*mpo_execve_transition)(struct ucred *old, struct ucred *new,
245 struct vnode *vp, struct label *vnodelabel);
246 int (*mpo_execve_will_transition)(struct ucred *old,
247 struct vnode *vp, struct label *vnodelabel);
248 void (*mpo_create_proc0)(struct ucred *cred);
249 void (*mpo_create_proc1)(struct ucred *cred);
250 void (*mpo_relabel_cred)(struct ucred *cred,
251 struct label *newlabel);
252 void (*mpo_thread_userret)(struct thread *thread);
255 * Access control checks.
257 int (*mpo_check_bpfdesc_receive)(struct bpf_d *bpf_d,
258 struct label *bpflabel, struct ifnet *ifnet,
259 struct label *ifnetlabel);
260 int (*mpo_check_cred_relabel)(struct ucred *cred,
261 struct label *newlabel);
262 int (*mpo_check_cred_visible)(struct ucred *u1, struct ucred *u2);
263 int (*mpo_check_ifnet_relabel)(struct ucred *cred,
264 struct ifnet *ifnet, struct label *ifnetlabel,
265 struct label *newlabel);
266 int (*mpo_check_ifnet_transmit)(struct ifnet *ifnet,
267 struct label *ifnetlabel, struct mbuf *m,
268 struct label *mbuflabel);
269 int (*mpo_check_kenv_dump)(struct ucred *cred);
270 int (*mpo_check_kenv_get)(struct ucred *cred, char *name);
271 int (*mpo_check_kenv_set)(struct ucred *cred, char *name,
273 int (*mpo_check_kenv_unset)(struct ucred *cred, char *name);
274 int (*mpo_check_mount_stat)(struct ucred *cred, struct mount *mp,
275 struct label *mntlabel);
276 int (*mpo_check_pipe_ioctl)(struct ucred *cred, struct pipe *pipe,
277 struct label *pipelabel, unsigned long cmd, void *data);
278 int (*mpo_check_pipe_poll)(struct ucred *cred, struct pipe *pipe,
279 struct label *pipelabel);
280 int (*mpo_check_pipe_read)(struct ucred *cred, struct pipe *pipe,
281 struct label *pipelabel);
282 int (*mpo_check_pipe_relabel)(struct ucred *cred,
283 struct pipe *pipe, struct label *pipelabel,
284 struct label *newlabel);
285 int (*mpo_check_pipe_stat)(struct ucred *cred, struct pipe *pipe,
286 struct label *pipelabel);
287 int (*mpo_check_pipe_write)(struct ucred *cred, struct pipe *pipe,
288 struct label *pipelabel);
289 int (*mpo_check_proc_debug)(struct ucred *cred,
291 int (*mpo_check_proc_sched)(struct ucred *cred,
293 int (*mpo_check_proc_signal)(struct ucred *cred,
294 struct proc *proc, int signum);
295 int (*mpo_check_socket_bind)(struct ucred *cred,
296 struct socket *so, struct label *socketlabel,
297 struct sockaddr *sockaddr);
298 int (*mpo_check_socket_connect)(struct ucred *cred,
299 struct socket *so, struct label *socketlabel,
300 struct sockaddr *sockaddr);
301 int (*mpo_check_socket_deliver)(struct socket *so,
302 struct label *socketlabel, struct mbuf *m,
303 struct label *mbuflabel);
304 int (*mpo_check_socket_listen)(struct ucred *cred,
305 struct socket *so, struct label *socketlabel);
306 int (*mpo_check_socket_receive)(struct ucred *cred,
307 struct socket *so, struct label *socketlabel);
308 int (*mpo_check_socket_relabel)(struct ucred *cred,
309 struct socket *so, struct label *socketlabel,
310 struct label *newlabel);
311 int (*mpo_check_socket_send)(struct ucred *cred,
312 struct socket *so, struct label *socketlabel);
313 int (*mpo_check_socket_visible)(struct ucred *cred,
314 struct socket *so, struct label *socketlabel);
315 int (*mpo_check_system_reboot)(struct ucred *cred, int howto);
316 int (*mpo_check_system_settime)(struct ucred *cred);
317 int (*mpo_check_system_swapon)(struct ucred *cred,
318 struct vnode *vp, struct label *label);
319 int (*mpo_check_system_sysctl)(struct ucred *cred, int *name,
320 u_int namelen, void *old, size_t *oldlenp, int inkernel,
321 void *new, size_t newlen);
322 int (*mpo_check_vnode_access)(struct ucred *cred,
323 struct vnode *vp, struct label *label, int acc_mode);
324 int (*mpo_check_vnode_chdir)(struct ucred *cred,
325 struct vnode *dvp, struct label *dlabel);
326 int (*mpo_check_vnode_chroot)(struct ucred *cred,
327 struct vnode *dvp, struct label *dlabel);
328 int (*mpo_check_vnode_create)(struct ucred *cred,
329 struct vnode *dvp, struct label *dlabel,
330 struct componentname *cnp, struct vattr *vap);
331 int (*mpo_check_vnode_delete)(struct ucred *cred,
332 struct vnode *dvp, struct label *dlabel,
333 struct vnode *vp, struct label *label,
334 struct componentname *cnp);
335 int (*mpo_check_vnode_deleteacl)(struct ucred *cred,
336 struct vnode *vp, struct label *label, acl_type_t type);
337 int (*mpo_check_vnode_exec)(struct ucred *cred, struct vnode *vp,
338 struct label *label);
339 int (*mpo_check_vnode_getacl)(struct ucred *cred,
340 struct vnode *vp, struct label *label, acl_type_t type);
341 int (*mpo_check_vnode_getextattr)(struct ucred *cred,
342 struct vnode *vp, struct label *label, int attrnamespace,
343 const char *name, struct uio *uio);
344 int (*mpo_check_vnode_link)(struct ucred *cred, struct vnode *dvp,
345 struct label *dlabel, struct vnode *vp,
346 struct label *label, struct componentname *cnp);
347 int (*mpo_check_vnode_lookup)(struct ucred *cred,
348 struct vnode *dvp, struct label *dlabel,
349 struct componentname *cnp);
350 int (*mpo_check_vnode_mmap)(struct ucred *cred, struct vnode *vp,
351 struct label *label, int prot);
352 void (*mpo_check_vnode_mmap_downgrade)(struct ucred *cred,
353 struct vnode *vp, struct label *label, int *prot);
354 int (*mpo_check_vnode_mprotect)(struct ucred *cred,
355 struct vnode *vp, struct label *label, int prot);
356 int (*mpo_check_vnode_open)(struct ucred *cred, struct vnode *vp,
357 struct label *label, int acc_mode);
358 int (*mpo_check_vnode_poll)(struct ucred *active_cred,
359 struct ucred *file_cred, struct vnode *vp,
360 struct label *label);
361 int (*mpo_check_vnode_read)(struct ucred *active_cred,
362 struct ucred *file_cred, struct vnode *vp,
363 struct label *label);
364 int (*mpo_check_vnode_readdir)(struct ucred *cred,
365 struct vnode *dvp, struct label *dlabel);
366 int (*mpo_check_vnode_readlink)(struct ucred *cred,
367 struct vnode *vp, struct label *label);
368 int (*mpo_check_vnode_relabel)(struct ucred *cred,
369 struct vnode *vp, struct label *vnodelabel,
370 struct label *newlabel);
371 int (*mpo_check_vnode_rename_from)(struct ucred *cred,
372 struct vnode *dvp, struct label *dlabel, struct vnode *vp,
373 struct label *label, struct componentname *cnp);
374 int (*mpo_check_vnode_rename_to)(struct ucred *cred,
375 struct vnode *dvp, struct label *dlabel, struct vnode *vp,
376 struct label *label, int samedir,
377 struct componentname *cnp);
378 int (*mpo_check_vnode_revoke)(struct ucred *cred,
379 struct vnode *vp, struct label *label);
380 int (*mpo_check_vnode_setacl)(struct ucred *cred,
381 struct vnode *vp, struct label *label, acl_type_t type,
383 int (*mpo_check_vnode_setextattr)(struct ucred *cred,
384 struct vnode *vp, struct label *label, int attrnamespace,
385 const char *name, struct uio *uio);
386 int (*mpo_check_vnode_setflags)(struct ucred *cred,
387 struct vnode *vp, struct label *label, u_long flags);
388 int (*mpo_check_vnode_setmode)(struct ucred *cred,
389 struct vnode *vp, struct label *label, mode_t mode);
390 int (*mpo_check_vnode_setowner)(struct ucred *cred,
391 struct vnode *vp, struct label *label, uid_t uid,
393 int (*mpo_check_vnode_setutimes)(struct ucred *cred,
394 struct vnode *vp, struct label *label,
395 struct timespec atime, struct timespec mtime);
396 int (*mpo_check_vnode_stat)(struct ucred *active_cred,
397 struct ucred *file_cred, struct vnode *vp,
398 struct label *label);
399 int (*mpo_check_vnode_write)(struct ucred *active_cred,
400 struct ucred *file_cred, struct vnode *vp,
401 struct label *label);
404 struct mac_policy_conf {
405 char *mpc_name; /* policy name */
406 char *mpc_fullname; /* policy full name */
407 struct mac_policy_ops *mpc_ops; /* policy operations */
408 int mpc_loadtime_flags; /* flags */
409 int *mpc_field_off; /* security field */
410 int mpc_runtime_flags; /* flags */
411 LIST_ENTRY(mac_policy_conf) mpc_list; /* global list */
414 /* Flags for the mpc_loadtime_flags field. */
415 #define MPC_LOADTIME_FLAG_NOTLATE 0x00000001
416 #define MPC_LOADTIME_FLAG_UNLOADOK 0x00000002
418 /* Flags for the mpc_runtime_flags field. */
419 #define MPC_RUNTIME_FLAG_REGISTERED 0x00000001
421 #define MAC_POLICY_SET(mpops, mpname, mpfullname, mpflags, privdata_wanted) \
422 static struct mac_policy_conf mpname##_mac_policy_conf = { \
430 static moduledata_t mpname##_mod = { \
432 mac_policy_modevent, \
433 &mpname##_mac_policy_conf \
435 MODULE_DEPEND(mpname, kernel_mac_support, 1, 1, 1); \
436 DECLARE_MODULE(mpname, mpname##_mod, SI_SUB_MAC_POLICY, \
439 int mac_policy_modevent(module_t mod, int type, void *data);
441 #define LABEL_TO_SLOT(l, s) (l)->l_perpolicy[s]
443 #endif /* !_SYS_MAC_POLICY_H */