netfilter: nf_conntrack: table max size should hold at least table size
[safe/jmp/linux-2.6] / security / selinux / ss / policydb.c
index 8e6262d..72e4a54 100644 (file)
  *
  * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
  *
- *     Added conditional policy language extensions
+ *     Added conditional policy language extensions
  *
+ * Updated: Hewlett-Packard <paul.moore@hp.com>
+ *
+ *      Added support for the policy capability bitmap
+ *
+ * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
  * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
  * Copyright (C) 2003 - 2004 Tresys Technology, LLC
  *     This program is free software; you can redistribute it and/or modify
- *     it under the terms of the GNU General Public License as published by
+ *     it under the terms of the GNU General Public License as published by
  *     the Free Software Foundation, version 2.
  */
 
 #include <linux/kernel.h>
+#include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/errno.h>
+#include <linux/audit.h>
 #include "security.h"
 
 #include "policydb.h"
@@ -45,7 +52,7 @@ static char *symtab_name[SYM_NUM] = {
 };
 #endif
 
-int selinux_mls_enabled = 0;
+int selinux_mls_enabled;
 
 static unsigned int symtab_sizes[SYM_NUM] = {
        2,
@@ -67,34 +74,54 @@ struct policydb_compat_info {
 /* These need to be updated if SYM_NUM or OCON_NUM changes */
 static struct policydb_compat_info policydb_compat[] = {
        {
-               .version        = POLICYDB_VERSION_BASE,
-               .sym_num        = SYM_NUM - 3,
-               .ocon_num       = OCON_NUM - 1,
+               .version        = POLICYDB_VERSION_BASE,
+               .sym_num        = SYM_NUM - 3,
+               .ocon_num       = OCON_NUM - 1,
+       },
+       {
+               .version        = POLICYDB_VERSION_BOOL,
+               .sym_num        = SYM_NUM - 2,
+               .ocon_num       = OCON_NUM - 1,
+       },
+       {
+               .version        = POLICYDB_VERSION_IPV6,
+               .sym_num        = SYM_NUM - 2,
+               .ocon_num       = OCON_NUM,
+       },
+       {
+               .version        = POLICYDB_VERSION_NLCLASS,
+               .sym_num        = SYM_NUM - 2,
+               .ocon_num       = OCON_NUM,
+       },
+       {
+               .version        = POLICYDB_VERSION_MLS,
+               .sym_num        = SYM_NUM,
+               .ocon_num       = OCON_NUM,
        },
        {
-               .version        = POLICYDB_VERSION_BOOL,
-               .sym_num        = SYM_NUM - 2,
-               .ocon_num       = OCON_NUM - 1,
+               .version        = POLICYDB_VERSION_AVTAB,
+               .sym_num        = SYM_NUM,
+               .ocon_num       = OCON_NUM,
        },
        {
-               .version        = POLICYDB_VERSION_IPV6,
-               .sym_num        = SYM_NUM - 2,
-               .ocon_num       = OCON_NUM,
+               .version        = POLICYDB_VERSION_RANGETRANS,
+               .sym_num        = SYM_NUM,
+               .ocon_num       = OCON_NUM,
        },
        {
-               .version        = POLICYDB_VERSION_NLCLASS,
-               .sym_num        = SYM_NUM - 2,
-               .ocon_num       = OCON_NUM,
+               .version        = POLICYDB_VERSION_POLCAP,
+               .sym_num        = SYM_NUM,
+               .ocon_num       = OCON_NUM,
        },
        {
-               .version        = POLICYDB_VERSION_MLS,
-               .sym_num        = SYM_NUM,
-               .ocon_num       = OCON_NUM,
+               .version        = POLICYDB_VERSION_PERMISSIVE,
+               .sym_num        = SYM_NUM,
+               .ocon_num       = OCON_NUM,
        },
        {
-               .version        = POLICYDB_VERSION_AVTAB,
-               .sym_num        = SYM_NUM,
-               .ocon_num       = OCON_NUM,
+               .version        = POLICYDB_VERSION_BOUNDARY,
+               .sym_num        = SYM_NUM,
+               .ocon_num       = OCON_NUM,
        },
 };
 
@@ -103,7 +130,7 @@ static struct policydb_compat_info *policydb_lookup_compat(int version)
        int i;
        struct policydb_compat_info *info = NULL;
 
-       for (i = 0; i < sizeof(policydb_compat)/sizeof(*info); i++) {
+       for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
                if (policydb_compat[i].version == version) {
                        info = &policydb_compat[i];
                        break;
@@ -121,18 +148,17 @@ static int roles_init(struct policydb *p)
        int rc;
        struct role_datum *role;
 
-       role = kmalloc(sizeof(*role), GFP_KERNEL);
+       role = kzalloc(sizeof(*role), GFP_KERNEL);
        if (!role) {
                rc = -ENOMEM;
                goto out;
        }
-       memset(role, 0, sizeof(*role));
        role->value = ++p->p_roles.nprim;
        if (role->value != OBJECT_R_VAL) {
                rc = -EINVAL;
                goto out_free_role;
        }
-       key = kmalloc(strlen(OBJECT_R)+1,GFP_KERNEL);
+       key = kmalloc(strlen(OBJECT_R)+1, GFP_KERNEL);
        if (!key) {
                rc = -ENOMEM;
                goto out_free_role;
@@ -172,18 +198,18 @@ static int policydb_init(struct policydb *p)
 
        rc = roles_init(p);
        if (rc)
-               goto out_free_avtab;
+               goto out_free_symtab;
 
        rc = cond_policydb_init(p);
        if (rc)
-               goto out_free_avtab;
+               goto out_free_symtab;
+
+       ebitmap_init(&p->policycaps);
+       ebitmap_init(&p->permissive_map);
 
 out:
        return rc;
 
-out_free_avtab:
-       avtab_destroy(&p->te_avtab);
-
 out_free_symtab:
        for (i = 0; i < SYM_NUM; i++)
                hashtab_destroy(p->symtab[i].table);
@@ -234,7 +260,9 @@ static int role_index(void *key, void *datum, void *datap)
 
        role = datum;
        p = datap;
-       if (!role->value || role->value > p->p_roles.nprim)
+       if (!role->value
+           || role->value > p->p_roles.nprim
+           || role->bounds > p->p_roles.nprim)
                return -EINVAL;
        p->p_role_val_to_name[role->value - 1] = key;
        p->role_val_to_struct[role->value - 1] = role;
@@ -250,9 +278,12 @@ static int type_index(void *key, void *datum, void *datap)
        p = datap;
 
        if (typdatum->primary) {
-               if (!typdatum->value || typdatum->value > p->p_types.nprim)
+               if (!typdatum->value
+                   || typdatum->value > p->p_types.nprim
+                   || typdatum->bounds > p->p_types.nprim)
                        return -EINVAL;
                p->p_type_val_to_name[typdatum->value - 1] = key;
+               p->type_val_to_struct[typdatum->value - 1] = typdatum;
        }
 
        return 0;
@@ -265,7 +296,9 @@ static int user_index(void *key, void *datum, void *datap)
 
        usrdatum = datum;
        p = datap;
-       if (!usrdatum->value || usrdatum->value > p->p_users.nprim)
+       if (!usrdatum->value
+           || usrdatum->value > p->p_users.nprim
+           || usrdatum->bounds > p->p_users.nprim)
                return -EINVAL;
        p->p_user_val_to_name[usrdatum->value - 1] = key;
        p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
@@ -370,7 +403,7 @@ static void symtab_hash_eval(struct symtab *s)
                struct hashtab_info info;
 
                hashtab_stat(h, &info);
-               printk(KERN_INFO "%s:  %d entries and %d/%d buckets used, "
+               printk(KERN_DEBUG "SELinux: %s:  %d entries and %d/%d buckets used, "
                       "longest chain length %d\n", symtab_name[i], h->nel,
                       info.slots_used, h->size, info.max_chain_len);
        }
@@ -387,14 +420,14 @@ static int policydb_index_others(struct policydb *p)
 {
        int i, rc = 0;
 
-       printk(KERN_INFO "security:  %d users, %d roles, %d types, %d bools",
+       printk(KERN_DEBUG "SELinux:  %d users, %d roles, %d types, %d bools",
               p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
        if (selinux_mls_enabled)
                printk(", %d sens, %d cats", p->p_levels.nprim,
                       p->p_cats.nprim);
        printk("\n");
 
-       printk(KERN_INFO "security:  %d classes, %d rules\n",
+       printk(KERN_DEBUG "SELinux:  %d classes, %d rules\n",
               p->p_classes.nprim, p->te_avtab.nel);
 
 #ifdef DEBUG_HASHES
@@ -404,7 +437,7 @@ static int policydb_index_others(struct policydb *p)
 
        p->role_val_to_struct =
                kmalloc(p->p_roles.nprim * sizeof(*(p->role_val_to_struct)),
-                       GFP_KERNEL);
+                       GFP_KERNEL);
        if (!p->role_val_to_struct) {
                rc = -ENOMEM;
                goto out;
@@ -412,12 +445,20 @@ static int policydb_index_others(struct policydb *p)
 
        p->user_val_to_struct =
                kmalloc(p->p_users.nprim * sizeof(*(p->user_val_to_struct)),
-                       GFP_KERNEL);
+                       GFP_KERNEL);
        if (!p->user_val_to_struct) {
                rc = -ENOMEM;
                goto out;
        }
 
+       p->type_val_to_struct =
+               kmalloc(p->p_types.nprim * sizeof(*(p->type_val_to_struct)),
+                       GFP_KERNEL);
+       if (!p->type_val_to_struct) {
+               rc = -ENOMEM;
+               goto out;
+       }
+
        if (cond_init_bool_indexes(p)) {
                rc = -ENOMEM;
                goto out;
@@ -464,7 +505,7 @@ static int common_destroy(void *key, void *datum, void *p)
        return 0;
 }
 
-static int class_destroy(void *key, void *datum, void *p)
+static int cls_destroy(void *key, void *datum, void *p)
 {
        struct class_datum *cladatum;
        struct constraint_node *constraint, *ctemp;
@@ -562,7 +603,7 @@ static int cat_destroy(void *key, void *datum, void *p)
 static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
 {
        common_destroy,
-       class_destroy,
+       cls_destroy,
        role_destroy,
        type_destroy,
        user_destroy,
@@ -594,6 +635,7 @@ void policydb_destroy(struct policydb *p)
        struct range_trans *rt, *lrt = NULL;
 
        for (i = 0; i < SYM_NUM; i++) {
+               cond_resched();
                hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
                hashtab_destroy(p->symtab[i].table);
        }
@@ -604,57 +646,76 @@ void policydb_destroy(struct policydb *p)
        kfree(p->class_val_to_struct);
        kfree(p->role_val_to_struct);
        kfree(p->user_val_to_struct);
+       kfree(p->type_val_to_struct);
 
        avtab_destroy(&p->te_avtab);
 
        for (i = 0; i < OCON_NUM; i++) {
+               cond_resched();
                c = p->ocontexts[i];
                while (c) {
                        ctmp = c;
                        c = c->next;
-                       ocontext_destroy(ctmp,i);
+                       ocontext_destroy(ctmp, i);
                }
+               p->ocontexts[i] = NULL;
        }
 
        g = p->genfs;
        while (g) {
+               cond_resched();
                kfree(g->fstype);
                c = g->head;
                while (c) {
                        ctmp = c;
                        c = c->next;
-                       ocontext_destroy(ctmp,OCON_FSUSE);
+                       ocontext_destroy(ctmp, OCON_FSUSE);
                }
                gtmp = g;
                g = g->next;
                kfree(gtmp);
        }
+       p->genfs = NULL;
 
        cond_policydb_destroy(p);
 
        for (tr = p->role_tr; tr; tr = tr->next) {
-               if (ltr) kfree(ltr);
+               cond_resched();
+               kfree(ltr);
                ltr = tr;
        }
-       if (ltr) kfree(ltr);
+       kfree(ltr);
 
-       for (ra = p->role_allow; ra; ra = ra -> next) {
-               if (lra) kfree(lra);
+       for (ra = p->role_allow; ra; ra = ra->next) {
+               cond_resched();
+               kfree(lra);
                lra = ra;
        }
-       if (lra) kfree(lra);
+       kfree(lra);
 
-       for (rt = p->range_tr; rt; rt = rt -> next) {
-               if (lrt) kfree(lrt);
+       for (rt = p->range_tr; rt; rt = rt->next) {
+               cond_resched();
+               if (lrt) {
+                       ebitmap_destroy(&lrt->target_range.level[0].cat);
+                       ebitmap_destroy(&lrt->target_range.level[1].cat);
+                       kfree(lrt);
+               }
                lrt = rt;
        }
-       if (lrt) kfree(lrt);
+       if (lrt) {
+               ebitmap_destroy(&lrt->target_range.level[0].cat);
+               ebitmap_destroy(&lrt->target_range.level[1].cat);
+               kfree(lrt);
+       }
 
        if (p->type_attr_map) {
                for (i = 0; i < p->p_types.nprim; i++)
                        ebitmap_destroy(&p->type_attr_map[i]);
        }
        kfree(p->type_attr_map);
+       kfree(p->undefined_perms);
+       ebitmap_destroy(&p->policycaps);
+       ebitmap_destroy(&p->permissive_map);
 
        return;
 }
@@ -670,20 +731,20 @@ int policydb_load_isids(struct policydb *p, struct sidtab *s)
 
        rc = sidtab_init(s);
        if (rc) {
-               printk(KERN_ERR "security:  out of memory on SID table init\n");
+               printk(KERN_ERR "SELinux:  out of memory on SID table init\n");
                goto out;
        }
 
        head = p->ocontexts[OCON_ISID];
        for (c = head; c; c = c->next) {
                if (!c->context[0].user) {
-                       printk(KERN_ERR "security:  SID %s was never "
+                       printk(KERN_ERR "SELinux:  SID %s was never "
                               "defined.\n", c->u.name);
                        rc = -EINVAL;
                        goto out;
                }
                if (sidtab_insert(s, c->sid[0], &c->context[0])) {
-                       printk(KERN_ERR "security:  unable to load initial "
+                       printk(KERN_ERR "SELinux:  unable to load initial "
                               "SID %s.\n", c->u.name);
                        rc = -EINVAL;
                        goto out;
@@ -693,6 +754,27 @@ out:
        return rc;
 }
 
+int policydb_class_isvalid(struct policydb *p, unsigned int class)
+{
+       if (!class || class > p->p_classes.nprim)
+               return 0;
+       return 1;
+}
+
+int policydb_role_isvalid(struct policydb *p, unsigned int role)
+{
+       if (!role || role > p->p_roles.nprim)
+               return 0;
+       return 1;
+}
+
+int policydb_type_isvalid(struct policydb *p, unsigned int type)
+{
+       if (!type || type > p->p_types.nprim)
+               return 0;
+       return 1;
+}
+
 /*
  * Return 1 if the fields in the security context
  * structure `c' are valid.  Return 0 otherwise.
@@ -756,13 +838,13 @@ static int mls_read_range_helper(struct mls_range *r, void *fp)
 
        items = le32_to_cpu(buf[0]);
        if (items > ARRAY_SIZE(buf)) {
-               printk(KERN_ERR "security: mls:  range overflow\n");
+               printk(KERN_ERR "SELinux: mls:  range overflow\n");
                rc = -EINVAL;
                goto out;
        }
        rc = next_entry(buf, fp, sizeof(u32) * items);
        if (rc < 0) {
-               printk(KERN_ERR "security: mls:  truncated range\n");
+               printk(KERN_ERR "SELinux: mls:  truncated range\n");
                goto out;
        }
        r->level[0].sens = le32_to_cpu(buf[0]);
@@ -773,21 +855,21 @@ static int mls_read_range_helper(struct mls_range *r, void *fp)
 
        rc = ebitmap_read(&r->level[0].cat, fp);
        if (rc) {
-               printk(KERN_ERR "security: mls:  error reading low "
+               printk(KERN_ERR "SELinux: mls:  error reading low "
                       "categories\n");
                goto out;
        }
        if (items > 1) {
                rc = ebitmap_read(&r->level[1].cat, fp);
                if (rc) {
-                       printk(KERN_ERR "security: mls:  error reading high "
+                       printk(KERN_ERR "SELinux: mls:  error reading high "
                               "categories\n");
                        goto bad_high;
                }
        } else {
                rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
                if (rc) {
-                       printk(KERN_ERR "security: mls:  out of memory\n");
+                       printk(KERN_ERR "SELinux: mls:  out of memory\n");
                        goto bad_high;
                }
        }
@@ -813,7 +895,7 @@ static int context_read_and_validate(struct context *c,
 
        rc = next_entry(buf, fp, sizeof buf);
        if (rc < 0) {
-               printk(KERN_ERR "security: context truncated\n");
+               printk(KERN_ERR "SELinux: context truncated\n");
                goto out;
        }
        c->user = le32_to_cpu(buf[0]);
@@ -821,7 +903,7 @@ static int context_read_and_validate(struct context *c,
        c->type = le32_to_cpu(buf[2]);
        if (p->policyvers >= POLICYDB_VERSION_MLS) {
                if (mls_read_range_helper(&c->range, fp)) {
-                       printk(KERN_ERR "security: error reading MLS range of "
+                       printk(KERN_ERR "SELinux: error reading MLS range of "
                               "context\n");
                        rc = -EINVAL;
                        goto out;
@@ -829,7 +911,7 @@ static int context_read_and_validate(struct context *c,
        }
 
        if (!policydb_context_isvalid(p, c)) {
-               printk(KERN_ERR "security:  invalid security context\n");
+               printk(KERN_ERR "SELinux:  invalid security context\n");
                context_destroy(c);
                rc = -EINVAL;
        }
@@ -851,12 +933,11 @@ static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
        __le32 buf[2];
        u32 len;
 
-       perdatum = kmalloc(sizeof(*perdatum), GFP_KERNEL);
+       perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
        if (!perdatum) {
                rc = -ENOMEM;
                goto out;
        }
-       memset(perdatum, 0, sizeof(*perdatum));
 
        rc = next_entry(buf, fp, sizeof buf);
        if (rc < 0)
@@ -865,7 +946,7 @@ static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
        len = le32_to_cpu(buf[0]);
        perdatum->value = le32_to_cpu(buf[1]);
 
-       key = kmalloc(len + 1,GFP_KERNEL);
+       key = kmalloc(len + 1, GFP_KERNEL);
        if (!key) {
                rc = -ENOMEM;
                goto bad;
@@ -873,7 +954,7 @@ static int perm_read(struct policydb *p, struct hashtab *h, void *fp)
        rc = next_entry(key, fp, len);
        if (rc < 0)
                goto bad;
-       key[len] = 0;
+       key[len] = '\0';
 
        rc = hashtab_insert(h, key, perdatum);
        if (rc)
@@ -893,12 +974,11 @@ static int common_read(struct policydb *p, struct hashtab *h, void *fp)
        u32 len, nel;
        int i, rc;
 
-       comdatum = kmalloc(sizeof(*comdatum), GFP_KERNEL);
+       comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
        if (!comdatum) {
                rc = -ENOMEM;
                goto out;
        }
-       memset(comdatum, 0, sizeof(*comdatum));
 
        rc = next_entry(buf, fp, sizeof buf);
        if (rc < 0)
@@ -913,7 +993,7 @@ static int common_read(struct policydb *p, struct hashtab *h, void *fp)
        comdatum->permissions.nprim = le32_to_cpu(buf[2]);
        nel = le32_to_cpu(buf[3]);
 
-       key = kmalloc(len + 1,GFP_KERNEL);
+       key = kmalloc(len + 1, GFP_KERNEL);
        if (!key) {
                rc = -ENOMEM;
                goto bad;
@@ -921,7 +1001,7 @@ static int common_read(struct policydb *p, struct hashtab *h, void *fp)
        rc = next_entry(key, fp, len);
        if (rc < 0)
                goto bad;
-       key[len] = 0;
+       key[len] = '\0';
 
        for (i = 0; i < nel; i++) {
                rc = perm_read(p, comdatum->permissions.table, fp);
@@ -940,7 +1020,7 @@ bad:
 }
 
 static int read_cons_helper(struct constraint_node **nodep, int ncons,
-                            int allowxtarget, void *fp)
+                           int allowxtarget, void *fp)
 {
        struct constraint_node *c, *lc;
        struct constraint_expr *e, *le;
@@ -950,16 +1030,14 @@ static int read_cons_helper(struct constraint_node **nodep, int ncons,
 
        lc = NULL;
        for (i = 0; i < ncons; i++) {
-               c = kmalloc(sizeof(*c), GFP_KERNEL);
+               c = kzalloc(sizeof(*c), GFP_KERNEL);
                if (!c)
                        return -ENOMEM;
-               memset(c, 0, sizeof(*c));
 
-               if (lc) {
+               if (lc)
                        lc->next = c;
-               } else {
+               else
                        *nodep = c;
-               }
 
                rc = next_entry(buf, fp, (sizeof(u32) * 2));
                if (rc < 0)
@@ -969,16 +1047,14 @@ static int read_cons_helper(struct constraint_node **nodep, int ncons,
                le = NULL;
                depth = -1;
                for (j = 0; j < nexpr; j++) {
-                       e = kmalloc(sizeof(*e), GFP_KERNEL);
+                       e = kzalloc(sizeof(*e), GFP_KERNEL);
                        if (!e)
                                return -ENOMEM;
-                       memset(e, 0, sizeof(*e));
 
-                       if (le) {
+                       if (le)
                                le->next = e;
-                       } else {
+                       else
                                c->expr = e;
-                       }
 
                        rc = next_entry(buf, fp, (sizeof(u32) * 3));
                        if (rc < 0)
@@ -1033,12 +1109,11 @@ static int class_read(struct policydb *p, struct hashtab *h, void *fp)
        u32 len, len2, ncons, nel;
        int i, rc;
 
-       cladatum = kmalloc(sizeof(*cladatum), GFP_KERNEL);
+       cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
        if (!cladatum) {
                rc = -ENOMEM;
                goto out;
        }
-       memset(cladatum, 0, sizeof(*cladatum));
 
        rc = next_entry(buf, fp, sizeof(u32)*6);
        if (rc < 0)
@@ -1056,7 +1131,7 @@ static int class_read(struct policydb *p, struct hashtab *h, void *fp)
 
        ncons = le32_to_cpu(buf[5]);
 
-       key = kmalloc(len + 1,GFP_KERNEL);
+       key = kmalloc(len + 1, GFP_KERNEL);
        if (!key) {
                rc = -ENOMEM;
                goto bad;
@@ -1064,10 +1139,10 @@ static int class_read(struct policydb *p, struct hashtab *h, void *fp)
        rc = next_entry(key, fp, len);
        if (rc < 0)
                goto bad;
-       key[len] = 0;
+       key[len] = '\0';
 
        if (len2) {
-               cladatum->comkey = kmalloc(len2 + 1,GFP_KERNEL);
+               cladatum->comkey = kmalloc(len2 + 1, GFP_KERNEL);
                if (!cladatum->comkey) {
                        rc = -ENOMEM;
                        goto bad;
@@ -1075,12 +1150,12 @@ static int class_read(struct policydb *p, struct hashtab *h, void *fp)
                rc = next_entry(cladatum->comkey, fp, len2);
                if (rc < 0)
                        goto bad;
-               cladatum->comkey[len2] = 0;
+               cladatum->comkey[len2] = '\0';
 
                cladatum->comdatum = hashtab_search(p->p_commons.table,
                                                    cladatum->comkey);
                if (!cladatum->comdatum) {
-                       printk(KERN_ERR "security:  unknown common %s\n",
+                       printk(KERN_ERR "SELinux:  unknown common %s\n",
                               cladatum->comkey);
                        rc = -EINVAL;
                        goto bad;
@@ -1115,7 +1190,7 @@ static int class_read(struct policydb *p, struct hashtab *h, void *fp)
 out:
        return rc;
 bad:
-       class_destroy(key, cladatum, NULL);
+       cls_destroy(key, cladatum, NULL);
        goto out;
 }
 
@@ -1123,25 +1198,29 @@ static int role_read(struct policydb *p, struct hashtab *h, void *fp)
 {
        char *key = NULL;
        struct role_datum *role;
-       int rc;
-       __le32 buf[2];
+       int rc, to_read = 2;
+       __le32 buf[3];
        u32 len;
 
-       role = kmalloc(sizeof(*role), GFP_KERNEL);
+       role = kzalloc(sizeof(*role), GFP_KERNEL);
        if (!role) {
                rc = -ENOMEM;
                goto out;
        }
-       memset(role, 0, sizeof(*role));
 
-       rc = next_entry(buf, fp, sizeof buf);
+       if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
+               to_read = 3;
+
+       rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
        if (rc < 0)
                goto bad;
 
        len = le32_to_cpu(buf[0]);
        role->value = le32_to_cpu(buf[1]);
+       if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
+               role->bounds = le32_to_cpu(buf[2]);
 
-       key = kmalloc(len + 1,GFP_KERNEL);
+       key = kmalloc(len + 1, GFP_KERNEL);
        if (!key) {
                rc = -ENOMEM;
                goto bad;
@@ -1149,7 +1228,7 @@ static int role_read(struct policydb *p, struct hashtab *h, void *fp)
        rc = next_entry(key, fp, len);
        if (rc < 0)
                goto bad;
-       key[len] = 0;
+       key[len] = '\0';
 
        rc = ebitmap_read(&role->dominates, fp);
        if (rc)
@@ -1161,7 +1240,7 @@ static int role_read(struct policydb *p, struct hashtab *h, void *fp)
 
        if (strcmp(key, OBJECT_R) == 0) {
                if (role->value != OBJECT_R_VAL) {
-                       printk(KERN_ERR "Role %s has wrong value %d\n",
+                       printk(KERN_ERR "SELinux: Role %s has wrong value %d\n",
                               OBJECT_R, role->value);
                        rc = -EINVAL;
                        goto bad;
@@ -1184,26 +1263,39 @@ static int type_read(struct policydb *p, struct hashtab *h, void *fp)
 {
        char *key = NULL;
        struct type_datum *typdatum;
-       int rc;
-       __le32 buf[3];
+       int rc, to_read = 3;
+       __le32 buf[4];
        u32 len;
 
-       typdatum = kmalloc(sizeof(*typdatum),GFP_KERNEL);
+       typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
        if (!typdatum) {
                rc = -ENOMEM;
                return rc;
        }
-       memset(typdatum, 0, sizeof(*typdatum));
 
-       rc = next_entry(buf, fp, sizeof buf);
+       if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
+               to_read = 4;
+
+       rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
        if (rc < 0)
                goto bad;
 
        len = le32_to_cpu(buf[0]);
        typdatum->value = le32_to_cpu(buf[1]);
-       typdatum->primary = le32_to_cpu(buf[2]);
+       if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
+               u32 prop = le32_to_cpu(buf[2]);
+
+               if (prop & TYPEDATUM_PROPERTY_PRIMARY)
+                       typdatum->primary = 1;
+               if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
+                       typdatum->attribute = 1;
+
+               typdatum->bounds = le32_to_cpu(buf[3]);
+       } else {
+               typdatum->primary = le32_to_cpu(buf[2]);
+       }
 
-       key = kmalloc(len + 1,GFP_KERNEL);
+       key = kmalloc(len + 1, GFP_KERNEL);
        if (!key) {
                rc = -ENOMEM;
                goto bad;
@@ -1211,7 +1303,7 @@ static int type_read(struct policydb *p, struct hashtab *h, void *fp)
        rc = next_entry(key, fp, len);
        if (rc < 0)
                goto bad;
-       key[len] = 0;
+       key[len] = '\0';
 
        rc = hashtab_insert(h, key, typdatum);
        if (rc)
@@ -1237,16 +1329,17 @@ static int mls_read_level(struct mls_level *lp, void *fp)
 
        rc = next_entry(buf, fp, sizeof buf);
        if (rc < 0) {
-               printk(KERN_ERR "security: mls: truncated level\n");
+               printk(KERN_ERR "SELinux: mls: truncated level\n");
                goto bad;
        }
        lp->sens = le32_to_cpu(buf[0]);
 
        if (ebitmap_read(&lp->cat, fp)) {
-               printk(KERN_ERR "security: mls:  error reading level "
+               printk(KERN_ERR "SELinux: mls:  error reading level "
                       "categories\n");
                goto bad;
        }
+
        return 0;
 
 bad:
@@ -1257,25 +1350,29 @@ static int user_read(struct policydb *p, struct hashtab *h, void *fp)
 {
        char *key = NULL;
        struct user_datum *usrdatum;
-       int rc;
-       __le32 buf[2];
+       int rc, to_read = 2;
+       __le32 buf[3];
        u32 len;
 
-       usrdatum = kmalloc(sizeof(*usrdatum), GFP_KERNEL);
+       usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
        if (!usrdatum) {
                rc = -ENOMEM;
                goto out;
        }
-       memset(usrdatum, 0, sizeof(*usrdatum));
 
-       rc = next_entry(buf, fp, sizeof buf);
+       if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
+               to_read = 3;
+
+       rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
        if (rc < 0)
                goto bad;
 
        len = le32_to_cpu(buf[0]);
        usrdatum->value = le32_to_cpu(buf[1]);
+       if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
+               usrdatum->bounds = le32_to_cpu(buf[2]);
 
-       key = kmalloc(len + 1,GFP_KERNEL);
+       key = kmalloc(len + 1, GFP_KERNEL);
        if (!key) {
                rc = -ENOMEM;
                goto bad;
@@ -1283,7 +1380,7 @@ static int user_read(struct policydb *p, struct hashtab *h, void *fp)
        rc = next_entry(key, fp, len);
        if (rc < 0)
                goto bad;
-       key[len] = 0;
+       key[len] = '\0';
 
        rc = ebitmap_read(&usrdatum->roles, fp);
        if (rc)
@@ -1316,12 +1413,11 @@ static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
        __le32 buf[2];
        u32 len;
 
-       levdatum = kmalloc(sizeof(*levdatum), GFP_ATOMIC);
+       levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
        if (!levdatum) {
                rc = -ENOMEM;
                goto out;
        }
-       memset(levdatum, 0, sizeof(*levdatum));
 
        rc = next_entry(buf, fp, sizeof buf);
        if (rc < 0)
@@ -1330,7 +1426,7 @@ static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
        len = le32_to_cpu(buf[0]);
        levdatum->isalias = le32_to_cpu(buf[1]);
 
-       key = kmalloc(len + 1,GFP_ATOMIC);
+       key = kmalloc(len + 1, GFP_ATOMIC);
        if (!key) {
                rc = -ENOMEM;
                goto bad;
@@ -1338,7 +1434,7 @@ static int sens_read(struct policydb *p, struct hashtab *h, void *fp)
        rc = next_entry(key, fp, len);
        if (rc < 0)
                goto bad;
-       key[len] = 0;
+       key[len] = '\0';
 
        levdatum->level = kmalloc(sizeof(struct mls_level), GFP_ATOMIC);
        if (!levdatum->level) {
@@ -1368,12 +1464,11 @@ static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
        __le32 buf[3];
        u32 len;
 
-       catdatum = kmalloc(sizeof(*catdatum), GFP_ATOMIC);
+       catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
        if (!catdatum) {
                rc = -ENOMEM;
                goto out;
        }
-       memset(catdatum, 0, sizeof(*catdatum));
 
        rc = next_entry(buf, fp, sizeof buf);
        if (rc < 0)
@@ -1383,7 +1478,7 @@ static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
        catdatum->value = le32_to_cpu(buf[1]);
        catdatum->isalias = le32_to_cpu(buf[2]);
 
-       key = kmalloc(len + 1,GFP_ATOMIC);
+       key = kmalloc(len + 1, GFP_ATOMIC);
        if (!key) {
                rc = -ENOMEM;
                goto bad;
@@ -1391,7 +1486,7 @@ static int cat_read(struct policydb *p, struct hashtab *h, void *fp)
        rc = next_entry(key, fp, len);
        if (rc < 0)
                goto bad;
-       key[len] = 0;
+       key[len] = '\0';
 
        rc = hashtab_insert(h, key, catdatum);
        if (rc)
@@ -1416,6 +1511,133 @@ static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp)
        cat_read,
 };
 
+static int user_bounds_sanity_check(void *key, void *datum, void *datap)
+{
+       struct user_datum *upper, *user;
+       struct policydb *p = datap;
+       int depth = 0;
+
+       upper = user = datum;
+       while (upper->bounds) {
+               struct ebitmap_node *node;
+               unsigned long bit;
+
+               if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
+                       printk(KERN_ERR "SELinux: user %s: "
+                              "too deep or looped boundary",
+                              (char *) key);
+                       return -EINVAL;
+               }
+
+               upper = p->user_val_to_struct[upper->bounds - 1];
+               ebitmap_for_each_positive_bit(&user->roles, node, bit) {
+                       if (ebitmap_get_bit(&upper->roles, bit))
+                               continue;
+
+                       printk(KERN_ERR
+                              "SELinux: boundary violated policy: "
+                              "user=%s role=%s bounds=%s\n",
+                              p->p_user_val_to_name[user->value - 1],
+                              p->p_role_val_to_name[bit],
+                              p->p_user_val_to_name[upper->value - 1]);
+
+                       return -EINVAL;
+               }
+       }
+
+       return 0;
+}
+
+static int role_bounds_sanity_check(void *key, void *datum, void *datap)
+{
+       struct role_datum *upper, *role;
+       struct policydb *p = datap;
+       int depth = 0;
+
+       upper = role = datum;
+       while (upper->bounds) {
+               struct ebitmap_node *node;
+               unsigned long bit;
+
+               if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
+                       printk(KERN_ERR "SELinux: role %s: "
+                              "too deep or looped bounds\n",
+                              (char *) key);
+                       return -EINVAL;
+               }
+
+               upper = p->role_val_to_struct[upper->bounds - 1];
+               ebitmap_for_each_positive_bit(&role->types, node, bit) {
+                       if (ebitmap_get_bit(&upper->types, bit))
+                               continue;
+
+                       printk(KERN_ERR
+                              "SELinux: boundary violated policy: "
+                              "role=%s type=%s bounds=%s\n",
+                              p->p_role_val_to_name[role->value - 1],
+                              p->p_type_val_to_name[bit],
+                              p->p_role_val_to_name[upper->value - 1]);
+
+                       return -EINVAL;
+               }
+       }
+
+       return 0;
+}
+
+static int type_bounds_sanity_check(void *key, void *datum, void *datap)
+{
+       struct type_datum *upper, *type;
+       struct policydb *p = datap;
+       int depth = 0;
+
+       upper = type = datum;
+       while (upper->bounds) {
+               if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
+                       printk(KERN_ERR "SELinux: type %s: "
+                              "too deep or looped boundary\n",
+                              (char *) key);
+                       return -EINVAL;
+               }
+
+               upper = p->type_val_to_struct[upper->bounds - 1];
+               if (upper->attribute) {
+                       printk(KERN_ERR "SELinux: type %s: "
+                              "bounded by attribute %s",
+                              (char *) key,
+                              p->p_type_val_to_name[upper->value - 1]);
+                       return -EINVAL;
+               }
+       }
+
+       return 0;
+}
+
+static int policydb_bounds_sanity_check(struct policydb *p)
+{
+       int rc;
+
+       if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
+               return 0;
+
+       rc = hashtab_map(p->p_users.table,
+                        user_bounds_sanity_check, p);
+       if (rc)
+               return rc;
+
+       rc = hashtab_map(p->p_roles.table,
+                        role_bounds_sanity_check, p);
+       if (rc)
+               return rc;
+
+       rc = hashtab_map(p->p_types.table,
+                        type_bounds_sanity_check, p);
+       if (rc)
+               return rc;
+
+       return 0;
+}
+
 extern int ss_initialized;
 
 /*
@@ -1429,7 +1651,8 @@ int policydb_read(struct policydb *p, void *fp)
        struct ocontext *l, *c, *newc;
        struct genfs *genfs_p, *genfs, *newgenfs;
        int i, j, rc;
-       __le32 buf[8];
+       __le32 buf[4];
+       u32 nodebuf[8];
        u32 len, len2, config, nprim, nel, nel2;
        char *policydb_str;
        struct policydb_compat_info *info;
@@ -1442,12 +1665,12 @@ int policydb_read(struct policydb *p, void *fp)
                goto out;
 
        /* Read the magic number and string length. */
-       rc = next_entry(buf, fp, sizeof(u32)* 2);
+       rc = next_entry(buf, fp, sizeof(u32) * 2);
        if (rc < 0)
                goto bad;
 
        if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
-               printk(KERN_ERR "security:  policydb magic number 0x%x does "
+               printk(KERN_ERR "SELinux:  policydb magic number 0x%x does "
                       "not match expected magic number 0x%x\n",
                       le32_to_cpu(buf[0]), POLICYDB_MAGIC);
                goto bad;
@@ -1455,27 +1678,27 @@ int policydb_read(struct policydb *p, void *fp)
 
        len = le32_to_cpu(buf[1]);
        if (len != strlen(POLICYDB_STRING)) {
-               printk(KERN_ERR "security:  policydb string length %d does not "
+               printk(KERN_ERR "SELinux:  policydb string length %d does not "
                       "match expected length %Zu\n",
                       len, strlen(POLICYDB_STRING));
                goto bad;
        }
-       policydb_str = kmalloc(len + 1,GFP_KERNEL);
+       policydb_str = kmalloc(len + 1, GFP_KERNEL);
        if (!policydb_str) {
-               printk(KERN_ERR "security:  unable to allocate memory for policydb "
+               printk(KERN_ERR "SELinux:  unable to allocate memory for policydb "
                       "string of length %d\n", len);
                rc = -ENOMEM;
                goto bad;
        }
        rc = next_entry(policydb_str, fp, len);
        if (rc < 0) {
-               printk(KERN_ERR "security:  truncated policydb string identifier\n");
+               printk(KERN_ERR "SELinux:  truncated policydb string identifier\n");
                kfree(policydb_str);
                goto bad;
        }
-       policydb_str[len] = 0;
+       policydb_str[len] = '\0';
        if (strcmp(policydb_str, POLICYDB_STRING)) {
-               printk(KERN_ERR "security:  policydb string %s does not match "
+               printk(KERN_ERR "SELinux:  policydb string %s does not match "
                       "my string %s\n", policydb_str, POLICYDB_STRING);
                kfree(policydb_str);
                goto bad;
@@ -1492,44 +1715,55 @@ int policydb_read(struct policydb *p, void *fp)
        p->policyvers = le32_to_cpu(buf[0]);
        if (p->policyvers < POLICYDB_VERSION_MIN ||
            p->policyvers > POLICYDB_VERSION_MAX) {
-               printk(KERN_ERR "security:  policydb version %d does not match "
-                      "my version range %d-%d\n",
-                      le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
-               goto bad;
+               printk(KERN_ERR "SELinux:  policydb version %d does not match "
+                      "my version range %d-%d\n",
+                      le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
+               goto bad;
        }
 
        if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
                if (ss_initialized && !selinux_mls_enabled) {
-                       printk(KERN_ERR "Cannot switch between non-MLS and MLS "
-                              "policies\n");
+                       printk(KERN_ERR "SELinux: Cannot switch between non-MLS"
+                               " and MLS policies\n");
                        goto bad;
                }
                selinux_mls_enabled = 1;
                config |= POLICYDB_CONFIG_MLS;
 
                if (p->policyvers < POLICYDB_VERSION_MLS) {
-                       printk(KERN_ERR "security policydb version %d (MLS) "
-                              "not backwards compatible\n", p->policyvers);
+                       printk(KERN_ERR "SELinux: security policydb version %d "
+                               "(MLS) not backwards compatible\n",
+                               p->policyvers);
                        goto bad;
                }
        } else {
                if (ss_initialized && selinux_mls_enabled) {
-                       printk(KERN_ERR "Cannot switch between MLS and non-MLS "
-                              "policies\n");
+                       printk(KERN_ERR "SELinux: Cannot switch between MLS and"
+                               " non-MLS policies\n");
                        goto bad;
                }
        }
+       p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
+       p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
+
+       if (p->policyvers >= POLICYDB_VERSION_POLCAP &&
+           ebitmap_read(&p->policycaps, fp) != 0)
+               goto bad;
+
+       if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE &&
+           ebitmap_read(&p->permissive_map, fp) != 0)
+               goto bad;
 
        info = policydb_lookup_compat(p->policyvers);
        if (!info) {
-               printk(KERN_ERR "security:  unable to find policy compat info "
+               printk(KERN_ERR "SELinux:  unable to find policy compat info "
                       "for version %d\n", p->policyvers);
                goto bad;
        }
 
        if (le32_to_cpu(buf[2]) != info->sym_num ||
                le32_to_cpu(buf[3]) != info->ocon_num) {
-               printk(KERN_ERR "security:  policydb table sizes (%d,%d) do "
+               printk(KERN_ERR "SELinux:  policydb table sizes (%d,%d) do "
                       "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
                        le32_to_cpu(buf[3]),
                       info->sym_num, info->ocon_num);
@@ -1551,7 +1785,7 @@ int policydb_read(struct policydb *p, void *fp)
                p->symtab[i].nprim = nprim;
        }
 
-       rc = avtab_read(&p->te_avtab, fp, p->policyvers);
+       rc = avtab_read(&p->te_avtab, fp, p);
        if (rc)
                goto bad;
 
@@ -1567,23 +1801,27 @@ int policydb_read(struct policydb *p, void *fp)
        nel = le32_to_cpu(buf[0]);
        ltr = NULL;
        for (i = 0; i < nel; i++) {
-               tr = kmalloc(sizeof(*tr), GFP_KERNEL);
+               tr = kzalloc(sizeof(*tr), GFP_KERNEL);
                if (!tr) {
                        rc = -ENOMEM;
                        goto bad;
                }
-               memset(tr, 0, sizeof(*tr));
-               if (ltr) {
+               if (ltr)
                        ltr->next = tr;
-               } else {
+               else
                        p->role_tr = tr;
-               }
                rc = next_entry(buf, fp, sizeof(u32)*3);
                if (rc < 0)
                        goto bad;
                tr->role = le32_to_cpu(buf[0]);
                tr->type = le32_to_cpu(buf[1]);
                tr->new_role = le32_to_cpu(buf[2]);
+               if (!policydb_role_isvalid(p, tr->role) ||
+                   !policydb_type_isvalid(p, tr->type) ||
+                   !policydb_role_isvalid(p, tr->new_role)) {
+                       rc = -EINVAL;
+                       goto bad;
+               }
                ltr = tr;
        }
 
@@ -1593,22 +1831,25 @@ int policydb_read(struct policydb *p, void *fp)
        nel = le32_to_cpu(buf[0]);
        lra = NULL;
        for (i = 0; i < nel; i++) {
-               ra = kmalloc(sizeof(*ra), GFP_KERNEL);
+               ra = kzalloc(sizeof(*ra), GFP_KERNEL);
                if (!ra) {
                        rc = -ENOMEM;
                        goto bad;
                }
-               memset(ra, 0, sizeof(*ra));
-               if (lra) {
+               if (lra)
                        lra->next = ra;
-               } else {
+               else
                        p->role_allow = ra;
-               }
                rc = next_entry(buf, fp, sizeof(u32)*2);
                if (rc < 0)
                        goto bad;
                ra->role = le32_to_cpu(buf[0]);
                ra->new_role = le32_to_cpu(buf[1]);
+               if (!policydb_role_isvalid(p, ra->role) ||
+                   !policydb_role_isvalid(p, ra->new_role)) {
+                       rc = -EINVAL;
+                       goto bad;
+               }
                lra = ra;
        }
 
@@ -1627,17 +1868,15 @@ int policydb_read(struct policydb *p, void *fp)
                nel = le32_to_cpu(buf[0]);
                l = NULL;
                for (j = 0; j < nel; j++) {
-                       c = kmalloc(sizeof(*c), GFP_KERNEL);
+                       c = kzalloc(sizeof(*c), GFP_KERNEL);
                        if (!c) {
                                rc = -ENOMEM;
                                goto bad;
                        }
-                       memset(c, 0, sizeof(*c));
-                       if (l) {
+                       if (l)
                                l->next = c;
-                       } else {
+                       else
                                p->ocontexts[i] = c;
-                       }
                        l = c;
                        rc = -EINVAL;
                        switch (i) {
@@ -1656,7 +1895,7 @@ int policydb_read(struct policydb *p, void *fp)
                                if (rc < 0)
                                        goto bad;
                                len = le32_to_cpu(buf[0]);
-                               c->u.name = kmalloc(len + 1,GFP_KERNEL);
+                               c->u.name = kmalloc(len + 1, GFP_KERNEL);
                                if (!c->u.name) {
                                        rc = -ENOMEM;
                                        goto bad;
@@ -1684,11 +1923,11 @@ int policydb_read(struct policydb *p, void *fp)
                                        goto bad;
                                break;
                        case OCON_NODE:
-                               rc = next_entry(buf, fp, sizeof(u32)* 2);
+                               rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
                                if (rc < 0)
                                        goto bad;
-                               c->u.node.addr = le32_to_cpu(buf[0]);
-                               c->u.node.mask = le32_to_cpu(buf[1]);
+                               c->u.node.addr = nodebuf[0]; /* network order */
+                               c->u.node.mask = nodebuf[1]; /* network order */
                                rc = context_read_and_validate(&c->context[0], p, fp);
                                if (rc)
                                        goto bad;
@@ -1701,7 +1940,7 @@ int policydb_read(struct policydb *p, void *fp)
                                if (c->v.behavior > SECURITY_FS_USE_NONE)
                                        goto bad;
                                len = le32_to_cpu(buf[1]);
-                               c->u.name = kmalloc(len + 1,GFP_KERNEL);
+                               c->u.name = kmalloc(len + 1, GFP_KERNEL);
                                if (!c->u.name) {
                                        rc = -ENOMEM;
                                        goto bad;
@@ -1717,13 +1956,13 @@ int policydb_read(struct policydb *p, void *fp)
                        case OCON_NODE6: {
                                int k;
 
-                               rc = next_entry(buf, fp, sizeof(u32) * 8);
+                               rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
                                if (rc < 0)
                                        goto bad;
                                for (k = 0; k < 4; k++)
-                                       c->u.node6.addr[k] = le32_to_cpu(buf[k]);
+                                       c->u.node6.addr[k] = nodebuf[k];
                                for (k = 0; k < 4; k++)
-                                       c->u.node6.mask[k] = le32_to_cpu(buf[k+4]);
+                                       c->u.node6.mask[k] = nodebuf[k+4];
                                if (context_read_and_validate(&c->context[0], p, fp))
                                        goto bad;
                                break;
@@ -1743,14 +1982,13 @@ int policydb_read(struct policydb *p, void *fp)
                if (rc < 0)
                        goto bad;
                len = le32_to_cpu(buf[0]);
-               newgenfs = kmalloc(sizeof(*newgenfs), GFP_KERNEL);
+               newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
                if (!newgenfs) {
                        rc = -ENOMEM;
                        goto bad;
                }
-               memset(newgenfs, 0, sizeof(*newgenfs));
 
-               newgenfs->fstype = kmalloc(len + 1,GFP_KERNEL);
+               newgenfs->fstype = kmalloc(len + 1, GFP_KERNEL);
                if (!newgenfs->fstype) {
                        rc = -ENOMEM;
                        kfree(newgenfs);
@@ -1766,7 +2004,7 @@ int policydb_read(struct policydb *p, void *fp)
                for (genfs_p = NULL, genfs = p->genfs; genfs;
                     genfs_p = genfs, genfs = genfs->next) {
                        if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
-                               printk(KERN_ERR "security:  dup genfs "
+                               printk(KERN_ERR "SELinux:  dup genfs "
                                       "fstype %s\n", newgenfs->fstype);
                                kfree(newgenfs->fstype);
                                kfree(newgenfs);
@@ -1790,14 +2028,13 @@ int policydb_read(struct policydb *p, void *fp)
                                goto bad;
                        len = le32_to_cpu(buf[0]);
 
-                       newc = kmalloc(sizeof(*newc), GFP_KERNEL);
+                       newc = kzalloc(sizeof(*newc), GFP_KERNEL);
                        if (!newc) {
                                rc = -ENOMEM;
                                goto bad;
                        }
-                       memset(newc, 0, sizeof(*newc));
 
-                       newc->u.name = kmalloc(len + 1,GFP_KERNEL);
+                       newc->u.name = kmalloc(len + 1, GFP_KERNEL);
                        if (!newc->u.name) {
                                rc = -ENOMEM;
                                goto bad_newc;
@@ -1817,7 +2054,7 @@ int policydb_read(struct policydb *p, void *fp)
                                if (!strcmp(newc->u.name, c->u.name) &&
                                    (!c->v.sclass || !newc->v.sclass ||
                                     newc->v.sclass == c->v.sclass)) {
-                                       printk(KERN_ERR "security:  dup genfs "
+                                       printk(KERN_ERR "SELinux:  dup genfs "
                                               "entry (%s,%s)\n",
                                               newgenfs->fstype, c->u.name);
                                        goto bad_newc;
@@ -1837,18 +2074,18 @@ int policydb_read(struct policydb *p, void *fp)
        }
 
        if (p->policyvers >= POLICYDB_VERSION_MLS) {
+               int new_rangetr = p->policyvers >= POLICYDB_VERSION_RANGETRANS;
                rc = next_entry(buf, fp, sizeof(u32));
                if (rc < 0)
                        goto bad;
                nel = le32_to_cpu(buf[0]);
                lrt = NULL;
                for (i = 0; i < nel; i++) {
-                       rt = kmalloc(sizeof(*rt), GFP_KERNEL);
+                       rt = kzalloc(sizeof(*rt), GFP_KERNEL);
                        if (!rt) {
                                rc = -ENOMEM;
                                goto bad;
                        }
-                       memset(rt, 0, sizeof(*rt));
                        if (lrt)
                                lrt->next = rt;
                        else
@@ -1856,11 +2093,28 @@ int policydb_read(struct policydb *p, void *fp)
                        rc = next_entry(buf, fp, (sizeof(u32) * 2));
                        if (rc < 0)
                                goto bad;
-                       rt->dom = le32_to_cpu(buf[0]);
-                       rt->type = le32_to_cpu(buf[1]);
-                       rc = mls_read_range_helper(&rt->range, fp);
+                       rt->source_type = le32_to_cpu(buf[0]);
+                       rt->target_type = le32_to_cpu(buf[1]);
+                       if (new_rangetr) {
+                               rc = next_entry(buf, fp, sizeof(u32));
+                               if (rc < 0)
+                                       goto bad;
+                               rt->target_class = le32_to_cpu(buf[0]);
+                       } else
+                               rt->target_class = SECCLASS_PROCESS;
+                       if (!policydb_type_isvalid(p, rt->source_type) ||
+                           !policydb_type_isvalid(p, rt->target_type) ||
+                           !policydb_class_isvalid(p, rt->target_class)) {
+                               rc = -EINVAL;
+                               goto bad;
+                       }
+                       rc = mls_read_range_helper(&rt->target_range, fp);
                        if (rc)
                                goto bad;
+                       if (!mls_range_isvalid(p, &rt->target_range)) {
+                               printk(KERN_WARNING "SELinux:  rangetrans:  invalid range\n");
+                               goto bad;
+                       }
                        lrt = rt;
                }
        }
@@ -1880,11 +2134,15 @@ int policydb_read(struct policydb *p, void *fp)
                                goto bad;
        }
 
+       rc = policydb_bounds_sanity_check(p);
+       if (rc)
+               goto bad;
+
        rc = 0;
 out:
        return rc;
 bad_newc:
-       ocontext_destroy(newc,OCON_FSUSE);
+       ocontext_destroy(newc, OCON_FSUSE);
 bad:
        if (!rc)
                rc = -EINVAL;