Skip to content

Commit 3c29548

Browse files
committed
Merge branch 'next-integrity' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security
Pull integrity updates from James Morris: "This contains a mixture of bug fixes, code cleanup, and new functionality. Of note is the integrity cache locking fix, file change detection, and support for a new EVM portable and immutable signature type. The re-introduction of the integrity cache lock (iint) fixes the problem of attempting to take the i_rwsem shared a second time, when it was previously taken exclusively. Defining atomic flags resolves the original iint/i_rwsem circular locking - accessing the file data vs. modifying the file metadata. Although it fixes the O_DIRECT problem as well, a subsequent patch is needed to remove the explicit O_DIRECT prevention. For performance reasons, detecting when a file has changed and needs to be re-measured, re-appraised, and/or re-audited, was limited to after the last writer has closed, and only if the file data has changed. Detecting file change is based on i_version. For filesystems that do not support i_version, remote filesystems, or userspace filesystems, the file was measured, appraised and/or audited once and never re-evaluated. Now local filesystems, which do not support i_version or are not mounted with the i_version option, assume the file has changed and are required to re-evaluate the file. This change does not address detecting file change on remote or userspace filesystems. Unlike file data signatures, which can be included and distributed in software packages (eg. rpm, deb), the existing EVM signature, which protects the file metadata, could not be included in software packages, as it includes file system specific information (eg. i_ino, possibly the UUID). This pull request defines a new EVM portable and immutable file metadata signature format, which can be included in software packages" * 'next-integrity' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security: ima/policy: fix parsing of fsuuid ima: Use i_version only when filesystem supports it integrity: remove unneeded initializations in integrity_iint_cache entries ima: log message to module appraisal error ima: pass filename to ima_rdwr_violation_check() ima: Fix line continuation format ima: support new "hash" and "dont_hash" policy actions ima: re-introduce own integrity cache lock EVM: Add support for portable signature format EVM: Allow userland to permit modification of EVM-protected metadata ima: relax requiring a file signature for new files with zero length
2 parents e1c70f3 + 3644745 commit 3c29548

14 files changed

Lines changed: 332 additions & 125 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.

‎Documentation/ABI/testing/ima_policy‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ Description:
1717

1818
rule format: action [condition ...]
1919

20-
action: measure | dont_measure | appraise | dont_appraise | audit
20+
action: measure | dont_measure | appraise | dont_appraise |
21+
audit | hash | dont_hash
2122
condition:= base | lsm [option]
2223
base: [[func=] [mask=] [fsmagic=] [fsuuid=] [uid=]
2324
[euid=] [fowner=]]

‎include/linux/integrity.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
enum integrity_status {
1616
INTEGRITY_PASS = 0,
17+
INTEGRITY_PASS_IMMUTABLE,
1718
INTEGRITY_FAIL,
1819
INTEGRITY_NOLABEL,
1920
INTEGRITY_NOXATTRS,

‎security/integrity/evm/evm.h‎

Lines changed: 6 additions & 3 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;
@@ -51,7 +54,7 @@ int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
5154
size_t req_xattr_value_len, char *digest);
5255
int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
5356
const char *req_xattr_value,
54-
size_t req_xattr_value_len, char *digest);
57+
size_t req_xattr_value_len, char type, char *digest);
5558
int evm_init_hmac(struct inode *inode, const struct xattr *xattr,
5659
char *hmac_val);
5760
int evm_init_secfs(void);

‎security/integrity/evm/evm_crypto.c‎

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ static struct shash_desc *init_desc(char type)
138138
* protection.)
139139
*/
140140
static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
141-
char *digest)
141+
char type, char *digest)
142142
{
143143
struct h_misc {
144144
unsigned long ino;
@@ -149,8 +149,13 @@ static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
149149
} hmac_misc;
150150

151151
memset(&hmac_misc, 0, sizeof(hmac_misc));
152-
hmac_misc.ino = inode->i_ino;
153-
hmac_misc.generation = inode->i_generation;
152+
/* Don't include the inode or generation number in portable
153+
* signatures
154+
*/
155+
if (type != EVM_XATTR_PORTABLE_DIGSIG) {
156+
hmac_misc.ino = inode->i_ino;
157+
hmac_misc.generation = inode->i_generation;
158+
}
154159
/* The hmac uid and gid must be encoded in the initial user
155160
* namespace (not the filesystems user namespace) as encoding
156161
* them in the filesystems user namespace allows an attack
@@ -163,7 +168,8 @@ static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
163168
hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid);
164169
hmac_misc.mode = inode->i_mode;
165170
crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc));
166-
if (evm_hmac_attrs & EVM_ATTR_FSUUID)
171+
if ((evm_hmac_attrs & EVM_ATTR_FSUUID) &&
172+
type != EVM_XATTR_PORTABLE_DIGSIG)
167173
crypto_shash_update(desc, &inode->i_sb->s_uuid.b[0],
168174
sizeof(inode->i_sb->s_uuid));
169175
crypto_shash_final(desc, digest);
@@ -189,6 +195,7 @@ static int evm_calc_hmac_or_hash(struct dentry *dentry,
189195
char *xattr_value = NULL;
190196
int error;
191197
int size;
198+
bool ima_present = false;
192199

193200
if (!(inode->i_opflags & IOP_XATTR))
194201
return -EOPNOTSUPP;
@@ -199,11 +206,18 @@ static int evm_calc_hmac_or_hash(struct dentry *dentry,
199206

200207
error = -ENODATA;
201208
for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
209+
bool is_ima = false;
210+
211+
if (strcmp(*xattrname, XATTR_NAME_IMA) == 0)
212+
is_ima = true;
213+
202214
if ((req_xattr_name && req_xattr_value)
203215
&& !strcmp(*xattrname, req_xattr_name)) {
204216
error = 0;
205217
crypto_shash_update(desc, (const u8 *)req_xattr_value,
206218
req_xattr_value_len);
219+
if (is_ima)
220+
ima_present = true;
207221
continue;
208222
}
209223
size = vfs_getxattr_alloc(dentry, *xattrname,
@@ -218,9 +232,14 @@ static int evm_calc_hmac_or_hash(struct dentry *dentry,
218232
error = 0;
219233
xattr_size = size;
220234
crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
235+
if (is_ima)
236+
ima_present = true;
221237
}
222-
hmac_add_misc(desc, inode, digest);
238+
hmac_add_misc(desc, inode, type, digest);
223239

240+
/* Portable EVM signatures must include an IMA hash */
241+
if (type == EVM_XATTR_PORTABLE_DIGSIG && !ima_present)
242+
return -EPERM;
224243
out:
225244
kfree(xattr_value);
226245
kfree(desc);
@@ -232,17 +251,45 @@ int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
232251
char *digest)
233252
{
234253
return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
235-
req_xattr_value_len, EVM_XATTR_HMAC, digest);
254+
req_xattr_value_len, EVM_XATTR_HMAC, digest);
236255
}
237256

238257
int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
239258
const char *req_xattr_value, size_t req_xattr_value_len,
240-
char *digest)
259+
char type, char *digest)
241260
{
242261
return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
243-
req_xattr_value_len, IMA_XATTR_DIGEST, digest);
262+
req_xattr_value_len, type, digest);
263+
}
264+
265+
static int evm_is_immutable(struct dentry *dentry, struct inode *inode)
266+
{
267+
const struct evm_ima_xattr_data *xattr_data = NULL;
268+
struct integrity_iint_cache *iint;
269+
int rc = 0;
270+
271+
iint = integrity_iint_find(inode);
272+
if (iint && (iint->flags & EVM_IMMUTABLE_DIGSIG))
273+
return 1;
274+
275+
/* Do this the hard way */
276+
rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0,
277+
GFP_NOFS);
278+
if (rc <= 0) {
279+
if (rc == -ENODATA)
280+
return 0;
281+
return rc;
282+
}
283+
if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG)
284+
rc = 1;
285+
else
286+
rc = 0;
287+
288+
kfree(xattr_data);
289+
return rc;
244290
}
245291

292+
246293
/*
247294
* Calculate the hmac and update security.evm xattr
248295
*
@@ -255,6 +302,16 @@ int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
255302
struct evm_ima_xattr_data xattr_data;
256303
int rc = 0;
257304

305+
/*
306+
* Don't permit any transformation of the EVM xattr if the signature
307+
* is of an immutable type
308+
*/
309+
rc = evm_is_immutable(dentry, inode);
310+
if (rc < 0)
311+
return rc;
312+
if (rc)
313+
return -EPERM;
314+
258315
rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
259316
xattr_value_len, xattr_data.digest);
260317
if (rc == 0) {
@@ -280,7 +337,7 @@ int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
280337
}
281338

282339
crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
283-
hmac_add_misc(desc, inode, hmac_val);
340+
hmac_add_misc(desc, inode, EVM_XATTR_HMAC, hmac_val);
284341
kfree(desc);
285342
return 0;
286343
}

0 commit comments

Comments
 (0)