Skip to content

Commit 1886ab0

Browse files
robertosassumimizohar
authored andcommitted
evm: Allow setxattr() and setattr() for unmodified metadata
With the patch to allow xattr/attr operations if a portable signature verification fails, cp and tar can copy all xattrs/attrs so that at the end of the process verification succeeds. However, it might happen that the xattrs/attrs are already set to the correct value (taken at signing time) and signature verification succeeds before the copy has completed. For example, an archive might contains files owned by root and the archive is extracted by root. Then, since portable signatures are immutable, all subsequent operations fail (e.g. fchown()), even if the operation is legitimate (does not alter the current value). This patch avoids this problem by reporting successful operation to user space when that operation does not alter the current value of xattrs/attrs. With this patch, the one that introduces evm_hmac_disabled() and the one that allows a metadata operation on the INTEGRITY_FAIL_IMMUTABLE error, EVM portable signatures can be used without disabling metadata verification (by setting EVM_ALLOW_METADATA_WRITES). Due to keeping metadata verification enabled, altering immutable metadata protected with a portable signature that was successfully verified will be denied (existing behavior). Reported-by: kernel test robot <lkp@intel.com> [implicit declaration of function] Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com> Reviewed-by: Christian Brauner <christian.brauner@ubuntu.com> Cc: Andreas Gruenbacher <agruenba@redhat.com> Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>
1 parent 7e135dc commit 1886ab0

1 file changed

Lines changed: 112 additions & 1 deletion

File tree

security/integrity/evm/evm_main.c

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/integrity.h>
1919
#include <linux/evm.h>
2020
#include <linux/magic.h>
21+
#include <linux/posix_acl_xattr.h>
2122

2223
#include <crypto/hash.h>
2324
#include <crypto/hash_info.h>
@@ -330,6 +331,92 @@ static enum integrity_status evm_verify_current_integrity(struct dentry *dentry)
330331
return evm_verify_hmac(dentry, NULL, NULL, 0, NULL);
331332
}
332333

334+
/*
335+
* evm_xattr_acl_change - check if passed ACL changes the inode mode
336+
* @mnt_userns: user namespace of the idmapped mount
337+
* @dentry: pointer to the affected dentry
338+
* @xattr_name: requested xattr
339+
* @xattr_value: requested xattr value
340+
* @xattr_value_len: requested xattr value length
341+
*
342+
* Check if passed ACL changes the inode mode, which is protected by EVM.
343+
*
344+
* Returns 1 if passed ACL causes inode mode change, 0 otherwise.
345+
*/
346+
static int evm_xattr_acl_change(struct user_namespace *mnt_userns,
347+
struct dentry *dentry, const char *xattr_name,
348+
const void *xattr_value, size_t xattr_value_len)
349+
{
350+
#ifdef CONFIG_FS_POSIX_ACL
351+
umode_t mode;
352+
struct posix_acl *acl = NULL, *acl_res;
353+
struct inode *inode = d_backing_inode(dentry);
354+
int rc;
355+
356+
/*
357+
* user_ns is not relevant here, ACL_USER/ACL_GROUP don't have impact
358+
* on the inode mode (see posix_acl_equiv_mode()).
359+
*/
360+
acl = posix_acl_from_xattr(&init_user_ns, xattr_value, xattr_value_len);
361+
if (IS_ERR_OR_NULL(acl))
362+
return 1;
363+
364+
acl_res = acl;
365+
/*
366+
* Passing mnt_userns is necessary to correctly determine the GID in
367+
* an idmapped mount, as the GID is used to clear the setgid bit in
368+
* the inode mode.
369+
*/
370+
rc = posix_acl_update_mode(mnt_userns, inode, &mode, &acl_res);
371+
372+
posix_acl_release(acl);
373+
374+
if (rc)
375+
return 1;
376+
377+
if (inode->i_mode != mode)
378+
return 1;
379+
#endif
380+
return 0;
381+
}
382+
383+
/*
384+
* evm_xattr_change - check if passed xattr value differs from current value
385+
* @mnt_userns: user namespace of the idmapped mount
386+
* @dentry: pointer to the affected dentry
387+
* @xattr_name: requested xattr
388+
* @xattr_value: requested xattr value
389+
* @xattr_value_len: requested xattr value length
390+
*
391+
* Check if passed xattr value differs from current value.
392+
*
393+
* Returns 1 if passed xattr value differs from current value, 0 otherwise.
394+
*/
395+
static int evm_xattr_change(struct user_namespace *mnt_userns,
396+
struct dentry *dentry, const char *xattr_name,
397+
const void *xattr_value, size_t xattr_value_len)
398+
{
399+
char *xattr_data = NULL;
400+
int rc = 0;
401+
402+
if (posix_xattr_acl(xattr_name))
403+
return evm_xattr_acl_change(mnt_userns, dentry, xattr_name,
404+
xattr_value, xattr_value_len);
405+
406+
rc = vfs_getxattr_alloc(&init_user_ns, dentry, xattr_name, &xattr_data,
407+
0, GFP_NOFS);
408+
if (rc < 0)
409+
return 1;
410+
411+
if (rc == xattr_value_len)
412+
rc = !!memcmp(xattr_value, xattr_data, rc);
413+
else
414+
rc = 1;
415+
416+
kfree(xattr_data);
417+
return rc;
418+
}
419+
333420
/*
334421
* evm_protect_xattr - protect the EVM extended attribute
335422
*
@@ -397,7 +484,13 @@ static int evm_protect_xattr(struct user_namespace *mnt_userns,
397484
if (evm_status == INTEGRITY_FAIL_IMMUTABLE)
398485
return 0;
399486

400-
if (evm_status != INTEGRITY_PASS)
487+
if (evm_status == INTEGRITY_PASS_IMMUTABLE &&
488+
!evm_xattr_change(mnt_userns, dentry, xattr_name, xattr_value,
489+
xattr_value_len))
490+
return 0;
491+
492+
if (evm_status != INTEGRITY_PASS &&
493+
evm_status != INTEGRITY_PASS_IMMUTABLE)
401494
integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
402495
dentry->d_name.name, "appraise_metadata",
403496
integrity_status_msg[evm_status],
@@ -553,6 +646,19 @@ void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
553646
evm_update_evmxattr(dentry, xattr_name, NULL, 0);
554647
}
555648

649+
static int evm_attr_change(struct dentry *dentry, struct iattr *attr)
650+
{
651+
struct inode *inode = d_backing_inode(dentry);
652+
unsigned int ia_valid = attr->ia_valid;
653+
654+
if ((!(ia_valid & ATTR_UID) || uid_eq(attr->ia_uid, inode->i_uid)) &&
655+
(!(ia_valid & ATTR_GID) || gid_eq(attr->ia_gid, inode->i_gid)) &&
656+
(!(ia_valid & ATTR_MODE) || attr->ia_mode == inode->i_mode))
657+
return 0;
658+
659+
return 1;
660+
}
661+
556662
/**
557663
* evm_inode_setattr - prevent updating an invalid EVM extended attribute
558664
* @dentry: pointer to the affected dentry
@@ -584,6 +690,11 @@ int evm_inode_setattr(struct dentry *dentry, struct iattr *attr)
584690
(evm_hmac_disabled() && (evm_status == INTEGRITY_NOLABEL ||
585691
evm_status == INTEGRITY_UNKNOWN)))
586692
return 0;
693+
694+
if (evm_status == INTEGRITY_PASS_IMMUTABLE &&
695+
!evm_attr_change(dentry, attr))
696+
return 0;
697+
587698
integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
588699
dentry->d_name.name, "appraise_metadata",
589700
integrity_status_msg[evm_status], -EPERM, 0);

0 commit comments

Comments
 (0)