1/*-
2 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
3 * Copyright (c) 2001, 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed by Robert Watson for the TrustedBSD Project.
7 *
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.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
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.
21 *
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
32 * SUCH DAMAGE.
33 *
34 * $FreeBSD: head/sys/security/mac/mac_policy.h 106788 2002-11-12 04:20:36Z rwatson $
35 */
36/*
37 * Kernel interface for MAC policy modules.
38 */
39#ifndef _SYS_MAC_POLICY_H
40#define _SYS_MAC_POLICY_H
41
42/*-
43 * Pluggable access control policy definition structure.
44 *
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.
50 *
51 * Operations are sorted first by general class of operation, then
52 * alphabetically.
53 */
54struct mac_policy_conf;
55struct mac_policy_ops {
56 /*
57 * Policy module operations.
58 */
59 void (*mpo_destroy)(struct mac_policy_conf *mpc);
60 void (*mpo_init)(struct mac_policy_conf *mpc);
61
62 /*
63 * General policy-directed security system call so that policies
64 * may implement new services without reserving explicit
65 * system call numbers.
66 */
67 int (*mpo_syscall)(struct thread *td, int call, void *arg);
68
69 /*
70 * Label operations.
71 */
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,
97 struct label *dest);
98 void (*mpo_copy_vnode_label)(struct label *src,
99 struct label *dest);
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);
128
129 /*
130 * Labeling event operations: file system objects, and things that
131 * look a lot like file system objects.
132 */
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 int (*mpo_create_vnode_extattr)(struct ucred *cred,
151 struct mount *mp, struct label *fslabel,
152 struct vnode *dvp, struct label *dlabel,
153 struct vnode *vp, struct label *vlabel,
154 struct componentname *cnp);
155 void (*mpo_create_mount)(struct ucred *cred, struct mount *mp,
156 struct label *mntlabel, struct label *fslabel);
157 void (*mpo_create_root_mount)(struct ucred *cred, struct mount *mp,
158 struct label *mountlabel, struct label *fslabel);
159 void (*mpo_relabel_vnode)(struct ucred *cred, struct vnode *vp,
160 struct label *vnodelabel, struct label *label);
161 int (*mpo_setlabel_vnode_extattr)(struct ucred *cred,
162 struct vnode *vp, struct label *vlabel,
163 struct label *intlabel);
164 void (*mpo_update_devfsdirent)(struct devfs_dirent *devfs_dirent,
165 struct label *direntlabel, struct vnode *vp,
166 struct label *vnodelabel);
167
168 /*
169 * Labeling event operations: IPC objects.
170 */
171 void (*mpo_create_mbuf_from_socket)(struct socket *so,
172 struct label *socketlabel, struct mbuf *m,
173 struct label *mbuflabel);
174 void (*mpo_create_socket)(struct ucred *cred, struct socket *so,
175 struct label *socketlabel);
176 void (*mpo_create_socket_from_socket)(struct socket *oldsocket,
177 struct label *oldsocketlabel, struct socket *newsocket,
178 struct label *newsocketlabel);
179 void (*mpo_relabel_socket)(struct ucred *cred, struct socket *so,
180 struct label *oldlabel, struct label *newlabel);
181 void (*mpo_relabel_pipe)(struct ucred *cred, struct pipe *pipe,
182 struct label *oldlabel, struct label *newlabel);
183 void (*mpo_set_socket_peer_from_mbuf)(struct mbuf *mbuf,
184 struct label *mbuflabel, struct socket *so,
185 struct label *socketpeerlabel);
186 void (*mpo_set_socket_peer_from_socket)(struct socket *oldsocket,
187 struct label *oldsocketlabel, struct socket *newsocket,
188 struct label *newsocketpeerlabel);
189 void (*mpo_create_pipe)(struct ucred *cred, struct pipe *pipe,
190 struct label *pipelabel);
191
192 /*
193 * Labeling event operations: network objects.
194 */
195 void (*mpo_create_bpfdesc)(struct ucred *cred, struct bpf_d *bpf_d,
196 struct label *bpflabel);
197 void (*mpo_create_ifnet)(struct ifnet *ifnet,
198 struct label *ifnetlabel);
199 void (*mpo_create_ipq)(struct mbuf *fragment,
200 struct label *fragmentlabel, struct ipq *ipq,
201 struct label *ipqlabel);
202 void (*mpo_create_datagram_from_ipq)
203 (struct ipq *ipq, struct label *ipqlabel,
204 struct mbuf *datagram, struct label *datagramlabel);
205 void (*mpo_create_fragment)(struct mbuf *datagram,
206 struct label *datagramlabel, struct mbuf *fragment,
207 struct label *fragmentlabel);
208 void (*mpo_create_mbuf_from_mbuf)(struct mbuf *oldmbuf,
209 struct label *oldlabel, struct mbuf *newmbuf,
210 struct label *newlabel);
211 void (*mpo_create_mbuf_linklayer)(struct ifnet *ifnet,
212 struct label *ifnetlabel, struct mbuf *mbuf,
213 struct label *mbuflabel);
214 void (*mpo_create_mbuf_from_bpfdesc)(struct bpf_d *bpf_d,
215 struct label *bpflabel, struct mbuf *mbuf,
216 struct label *mbuflabel);
217 void (*mpo_create_mbuf_from_ifnet)(struct ifnet *ifnet,
218 struct label *ifnetlabel, struct mbuf *mbuf,
219 struct label *mbuflabel);
220 void (*mpo_create_mbuf_multicast_encap)(struct mbuf *oldmbuf,
221 struct label *oldmbuflabel, struct ifnet *ifnet,
222 struct label *ifnetlabel, struct mbuf *newmbuf,
223 struct label *newmbuflabel);
224 void (*mpo_create_mbuf_netlayer)(struct mbuf *oldmbuf,
225 struct label *oldmbuflabel, struct mbuf *newmbuf,
226 struct label *newmbuflabel);
227 int (*mpo_fragment_match)(struct mbuf *fragment,
228 struct label *fragmentlabel, struct ipq *ipq,
229 struct label *ipqlabel);
230 void (*mpo_relabel_ifnet)(struct ucred *cred, struct ifnet *ifnet,
231 struct label *ifnetlabel, struct label *newlabel);
232 void (*mpo_update_ipq)(struct mbuf *fragment,
233 struct label *fragmentlabel, struct ipq *ipq,
234 struct label *ipqlabel);
235
236 /*
237 * Labeling event operations: processes.
238 */
239 void (*mpo_create_cred)(struct ucred *parent_cred,
240 struct ucred *child_cred);
241 void (*mpo_execve_transition)(struct ucred *old, struct ucred *new,
242 struct vnode *vp, struct label *vnodelabel,
243 struct label *interpvnodelabel,
244 struct image_params *imgp, struct label *execlabel);
245 int (*mpo_execve_will_transition)(struct ucred *old,
246 struct vnode *vp, struct label *vnodelabel,
247 struct label *interpvnodelabel,
248 struct image_params *imgp, struct label *execlabel);
249 void (*mpo_create_proc0)(struct ucred *cred);
250 void (*mpo_create_proc1)(struct ucred *cred);
251 void (*mpo_relabel_cred)(struct ucred *cred,
252 struct label *newlabel);
253 void (*mpo_thread_userret)(struct thread *thread);
254
255 /*
256 * Access control checks.
257 */
258 int (*mpo_check_bpfdesc_receive)(struct bpf_d *bpf_d,
259 struct label *bpflabel, struct ifnet *ifnet,
260 struct label *ifnetlabel);
261 int (*mpo_check_cred_relabel)(struct ucred *cred,
262 struct label *newlabel);
263 int (*mpo_check_cred_visible)(struct ucred *u1, struct ucred *u2);
264 int (*mpo_check_ifnet_relabel)(struct ucred *cred,
265 struct ifnet *ifnet, struct label *ifnetlabel,
266 struct label *newlabel);
267 int (*mpo_check_ifnet_transmit)(struct ifnet *ifnet,
268 struct label *ifnetlabel, struct mbuf *m,
269 struct label *mbuflabel);
270 int (*mpo_check_kenv_dump)(struct ucred *cred);
271 int (*mpo_check_kenv_get)(struct ucred *cred, char *name);
272 int (*mpo_check_kenv_set)(struct ucred *cred, char *name,
273 char *value);
274 int (*mpo_check_kenv_unset)(struct ucred *cred, char *name);
275 int (*mpo_check_mount_stat)(struct ucred *cred, struct mount *mp,
276 struct label *mntlabel);
277 int (*mpo_check_pipe_ioctl)(struct ucred *cred, struct pipe *pipe,
278 struct label *pipelabel, unsigned long cmd, void *data);
279 int (*mpo_check_pipe_poll)(struct ucred *cred, struct pipe *pipe,
280 struct label *pipelabel);
281 int (*mpo_check_pipe_read)(struct ucred *cred, struct pipe *pipe,
282 struct label *pipelabel);
283 int (*mpo_check_pipe_relabel)(struct ucred *cred,
284 struct pipe *pipe, struct label *pipelabel,
285 struct label *newlabel);
286 int (*mpo_check_pipe_stat)(struct ucred *cred, struct pipe *pipe,
287 struct label *pipelabel);
288 int (*mpo_check_pipe_write)(struct ucred *cred, struct pipe *pipe,
289 struct label *pipelabel);
290 int (*mpo_check_proc_debug)(struct ucred *cred,
291 struct proc *proc);
292 int (*mpo_check_proc_sched)(struct ucred *cred,
293 struct proc *proc);
294 int (*mpo_check_proc_signal)(struct ucred *cred,
295 struct proc *proc, int signum);
296 int (*mpo_check_socket_bind)(struct ucred *cred,
297 struct socket *so, struct label *socketlabel,
298 struct sockaddr *sockaddr);
299 int (*mpo_check_socket_connect)(struct ucred *cred,
300 struct socket *so, struct label *socketlabel,
301 struct sockaddr *sockaddr);
302 int (*mpo_check_socket_deliver)(struct socket *so,
303 struct label *socketlabel, struct mbuf *m,
304 struct label *mbuflabel);
305 int (*mpo_check_socket_listen)(struct ucred *cred,
306 struct socket *so, struct label *socketlabel);
307 int (*mpo_check_socket_receive)(struct ucred *cred,
308 struct socket *so, struct label *socketlabel);
309 int (*mpo_check_socket_relabel)(struct ucred *cred,
310 struct socket *so, struct label *socketlabel,
311 struct label *newlabel);
312 int (*mpo_check_socket_send)(struct ucred *cred,
313 struct socket *so, struct label *socketlabel);
314 int (*mpo_check_socket_visible)(struct ucred *cred,
315 struct socket *so, struct label *socketlabel);
316 int (*mpo_check_system_acct)(struct ucred *cred,
317 struct vnode *vp, struct label *vlabel);
318 int (*mpo_check_system_nfsd)(struct ucred *cred);
319 int (*mpo_check_system_reboot)(struct ucred *cred, int howto);
320 int (*mpo_check_system_settime)(struct ucred *cred);
321 int (*mpo_check_system_swapon)(struct ucred *cred,
322 struct vnode *vp, struct label *label);
323 int (*mpo_check_system_sysctl)(struct ucred *cred, int *name,
324 u_int namelen, void *old, size_t *oldlenp, int inkernel,
325 void *new, size_t newlen);
326 int (*mpo_check_vnode_access)(struct ucred *cred,
327 struct vnode *vp, struct label *label, int acc_mode);
328 int (*mpo_check_vnode_chdir)(struct ucred *cred,
329 struct vnode *dvp, struct label *dlabel);
330 int (*mpo_check_vnode_chroot)(struct ucred *cred,
331 struct vnode *dvp, struct label *dlabel);
332 int (*mpo_check_vnode_create)(struct ucred *cred,
333 struct vnode *dvp, struct label *dlabel,
334 struct componentname *cnp, struct vattr *vap);
335 int (*mpo_check_vnode_delete)(struct ucred *cred,
336 struct vnode *dvp, struct label *dlabel,
337 struct vnode *vp, struct label *label,
338 struct componentname *cnp);
339 int (*mpo_check_vnode_deleteacl)(struct ucred *cred,
340 struct vnode *vp, struct label *label, acl_type_t type);
341 int (*mpo_check_vnode_exec)(struct ucred *cred, struct vnode *vp,
342 struct label *label, struct image_params *imgp,
343 struct label *execlabel);
344 int (*mpo_check_vnode_getacl)(struct ucred *cred,
345 struct vnode *vp, struct label *label, acl_type_t type);
346 int (*mpo_check_vnode_getextattr)(struct ucred *cred,
347 struct vnode *vp, struct label *label, int attrnamespace,
348 const char *name, struct uio *uio);
349 int (*mpo_check_vnode_link)(struct ucred *cred, struct vnode *dvp,
350 struct label *dlabel, struct vnode *vp,
351 struct label *label, struct componentname *cnp);
352 int (*mpo_check_vnode_lookup)(struct ucred *cred,
353 struct vnode *dvp, struct label *dlabel,
354 struct componentname *cnp);
355 int (*mpo_check_vnode_mmap)(struct ucred *cred, struct vnode *vp,
356 struct label *label, int prot);
357 void (*mpo_check_vnode_mmap_downgrade)(struct ucred *cred,
358 struct vnode *vp, struct label *label, int *prot);
359 int (*mpo_check_vnode_mprotect)(struct ucred *cred,
360 struct vnode *vp, struct label *label, int prot);
361 int (*mpo_check_vnode_open)(struct ucred *cred, struct vnode *vp,
362 struct label *label, int acc_mode);
363 int (*mpo_check_vnode_poll)(struct ucred *active_cred,
364 struct ucred *file_cred, struct vnode *vp,
365 struct label *label);
366 int (*mpo_check_vnode_read)(struct ucred *active_cred,
367 struct ucred *file_cred, struct vnode *vp,
368 struct label *label);
369 int (*mpo_check_vnode_readdir)(struct ucred *cred,
370 struct vnode *dvp, struct label *dlabel);
371 int (*mpo_check_vnode_readlink)(struct ucred *cred,
372 struct vnode *vp, struct label *label);
373 int (*mpo_check_vnode_relabel)(struct ucred *cred,
374 struct vnode *vp, struct label *vnodelabel,
375 struct label *newlabel);
376 int (*mpo_check_vnode_rename_from)(struct ucred *cred,
377 struct vnode *dvp, struct label *dlabel, struct vnode *vp,
378 struct label *label, struct componentname *cnp);
379 int (*mpo_check_vnode_rename_to)(struct ucred *cred,
380 struct vnode *dvp, struct label *dlabel, struct vnode *vp,
381 struct label *label, int samedir,
382 struct componentname *cnp);
383 int (*mpo_check_vnode_revoke)(struct ucred *cred,
384 struct vnode *vp, struct label *label);
385 int (*mpo_check_vnode_setacl)(struct ucred *cred,
386 struct vnode *vp, struct label *label, acl_type_t type,
387 struct acl *acl);
388 int (*mpo_check_vnode_setextattr)(struct ucred *cred,
389 struct vnode *vp, struct label *label, int attrnamespace,
390 const char *name, struct uio *uio);
391 int (*mpo_check_vnode_setflags)(struct ucred *cred,
392 struct vnode *vp, struct label *label, u_long flags);
393 int (*mpo_check_vnode_setmode)(struct ucred *cred,
394 struct vnode *vp, struct label *label, mode_t mode);
395 int (*mpo_check_vnode_setowner)(struct ucred *cred,
396 struct vnode *vp, struct label *label, uid_t uid,
397 gid_t gid);
398 int (*mpo_check_vnode_setutimes)(struct ucred *cred,
399 struct vnode *vp, struct label *label,
400 struct timespec atime, struct timespec mtime);
401 int (*mpo_check_vnode_stat)(struct ucred *active_cred,
402 struct ucred *file_cred, struct vnode *vp,
403 struct label *label);
404 int (*mpo_check_vnode_write)(struct ucred *active_cred,
405 struct ucred *file_cred, struct vnode *vp,
406 struct label *label);
407};
408
409struct mac_policy_conf {
410 char *mpc_name; /* policy name */
411 char *mpc_fullname; /* policy full name */
412 struct mac_policy_ops *mpc_ops; /* policy operations */
413 int mpc_loadtime_flags; /* flags */
414 int *mpc_field_off; /* security field */
415 int mpc_runtime_flags; /* flags */
416 LIST_ENTRY(mac_policy_conf) mpc_list; /* global list */
417};
418
419/* Flags for the mpc_loadtime_flags field. */
420#define MPC_LOADTIME_FLAG_NOTLATE 0x00000001
421#define MPC_LOADTIME_FLAG_UNLOADOK 0x00000002
422
423/* Flags for the mpc_runtime_flags field. */
424#define MPC_RUNTIME_FLAG_REGISTERED 0x00000001
425
426#define MAC_POLICY_SET(mpops, mpname, mpfullname, mpflags, privdata_wanted) \
427 static struct mac_policy_conf mpname##_mac_policy_conf = { \
428 #mpname, \
429 mpfullname, \
430 mpops, \
431 mpflags, \
432 privdata_wanted, \
433 0, \
434 }; \
435 static moduledata_t mpname##_mod = { \
436 #mpname, \
437 mac_policy_modevent, \
438 &mpname##_mac_policy_conf \
439 }; \
440 MODULE_DEPEND(mpname, kernel_mac_support, 1, 1, 1); \
441 DECLARE_MODULE(mpname, mpname##_mod, SI_SUB_MAC_POLICY, \
442 SI_ORDER_MIDDLE)
443
444int mac_policy_modevent(module_t mod, int type, void *data);
445
446#define LABEL_TO_SLOT(l, s) (l)->l_perpolicy[s]
447
448#endif /* !_SYS_MAC_POLICY_H */
2 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
3 * Copyright (c) 2001, 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed by Robert Watson for the TrustedBSD Project.
7 *
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.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
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.
21 *
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
32 * SUCH DAMAGE.
33 *
34 * $FreeBSD: head/sys/security/mac/mac_policy.h 106788 2002-11-12 04:20:36Z rwatson $
35 */
36/*
37 * Kernel interface for MAC policy modules.
38 */
39#ifndef _SYS_MAC_POLICY_H
40#define _SYS_MAC_POLICY_H
41
42/*-
43 * Pluggable access control policy definition structure.
44 *
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.
50 *
51 * Operations are sorted first by general class of operation, then
52 * alphabetically.
53 */
54struct mac_policy_conf;
55struct mac_policy_ops {
56 /*
57 * Policy module operations.
58 */
59 void (*mpo_destroy)(struct mac_policy_conf *mpc);
60 void (*mpo_init)(struct mac_policy_conf *mpc);
61
62 /*
63 * General policy-directed security system call so that policies
64 * may implement new services without reserving explicit
65 * system call numbers.
66 */
67 int (*mpo_syscall)(struct thread *td, int call, void *arg);
68
69 /*
70 * Label operations.
71 */
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,
97 struct label *dest);
98 void (*mpo_copy_vnode_label)(struct label *src,
99 struct label *dest);
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);
128
129 /*
130 * Labeling event operations: file system objects, and things that
131 * look a lot like file system objects.
132 */
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 int (*mpo_create_vnode_extattr)(struct ucred *cred,
151 struct mount *mp, struct label *fslabel,
152 struct vnode *dvp, struct label *dlabel,
153 struct vnode *vp, struct label *vlabel,
154 struct componentname *cnp);
155 void (*mpo_create_mount)(struct ucred *cred, struct mount *mp,
156 struct label *mntlabel, struct label *fslabel);
157 void (*mpo_create_root_mount)(struct ucred *cred, struct mount *mp,
158 struct label *mountlabel, struct label *fslabel);
159 void (*mpo_relabel_vnode)(struct ucred *cred, struct vnode *vp,
160 struct label *vnodelabel, struct label *label);
161 int (*mpo_setlabel_vnode_extattr)(struct ucred *cred,
162 struct vnode *vp, struct label *vlabel,
163 struct label *intlabel);
164 void (*mpo_update_devfsdirent)(struct devfs_dirent *devfs_dirent,
165 struct label *direntlabel, struct vnode *vp,
166 struct label *vnodelabel);
167
168 /*
169 * Labeling event operations: IPC objects.
170 */
171 void (*mpo_create_mbuf_from_socket)(struct socket *so,
172 struct label *socketlabel, struct mbuf *m,
173 struct label *mbuflabel);
174 void (*mpo_create_socket)(struct ucred *cred, struct socket *so,
175 struct label *socketlabel);
176 void (*mpo_create_socket_from_socket)(struct socket *oldsocket,
177 struct label *oldsocketlabel, struct socket *newsocket,
178 struct label *newsocketlabel);
179 void (*mpo_relabel_socket)(struct ucred *cred, struct socket *so,
180 struct label *oldlabel, struct label *newlabel);
181 void (*mpo_relabel_pipe)(struct ucred *cred, struct pipe *pipe,
182 struct label *oldlabel, struct label *newlabel);
183 void (*mpo_set_socket_peer_from_mbuf)(struct mbuf *mbuf,
184 struct label *mbuflabel, struct socket *so,
185 struct label *socketpeerlabel);
186 void (*mpo_set_socket_peer_from_socket)(struct socket *oldsocket,
187 struct label *oldsocketlabel, struct socket *newsocket,
188 struct label *newsocketpeerlabel);
189 void (*mpo_create_pipe)(struct ucred *cred, struct pipe *pipe,
190 struct label *pipelabel);
191
192 /*
193 * Labeling event operations: network objects.
194 */
195 void (*mpo_create_bpfdesc)(struct ucred *cred, struct bpf_d *bpf_d,
196 struct label *bpflabel);
197 void (*mpo_create_ifnet)(struct ifnet *ifnet,
198 struct label *ifnetlabel);
199 void (*mpo_create_ipq)(struct mbuf *fragment,
200 struct label *fragmentlabel, struct ipq *ipq,
201 struct label *ipqlabel);
202 void (*mpo_create_datagram_from_ipq)
203 (struct ipq *ipq, struct label *ipqlabel,
204 struct mbuf *datagram, struct label *datagramlabel);
205 void (*mpo_create_fragment)(struct mbuf *datagram,
206 struct label *datagramlabel, struct mbuf *fragment,
207 struct label *fragmentlabel);
208 void (*mpo_create_mbuf_from_mbuf)(struct mbuf *oldmbuf,
209 struct label *oldlabel, struct mbuf *newmbuf,
210 struct label *newlabel);
211 void (*mpo_create_mbuf_linklayer)(struct ifnet *ifnet,
212 struct label *ifnetlabel, struct mbuf *mbuf,
213 struct label *mbuflabel);
214 void (*mpo_create_mbuf_from_bpfdesc)(struct bpf_d *bpf_d,
215 struct label *bpflabel, struct mbuf *mbuf,
216 struct label *mbuflabel);
217 void (*mpo_create_mbuf_from_ifnet)(struct ifnet *ifnet,
218 struct label *ifnetlabel, struct mbuf *mbuf,
219 struct label *mbuflabel);
220 void (*mpo_create_mbuf_multicast_encap)(struct mbuf *oldmbuf,
221 struct label *oldmbuflabel, struct ifnet *ifnet,
222 struct label *ifnetlabel, struct mbuf *newmbuf,
223 struct label *newmbuflabel);
224 void (*mpo_create_mbuf_netlayer)(struct mbuf *oldmbuf,
225 struct label *oldmbuflabel, struct mbuf *newmbuf,
226 struct label *newmbuflabel);
227 int (*mpo_fragment_match)(struct mbuf *fragment,
228 struct label *fragmentlabel, struct ipq *ipq,
229 struct label *ipqlabel);
230 void (*mpo_relabel_ifnet)(struct ucred *cred, struct ifnet *ifnet,
231 struct label *ifnetlabel, struct label *newlabel);
232 void (*mpo_update_ipq)(struct mbuf *fragment,
233 struct label *fragmentlabel, struct ipq *ipq,
234 struct label *ipqlabel);
235
236 /*
237 * Labeling event operations: processes.
238 */
239 void (*mpo_create_cred)(struct ucred *parent_cred,
240 struct ucred *child_cred);
241 void (*mpo_execve_transition)(struct ucred *old, struct ucred *new,
242 struct vnode *vp, struct label *vnodelabel,
243 struct label *interpvnodelabel,
244 struct image_params *imgp, struct label *execlabel);
245 int (*mpo_execve_will_transition)(struct ucred *old,
246 struct vnode *vp, struct label *vnodelabel,
247 struct label *interpvnodelabel,
248 struct image_params *imgp, struct label *execlabel);
249 void (*mpo_create_proc0)(struct ucred *cred);
250 void (*mpo_create_proc1)(struct ucred *cred);
251 void (*mpo_relabel_cred)(struct ucred *cred,
252 struct label *newlabel);
253 void (*mpo_thread_userret)(struct thread *thread);
254
255 /*
256 * Access control checks.
257 */
258 int (*mpo_check_bpfdesc_receive)(struct bpf_d *bpf_d,
259 struct label *bpflabel, struct ifnet *ifnet,
260 struct label *ifnetlabel);
261 int (*mpo_check_cred_relabel)(struct ucred *cred,
262 struct label *newlabel);
263 int (*mpo_check_cred_visible)(struct ucred *u1, struct ucred *u2);
264 int (*mpo_check_ifnet_relabel)(struct ucred *cred,
265 struct ifnet *ifnet, struct label *ifnetlabel,
266 struct label *newlabel);
267 int (*mpo_check_ifnet_transmit)(struct ifnet *ifnet,
268 struct label *ifnetlabel, struct mbuf *m,
269 struct label *mbuflabel);
270 int (*mpo_check_kenv_dump)(struct ucred *cred);
271 int (*mpo_check_kenv_get)(struct ucred *cred, char *name);
272 int (*mpo_check_kenv_set)(struct ucred *cred, char *name,
273 char *value);
274 int (*mpo_check_kenv_unset)(struct ucred *cred, char *name);
275 int (*mpo_check_mount_stat)(struct ucred *cred, struct mount *mp,
276 struct label *mntlabel);
277 int (*mpo_check_pipe_ioctl)(struct ucred *cred, struct pipe *pipe,
278 struct label *pipelabel, unsigned long cmd, void *data);
279 int (*mpo_check_pipe_poll)(struct ucred *cred, struct pipe *pipe,
280 struct label *pipelabel);
281 int (*mpo_check_pipe_read)(struct ucred *cred, struct pipe *pipe,
282 struct label *pipelabel);
283 int (*mpo_check_pipe_relabel)(struct ucred *cred,
284 struct pipe *pipe, struct label *pipelabel,
285 struct label *newlabel);
286 int (*mpo_check_pipe_stat)(struct ucred *cred, struct pipe *pipe,
287 struct label *pipelabel);
288 int (*mpo_check_pipe_write)(struct ucred *cred, struct pipe *pipe,
289 struct label *pipelabel);
290 int (*mpo_check_proc_debug)(struct ucred *cred,
291 struct proc *proc);
292 int (*mpo_check_proc_sched)(struct ucred *cred,
293 struct proc *proc);
294 int (*mpo_check_proc_signal)(struct ucred *cred,
295 struct proc *proc, int signum);
296 int (*mpo_check_socket_bind)(struct ucred *cred,
297 struct socket *so, struct label *socketlabel,
298 struct sockaddr *sockaddr);
299 int (*mpo_check_socket_connect)(struct ucred *cred,
300 struct socket *so, struct label *socketlabel,
301 struct sockaddr *sockaddr);
302 int (*mpo_check_socket_deliver)(struct socket *so,
303 struct label *socketlabel, struct mbuf *m,
304 struct label *mbuflabel);
305 int (*mpo_check_socket_listen)(struct ucred *cred,
306 struct socket *so, struct label *socketlabel);
307 int (*mpo_check_socket_receive)(struct ucred *cred,
308 struct socket *so, struct label *socketlabel);
309 int (*mpo_check_socket_relabel)(struct ucred *cred,
310 struct socket *so, struct label *socketlabel,
311 struct label *newlabel);
312 int (*mpo_check_socket_send)(struct ucred *cred,
313 struct socket *so, struct label *socketlabel);
314 int (*mpo_check_socket_visible)(struct ucred *cred,
315 struct socket *so, struct label *socketlabel);
316 int (*mpo_check_system_acct)(struct ucred *cred,
317 struct vnode *vp, struct label *vlabel);
318 int (*mpo_check_system_nfsd)(struct ucred *cred);
319 int (*mpo_check_system_reboot)(struct ucred *cred, int howto);
320 int (*mpo_check_system_settime)(struct ucred *cred);
321 int (*mpo_check_system_swapon)(struct ucred *cred,
322 struct vnode *vp, struct label *label);
323 int (*mpo_check_system_sysctl)(struct ucred *cred, int *name,
324 u_int namelen, void *old, size_t *oldlenp, int inkernel,
325 void *new, size_t newlen);
326 int (*mpo_check_vnode_access)(struct ucred *cred,
327 struct vnode *vp, struct label *label, int acc_mode);
328 int (*mpo_check_vnode_chdir)(struct ucred *cred,
329 struct vnode *dvp, struct label *dlabel);
330 int (*mpo_check_vnode_chroot)(struct ucred *cred,
331 struct vnode *dvp, struct label *dlabel);
332 int (*mpo_check_vnode_create)(struct ucred *cred,
333 struct vnode *dvp, struct label *dlabel,
334 struct componentname *cnp, struct vattr *vap);
335 int (*mpo_check_vnode_delete)(struct ucred *cred,
336 struct vnode *dvp, struct label *dlabel,
337 struct vnode *vp, struct label *label,
338 struct componentname *cnp);
339 int (*mpo_check_vnode_deleteacl)(struct ucred *cred,
340 struct vnode *vp, struct label *label, acl_type_t type);
341 int (*mpo_check_vnode_exec)(struct ucred *cred, struct vnode *vp,
342 struct label *label, struct image_params *imgp,
343 struct label *execlabel);
344 int (*mpo_check_vnode_getacl)(struct ucred *cred,
345 struct vnode *vp, struct label *label, acl_type_t type);
346 int (*mpo_check_vnode_getextattr)(struct ucred *cred,
347 struct vnode *vp, struct label *label, int attrnamespace,
348 const char *name, struct uio *uio);
349 int (*mpo_check_vnode_link)(struct ucred *cred, struct vnode *dvp,
350 struct label *dlabel, struct vnode *vp,
351 struct label *label, struct componentname *cnp);
352 int (*mpo_check_vnode_lookup)(struct ucred *cred,
353 struct vnode *dvp, struct label *dlabel,
354 struct componentname *cnp);
355 int (*mpo_check_vnode_mmap)(struct ucred *cred, struct vnode *vp,
356 struct label *label, int prot);
357 void (*mpo_check_vnode_mmap_downgrade)(struct ucred *cred,
358 struct vnode *vp, struct label *label, int *prot);
359 int (*mpo_check_vnode_mprotect)(struct ucred *cred,
360 struct vnode *vp, struct label *label, int prot);
361 int (*mpo_check_vnode_open)(struct ucred *cred, struct vnode *vp,
362 struct label *label, int acc_mode);
363 int (*mpo_check_vnode_poll)(struct ucred *active_cred,
364 struct ucred *file_cred, struct vnode *vp,
365 struct label *label);
366 int (*mpo_check_vnode_read)(struct ucred *active_cred,
367 struct ucred *file_cred, struct vnode *vp,
368 struct label *label);
369 int (*mpo_check_vnode_readdir)(struct ucred *cred,
370 struct vnode *dvp, struct label *dlabel);
371 int (*mpo_check_vnode_readlink)(struct ucred *cred,
372 struct vnode *vp, struct label *label);
373 int (*mpo_check_vnode_relabel)(struct ucred *cred,
374 struct vnode *vp, struct label *vnodelabel,
375 struct label *newlabel);
376 int (*mpo_check_vnode_rename_from)(struct ucred *cred,
377 struct vnode *dvp, struct label *dlabel, struct vnode *vp,
378 struct label *label, struct componentname *cnp);
379 int (*mpo_check_vnode_rename_to)(struct ucred *cred,
380 struct vnode *dvp, struct label *dlabel, struct vnode *vp,
381 struct label *label, int samedir,
382 struct componentname *cnp);
383 int (*mpo_check_vnode_revoke)(struct ucred *cred,
384 struct vnode *vp, struct label *label);
385 int (*mpo_check_vnode_setacl)(struct ucred *cred,
386 struct vnode *vp, struct label *label, acl_type_t type,
387 struct acl *acl);
388 int (*mpo_check_vnode_setextattr)(struct ucred *cred,
389 struct vnode *vp, struct label *label, int attrnamespace,
390 const char *name, struct uio *uio);
391 int (*mpo_check_vnode_setflags)(struct ucred *cred,
392 struct vnode *vp, struct label *label, u_long flags);
393 int (*mpo_check_vnode_setmode)(struct ucred *cred,
394 struct vnode *vp, struct label *label, mode_t mode);
395 int (*mpo_check_vnode_setowner)(struct ucred *cred,
396 struct vnode *vp, struct label *label, uid_t uid,
397 gid_t gid);
398 int (*mpo_check_vnode_setutimes)(struct ucred *cred,
399 struct vnode *vp, struct label *label,
400 struct timespec atime, struct timespec mtime);
401 int (*mpo_check_vnode_stat)(struct ucred *active_cred,
402 struct ucred *file_cred, struct vnode *vp,
403 struct label *label);
404 int (*mpo_check_vnode_write)(struct ucred *active_cred,
405 struct ucred *file_cred, struct vnode *vp,
406 struct label *label);
407};
408
409struct mac_policy_conf {
410 char *mpc_name; /* policy name */
411 char *mpc_fullname; /* policy full name */
412 struct mac_policy_ops *mpc_ops; /* policy operations */
413 int mpc_loadtime_flags; /* flags */
414 int *mpc_field_off; /* security field */
415 int mpc_runtime_flags; /* flags */
416 LIST_ENTRY(mac_policy_conf) mpc_list; /* global list */
417};
418
419/* Flags for the mpc_loadtime_flags field. */
420#define MPC_LOADTIME_FLAG_NOTLATE 0x00000001
421#define MPC_LOADTIME_FLAG_UNLOADOK 0x00000002
422
423/* Flags for the mpc_runtime_flags field. */
424#define MPC_RUNTIME_FLAG_REGISTERED 0x00000001
425
426#define MAC_POLICY_SET(mpops, mpname, mpfullname, mpflags, privdata_wanted) \
427 static struct mac_policy_conf mpname##_mac_policy_conf = { \
428 #mpname, \
429 mpfullname, \
430 mpops, \
431 mpflags, \
432 privdata_wanted, \
433 0, \
434 }; \
435 static moduledata_t mpname##_mod = { \
436 #mpname, \
437 mac_policy_modevent, \
438 &mpname##_mac_policy_conf \
439 }; \
440 MODULE_DEPEND(mpname, kernel_mac_support, 1, 1, 1); \
441 DECLARE_MODULE(mpname, mpname##_mod, SI_SUB_MAC_POLICY, \
442 SI_ORDER_MIDDLE)
443
444int mac_policy_modevent(module_t mod, int type, void *data);
445
446#define LABEL_TO_SLOT(l, s) (l)->l_perpolicy[s]
447
448#endif /* !_SYS_MAC_POLICY_H */