mac_policy.h revision 107089
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 107089 2002-11-19 22:12:42Z 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_kld_load)(struct ucred *cred, struct vnode *vp, 276 struct label *vlabel); 277 int (*mpo_check_kld_stat)(struct ucred *cred); 278 int (*mpo_check_kld_unload)(struct ucred *cred); 279 int (*mpo_check_mount_stat)(struct ucred *cred, struct mount *mp, 280 struct label *mntlabel); 281 int (*mpo_check_pipe_ioctl)(struct ucred *cred, struct pipe *pipe, 282 struct label *pipelabel, unsigned long cmd, void *data); 283 int (*mpo_check_pipe_poll)(struct ucred *cred, struct pipe *pipe, 284 struct label *pipelabel); 285 int (*mpo_check_pipe_read)(struct ucred *cred, struct pipe *pipe, 286 struct label *pipelabel); 287 int (*mpo_check_pipe_relabel)(struct ucred *cred, 288 struct pipe *pipe, struct label *pipelabel, 289 struct label *newlabel); 290 int (*mpo_check_pipe_stat)(struct ucred *cred, struct pipe *pipe, 291 struct label *pipelabel); 292 int (*mpo_check_pipe_write)(struct ucred *cred, struct pipe *pipe, 293 struct label *pipelabel); 294 int (*mpo_check_proc_debug)(struct ucred *cred, 295 struct proc *proc); 296 int (*mpo_check_proc_sched)(struct ucred *cred, 297 struct proc *proc); 298 int (*mpo_check_proc_signal)(struct ucred *cred, 299 struct proc *proc, int signum); 300 int (*mpo_check_socket_bind)(struct ucred *cred, 301 struct socket *so, struct label *socketlabel, 302 struct sockaddr *sockaddr); 303 int (*mpo_check_socket_connect)(struct ucred *cred, 304 struct socket *so, struct label *socketlabel, 305 struct sockaddr *sockaddr); 306 int (*mpo_check_socket_deliver)(struct socket *so, 307 struct label *socketlabel, struct mbuf *m, 308 struct label *mbuflabel); 309 int (*mpo_check_socket_listen)(struct ucred *cred, 310 struct socket *so, struct label *socketlabel); 311 int (*mpo_check_socket_receive)(struct ucred *cred, 312 struct socket *so, struct label *socketlabel); 313 int (*mpo_check_socket_relabel)(struct ucred *cred, 314 struct socket *so, struct label *socketlabel, 315 struct label *newlabel); 316 int (*mpo_check_socket_send)(struct ucred *cred, 317 struct socket *so, struct label *socketlabel); 318 int (*mpo_check_socket_visible)(struct ucred *cred, 319 struct socket *so, struct label *socketlabel); 320 int (*mpo_check_system_acct)(struct ucred *cred, 321 struct vnode *vp, struct label *vlabel); 322 int (*mpo_check_system_nfsd)(struct ucred *cred); 323 int (*mpo_check_system_reboot)(struct ucred *cred, int howto); 324 int (*mpo_check_system_settime)(struct ucred *cred); 325 int (*mpo_check_system_swapon)(struct ucred *cred, 326 struct vnode *vp, struct label *label); 327 int (*mpo_check_system_sysctl)(struct ucred *cred, int *name, 328 u_int namelen, void *old, size_t *oldlenp, int inkernel, 329 void *new, size_t newlen); 330 int (*mpo_check_vnode_access)(struct ucred *cred, 331 struct vnode *vp, struct label *label, int acc_mode); 332 int (*mpo_check_vnode_chdir)(struct ucred *cred, 333 struct vnode *dvp, struct label *dlabel); 334 int (*mpo_check_vnode_chroot)(struct ucred *cred, 335 struct vnode *dvp, struct label *dlabel); 336 int (*mpo_check_vnode_create)(struct ucred *cred, 337 struct vnode *dvp, struct label *dlabel, 338 struct componentname *cnp, struct vattr *vap); 339 int (*mpo_check_vnode_delete)(struct ucred *cred, 340 struct vnode *dvp, struct label *dlabel, 341 struct vnode *vp, struct label *label, 342 struct componentname *cnp); 343 int (*mpo_check_vnode_deleteacl)(struct ucred *cred, 344 struct vnode *vp, struct label *label, acl_type_t type); 345 int (*mpo_check_vnode_exec)(struct ucred *cred, struct vnode *vp, 346 struct label *label, struct image_params *imgp, 347 struct label *execlabel); 348 int (*mpo_check_vnode_getacl)(struct ucred *cred, 349 struct vnode *vp, struct label *label, acl_type_t type); 350 int (*mpo_check_vnode_getextattr)(struct ucred *cred, 351 struct vnode *vp, struct label *label, int attrnamespace, 352 const char *name, struct uio *uio); 353 int (*mpo_check_vnode_link)(struct ucred *cred, struct vnode *dvp, 354 struct label *dlabel, struct vnode *vp, 355 struct label *label, struct componentname *cnp); 356 int (*mpo_check_vnode_lookup)(struct ucred *cred, 357 struct vnode *dvp, struct label *dlabel, 358 struct componentname *cnp); 359 int (*mpo_check_vnode_mmap)(struct ucred *cred, struct vnode *vp, 360 struct label *label, int prot); 361 void (*mpo_check_vnode_mmap_downgrade)(struct ucred *cred, 362 struct vnode *vp, struct label *label, int *prot); 363 int (*mpo_check_vnode_mprotect)(struct ucred *cred, 364 struct vnode *vp, struct label *label, int prot); 365 int (*mpo_check_vnode_open)(struct ucred *cred, struct vnode *vp, 366 struct label *label, int acc_mode); 367 int (*mpo_check_vnode_poll)(struct ucred *active_cred, 368 struct ucred *file_cred, struct vnode *vp, 369 struct label *label); 370 int (*mpo_check_vnode_read)(struct ucred *active_cred, 371 struct ucred *file_cred, struct vnode *vp, 372 struct label *label); 373 int (*mpo_check_vnode_readdir)(struct ucred *cred, 374 struct vnode *dvp, struct label *dlabel); 375 int (*mpo_check_vnode_readlink)(struct ucred *cred, 376 struct vnode *vp, struct label *label); 377 int (*mpo_check_vnode_relabel)(struct ucred *cred, 378 struct vnode *vp, struct label *vnodelabel, 379 struct label *newlabel); 380 int (*mpo_check_vnode_rename_from)(struct ucred *cred, 381 struct vnode *dvp, struct label *dlabel, struct vnode *vp, 382 struct label *label, struct componentname *cnp); 383 int (*mpo_check_vnode_rename_to)(struct ucred *cred, 384 struct vnode *dvp, struct label *dlabel, struct vnode *vp, 385 struct label *label, int samedir, 386 struct componentname *cnp); 387 int (*mpo_check_vnode_revoke)(struct ucred *cred, 388 struct vnode *vp, struct label *label); 389 int (*mpo_check_vnode_setacl)(struct ucred *cred, 390 struct vnode *vp, struct label *label, acl_type_t type, 391 struct acl *acl); 392 int (*mpo_check_vnode_setextattr)(struct ucred *cred, 393 struct vnode *vp, struct label *label, int attrnamespace, 394 const char *name, struct uio *uio); 395 int (*mpo_check_vnode_setflags)(struct ucred *cred, 396 struct vnode *vp, struct label *label, u_long flags); 397 int (*mpo_check_vnode_setmode)(struct ucred *cred, 398 struct vnode *vp, struct label *label, mode_t mode); 399 int (*mpo_check_vnode_setowner)(struct ucred *cred, 400 struct vnode *vp, struct label *label, uid_t uid, 401 gid_t gid); 402 int (*mpo_check_vnode_setutimes)(struct ucred *cred, 403 struct vnode *vp, struct label *label, 404 struct timespec atime, struct timespec mtime); 405 int (*mpo_check_vnode_stat)(struct ucred *active_cred, 406 struct ucred *file_cred, struct vnode *vp, 407 struct label *label); 408 int (*mpo_check_vnode_write)(struct ucred *active_cred, 409 struct ucred *file_cred, struct vnode *vp, 410 struct label *label); 411}; 412 413struct mac_policy_conf { 414 char *mpc_name; /* policy name */ 415 char *mpc_fullname; /* policy full name */ 416 struct mac_policy_ops *mpc_ops; /* policy operations */ 417 int mpc_loadtime_flags; /* flags */ 418 int *mpc_field_off; /* security field */ 419 int mpc_runtime_flags; /* flags */ 420 LIST_ENTRY(mac_policy_conf) mpc_list; /* global list */ 421}; 422 423/* Flags for the mpc_loadtime_flags field. */ 424#define MPC_LOADTIME_FLAG_NOTLATE 0x00000001 425#define MPC_LOADTIME_FLAG_UNLOADOK 0x00000002 426 427/* Flags for the mpc_runtime_flags field. */ 428#define MPC_RUNTIME_FLAG_REGISTERED 0x00000001 429 430#define MAC_POLICY_SET(mpops, mpname, mpfullname, mpflags, privdata_wanted) \ 431 static struct mac_policy_conf mpname##_mac_policy_conf = { \ 432 #mpname, \ 433 mpfullname, \ 434 mpops, \ 435 mpflags, \ 436 privdata_wanted, \ 437 0, \ 438 }; \ 439 static moduledata_t mpname##_mod = { \ 440 #mpname, \ 441 mac_policy_modevent, \ 442 &mpname##_mac_policy_conf \ 443 }; \ 444 MODULE_DEPEND(mpname, kernel_mac_support, 1, 1, 1); \ 445 DECLARE_MODULE(mpname, mpname##_mod, SI_SUB_MAC_POLICY, \ 446 SI_ORDER_MIDDLE) 447 448int mac_policy_modevent(module_t mod, int type, void *data); 449 450#define LABEL_TO_SLOT(l, s) (l)->l_perpolicy[s] 451 452#endif /* !_SYS_MAC_POLICY_H */ 453