Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2005-2010 IBM Corporation
0004  *
0005  * Author:
0006  * Mimi Zohar <zohar@us.ibm.com>
0007  * Kylene Hall <kjhall@us.ibm.com>
0008  *
0009  * File: evm_main.c
0010  *  implements evm_inode_setxattr, evm_inode_post_setxattr,
0011  *  evm_inode_removexattr, and evm_verifyxattr
0012  */
0013 
0014 #define pr_fmt(fmt) "EVM: "fmt
0015 
0016 #include <linux/init.h>
0017 #include <linux/crypto.h>
0018 #include <linux/audit.h>
0019 #include <linux/xattr.h>
0020 #include <linux/integrity.h>
0021 #include <linux/evm.h>
0022 #include <linux/magic.h>
0023 #include <linux/posix_acl_xattr.h>
0024 
0025 #include <crypto/hash.h>
0026 #include <crypto/hash_info.h>
0027 #include <crypto/algapi.h>
0028 #include "evm.h"
0029 
0030 int evm_initialized;
0031 
0032 static const char * const integrity_status_msg[] = {
0033     "pass", "pass_immutable", "fail", "fail_immutable", "no_label",
0034     "no_xattrs", "unknown"
0035 };
0036 int evm_hmac_attrs;
0037 
0038 static struct xattr_list evm_config_default_xattrnames[] = {
0039     {
0040      .name = XATTR_NAME_SELINUX,
0041      .enabled = IS_ENABLED(CONFIG_SECURITY_SELINUX)
0042     },
0043     {
0044      .name = XATTR_NAME_SMACK,
0045      .enabled = IS_ENABLED(CONFIG_SECURITY_SMACK)
0046     },
0047     {
0048      .name = XATTR_NAME_SMACKEXEC,
0049      .enabled = IS_ENABLED(CONFIG_EVM_EXTRA_SMACK_XATTRS)
0050     },
0051     {
0052      .name = XATTR_NAME_SMACKTRANSMUTE,
0053      .enabled = IS_ENABLED(CONFIG_EVM_EXTRA_SMACK_XATTRS)
0054     },
0055     {
0056      .name = XATTR_NAME_SMACKMMAP,
0057      .enabled = IS_ENABLED(CONFIG_EVM_EXTRA_SMACK_XATTRS)
0058     },
0059     {
0060      .name = XATTR_NAME_APPARMOR,
0061      .enabled = IS_ENABLED(CONFIG_SECURITY_APPARMOR)
0062     },
0063     {
0064      .name = XATTR_NAME_IMA,
0065      .enabled = IS_ENABLED(CONFIG_IMA_APPRAISE)
0066     },
0067     {
0068      .name = XATTR_NAME_CAPS,
0069      .enabled = true
0070     },
0071 };
0072 
0073 LIST_HEAD(evm_config_xattrnames);
0074 
0075 static int evm_fixmode __ro_after_init;
0076 static int __init evm_set_fixmode(char *str)
0077 {
0078     if (strncmp(str, "fix", 3) == 0)
0079         evm_fixmode = 1;
0080     else
0081         pr_err("invalid \"%s\" mode", str);
0082 
0083     return 1;
0084 }
0085 __setup("evm=", evm_set_fixmode);
0086 
0087 static void __init evm_init_config(void)
0088 {
0089     int i, xattrs;
0090 
0091     xattrs = ARRAY_SIZE(evm_config_default_xattrnames);
0092 
0093     pr_info("Initialising EVM extended attributes:\n");
0094     for (i = 0; i < xattrs; i++) {
0095         pr_info("%s%s\n", evm_config_default_xattrnames[i].name,
0096             !evm_config_default_xattrnames[i].enabled ?
0097             " (disabled)" : "");
0098         list_add_tail(&evm_config_default_xattrnames[i].list,
0099                   &evm_config_xattrnames);
0100     }
0101 
0102 #ifdef CONFIG_EVM_ATTR_FSUUID
0103     evm_hmac_attrs |= EVM_ATTR_FSUUID;
0104 #endif
0105     pr_info("HMAC attrs: 0x%x\n", evm_hmac_attrs);
0106 }
0107 
0108 static bool evm_key_loaded(void)
0109 {
0110     return (bool)(evm_initialized & EVM_KEY_MASK);
0111 }
0112 
0113 /*
0114  * This function determines whether or not it is safe to ignore verification
0115  * errors, based on the ability of EVM to calculate HMACs. If the HMAC key
0116  * is not loaded, and it cannot be loaded in the future due to the
0117  * EVM_SETUP_COMPLETE initialization flag, allowing an operation despite the
0118  * attrs/xattrs being found invalid will not make them valid.
0119  */
0120 static bool evm_hmac_disabled(void)
0121 {
0122     if (evm_initialized & EVM_INIT_HMAC)
0123         return false;
0124 
0125     if (!(evm_initialized & EVM_SETUP_COMPLETE))
0126         return false;
0127 
0128     return true;
0129 }
0130 
0131 static int evm_find_protected_xattrs(struct dentry *dentry)
0132 {
0133     struct inode *inode = d_backing_inode(dentry);
0134     struct xattr_list *xattr;
0135     int error;
0136     int count = 0;
0137 
0138     if (!(inode->i_opflags & IOP_XATTR))
0139         return -EOPNOTSUPP;
0140 
0141     list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
0142         error = __vfs_getxattr(dentry, inode, xattr->name, NULL, 0);
0143         if (error < 0) {
0144             if (error == -ENODATA)
0145                 continue;
0146             return error;
0147         }
0148         count++;
0149     }
0150 
0151     return count;
0152 }
0153 
0154 /*
0155  * evm_verify_hmac - calculate and compare the HMAC with the EVM xattr
0156  *
0157  * Compute the HMAC on the dentry's protected set of extended attributes
0158  * and compare it against the stored security.evm xattr.
0159  *
0160  * For performance:
0161  * - use the previoulsy retrieved xattr value and length to calculate the
0162  *   HMAC.)
0163  * - cache the verification result in the iint, when available.
0164  *
0165  * Returns integrity status
0166  */
0167 static enum integrity_status evm_verify_hmac(struct dentry *dentry,
0168                          const char *xattr_name,
0169                          char *xattr_value,
0170                          size_t xattr_value_len,
0171                          struct integrity_iint_cache *iint)
0172 {
0173     struct evm_ima_xattr_data *xattr_data = NULL;
0174     struct signature_v2_hdr *hdr;
0175     enum integrity_status evm_status = INTEGRITY_PASS;
0176     struct evm_digest digest;
0177     struct inode *inode;
0178     int rc, xattr_len, evm_immutable = 0;
0179 
0180     if (iint && (iint->evm_status == INTEGRITY_PASS ||
0181              iint->evm_status == INTEGRITY_PASS_IMMUTABLE))
0182         return iint->evm_status;
0183 
0184     /* if status is not PASS, try to check again - against -ENOMEM */
0185 
0186     /* first need to know the sig type */
0187     rc = vfs_getxattr_alloc(&init_user_ns, dentry, XATTR_NAME_EVM,
0188                 (char **)&xattr_data, 0, GFP_NOFS);
0189     if (rc <= 0) {
0190         evm_status = INTEGRITY_FAIL;
0191         if (rc == -ENODATA) {
0192             rc = evm_find_protected_xattrs(dentry);
0193             if (rc > 0)
0194                 evm_status = INTEGRITY_NOLABEL;
0195             else if (rc == 0)
0196                 evm_status = INTEGRITY_NOXATTRS; /* new file */
0197         } else if (rc == -EOPNOTSUPP) {
0198             evm_status = INTEGRITY_UNKNOWN;
0199         }
0200         goto out;
0201     }
0202 
0203     xattr_len = rc;
0204 
0205     /* check value type */
0206     switch (xattr_data->type) {
0207     case EVM_XATTR_HMAC:
0208         if (xattr_len != sizeof(struct evm_xattr)) {
0209             evm_status = INTEGRITY_FAIL;
0210             goto out;
0211         }
0212 
0213         digest.hdr.algo = HASH_ALGO_SHA1;
0214         rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
0215                    xattr_value_len, &digest);
0216         if (rc)
0217             break;
0218         rc = crypto_memneq(xattr_data->data, digest.digest,
0219                    SHA1_DIGEST_SIZE);
0220         if (rc)
0221             rc = -EINVAL;
0222         break;
0223     case EVM_XATTR_PORTABLE_DIGSIG:
0224         evm_immutable = 1;
0225         fallthrough;
0226     case EVM_IMA_XATTR_DIGSIG:
0227         /* accept xattr with non-empty signature field */
0228         if (xattr_len <= sizeof(struct signature_v2_hdr)) {
0229             evm_status = INTEGRITY_FAIL;
0230             goto out;
0231         }
0232 
0233         hdr = (struct signature_v2_hdr *)xattr_data;
0234         digest.hdr.algo = hdr->hash_algo;
0235         rc = evm_calc_hash(dentry, xattr_name, xattr_value,
0236                    xattr_value_len, xattr_data->type, &digest);
0237         if (rc)
0238             break;
0239         rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
0240                     (const char *)xattr_data, xattr_len,
0241                     digest.digest, digest.hdr.length);
0242         if (!rc) {
0243             inode = d_backing_inode(dentry);
0244 
0245             if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG) {
0246                 if (iint)
0247                     iint->flags |= EVM_IMMUTABLE_DIGSIG;
0248                 evm_status = INTEGRITY_PASS_IMMUTABLE;
0249             } else if (!IS_RDONLY(inode) &&
0250                    !(inode->i_sb->s_readonly_remount) &&
0251                    !IS_IMMUTABLE(inode)) {
0252                 evm_update_evmxattr(dentry, xattr_name,
0253                             xattr_value,
0254                             xattr_value_len);
0255             }
0256         }
0257         break;
0258     default:
0259         rc = -EINVAL;
0260         break;
0261     }
0262 
0263     if (rc) {
0264         if (rc == -ENODATA)
0265             evm_status = INTEGRITY_NOXATTRS;
0266         else if (evm_immutable)
0267             evm_status = INTEGRITY_FAIL_IMMUTABLE;
0268         else
0269             evm_status = INTEGRITY_FAIL;
0270     }
0271     pr_debug("digest: (%d) [%*phN]\n", digest.hdr.length, digest.hdr.length,
0272           digest.digest);
0273 out:
0274     if (iint)
0275         iint->evm_status = evm_status;
0276     kfree(xattr_data);
0277     return evm_status;
0278 }
0279 
0280 static int evm_protected_xattr_common(const char *req_xattr_name,
0281                       bool all_xattrs)
0282 {
0283     int namelen;
0284     int found = 0;
0285     struct xattr_list *xattr;
0286 
0287     namelen = strlen(req_xattr_name);
0288     list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
0289         if (!all_xattrs && !xattr->enabled)
0290             continue;
0291 
0292         if ((strlen(xattr->name) == namelen)
0293             && (strncmp(req_xattr_name, xattr->name, namelen) == 0)) {
0294             found = 1;
0295             break;
0296         }
0297         if (strncmp(req_xattr_name,
0298                 xattr->name + XATTR_SECURITY_PREFIX_LEN,
0299                 strlen(req_xattr_name)) == 0) {
0300             found = 1;
0301             break;
0302         }
0303     }
0304 
0305     return found;
0306 }
0307 
0308 static int evm_protected_xattr(const char *req_xattr_name)
0309 {
0310     return evm_protected_xattr_common(req_xattr_name, false);
0311 }
0312 
0313 int evm_protected_xattr_if_enabled(const char *req_xattr_name)
0314 {
0315     return evm_protected_xattr_common(req_xattr_name, true);
0316 }
0317 
0318 /**
0319  * evm_read_protected_xattrs - read EVM protected xattr names, lengths, values
0320  * @dentry: dentry of the read xattrs
0321  * @inode: inode of the read xattrs
0322  * @buffer: buffer xattr names, lengths or values are copied to
0323  * @buffer_size: size of buffer
0324  * @type: n: names, l: lengths, v: values
0325  * @canonical_fmt: data format (true: little endian, false: native format)
0326  *
0327  * Read protected xattr names (separated by |), lengths (u32) or values for a
0328  * given dentry and return the total size of copied data. If buffer is NULL,
0329  * just return the total size.
0330  *
0331  * Returns the total size on success, a negative value on error.
0332  */
0333 int evm_read_protected_xattrs(struct dentry *dentry, u8 *buffer,
0334                   int buffer_size, char type, bool canonical_fmt)
0335 {
0336     struct xattr_list *xattr;
0337     int rc, size, total_size = 0;
0338 
0339     list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
0340         rc = __vfs_getxattr(dentry, d_backing_inode(dentry),
0341                     xattr->name, NULL, 0);
0342         if (rc < 0 && rc == -ENODATA)
0343             continue;
0344         else if (rc < 0)
0345             return rc;
0346 
0347         switch (type) {
0348         case 'n':
0349             size = strlen(xattr->name) + 1;
0350             if (buffer) {
0351                 if (total_size)
0352                     *(buffer + total_size - 1) = '|';
0353 
0354                 memcpy(buffer + total_size, xattr->name, size);
0355             }
0356             break;
0357         case 'l':
0358             size = sizeof(u32);
0359             if (buffer) {
0360                 if (canonical_fmt)
0361                     rc = (__force int)cpu_to_le32(rc);
0362 
0363                 *(u32 *)(buffer + total_size) = rc;
0364             }
0365             break;
0366         case 'v':
0367             size = rc;
0368             if (buffer) {
0369                 rc = __vfs_getxattr(dentry,
0370                     d_backing_inode(dentry), xattr->name,
0371                     buffer + total_size,
0372                     buffer_size - total_size);
0373                 if (rc < 0)
0374                     return rc;
0375             }
0376             break;
0377         default:
0378             return -EINVAL;
0379         }
0380 
0381         total_size += size;
0382     }
0383 
0384     return total_size;
0385 }
0386 
0387 /**
0388  * evm_verifyxattr - verify the integrity of the requested xattr
0389  * @dentry: object of the verify xattr
0390  * @xattr_name: requested xattr
0391  * @xattr_value: requested xattr value
0392  * @xattr_value_len: requested xattr value length
0393  *
0394  * Calculate the HMAC for the given dentry and verify it against the stored
0395  * security.evm xattr. For performance, use the xattr value and length
0396  * previously retrieved to calculate the HMAC.
0397  *
0398  * Returns the xattr integrity status.
0399  *
0400  * This function requires the caller to lock the inode's i_mutex before it
0401  * is executed.
0402  */
0403 enum integrity_status evm_verifyxattr(struct dentry *dentry,
0404                       const char *xattr_name,
0405                       void *xattr_value, size_t xattr_value_len,
0406                       struct integrity_iint_cache *iint)
0407 {
0408     if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
0409         return INTEGRITY_UNKNOWN;
0410 
0411     if (!iint) {
0412         iint = integrity_iint_find(d_backing_inode(dentry));
0413         if (!iint)
0414             return INTEGRITY_UNKNOWN;
0415     }
0416     return evm_verify_hmac(dentry, xattr_name, xattr_value,
0417                  xattr_value_len, iint);
0418 }
0419 EXPORT_SYMBOL_GPL(evm_verifyxattr);
0420 
0421 /*
0422  * evm_verify_current_integrity - verify the dentry's metadata integrity
0423  * @dentry: pointer to the affected dentry
0424  *
0425  * Verify and return the dentry's metadata integrity. The exceptions are
0426  * before EVM is initialized or in 'fix' mode.
0427  */
0428 static enum integrity_status evm_verify_current_integrity(struct dentry *dentry)
0429 {
0430     struct inode *inode = d_backing_inode(dentry);
0431 
0432     if (!evm_key_loaded() || !S_ISREG(inode->i_mode) || evm_fixmode)
0433         return INTEGRITY_PASS;
0434     return evm_verify_hmac(dentry, NULL, NULL, 0, NULL);
0435 }
0436 
0437 /*
0438  * evm_xattr_acl_change - check if passed ACL changes the inode mode
0439  * @mnt_userns: user namespace of the idmapped mount
0440  * @dentry: pointer to the affected dentry
0441  * @xattr_name: requested xattr
0442  * @xattr_value: requested xattr value
0443  * @xattr_value_len: requested xattr value length
0444  *
0445  * Check if passed ACL changes the inode mode, which is protected by EVM.
0446  *
0447  * Returns 1 if passed ACL causes inode mode change, 0 otherwise.
0448  */
0449 static int evm_xattr_acl_change(struct user_namespace *mnt_userns,
0450                 struct dentry *dentry, const char *xattr_name,
0451                 const void *xattr_value, size_t xattr_value_len)
0452 {
0453 #ifdef CONFIG_FS_POSIX_ACL
0454     umode_t mode;
0455     struct posix_acl *acl = NULL, *acl_res;
0456     struct inode *inode = d_backing_inode(dentry);
0457     int rc;
0458 
0459     /*
0460      * user_ns is not relevant here, ACL_USER/ACL_GROUP don't have impact
0461      * on the inode mode (see posix_acl_equiv_mode()).
0462      */
0463     acl = posix_acl_from_xattr(&init_user_ns, xattr_value, xattr_value_len);
0464     if (IS_ERR_OR_NULL(acl))
0465         return 1;
0466 
0467     acl_res = acl;
0468     /*
0469      * Passing mnt_userns is necessary to correctly determine the GID in
0470      * an idmapped mount, as the GID is used to clear the setgid bit in
0471      * the inode mode.
0472      */
0473     rc = posix_acl_update_mode(mnt_userns, inode, &mode, &acl_res);
0474 
0475     posix_acl_release(acl);
0476 
0477     if (rc)
0478         return 1;
0479 
0480     if (inode->i_mode != mode)
0481         return 1;
0482 #endif
0483     return 0;
0484 }
0485 
0486 /*
0487  * evm_xattr_change - check if passed xattr value differs from current value
0488  * @mnt_userns: user namespace of the idmapped mount
0489  * @dentry: pointer to the affected dentry
0490  * @xattr_name: requested xattr
0491  * @xattr_value: requested xattr value
0492  * @xattr_value_len: requested xattr value length
0493  *
0494  * Check if passed xattr value differs from current value.
0495  *
0496  * Returns 1 if passed xattr value differs from current value, 0 otherwise.
0497  */
0498 static int evm_xattr_change(struct user_namespace *mnt_userns,
0499                 struct dentry *dentry, const char *xattr_name,
0500                 const void *xattr_value, size_t xattr_value_len)
0501 {
0502     char *xattr_data = NULL;
0503     int rc = 0;
0504 
0505     if (posix_xattr_acl(xattr_name))
0506         return evm_xattr_acl_change(mnt_userns, dentry, xattr_name,
0507                         xattr_value, xattr_value_len);
0508 
0509     rc = vfs_getxattr_alloc(&init_user_ns, dentry, xattr_name, &xattr_data,
0510                 0, GFP_NOFS);
0511     if (rc < 0)
0512         return 1;
0513 
0514     if (rc == xattr_value_len)
0515         rc = !!memcmp(xattr_value, xattr_data, rc);
0516     else
0517         rc = 1;
0518 
0519     kfree(xattr_data);
0520     return rc;
0521 }
0522 
0523 /*
0524  * evm_protect_xattr - protect the EVM extended attribute
0525  *
0526  * Prevent security.evm from being modified or removed without the
0527  * necessary permissions or when the existing value is invalid.
0528  *
0529  * The posix xattr acls are 'system' prefixed, which normally would not
0530  * affect security.evm.  An interesting side affect of writing posix xattr
0531  * acls is their modifying of the i_mode, which is included in security.evm.
0532  * For posix xattr acls only, permit security.evm, even if it currently
0533  * doesn't exist, to be updated unless the EVM signature is immutable.
0534  */
0535 static int evm_protect_xattr(struct user_namespace *mnt_userns,
0536                  struct dentry *dentry, const char *xattr_name,
0537                  const void *xattr_value, size_t xattr_value_len)
0538 {
0539     enum integrity_status evm_status;
0540 
0541     if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
0542         if (!capable(CAP_SYS_ADMIN))
0543             return -EPERM;
0544     } else if (!evm_protected_xattr(xattr_name)) {
0545         if (!posix_xattr_acl(xattr_name))
0546             return 0;
0547         evm_status = evm_verify_current_integrity(dentry);
0548         if ((evm_status == INTEGRITY_PASS) ||
0549             (evm_status == INTEGRITY_NOXATTRS))
0550             return 0;
0551         goto out;
0552     }
0553 
0554     evm_status = evm_verify_current_integrity(dentry);
0555     if (evm_status == INTEGRITY_NOXATTRS) {
0556         struct integrity_iint_cache *iint;
0557 
0558         /* Exception if the HMAC is not going to be calculated. */
0559         if (evm_hmac_disabled())
0560             return 0;
0561 
0562         iint = integrity_iint_find(d_backing_inode(dentry));
0563         if (iint && (iint->flags & IMA_NEW_FILE))
0564             return 0;
0565 
0566         /* exception for pseudo filesystems */
0567         if (dentry->d_sb->s_magic == TMPFS_MAGIC
0568             || dentry->d_sb->s_magic == SYSFS_MAGIC)
0569             return 0;
0570 
0571         integrity_audit_msg(AUDIT_INTEGRITY_METADATA,
0572                     dentry->d_inode, dentry->d_name.name,
0573                     "update_metadata",
0574                     integrity_status_msg[evm_status],
0575                     -EPERM, 0);
0576     }
0577 out:
0578     /* Exception if the HMAC is not going to be calculated. */
0579     if (evm_hmac_disabled() && (evm_status == INTEGRITY_NOLABEL ||
0580         evm_status == INTEGRITY_UNKNOWN))
0581         return 0;
0582 
0583     /*
0584      * Writing other xattrs is safe for portable signatures, as portable
0585      * signatures are immutable and can never be updated.
0586      */
0587     if (evm_status == INTEGRITY_FAIL_IMMUTABLE)
0588         return 0;
0589 
0590     if (evm_status == INTEGRITY_PASS_IMMUTABLE &&
0591         !evm_xattr_change(mnt_userns, dentry, xattr_name, xattr_value,
0592                   xattr_value_len))
0593         return 0;
0594 
0595     if (evm_status != INTEGRITY_PASS &&
0596         evm_status != INTEGRITY_PASS_IMMUTABLE)
0597         integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
0598                     dentry->d_name.name, "appraise_metadata",
0599                     integrity_status_msg[evm_status],
0600                     -EPERM, 0);
0601     return evm_status == INTEGRITY_PASS ? 0 : -EPERM;
0602 }
0603 
0604 /**
0605  * evm_inode_setxattr - protect the EVM extended attribute
0606  * @mnt_userns: user namespace of the idmapped mount
0607  * @dentry: pointer to the affected dentry
0608  * @xattr_name: pointer to the affected extended attribute name
0609  * @xattr_value: pointer to the new extended attribute value
0610  * @xattr_value_len: pointer to the new extended attribute value length
0611  *
0612  * Before allowing the 'security.evm' protected xattr to be updated,
0613  * verify the existing value is valid.  As only the kernel should have
0614  * access to the EVM encrypted key needed to calculate the HMAC, prevent
0615  * userspace from writing HMAC value.  Writing 'security.evm' requires
0616  * requires CAP_SYS_ADMIN privileges.
0617  */
0618 int evm_inode_setxattr(struct user_namespace *mnt_userns, struct dentry *dentry,
0619                const char *xattr_name, const void *xattr_value,
0620                size_t xattr_value_len)
0621 {
0622     const struct evm_ima_xattr_data *xattr_data = xattr_value;
0623 
0624     /* Policy permits modification of the protected xattrs even though
0625      * there's no HMAC key loaded
0626      */
0627     if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
0628         return 0;
0629 
0630     if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
0631         if (!xattr_value_len)
0632             return -EINVAL;
0633         if (xattr_data->type != EVM_IMA_XATTR_DIGSIG &&
0634             xattr_data->type != EVM_XATTR_PORTABLE_DIGSIG)
0635             return -EPERM;
0636     }
0637     return evm_protect_xattr(mnt_userns, dentry, xattr_name, xattr_value,
0638                  xattr_value_len);
0639 }
0640 
0641 /**
0642  * evm_inode_removexattr - protect the EVM extended attribute
0643  * @mnt_userns: user namespace of the idmapped mount
0644  * @dentry: pointer to the affected dentry
0645  * @xattr_name: pointer to the affected extended attribute name
0646  *
0647  * Removing 'security.evm' requires CAP_SYS_ADMIN privileges and that
0648  * the current value is valid.
0649  */
0650 int evm_inode_removexattr(struct user_namespace *mnt_userns,
0651               struct dentry *dentry, const char *xattr_name)
0652 {
0653     /* Policy permits modification of the protected xattrs even though
0654      * there's no HMAC key loaded
0655      */
0656     if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
0657         return 0;
0658 
0659     return evm_protect_xattr(mnt_userns, dentry, xattr_name, NULL, 0);
0660 }
0661 
0662 static void evm_reset_status(struct inode *inode)
0663 {
0664     struct integrity_iint_cache *iint;
0665 
0666     iint = integrity_iint_find(inode);
0667     if (iint)
0668         iint->evm_status = INTEGRITY_UNKNOWN;
0669 }
0670 
0671 /**
0672  * evm_revalidate_status - report whether EVM status re-validation is necessary
0673  * @xattr_name: pointer to the affected extended attribute name
0674  *
0675  * Report whether callers of evm_verifyxattr() should re-validate the
0676  * EVM status.
0677  *
0678  * Return true if re-validation is necessary, false otherwise.
0679  */
0680 bool evm_revalidate_status(const char *xattr_name)
0681 {
0682     if (!evm_key_loaded())
0683         return false;
0684 
0685     /* evm_inode_post_setattr() passes NULL */
0686     if (!xattr_name)
0687         return true;
0688 
0689     if (!evm_protected_xattr(xattr_name) && !posix_xattr_acl(xattr_name) &&
0690         strcmp(xattr_name, XATTR_NAME_EVM))
0691         return false;
0692 
0693     return true;
0694 }
0695 
0696 /**
0697  * evm_inode_post_setxattr - update 'security.evm' to reflect the changes
0698  * @dentry: pointer to the affected dentry
0699  * @xattr_name: pointer to the affected extended attribute name
0700  * @xattr_value: pointer to the new extended attribute value
0701  * @xattr_value_len: pointer to the new extended attribute value length
0702  *
0703  * Update the HMAC stored in 'security.evm' to reflect the change.
0704  *
0705  * No need to take the i_mutex lock here, as this function is called from
0706  * __vfs_setxattr_noperm().  The caller of which has taken the inode's
0707  * i_mutex lock.
0708  */
0709 void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
0710                  const void *xattr_value, size_t xattr_value_len)
0711 {
0712     if (!evm_revalidate_status(xattr_name))
0713         return;
0714 
0715     evm_reset_status(dentry->d_inode);
0716 
0717     if (!strcmp(xattr_name, XATTR_NAME_EVM))
0718         return;
0719 
0720     if (!(evm_initialized & EVM_INIT_HMAC))
0721         return;
0722 
0723     evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len);
0724 }
0725 
0726 /**
0727  * evm_inode_post_removexattr - update 'security.evm' after removing the xattr
0728  * @dentry: pointer to the affected dentry
0729  * @xattr_name: pointer to the affected extended attribute name
0730  *
0731  * Update the HMAC stored in 'security.evm' to reflect removal of the xattr.
0732  *
0733  * No need to take the i_mutex lock here, as this function is called from
0734  * vfs_removexattr() which takes the i_mutex.
0735  */
0736 void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
0737 {
0738     if (!evm_revalidate_status(xattr_name))
0739         return;
0740 
0741     evm_reset_status(dentry->d_inode);
0742 
0743     if (!strcmp(xattr_name, XATTR_NAME_EVM))
0744         return;
0745 
0746     if (!(evm_initialized & EVM_INIT_HMAC))
0747         return;
0748 
0749     evm_update_evmxattr(dentry, xattr_name, NULL, 0);
0750 }
0751 
0752 static int evm_attr_change(struct user_namespace *mnt_userns,
0753                struct dentry *dentry, struct iattr *attr)
0754 {
0755     struct inode *inode = d_backing_inode(dentry);
0756     unsigned int ia_valid = attr->ia_valid;
0757 
0758     if (!i_uid_needs_update(mnt_userns, attr, inode) &&
0759         !i_gid_needs_update(mnt_userns, attr, inode) &&
0760         (!(ia_valid & ATTR_MODE) || attr->ia_mode == inode->i_mode))
0761         return 0;
0762 
0763     return 1;
0764 }
0765 
0766 /**
0767  * evm_inode_setattr - prevent updating an invalid EVM extended attribute
0768  * @dentry: pointer to the affected dentry
0769  *
0770  * Permit update of file attributes when files have a valid EVM signature,
0771  * except in the case of them having an immutable portable signature.
0772  */
0773 int evm_inode_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
0774               struct iattr *attr)
0775 {
0776     unsigned int ia_valid = attr->ia_valid;
0777     enum integrity_status evm_status;
0778 
0779     /* Policy permits modification of the protected attrs even though
0780      * there's no HMAC key loaded
0781      */
0782     if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
0783         return 0;
0784 
0785     if (!(ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)))
0786         return 0;
0787     evm_status = evm_verify_current_integrity(dentry);
0788     /*
0789      * Writing attrs is safe for portable signatures, as portable signatures
0790      * are immutable and can never be updated.
0791      */
0792     if ((evm_status == INTEGRITY_PASS) ||
0793         (evm_status == INTEGRITY_NOXATTRS) ||
0794         (evm_status == INTEGRITY_FAIL_IMMUTABLE) ||
0795         (evm_hmac_disabled() && (evm_status == INTEGRITY_NOLABEL ||
0796          evm_status == INTEGRITY_UNKNOWN)))
0797         return 0;
0798 
0799     if (evm_status == INTEGRITY_PASS_IMMUTABLE &&
0800         !evm_attr_change(mnt_userns, dentry, attr))
0801         return 0;
0802 
0803     integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
0804                 dentry->d_name.name, "appraise_metadata",
0805                 integrity_status_msg[evm_status], -EPERM, 0);
0806     return -EPERM;
0807 }
0808 
0809 /**
0810  * evm_inode_post_setattr - update 'security.evm' after modifying metadata
0811  * @dentry: pointer to the affected dentry
0812  * @ia_valid: for the UID and GID status
0813  *
0814  * For now, update the HMAC stored in 'security.evm' to reflect UID/GID
0815  * changes.
0816  *
0817  * This function is called from notify_change(), which expects the caller
0818  * to lock the inode's i_mutex.
0819  */
0820 void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
0821 {
0822     if (!evm_revalidate_status(NULL))
0823         return;
0824 
0825     evm_reset_status(dentry->d_inode);
0826 
0827     if (!(evm_initialized & EVM_INIT_HMAC))
0828         return;
0829 
0830     if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
0831         evm_update_evmxattr(dentry, NULL, NULL, 0);
0832 }
0833 
0834 /*
0835  * evm_inode_init_security - initializes security.evm HMAC value
0836  */
0837 int evm_inode_init_security(struct inode *inode,
0838                  const struct xattr *lsm_xattr,
0839                  struct xattr *evm_xattr)
0840 {
0841     struct evm_xattr *xattr_data;
0842     int rc;
0843 
0844     if (!(evm_initialized & EVM_INIT_HMAC) ||
0845         !evm_protected_xattr(lsm_xattr->name))
0846         return 0;
0847 
0848     xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);
0849     if (!xattr_data)
0850         return -ENOMEM;
0851 
0852     xattr_data->data.type = EVM_XATTR_HMAC;
0853     rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest);
0854     if (rc < 0)
0855         goto out;
0856 
0857     evm_xattr->value = xattr_data;
0858     evm_xattr->value_len = sizeof(*xattr_data);
0859     evm_xattr->name = XATTR_EVM_SUFFIX;
0860     return 0;
0861 out:
0862     kfree(xattr_data);
0863     return rc;
0864 }
0865 EXPORT_SYMBOL_GPL(evm_inode_init_security);
0866 
0867 #ifdef CONFIG_EVM_LOAD_X509
0868 void __init evm_load_x509(void)
0869 {
0870     int rc;
0871 
0872     rc = integrity_load_x509(INTEGRITY_KEYRING_EVM, CONFIG_EVM_X509_PATH);
0873     if (!rc)
0874         evm_initialized |= EVM_INIT_X509;
0875 }
0876 #endif
0877 
0878 static int __init init_evm(void)
0879 {
0880     int error;
0881     struct list_head *pos, *q;
0882 
0883     evm_init_config();
0884 
0885     error = integrity_init_keyring(INTEGRITY_KEYRING_EVM);
0886     if (error)
0887         goto error;
0888 
0889     error = evm_init_secfs();
0890     if (error < 0) {
0891         pr_info("Error registering secfs\n");
0892         goto error;
0893     }
0894 
0895 error:
0896     if (error != 0) {
0897         if (!list_empty(&evm_config_xattrnames)) {
0898             list_for_each_safe(pos, q, &evm_config_xattrnames)
0899                 list_del(pos);
0900         }
0901     }
0902 
0903     return error;
0904 }
0905 
0906 late_initcall(init_evm);