Path: blob/master/security/integrity/ima/ima_appraise.c
26424 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Copyright (C) 2011 IBM Corporation3*4* Author:5* Mimi Zohar <[email protected]>6*/7#include <linux/module.h>8#include <linux/init.h>9#include <linux/file.h>10#include <linux/binfmts.h>11#include <linux/fs.h>12#include <linux/xattr.h>13#include <linux/magic.h>14#include <linux/ima.h>15#include <linux/evm.h>16#include <linux/fsverity.h>17#include <keys/system_keyring.h>18#include <uapi/linux/fsverity.h>1920#include "ima.h"2122#ifdef CONFIG_IMA_APPRAISE_BOOTPARAM23static char *ima_appraise_cmdline_default __initdata;24core_param(ima_appraise, ima_appraise_cmdline_default, charp, 0);2526void __init ima_appraise_parse_cmdline(void)27{28const char *str = ima_appraise_cmdline_default;29bool sb_state = arch_ima_get_secureboot();30int appraisal_state = ima_appraise;3132if (!str)33return;3435if (strncmp(str, "off", 3) == 0)36appraisal_state = 0;37else if (strncmp(str, "log", 3) == 0)38appraisal_state = IMA_APPRAISE_LOG;39else if (strncmp(str, "fix", 3) == 0)40appraisal_state = IMA_APPRAISE_FIX;41else if (strncmp(str, "enforce", 7) == 0)42appraisal_state = IMA_APPRAISE_ENFORCE;43else44pr_err("invalid \"%s\" appraise option", str);4546/* If appraisal state was changed, but secure boot is enabled,47* keep its default */48if (sb_state) {49if (!(appraisal_state & IMA_APPRAISE_ENFORCE))50pr_info("Secure boot enabled: ignoring ima_appraise=%s option",51str);52} else {53ima_appraise = appraisal_state;54}55}56#endif5758/*59* is_ima_appraise_enabled - return appraise status60*61* Only return enabled, if not in ima_appraise="fix" or "log" modes.62*/63bool is_ima_appraise_enabled(void)64{65return ima_appraise & IMA_APPRAISE_ENFORCE;66}6768/*69* ima_must_appraise - set appraise flag70*71* Return 1 to appraise or hash72*/73int ima_must_appraise(struct mnt_idmap *idmap, struct inode *inode,74int mask, enum ima_hooks func)75{76struct lsm_prop prop;7778if (!ima_appraise)79return 0;8081security_current_getlsmprop_subj(&prop);82return ima_match_policy(idmap, inode, current_cred(), &prop,83func, mask, IMA_APPRAISE | IMA_HASH, NULL,84NULL, NULL, NULL);85}8687static int ima_fix_xattr(struct dentry *dentry, struct ima_iint_cache *iint)88{89int rc, offset;90u8 algo = iint->ima_hash->algo;9192if (algo <= HASH_ALGO_SHA1) {93offset = 1;94iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;95} else {96offset = 0;97iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;98iint->ima_hash->xattr.ng.algo = algo;99}100rc = __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, XATTR_NAME_IMA,101&iint->ima_hash->xattr.data[offset],102(sizeof(iint->ima_hash->xattr) - offset) +103iint->ima_hash->length, 0);104return rc;105}106107/* Return specific func appraised cached result */108enum integrity_status ima_get_cache_status(struct ima_iint_cache *iint,109enum ima_hooks func)110{111switch (func) {112case MMAP_CHECK:113case MMAP_CHECK_REQPROT:114return iint->ima_mmap_status;115case BPRM_CHECK:116return iint->ima_bprm_status;117case CREDS_CHECK:118return iint->ima_creds_status;119case FILE_CHECK:120case POST_SETATTR:121return iint->ima_file_status;122case MODULE_CHECK ... MAX_CHECK - 1:123default:124return iint->ima_read_status;125}126}127128static void ima_set_cache_status(struct ima_iint_cache *iint,129enum ima_hooks func,130enum integrity_status status)131{132switch (func) {133case MMAP_CHECK:134case MMAP_CHECK_REQPROT:135iint->ima_mmap_status = status;136break;137case BPRM_CHECK:138iint->ima_bprm_status = status;139break;140case CREDS_CHECK:141iint->ima_creds_status = status;142break;143case FILE_CHECK:144case POST_SETATTR:145iint->ima_file_status = status;146break;147case MODULE_CHECK ... MAX_CHECK - 1:148default:149iint->ima_read_status = status;150break;151}152}153154static void ima_cache_flags(struct ima_iint_cache *iint, enum ima_hooks func)155{156switch (func) {157case MMAP_CHECK:158case MMAP_CHECK_REQPROT:159iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);160break;161case BPRM_CHECK:162iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);163break;164case CREDS_CHECK:165iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED);166break;167case FILE_CHECK:168case POST_SETATTR:169iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);170break;171case MODULE_CHECK ... MAX_CHECK - 1:172default:173iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);174break;175}176}177178enum hash_algo ima_get_hash_algo(const struct evm_ima_xattr_data *xattr_value,179int xattr_len)180{181struct signature_v2_hdr *sig;182enum hash_algo ret;183184if (!xattr_value || xattr_len < 2)185/* return default hash algo */186return ima_hash_algo;187188switch (xattr_value->type) {189case IMA_VERITY_DIGSIG:190sig = (typeof(sig))xattr_value;191if (sig->version != 3 || xattr_len <= sizeof(*sig) ||192sig->hash_algo >= HASH_ALGO__LAST)193return ima_hash_algo;194return sig->hash_algo;195case EVM_IMA_XATTR_DIGSIG:196sig = (typeof(sig))xattr_value;197if (sig->version != 2 || xattr_len <= sizeof(*sig)198|| sig->hash_algo >= HASH_ALGO__LAST)199return ima_hash_algo;200return sig->hash_algo;201case IMA_XATTR_DIGEST_NG:202/* first byte contains algorithm id */203ret = xattr_value->data[0];204if (ret < HASH_ALGO__LAST)205return ret;206break;207case IMA_XATTR_DIGEST:208/* this is for backward compatibility */209if (xattr_len == 21) {210unsigned int zero = 0;211if (!memcmp(&xattr_value->data[16], &zero, 4))212return HASH_ALGO_MD5;213else214return HASH_ALGO_SHA1;215} else if (xattr_len == 17)216return HASH_ALGO_MD5;217break;218}219220/* return default hash algo */221return ima_hash_algo;222}223224int ima_read_xattr(struct dentry *dentry,225struct evm_ima_xattr_data **xattr_value, int xattr_len)226{227int ret;228229ret = vfs_getxattr_alloc(&nop_mnt_idmap, dentry, XATTR_NAME_IMA,230(char **)xattr_value, xattr_len, GFP_NOFS);231if (ret == -EOPNOTSUPP)232ret = 0;233return ret;234}235236/*237* calc_file_id_hash - calculate the hash of the ima_file_id struct data238* @type: xattr type [enum evm_ima_xattr_type]239* @algo: hash algorithm [enum hash_algo]240* @digest: pointer to the digest to be hashed241* @hash: (out) pointer to the hash242*243* IMA signature version 3 disambiguates the data that is signed by244* indirectly signing the hash of the ima_file_id structure data.245*246* Signing the ima_file_id struct is currently only supported for247* IMA_VERITY_DIGSIG type xattrs.248*249* Return 0 on success, error code otherwise.250*/251static int calc_file_id_hash(enum evm_ima_xattr_type type,252enum hash_algo algo, const u8 *digest,253struct ima_digest_data *hash)254{255struct ima_file_id file_id = {256.hash_type = IMA_VERITY_DIGSIG, .hash_algorithm = algo};257unsigned int unused = HASH_MAX_DIGESTSIZE - hash_digest_size[algo];258259if (type != IMA_VERITY_DIGSIG)260return -EINVAL;261262memcpy(file_id.hash, digest, hash_digest_size[algo]);263264hash->algo = algo;265hash->length = hash_digest_size[algo];266267return ima_calc_buffer_hash(&file_id, sizeof(file_id) - unused, hash);268}269270/*271* xattr_verify - verify xattr digest or signature272*273* Verify whether the hash or signature matches the file contents.274*275* Return 0 on success, error code otherwise.276*/277static int xattr_verify(enum ima_hooks func, struct ima_iint_cache *iint,278struct evm_ima_xattr_data *xattr_value, int xattr_len,279enum integrity_status *status, const char **cause)280{281struct ima_max_digest_data hash;282struct signature_v2_hdr *sig;283int rc = -EINVAL, hash_start = 0;284int mask;285286switch (xattr_value->type) {287case IMA_XATTR_DIGEST_NG:288/* first byte contains algorithm id */289hash_start = 1;290fallthrough;291case IMA_XATTR_DIGEST:292if (*status != INTEGRITY_PASS_IMMUTABLE) {293if (iint->flags & IMA_DIGSIG_REQUIRED) {294if (iint->flags & IMA_VERITY_REQUIRED)295*cause = "verity-signature-required";296else297*cause = "IMA-signature-required";298*status = INTEGRITY_FAIL;299break;300}301clear_bit(IMA_DIGSIG, &iint->atomic_flags);302} else {303set_bit(IMA_DIGSIG, &iint->atomic_flags);304}305if (xattr_len - sizeof(xattr_value->type) - hash_start >=306iint->ima_hash->length)307/*308* xattr length may be longer. md5 hash in previous309* version occupied 20 bytes in xattr, instead of 16310*/311rc = memcmp(&xattr_value->data[hash_start],312iint->ima_hash->digest,313iint->ima_hash->length);314else315rc = -EINVAL;316if (rc) {317*cause = "invalid-hash";318*status = INTEGRITY_FAIL;319break;320}321*status = INTEGRITY_PASS;322break;323case EVM_IMA_XATTR_DIGSIG:324set_bit(IMA_DIGSIG, &iint->atomic_flags);325326mask = IMA_DIGSIG_REQUIRED | IMA_VERITY_REQUIRED;327if ((iint->flags & mask) == mask) {328*cause = "verity-signature-required";329*status = INTEGRITY_FAIL;330break;331}332333sig = (typeof(sig))xattr_value;334if (sig->version >= 3) {335*cause = "invalid-signature-version";336*status = INTEGRITY_FAIL;337break;338}339rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,340(const char *)xattr_value,341xattr_len,342iint->ima_hash->digest,343iint->ima_hash->length);344if (rc == -EOPNOTSUPP) {345*status = INTEGRITY_UNKNOWN;346break;347}348if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&349func == KEXEC_KERNEL_CHECK)350rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM,351(const char *)xattr_value,352xattr_len,353iint->ima_hash->digest,354iint->ima_hash->length);355if (rc) {356*cause = "invalid-signature";357*status = INTEGRITY_FAIL;358} else {359*status = INTEGRITY_PASS;360}361break;362case IMA_VERITY_DIGSIG:363set_bit(IMA_DIGSIG, &iint->atomic_flags);364365if (iint->flags & IMA_DIGSIG_REQUIRED) {366if (!(iint->flags & IMA_VERITY_REQUIRED)) {367*cause = "IMA-signature-required";368*status = INTEGRITY_FAIL;369break;370}371}372373sig = (typeof(sig))xattr_value;374if (sig->version != 3) {375*cause = "invalid-signature-version";376*status = INTEGRITY_FAIL;377break;378}379380rc = calc_file_id_hash(IMA_VERITY_DIGSIG, iint->ima_hash->algo,381iint->ima_hash->digest,382container_of(&hash.hdr,383struct ima_digest_data, hdr));384if (rc) {385*cause = "sigv3-hashing-error";386*status = INTEGRITY_FAIL;387break;388}389390rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,391(const char *)xattr_value,392xattr_len, hash.digest,393hash.hdr.length);394if (rc) {395*cause = "invalid-verity-signature";396*status = INTEGRITY_FAIL;397} else {398*status = INTEGRITY_PASS;399}400401break;402default:403*status = INTEGRITY_UNKNOWN;404*cause = "unknown-ima-data";405break;406}407408return rc;409}410411/*412* modsig_verify - verify modsig signature413*414* Verify whether the signature matches the file contents.415*416* Return 0 on success, error code otherwise.417*/418static int modsig_verify(enum ima_hooks func, const struct modsig *modsig,419enum integrity_status *status, const char **cause)420{421int rc;422423rc = integrity_modsig_verify(INTEGRITY_KEYRING_IMA, modsig);424if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&425func == KEXEC_KERNEL_CHECK)426rc = integrity_modsig_verify(INTEGRITY_KEYRING_PLATFORM,427modsig);428if (rc) {429*cause = "invalid-signature";430*status = INTEGRITY_FAIL;431} else {432*status = INTEGRITY_PASS;433}434435return rc;436}437438/*439* ima_check_blacklist - determine if the binary is blacklisted.440*441* Add the hash of the blacklisted binary to the measurement list, based442* on policy.443*444* Returns -EPERM if the hash is blacklisted.445*/446int ima_check_blacklist(struct ima_iint_cache *iint,447const struct modsig *modsig, int pcr)448{449enum hash_algo hash_algo;450const u8 *digest = NULL;451u32 digestsize = 0;452int rc = 0;453454if (!(iint->flags & IMA_CHECK_BLACKLIST))455return 0;456457if (iint->flags & IMA_MODSIG_ALLOWED && modsig) {458ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize);459460rc = is_binary_blacklisted(digest, digestsize);461} else if (iint->flags & IMA_DIGSIG_REQUIRED && iint->ima_hash)462rc = is_binary_blacklisted(iint->ima_hash->digest, iint->ima_hash->length);463464if ((rc == -EPERM) && (iint->flags & IMA_MEASURE))465process_buffer_measurement(&nop_mnt_idmap, NULL, digest, digestsize,466"blacklisted-hash", NONE,467pcr, NULL, false, NULL, 0);468469return rc;470}471472static bool is_bprm_creds_for_exec(enum ima_hooks func, struct file *file)473{474struct linux_binprm *bprm;475476if (func == BPRM_CHECK) {477bprm = container_of(&file, struct linux_binprm, file);478return bprm->is_check;479}480return false;481}482483/*484* ima_appraise_measurement - appraise file measurement485*486* Call evm_verifyxattr() to verify the integrity of 'security.ima'.487* Assuming success, compare the xattr hash with the collected measurement.488*489* Return 0 on success, error code otherwise490*/491int ima_appraise_measurement(enum ima_hooks func, struct ima_iint_cache *iint,492struct file *file, const unsigned char *filename,493struct evm_ima_xattr_data *xattr_value,494int xattr_len, const struct modsig *modsig)495{496static const char op[] = "appraise_data";497int audit_msgno = AUDIT_INTEGRITY_DATA;498const char *cause = "unknown";499struct dentry *dentry = file_dentry(file);500struct inode *inode = d_backing_inode(dentry);501enum integrity_status status = INTEGRITY_UNKNOWN;502int rc = xattr_len;503bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig;504505/* If not appraising a modsig, we need an xattr. */506if (!(inode->i_opflags & IOP_XATTR) && !try_modsig)507return INTEGRITY_UNKNOWN;508509/*510* Unlike any of the other LSM hooks where the kernel enforces file511* integrity, enforcing file integrity for the bprm_creds_for_exec()512* LSM hook with the AT_EXECVE_CHECK flag is left up to the discretion513* of the script interpreter(userspace). Differentiate kernel and514* userspace enforced integrity audit messages.515*/516if (is_bprm_creds_for_exec(func, file))517audit_msgno = AUDIT_INTEGRITY_USERSPACE;518519/* If reading the xattr failed and there's no modsig, error out. */520if (rc <= 0 && !try_modsig) {521if (rc && rc != -ENODATA)522goto out;523524if (iint->flags & IMA_DIGSIG_REQUIRED) {525if (iint->flags & IMA_VERITY_REQUIRED)526cause = "verity-signature-required";527else528cause = "IMA-signature-required";529} else {530cause = "missing-hash";531}532533status = INTEGRITY_NOLABEL;534if (file->f_mode & FMODE_CREATED)535iint->flags |= IMA_NEW_FILE;536if ((iint->flags & IMA_NEW_FILE) &&537(!(iint->flags & IMA_DIGSIG_REQUIRED) ||538(inode->i_size == 0)))539status = INTEGRITY_PASS;540goto out;541}542543status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value,544rc < 0 ? 0 : rc);545switch (status) {546case INTEGRITY_PASS:547case INTEGRITY_PASS_IMMUTABLE:548case INTEGRITY_UNKNOWN:549break;550case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */551/* It's fine not to have xattrs when using a modsig. */552if (try_modsig)553break;554fallthrough;555case INTEGRITY_NOLABEL: /* No security.evm xattr. */556cause = "missing-HMAC";557goto out;558case INTEGRITY_FAIL_IMMUTABLE:559set_bit(IMA_DIGSIG, &iint->atomic_flags);560cause = "invalid-fail-immutable";561goto out;562case INTEGRITY_FAIL: /* Invalid HMAC/signature. */563cause = "invalid-HMAC";564goto out;565default:566WARN_ONCE(true, "Unexpected integrity status %d\n", status);567}568569if (xattr_value)570rc = xattr_verify(func, iint, xattr_value, xattr_len, &status,571&cause);572573/*574* If we have a modsig and either no imasig or the imasig's key isn't575* known, then try verifying the modsig.576*/577if (try_modsig &&578(!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG ||579rc == -ENOKEY))580rc = modsig_verify(func, modsig, &status, &cause);581582out:583/*584* File signatures on some filesystems can not be properly verified.585* When such filesystems are mounted by an untrusted mounter or on a586* system not willing to accept such a risk, fail the file signature587* verification.588*/589if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&590((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||591(iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {592status = INTEGRITY_FAIL;593cause = "unverifiable-signature";594integrity_audit_msg(audit_msgno, inode, filename,595op, cause, rc, 0);596} else if (status != INTEGRITY_PASS) {597/* Fix mode, but don't replace file signatures. */598if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig &&599(!xattr_value ||600xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {601if (!ima_fix_xattr(dentry, iint))602status = INTEGRITY_PASS;603}604605/*606* Permit new files with file/EVM portable signatures, but607* without data.608*/609if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&610test_bit(IMA_DIGSIG, &iint->atomic_flags)) {611status = INTEGRITY_PASS;612}613614integrity_audit_msg(audit_msgno, inode, filename,615op, cause, rc, 0);616} else {617ima_cache_flags(iint, func);618}619620ima_set_cache_status(iint, func, status);621return status;622}623624/*625* ima_update_xattr - update 'security.ima' hash value626*/627void ima_update_xattr(struct ima_iint_cache *iint, struct file *file)628{629struct dentry *dentry = file_dentry(file);630int rc = 0;631632/* do not collect and update hash for digital signatures */633if (test_bit(IMA_DIGSIG, &iint->atomic_flags))634return;635636if ((iint->ima_file_status != INTEGRITY_PASS) &&637!(iint->flags & IMA_HASH))638return;639640rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL);641if (rc < 0)642return;643644inode_lock(file_inode(file));645ima_fix_xattr(dentry, iint);646inode_unlock(file_inode(file));647}648649/**650* ima_inode_post_setattr - reflect file metadata changes651* @idmap: idmap of the mount the inode was found from652* @dentry: pointer to the affected dentry653* @ia_valid: for the UID and GID status654*655* Changes to a dentry's metadata might result in needing to appraise.656*657* This function is called from notify_change(), which expects the caller658* to lock the inode's i_mutex.659*/660static void ima_inode_post_setattr(struct mnt_idmap *idmap,661struct dentry *dentry, int ia_valid)662{663struct inode *inode = d_backing_inode(dentry);664struct ima_iint_cache *iint;665int action;666667if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)668|| !(inode->i_opflags & IOP_XATTR))669return;670671action = ima_must_appraise(idmap, inode, MAY_ACCESS, POST_SETATTR);672iint = ima_iint_find(inode);673if (iint) {674set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);675if (!action)676clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);677}678}679680/*681* ima_protect_xattr - protect 'security.ima'682*683* Ensure that not just anyone can modify or remove 'security.ima'.684*/685static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,686const void *xattr_value, size_t xattr_value_len)687{688if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {689if (!capable(CAP_SYS_ADMIN))690return -EPERM;691return 1;692}693return 0;694}695696static void ima_reset_appraise_flags(struct inode *inode, int digsig)697{698struct ima_iint_cache *iint;699700if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))701return;702703iint = ima_iint_find(inode);704if (!iint)705return;706iint->measured_pcrs = 0;707set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);708if (digsig)709set_bit(IMA_DIGSIG, &iint->atomic_flags);710else711clear_bit(IMA_DIGSIG, &iint->atomic_flags);712}713714/**715* validate_hash_algo() - Block setxattr with unsupported hash algorithms716* @dentry: object of the setxattr()717* @xattr_value: userland supplied xattr value718* @xattr_value_len: length of xattr_value719*720* The xattr value is mapped to its hash algorithm, and this algorithm721* must be built in the kernel for the setxattr to be allowed.722*723* Emit an audit message when the algorithm is invalid.724*725* Return: 0 on success, else an error.726*/727static int validate_hash_algo(struct dentry *dentry,728const struct evm_ima_xattr_data *xattr_value,729size_t xattr_value_len)730{731char *path = NULL, *pathbuf = NULL;732enum hash_algo xattr_hash_algo;733const char *errmsg = "unavailable-hash-algorithm";734unsigned int allowed_hashes;735736xattr_hash_algo = ima_get_hash_algo(xattr_value, xattr_value_len);737738allowed_hashes = atomic_read(&ima_setxattr_allowed_hash_algorithms);739740if (allowed_hashes) {741/* success if the algorithm is allowed in the ima policy */742if (allowed_hashes & (1U << xattr_hash_algo))743return 0;744745/*746* We use a different audit message when the hash algorithm747* is denied by a policy rule, instead of not being built748* in the kernel image749*/750errmsg = "denied-hash-algorithm";751} else {752if (likely(xattr_hash_algo == ima_hash_algo))753return 0;754755/* allow any xattr using an algorithm built in the kernel */756if (crypto_has_alg(hash_algo_name[xattr_hash_algo], 0, 0))757return 0;758}759760pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);761if (!pathbuf)762return -EACCES;763764path = dentry_path(dentry, pathbuf, PATH_MAX);765766integrity_audit_msg(AUDIT_INTEGRITY_DATA, d_inode(dentry), path,767"set_data", errmsg, -EACCES, 0);768769kfree(pathbuf);770771return -EACCES;772}773774static int ima_inode_setxattr(struct mnt_idmap *idmap, struct dentry *dentry,775const char *xattr_name, const void *xattr_value,776size_t xattr_value_len, int flags)777{778const struct evm_ima_xattr_data *xvalue = xattr_value;779int digsig = 0;780int result;781int err;782783result = ima_protect_xattr(dentry, xattr_name, xattr_value,784xattr_value_len);785if (result == 1) {786if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))787return -EINVAL;788789err = validate_hash_algo(dentry, xvalue, xattr_value_len);790if (err)791return err;792793digsig = (xvalue->type == EVM_IMA_XATTR_DIGSIG);794} else if (!strcmp(xattr_name, XATTR_NAME_EVM) && xattr_value_len > 0) {795digsig = (xvalue->type == EVM_XATTR_PORTABLE_DIGSIG);796}797if (result == 1 || evm_revalidate_status(xattr_name)) {798ima_reset_appraise_flags(d_backing_inode(dentry), digsig);799if (result == 1)800result = 0;801}802return result;803}804805static int ima_inode_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,806const char *acl_name, struct posix_acl *kacl)807{808if (evm_revalidate_status(acl_name))809ima_reset_appraise_flags(d_backing_inode(dentry), 0);810811return 0;812}813814static int ima_inode_removexattr(struct mnt_idmap *idmap, struct dentry *dentry,815const char *xattr_name)816{817int result;818819result = ima_protect_xattr(dentry, xattr_name, NULL, 0);820if (result == 1 || evm_revalidate_status(xattr_name)) {821ima_reset_appraise_flags(d_backing_inode(dentry), 0);822if (result == 1)823result = 0;824}825return result;826}827828static int ima_inode_remove_acl(struct mnt_idmap *idmap, struct dentry *dentry,829const char *acl_name)830{831return ima_inode_set_acl(idmap, dentry, acl_name, NULL);832}833834static struct security_hook_list ima_appraise_hooks[] __ro_after_init = {835LSM_HOOK_INIT(inode_post_setattr, ima_inode_post_setattr),836LSM_HOOK_INIT(inode_setxattr, ima_inode_setxattr),837LSM_HOOK_INIT(inode_set_acl, ima_inode_set_acl),838LSM_HOOK_INIT(inode_removexattr, ima_inode_removexattr),839LSM_HOOK_INIT(inode_remove_acl, ima_inode_remove_acl),840};841842void __init init_ima_appraise_lsm(const struct lsm_id *lsmid)843{844security_add_hooks(ima_appraise_hooks, ARRAY_SIZE(ima_appraise_hooks),845lsmid);846}847848849