···8282#include <linux/syslog.h>8383#include <linux/user_namespace.h>8484#include <linux/export.h>8585-#include <linux/security.h>8685#include <linux/msg.h>8786#include <linux/shm.h>8887···44734474{44744475 struct sk_security_struct *sksec = sk->sk_security;44754476 int err;44764476- u16 family = sk->sk_family;44774477+ u16 family = req->rsk_ops->family;44774478 u32 connsid;44784479 u32 peersid;44794479-44804480- /* handle mapped IPv4 packets arriving via IPv6 sockets */44814481- if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))44824482- family = PF_INET;4483448044844481 err = selinux_skb_peerlbl_sid(skb, family, &peersid);44854482 if (err)
+2-1
security/selinux/include/security.h
···3333#define POLICYDB_VERSION_ROLETRANS 263434#define POLICYDB_VERSION_NEW_OBJECT_DEFAULTS 273535#define POLICYDB_VERSION_DEFAULT_TYPE 283636+#define POLICYDB_VERSION_CONSTRAINT_NAMES 2936373738/* Range of policy versions we understand*/3839#define POLICYDB_VERSION_MIN POLICYDB_VERSION_BASE3940#ifdef CONFIG_SECURITY_SELINUX_POLICYDB_VERSION_MAX4041#define POLICYDB_VERSION_MAX CONFIG_SECURITY_SELINUX_POLICYDB_VERSION_MAX_VALUE4142#else4242-#define POLICYDB_VERSION_MAX POLICYDB_VERSION_DEFAULT_TYPE4343+#define POLICYDB_VERSION_MAX POLICYDB_VERSION_CONSTRAINT_NAMES4344#endif44454546/* Mask for just the mount related flags */
+30-1
security/selinux/netlabel.c
···101101}102102103103/**104104+ * selinux_netlbl_sock_getattr - Get the cached NetLabel secattr105105+ * @sk: the socket106106+ * @sid: the SID107107+ *108108+ * Query the socket's cached secattr and if the SID matches the cached value109109+ * return the cache, otherwise return NULL.110110+ *111111+ */112112+static struct netlbl_lsm_secattr *selinux_netlbl_sock_getattr(113113+ const struct sock *sk,114114+ u32 sid)115115+{116116+ struct sk_security_struct *sksec = sk->sk_security;117117+ struct netlbl_lsm_secattr *secattr = sksec->nlbl_secattr;118118+119119+ if (secattr == NULL)120120+ return NULL;121121+122122+ if ((secattr->flags & NETLBL_SECATTR_SECID) &&123123+ (secattr->attr.secid == sid))124124+ return secattr;125125+126126+ return NULL;127127+}128128+129129+/**104130 * selinux_netlbl_cache_invalidate - Invalidate the NetLabel cache105131 *106132 * Description:···250224 struct sk_security_struct *sksec = sk->sk_security;251225 if (sksec->nlbl_state != NLBL_REQSKB)252226 return 0;253253- secattr = sksec->nlbl_secattr;227227+ secattr = selinux_netlbl_sock_getattr(sk, sid);254228 }255229 if (secattr == NULL) {256230 secattr = &secattr_storage;···436410 sksec->nlbl_state == NLBL_CONNLABELED)) {437411 netlbl_secattr_init(&secattr);438412 lock_sock(sk);413413+ /* call the netlabel function directly as we want to see the414414+ * on-the-wire label that is assigned via the socket's options415415+ * and not the cached netlabel/lsm attributes */439416 rc = netlbl_sock_getattr(sk, &secattr);440417 release_sock(sk);441418 if (rc == 0)
···143143 .sym_num = SYM_NUM,144144 .ocon_num = OCON_NUM,145145 },146146+ {147147+ .version = POLICYDB_VERSION_CONSTRAINT_NAMES,148148+ .sym_num = SYM_NUM,149149+ .ocon_num = OCON_NUM,150150+ },146151};147152148153static struct policydb_compat_info *policydb_lookup_compat(int version)···618613 return 0;619614}620615616616+static void constraint_expr_destroy(struct constraint_expr *expr)617617+{618618+ if (expr) {619619+ ebitmap_destroy(&expr->names);620620+ if (expr->type_names) {621621+ ebitmap_destroy(&expr->type_names->types);622622+ ebitmap_destroy(&expr->type_names->negset);623623+ kfree(expr->type_names);624624+ }625625+ kfree(expr);626626+ }627627+}628628+621629static int cls_destroy(void *key, void *datum, void *p)622630{623631 struct class_datum *cladatum;···646628 while (constraint) {647629 e = constraint->expr;648630 while (e) {649649- ebitmap_destroy(&e->names);650631 etmp = e;651632 e = e->next;652652- kfree(etmp);633633+ constraint_expr_destroy(etmp);653634 }654635 ctemp = constraint;655636 constraint = constraint->next;···659642 while (constraint) {660643 e = constraint->expr;661644 while (e) {662662- ebitmap_destroy(&e->names);663645 etmp = e;664646 e = e->next;665665- kfree(etmp);647647+ constraint_expr_destroy(etmp);666648 }667649 ctemp = constraint;668650 constraint = constraint->next;669651 kfree(ctemp);670652 }671671-672653 kfree(cladatum->comkey);673654 }674655 kfree(datum);···11711156 return rc;11721157}1173115811741174-static int read_cons_helper(struct constraint_node **nodep, int ncons,11751175- int allowxtarget, void *fp)11591159+static void type_set_init(struct type_set *t)11601160+{11611161+ ebitmap_init(&t->types);11621162+ ebitmap_init(&t->negset);11631163+}11641164+11651165+static int type_set_read(struct type_set *t, void *fp)11661166+{11671167+ __le32 buf[1];11681168+ int rc;11691169+11701170+ if (ebitmap_read(&t->types, fp))11711171+ return -EINVAL;11721172+ if (ebitmap_read(&t->negset, fp))11731173+ return -EINVAL;11741174+11751175+ rc = next_entry(buf, fp, sizeof(u32));11761176+ if (rc < 0)11771177+ return -EINVAL;11781178+ t->flags = le32_to_cpu(buf[0]);11791179+11801180+ return 0;11811181+}11821182+11831183+11841184+static int read_cons_helper(struct policydb *p,11851185+ struct constraint_node **nodep,11861186+ int ncons, int allowxtarget, void *fp)11761187{11771188 struct constraint_node *c, *lc;11781189 struct constraint_expr *e, *le;···12661225 rc = ebitmap_read(&e->names, fp);12671226 if (rc)12681227 return rc;12281228+ if (p->policyvers >=12291229+ POLICYDB_VERSION_CONSTRAINT_NAMES) {12301230+ e->type_names = kzalloc(sizeof12311231+ (*e->type_names),12321232+ GFP_KERNEL);12331233+ if (!e->type_names)12341234+ return -ENOMEM;12351235+ type_set_init(e->type_names);12361236+ rc = type_set_read(e->type_names, fp);12371237+ if (rc)12381238+ return rc;12391239+ }12691240 break;12701241 default:12711242 return -EINVAL;···13541301 goto bad;13551302 }1356130313571357- rc = read_cons_helper(&cladatum->constraints, ncons, 0, fp);13041304+ rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);13581305 if (rc)13591306 goto bad;13601307···13641311 if (rc)13651312 goto bad;13661313 ncons = le32_to_cpu(buf[0]);13671367- rc = read_cons_helper(&cladatum->validatetrans, ncons, 1, fp);13141314+ rc = read_cons_helper(p, &cladatum->validatetrans,13151315+ ncons, 1, fp);13681316 if (rc)13691317 goto bad;13701318 }···28072753 return 0;28082754}2809275527562756+static int type_set_write(struct type_set *t, void *fp)27572757+{27582758+ int rc;27592759+ __le32 buf[1];27602760+27612761+ if (ebitmap_write(&t->types, fp))27622762+ return -EINVAL;27632763+ if (ebitmap_write(&t->negset, fp))27642764+ return -EINVAL;27652765+27662766+ buf[0] = cpu_to_le32(t->flags);27672767+ rc = put_entry(buf, sizeof(u32), 1, fp);27682768+ if (rc)27692769+ return -EINVAL;27702770+27712771+ return 0;27722772+}27732773+28102774static int write_cons_helper(struct policydb *p, struct constraint_node *node,28112775 void *fp)28122776{···28562784 rc = ebitmap_write(&e->names, fp);28572785 if (rc)28582786 return rc;27872787+ if (p->policyvers >=27882788+ POLICYDB_VERSION_CONSTRAINT_NAMES) {27892789+ rc = type_set_write(e->type_names, fp);27902790+ if (rc)27912791+ return rc;27922792+ }28592793 break;28602794 default:28612795 break;
+11
security/selinux/ss/policydb.h
···154154struct cond_node;155155156156/*157157+ * type set preserves data needed to determine constraint info from158158+ * policy source. This is not used by the kernel policy but allows159159+ * utilities such as audit2allow to determine constraint denials.160160+ */161161+struct type_set {162162+ struct ebitmap types;163163+ struct ebitmap negset;164164+ u32 flags;165165+};166166+167167+/*157168 * The configuration data includes security contexts for158169 * initial SIDs, unlabeled file systems, TCP and UDP port numbers,159170 * network interfaces, and nodes. This structure stores the
+32-22
security/selinux/ss/services.c
···18311831 */18321832int security_load_policy(void *data, size_t len)18331833{18341834- struct policydb oldpolicydb, newpolicydb;18341834+ struct policydb *oldpolicydb, *newpolicydb;18351835 struct sidtab oldsidtab, newsidtab;18361836 struct selinux_mapping *oldmap, *map = NULL;18371837 struct convert_context_args args;···18401840 int rc = 0;18411841 struct policy_file file = { data, len }, *fp = &file;1842184218431843+ oldpolicydb = kzalloc(2 * sizeof(*oldpolicydb), GFP_KERNEL);18441844+ if (!oldpolicydb) {18451845+ rc = -ENOMEM;18461846+ goto out;18471847+ }18481848+ newpolicydb = oldpolicydb + 1;18491849+18431850 if (!ss_initialized) {18441851 avtab_cache_init();18451852 rc = policydb_read(&policydb, fp);18461853 if (rc) {18471854 avtab_cache_destroy();18481848- return rc;18551855+ goto out;18491856 }1850185718511858 policydb.len = len;···18621855 if (rc) {18631856 policydb_destroy(&policydb);18641857 avtab_cache_destroy();18651865- return rc;18581858+ goto out;18661859 }1867186018681861 rc = policydb_load_isids(&policydb, &sidtab);18691862 if (rc) {18701863 policydb_destroy(&policydb);18711864 avtab_cache_destroy();18721872- return rc;18651865+ goto out;18731866 }1874186718751868 security_load_policycaps();···18811874 selinux_status_update_policyload(seqno);18821875 selinux_netlbl_cache_invalidate();18831876 selinux_xfrm_notify_policyload();18841884- return 0;18771877+ goto out;18851878 }1886187918871880#if 018881881 sidtab_hash_eval(&sidtab, "sids");18891882#endif1890188318911891- rc = policydb_read(&newpolicydb, fp);18841884+ rc = policydb_read(newpolicydb, fp);18921885 if (rc)18931893- return rc;18861886+ goto out;1894188718951895- newpolicydb.len = len;18881888+ newpolicydb->len = len;18961889 /* If switching between different policy types, log MLS status */18971897- if (policydb.mls_enabled && !newpolicydb.mls_enabled)18901890+ if (policydb.mls_enabled && !newpolicydb->mls_enabled)18981891 printk(KERN_INFO "SELinux: Disabling MLS support...\n");18991899- else if (!policydb.mls_enabled && newpolicydb.mls_enabled)18921892+ else if (!policydb.mls_enabled && newpolicydb->mls_enabled)19001893 printk(KERN_INFO "SELinux: Enabling MLS support...\n");1901189419021902- rc = policydb_load_isids(&newpolicydb, &newsidtab);18951895+ rc = policydb_load_isids(newpolicydb, &newsidtab);19031896 if (rc) {19041897 printk(KERN_ERR "SELinux: unable to load the initial SIDs\n");19051905- policydb_destroy(&newpolicydb);19061906- return rc;18981898+ policydb_destroy(newpolicydb);18991899+ goto out;19071900 }1908190119091909- rc = selinux_set_mapping(&newpolicydb, secclass_map, &map, &map_size);19021902+ rc = selinux_set_mapping(newpolicydb, secclass_map, &map, &map_size);19101903 if (rc)19111904 goto err;1912190519131913- rc = security_preserve_bools(&newpolicydb);19061906+ rc = security_preserve_bools(newpolicydb);19141907 if (rc) {19151908 printk(KERN_ERR "SELinux: unable to preserve booleans\n");19161909 goto err;···19281921 * in the new SID table.19291922 */19301923 args.oldp = &policydb;19311931- args.newp = &newpolicydb;19241924+ args.newp = newpolicydb;19321925 rc = sidtab_map(&newsidtab, convert_context, &args);19331926 if (rc) {19341927 printk(KERN_ERR "SELinux: unable to convert the internal"···19381931 }1939193219401933 /* Save the old policydb and SID table to free later. */19411941- memcpy(&oldpolicydb, &policydb, sizeof policydb);19341934+ memcpy(oldpolicydb, &policydb, sizeof(policydb));19421935 sidtab_set(&oldsidtab, &sidtab);1943193619441937 /* Install the new policydb and SID table. */19451938 write_lock_irq(&policy_rwlock);19461946- memcpy(&policydb, &newpolicydb, sizeof policydb);19391939+ memcpy(&policydb, newpolicydb, sizeof(policydb));19471940 sidtab_set(&sidtab, &newsidtab);19481941 security_load_policycaps();19491942 oldmap = current_mapping;···19531946 write_unlock_irq(&policy_rwlock);1954194719551948 /* Free the old policydb and SID table. */19561956- policydb_destroy(&oldpolicydb);19491949+ policydb_destroy(oldpolicydb);19571950 sidtab_destroy(&oldsidtab);19581951 kfree(oldmap);19591952···19631956 selinux_netlbl_cache_invalidate();19641957 selinux_xfrm_notify_policyload();1965195819661966- return 0;19591959+ rc = 0;19601960+ goto out;1967196119681962err:19691963 kfree(map);19701964 sidtab_destroy(&newsidtab);19711971- policydb_destroy(&newpolicydb);19721972- return rc;19651965+ policydb_destroy(newpolicydb);1973196619671967+out:19681968+ kfree(oldpolicydb);19691969+ return rc;19741970}1975197119761972size_t security_policydb_len(void)