The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/security/mac_biba/mac_biba.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1999-2002, 2007-2011 Robert N. M. Watson
    3  * Copyright (c) 2001-2005 McAfee, Inc.
    4  * Copyright (c) 2006 SPARTA, Inc.
    5  * All rights reserved.
    6  *
    7  * This software was developed by Robert Watson for the TrustedBSD Project.
    8  *
    9  * This software was developed for the FreeBSD Project in part by McAfee
   10  * Research, the Security Research Division of McAfee, Inc. under
   11  * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
   12  * CHATS research program.
   13  *
   14  * This software was enhanced by SPARTA ISSO under SPAWAR contract
   15  * N66001-04-C-6019 ("SEFOS").
   16  *
   17  * This software was developed at the University of Cambridge Computer
   18  * Laboratory with support from a grant from Google, Inc.
   19  *
   20  * Redistribution and use in source and binary forms, with or without
   21  * modification, are permitted provided that the following conditions
   22  * are met:
   23  * 1. Redistributions of source code must retain the above copyright
   24  *    notice, this list of conditions and the following disclaimer.
   25  * 2. Redistributions in binary form must reproduce the above copyright
   26  *    notice, this list of conditions and the following disclaimer in the
   27  *    documentation and/or other materials provided with the distribution.
   28  *
   29  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   39  * SUCH DAMAGE.
   40  *
   41  * $FreeBSD: releng/10.0/sys/security/mac_biba/mac_biba.c 254603 2013-08-21 17:45:00Z kib $
   42  */
   43 
   44 /*
   45  * Developed by the TrustedBSD Project.
   46  *
   47  * Biba fixed label mandatory integrity policy.
   48  */
   49 
   50 #include <sys/param.h>
   51 #include <sys/conf.h>
   52 #include <sys/extattr.h>
   53 #include <sys/kernel.h>
   54 #include <sys/ksem.h>
   55 #include <sys/malloc.h>
   56 #include <sys/mman.h>
   57 #include <sys/mount.h>
   58 #include <sys/priv.h>
   59 #include <sys/proc.h>
   60 #include <sys/sbuf.h>
   61 #include <sys/systm.h>
   62 #include <sys/sysproto.h>
   63 #include <sys/sysent.h>
   64 #include <sys/systm.h>
   65 #include <sys/vnode.h>
   66 #include <sys/file.h>
   67 #include <sys/socket.h>
   68 #include <sys/socketvar.h>
   69 #include <sys/pipe.h>
   70 #include <sys/sx.h>
   71 #include <sys/sysctl.h>
   72 #include <sys/msg.h>
   73 #include <sys/sem.h>
   74 #include <sys/shm.h>
   75 
   76 #include <fs/devfs/devfs.h>
   77 
   78 #include <net/bpfdesc.h>
   79 #include <net/if.h>
   80 #include <net/if_types.h>
   81 #include <net/if_var.h>
   82 
   83 #include <netinet/in.h>
   84 #include <netinet/in_pcb.h>
   85 #include <netinet/ip_var.h>
   86 
   87 #include <vm/uma.h>
   88 #include <vm/vm.h>
   89 
   90 #include <security/mac/mac_policy.h>
   91 #include <security/mac_biba/mac_biba.h>
   92 
   93 SYSCTL_DECL(_security_mac);
   94 
   95 static SYSCTL_NODE(_security_mac, OID_AUTO, biba, CTLFLAG_RW, 0,
   96     "TrustedBSD mac_biba policy controls");
   97 
   98 static int      biba_label_size = sizeof(struct mac_biba);
   99 SYSCTL_INT(_security_mac_biba, OID_AUTO, label_size, CTLFLAG_RD,
  100     &biba_label_size, 0, "Size of struct mac_biba");
  101 
  102 static int      biba_enabled = 1;
  103 SYSCTL_INT(_security_mac_biba, OID_AUTO, enabled, CTLFLAG_RW, &biba_enabled,
  104     0, "Enforce MAC/Biba policy");
  105 TUNABLE_INT("security.mac.biba.enabled", &biba_enabled);
  106 
  107 static int      destroyed_not_inited;
  108 SYSCTL_INT(_security_mac_biba, OID_AUTO, destroyed_not_inited, CTLFLAG_RD,
  109     &destroyed_not_inited, 0, "Count of labels destroyed but not inited");
  110 
  111 static int      trust_all_interfaces = 0;
  112 SYSCTL_INT(_security_mac_biba, OID_AUTO, trust_all_interfaces, CTLFLAG_RD,
  113     &trust_all_interfaces, 0, "Consider all interfaces 'trusted' by MAC/Biba");
  114 TUNABLE_INT("security.mac.biba.trust_all_interfaces", &trust_all_interfaces);
  115 
  116 static char     trusted_interfaces[128];
  117 SYSCTL_STRING(_security_mac_biba, OID_AUTO, trusted_interfaces, CTLFLAG_RD,
  118     trusted_interfaces, 0, "Interfaces considered 'trusted' by MAC/Biba");
  119 TUNABLE_STR("security.mac.biba.trusted_interfaces", trusted_interfaces,
  120     sizeof(trusted_interfaces));
  121 
  122 static int      max_compartments = MAC_BIBA_MAX_COMPARTMENTS;
  123 SYSCTL_INT(_security_mac_biba, OID_AUTO, max_compartments, CTLFLAG_RD,
  124     &max_compartments, 0, "Maximum supported compartments");
  125 
  126 static int      ptys_equal = 0;
  127 SYSCTL_INT(_security_mac_biba, OID_AUTO, ptys_equal, CTLFLAG_RW, &ptys_equal,
  128     0, "Label pty devices as biba/equal on create");
  129 TUNABLE_INT("security.mac.biba.ptys_equal", &ptys_equal);
  130 
  131 static int      interfaces_equal = 1;
  132 SYSCTL_INT(_security_mac_biba, OID_AUTO, interfaces_equal, CTLFLAG_RW,
  133     &interfaces_equal, 0, "Label network interfaces as biba/equal on create");
  134 TUNABLE_INT("security.mac.biba.interfaces_equal", &interfaces_equal);
  135 
  136 static int      revocation_enabled = 0;
  137 SYSCTL_INT(_security_mac_biba, OID_AUTO, revocation_enabled, CTLFLAG_RW,
  138     &revocation_enabled, 0, "Revoke access to objects on relabel");
  139 TUNABLE_INT("security.mac.biba.revocation_enabled", &revocation_enabled);
  140 
  141 static int      biba_slot;
  142 #define SLOT(l) ((struct mac_biba *)mac_label_get((l), biba_slot))
  143 #define SLOT_SET(l, val) mac_label_set((l), biba_slot, (uintptr_t)(val))
  144 
  145 static uma_zone_t       zone_biba;
  146 
  147 static __inline int
  148 biba_bit_set_empty(u_char *set) {
  149         int i;
  150 
  151         for (i = 0; i < MAC_BIBA_MAX_COMPARTMENTS >> 3; i++)
  152                 if (set[i] != 0)
  153                         return (0);
  154         return (1);
  155 }
  156 
  157 static struct mac_biba *
  158 biba_alloc(int flag)
  159 {
  160 
  161         return (uma_zalloc(zone_biba, flag | M_ZERO));
  162 }
  163 
  164 static void
  165 biba_free(struct mac_biba *mb)
  166 {
  167 
  168         if (mb != NULL)
  169                 uma_zfree(zone_biba, mb);
  170         else
  171                 atomic_add_int(&destroyed_not_inited, 1);
  172 }
  173 
  174 static int
  175 biba_atmostflags(struct mac_biba *mb, int flags)
  176 {
  177 
  178         if ((mb->mb_flags & flags) != mb->mb_flags)
  179                 return (EINVAL);
  180         return (0);
  181 }
  182 
  183 static int
  184 biba_dominate_element(struct mac_biba_element *a, struct mac_biba_element *b)
  185 {
  186         int bit;
  187 
  188         switch (a->mbe_type) {
  189         case MAC_BIBA_TYPE_EQUAL:
  190         case MAC_BIBA_TYPE_HIGH:
  191                 return (1);
  192 
  193         case MAC_BIBA_TYPE_LOW:
  194                 switch (b->mbe_type) {
  195                 case MAC_BIBA_TYPE_GRADE:
  196                 case MAC_BIBA_TYPE_HIGH:
  197                         return (0);
  198 
  199                 case MAC_BIBA_TYPE_EQUAL:
  200                 case MAC_BIBA_TYPE_LOW:
  201                         return (1);
  202 
  203                 default:
  204                         panic("biba_dominate_element: b->mbe_type invalid");
  205                 }
  206 
  207         case MAC_BIBA_TYPE_GRADE:
  208                 switch (b->mbe_type) {
  209                 case MAC_BIBA_TYPE_EQUAL:
  210                 case MAC_BIBA_TYPE_LOW:
  211                         return (1);
  212 
  213                 case MAC_BIBA_TYPE_HIGH:
  214                         return (0);
  215 
  216                 case MAC_BIBA_TYPE_GRADE:
  217                         for (bit = 1; bit <= MAC_BIBA_MAX_COMPARTMENTS; bit++)
  218                                 if (!MAC_BIBA_BIT_TEST(bit,
  219                                     a->mbe_compartments) &&
  220                                     MAC_BIBA_BIT_TEST(bit, b->mbe_compartments))
  221                                         return (0);
  222                         return (a->mbe_grade >= b->mbe_grade);
  223 
  224                 default:
  225                         panic("biba_dominate_element: b->mbe_type invalid");
  226                 }
  227 
  228         default:
  229                 panic("biba_dominate_element: a->mbe_type invalid");
  230         }
  231 
  232         return (0);
  233 }
  234 
  235 static int
  236 biba_subject_dominate_high(struct mac_biba *mb)
  237 {
  238         struct mac_biba_element *element;
  239 
  240         KASSERT((mb->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0,
  241             ("biba_effective_in_range: mb not effective"));
  242         element = &mb->mb_effective;
  243 
  244         return (element->mbe_type == MAC_BIBA_TYPE_EQUAL ||
  245             element->mbe_type == MAC_BIBA_TYPE_HIGH);
  246 }
  247 
  248 static int
  249 biba_range_in_range(struct mac_biba *rangea, struct mac_biba *rangeb)
  250 {
  251 
  252         return (biba_dominate_element(&rangeb->mb_rangehigh,
  253             &rangea->mb_rangehigh) &&
  254             biba_dominate_element(&rangea->mb_rangelow,
  255             &rangeb->mb_rangelow));
  256 }
  257 
  258 static int
  259 biba_effective_in_range(struct mac_biba *effective, struct mac_biba *range)
  260 {
  261 
  262         KASSERT((effective->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0,
  263             ("biba_effective_in_range: a not effective"));
  264         KASSERT((range->mb_flags & MAC_BIBA_FLAG_RANGE) != 0,
  265             ("biba_effective_in_range: b not range"));
  266 
  267         return (biba_dominate_element(&range->mb_rangehigh,
  268             &effective->mb_effective) &&
  269             biba_dominate_element(&effective->mb_effective,
  270             &range->mb_rangelow));
  271 
  272         return (1);
  273 }
  274 
  275 static int
  276 biba_dominate_effective(struct mac_biba *a, struct mac_biba *b)
  277 {
  278         KASSERT((a->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0,
  279             ("biba_dominate_effective: a not effective"));
  280         KASSERT((b->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0,
  281             ("biba_dominate_effective: b not effective"));
  282 
  283         return (biba_dominate_element(&a->mb_effective, &b->mb_effective));
  284 }
  285 
  286 static int
  287 biba_equal_element(struct mac_biba_element *a, struct mac_biba_element *b)
  288 {
  289 
  290         if (a->mbe_type == MAC_BIBA_TYPE_EQUAL ||
  291             b->mbe_type == MAC_BIBA_TYPE_EQUAL)
  292                 return (1);
  293 
  294         return (a->mbe_type == b->mbe_type && a->mbe_grade == b->mbe_grade);
  295 }
  296 
  297 static int
  298 biba_equal_effective(struct mac_biba *a, struct mac_biba *b)
  299 {
  300 
  301         KASSERT((a->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0,
  302             ("biba_equal_effective: a not effective"));
  303         KASSERT((b->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0,
  304             ("biba_equal_effective: b not effective"));
  305 
  306         return (biba_equal_element(&a->mb_effective, &b->mb_effective));
  307 }
  308 
  309 static int
  310 biba_contains_equal(struct mac_biba *mb)
  311 {
  312 
  313         if (mb->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) {
  314                 if (mb->mb_effective.mbe_type == MAC_BIBA_TYPE_EQUAL)
  315                         return (1);
  316         }
  317 
  318         if (mb->mb_flags & MAC_BIBA_FLAG_RANGE) {
  319                 if (mb->mb_rangelow.mbe_type == MAC_BIBA_TYPE_EQUAL)
  320                         return (1);
  321                 if (mb->mb_rangehigh.mbe_type == MAC_BIBA_TYPE_EQUAL)
  322                         return (1);
  323         }
  324 
  325         return (0);
  326 }
  327 
  328 static int
  329 biba_subject_privileged(struct mac_biba *mb)
  330 {
  331 
  332         KASSERT((mb->mb_flags & MAC_BIBA_FLAGS_BOTH) == MAC_BIBA_FLAGS_BOTH,
  333             ("biba_subject_privileged: subject doesn't have both labels"));
  334 
  335         /* If the effective is EQUAL, it's ok. */
  336         if (mb->mb_effective.mbe_type == MAC_BIBA_TYPE_EQUAL)
  337                 return (0);
  338 
  339         /* If either range endpoint is EQUAL, it's ok. */
  340         if (mb->mb_rangelow.mbe_type == MAC_BIBA_TYPE_EQUAL ||
  341             mb->mb_rangehigh.mbe_type == MAC_BIBA_TYPE_EQUAL)
  342                 return (0);
  343 
  344         /* If the range is low-high, it's ok. */
  345         if (mb->mb_rangelow.mbe_type == MAC_BIBA_TYPE_LOW &&
  346             mb->mb_rangehigh.mbe_type == MAC_BIBA_TYPE_HIGH)
  347                 return (0);
  348 
  349         /* It's not ok. */
  350         return (EPERM);
  351 }
  352 
  353 static int
  354 biba_high_effective(struct mac_biba *mb)
  355 {
  356 
  357         KASSERT((mb->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0,
  358             ("biba_equal_effective: mb not effective"));
  359 
  360         return (mb->mb_effective.mbe_type == MAC_BIBA_TYPE_HIGH);
  361 }
  362 
  363 static int
  364 biba_valid(struct mac_biba *mb)
  365 {
  366 
  367         if (mb->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) {
  368                 switch (mb->mb_effective.mbe_type) {
  369                 case MAC_BIBA_TYPE_GRADE:
  370                         break;
  371 
  372                 case MAC_BIBA_TYPE_EQUAL:
  373                 case MAC_BIBA_TYPE_HIGH:
  374                 case MAC_BIBA_TYPE_LOW:
  375                         if (mb->mb_effective.mbe_grade != 0 ||
  376                             !MAC_BIBA_BIT_SET_EMPTY(
  377                             mb->mb_effective.mbe_compartments))
  378                                 return (EINVAL);
  379                         break;
  380 
  381                 default:
  382                         return (EINVAL);
  383                 }
  384         } else {
  385                 if (mb->mb_effective.mbe_type != MAC_BIBA_TYPE_UNDEF)
  386                         return (EINVAL);
  387         }
  388 
  389         if (mb->mb_flags & MAC_BIBA_FLAG_RANGE) {
  390                 switch (mb->mb_rangelow.mbe_type) {
  391                 case MAC_BIBA_TYPE_GRADE:
  392                         break;
  393 
  394                 case MAC_BIBA_TYPE_EQUAL:
  395                 case MAC_BIBA_TYPE_HIGH:
  396                 case MAC_BIBA_TYPE_LOW:
  397                         if (mb->mb_rangelow.mbe_grade != 0 ||
  398                             !MAC_BIBA_BIT_SET_EMPTY(
  399                             mb->mb_rangelow.mbe_compartments))
  400                                 return (EINVAL);
  401                         break;
  402 
  403                 default:
  404                         return (EINVAL);
  405                 }
  406 
  407                 switch (mb->mb_rangehigh.mbe_type) {
  408                 case MAC_BIBA_TYPE_GRADE:
  409                         break;
  410 
  411                 case MAC_BIBA_TYPE_EQUAL:
  412                 case MAC_BIBA_TYPE_HIGH:
  413                 case MAC_BIBA_TYPE_LOW:
  414                         if (mb->mb_rangehigh.mbe_grade != 0 ||
  415                             !MAC_BIBA_BIT_SET_EMPTY(
  416                             mb->mb_rangehigh.mbe_compartments))
  417                                 return (EINVAL);
  418                         break;
  419 
  420                 default:
  421                         return (EINVAL);
  422                 }
  423                 if (!biba_dominate_element(&mb->mb_rangehigh,
  424                     &mb->mb_rangelow))
  425                         return (EINVAL);
  426         } else {
  427                 if (mb->mb_rangelow.mbe_type != MAC_BIBA_TYPE_UNDEF ||
  428                     mb->mb_rangehigh.mbe_type != MAC_BIBA_TYPE_UNDEF)
  429                         return (EINVAL);
  430         }
  431 
  432         return (0);
  433 }
  434 
  435 static void
  436 biba_set_range(struct mac_biba *mb, u_short typelow, u_short gradelow,
  437     u_char *compartmentslow, u_short typehigh, u_short gradehigh,
  438     u_char *compartmentshigh)
  439 {
  440 
  441         mb->mb_rangelow.mbe_type = typelow;
  442         mb->mb_rangelow.mbe_grade = gradelow;
  443         if (compartmentslow != NULL)
  444                 memcpy(mb->mb_rangelow.mbe_compartments, compartmentslow,
  445                     sizeof(mb->mb_rangelow.mbe_compartments));
  446         mb->mb_rangehigh.mbe_type = typehigh;
  447         mb->mb_rangehigh.mbe_grade = gradehigh;
  448         if (compartmentshigh != NULL)
  449                 memcpy(mb->mb_rangehigh.mbe_compartments, compartmentshigh,
  450                     sizeof(mb->mb_rangehigh.mbe_compartments));
  451         mb->mb_flags |= MAC_BIBA_FLAG_RANGE;
  452 }
  453 
  454 static void
  455 biba_set_effective(struct mac_biba *mb, u_short type, u_short grade,
  456     u_char *compartments)
  457 {
  458 
  459         mb->mb_effective.mbe_type = type;
  460         mb->mb_effective.mbe_grade = grade;
  461         if (compartments != NULL)
  462                 memcpy(mb->mb_effective.mbe_compartments, compartments,
  463                     sizeof(mb->mb_effective.mbe_compartments));
  464         mb->mb_flags |= MAC_BIBA_FLAG_EFFECTIVE;
  465 }
  466 
  467 static void
  468 biba_copy_range(struct mac_biba *labelfrom, struct mac_biba *labelto)
  469 {
  470 
  471         KASSERT((labelfrom->mb_flags & MAC_BIBA_FLAG_RANGE) != 0,
  472             ("biba_copy_range: labelfrom not range"));
  473 
  474         labelto->mb_rangelow = labelfrom->mb_rangelow;
  475         labelto->mb_rangehigh = labelfrom->mb_rangehigh;
  476         labelto->mb_flags |= MAC_BIBA_FLAG_RANGE;
  477 }
  478 
  479 static void
  480 biba_copy_effective(struct mac_biba *labelfrom, struct mac_biba *labelto)
  481 {
  482 
  483         KASSERT((labelfrom->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0,
  484             ("biba_copy_effective: labelfrom not effective"));
  485 
  486         labelto->mb_effective = labelfrom->mb_effective;
  487         labelto->mb_flags |= MAC_BIBA_FLAG_EFFECTIVE;
  488 }
  489 
  490 static void
  491 biba_copy(struct mac_biba *source, struct mac_biba *dest)
  492 {
  493 
  494         if (source->mb_flags & MAC_BIBA_FLAG_EFFECTIVE)
  495                 biba_copy_effective(source, dest);
  496         if (source->mb_flags & MAC_BIBA_FLAG_RANGE)
  497                 biba_copy_range(source, dest);
  498 }
  499 
  500 /*
  501  * Policy module operations.
  502  */
  503 static void
  504 biba_init(struct mac_policy_conf *conf)
  505 {
  506 
  507         zone_biba = uma_zcreate("mac_biba", sizeof(struct mac_biba), NULL,
  508             NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
  509 }
  510 
  511 /*
  512  * Label operations.
  513  */
  514 static void
  515 biba_init_label(struct label *label)
  516 {
  517 
  518         SLOT_SET(label, biba_alloc(M_WAITOK));
  519 }
  520 
  521 static int
  522 biba_init_label_waitcheck(struct label *label, int flag)
  523 {
  524 
  525         SLOT_SET(label, biba_alloc(flag));
  526         if (SLOT(label) == NULL)
  527                 return (ENOMEM);
  528 
  529         return (0);
  530 }
  531 
  532 static void
  533 biba_destroy_label(struct label *label)
  534 {
  535 
  536         biba_free(SLOT(label));
  537         SLOT_SET(label, NULL);
  538 }
  539 
  540 /*
  541  * biba_element_to_string() accepts an sbuf and Biba element.  It converts
  542  * the Biba element to a string and stores the result in the sbuf; if there
  543  * isn't space in the sbuf, -1 is returned.
  544  */
  545 static int
  546 biba_element_to_string(struct sbuf *sb, struct mac_biba_element *element)
  547 {
  548         int i, first;
  549 
  550         switch (element->mbe_type) {
  551         case MAC_BIBA_TYPE_HIGH:
  552                 return (sbuf_printf(sb, "high"));
  553 
  554         case MAC_BIBA_TYPE_LOW:
  555                 return (sbuf_printf(sb, "low"));
  556 
  557         case MAC_BIBA_TYPE_EQUAL:
  558                 return (sbuf_printf(sb, "equal"));
  559 
  560         case MAC_BIBA_TYPE_GRADE:
  561                 if (sbuf_printf(sb, "%d", element->mbe_grade) == -1)
  562                         return (-1);
  563 
  564                 first = 1;
  565                 for (i = 1; i <= MAC_BIBA_MAX_COMPARTMENTS; i++) {
  566                         if (MAC_BIBA_BIT_TEST(i, element->mbe_compartments)) {
  567                                 if (first) {
  568                                         if (sbuf_putc(sb, ':') == -1)
  569                                                 return (-1);
  570                                         if (sbuf_printf(sb, "%d", i) == -1)
  571                                                 return (-1);
  572                                         first = 0;
  573                                 } else {
  574                                         if (sbuf_printf(sb, "+%d", i) == -1)
  575                                                 return (-1);
  576                                 }
  577                         }
  578                 }
  579                 return (0);
  580 
  581         default:
  582                 panic("biba_element_to_string: invalid type (%d)",
  583                     element->mbe_type);
  584         }
  585 }
  586 
  587 /*
  588  * biba_to_string() converts a Biba label to a string, and places the results
  589  * in the passed sbuf.  It returns 0 on success, or EINVAL if there isn't
  590  * room in the sbuf.  Note: the sbuf will be modified even in a failure case,
  591  * so the caller may need to revert the sbuf by restoring the offset if
  592  * that's undesired.
  593  */
  594 static int
  595 biba_to_string(struct sbuf *sb, struct mac_biba *mb)
  596 {
  597 
  598         if (mb->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) {
  599                 if (biba_element_to_string(sb, &mb->mb_effective) == -1)
  600                         return (EINVAL);
  601         }
  602 
  603         if (mb->mb_flags & MAC_BIBA_FLAG_RANGE) {
  604                 if (sbuf_putc(sb, '(') == -1)
  605                         return (EINVAL);
  606 
  607                 if (biba_element_to_string(sb, &mb->mb_rangelow) == -1)
  608                         return (EINVAL);
  609 
  610                 if (sbuf_putc(sb, '-') == -1)
  611                         return (EINVAL);
  612 
  613                 if (biba_element_to_string(sb, &mb->mb_rangehigh) == -1)
  614                         return (EINVAL);
  615 
  616                 if (sbuf_putc(sb, ')') == -1)
  617                         return (EINVAL);
  618         }
  619 
  620         return (0);
  621 }
  622 
  623 static int
  624 biba_externalize_label(struct label *label, char *element_name,
  625     struct sbuf *sb, int *claimed)
  626 {
  627         struct mac_biba *mb;
  628 
  629         if (strcmp(MAC_BIBA_LABEL_NAME, element_name) != 0)
  630                 return (0);
  631 
  632         (*claimed)++;
  633 
  634         mb = SLOT(label);
  635         return (biba_to_string(sb, mb));
  636 }
  637 
  638 static int
  639 biba_parse_element(struct mac_biba_element *element, char *string)
  640 {
  641         char *compartment, *end, *grade;
  642         int value;
  643 
  644         if (strcmp(string, "high") == 0 || strcmp(string, "hi") == 0) {
  645                 element->mbe_type = MAC_BIBA_TYPE_HIGH;
  646                 element->mbe_grade = MAC_BIBA_TYPE_UNDEF;
  647         } else if (strcmp(string, "low") == 0 || strcmp(string, "lo") == 0) {
  648                 element->mbe_type = MAC_BIBA_TYPE_LOW;
  649                 element->mbe_grade = MAC_BIBA_TYPE_UNDEF;
  650         } else if (strcmp(string, "equal") == 0 ||
  651             strcmp(string, "eq") == 0) {
  652                 element->mbe_type = MAC_BIBA_TYPE_EQUAL;
  653                 element->mbe_grade = MAC_BIBA_TYPE_UNDEF;
  654         } else {
  655                 element->mbe_type = MAC_BIBA_TYPE_GRADE;
  656 
  657                 /*
  658                  * Numeric grade piece of the element.
  659                  */
  660                 grade = strsep(&string, ":");
  661                 value = strtol(grade, &end, 10);
  662                 if (end == grade || *end != '\0')
  663                         return (EINVAL);
  664                 if (value < 0 || value > 65535)
  665                         return (EINVAL);
  666                 element->mbe_grade = value;
  667 
  668                 /*
  669                  * Optional compartment piece of the element.  If none are
  670                  * included, we assume that the label has no compartments.
  671                  */
  672                 if (string == NULL)
  673                         return (0);
  674                 if (*string == '\0')
  675                         return (0);
  676 
  677                 while ((compartment = strsep(&string, "+")) != NULL) {
  678                         value = strtol(compartment, &end, 10);
  679                         if (compartment == end || *end != '\0')
  680                                 return (EINVAL);
  681                         if (value < 1 || value > MAC_BIBA_MAX_COMPARTMENTS)
  682                                 return (EINVAL);
  683                         MAC_BIBA_BIT_SET(value, element->mbe_compartments);
  684                 }
  685         }
  686 
  687         return (0);
  688 }
  689 
  690 /*
  691  * Note: destructively consumes the string, make a local copy before calling
  692  * if that's a problem.
  693  */
  694 static int
  695 biba_parse(struct mac_biba *mb, char *string)
  696 {
  697         char *rangehigh, *rangelow, *effective;
  698         int error;
  699 
  700         effective = strsep(&string, "(");
  701         if (*effective == '\0')
  702                 effective = NULL;
  703 
  704         if (string != NULL) {
  705                 rangelow = strsep(&string, "-");
  706                 if (string == NULL)
  707                         return (EINVAL);
  708                 rangehigh = strsep(&string, ")");
  709                 if (string == NULL)
  710                         return (EINVAL);
  711                 if (*string != '\0')
  712                         return (EINVAL);
  713         } else {
  714                 rangelow = NULL;
  715                 rangehigh = NULL;
  716         }
  717 
  718         KASSERT((rangelow != NULL && rangehigh != NULL) ||
  719             (rangelow == NULL && rangehigh == NULL),
  720             ("biba_parse: range mismatch"));
  721 
  722         bzero(mb, sizeof(*mb));
  723         if (effective != NULL) {
  724                 error = biba_parse_element(&mb->mb_effective, effective);
  725                 if (error)
  726                         return (error);
  727                 mb->mb_flags |= MAC_BIBA_FLAG_EFFECTIVE;
  728         }
  729 
  730         if (rangelow != NULL) {
  731                 error = biba_parse_element(&mb->mb_rangelow, rangelow);
  732                 if (error)
  733                         return (error);
  734                 error = biba_parse_element(&mb->mb_rangehigh, rangehigh);
  735                 if (error)
  736                         return (error);
  737                 mb->mb_flags |= MAC_BIBA_FLAG_RANGE;
  738         }
  739 
  740         error = biba_valid(mb);
  741         if (error)
  742                 return (error);
  743 
  744         return (0);
  745 }
  746 
  747 static int
  748 biba_internalize_label(struct label *label, char *element_name,
  749     char *element_data, int *claimed)
  750 {
  751         struct mac_biba *mb, mb_temp;
  752         int error;
  753 
  754         if (strcmp(MAC_BIBA_LABEL_NAME, element_name) != 0)
  755                 return (0);
  756 
  757         (*claimed)++;
  758 
  759         error = biba_parse(&mb_temp, element_data);
  760         if (error)
  761                 return (error);
  762 
  763         mb = SLOT(label);
  764         *mb = mb_temp;
  765 
  766         return (0);
  767 }
  768 
  769 static void
  770 biba_copy_label(struct label *src, struct label *dest)
  771 {
  772 
  773         *SLOT(dest) = *SLOT(src);
  774 }
  775 
  776 /*
  777  * Object-specific entry point implementations are sorted alphabetically by
  778  * object type name and then by operation.
  779  */
  780 static int
  781 biba_bpfdesc_check_receive(struct bpf_d *d, struct label *dlabel,
  782     struct ifnet *ifp, struct label *ifplabel)
  783 {
  784         struct mac_biba *a, *b;
  785 
  786         if (!biba_enabled)
  787                 return (0);
  788 
  789         a = SLOT(dlabel);
  790         b = SLOT(ifplabel);
  791 
  792         if (biba_equal_effective(a, b))
  793                 return (0);
  794         return (EACCES);
  795 }
  796 
  797 static void
  798 biba_bpfdesc_create(struct ucred *cred, struct bpf_d *d,
  799     struct label *dlabel)
  800 {
  801         struct mac_biba *source, *dest;
  802 
  803         source = SLOT(cred->cr_label);
  804         dest = SLOT(dlabel);
  805 
  806         biba_copy_effective(source, dest);
  807 }
  808 
  809 static void
  810 biba_bpfdesc_create_mbuf(struct bpf_d *d, struct label *dlabel,
  811     struct mbuf *m, struct label *mlabel)
  812 {
  813         struct mac_biba *source, *dest;
  814 
  815         source = SLOT(dlabel);
  816         dest = SLOT(mlabel);
  817 
  818         biba_copy_effective(source, dest);
  819 }
  820 
  821 static void
  822 biba_cred_associate_nfsd(struct ucred *cred)
  823 {
  824         struct mac_biba *label;
  825 
  826         label = SLOT(cred->cr_label);
  827         biba_set_effective(label, MAC_BIBA_TYPE_LOW, 0, NULL);
  828         biba_set_range(label, MAC_BIBA_TYPE_LOW, 0, NULL, MAC_BIBA_TYPE_HIGH,
  829             0, NULL);
  830 }
  831 
  832 static int
  833 biba_cred_check_relabel(struct ucred *cred, struct label *newlabel)
  834 {
  835         struct mac_biba *subj, *new;
  836         int error;
  837 
  838         subj = SLOT(cred->cr_label);
  839         new = SLOT(newlabel);
  840 
  841         /*
  842          * If there is a Biba label update for the credential, it may
  843          * be an update of the effective, range, or both.
  844          */
  845         error = biba_atmostflags(new, MAC_BIBA_FLAGS_BOTH);
  846         if (error)
  847                 return (error);
  848 
  849         /*
  850          * If the Biba label is to be changed, authorize as appropriate.
  851          */
  852         if (new->mb_flags & MAC_BIBA_FLAGS_BOTH) {
  853                 /*
  854                  * If the change request modifies both the Biba label
  855                  * effective and range, check that the new effective will be
  856                  * in the new range.
  857                  */
  858                 if ((new->mb_flags & MAC_BIBA_FLAGS_BOTH) ==
  859                     MAC_BIBA_FLAGS_BOTH &&
  860                     !biba_effective_in_range(new, new))
  861                         return (EINVAL);
  862 
  863                 /*
  864                  * To change the Biba effective label on a credential, the
  865                  * new effective label must be in the current range.
  866                  */
  867                 if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE &&
  868                     !biba_effective_in_range(new, subj))
  869                         return (EPERM);
  870 
  871                 /*
  872                  * To change the Biba range on a credential, the new range
  873                  * label must be in the current range.
  874                  */
  875                 if (new->mb_flags & MAC_BIBA_FLAG_RANGE &&
  876                     !biba_range_in_range(new, subj))
  877                         return (EPERM);
  878 
  879                 /*
  880                  * To have EQUAL in any component of the new credential Biba
  881                  * label, the subject must already have EQUAL in their label.
  882                  */
  883                 if (biba_contains_equal(new)) {
  884                         error = biba_subject_privileged(subj);
  885                         if (error)
  886                                 return (error);
  887                 }
  888         }
  889 
  890         return (0);
  891 }
  892 
  893 static int
  894 biba_cred_check_visible(struct ucred *u1, struct ucred *u2)
  895 {
  896         struct mac_biba *subj, *obj;
  897 
  898         if (!biba_enabled)
  899                 return (0);
  900 
  901         subj = SLOT(u1->cr_label);
  902         obj = SLOT(u2->cr_label);
  903 
  904         /* XXX: range */
  905         if (!biba_dominate_effective(obj, subj))
  906                 return (ESRCH);
  907 
  908         return (0);
  909 }
  910 
  911 static void
  912 biba_cred_create_init(struct ucred *cred)
  913 {
  914         struct mac_biba *dest;
  915 
  916         dest = SLOT(cred->cr_label);
  917 
  918         biba_set_effective(dest, MAC_BIBA_TYPE_HIGH, 0, NULL);
  919         biba_set_range(dest, MAC_BIBA_TYPE_LOW, 0, NULL, MAC_BIBA_TYPE_HIGH,
  920             0, NULL);
  921 }
  922 
  923 static void
  924 biba_cred_create_swapper(struct ucred *cred)
  925 {
  926         struct mac_biba *dest;
  927 
  928         dest = SLOT(cred->cr_label);
  929 
  930         biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
  931         biba_set_range(dest, MAC_BIBA_TYPE_LOW, 0, NULL, MAC_BIBA_TYPE_HIGH,
  932             0, NULL);
  933 }
  934 
  935 static void
  936 biba_cred_relabel(struct ucred *cred, struct label *newlabel)
  937 {
  938         struct mac_biba *source, *dest;
  939 
  940         source = SLOT(newlabel);
  941         dest = SLOT(cred->cr_label);
  942 
  943         biba_copy(source, dest);
  944 }
  945 
  946 static void
  947 biba_devfs_create_device(struct ucred *cred, struct mount *mp,
  948     struct cdev *dev, struct devfs_dirent *de, struct label *delabel)
  949 {
  950         struct mac_biba *mb;
  951         const char *dn;
  952         int biba_type;
  953 
  954         mb = SLOT(delabel);
  955         dn = devtoname(dev);
  956         if (strcmp(dn, "null") == 0 ||
  957             strcmp(dn, "zero") == 0 ||
  958             strcmp(dn, "random") == 0 ||
  959             strncmp(dn, "fd/", strlen("fd/")) == 0)
  960                 biba_type = MAC_BIBA_TYPE_EQUAL;
  961         else if (ptys_equal &&
  962             (strncmp(dn, "ttyp", strlen("ttyp")) == 0 ||
  963             strncmp(dn, "pts/", strlen("pts/")) == 0 ||
  964             strncmp(dn, "ptyp", strlen("ptyp")) == 0))
  965                 biba_type = MAC_BIBA_TYPE_EQUAL;
  966         else
  967                 biba_type = MAC_BIBA_TYPE_HIGH;
  968         biba_set_effective(mb, biba_type, 0, NULL);
  969 }
  970 
  971 static void
  972 biba_devfs_create_directory(struct mount *mp, char *dirname, int dirnamelen,
  973     struct devfs_dirent *de, struct label *delabel)
  974 {
  975         struct mac_biba *mb;
  976 
  977         mb = SLOT(delabel);
  978 
  979         biba_set_effective(mb, MAC_BIBA_TYPE_HIGH, 0, NULL);
  980 }
  981 
  982 static void
  983 biba_devfs_create_symlink(struct ucred *cred, struct mount *mp,
  984     struct devfs_dirent *dd, struct label *ddlabel, struct devfs_dirent *de,
  985     struct label *delabel)
  986 {
  987         struct mac_biba *source, *dest;
  988 
  989         source = SLOT(cred->cr_label);
  990         dest = SLOT(delabel);
  991 
  992         biba_copy_effective(source, dest);
  993 }
  994 
  995 static void
  996 biba_devfs_update(struct mount *mp, struct devfs_dirent *de,
  997     struct label *delabel, struct vnode *vp, struct label *vplabel)
  998 {
  999         struct mac_biba *source, *dest;
 1000 
 1001         source = SLOT(vplabel);
 1002         dest = SLOT(delabel);
 1003 
 1004         biba_copy(source, dest);
 1005 }
 1006 
 1007 static void
 1008 biba_devfs_vnode_associate(struct mount *mp, struct label *mntlabel,
 1009     struct devfs_dirent *de, struct label *delabel, struct vnode *vp,
 1010     struct label *vplabel)
 1011 {
 1012         struct mac_biba *source, *dest;
 1013 
 1014         source = SLOT(delabel);
 1015         dest = SLOT(vplabel);
 1016 
 1017         biba_copy_effective(source, dest);
 1018 }
 1019 
 1020 static int
 1021 biba_ifnet_check_relabel(struct ucred *cred, struct ifnet *ifp,
 1022     struct label *ifplabel, struct label *newlabel)
 1023 {
 1024         struct mac_biba *subj, *new;
 1025         int error;
 1026 
 1027         subj = SLOT(cred->cr_label);
 1028         new = SLOT(newlabel);
 1029 
 1030         /*
 1031          * If there is a Biba label update for the interface, it may be an
 1032          * update of the effective, range, or both.
 1033          */
 1034         error = biba_atmostflags(new, MAC_BIBA_FLAGS_BOTH);
 1035         if (error)
 1036                 return (error);
 1037 
 1038         /*
 1039          * Relabling network interfaces requires Biba privilege.
 1040          */
 1041         error = biba_subject_privileged(subj);
 1042         if (error)
 1043                 return (error);
 1044 
 1045         return (0);
 1046 }
 1047 
 1048 static int
 1049 biba_ifnet_check_transmit(struct ifnet *ifp, struct label *ifplabel,
 1050     struct mbuf *m, struct label *mlabel)
 1051 {
 1052         struct mac_biba *p, *i;
 1053 
 1054         if (!biba_enabled)
 1055                 return (0);
 1056 
 1057         p = SLOT(mlabel);
 1058         i = SLOT(ifplabel);
 1059 
 1060         return (biba_effective_in_range(p, i) ? 0 : EACCES);
 1061 }
 1062 
 1063 static void
 1064 biba_ifnet_create(struct ifnet *ifp, struct label *ifplabel)
 1065 {
 1066         char tifname[IFNAMSIZ], *p, *q;
 1067         char tiflist[sizeof(trusted_interfaces)];
 1068         struct mac_biba *dest;
 1069         int len, type;
 1070 
 1071         dest = SLOT(ifplabel);
 1072 
 1073         if (ifp->if_type == IFT_LOOP || interfaces_equal != 0) {
 1074                 type = MAC_BIBA_TYPE_EQUAL;
 1075                 goto set;
 1076         }
 1077 
 1078         if (trust_all_interfaces) {
 1079                 type = MAC_BIBA_TYPE_HIGH;
 1080                 goto set;
 1081         }
 1082 
 1083         type = MAC_BIBA_TYPE_LOW;
 1084 
 1085         if (trusted_interfaces[0] == '\0' ||
 1086             !strvalid(trusted_interfaces, sizeof(trusted_interfaces)))
 1087                 goto set;
 1088 
 1089         bzero(tiflist, sizeof(tiflist));
 1090         for (p = trusted_interfaces, q = tiflist; *p != '\0'; p++, q++)
 1091                 if(*p != ' ' && *p != '\t')
 1092                         *q = *p;
 1093 
 1094         for (p = q = tiflist;; p++) {
 1095                 if (*p == ',' || *p == '\0') {
 1096                         len = p - q;
 1097                         if (len < IFNAMSIZ) {
 1098                                 bzero(tifname, sizeof(tifname));
 1099                                 bcopy(q, tifname, len);
 1100                                 if (strcmp(tifname, ifp->if_xname) == 0) {
 1101                                         type = MAC_BIBA_TYPE_HIGH;
 1102                                         break;
 1103                                 }
 1104                         } else {
 1105                                 *p = '\0';
 1106                                 printf("mac_biba warning: interface name "
 1107                                     "\"%s\" is too long (must be < %d)\n",
 1108                                     q, IFNAMSIZ);
 1109                         }
 1110                         if (*p == '\0')
 1111                                 break;
 1112                         q = p + 1;
 1113                 }
 1114         }
 1115 set:
 1116         biba_set_effective(dest, type, 0, NULL);
 1117         biba_set_range(dest, type, 0, NULL, type, 0, NULL);
 1118 }
 1119 
 1120 static void
 1121 biba_ifnet_create_mbuf(struct ifnet *ifp, struct label *ifplabel,
 1122     struct mbuf *m, struct label *mlabel)
 1123 {
 1124         struct mac_biba *source, *dest;
 1125 
 1126         source = SLOT(ifplabel);
 1127         dest = SLOT(mlabel);
 1128 
 1129         biba_copy_effective(source, dest);
 1130 }
 1131 
 1132 static void
 1133 biba_ifnet_relabel(struct ucred *cred, struct ifnet *ifp,
 1134     struct label *ifplabel, struct label *newlabel)
 1135 {
 1136         struct mac_biba *source, *dest;
 1137 
 1138         source = SLOT(newlabel);
 1139         dest = SLOT(ifplabel);
 1140 
 1141         biba_copy(source, dest);
 1142 }
 1143 
 1144 static int
 1145 biba_inpcb_check_deliver(struct inpcb *inp, struct label *inplabel,
 1146     struct mbuf *m, struct label *mlabel)
 1147 {
 1148         struct mac_biba *p, *i;
 1149 
 1150         if (!biba_enabled)
 1151                 return (0);
 1152 
 1153         p = SLOT(mlabel);
 1154         i = SLOT(inplabel);
 1155 
 1156         return (biba_equal_effective(p, i) ? 0 : EACCES);
 1157 }
 1158 
 1159 static int
 1160 biba_inpcb_check_visible(struct ucred *cred, struct inpcb *inp,
 1161     struct label *inplabel)
 1162 {
 1163         struct mac_biba *subj, *obj;
 1164 
 1165         if (!biba_enabled)
 1166                 return (0);
 1167 
 1168         subj = SLOT(cred->cr_label);
 1169         obj = SLOT(inplabel);
 1170 
 1171         if (!biba_dominate_effective(obj, subj))
 1172                 return (ENOENT);
 1173 
 1174         return (0);
 1175 }
 1176 
 1177 static void
 1178 biba_inpcb_create(struct socket *so, struct label *solabel,
 1179     struct inpcb *inp, struct label *inplabel)
 1180 {
 1181         struct mac_biba *source, *dest;
 1182 
 1183         source = SLOT(solabel);
 1184         dest = SLOT(inplabel);
 1185 
 1186         SOCK_LOCK(so);
 1187         biba_copy_effective(source, dest);
 1188         SOCK_UNLOCK(so);
 1189 }
 1190 
 1191 static void
 1192 biba_inpcb_create_mbuf(struct inpcb *inp, struct label *inplabel,
 1193     struct mbuf *m, struct label *mlabel)
 1194 {
 1195         struct mac_biba *source, *dest;
 1196 
 1197         source = SLOT(inplabel);
 1198         dest = SLOT(mlabel);
 1199 
 1200         biba_copy_effective(source, dest);
 1201 }
 1202 
 1203 static void
 1204 biba_inpcb_sosetlabel(struct socket *so, struct label *solabel,
 1205     struct inpcb *inp, struct label *inplabel)
 1206 {
 1207         struct mac_biba *source, *dest;
 1208 
 1209         SOCK_LOCK_ASSERT(so);
 1210 
 1211         source = SLOT(solabel);
 1212         dest = SLOT(inplabel);
 1213 
 1214         biba_copy(source, dest);
 1215 }
 1216 
 1217 static void
 1218 biba_ip6q_create(struct mbuf *m, struct label *mlabel, struct ip6q *q6,
 1219     struct label *q6label)
 1220 {
 1221         struct mac_biba *source, *dest;
 1222 
 1223         source = SLOT(mlabel);
 1224         dest = SLOT(q6label);
 1225 
 1226         biba_copy_effective(source, dest);
 1227 }
 1228 
 1229 static int
 1230 biba_ip6q_match(struct mbuf *m, struct label *mlabel, struct ip6q *q6,
 1231     struct label *q6label)
 1232 {
 1233         struct mac_biba *a, *b;
 1234 
 1235         a = SLOT(q6label);
 1236         b = SLOT(mlabel);
 1237 
 1238         return (biba_equal_effective(a, b));
 1239 }
 1240 
 1241 static void
 1242 biba_ip6q_reassemble(struct ip6q *q6, struct label *q6label, struct mbuf *m,
 1243     struct label *mlabel)
 1244 {
 1245         struct mac_biba *source, *dest;
 1246 
 1247         source = SLOT(q6label);
 1248         dest = SLOT(mlabel);
 1249 
 1250         /* Just use the head, since we require them all to match. */
 1251         biba_copy_effective(source, dest);
 1252 }
 1253 
 1254 static void
 1255 biba_ip6q_update(struct mbuf *m, struct label *mlabel, struct ip6q *q6,
 1256     struct label *q6label)
 1257 {
 1258 
 1259         /* NOOP: we only accept matching labels, so no need to update */
 1260 }
 1261 
 1262 static void
 1263 biba_ipq_create(struct mbuf *m, struct label *mlabel, struct ipq *q,
 1264     struct label *qlabel)
 1265 {
 1266         struct mac_biba *source, *dest;
 1267 
 1268         source = SLOT(mlabel);
 1269         dest = SLOT(qlabel);
 1270 
 1271         biba_copy_effective(source, dest);
 1272 }
 1273 
 1274 static int
 1275 biba_ipq_match(struct mbuf *m, struct label *mlabel, struct ipq *q,
 1276     struct label *qlabel)
 1277 {
 1278         struct mac_biba *a, *b;
 1279 
 1280         a = SLOT(qlabel);
 1281         b = SLOT(mlabel);
 1282 
 1283         return (biba_equal_effective(a, b));
 1284 }
 1285 
 1286 static void
 1287 biba_ipq_reassemble(struct ipq *q, struct label *qlabel, struct mbuf *m,
 1288     struct label *mlabel)
 1289 {
 1290         struct mac_biba *source, *dest;
 1291 
 1292         source = SLOT(qlabel);
 1293         dest = SLOT(mlabel);
 1294 
 1295         /* Just use the head, since we require them all to match. */
 1296         biba_copy_effective(source, dest);
 1297 }
 1298 
 1299 static void
 1300 biba_ipq_update(struct mbuf *m, struct label *mlabel, struct ipq *q,
 1301     struct label *qlabel)
 1302 {
 1303 
 1304         /* NOOP: we only accept matching labels, so no need to update */
 1305 }
 1306 
 1307 static int
 1308 biba_kld_check_load(struct ucred *cred, struct vnode *vp,
 1309     struct label *vplabel)
 1310 {
 1311         struct mac_biba *subj, *obj;
 1312         int error;
 1313 
 1314         if (!biba_enabled)
 1315                 return (0);
 1316 
 1317         subj = SLOT(cred->cr_label);
 1318 
 1319         error = biba_subject_privileged(subj);
 1320         if (error)
 1321                 return (error);
 1322 
 1323         obj = SLOT(vplabel);
 1324         if (!biba_high_effective(obj))
 1325                 return (EACCES);
 1326 
 1327         return (0);
 1328 }
 1329 
 1330 static int
 1331 biba_mount_check_stat(struct ucred *cred, struct mount *mp,
 1332     struct label *mplabel)
 1333 {
 1334         struct mac_biba *subj, *obj;
 1335 
 1336         if (!biba_enabled)
 1337                 return (0);
 1338 
 1339         subj = SLOT(cred->cr_label);
 1340         obj = SLOT(mplabel);
 1341 
 1342         if (!biba_dominate_effective(obj, subj))
 1343                 return (EACCES);
 1344 
 1345         return (0);
 1346 }
 1347 
 1348 static void
 1349 biba_mount_create(struct ucred *cred, struct mount *mp,
 1350     struct label *mplabel)
 1351 {
 1352         struct mac_biba *source, *dest;
 1353 
 1354         source = SLOT(cred->cr_label);
 1355         dest = SLOT(mplabel);
 1356 
 1357         biba_copy_effective(source, dest);
 1358 }
 1359 
 1360 static void
 1361 biba_netatalk_aarp_send(struct ifnet *ifp, struct label *ifplabel,
 1362     struct mbuf *m, struct label *mlabel)
 1363 {
 1364         struct mac_biba *dest;
 1365 
 1366         dest = SLOT(mlabel);
 1367 
 1368         biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
 1369 }
 1370 
 1371 static void
 1372 biba_netinet_arp_send(struct ifnet *ifp, struct label *ifplabel,
 1373     struct mbuf *m, struct label *mlabel)
 1374 {
 1375         struct mac_biba *dest;
 1376 
 1377         dest = SLOT(mlabel);
 1378 
 1379         biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
 1380 }
 1381 
 1382 static void
 1383 biba_netinet_firewall_reply(struct mbuf *mrecv, struct label *mrecvlabel,
 1384     struct mbuf *msend, struct label *msendlabel)
 1385 {
 1386         struct mac_biba *source, *dest;
 1387 
 1388         source = SLOT(mrecvlabel);
 1389         dest = SLOT(msendlabel);
 1390 
 1391         biba_copy_effective(source, dest);
 1392 }
 1393 
 1394 static void
 1395 biba_netinet_firewall_send(struct mbuf *m, struct label *mlabel)
 1396 {
 1397         struct mac_biba *dest;
 1398 
 1399         dest = SLOT(mlabel);
 1400 
 1401         /* XXX: where is the label for the firewall really coming from? */
 1402         biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
 1403 }
 1404 
 1405 static void
 1406 biba_netinet_fragment(struct mbuf *m, struct label *mlabel,
 1407     struct mbuf *frag, struct label *fraglabel)
 1408 {
 1409         struct mac_biba *source, *dest;
 1410 
 1411         source = SLOT(mlabel);
 1412         dest = SLOT(fraglabel);
 1413 
 1414         biba_copy_effective(source, dest);
 1415 }
 1416 
 1417 static void
 1418 biba_netinet_icmp_reply(struct mbuf *mrecv, struct label *mrecvlabel,
 1419     struct mbuf *msend, struct label *msendlabel)
 1420 {
 1421         struct mac_biba *source, *dest;
 1422 
 1423         source = SLOT(mrecvlabel);
 1424         dest = SLOT(msendlabel);
 1425 
 1426         biba_copy_effective(source, dest);
 1427 }
 1428 
 1429 static void
 1430 biba_netinet_igmp_send(struct ifnet *ifp, struct label *ifplabel,
 1431     struct mbuf *m, struct label *mlabel)
 1432 {
 1433         struct mac_biba *dest;
 1434 
 1435         dest = SLOT(mlabel);
 1436 
 1437         biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
 1438 }
 1439 
 1440 static void
 1441 biba_netinet6_nd6_send(struct ifnet *ifp, struct label *ifplabel,
 1442     struct mbuf *m, struct label *mlabel)
 1443 {
 1444         struct mac_biba *dest;
 1445 
 1446         dest = SLOT(mlabel);
 1447 
 1448         biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
 1449 }
 1450 
 1451 static int
 1452 biba_pipe_check_ioctl(struct ucred *cred, struct pipepair *pp,
 1453     struct label *pplabel, unsigned long cmd, void /* caddr_t */ *data)
 1454 {
 1455 
 1456         if(!biba_enabled)
 1457                 return (0);
 1458 
 1459         /* XXX: This will be implemented soon... */
 1460 
 1461         return (0);
 1462 }
 1463 
 1464 static int
 1465 biba_pipe_check_poll(struct ucred *cred, struct pipepair *pp,
 1466     struct label *pplabel)
 1467 {
 1468         struct mac_biba *subj, *obj;
 1469 
 1470         if (!biba_enabled)
 1471                 return (0);
 1472 
 1473         subj = SLOT(cred->cr_label);
 1474         obj = SLOT(pplabel);
 1475 
 1476         if (!biba_dominate_effective(obj, subj))
 1477                 return (EACCES);
 1478 
 1479         return (0);
 1480 }
 1481 
 1482 static int
 1483 biba_pipe_check_read(struct ucred *cred, struct pipepair *pp,
 1484     struct label *pplabel)
 1485 {
 1486         struct mac_biba *subj, *obj;
 1487 
 1488         if (!biba_enabled)
 1489                 return (0);
 1490 
 1491         subj = SLOT(cred->cr_label);
 1492         obj = SLOT(pplabel);
 1493 
 1494         if (!biba_dominate_effective(obj, subj))
 1495                 return (EACCES);
 1496 
 1497         return (0);
 1498 }
 1499 
 1500 static int
 1501 biba_pipe_check_relabel(struct ucred *cred, struct pipepair *pp,
 1502     struct label *pplabel, struct label *newlabel)
 1503 {
 1504         struct mac_biba *subj, *obj, *new;
 1505         int error;
 1506 
 1507         new = SLOT(newlabel);
 1508         subj = SLOT(cred->cr_label);
 1509         obj = SLOT(pplabel);
 1510 
 1511         /*
 1512          * If there is a Biba label update for a pipe, it must be a effective
 1513          * update.
 1514          */
 1515         error = biba_atmostflags(new, MAC_BIBA_FLAG_EFFECTIVE);
 1516         if (error)
 1517                 return (error);
 1518 
 1519         /*
 1520          * To perform a relabel of a pipe (Biba label or not), Biba must
 1521          * authorize the relabel.
 1522          */
 1523         if (!biba_effective_in_range(obj, subj))
 1524                 return (EPERM);
 1525 
 1526         /*
 1527          * If the Biba label is to be changed, authorize as appropriate.
 1528          */
 1529         if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) {
 1530                 /*
 1531                  * To change the Biba label on a pipe, the new pipe label
 1532                  * must be in the subject range.
 1533                  */
 1534                 if (!biba_effective_in_range(new, subj))
 1535                         return (EPERM);
 1536 
 1537                 /*
 1538                  * To change the Biba label on a pipe to be EQUAL, the
 1539                  * subject must have appropriate privilege.
 1540                  */
 1541                 if (biba_contains_equal(new)) {
 1542                         error = biba_subject_privileged(subj);
 1543                         if (error)
 1544                                 return (error);
 1545                 }
 1546         }
 1547 
 1548         return (0);
 1549 }
 1550 
 1551 static int
 1552 biba_pipe_check_stat(struct ucred *cred, struct pipepair *pp,
 1553     struct label *pplabel)
 1554 {
 1555         struct mac_biba *subj, *obj;
 1556 
 1557         if (!biba_enabled)
 1558                 return (0);
 1559 
 1560         subj = SLOT(cred->cr_label);
 1561         obj = SLOT(pplabel);
 1562 
 1563         if (!biba_dominate_effective(obj, subj))
 1564                 return (EACCES);
 1565 
 1566         return (0);
 1567 }
 1568 
 1569 static int
 1570 biba_pipe_check_write(struct ucred *cred, struct pipepair *pp,
 1571     struct label *pplabel)
 1572 {
 1573         struct mac_biba *subj, *obj;
 1574 
 1575         if (!biba_enabled)
 1576                 return (0);
 1577 
 1578         subj = SLOT(cred->cr_label);
 1579         obj = SLOT(pplabel);
 1580 
 1581         if (!biba_dominate_effective(subj, obj))
 1582                 return (EACCES);
 1583 
 1584         return (0);
 1585 }
 1586 
 1587 static void
 1588 biba_pipe_create(struct ucred *cred, struct pipepair *pp,
 1589     struct label *pplabel)
 1590 {
 1591         struct mac_biba *source, *dest;
 1592 
 1593         source = SLOT(cred->cr_label);
 1594         dest = SLOT(pplabel);
 1595 
 1596         biba_copy_effective(source, dest);
 1597 }
 1598 
 1599 static void
 1600 biba_pipe_relabel(struct ucred *cred, struct pipepair *pp,
 1601     struct label *pplabel, struct label *newlabel)
 1602 {
 1603         struct mac_biba *source, *dest;
 1604 
 1605         source = SLOT(newlabel);
 1606         dest = SLOT(pplabel);
 1607 
 1608         biba_copy(source, dest);
 1609 }
 1610 
 1611 static int
 1612 biba_posixsem_check_openunlink(struct ucred *cred, struct ksem *ks,
 1613     struct label *kslabel)
 1614 {
 1615         struct mac_biba *subj, *obj;
 1616 
 1617         if (!biba_enabled)
 1618                 return (0);
 1619 
 1620         subj = SLOT(cred->cr_label);
 1621         obj = SLOT(kslabel);
 1622 
 1623         if (!biba_dominate_effective(subj, obj))
 1624                 return (EACCES);
 1625 
 1626         return (0);
 1627 }
 1628 
 1629 static int
 1630 biba_posixsem_check_setmode(struct ucred *cred, struct ksem *ks,
 1631     struct label *kslabel, mode_t mode)
 1632 {
 1633         struct mac_biba *subj, *obj;
 1634 
 1635         if (!biba_enabled)
 1636                 return (0);
 1637 
 1638         subj = SLOT(cred->cr_label);
 1639         obj = SLOT(kslabel);
 1640 
 1641         if (!biba_dominate_effective(subj, obj))
 1642                 return (EACCES);
 1643 
 1644         return (0);
 1645 }
 1646 
 1647 static int
 1648 biba_posixsem_check_setowner(struct ucred *cred, struct ksem *ks,
 1649     struct label *kslabel, uid_t uid, gid_t gid)
 1650 {
 1651         struct mac_biba *subj, *obj;
 1652 
 1653         if (!biba_enabled)
 1654                 return (0);
 1655 
 1656         subj = SLOT(cred->cr_label);
 1657         obj = SLOT(kslabel);
 1658 
 1659         if (!biba_dominate_effective(subj, obj))
 1660                 return (EACCES);
 1661 
 1662         return (0);
 1663 }
 1664 
 1665 static int
 1666 biba_posixsem_check_write(struct ucred *active_cred, struct ucred *file_cred,
 1667     struct ksem *ks, struct label *kslabel)
 1668 {
 1669         struct mac_biba *subj, *obj;
 1670 
 1671         if (!biba_enabled)
 1672                 return (0);
 1673 
 1674         subj = SLOT(active_cred->cr_label);
 1675         obj = SLOT(kslabel);
 1676 
 1677         if (!biba_dominate_effective(subj, obj))
 1678                 return (EACCES);
 1679 
 1680         return (0);
 1681 }
 1682 
 1683 static int
 1684 biba_posixsem_check_rdonly(struct ucred *active_cred, struct ucred *file_cred,
 1685     struct ksem *ks, struct label *kslabel)
 1686 {
 1687         struct mac_biba *subj, *obj;
 1688 
 1689         if (!biba_enabled)
 1690                 return (0);
 1691 
 1692         subj = SLOT(active_cred->cr_label);
 1693         obj = SLOT(kslabel);
 1694 
 1695         if (!biba_dominate_effective(obj, subj))
 1696                 return (EACCES);
 1697 
 1698         return (0);
 1699 }
 1700 
 1701 static void
 1702 biba_posixsem_create(struct ucred *cred, struct ksem *ks,
 1703     struct label *kslabel)
 1704 {
 1705         struct mac_biba *source, *dest;
 1706 
 1707         source = SLOT(cred->cr_label);
 1708         dest = SLOT(kslabel);
 1709 
 1710         biba_copy_effective(source, dest);
 1711 }
 1712 
 1713 static int
 1714 biba_posixshm_check_mmap(struct ucred *cred, struct shmfd *shmfd,
 1715     struct label *shmlabel, int prot, int flags)
 1716 {
 1717         struct mac_biba *subj, *obj;
 1718 
 1719         if (!biba_enabled || !revocation_enabled)
 1720                 return (0);
 1721 
 1722         subj = SLOT(cred->cr_label);
 1723         obj = SLOT(shmlabel);
 1724 
 1725         if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
 1726                 if (!biba_dominate_effective(obj, subj))
 1727                         return (EACCES);
 1728         }
 1729         if (((prot & VM_PROT_WRITE) != 0) && ((flags & MAP_SHARED) != 0)) {
 1730                 if (!biba_dominate_effective(subj, obj))
 1731                         return (EACCES);
 1732         }
 1733 
 1734         return (0);
 1735 }
 1736 
 1737 static int
 1738 biba_posixshm_check_open(struct ucred *cred, struct shmfd *shmfd,
 1739     struct label *shmlabel, accmode_t accmode)
 1740 {
 1741         struct mac_biba *subj, *obj;
 1742 
 1743         if (!biba_enabled)
 1744                 return (0);
 1745 
 1746         subj = SLOT(cred->cr_label);
 1747         obj = SLOT(shmlabel);
 1748 
 1749         if (accmode & (VREAD | VEXEC | VSTAT_PERMS)) {
 1750                 if (!biba_dominate_effective(obj, subj))
 1751                         return (EACCES);
 1752         }
 1753         if (accmode & VMODIFY_PERMS) {
 1754                 if (!biba_dominate_effective(subj, obj))
 1755                         return (EACCES);
 1756         }
 1757 
 1758         return (0);
 1759 }
 1760 
 1761 static int
 1762 biba_posixshm_check_read(struct ucred *active_cred, struct ucred *file_cred,
 1763     struct shmfd *vp, struct label *shmlabel)
 1764 {
 1765         struct mac_biba *subj, *obj;
 1766 
 1767         if (!biba_enabled || !revocation_enabled)
 1768                 return (0);
 1769 
 1770         subj = SLOT(active_cred->cr_label);
 1771         obj = SLOT(shmlabel);
 1772 
 1773         if (!biba_dominate_effective(obj, subj))
 1774                 return (EACCES);
 1775 
 1776         return (0);
 1777 }
 1778 
 1779 static int
 1780 biba_posixshm_check_setmode(struct ucred *cred, struct shmfd *shmfd,
 1781     struct label *shmlabel, mode_t mode)
 1782 {
 1783         struct mac_biba *subj, *obj;
 1784 
 1785         if (!biba_enabled)
 1786                 return (0);
 1787 
 1788         subj = SLOT(cred->cr_label);
 1789         obj = SLOT(shmlabel);
 1790 
 1791         if (!biba_dominate_effective(subj, obj))
 1792                 return (EACCES);
 1793 
 1794         return (0);
 1795 }
 1796 
 1797 static int
 1798 biba_posixshm_check_setowner(struct ucred *cred, struct shmfd *shmfd,
 1799     struct label *shmlabel, uid_t uid, gid_t gid)
 1800 {
 1801         struct mac_biba *subj, *obj;
 1802 
 1803         if (!biba_enabled)
 1804                 return (0);
 1805 
 1806         subj = SLOT(cred->cr_label);
 1807         obj = SLOT(shmlabel);
 1808 
 1809         if (!biba_dominate_effective(subj, obj))
 1810                 return (EACCES);
 1811 
 1812         return (0);
 1813 }
 1814 
 1815 static int
 1816 biba_posixshm_check_stat(struct ucred *active_cred, struct ucred *file_cred,
 1817     struct shmfd *shmfd, struct label *shmlabel)
 1818 {
 1819         struct mac_biba *subj, *obj;
 1820 
 1821         if (!biba_enabled)
 1822                 return (0);
 1823 
 1824         subj = SLOT(active_cred->cr_label);
 1825         obj = SLOT(shmlabel);
 1826 
 1827         if (!biba_dominate_effective(obj, subj))
 1828                 return (EACCES);
 1829 
 1830         return (0);
 1831 }
 1832 
 1833 static int
 1834 biba_posixshm_check_truncate(struct ucred *active_cred,
 1835     struct ucred *file_cred, struct shmfd *shmfd, struct label *shmlabel)
 1836 {
 1837         struct mac_biba *subj, *obj;
 1838 
 1839         if (!biba_enabled)
 1840                 return (0);
 1841 
 1842         subj = SLOT(active_cred->cr_label);
 1843         obj = SLOT(shmlabel);
 1844 
 1845         if (!biba_dominate_effective(subj, obj))
 1846                 return (EACCES);
 1847 
 1848         return (0);
 1849 }
 1850 
 1851 static int
 1852 biba_posixshm_check_unlink(struct ucred *cred, struct shmfd *shmfd,
 1853     struct label *shmlabel)
 1854 {
 1855         struct mac_biba *subj, *obj;
 1856 
 1857         if (!biba_enabled)
 1858                 return (0);
 1859 
 1860         subj = SLOT(cred->cr_label);
 1861         obj = SLOT(shmlabel);
 1862 
 1863         if (!biba_dominate_effective(subj, obj))
 1864                 return (EACCES);
 1865     
 1866         return (0);
 1867 }
 1868 
 1869 static int
 1870 biba_posixshm_check_write(struct ucred *active_cred, struct ucred *file_cred,
 1871     struct shmfd *vp, struct label *shmlabel)
 1872 {
 1873         struct mac_biba *subj, *obj;
 1874 
 1875         if (!biba_enabled || !revocation_enabled)
 1876                 return (0);
 1877 
 1878         subj = SLOT(active_cred->cr_label);
 1879         obj = SLOT(shmlabel);
 1880 
 1881         if (!biba_dominate_effective(obj, subj))
 1882                 return (EACCES);
 1883 
 1884         return (0);
 1885 }
 1886 
 1887 static void
 1888 biba_posixshm_create(struct ucred *cred, struct shmfd *shmfd,
 1889     struct label *shmlabel)
 1890 {
 1891         struct mac_biba *source, *dest;
 1892 
 1893         source = SLOT(cred->cr_label);
 1894         dest = SLOT(shmlabel);
 1895 
 1896         biba_copy_effective(source, dest);
 1897 }
 1898 
 1899 /*
 1900  * Some system privileges are allowed regardless of integrity grade; others
 1901  * are allowed only when running with privilege with respect to the Biba
 1902  * policy as they might otherwise allow bypassing of the integrity policy.
 1903  */
 1904 static int
 1905 biba_priv_check(struct ucred *cred, int priv)
 1906 {
 1907         struct mac_biba *subj;
 1908         int error;
 1909 
 1910         if (!biba_enabled)
 1911                 return (0);
 1912 
 1913         /*
 1914          * Exempt only specific privileges from the Biba integrity policy.
 1915          */
 1916         switch (priv) {
 1917         case PRIV_KTRACE:
 1918         case PRIV_MSGBUF:
 1919 
 1920         /*
 1921          * Allow processes to manipulate basic process audit properties, and
 1922          * to submit audit records.
 1923          */
 1924         case PRIV_AUDIT_GETAUDIT:
 1925         case PRIV_AUDIT_SETAUDIT:
 1926         case PRIV_AUDIT_SUBMIT:
 1927 
 1928         /*
 1929          * Allow processes to manipulate their regular UNIX credentials.
 1930          */
 1931         case PRIV_CRED_SETUID:
 1932         case PRIV_CRED_SETEUID:
 1933         case PRIV_CRED_SETGID:
 1934         case PRIV_CRED_SETEGID:
 1935         case PRIV_CRED_SETGROUPS:
 1936         case PRIV_CRED_SETREUID:
 1937         case PRIV_CRED_SETREGID:
 1938         case PRIV_CRED_SETRESUID:
 1939         case PRIV_CRED_SETRESGID:
 1940 
 1941         /*
 1942          * Allow processes to perform system monitoring.
 1943          */
 1944         case PRIV_SEEOTHERGIDS:
 1945         case PRIV_SEEOTHERUIDS:
 1946                 break;
 1947 
 1948         /*
 1949          * Allow access to general process debugging facilities.  We
 1950          * separately control debugging based on MAC label.
 1951          */
 1952         case PRIV_DEBUG_DIFFCRED:
 1953         case PRIV_DEBUG_SUGID:
 1954         case PRIV_DEBUG_UNPRIV:
 1955 
 1956         /*
 1957          * Allow manipulating jails.
 1958          */
 1959         case PRIV_JAIL_ATTACH:
 1960 
 1961         /*
 1962          * Allow privilege with respect to the Partition policy, but not the
 1963          * Privs policy.
 1964          */
 1965         case PRIV_MAC_PARTITION:
 1966 
 1967         /*
 1968          * Allow privilege with respect to process resource limits and login
 1969          * context.
 1970          */
 1971         case PRIV_PROC_LIMIT:
 1972         case PRIV_PROC_SETLOGIN:
 1973         case PRIV_PROC_SETRLIMIT:
 1974 
 1975         /*
 1976          * Allow System V and POSIX IPC privileges.
 1977          */
 1978         case PRIV_IPC_READ:
 1979         case PRIV_IPC_WRITE:
 1980         case PRIV_IPC_ADMIN:
 1981         case PRIV_IPC_MSGSIZE:
 1982         case PRIV_MQ_ADMIN:
 1983 
 1984         /*
 1985          * Allow certain scheduler manipulations -- possibly this should be
 1986          * controlled by more fine-grained policy, as potentially low
 1987          * integrity processes can deny CPU to higher integrity ones.
 1988          */
 1989         case PRIV_SCHED_DIFFCRED:
 1990         case PRIV_SCHED_SETPRIORITY:
 1991         case PRIV_SCHED_RTPRIO:
 1992         case PRIV_SCHED_SETPOLICY:
 1993         case PRIV_SCHED_SET:
 1994         case PRIV_SCHED_SETPARAM:
 1995 
 1996         /*
 1997          * More IPC privileges.
 1998          */
 1999         case PRIV_SEM_WRITE:
 2000 
 2001         /*
 2002          * Allow signaling privileges subject to integrity policy.
 2003          */
 2004         case PRIV_SIGNAL_DIFFCRED:
 2005         case PRIV_SIGNAL_SUGID:
 2006 
 2007         /*
 2008          * Allow access to only limited sysctls from lower integrity levels;
 2009          * piggy-back on the Jail definition.
 2010          */
 2011         case PRIV_SYSCTL_WRITEJAIL:
 2012 
 2013         /*
 2014          * Allow TTY-based privileges, subject to general device access using
 2015          * labels on TTY device nodes, but not console privilege.
 2016          */
 2017         case PRIV_TTY_DRAINWAIT:
 2018         case PRIV_TTY_DTRWAIT:
 2019         case PRIV_TTY_EXCLUSIVE:
 2020         case PRIV_TTY_STI:
 2021         case PRIV_TTY_SETA:
 2022 
 2023         /*
 2024          * Grant most VFS privileges, as almost all are in practice bounded
 2025          * by more specific checks using labels.
 2026          */
 2027         case PRIV_VFS_READ:
 2028         case PRIV_VFS_WRITE:
 2029         case PRIV_VFS_ADMIN:
 2030         case PRIV_VFS_EXEC:
 2031         case PRIV_VFS_LOOKUP:
 2032         case PRIV_VFS_CHFLAGS_DEV:
 2033         case PRIV_VFS_CHOWN:
 2034         case PRIV_VFS_CHROOT:
 2035         case PRIV_VFS_RETAINSUGID:
 2036         case PRIV_VFS_EXCEEDQUOTA:
 2037         case PRIV_VFS_FCHROOT:
 2038         case PRIV_VFS_FHOPEN:
 2039         case PRIV_VFS_FHSTATFS:
 2040         case PRIV_VFS_GENERATION:
 2041         case PRIV_VFS_GETFH:
 2042         case PRIV_VFS_GETQUOTA:
 2043         case PRIV_VFS_LINK:
 2044         case PRIV_VFS_MOUNT:
 2045         case PRIV_VFS_MOUNT_OWNER:
 2046         case PRIV_VFS_MOUNT_PERM:
 2047         case PRIV_VFS_MOUNT_SUIDDIR:
 2048         case PRIV_VFS_MOUNT_NONUSER:
 2049         case PRIV_VFS_SETGID:
 2050         case PRIV_VFS_STICKYFILE:
 2051         case PRIV_VFS_SYSFLAGS:
 2052         case PRIV_VFS_UNMOUNT:
 2053 
 2054         /*
 2055          * Allow VM privileges; it would be nice if these were subject to
 2056          * resource limits.
 2057          */
 2058         case PRIV_VM_MADV_PROTECT:
 2059         case PRIV_VM_MLOCK:
 2060         case PRIV_VM_MUNLOCK:
 2061         case PRIV_VM_SWAP_NOQUOTA:
 2062         case PRIV_VM_SWAP_NORLIMIT:
 2063 
 2064         /*
 2065          * Allow some but not all network privileges.  In general, dont allow
 2066          * reconfiguring the network stack, just normal use.
 2067          */
 2068         case PRIV_NETATALK_RESERVEDPORT:
 2069         case PRIV_NETINET_RESERVEDPORT:
 2070         case PRIV_NETINET_RAW:
 2071         case PRIV_NETINET_REUSEPORT:
 2072         case PRIV_NETIPX_RESERVEDPORT:
 2073         case PRIV_NETIPX_RAW:
 2074                 break;
 2075 
 2076         /*
 2077          * All remaining system privileges are allow only if the process
 2078          * holds privilege with respect to the Biba policy.
 2079          */
 2080         default:
 2081                 subj = SLOT(cred->cr_label);
 2082                 error = biba_subject_privileged(subj);
 2083                 if (error)
 2084                         return (error);
 2085         }
 2086         return (0);
 2087 }
 2088 
 2089 static int
 2090 biba_proc_check_debug(struct ucred *cred, struct proc *p)
 2091 {
 2092         struct mac_biba *subj, *obj;
 2093 
 2094         if (!biba_enabled)
 2095                 return (0);
 2096 
 2097         subj = SLOT(cred->cr_label);
 2098         obj = SLOT(p->p_ucred->cr_label);
 2099 
 2100         /* XXX: range checks */
 2101         if (!biba_dominate_effective(obj, subj))
 2102                 return (ESRCH);
 2103         if (!biba_dominate_effective(subj, obj))
 2104                 return (EACCES);
 2105 
 2106         return (0);
 2107 }
 2108 
 2109 static int
 2110 biba_proc_check_sched(struct ucred *cred, struct proc *p)
 2111 {
 2112         struct mac_biba *subj, *obj;
 2113 
 2114         if (!biba_enabled)
 2115                 return (0);
 2116 
 2117         subj = SLOT(cred->cr_label);
 2118         obj = SLOT(p->p_ucred->cr_label);
 2119 
 2120         /* XXX: range checks */
 2121         if (!biba_dominate_effective(obj, subj))
 2122                 return (ESRCH);
 2123         if (!biba_dominate_effective(subj, obj))
 2124                 return (EACCES);
 2125 
 2126         return (0);
 2127 }
 2128 
 2129 static int
 2130 biba_proc_check_signal(struct ucred *cred, struct proc *p, int signum)
 2131 {
 2132         struct mac_biba *subj, *obj;
 2133 
 2134         if (!biba_enabled)
 2135                 return (0);
 2136 
 2137         subj = SLOT(cred->cr_label);
 2138         obj = SLOT(p->p_ucred->cr_label);
 2139 
 2140         /* XXX: range checks */
 2141         if (!biba_dominate_effective(obj, subj))
 2142                 return (ESRCH);
 2143         if (!biba_dominate_effective(subj, obj))
 2144                 return (EACCES);
 2145 
 2146         return (0);
 2147 }
 2148 
 2149 static int
 2150 biba_socket_check_deliver(struct socket *so, struct label *solabel,
 2151     struct mbuf *m, struct label *mlabel)
 2152 {
 2153         struct mac_biba *p, *s;
 2154         int error;
 2155 
 2156         if (!biba_enabled)
 2157                 return (0);
 2158 
 2159         p = SLOT(mlabel);
 2160         s = SLOT(solabel);
 2161 
 2162         SOCK_LOCK(so);
 2163         error = biba_equal_effective(p, s) ? 0 : EACCES;
 2164         SOCK_UNLOCK(so);
 2165         return (error);
 2166 }
 2167 
 2168 static int
 2169 biba_socket_check_relabel(struct ucred *cred, struct socket *so,
 2170     struct label *solabel, struct label *newlabel)
 2171 {
 2172         struct mac_biba *subj, *obj, *new;
 2173         int error;
 2174 
 2175         SOCK_LOCK_ASSERT(so);
 2176 
 2177         new = SLOT(newlabel);
 2178         subj = SLOT(cred->cr_label);
 2179         obj = SLOT(solabel);
 2180 
 2181         /*
 2182          * If there is a Biba label update for the socket, it may be an
 2183          * update of effective.
 2184          */
 2185         error = biba_atmostflags(new, MAC_BIBA_FLAG_EFFECTIVE);
 2186         if (error)
 2187                 return (error);
 2188 
 2189         /*
 2190          * To relabel a socket, the old socket effective must be in the
 2191          * subject range.
 2192          */
 2193         if (!biba_effective_in_range(obj, subj))
 2194                 return (EPERM);
 2195 
 2196         /*
 2197          * If the Biba label is to be changed, authorize as appropriate.
 2198          */
 2199         if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) {
 2200                 /*
 2201                  * To relabel a socket, the new socket effective must be in
 2202                  * the subject range.
 2203                  */
 2204                 if (!biba_effective_in_range(new, subj))
 2205                         return (EPERM);
 2206 
 2207                 /*
 2208                  * To change the Biba label on the socket to contain EQUAL,
 2209                  * the subject must have appropriate privilege.
 2210                  */
 2211                 if (biba_contains_equal(new)) {
 2212                         error = biba_subject_privileged(subj);
 2213                         if (error)
 2214                                 return (error);
 2215                 }
 2216         }
 2217 
 2218         return (0);
 2219 }
 2220 
 2221 static int
 2222 biba_socket_check_visible(struct ucred *cred, struct socket *so,
 2223     struct label *solabel)
 2224 {
 2225         struct mac_biba *subj, *obj;
 2226 
 2227         if (!biba_enabled)
 2228                 return (0);
 2229 
 2230         subj = SLOT(cred->cr_label);
 2231         obj = SLOT(solabel);
 2232 
 2233         SOCK_LOCK(so);
 2234         if (!biba_dominate_effective(obj, subj)) {
 2235                 SOCK_UNLOCK(so);
 2236                 return (ENOENT);
 2237         }
 2238         SOCK_UNLOCK(so);
 2239 
 2240         return (0);
 2241 }
 2242 
 2243 static void
 2244 biba_socket_create(struct ucred *cred, struct socket *so,
 2245     struct label *solabel)
 2246 {
 2247         struct mac_biba *source, *dest;
 2248 
 2249         source = SLOT(cred->cr_label);
 2250         dest = SLOT(solabel);
 2251 
 2252         biba_copy_effective(source, dest);
 2253 }
 2254 
 2255 static void
 2256 biba_socket_create_mbuf(struct socket *so, struct label *solabel,
 2257     struct mbuf *m, struct label *mlabel)
 2258 {
 2259         struct mac_biba *source, *dest;
 2260 
 2261         source = SLOT(solabel);
 2262         dest = SLOT(mlabel);
 2263 
 2264         SOCK_LOCK(so);
 2265         biba_copy_effective(source, dest);
 2266         SOCK_UNLOCK(so);
 2267 }
 2268 
 2269 static void
 2270 biba_socket_newconn(struct socket *oldso, struct label *oldsolabel,
 2271     struct socket *newso, struct label *newsolabel)
 2272 {
 2273         struct mac_biba source, *dest;
 2274 
 2275         SOCK_LOCK(oldso);
 2276         source = *SLOT(oldsolabel);
 2277         SOCK_UNLOCK(oldso);
 2278 
 2279         dest = SLOT(newsolabel);
 2280 
 2281         SOCK_LOCK(newso);
 2282         biba_copy_effective(&source, dest);
 2283         SOCK_UNLOCK(newso);
 2284 }
 2285 
 2286 static void
 2287 biba_socket_relabel(struct ucred *cred, struct socket *so,
 2288     struct label *solabel, struct label *newlabel)
 2289 {
 2290         struct mac_biba *source, *dest;
 2291 
 2292         SOCK_LOCK_ASSERT(so);
 2293 
 2294         source = SLOT(newlabel);
 2295         dest = SLOT(solabel);
 2296 
 2297         biba_copy(source, dest);
 2298 }
 2299 
 2300 static void
 2301 biba_socketpeer_set_from_mbuf(struct mbuf *m, struct label *mlabel,
 2302     struct socket *so, struct label *sopeerlabel)
 2303 {
 2304         struct mac_biba *source, *dest;
 2305 
 2306         source = SLOT(mlabel);
 2307         dest = SLOT(sopeerlabel);
 2308 
 2309         SOCK_LOCK(so);
 2310         biba_copy_effective(source, dest);
 2311         SOCK_UNLOCK(so);
 2312 }
 2313 
 2314 static void
 2315 biba_socketpeer_set_from_socket(struct socket *oldso,
 2316     struct label *oldsolabel, struct socket *newso,
 2317     struct label *newsopeerlabel)
 2318 {
 2319         struct mac_biba source, *dest;
 2320 
 2321         SOCK_LOCK(oldso);
 2322         source = *SLOT(oldsolabel);
 2323         SOCK_UNLOCK(oldso);
 2324         dest = SLOT(newsopeerlabel);
 2325 
 2326         SOCK_LOCK(newso);
 2327         biba_copy_effective(&source, dest);
 2328         SOCK_UNLOCK(newso);
 2329 }
 2330 
 2331 static void
 2332 biba_syncache_create(struct label *label, struct inpcb *inp)
 2333 {
 2334         struct mac_biba *source, *dest;
 2335 
 2336         source = SLOT(inp->inp_label);
 2337         dest = SLOT(label);
 2338         biba_copy_effective(source, dest);
 2339 }
 2340 
 2341 static void
 2342 biba_syncache_create_mbuf(struct label *sc_label, struct mbuf *m,
 2343     struct label *mlabel)
 2344 {
 2345         struct mac_biba *source, *dest;
 2346 
 2347         source = SLOT(sc_label);
 2348         dest = SLOT(mlabel);
 2349         biba_copy_effective(source, dest);
 2350 }
 2351 
 2352 static int
 2353 biba_system_check_acct(struct ucred *cred, struct vnode *vp,
 2354     struct label *vplabel)
 2355 {
 2356         struct mac_biba *subj, *obj;
 2357         int error;
 2358 
 2359         if (!biba_enabled)
 2360                 return (0);
 2361 
 2362         subj = SLOT(cred->cr_label);
 2363 
 2364         error = biba_subject_privileged(subj);
 2365         if (error)
 2366                 return (error);
 2367 
 2368         if (vplabel == NULL)
 2369                 return (0);
 2370 
 2371         obj = SLOT(vplabel);
 2372         if (!biba_high_effective(obj))
 2373                 return (EACCES);
 2374 
 2375         return (0);
 2376 }
 2377 
 2378 static int
 2379 biba_system_check_auditctl(struct ucred *cred, struct vnode *vp,
 2380     struct label *vplabel)
 2381 {
 2382         struct mac_biba *subj, *obj;
 2383         int error;
 2384 
 2385         if (!biba_enabled)
 2386                 return (0);
 2387 
 2388         subj = SLOT(cred->cr_label);
 2389 
 2390         error = biba_subject_privileged(subj);
 2391         if (error)
 2392                 return (error);
 2393 
 2394         if (vplabel == NULL)
 2395                 return (0);
 2396 
 2397         obj = SLOT(vplabel);
 2398         if (!biba_high_effective(obj))
 2399                 return (EACCES);
 2400 
 2401         return (0);
 2402 }
 2403 
 2404 static int
 2405 biba_system_check_auditon(struct ucred *cred, int cmd)
 2406 {
 2407         struct mac_biba *subj;
 2408         int error;
 2409 
 2410         if (!biba_enabled)
 2411                 return (0);
 2412 
 2413         subj = SLOT(cred->cr_label);
 2414 
 2415         error = biba_subject_privileged(subj);
 2416         if (error)
 2417                 return (error);
 2418 
 2419         return (0);
 2420 }
 2421 
 2422 static int
 2423 biba_system_check_swapoff(struct ucred *cred, struct vnode *vp,
 2424     struct label *label)
 2425 {
 2426         struct mac_biba *subj;
 2427         int error;
 2428 
 2429         if (!biba_enabled)
 2430                 return (0);
 2431 
 2432         subj = SLOT(cred->cr_label);
 2433 
 2434         error = biba_subject_privileged(subj);
 2435         if (error)
 2436                 return (error);
 2437 
 2438         return (0);
 2439 }
 2440 
 2441 static int
 2442 biba_system_check_swapon(struct ucred *cred, struct vnode *vp,
 2443     struct label *vplabel)
 2444 {
 2445         struct mac_biba *subj, *obj;
 2446         int error;
 2447 
 2448         if (!biba_enabled)
 2449                 return (0);
 2450 
 2451         subj = SLOT(cred->cr_label);
 2452         obj = SLOT(vplabel);
 2453 
 2454         error = biba_subject_privileged(subj);
 2455         if (error)
 2456                 return (error);
 2457 
 2458         if (!biba_high_effective(obj))
 2459                 return (EACCES);
 2460 
 2461         return (0);
 2462 }
 2463 
 2464 static int
 2465 biba_system_check_sysctl(struct ucred *cred, struct sysctl_oid *oidp,
 2466     void *arg1, int arg2, struct sysctl_req *req)
 2467 {
 2468         struct mac_biba *subj;
 2469         int error;
 2470 
 2471         if (!biba_enabled)
 2472                 return (0);
 2473 
 2474         subj = SLOT(cred->cr_label);
 2475 
 2476         /*
 2477          * Treat sysctl variables without CTLFLAG_ANYBODY flag as biba/high,
 2478          * but also require privilege to change them.
 2479          */
 2480         if (req->newptr != NULL && (oidp->oid_kind & CTLFLAG_ANYBODY) == 0) {
 2481                 if (!biba_subject_dominate_high(subj))
 2482                         return (EACCES);
 2483 
 2484                 error = biba_subject_privileged(subj);
 2485                 if (error)
 2486                         return (error);
 2487         }
 2488 
 2489         return (0);
 2490 }
 2491 
 2492 static void
 2493 biba_sysvmsg_cleanup(struct label *msglabel)
 2494 {
 2495 
 2496         bzero(SLOT(msglabel), sizeof(struct mac_biba));
 2497 }
 2498 
 2499 static void
 2500 biba_sysvmsg_create(struct ucred *cred, struct msqid_kernel *msqkptr,
 2501     struct label *msqlabel, struct msg *msgptr, struct label *msglabel)
 2502 {
 2503         struct mac_biba *source, *dest;
 2504 
 2505         /* Ignore the msgq label */
 2506         source = SLOT(cred->cr_label);
 2507         dest = SLOT(msglabel);
 2508 
 2509         biba_copy_effective(source, dest);
 2510 }
 2511 
 2512 static int
 2513 biba_sysvmsq_check_msgrcv(struct ucred *cred, struct msg *msgptr,
 2514     struct label *msglabel)
 2515 {
 2516         struct mac_biba *subj, *obj;
 2517 
 2518         if (!biba_enabled)
 2519                 return (0);
 2520 
 2521         subj = SLOT(cred->cr_label);
 2522         obj = SLOT(msglabel);
 2523 
 2524         if (!biba_dominate_effective(obj, subj))
 2525                 return (EACCES);
 2526 
 2527         return (0);
 2528 }
 2529 
 2530 static int
 2531 biba_sysvmsq_check_msgrmid(struct ucred *cred, struct msg *msgptr,
 2532     struct label *msglabel)
 2533 {
 2534         struct mac_biba *subj, *obj;
 2535 
 2536         if (!biba_enabled)
 2537                 return (0);
 2538 
 2539         subj = SLOT(cred->cr_label);
 2540         obj = SLOT(msglabel);
 2541 
 2542         if (!biba_dominate_effective(subj, obj))
 2543                 return (EACCES);
 2544 
 2545         return (0);
 2546 }
 2547 
 2548 static int
 2549 biba_sysvmsq_check_msqget(struct ucred *cred, struct msqid_kernel *msqkptr,
 2550     struct label *msqklabel)
 2551 {
 2552         struct mac_biba *subj, *obj;
 2553 
 2554         if (!biba_enabled)
 2555                 return (0);
 2556 
 2557         subj = SLOT(cred->cr_label);
 2558         obj = SLOT(msqklabel);
 2559 
 2560         if (!biba_dominate_effective(obj, subj))
 2561                 return (EACCES);
 2562 
 2563         return (0);
 2564 }
 2565 
 2566 static int
 2567 biba_sysvmsq_check_msqsnd(struct ucred *cred, struct msqid_kernel *msqkptr,
 2568     struct label *msqklabel)
 2569 {
 2570         struct mac_biba *subj, *obj;
 2571 
 2572         if (!biba_enabled)
 2573                 return (0);
 2574 
 2575         subj = SLOT(cred->cr_label);
 2576         obj = SLOT(msqklabel);
 2577 
 2578         if (!biba_dominate_effective(subj, obj))
 2579                 return (EACCES);
 2580 
 2581         return (0);
 2582 }
 2583 
 2584 static int
 2585 biba_sysvmsq_check_msqrcv(struct ucred *cred, struct msqid_kernel *msqkptr,
 2586     struct label *msqklabel)
 2587 {
 2588         struct mac_biba *subj, *obj;
 2589 
 2590         if (!biba_enabled)
 2591                 return (0);
 2592 
 2593         subj = SLOT(cred->cr_label);
 2594         obj = SLOT(msqklabel);
 2595 
 2596         if (!biba_dominate_effective(obj, subj))
 2597                 return (EACCES);
 2598 
 2599         return (0);
 2600 }
 2601 
 2602 static int
 2603 biba_sysvmsq_check_msqctl(struct ucred *cred, struct msqid_kernel *msqkptr,
 2604     struct label *msqklabel, int cmd)
 2605 {
 2606         struct mac_biba *subj, *obj;
 2607 
 2608         if (!biba_enabled)
 2609                 return (0);
 2610 
 2611         subj = SLOT(cred->cr_label);
 2612         obj = SLOT(msqklabel);
 2613 
 2614         switch(cmd) {
 2615         case IPC_RMID:
 2616         case IPC_SET:
 2617                 if (!biba_dominate_effective(subj, obj))
 2618                         return (EACCES);
 2619                 break;
 2620 
 2621         case IPC_STAT:
 2622                 if (!biba_dominate_effective(obj, subj))
 2623                         return (EACCES);
 2624                 break;
 2625 
 2626         default:
 2627                 return (EACCES);
 2628         }
 2629 
 2630         return (0);
 2631 }
 2632 
 2633 static void
 2634 biba_sysvmsq_cleanup(struct label *msqlabel)
 2635 {
 2636 
 2637         bzero(SLOT(msqlabel), sizeof(struct mac_biba));
 2638 }
 2639 
 2640 static void
 2641 biba_sysvmsq_create(struct ucred *cred, struct msqid_kernel *msqkptr,
 2642     struct label *msqlabel)
 2643 {
 2644         struct mac_biba *source, *dest;
 2645 
 2646         source = SLOT(cred->cr_label);
 2647         dest = SLOT(msqlabel);
 2648 
 2649         biba_copy_effective(source, dest);
 2650 }
 2651 
 2652 static int
 2653 biba_sysvsem_check_semctl(struct ucred *cred, struct semid_kernel *semakptr,
 2654     struct label *semaklabel, int cmd)
 2655 {
 2656         struct mac_biba *subj, *obj;
 2657 
 2658         if (!biba_enabled)
 2659                 return (0);
 2660 
 2661         subj = SLOT(cred->cr_label);
 2662         obj = SLOT(semaklabel);
 2663 
 2664         switch(cmd) {
 2665         case IPC_RMID:
 2666         case IPC_SET:
 2667         case SETVAL:
 2668         case SETALL:
 2669                 if (!biba_dominate_effective(subj, obj))
 2670                         return (EACCES);
 2671                 break;
 2672 
 2673         case IPC_STAT:
 2674         case GETVAL:
 2675         case GETPID:
 2676         case GETNCNT:
 2677         case GETZCNT:
 2678         case GETALL:
 2679                 if (!biba_dominate_effective(obj, subj))
 2680                         return (EACCES);
 2681                 break;
 2682 
 2683         default:
 2684                 return (EACCES);
 2685         }
 2686 
 2687         return (0);
 2688 }
 2689 
 2690 static int
 2691 biba_sysvsem_check_semget(struct ucred *cred, struct semid_kernel *semakptr,
 2692     struct label *semaklabel)
 2693 {
 2694         struct mac_biba *subj, *obj;
 2695 
 2696         if (!biba_enabled)
 2697                 return (0);
 2698 
 2699         subj = SLOT(cred->cr_label);
 2700         obj = SLOT(semaklabel);
 2701 
 2702         if (!biba_dominate_effective(obj, subj))
 2703                 return (EACCES);
 2704 
 2705         return (0);
 2706 }
 2707 
 2708 static int
 2709 biba_sysvsem_check_semop(struct ucred *cred, struct semid_kernel *semakptr,
 2710     struct label *semaklabel, size_t accesstype)
 2711 {
 2712         struct mac_biba *subj, *obj;
 2713 
 2714         if (!biba_enabled)
 2715                 return (0);
 2716 
 2717         subj = SLOT(cred->cr_label);
 2718         obj = SLOT(semaklabel);
 2719 
 2720         if (accesstype & SEM_R)
 2721                 if (!biba_dominate_effective(obj, subj))
 2722                         return (EACCES);
 2723 
 2724         if (accesstype & SEM_A)
 2725                 if (!biba_dominate_effective(subj, obj))
 2726                         return (EACCES);
 2727 
 2728         return (0);
 2729 }
 2730 
 2731 static void
 2732 biba_sysvsem_cleanup(struct label *semalabel)
 2733 {
 2734 
 2735         bzero(SLOT(semalabel), sizeof(struct mac_biba));
 2736 }
 2737 
 2738 static void
 2739 biba_sysvsem_create(struct ucred *cred, struct semid_kernel *semakptr,
 2740     struct label *semalabel)
 2741 {
 2742         struct mac_biba *source, *dest;
 2743 
 2744         source = SLOT(cred->cr_label);
 2745         dest = SLOT(semalabel);
 2746 
 2747         biba_copy_effective(source, dest);
 2748 }
 2749 
 2750 static int
 2751 biba_sysvshm_check_shmat(struct ucred *cred, struct shmid_kernel *shmsegptr,
 2752     struct label *shmseglabel, int shmflg)
 2753 {
 2754         struct mac_biba *subj, *obj;
 2755 
 2756         if (!biba_enabled)
 2757                 return (0);
 2758 
 2759         subj = SLOT(cred->cr_label);
 2760         obj = SLOT(shmseglabel);
 2761 
 2762         if (!biba_dominate_effective(obj, subj))
 2763                 return (EACCES);
 2764         if ((shmflg & SHM_RDONLY) == 0) {
 2765                 if (!biba_dominate_effective(subj, obj))
 2766                         return (EACCES);
 2767         }
 2768         
 2769         return (0);
 2770 }
 2771 
 2772 static int
 2773 biba_sysvshm_check_shmctl(struct ucred *cred, struct shmid_kernel *shmsegptr,
 2774     struct label *shmseglabel, int cmd)
 2775 {
 2776         struct mac_biba *subj, *obj;
 2777 
 2778         if (!biba_enabled)
 2779                 return (0);
 2780 
 2781         subj = SLOT(cred->cr_label);
 2782         obj = SLOT(shmseglabel);
 2783 
 2784         switch(cmd) {
 2785         case IPC_RMID:
 2786         case IPC_SET:
 2787                 if (!biba_dominate_effective(subj, obj))
 2788                         return (EACCES);
 2789                 break;
 2790 
 2791         case IPC_STAT:
 2792         case SHM_STAT:
 2793                 if (!biba_dominate_effective(obj, subj))
 2794                         return (EACCES);
 2795                 break;
 2796 
 2797         default:
 2798                 return (EACCES);
 2799         }
 2800 
 2801         return (0);
 2802 }
 2803 
 2804 static int
 2805 biba_sysvshm_check_shmget(struct ucred *cred, struct shmid_kernel *shmsegptr,
 2806     struct label *shmseglabel, int shmflg)
 2807 {
 2808         struct mac_biba *subj, *obj;
 2809 
 2810         if (!biba_enabled)
 2811                 return (0);
 2812 
 2813         subj = SLOT(cred->cr_label);
 2814         obj = SLOT(shmseglabel);
 2815 
 2816         if (!biba_dominate_effective(obj, subj))
 2817                 return (EACCES);
 2818 
 2819         return (0);
 2820 }
 2821 
 2822 static void
 2823 biba_sysvshm_cleanup(struct label *shmlabel)
 2824 {
 2825 
 2826         bzero(SLOT(shmlabel), sizeof(struct mac_biba));
 2827 }
 2828 
 2829 static void
 2830 biba_sysvshm_create(struct ucred *cred, struct shmid_kernel *shmsegptr,
 2831     struct label *shmlabel)
 2832 {
 2833         struct mac_biba *source, *dest;
 2834 
 2835         source = SLOT(cred->cr_label);
 2836         dest = SLOT(shmlabel);
 2837 
 2838         biba_copy_effective(source, dest);
 2839 }
 2840 
 2841 static int
 2842 biba_vnode_associate_extattr(struct mount *mp, struct label *mplabel,
 2843     struct vnode *vp, struct label *vplabel)
 2844 {
 2845         struct mac_biba mb_temp, *source, *dest;
 2846         int buflen, error;
 2847 
 2848         source = SLOT(mplabel);
 2849         dest = SLOT(vplabel);
 2850 
 2851         buflen = sizeof(mb_temp);
 2852         bzero(&mb_temp, buflen);
 2853 
 2854         error = vn_extattr_get(vp, IO_NODELOCKED, MAC_BIBA_EXTATTR_NAMESPACE,
 2855             MAC_BIBA_EXTATTR_NAME, &buflen, (char *) &mb_temp, curthread);
 2856         if (error == ENOATTR || error == EOPNOTSUPP) {
 2857                 /* Fall back to the mntlabel. */
 2858                 biba_copy_effective(source, dest);
 2859                 return (0);
 2860         } else if (error)
 2861                 return (error);
 2862 
 2863         if (buflen != sizeof(mb_temp)) {
 2864                 printf("biba_vnode_associate_extattr: bad size %d\n",
 2865                     buflen);
 2866                 return (EPERM);
 2867         }
 2868         if (biba_valid(&mb_temp) != 0) {
 2869                 printf("biba_vnode_associate_extattr: invalid\n");
 2870                 return (EPERM);
 2871         }
 2872         if ((mb_temp.mb_flags & MAC_BIBA_FLAGS_BOTH) !=
 2873             MAC_BIBA_FLAG_EFFECTIVE) {
 2874                 printf("biba_vnode_associate_extattr: not effective\n");
 2875                 return (EPERM);
 2876         }
 2877 
 2878         biba_copy_effective(&mb_temp, dest);
 2879         return (0);
 2880 }
 2881 
 2882 static void
 2883 biba_vnode_associate_singlelabel(struct mount *mp, struct label *mplabel,
 2884     struct vnode *vp, struct label *vplabel)
 2885 {
 2886         struct mac_biba *source, *dest;
 2887 
 2888         source = SLOT(mplabel);
 2889         dest = SLOT(vplabel);
 2890 
 2891         biba_copy_effective(source, dest);
 2892 }
 2893 
 2894 static int
 2895 biba_vnode_check_chdir(struct ucred *cred, struct vnode *dvp,
 2896     struct label *dvplabel)
 2897 {
 2898         struct mac_biba *subj, *obj;
 2899 
 2900         if (!biba_enabled)
 2901                 return (0);
 2902 
 2903         subj = SLOT(cred->cr_label);
 2904         obj = SLOT(dvplabel);
 2905 
 2906         if (!biba_dominate_effective(obj, subj))
 2907                 return (EACCES);
 2908 
 2909         return (0);
 2910 }
 2911 
 2912 static int
 2913 biba_vnode_check_chroot(struct ucred *cred, struct vnode *dvp,
 2914     struct label *dvplabel)
 2915 {
 2916         struct mac_biba *subj, *obj;
 2917 
 2918         if (!biba_enabled)
 2919                 return (0);
 2920 
 2921         subj = SLOT(cred->cr_label);
 2922         obj = SLOT(dvplabel);
 2923 
 2924         if (!biba_dominate_effective(obj, subj))
 2925                 return (EACCES);
 2926 
 2927         return (0);
 2928 }
 2929 
 2930 static int
 2931 biba_vnode_check_create(struct ucred *cred, struct vnode *dvp,
 2932     struct label *dvplabel, struct componentname *cnp, struct vattr *vap)
 2933 {
 2934         struct mac_biba *subj, *obj;
 2935 
 2936         if (!biba_enabled)
 2937                 return (0);
 2938 
 2939         subj = SLOT(cred->cr_label);
 2940         obj = SLOT(dvplabel);
 2941 
 2942         if (!biba_dominate_effective(subj, obj))
 2943                 return (EACCES);
 2944 
 2945         return (0);
 2946 }
 2947 
 2948 static int
 2949 biba_vnode_check_deleteacl(struct ucred *cred, struct vnode *vp,
 2950     struct label *vplabel, acl_type_t type)
 2951 {
 2952         struct mac_biba *subj, *obj;
 2953 
 2954         if (!biba_enabled)
 2955                 return (0);
 2956 
 2957         subj = SLOT(cred->cr_label);
 2958         obj = SLOT(vplabel);
 2959 
 2960         if (!biba_dominate_effective(subj, obj))
 2961                 return (EACCES);
 2962 
 2963         return (0);
 2964 }
 2965 
 2966 static int
 2967 biba_vnode_check_deleteextattr(struct ucred *cred, struct vnode *vp,
 2968     struct label *vplabel, int attrnamespace, const char *name)
 2969 {
 2970         struct mac_biba *subj, *obj;
 2971 
 2972         if (!biba_enabled)
 2973                 return (0);
 2974 
 2975         subj = SLOT(cred->cr_label);
 2976         obj = SLOT(vplabel);
 2977 
 2978         if (!biba_dominate_effective(subj, obj))
 2979                 return (EACCES);
 2980 
 2981         return (0);
 2982 }
 2983 
 2984 static int
 2985 biba_vnode_check_exec(struct ucred *cred, struct vnode *vp,
 2986     struct label *vplabel, struct image_params *imgp,
 2987     struct label *execlabel)
 2988 {
 2989         struct mac_biba *subj, *obj, *exec;
 2990         int error;
 2991 
 2992         if (execlabel != NULL) {
 2993                 /*
 2994                  * We currently don't permit labels to be changed at
 2995                  * exec-time as part of Biba, so disallow non-NULL Biba label
 2996                  * elements in the execlabel.
 2997                  */
 2998                 exec = SLOT(execlabel);
 2999                 error = biba_atmostflags(exec, 0);
 3000                 if (error)
 3001                         return (error);
 3002         }
 3003 
 3004         if (!biba_enabled)
 3005                 return (0);
 3006 
 3007         subj = SLOT(cred->cr_label);
 3008         obj = SLOT(vplabel);
 3009 
 3010         if (!biba_dominate_effective(obj, subj))
 3011                 return (EACCES);
 3012 
 3013         return (0);
 3014 }
 3015 
 3016 static int
 3017 biba_vnode_check_getacl(struct ucred *cred, struct vnode *vp,
 3018     struct label *vplabel, acl_type_t type)
 3019 {
 3020         struct mac_biba *subj, *obj;
 3021 
 3022         if (!biba_enabled)
 3023                 return (0);
 3024 
 3025         subj = SLOT(cred->cr_label);
 3026         obj = SLOT(vplabel);
 3027 
 3028         if (!biba_dominate_effective(obj, subj))
 3029                 return (EACCES);
 3030 
 3031         return (0);
 3032 }
 3033 
 3034 static int
 3035 biba_vnode_check_getextattr(struct ucred *cred, struct vnode *vp,
 3036     struct label *vplabel, int attrnamespace, const char *name)
 3037 {
 3038         struct mac_biba *subj, *obj;
 3039 
 3040         if (!biba_enabled)
 3041                 return (0);
 3042 
 3043         subj = SLOT(cred->cr_label);
 3044         obj = SLOT(vplabel);
 3045 
 3046         if (!biba_dominate_effective(obj, subj))
 3047                 return (EACCES);
 3048 
 3049         return (0);
 3050 }
 3051 
 3052 static int
 3053 biba_vnode_check_link(struct ucred *cred, struct vnode *dvp,
 3054     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
 3055     struct componentname *cnp)
 3056 {
 3057         struct mac_biba *subj, *obj;
 3058 
 3059         if (!biba_enabled)
 3060                 return (0);
 3061 
 3062         subj = SLOT(cred->cr_label);
 3063         obj = SLOT(dvplabel);
 3064 
 3065         if (!biba_dominate_effective(subj, obj))
 3066                 return (EACCES);
 3067 
 3068         obj = SLOT(vplabel);
 3069 
 3070         if (!biba_dominate_effective(subj, obj))
 3071                 return (EACCES);
 3072 
 3073         return (0);
 3074 }
 3075 
 3076 static int
 3077 biba_vnode_check_listextattr(struct ucred *cred, struct vnode *vp,
 3078     struct label *vplabel, int attrnamespace)
 3079 {
 3080         struct mac_biba *subj, *obj;
 3081 
 3082         if (!biba_enabled)
 3083                 return (0);
 3084 
 3085         subj = SLOT(cred->cr_label);
 3086         obj = SLOT(vplabel);
 3087 
 3088         if (!biba_dominate_effective(obj, subj))
 3089                 return (EACCES);
 3090 
 3091         return (0);
 3092 }
 3093 
 3094 static int
 3095 biba_vnode_check_lookup(struct ucred *cred, struct vnode *dvp,
 3096     struct label *dvplabel, struct componentname *cnp)
 3097 {
 3098         struct mac_biba *subj, *obj;
 3099 
 3100         if (!biba_enabled)
 3101                 return (0);
 3102 
 3103         subj = SLOT(cred->cr_label);
 3104         obj = SLOT(dvplabel);
 3105 
 3106         if (!biba_dominate_effective(obj, subj))
 3107                 return (EACCES);
 3108 
 3109         return (0);
 3110 }
 3111 
 3112 static int
 3113 biba_vnode_check_mmap(struct ucred *cred, struct vnode *vp,
 3114     struct label *vplabel, int prot, int flags)
 3115 {
 3116         struct mac_biba *subj, *obj;
 3117 
 3118         /*
 3119          * Rely on the use of open()-time protections to handle
 3120          * non-revocation cases.
 3121          */
 3122         if (!biba_enabled || !revocation_enabled)
 3123                 return (0);
 3124 
 3125         subj = SLOT(cred->cr_label);
 3126         obj = SLOT(vplabel);
 3127 
 3128         if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
 3129                 if (!biba_dominate_effective(obj, subj))
 3130                         return (EACCES);
 3131         }
 3132         if (((prot & VM_PROT_WRITE) != 0) && ((flags & MAP_SHARED) != 0)) {
 3133                 if (!biba_dominate_effective(subj, obj))
 3134                         return (EACCES);
 3135         }
 3136 
 3137         return (0);
 3138 }
 3139 
 3140 static int
 3141 biba_vnode_check_open(struct ucred *cred, struct vnode *vp,
 3142     struct label *vplabel, accmode_t accmode)
 3143 {
 3144         struct mac_biba *subj, *obj;
 3145 
 3146         if (!biba_enabled)
 3147                 return (0);
 3148 
 3149         subj = SLOT(cred->cr_label);
 3150         obj = SLOT(vplabel);
 3151 
 3152         /* XXX privilege override for admin? */
 3153         if (accmode & (VREAD | VEXEC | VSTAT_PERMS)) {
 3154                 if (!biba_dominate_effective(obj, subj))
 3155                         return (EACCES);
 3156         }
 3157         if (accmode & VMODIFY_PERMS) {
 3158                 if (!biba_dominate_effective(subj, obj))
 3159                         return (EACCES);
 3160         }
 3161 
 3162         return (0);
 3163 }
 3164 
 3165 static int
 3166 biba_vnode_check_poll(struct ucred *active_cred, struct ucred *file_cred,
 3167     struct vnode *vp, struct label *vplabel)
 3168 {
 3169         struct mac_biba *subj, *obj;
 3170 
 3171         if (!biba_enabled || !revocation_enabled)
 3172                 return (0);
 3173 
 3174         subj = SLOT(active_cred->cr_label);
 3175         obj = SLOT(vplabel);
 3176 
 3177         if (!biba_dominate_effective(obj, subj))
 3178                 return (EACCES);
 3179 
 3180         return (0);
 3181 }
 3182 
 3183 static int
 3184 biba_vnode_check_read(struct ucred *active_cred, struct ucred *file_cred,
 3185     struct vnode *vp, struct label *vplabel)
 3186 {
 3187         struct mac_biba *subj, *obj;
 3188 
 3189         if (!biba_enabled || !revocation_enabled)
 3190                 return (0);
 3191 
 3192         subj = SLOT(active_cred->cr_label);
 3193         obj = SLOT(vplabel);
 3194 
 3195         if (!biba_dominate_effective(obj, subj))
 3196                 return (EACCES);
 3197 
 3198         return (0);
 3199 }
 3200 
 3201 static int
 3202 biba_vnode_check_readdir(struct ucred *cred, struct vnode *dvp,
 3203     struct label *dvplabel)
 3204 {
 3205         struct mac_biba *subj, *obj;
 3206 
 3207         if (!biba_enabled)
 3208                 return (0);
 3209 
 3210         subj = SLOT(cred->cr_label);
 3211         obj = SLOT(dvplabel);
 3212 
 3213         if (!biba_dominate_effective(obj, subj))
 3214                 return (EACCES);
 3215 
 3216         return (0);
 3217 }
 3218 
 3219 static int
 3220 biba_vnode_check_readlink(struct ucred *cred, struct vnode *vp,
 3221     struct label *vplabel)
 3222 {
 3223         struct mac_biba *subj, *obj;
 3224 
 3225         if (!biba_enabled)
 3226                 return (0);
 3227 
 3228         subj = SLOT(cred->cr_label);
 3229         obj = SLOT(vplabel);
 3230 
 3231         if (!biba_dominate_effective(obj, subj))
 3232                 return (EACCES);
 3233 
 3234         return (0);
 3235 }
 3236 
 3237 static int
 3238 biba_vnode_check_relabel(struct ucred *cred, struct vnode *vp,
 3239     struct label *vplabel, struct label *newlabel)
 3240 {
 3241         struct mac_biba *old, *new, *subj;
 3242         int error;
 3243 
 3244         old = SLOT(vplabel);
 3245         new = SLOT(newlabel);
 3246         subj = SLOT(cred->cr_label);
 3247 
 3248         /*
 3249          * If there is a Biba label update for the vnode, it must be a
 3250          * effective label.
 3251          */
 3252         error = biba_atmostflags(new, MAC_BIBA_FLAG_EFFECTIVE);
 3253         if (error)
 3254                 return (error);
 3255 
 3256         /*
 3257          * To perform a relabel of the vnode (Biba label or not), Biba must
 3258          * authorize the relabel.
 3259          */
 3260         if (!biba_effective_in_range(old, subj))
 3261                 return (EPERM);
 3262 
 3263         /*
 3264          * If the Biba label is to be changed, authorize as appropriate.
 3265          */
 3266         if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) {
 3267                 /*
 3268                  * To change the Biba label on a vnode, the new vnode label
 3269                  * must be in the subject range.
 3270                  */
 3271                 if (!biba_effective_in_range(new, subj))
 3272                         return (EPERM);
 3273 
 3274                 /*
 3275                  * To change the Biba label on the vnode to be EQUAL, the
 3276                  * subject must have appropriate privilege.
 3277                  */
 3278                 if (biba_contains_equal(new)) {
 3279                         error = biba_subject_privileged(subj);
 3280                         if (error)
 3281                                 return (error);
 3282                 }
 3283         }
 3284 
 3285         return (0);
 3286 }
 3287 
 3288 static int
 3289 biba_vnode_check_rename_from(struct ucred *cred, struct vnode *dvp,
 3290     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
 3291     struct componentname *cnp)
 3292 {
 3293         struct mac_biba *subj, *obj;
 3294 
 3295         if (!biba_enabled)
 3296                 return (0);
 3297 
 3298         subj = SLOT(cred->cr_label);
 3299         obj = SLOT(dvplabel);
 3300 
 3301         if (!biba_dominate_effective(subj, obj))
 3302                 return (EACCES);
 3303 
 3304         obj = SLOT(vplabel);
 3305 
 3306         if (!biba_dominate_effective(subj, obj))
 3307                 return (EACCES);
 3308 
 3309         return (0);
 3310 }
 3311 
 3312 static int
 3313 biba_vnode_check_rename_to(struct ucred *cred, struct vnode *dvp,
 3314     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
 3315     int samedir, struct componentname *cnp)
 3316 {
 3317         struct mac_biba *subj, *obj;
 3318 
 3319         if (!biba_enabled)
 3320                 return (0);
 3321 
 3322         subj = SLOT(cred->cr_label);
 3323         obj = SLOT(dvplabel);
 3324 
 3325         if (!biba_dominate_effective(subj, obj))
 3326                 return (EACCES);
 3327 
 3328         if (vp != NULL) {
 3329                 obj = SLOT(vplabel);
 3330 
 3331                 if (!biba_dominate_effective(subj, obj))
 3332                         return (EACCES);
 3333         }
 3334 
 3335         return (0);
 3336 }
 3337 
 3338 static int
 3339 biba_vnode_check_revoke(struct ucred *cred, struct vnode *vp,
 3340     struct label *vplabel)
 3341 {
 3342         struct mac_biba *subj, *obj;
 3343 
 3344         if (!biba_enabled)
 3345                 return (0);
 3346 
 3347         subj = SLOT(cred->cr_label);
 3348         obj = SLOT(vplabel);
 3349 
 3350         if (!biba_dominate_effective(subj, obj))
 3351                 return (EACCES);
 3352 
 3353         return (0);
 3354 }
 3355 
 3356 static int
 3357 biba_vnode_check_setacl(struct ucred *cred, struct vnode *vp,
 3358     struct label *vplabel, acl_type_t type, struct acl *acl)
 3359 {
 3360         struct mac_biba *subj, *obj;
 3361 
 3362         if (!biba_enabled)
 3363                 return (0);
 3364 
 3365         subj = SLOT(cred->cr_label);
 3366         obj = SLOT(vplabel);
 3367 
 3368         if (!biba_dominate_effective(subj, obj))
 3369                 return (EACCES);
 3370 
 3371         return (0);
 3372 }
 3373 
 3374 static int
 3375 biba_vnode_check_setextattr(struct ucred *cred, struct vnode *vp,
 3376     struct label *vplabel, int attrnamespace, const char *name)
 3377 {
 3378         struct mac_biba *subj, *obj;
 3379 
 3380         if (!biba_enabled)
 3381                 return (0);
 3382 
 3383         subj = SLOT(cred->cr_label);
 3384         obj = SLOT(vplabel);
 3385 
 3386         if (!biba_dominate_effective(subj, obj))
 3387                 return (EACCES);
 3388 
 3389         /* XXX: protect the MAC EA in a special way? */
 3390 
 3391         return (0);
 3392 }
 3393 
 3394 static int
 3395 biba_vnode_check_setflags(struct ucred *cred, struct vnode *vp,
 3396     struct label *vplabel, u_long flags)
 3397 {
 3398         struct mac_biba *subj, *obj;
 3399 
 3400         if (!biba_enabled)
 3401                 return (0);
 3402 
 3403         subj = SLOT(cred->cr_label);
 3404         obj = SLOT(vplabel);
 3405 
 3406         if (!biba_dominate_effective(subj, obj))
 3407                 return (EACCES);
 3408 
 3409         return (0);
 3410 }
 3411 
 3412 static int
 3413 biba_vnode_check_setmode(struct ucred *cred, struct vnode *vp,
 3414     struct label *vplabel, mode_t mode)
 3415 {
 3416         struct mac_biba *subj, *obj;
 3417 
 3418         if (!biba_enabled)
 3419                 return (0);
 3420 
 3421         subj = SLOT(cred->cr_label);
 3422         obj = SLOT(vplabel);
 3423 
 3424         if (!biba_dominate_effective(subj, obj))
 3425                 return (EACCES);
 3426 
 3427         return (0);
 3428 }
 3429 
 3430 static int
 3431 biba_vnode_check_setowner(struct ucred *cred, struct vnode *vp,
 3432     struct label *vplabel, uid_t uid, gid_t gid)
 3433 {
 3434         struct mac_biba *subj, *obj;
 3435 
 3436         if (!biba_enabled)
 3437                 return (0);
 3438 
 3439         subj = SLOT(cred->cr_label);
 3440         obj = SLOT(vplabel);
 3441 
 3442         if (!biba_dominate_effective(subj, obj))
 3443                 return (EACCES);
 3444 
 3445         return (0);
 3446 }
 3447 
 3448 static int
 3449 biba_vnode_check_setutimes(struct ucred *cred, struct vnode *vp,
 3450     struct label *vplabel, struct timespec atime, struct timespec mtime)
 3451 {
 3452         struct mac_biba *subj, *obj;
 3453 
 3454         if (!biba_enabled)
 3455                 return (0);
 3456 
 3457         subj = SLOT(cred->cr_label);
 3458         obj = SLOT(vplabel);
 3459 
 3460         if (!biba_dominate_effective(subj, obj))
 3461                 return (EACCES);
 3462 
 3463         return (0);
 3464 }
 3465 
 3466 static int
 3467 biba_vnode_check_stat(struct ucred *active_cred, struct ucred *file_cred,
 3468     struct vnode *vp, struct label *vplabel)
 3469 {
 3470         struct mac_biba *subj, *obj;
 3471 
 3472         if (!biba_enabled)
 3473                 return (0);
 3474 
 3475         subj = SLOT(active_cred->cr_label);
 3476         obj = SLOT(vplabel);
 3477 
 3478         if (!biba_dominate_effective(obj, subj))
 3479                 return (EACCES);
 3480 
 3481         return (0);
 3482 }
 3483 
 3484 static int
 3485 biba_vnode_check_unlink(struct ucred *cred, struct vnode *dvp,
 3486     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
 3487     struct componentname *cnp)
 3488 {
 3489         struct mac_biba *subj, *obj;
 3490 
 3491         if (!biba_enabled)
 3492                 return (0);
 3493 
 3494         subj = SLOT(cred->cr_label);
 3495         obj = SLOT(dvplabel);
 3496 
 3497         if (!biba_dominate_effective(subj, obj))
 3498                 return (EACCES);
 3499 
 3500         obj = SLOT(vplabel);
 3501 
 3502         if (!biba_dominate_effective(subj, obj))
 3503                 return (EACCES);
 3504 
 3505         return (0);
 3506 }
 3507 
 3508 static int
 3509 biba_vnode_check_write(struct ucred *active_cred,
 3510     struct ucred *file_cred, struct vnode *vp, struct label *vplabel)
 3511 {
 3512         struct mac_biba *subj, *obj;
 3513 
 3514         if (!biba_enabled || !revocation_enabled)
 3515                 return (0);
 3516 
 3517         subj = SLOT(active_cred->cr_label);
 3518         obj = SLOT(vplabel);
 3519 
 3520         if (!biba_dominate_effective(subj, obj))
 3521                 return (EACCES);
 3522 
 3523         return (0);
 3524 }
 3525 
 3526 static int
 3527 biba_vnode_create_extattr(struct ucred *cred, struct mount *mp,
 3528     struct label *mplabel, struct vnode *dvp, struct label *dvplabel,
 3529     struct vnode *vp, struct label *vplabel, struct componentname *cnp)
 3530 {
 3531         struct mac_biba *source, *dest, mb_temp;
 3532         size_t buflen;
 3533         int error;
 3534 
 3535         buflen = sizeof(mb_temp);
 3536         bzero(&mb_temp, buflen);
 3537 
 3538         source = SLOT(cred->cr_label);
 3539         dest = SLOT(vplabel);
 3540         biba_copy_effective(source, &mb_temp);
 3541 
 3542         error = vn_extattr_set(vp, IO_NODELOCKED, MAC_BIBA_EXTATTR_NAMESPACE,
 3543             MAC_BIBA_EXTATTR_NAME, buflen, (char *) &mb_temp, curthread);
 3544         if (error == 0)
 3545                 biba_copy_effective(source, dest);
 3546         return (error);
 3547 }
 3548 
 3549 static void
 3550 biba_vnode_relabel(struct ucred *cred, struct vnode *vp,
 3551     struct label *vplabel, struct label *newlabel)
 3552 {
 3553         struct mac_biba *source, *dest;
 3554 
 3555         source = SLOT(newlabel);
 3556         dest = SLOT(vplabel);
 3557 
 3558         biba_copy(source, dest);
 3559 }
 3560 
 3561 static int
 3562 biba_vnode_setlabel_extattr(struct ucred *cred, struct vnode *vp,
 3563     struct label *vplabel, struct label *intlabel)
 3564 {
 3565         struct mac_biba *source, mb_temp;
 3566         size_t buflen;
 3567         int error;
 3568 
 3569         buflen = sizeof(mb_temp);
 3570         bzero(&mb_temp, buflen);
 3571 
 3572         source = SLOT(intlabel);
 3573         if ((source->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) == 0)
 3574                 return (0);
 3575 
 3576         biba_copy_effective(source, &mb_temp);
 3577 
 3578         error = vn_extattr_set(vp, IO_NODELOCKED, MAC_BIBA_EXTATTR_NAMESPACE,
 3579             MAC_BIBA_EXTATTR_NAME, buflen, (char *) &mb_temp, curthread);
 3580         return (error);
 3581 }
 3582 
 3583 static struct mac_policy_ops mac_biba_ops =
 3584 {
 3585         .mpo_init = biba_init,
 3586 
 3587         .mpo_bpfdesc_check_receive = biba_bpfdesc_check_receive,
 3588         .mpo_bpfdesc_create = biba_bpfdesc_create,
 3589         .mpo_bpfdesc_create_mbuf = biba_bpfdesc_create_mbuf,
 3590         .mpo_bpfdesc_destroy_label = biba_destroy_label,
 3591         .mpo_bpfdesc_init_label = biba_init_label,
 3592 
 3593         .mpo_cred_associate_nfsd = biba_cred_associate_nfsd,
 3594         .mpo_cred_check_relabel = biba_cred_check_relabel,
 3595         .mpo_cred_check_visible = biba_cred_check_visible,
 3596         .mpo_cred_copy_label = biba_copy_label,
 3597         .mpo_cred_create_init = biba_cred_create_init,
 3598         .mpo_cred_create_swapper = biba_cred_create_swapper,
 3599         .mpo_cred_destroy_label = biba_destroy_label,
 3600         .mpo_cred_externalize_label = biba_externalize_label,
 3601         .mpo_cred_init_label = biba_init_label,
 3602         .mpo_cred_internalize_label = biba_internalize_label,
 3603         .mpo_cred_relabel = biba_cred_relabel,
 3604 
 3605         .mpo_devfs_create_device = biba_devfs_create_device,
 3606         .mpo_devfs_create_directory = biba_devfs_create_directory,
 3607         .mpo_devfs_create_symlink = biba_devfs_create_symlink,
 3608         .mpo_devfs_destroy_label = biba_destroy_label,
 3609         .mpo_devfs_init_label = biba_init_label,
 3610         .mpo_devfs_update = biba_devfs_update,
 3611         .mpo_devfs_vnode_associate = biba_devfs_vnode_associate,
 3612 
 3613         .mpo_ifnet_check_relabel = biba_ifnet_check_relabel,
 3614         .mpo_ifnet_check_transmit = biba_ifnet_check_transmit,
 3615         .mpo_ifnet_copy_label = biba_copy_label,
 3616         .mpo_ifnet_create = biba_ifnet_create,
 3617         .mpo_ifnet_create_mbuf = biba_ifnet_create_mbuf,
 3618         .mpo_ifnet_destroy_label = biba_destroy_label,
 3619         .mpo_ifnet_externalize_label = biba_externalize_label,
 3620         .mpo_ifnet_init_label = biba_init_label,
 3621         .mpo_ifnet_internalize_label = biba_internalize_label,
 3622         .mpo_ifnet_relabel = biba_ifnet_relabel,
 3623 
 3624         .mpo_inpcb_check_deliver = biba_inpcb_check_deliver,
 3625         .mpo_inpcb_check_visible = biba_inpcb_check_visible,
 3626         .mpo_inpcb_create = biba_inpcb_create,
 3627         .mpo_inpcb_create_mbuf = biba_inpcb_create_mbuf,
 3628         .mpo_inpcb_destroy_label = biba_destroy_label,
 3629         .mpo_inpcb_init_label = biba_init_label_waitcheck,
 3630         .mpo_inpcb_sosetlabel = biba_inpcb_sosetlabel,
 3631 
 3632         .mpo_ip6q_create = biba_ip6q_create,
 3633         .mpo_ip6q_destroy_label = biba_destroy_label,
 3634         .mpo_ip6q_init_label = biba_init_label_waitcheck,
 3635         .mpo_ip6q_match = biba_ip6q_match,
 3636         .mpo_ip6q_reassemble = biba_ip6q_reassemble,
 3637         .mpo_ip6q_update = biba_ip6q_update,
 3638 
 3639         .mpo_ipq_create = biba_ipq_create,
 3640         .mpo_ipq_destroy_label = biba_destroy_label,
 3641         .mpo_ipq_init_label = biba_init_label_waitcheck,
 3642         .mpo_ipq_match = biba_ipq_match,
 3643         .mpo_ipq_reassemble = biba_ipq_reassemble,
 3644         .mpo_ipq_update = biba_ipq_update,
 3645 
 3646         .mpo_kld_check_load = biba_kld_check_load,
 3647 
 3648         .mpo_mbuf_copy_label = biba_copy_label,
 3649         .mpo_mbuf_destroy_label = biba_destroy_label,
 3650         .mpo_mbuf_init_label = biba_init_label_waitcheck,
 3651 
 3652         .mpo_mount_check_stat = biba_mount_check_stat,
 3653         .mpo_mount_create = biba_mount_create,
 3654         .mpo_mount_destroy_label = biba_destroy_label,
 3655         .mpo_mount_init_label = biba_init_label,
 3656 
 3657         .mpo_netatalk_aarp_send = biba_netatalk_aarp_send,
 3658 
 3659         .mpo_netinet_arp_send = biba_netinet_arp_send,
 3660         .mpo_netinet_firewall_reply = biba_netinet_firewall_reply,
 3661         .mpo_netinet_firewall_send = biba_netinet_firewall_send,
 3662         .mpo_netinet_fragment = biba_netinet_fragment,
 3663         .mpo_netinet_icmp_reply = biba_netinet_icmp_reply,
 3664         .mpo_netinet_igmp_send = biba_netinet_igmp_send,
 3665 
 3666         .mpo_netinet6_nd6_send = biba_netinet6_nd6_send,
 3667 
 3668         .mpo_pipe_check_ioctl = biba_pipe_check_ioctl,
 3669         .mpo_pipe_check_poll = biba_pipe_check_poll,
 3670         .mpo_pipe_check_read = biba_pipe_check_read,
 3671         .mpo_pipe_check_relabel = biba_pipe_check_relabel,
 3672         .mpo_pipe_check_stat = biba_pipe_check_stat,
 3673         .mpo_pipe_check_write = biba_pipe_check_write,
 3674         .mpo_pipe_copy_label = biba_copy_label,
 3675         .mpo_pipe_create = biba_pipe_create,
 3676         .mpo_pipe_destroy_label = biba_destroy_label,
 3677         .mpo_pipe_externalize_label = biba_externalize_label,
 3678         .mpo_pipe_init_label = biba_init_label,
 3679         .mpo_pipe_internalize_label = biba_internalize_label,
 3680         .mpo_pipe_relabel = biba_pipe_relabel,
 3681 
 3682         .mpo_posixsem_check_getvalue = biba_posixsem_check_rdonly,
 3683         .mpo_posixsem_check_open = biba_posixsem_check_openunlink,
 3684         .mpo_posixsem_check_post = biba_posixsem_check_write,
 3685         .mpo_posixsem_check_setmode = biba_posixsem_check_setmode,
 3686         .mpo_posixsem_check_setowner = biba_posixsem_check_setowner,
 3687         .mpo_posixsem_check_stat = biba_posixsem_check_rdonly,
 3688         .mpo_posixsem_check_unlink = biba_posixsem_check_openunlink,
 3689         .mpo_posixsem_check_wait = biba_posixsem_check_write,
 3690         .mpo_posixsem_create = biba_posixsem_create,
 3691         .mpo_posixsem_destroy_label = biba_destroy_label,
 3692         .mpo_posixsem_init_label = biba_init_label,
 3693 
 3694         .mpo_posixshm_check_mmap = biba_posixshm_check_mmap,
 3695         .mpo_posixshm_check_open = biba_posixshm_check_open,
 3696         .mpo_posixshm_check_read = biba_posixshm_check_read,
 3697         .mpo_posixshm_check_setmode = biba_posixshm_check_setmode,
 3698         .mpo_posixshm_check_setowner = biba_posixshm_check_setowner,
 3699         .mpo_posixshm_check_stat = biba_posixshm_check_stat,
 3700         .mpo_posixshm_check_truncate = biba_posixshm_check_truncate,
 3701         .mpo_posixshm_check_unlink = biba_posixshm_check_unlink,
 3702         .mpo_posixshm_check_write = biba_posixshm_check_write,
 3703         .mpo_posixshm_create = biba_posixshm_create,
 3704         .mpo_posixshm_destroy_label = biba_destroy_label,
 3705         .mpo_posixshm_init_label = biba_init_label,
 3706 
 3707         .mpo_priv_check = biba_priv_check,
 3708 
 3709         .mpo_proc_check_debug = biba_proc_check_debug,
 3710         .mpo_proc_check_sched = biba_proc_check_sched,
 3711         .mpo_proc_check_signal = biba_proc_check_signal,
 3712 
 3713         .mpo_socket_check_deliver = biba_socket_check_deliver,
 3714         .mpo_socket_check_relabel = biba_socket_check_relabel,
 3715         .mpo_socket_check_visible = biba_socket_check_visible,
 3716         .mpo_socket_copy_label = biba_copy_label,
 3717         .mpo_socket_create = biba_socket_create,
 3718         .mpo_socket_create_mbuf = biba_socket_create_mbuf,
 3719         .mpo_socket_destroy_label = biba_destroy_label,
 3720         .mpo_socket_externalize_label = biba_externalize_label,
 3721         .mpo_socket_init_label = biba_init_label_waitcheck,
 3722         .mpo_socket_internalize_label = biba_internalize_label,
 3723         .mpo_socket_newconn = biba_socket_newconn,
 3724         .mpo_socket_relabel = biba_socket_relabel,
 3725 
 3726         .mpo_socketpeer_destroy_label = biba_destroy_label,
 3727         .mpo_socketpeer_externalize_label = biba_externalize_label,
 3728         .mpo_socketpeer_init_label = biba_init_label_waitcheck,
 3729         .mpo_socketpeer_set_from_mbuf = biba_socketpeer_set_from_mbuf,
 3730         .mpo_socketpeer_set_from_socket = biba_socketpeer_set_from_socket,
 3731 
 3732         .mpo_syncache_create = biba_syncache_create,
 3733         .mpo_syncache_create_mbuf = biba_syncache_create_mbuf,
 3734         .mpo_syncache_destroy_label = biba_destroy_label,
 3735         .mpo_syncache_init_label = biba_init_label_waitcheck,
 3736 
 3737         .mpo_system_check_acct = biba_system_check_acct,
 3738         .mpo_system_check_auditctl = biba_system_check_auditctl,
 3739         .mpo_system_check_auditon = biba_system_check_auditon,
 3740         .mpo_system_check_swapoff = biba_system_check_swapoff,
 3741         .mpo_system_check_swapon = biba_system_check_swapon,
 3742         .mpo_system_check_sysctl = biba_system_check_sysctl,
 3743 
 3744         .mpo_sysvmsg_cleanup = biba_sysvmsg_cleanup,
 3745         .mpo_sysvmsg_create = biba_sysvmsg_create,
 3746         .mpo_sysvmsg_destroy_label = biba_destroy_label,
 3747         .mpo_sysvmsg_init_label = biba_init_label,
 3748 
 3749         .mpo_sysvmsq_check_msgrcv = biba_sysvmsq_check_msgrcv,
 3750         .mpo_sysvmsq_check_msgrmid = biba_sysvmsq_check_msgrmid,
 3751         .mpo_sysvmsq_check_msqget = biba_sysvmsq_check_msqget,
 3752         .mpo_sysvmsq_check_msqsnd = biba_sysvmsq_check_msqsnd,
 3753         .mpo_sysvmsq_check_msqrcv = biba_sysvmsq_check_msqrcv,
 3754         .mpo_sysvmsq_check_msqctl = biba_sysvmsq_check_msqctl,
 3755         .mpo_sysvmsq_cleanup = biba_sysvmsq_cleanup,
 3756         .mpo_sysvmsq_create = biba_sysvmsq_create,
 3757         .mpo_sysvmsq_destroy_label = biba_destroy_label,
 3758         .mpo_sysvmsq_init_label = biba_init_label,
 3759 
 3760         .mpo_sysvsem_check_semctl = biba_sysvsem_check_semctl,
 3761         .mpo_sysvsem_check_semget = biba_sysvsem_check_semget,
 3762         .mpo_sysvsem_check_semop = biba_sysvsem_check_semop,
 3763         .mpo_sysvsem_cleanup = biba_sysvsem_cleanup,
 3764         .mpo_sysvsem_create = biba_sysvsem_create,
 3765         .mpo_sysvsem_destroy_label = biba_destroy_label,
 3766         .mpo_sysvsem_init_label = biba_init_label,
 3767 
 3768         .mpo_sysvshm_check_shmat = biba_sysvshm_check_shmat,
 3769         .mpo_sysvshm_check_shmctl = biba_sysvshm_check_shmctl,
 3770         .mpo_sysvshm_check_shmget = biba_sysvshm_check_shmget,
 3771         .mpo_sysvshm_cleanup = biba_sysvshm_cleanup,
 3772         .mpo_sysvshm_create = biba_sysvshm_create,
 3773         .mpo_sysvshm_destroy_label = biba_destroy_label,
 3774         .mpo_sysvshm_init_label = biba_init_label,
 3775 
 3776         .mpo_vnode_associate_extattr = biba_vnode_associate_extattr,
 3777         .mpo_vnode_associate_singlelabel = biba_vnode_associate_singlelabel,
 3778         .mpo_vnode_check_access = biba_vnode_check_open,
 3779         .mpo_vnode_check_chdir = biba_vnode_check_chdir,
 3780         .mpo_vnode_check_chroot = biba_vnode_check_chroot,
 3781         .mpo_vnode_check_create = biba_vnode_check_create,
 3782         .mpo_vnode_check_deleteacl = biba_vnode_check_deleteacl,
 3783         .mpo_vnode_check_deleteextattr = biba_vnode_check_deleteextattr,
 3784         .mpo_vnode_check_exec = biba_vnode_check_exec,
 3785         .mpo_vnode_check_getacl = biba_vnode_check_getacl,
 3786         .mpo_vnode_check_getextattr = biba_vnode_check_getextattr,
 3787         .mpo_vnode_check_link = biba_vnode_check_link,
 3788         .mpo_vnode_check_listextattr = biba_vnode_check_listextattr,
 3789         .mpo_vnode_check_lookup = biba_vnode_check_lookup,
 3790         .mpo_vnode_check_mmap = biba_vnode_check_mmap,
 3791         .mpo_vnode_check_open = biba_vnode_check_open,
 3792         .mpo_vnode_check_poll = biba_vnode_check_poll,
 3793         .mpo_vnode_check_read = biba_vnode_check_read,
 3794         .mpo_vnode_check_readdir = biba_vnode_check_readdir,
 3795         .mpo_vnode_check_readlink = biba_vnode_check_readlink,
 3796         .mpo_vnode_check_relabel = biba_vnode_check_relabel,
 3797         .mpo_vnode_check_rename_from = biba_vnode_check_rename_from,
 3798         .mpo_vnode_check_rename_to = biba_vnode_check_rename_to,
 3799         .mpo_vnode_check_revoke = biba_vnode_check_revoke,
 3800         .mpo_vnode_check_setacl = biba_vnode_check_setacl,
 3801         .mpo_vnode_check_setextattr = biba_vnode_check_setextattr,
 3802         .mpo_vnode_check_setflags = biba_vnode_check_setflags,
 3803         .mpo_vnode_check_setmode = biba_vnode_check_setmode,
 3804         .mpo_vnode_check_setowner = biba_vnode_check_setowner,
 3805         .mpo_vnode_check_setutimes = biba_vnode_check_setutimes,
 3806         .mpo_vnode_check_stat = biba_vnode_check_stat,
 3807         .mpo_vnode_check_unlink = biba_vnode_check_unlink,
 3808         .mpo_vnode_check_write = biba_vnode_check_write,
 3809         .mpo_vnode_create_extattr = biba_vnode_create_extattr,
 3810         .mpo_vnode_copy_label = biba_copy_label,
 3811         .mpo_vnode_destroy_label = biba_destroy_label,
 3812         .mpo_vnode_externalize_label = biba_externalize_label,
 3813         .mpo_vnode_init_label = biba_init_label,
 3814         .mpo_vnode_internalize_label = biba_internalize_label,
 3815         .mpo_vnode_relabel = biba_vnode_relabel,
 3816         .mpo_vnode_setlabel_extattr = biba_vnode_setlabel_extattr,
 3817 };
 3818 
 3819 MAC_POLICY_SET(&mac_biba_ops, mac_biba, "TrustedBSD MAC/Biba",
 3820     MPC_LOADTIME_FLAG_NOTLATE, &biba_slot);

Cache object: 67a35bd50cf72c2a637fb158a77b8b05


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.