xref: /freebsd/sys/security/mac/mac_framework.h (revision 2a1c79af9295caf8e57fd5a3c133ffbc66691fc1)
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$
38  */
39 /*
40  * Userland/kernel interface for Mandatory Access Control.
41  *
42  * The POSIX.1e implementation page may be reached at:
43  * http://www.trustedbsd.org/
44  */
45 #ifndef _SYS_MAC_H
46 #define	_SYS_MAC_H
47 
48 #include <sys/_label.h>
49 
50 #ifndef _POSIX_MAC
51 #define	_POSIX_MAC
52 #endif
53 
54 /*
55  * XXXMAC: The single MAC extended attribute will be deprecated once
56  * compound EA writes on a single target file can be performed cleanly
57  * with UFS2.
58  */
59 #define	FREEBSD_MAC_EXTATTR_NAME	"freebsd.mac"
60 #define	FREEBSD_MAC_EXTATTR_NAMESPACE	EXTATTR_NAMESPACE_SYSTEM
61 
62 /*
63  * MAC framework-related constants and limits.
64  */
65 #define	MAC_MAX_POLICY_NAME	32
66 
67 /*
68  * XXXMAC: Per-policy structures will be moved from mac.h to per-policy
69  * include files once the revised user interface is available.
70  */
71 
72 /*
73  * Structures and constants associated with a Biba Integrity policy.
74  * mac_biba represents a Biba label, with mb_type determining its properties,
75  * and mb_grade represents the hierarchal grade if valid for the current
76  * mb_type.  These structures will move to mac_biba.h once we have dymamic
77  * labels exposed to userland.
78  */
79 #define	MAC_BIBA_MAX_COMPARTMENTS	256
80 struct mac_biba_element {
81 	u_short	mbe_type;
82 	u_short	mbe_grade;
83 	u_char	mbe_compartments[MAC_BIBA_MAX_COMPARTMENTS >> 3];
84 };
85 
86 /*
87  * Biba labels consist of two components: a single label, and a label
88  * range.  Depending on the context, one or both may be used; the mb_flags
89  * field permits the provider to indicate what fields are intended for
90  * use.
91  */
92 struct mac_biba {
93 	int			mb_flags;
94 	struct mac_biba_element	mb_single;
95 	struct mac_biba_element mb_rangelow, mb_rangehigh;
96 };
97 
98 /*
99  * Structures and constants associated with a Multi-Level Security policy.
100  * mac_mls represents an MLS label, with mm_type determining its properties,
101  * and mm_level represents the hierarchal sensitivity level if valid for the
102  * current mm_type.  These structures will move to mac_mls.h once we have
103  * dynamic labels exposed to userland.
104  */
105 #define	MAC_MLS_MAX_COMPARTMENTS	256
106 struct mac_mls_element {
107 	u_short	mme_type;
108 	u_short	mme_level;
109 	u_char	mme_compartments[MAC_MLS_MAX_COMPARTMENTS >> 3];
110 };
111 
112 /*
113  * MLS labels consist of two components: a single label, and a label
114  * range.  Depending on the context, one or both may be used; the mb_flags
115  * field permits the provider to indicate what fields are intended for
116  * use.
117  */
118 struct mac_mls {
119 	int			mm_flags;
120 	struct mac_mls_element	mm_single;
121 	struct mac_mls_element	mm_rangelow, mm_rangehigh;
122 };
123 
124 /*
125  * Structures and constants associated with a Type Enforcement policy.
126  * mac_te represents a Type Enforcement label.
127  */
128 #define	MAC_TE_TYPE_MAXLEN	32
129 struct mac_te {
130 	char	mt_type[MAC_TE_TYPE_MAXLEN+1];	/* TE type */
131 };
132 
133 struct mac_sebsd {
134 	uint32_t	ms_psid;	/* persistent sid storage */
135 };
136 
137 /*
138  * Composite structures and constants which combine the various policy
139  * elements into common structures to be associated with subjects and
140  * objects.
141  */
142 struct mac {
143 	int		m_macflags;
144 	struct mac_biba	m_biba;
145 	struct mac_mls	m_mls;
146 	struct mac_te	m_te;
147 	struct mac_sebsd m_sebsd;
148 };
149 typedef struct mac	*mac_t;
150 
151 #define	MAC_FLAG_INITIALIZED		0x00000001	/* Is initialized. */
152 
153 #ifndef _KERNEL
154 
155 /*
156  * POSIX.1e functions visible in the application namespace.
157  */
158 int	mac_dominate(const mac_t _labela, const mac_t _labelb);
159 int	mac_equal(const mac_t labela, const mac_t _labelb);
160 int	mac_free(void *_buf_p);
161 mac_t	mac_from_text(const char *_text_p);
162 mac_t	mac_get_fd(int _fildes);
163 mac_t	mac_get_file(const char *_path_p);
164 mac_t	mac_get_proc(void);
165 mac_t	mac_glb(const mac_t _labela, const mac_t _labelb);
166 mac_t	mac_lub(const mac_t _labela, const mac_t _labelb);
167 int	mac_set_fd(int _fildes, const mac_t _label);
168 int	mac_set_file(const char *_path_p, mac_t _label);
169 int	mac_set_proc(const mac_t _label);
170 ssize_t	mac_size(mac_t _label);
171 char *	mac_to_text(const mac_t _label, size_t *_len_p);
172 int	mac_valid(const mac_t _label);
173 
174 /*
175  * Extensions to POSIX.1e visible in the application namespace.
176  */
177 int	mac_is_present_np(const char *_policyname);
178 int	mac_syscall(const char *_policyname, int call, void *arg);
179 
180 /*
181  * System calls wrapped by some POSIX.1e functions.
182  */
183 int	__mac_get_fd(int _fd, struct mac *_mac_p);
184 int	__mac_get_file(const char *_path_p, struct mac *_mac_p);
185 int	__mac_get_proc(struct mac *_mac_p);
186 int	__mac_set_fd(int fd, struct mac *_mac_p);
187 int	__mac_set_file(const char *_path_p, struct mac *_mac_p);
188 int	__mac_set_proc(struct mac *_mac_p);
189 
190 #else /* _KERNEL */
191 
192 /*
193  * Kernel functions to manage and evaluate labels.
194  */
195 struct bpf_d;
196 struct componentname;
197 struct devfs_dirent;
198 struct ifnet;
199 struct ifreq;
200 struct ipq;
201 struct mbuf;
202 struct mount;
203 struct proc;
204 struct sockaddr;
205 struct socket;
206 struct pipe;
207 struct thread;
208 struct timespec;
209 struct ucred;
210 struct uio;
211 struct vattr;
212 struct vnode;
213 
214 #include <sys/acl.h>			/* XXX acl_type_t */
215 
216 struct vop_refreshlabel_args;
217 struct vop_setlabel_args;
218 
219 /*
220  * Label operations.
221  */
222 void	mac_init_bpfdesc(struct bpf_d *);
223 void	mac_init_cred(struct ucred *);
224 void	mac_init_devfsdirent(struct devfs_dirent *);
225 void	mac_init_ifnet(struct ifnet *);
226 void	mac_init_ipq(struct ipq *);
227 int	mac_init_socket(struct socket *, int flag);
228 void	mac_init_pipe(struct pipe *);
229 int	mac_init_mbuf(struct mbuf *m, int flag);
230 void	mac_init_mount(struct mount *);
231 void	mac_init_vnode(struct vnode *);
232 void	mac_destroy_bpfdesc(struct bpf_d *);
233 void	mac_destroy_cred(struct ucred *);
234 void	mac_destroy_devfsdirent(struct devfs_dirent *);
235 void	mac_destroy_ifnet(struct ifnet *);
236 void	mac_destroy_ipq(struct ipq *);
237 void	mac_destroy_socket(struct socket *);
238 void	mac_destroy_pipe(struct pipe *);
239 void	mac_destroy_mbuf(struct mbuf *);
240 void	mac_destroy_mount(struct mount *);
241 void	mac_destroy_vnode(struct vnode *);
242 
243 /*
244  * Labeling event operations: file system objects, and things that
245  * look a lot like file system objects.
246  */
247 void	mac_create_devfs_device(dev_t dev, struct devfs_dirent *de);
248 void	mac_create_devfs_directory(char *dirname, int dirnamelen,
249 	    struct devfs_dirent *de);
250 void	mac_create_devfs_vnode(struct devfs_dirent *de, struct vnode *vp);
251 void	mac_create_devfs_symlink(struct ucred *cred, struct devfs_dirent *dd,
252 	    struct devfs_dirent *de);
253 void	mac_create_vnode(struct ucred *cred, struct vnode *parent,
254 	    struct vnode *child);
255 void	mac_create_mount(struct ucred *cred, struct mount *mp);
256 void	mac_create_root_mount(struct ucred *cred, struct mount *mp);
257 void	mac_relabel_vnode(struct ucred *cred, struct vnode *vp,
258 	    struct label *newlabel);
259 void	mac_update_devfsdirent(struct devfs_dirent *de, struct vnode *vp);
260 void	mac_update_procfsvnode(struct vnode *vp, struct ucred *cred);
261 void	mac_update_vnode_from_mount(struct vnode *vp, struct mount *mp);
262 
263 /*
264  * Labeling event operations: IPC objects.
265  */
266 void	mac_create_mbuf_from_socket(struct socket *so, struct mbuf *m);
267 void	mac_create_socket(struct ucred *cred, struct socket *socket);
268 void	mac_create_socket_from_socket(struct socket *oldsocket,
269 	    struct socket *newsocket);
270 void	mac_set_socket_peer_from_mbuf(struct mbuf *mbuf,
271 	    struct socket *socket);
272 void	mac_set_socket_peer_from_socket(struct socket *oldsocket,
273 	    struct socket *newsocket);
274 void	mac_create_pipe(struct ucred *cred, struct pipe *pipe);
275 
276 /*
277  * Labeling event operations: network objects.
278  */
279 void	mac_create_bpfdesc(struct ucred *cred, struct bpf_d *bpf_d);
280 void	mac_create_ifnet(struct ifnet *ifp);
281 void	mac_create_ipq(struct mbuf *fragment, struct ipq *ipq);
282 void	mac_create_datagram_from_ipq(struct ipq *ipq, struct mbuf *datagram);
283 void	mac_create_fragment(struct mbuf *datagram, struct mbuf *fragment);
284 void	mac_create_mbuf_from_mbuf(struct mbuf *oldmbuf, struct mbuf *newmbuf);
285 void	mac_create_mbuf_linklayer(struct ifnet *ifnet, struct mbuf *m);
286 void	mac_create_mbuf_from_bpfdesc(struct bpf_d *bpf_d, struct mbuf *m);
287 void	mac_create_mbuf_from_ifnet(struct ifnet *ifnet, struct mbuf *m);
288 void	mac_create_mbuf_multicast_encap(struct mbuf *oldmbuf,
289 	    struct ifnet *ifnet, struct mbuf *newmbuf);
290 void	mac_create_mbuf_netlayer(struct mbuf *oldmbuf, struct mbuf *newmbuf);
291 int	mac_fragment_match(struct mbuf *fragment, struct ipq *ipq);
292 void	mac_update_ipq(struct mbuf *fragment, struct ipq *ipq);
293 
294 /*
295  * Labeling event operations: processes.
296  */
297 void	mac_create_cred(struct ucred *cred_parent, struct ucred *cred_child);
298 void	mac_execve_transition(struct ucred *old, struct ucred *new,
299 	    struct vnode *vp);
300 int	mac_execve_will_transition(struct ucred *old, struct vnode *vp);
301 void	mac_create_proc0(struct ucred *cred);
302 void	mac_create_proc1(struct ucred *cred);
303 void	mac_thread_userret(struct thread *td);
304 
305 /* Access control checks. */
306 int	mac_check_bpfdesc_receive(struct bpf_d *bpf_d, struct ifnet *ifnet);
307 int	mac_check_cred_visible(struct ucred *u1, struct ucred *u2);
308 int	mac_check_ifnet_transmit(struct ifnet *ifnet, struct mbuf *m);
309 int	mac_check_mount_stat(struct ucred *cred, struct mount *mp);
310 int	mac_check_pipe_ioctl(struct ucred *cred, struct pipe *pipe,
311 	    unsigned long cmd, void *data);
312 int	mac_check_pipe_poll(struct ucred *cred, struct pipe *pipe);
313 int	mac_check_pipe_read(struct ucred *cred, struct pipe *pipe);
314 int	mac_check_pipe_stat(struct ucred *cred, struct pipe *pipe);
315 int	mac_check_pipe_write(struct ucred *cred, struct pipe *pipe);
316 int	mac_check_proc_debug(struct ucred *cred, struct proc *proc);
317 int	mac_check_proc_sched(struct ucred *cred, struct proc *proc);
318 int	mac_check_proc_signal(struct ucred *cred, struct proc *proc,
319 	    int signum);
320 int	mac_check_socket_bind(struct ucred *cred, struct socket *so,
321 	    struct sockaddr *sockaddr);
322 int	mac_check_socket_connect(struct ucred *cred, struct socket *so,
323 	    struct sockaddr *sockaddr);
324 int	mac_check_socket_deliver(struct socket *so, struct mbuf *m);
325 int	mac_check_socket_listen(struct ucred *cred, struct socket *so);
326 int	mac_check_socket_receive(struct ucred *cred, struct socket *so);
327 int	mac_check_socket_send(struct ucred *cred, struct socket *so);
328 int	mac_check_socket_visible(struct ucred *cred, struct socket *so);
329 int	mac_check_vnode_access(struct ucred *cred, struct vnode *vp,
330 	    int flags);
331 int	mac_check_vnode_chdir(struct ucred *cred, struct vnode *dvp);
332 int	mac_check_vnode_chroot(struct ucred *cred, struct vnode *dvp);
333 int	mac_check_vnode_create(struct ucred *cred, struct vnode *dvp,
334 	    struct componentname *cnp, struct vattr *vap);
335 int	mac_check_vnode_delete(struct ucred *cred, struct vnode *dvp,
336 	    struct vnode *vp, struct componentname *cnp);
337 int	mac_check_vnode_deleteacl(struct ucred *cred, struct vnode *vp,
338 	    acl_type_t type);
339 int	mac_check_vnode_exec(struct ucred *cred, struct vnode *vp);
340 int	mac_check_vnode_getacl(struct ucred *cred, struct vnode *vp,
341 	    acl_type_t type);
342 int	mac_check_vnode_getextattr(struct ucred *cred, struct vnode *vp,
343 	    int attrnamespace, const char *name, struct uio *uio);
344 int	mac_check_vnode_link(struct ucred *cred, struct vnode *dvp,
345 	    struct vnode *vp, struct componentname *cnp);
346 int	mac_check_vnode_lookup(struct ucred *cred, struct vnode *dvp,
347  	    struct componentname *cnp);
348 int	mac_check_vnode_mmap(struct ucred *cred, struct vnode *vp,
349 	    int prot);
350 int	mac_check_vnode_mprotect(struct ucred *cred, struct vnode *vp,
351 	    int prot);
352 int	mac_check_vnode_open(struct ucred *cred, struct vnode *vp,
353 	    mode_t acc_mode);
354 int	mac_check_vnode_poll(struct ucred *active_cred,
355 	    struct ucred *file_cred, struct vnode *vp);
356 int	mac_check_vnode_read(struct ucred *active_cred,
357 	    struct ucred *file_cred, struct vnode *vp);
358 int	mac_check_vnode_readdir(struct ucred *cred, struct vnode *vp);
359 int	mac_check_vnode_readlink(struct ucred *cred, struct vnode *vp);
360 int	mac_check_vnode_rename_from(struct ucred *cred, struct vnode *dvp,
361 	    struct vnode *vp, struct componentname *cnp);
362 int	mac_check_vnode_rename_to(struct ucred *cred, struct vnode *dvp,
363 	    struct vnode *vp, int samedir, struct componentname *cnp);
364 int	mac_check_vnode_revoke(struct ucred *cred, struct vnode *vp);
365 int	mac_check_vnode_setacl(struct ucred *cred, struct vnode *vp,
366 	    acl_type_t type, struct acl *acl);
367 int	mac_check_vnode_setextattr(struct ucred *cred, struct vnode *vp,
368 	    int attrnamespace, const char *name, struct uio *uio);
369 int	mac_check_vnode_setflags(struct ucred *cred, struct vnode *vp,
370 	    u_long flags);
371 int	mac_check_vnode_setmode(struct ucred *cred, struct vnode *vp,
372 	    mode_t mode);
373 int	mac_check_vnode_setowner(struct ucred *cred, struct vnode *vp,
374 	    uid_t uid, gid_t gid);
375 int	mac_check_vnode_setutimes(struct ucred *cred, struct vnode *vp,
376 	    struct timespec atime, struct timespec mtime);
377 int	mac_check_vnode_stat(struct ucred *active_cred,
378 	    struct ucred *file_cred, struct vnode *vp);
379 int	mac_check_vnode_write(struct ucred *active_cred,
380 	    struct ucred *file_cred, struct vnode *vp);
381 int	mac_getsockopt_label_get(struct ucred *cred, struct socket *so,
382 	    struct mac *extmac);
383 int	mac_getsockopt_peerlabel_get(struct ucred *cred, struct socket *so,
384 	    struct mac *extmac);
385 int	mac_ioctl_ifnet_get(struct ucred *cred, struct ifreq *ifr,
386 	    struct ifnet *ifnet);
387 int	mac_ioctl_ifnet_set(struct ucred *cred, struct ifreq *ifr,
388 	    struct ifnet *ifnet);
389 int	mac_setsockopt_label_set(struct ucred *cred, struct socket *so,
390 	    struct mac *extmac);
391 int	mac_pipe_label_set(struct ucred *cred, struct pipe *pipe,
392 	    struct label *label);
393 
394 /*
395  * Calls to help various file systems implement labeling functionality
396  * using their existing EA implementation.
397  */
398 int	vop_stdcreatevnode_ea(struct vnode *dvp, struct vnode *tvp,
399 	    struct ucred *cred);
400 int	vop_stdrefreshlabel_ea(struct vop_refreshlabel_args *ap);
401 int	vop_stdsetlabel_ea(struct vop_setlabel_args *ap);
402 
403 #endif /* _KERNEL */
404 
405 #endif /* !_SYS_MAC_H */
406