Skip to content

Commit ae1ba16

Browse files
mjg59Mimi Zohar
authored andcommitted
EVM: Allow userland to permit modification of EVM-protected metadata
When EVM is enabled it forbids modification of metadata protected by EVM unless there is already a valid EVM signature. If any modification is made, the kernel will then generate a new EVM HMAC. However, this does not map well on use cases which use only asymmetric EVM signatures, as in this scenario the kernel is unable to generate new signatures. This patch extends the /sys/kernel/security/evm interface to allow userland to request that modification of these xattrs be permitted. This is only permitted if no keys have already been loaded. In this configuration, modifying the metadata will invalidate the EVM appraisal on the file in question. This allows packaging systems to write out new files, set the relevant extended attributes and then move them into place. There's also some refactoring of the use of evm_initialized in order to avoid heading down codepaths that assume there's a key available. Signed-off-by: Matthew Garrett <mjg59@google.com> Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
1 parent b7e27bc commit ae1ba16

4 files changed

Lines changed: 88 additions & 31 deletions

File tree

‎Documentation/ABI/testing/evm‎

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,46 @@ Description:
1414
generated either locally or remotely using an
1515
asymmetric key. These keys are loaded onto root's
1616
keyring using keyctl, and EVM is then enabled by
17-
echoing a value to <securityfs>/evm:
17+
echoing a value to <securityfs>/evm made up of the
18+
following bits:
1819

19-
1: enable HMAC validation and creation
20-
2: enable digital signature validation
21-
3: enable HMAC and digital signature validation and HMAC
22-
creation
20+
Bit Effect
21+
0 Enable HMAC validation and creation
22+
1 Enable digital signature validation
23+
2 Permit modification of EVM-protected metadata at
24+
runtime. Not supported if HMAC validation and
25+
creation is enabled.
26+
31 Disable further runtime modification of EVM policy
2327

24-
Further writes will be blocked if HMAC support is enabled or
25-
if bit 32 is set:
28+
For example:
2629

27-
echo 0x80000002 ><securityfs>/evm
30+
echo 1 ><securityfs>/evm
2831

29-
will enable digital signature validation and block
30-
further writes to <securityfs>/evm.
32+
will enable HMAC validation and creation
3133

32-
Until this is done, EVM can not create or validate the
33-
'security.evm' xattr, but returns INTEGRITY_UNKNOWN.
34-
Loading keys and signaling EVM should be done as early
35-
as possible. Normally this is done in the initramfs,
36-
which has already been measured as part of the trusted
37-
boot. For more information on creating and loading
38-
existing trusted/encrypted keys, refer to:
34+
echo 0x80000003 ><securityfs>/evm
3935

40-
Documentation/security/keys/trusted-encrypted.rst. Both dracut
41-
(via 97masterkey and 98integrity) and systemd (via
36+
will enable HMAC and digital signature validation and
37+
HMAC creation and disable all further modification of policy.
38+
39+
echo 0x80000006 ><securityfs>/evm
40+
41+
will enable digital signature validation, permit
42+
modification of EVM-protected metadata and
43+
disable all further modification of policy
44+
45+
Note that once a key has been loaded, it will no longer be
46+
possible to enable metadata modification.
47+
48+
Until key loading has been signaled EVM can not create
49+
or validate the 'security.evm' xattr, but returns
50+
INTEGRITY_UNKNOWN. Loading keys and signaling EVM
51+
should be done as early as possible. Normally this is
52+
done in the initramfs, which has already been measured
53+
as part of the trusted boot. For more information on
54+
creating and loading existing trusted/encrypted keys,
55+
refer to:
56+
Documentation/security/keys/trusted-encrypted.rst. Both
57+
dracut (via 97masterkey and 98integrity) and systemd (via
4258
core/ima-setup) have support for loading keys at boot
4359
time.

‎security/integrity/evm/evm.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323

2424
#define EVM_INIT_HMAC 0x0001
2525
#define EVM_INIT_X509 0x0002
26-
#define EVM_SETUP 0x80000000 /* userland has signaled key load */
26+
#define EVM_ALLOW_METADATA_WRITES 0x0004
27+
#define EVM_SETUP_COMPLETE 0x80000000 /* userland has signaled key load */
2728

28-
#define EVM_INIT_MASK (EVM_INIT_HMAC | EVM_INIT_X509 | EVM_SETUP)
29+
#define EVM_KEY_MASK (EVM_INIT_HMAC | EVM_INIT_X509)
30+
#define EVM_INIT_MASK (EVM_INIT_HMAC | EVM_INIT_X509 | EVM_SETUP_COMPLETE | \
31+
EVM_ALLOW_METADATA_WRITES)
2932

3033
extern int evm_initialized;
3134
extern char *evm_hmac;

‎security/integrity/evm/evm_main.c‎

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ static void __init evm_init_config(void)
7676
pr_info("HMAC attrs: 0x%x\n", evm_hmac_attrs);
7777
}
7878

79+
static bool evm_key_loaded(void)
80+
{
81+
return (bool)(evm_initialized & EVM_KEY_MASK);
82+
}
83+
7984
static int evm_find_protected_xattrs(struct dentry *dentry)
8085
{
8186
struct inode *inode = d_backing_inode(dentry);
@@ -241,7 +246,7 @@ enum integrity_status evm_verifyxattr(struct dentry *dentry,
241246
void *xattr_value, size_t xattr_value_len,
242247
struct integrity_iint_cache *iint)
243248
{
244-
if (!evm_initialized || !evm_protected_xattr(xattr_name))
249+
if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
245250
return INTEGRITY_UNKNOWN;
246251

247252
if (!iint) {
@@ -265,7 +270,7 @@ static enum integrity_status evm_verify_current_integrity(struct dentry *dentry)
265270
{
266271
struct inode *inode = d_backing_inode(dentry);
267272

268-
if (!evm_initialized || !S_ISREG(inode->i_mode) || evm_fixmode)
273+
if (!evm_key_loaded() || !S_ISREG(inode->i_mode) || evm_fixmode)
269274
return 0;
270275
return evm_verify_hmac(dentry, NULL, NULL, 0, NULL);
271276
}
@@ -299,6 +304,7 @@ static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name,
299304
return 0;
300305
goto out;
301306
}
307+
302308
evm_status = evm_verify_current_integrity(dentry);
303309
if (evm_status == INTEGRITY_NOXATTRS) {
304310
struct integrity_iint_cache *iint;
@@ -345,6 +351,12 @@ int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
345351
{
346352
const struct evm_ima_xattr_data *xattr_data = xattr_value;
347353

354+
/* Policy permits modification of the protected xattrs even though
355+
* there's no HMAC key loaded
356+
*/
357+
if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
358+
return 0;
359+
348360
if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
349361
if (!xattr_value_len)
350362
return -EINVAL;
@@ -365,6 +377,12 @@ int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
365377
*/
366378
int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name)
367379
{
380+
/* Policy permits modification of the protected xattrs even though
381+
* there's no HMAC key loaded
382+
*/
383+
if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
384+
return 0;
385+
368386
return evm_protect_xattr(dentry, xattr_name, NULL, 0);
369387
}
370388

@@ -393,8 +411,8 @@ static void evm_reset_status(struct inode *inode)
393411
void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
394412
const void *xattr_value, size_t xattr_value_len)
395413
{
396-
if (!evm_initialized || (!evm_protected_xattr(xattr_name)
397-
&& !posix_xattr_acl(xattr_name)))
414+
if (!evm_key_loaded() || (!evm_protected_xattr(xattr_name)
415+
&& !posix_xattr_acl(xattr_name)))
398416
return;
399417

400418
evm_reset_status(dentry->d_inode);
@@ -414,7 +432,7 @@ void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
414432
*/
415433
void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
416434
{
417-
if (!evm_initialized || !evm_protected_xattr(xattr_name))
435+
if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
418436
return;
419437

420438
evm_reset_status(dentry->d_inode);
@@ -431,6 +449,12 @@ int evm_inode_setattr(struct dentry *dentry, struct iattr *attr)
431449
unsigned int ia_valid = attr->ia_valid;
432450
enum integrity_status evm_status;
433451

452+
/* Policy permits modification of the protected attrs even though
453+
* there's no HMAC key loaded
454+
*/
455+
if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
456+
return 0;
457+
434458
if (!(ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)))
435459
return 0;
436460
evm_status = evm_verify_current_integrity(dentry);
@@ -456,7 +480,7 @@ int evm_inode_setattr(struct dentry *dentry, struct iattr *attr)
456480
*/
457481
void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
458482
{
459-
if (!evm_initialized)
483+
if (!evm_key_loaded())
460484
return;
461485

462486
if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
@@ -473,7 +497,7 @@ int evm_inode_init_security(struct inode *inode,
473497
struct evm_ima_xattr_data *xattr_data;
474498
int rc;
475499

476-
if (!evm_initialized || !evm_protected_xattr(lsm_xattr->name))
500+
if (!evm_key_loaded() || !evm_protected_xattr(lsm_xattr->name))
477501
return 0;
478502

479503
xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);

‎security/integrity/evm/evm_secfs.c‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static ssize_t evm_read_key(struct file *filp, char __user *buf,
4040
if (*ppos != 0)
4141
return 0;
4242

43-
sprintf(temp, "%d", (evm_initialized & ~EVM_SETUP));
43+
sprintf(temp, "%d", (evm_initialized & ~EVM_SETUP_COMPLETE));
4444
rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
4545

4646
return rc;
@@ -63,7 +63,7 @@ static ssize_t evm_write_key(struct file *file, const char __user *buf,
6363
{
6464
int i, ret;
6565

66-
if (!capable(CAP_SYS_ADMIN) || (evm_initialized & EVM_SETUP))
66+
if (!capable(CAP_SYS_ADMIN) || (evm_initialized & EVM_SETUP_COMPLETE))
6767
return -EPERM;
6868

6969
ret = kstrtoint_from_user(buf, count, 0, &i);
@@ -75,16 +75,30 @@ static ssize_t evm_write_key(struct file *file, const char __user *buf,
7575
if (!i || (i & ~EVM_INIT_MASK) != 0)
7676
return -EINVAL;
7777

78+
/* Don't allow a request to freshly enable metadata writes if
79+
* keys are loaded.
80+
*/
81+
if ((i & EVM_ALLOW_METADATA_WRITES) &&
82+
((evm_initialized & EVM_KEY_MASK) != 0) &&
83+
!(evm_initialized & EVM_ALLOW_METADATA_WRITES))
84+
return -EPERM;
85+
7886
if (i & EVM_INIT_HMAC) {
7987
ret = evm_init_key();
8088
if (ret != 0)
8189
return ret;
8290
/* Forbid further writes after the symmetric key is loaded */
83-
i |= EVM_SETUP;
91+
i |= EVM_SETUP_COMPLETE;
8492
}
8593

8694
evm_initialized |= i;
8795

96+
/* Don't allow protected metadata modification if a symmetric key
97+
* is loaded
98+
*/
99+
if (evm_initialized & EVM_INIT_HMAC)
100+
evm_initialized &= ~(EVM_ALLOW_METADATA_WRITES);
101+
88102
return count;
89103
}
90104

0 commit comments

Comments
 (0)