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