Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

security: Allow all LSMs to provide xattrs for inode_init_security hook

Currently, the LSM infrastructure supports only one LSM providing an xattr
and EVM calculating the HMAC on that xattr, plus other inode metadata.

Allow all LSMs to provide one or multiple xattrs, by extending the security
blob reservation mechanism. Introduce the new lbs_xattr_count field of the
lsm_blob_sizes structure, so that each LSM can specify how many xattrs it
needs, and the LSM infrastructure knows how many xattr slots it should
allocate.

Modify the inode_init_security hook definition, by passing the full
xattr array allocated in security_inode_init_security(), and the current
number of xattr slots in that array filled by LSMs. The first parameter
would allow EVM to access and calculate the HMAC on xattrs supplied by
other LSMs, the second to not leave gaps in the xattr array, when an LSM
requested but did not provide xattrs (e.g. if it is not initialized).

Introduce lsm_get_xattr_slot(), which LSMs can call as many times as the
number specified in the lbs_xattr_count field of the lsm_blob_sizes
structure. During each call, lsm_get_xattr_slot() increments the number of
filled xattrs, so that at the next invocation it returns the next xattr
slot to fill.

Cleanup security_inode_init_security(). Unify the !initxattrs and
initxattrs case by simply not allocating the new_xattrs array in the
former. Update the documentation to reflect the changes, and fix the
description of the xattr name, as it is not allocated anymore.

Adapt both SELinux and Smack to use the new definition of the
inode_init_security hook, and to call lsm_get_xattr_slot() to obtain and
fill the reserved slots in the xattr array.

Move the xattr->name assignment after the xattr->value one, so that it is
done only in case of successful memory allocation.

Finally, change the default return value of the inode_init_security hook
from zero to -EOPNOTSUPP, so that BPF LSM correctly follows the hook
conventions.

Reported-by: Nicolas Bouchinet <nicolas.bouchinet@clip-os.org>
Link: https://lore.kernel.org/linux-integrity/Y1FTSIo+1x+4X0LS@archlinux/
Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
Acked-by: Casey Schaufler <casey@schaufler-ca.com>
[PM: minor comment and variable tweaks, approved by RS]
Signed-off-by: Paul Moore <paul@paul-moore.com>

authored by

Roberto Sassu and committed by
Paul Moore
6bcdfd2c ff72942c

+94 -47
+3 -3
include/linux/lsm_hook_defs.h
··· 111 111 unsigned int obj_type) 112 112 LSM_HOOK(int, 0, inode_alloc_security, struct inode *inode) 113 113 LSM_HOOK(void, LSM_RET_VOID, inode_free_security, struct inode *inode) 114 - LSM_HOOK(int, 0, inode_init_security, struct inode *inode, 115 - struct inode *dir, const struct qstr *qstr, const char **name, 116 - void **value, size_t *len) 114 + LSM_HOOK(int, -EOPNOTSUPP, inode_init_security, struct inode *inode, 115 + struct inode *dir, const struct qstr *qstr, struct xattr *xattrs, 116 + int *xattr_count) 117 117 LSM_HOOK(int, 0, inode_init_security_anon, struct inode *inode, 118 118 const struct qstr *name, const struct inode *context_inode) 119 119 LSM_HOOK(int, 0, inode_create, struct inode *dir, struct dentry *dentry,
+20
include/linux/lsm_hooks.h
··· 28 28 #include <linux/security.h> 29 29 #include <linux/init.h> 30 30 #include <linux/rculist.h> 31 + #include <linux/xattr.h> 31 32 32 33 union security_list_options { 33 34 #define LSM_HOOK(RET, DEFAULT, NAME, ...) RET (*NAME)(__VA_ARGS__); ··· 64 63 int lbs_ipc; 65 64 int lbs_msg_msg; 66 65 int lbs_task; 66 + int lbs_xattr_count; /* number of xattr slots in new_xattrs array */ 67 67 }; 68 + 69 + /** 70 + * lsm_get_xattr_slot - Return the next available slot and increment the index 71 + * @xattrs: array storing LSM-provided xattrs 72 + * @xattr_count: number of already stored xattrs (updated) 73 + * 74 + * Retrieve the first available slot in the @xattrs array to fill with an xattr, 75 + * and increment @xattr_count. 76 + * 77 + * Return: The slot to fill in @xattrs if non-NULL, NULL otherwise. 78 + */ 79 + static inline struct xattr *lsm_get_xattr_slot(struct xattr *xattrs, 80 + int *xattr_count) 81 + { 82 + if (unlikely(!xattrs)) 83 + return NULL; 84 + return &xattrs[(*xattr_count)++]; 85 + } 68 86 69 87 /* 70 88 * LSM_RET_VOID is used as the default value in LSM_HOOK definitions for void
+49 -24
security/security.c
··· 31 31 #include <linux/msg.h> 32 32 #include <net/flow.h> 33 33 34 - #define MAX_LSM_EVM_XATTR 2 35 - 36 34 /* How many LSMs were built into the kernel? */ 37 35 #define LSM_COUNT (__end_lsm_info - __start_lsm_info) 38 36 ··· 210 212 lsm_set_blob_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg); 211 213 lsm_set_blob_size(&needed->lbs_superblock, &blob_sizes.lbs_superblock); 212 214 lsm_set_blob_size(&needed->lbs_task, &blob_sizes.lbs_task); 215 + lsm_set_blob_size(&needed->lbs_xattr_count, 216 + &blob_sizes.lbs_xattr_count); 213 217 } 214 218 215 219 /* Prepare LSM for initialization. */ ··· 378 378 init_debug("msg_msg blob size = %d\n", blob_sizes.lbs_msg_msg); 379 379 init_debug("superblock blob size = %d\n", blob_sizes.lbs_superblock); 380 380 init_debug("task blob size = %d\n", blob_sizes.lbs_task); 381 + init_debug("xattr slots = %d\n", blob_sizes.lbs_xattr_count); 381 382 382 383 /* 383 384 * Create any kmem_caches needed for blobs ··· 1592 1591 * created inode and set up the incore security field for the new inode. This 1593 1592 * hook is called by the fs code as part of the inode creation transaction and 1594 1593 * provides for atomic labeling of the inode, unlike the post_create/mkdir/... 1595 - * hooks called by the VFS. The hook function is expected to allocate the name 1596 - * and value via kmalloc, with the caller being responsible for calling kfree 1597 - * after using them. If the security module does not use security attributes 1598 - * or does not wish to put a security attribute on this particular inode, then 1599 - * it should return -EOPNOTSUPP to skip this processing. 1594 + * hooks called by the VFS. 1595 + * 1596 + * The hook function is expected to populate the xattrs array, by calling 1597 + * lsm_get_xattr_slot() to retrieve the slots reserved by the security module 1598 + * with the lbs_xattr_count field of the lsm_blob_sizes structure. For each 1599 + * slot, the hook function should set ->name to the attribute name suffix 1600 + * (e.g. selinux), to allocate ->value (will be freed by the caller) and set it 1601 + * to the attribute value, to set ->value_len to the length of the value. If 1602 + * the security module does not use security attributes or does not wish to put 1603 + * a security attribute on this particular inode, then it should return 1604 + * -EOPNOTSUPP to skip this processing. 1600 1605 * 1601 1606 * Return: Returns 0 on success, -EOPNOTSUPP if no security attribute is 1602 1607 * needed, or -ENOMEM on memory allocation failure. ··· 1611 1604 const struct qstr *qstr, 1612 1605 const initxattrs initxattrs, void *fs_data) 1613 1606 { 1614 - struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1]; 1615 - struct xattr *lsm_xattr, *evm_xattr, *xattr; 1616 - int ret; 1607 + struct security_hook_list *hp; 1608 + struct xattr *new_xattrs = NULL; 1609 + int ret = -EOPNOTSUPP, xattr_count = 0; 1617 1610 1618 1611 if (unlikely(IS_PRIVATE(inode))) 1619 1612 return 0; 1620 1613 1621 - if (!initxattrs) 1622 - return call_int_hook(inode_init_security, -EOPNOTSUPP, inode, 1623 - dir, qstr, NULL, NULL, NULL); 1624 - memset(new_xattrs, 0, sizeof(new_xattrs)); 1625 - lsm_xattr = new_xattrs; 1626 - ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr, 1627 - &lsm_xattr->name, 1628 - &lsm_xattr->value, 1629 - &lsm_xattr->value_len); 1630 - if (ret) 1614 + if (!blob_sizes.lbs_xattr_count) 1615 + return 0; 1616 + 1617 + if (initxattrs) { 1618 + /* Allocate +1 for EVM and +1 as terminator. */ 1619 + new_xattrs = kcalloc(blob_sizes.lbs_xattr_count + 2, 1620 + sizeof(*new_xattrs), GFP_NOFS); 1621 + if (!new_xattrs) 1622 + return -ENOMEM; 1623 + } 1624 + 1625 + hlist_for_each_entry(hp, &security_hook_heads.inode_init_security, 1626 + list) { 1627 + ret = hp->hook.inode_init_security(inode, dir, qstr, new_xattrs, 1628 + &xattr_count); 1629 + if (ret && ret != -EOPNOTSUPP) 1630 + goto out; 1631 + /* 1632 + * As documented in lsm_hooks.h, -EOPNOTSUPP in this context 1633 + * means that the LSM is not willing to provide an xattr, not 1634 + * that it wants to signal an error. Thus, continue to invoke 1635 + * the remaining LSMs. 1636 + */ 1637 + } 1638 + 1639 + /* If initxattrs() is NULL, xattr_count is zero, skip the call. */ 1640 + if (!xattr_count) 1631 1641 goto out; 1632 1642 1633 - evm_xattr = lsm_xattr + 1; 1634 - ret = evm_inode_init_security(inode, lsm_xattr, evm_xattr); 1643 + ret = evm_inode_init_security(inode, new_xattrs, 1644 + &new_xattrs[xattr_count]); 1635 1645 if (ret) 1636 1646 goto out; 1637 1647 ret = initxattrs(inode, new_xattrs, fs_data); 1638 1648 out: 1639 - for (xattr = new_xattrs; xattr->value != NULL; xattr++) 1640 - kfree(xattr->value); 1649 + for (; xattr_count > 0; xattr_count--) 1650 + kfree(new_xattrs[xattr_count - 1].value); 1651 + kfree(new_xattrs); 1641 1652 return (ret == -EOPNOTSUPP) ? 0 : ret; 1642 1653 } 1643 1654 EXPORT_SYMBOL(security_inode_init_security);
+9 -8
security/selinux/hooks.c
··· 104 104 #include "audit.h" 105 105 #include "avc_ss.h" 106 106 107 + #define SELINUX_INODE_INIT_XATTRS 1 108 + 107 109 struct selinux_state selinux_state; 108 110 109 111 /* SECMARK reference count */ ··· 2849 2847 2850 2848 static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 2851 2849 const struct qstr *qstr, 2852 - const char **name, 2853 - void **value, size_t *len) 2850 + struct xattr *xattrs, int *xattr_count) 2854 2851 { 2855 2852 const struct task_security_struct *tsec = selinux_cred(current_cred()); 2856 2853 struct superblock_security_struct *sbsec; 2854 + struct xattr *xattr = lsm_get_xattr_slot(xattrs, xattr_count); 2857 2855 u32 newsid, clen; 2858 2856 int rc; 2859 2857 char *context; ··· 2880 2878 !(sbsec->flags & SBLABEL_MNT)) 2881 2879 return -EOPNOTSUPP; 2882 2880 2883 - if (name) 2884 - *name = XATTR_SELINUX_SUFFIX; 2885 - 2886 - if (value && len) { 2881 + if (xattr) { 2887 2882 rc = security_sid_to_context_force(newsid, 2888 2883 &context, &clen); 2889 2884 if (rc) 2890 2885 return rc; 2891 - *value = context; 2892 - *len = clen; 2886 + xattr->value = context; 2887 + xattr->value_len = clen; 2888 + xattr->name = XATTR_SELINUX_SUFFIX; 2893 2889 } 2894 2890 2895 2891 return 0; ··· 6815 6815 .lbs_ipc = sizeof(struct ipc_security_struct), 6816 6816 .lbs_msg_msg = sizeof(struct msg_security_struct), 6817 6817 .lbs_superblock = sizeof(struct superblock_security_struct), 6818 + .lbs_xattr_count = SELINUX_INODE_INIT_XATTRS, 6818 6819 }; 6819 6820 6820 6821 #ifdef CONFIG_PERF_EVENTS
+13 -12
security/smack/smack_lsm.c
··· 52 52 #define SMK_RECEIVING 1 53 53 #define SMK_SENDING 2 54 54 55 + #define SMACK_INODE_INIT_XATTRS 1 56 + 55 57 #ifdef SMACK_IPV6_PORT_LABELING 56 58 static DEFINE_MUTEX(smack_ipv6_lock); 57 59 static LIST_HEAD(smk_ipv6_port_list); ··· 925 923 * @inode: the newly created inode 926 924 * @dir: containing directory object 927 925 * @qstr: unused 928 - * @name: where to put the attribute name 929 - * @value: where to put the attribute value 930 - * @len: where to put the length of the attribute 926 + * @xattrs: where to put the attributes 927 + * @xattr_count: current number of LSM-provided xattrs (updated) 931 928 * 932 929 * Returns 0 if it all works out, -ENOMEM if there's no memory 933 930 */ 934 931 static int smack_inode_init_security(struct inode *inode, struct inode *dir, 935 - const struct qstr *qstr, const char **name, 936 - void **value, size_t *len) 932 + const struct qstr *qstr, 933 + struct xattr *xattrs, int *xattr_count) 937 934 { 938 935 struct task_smack *tsp = smack_cred(current_cred()); 939 936 struct inode_smack *issp = smack_inode(inode); 940 937 struct smack_known *skp = smk_of_task(tsp); 941 938 struct smack_known *isp = smk_of_inode(inode); 942 939 struct smack_known *dsp = smk_of_inode(dir); 940 + struct xattr *xattr = lsm_get_xattr_slot(xattrs, xattr_count); 943 941 int may; 944 942 945 - if (name) 946 - *name = XATTR_SMACK_SUFFIX; 947 - 948 - if (value && len) { 943 + if (xattr) { 949 944 /* 950 945 * If equal, transmuting already occurred in 951 946 * smack_dentry_create_files_as(). No need to check again. ··· 974 975 issp->smk_flags |= SMK_INODE_CHANGED; 975 976 } 976 977 977 - *value = kstrdup(isp->smk_known, GFP_NOFS); 978 - if (*value == NULL) 978 + xattr->value = kstrdup(isp->smk_known, GFP_NOFS); 979 + if (!xattr->value) 979 980 return -ENOMEM; 980 981 981 - *len = strlen(isp->smk_known); 982 + xattr->value_len = strlen(isp->smk_known); 983 + xattr->name = XATTR_SMACK_SUFFIX; 982 984 } 983 985 984 986 return 0; ··· 4869 4869 .lbs_ipc = sizeof(struct smack_known *), 4870 4870 .lbs_msg_msg = sizeof(struct smack_known *), 4871 4871 .lbs_superblock = sizeof(struct superblock_smack), 4872 + .lbs_xattr_count = SMACK_INODE_INIT_XATTRS, 4872 4873 }; 4873 4874 4874 4875 static struct security_hook_list smack_hooks[] __ro_after_init = {