aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2018-01-31 16:07:35 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2018-01-31 16:07:35 -0500
commit3c29548f87f9545f2f3c1cd1a784fae8ad2d53ba (patch)
treea6ee072fea6f32e40fad48319ddf3cc3eca53dcb
parente1c70f32386c4984ed8ca1a7aedb9bbff9ed3414 (diff)
parent36447456e1cca853188505f2a964dbbeacfc7a7a (diff)
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
-rw-r--r--Documentation/ABI/testing/evm54
-rw-r--r--Documentation/ABI/testing/ima_policy3
-rw-r--r--include/linux/integrity.h1
-rw-r--r--security/integrity/evm/evm.h9
-rw-r--r--security/integrity/evm/evm_crypto.c75
-rw-r--r--security/integrity/evm/evm_main.c67
-rw-r--r--security/integrity/evm/evm_secfs.c20
-rw-r--r--security/integrity/iint.c4
-rw-r--r--security/integrity/ima/ima_api.c2
-rw-r--r--security/integrity/ima/ima_appraise.c46
-rw-r--r--security/integrity/ima/ima_main.c92
-rw-r--r--security/integrity/ima/ima_policy.c32
-rw-r--r--security/integrity/ima/ima_template.c11
-rw-r--r--security/integrity/integrity.h41
14 files changed, 332 insertions, 125 deletions
diff --git a/Documentation/ABI/testing/evm b/Documentation/ABI/testing/evm
index 9578247e1792..d12cb2eae9ee 100644
--- a/Documentation/ABI/testing/evm
+++ b/Documentation/ABI/testing/evm
@@ -14,30 +14,46 @@ Description:
14 generated either locally or remotely using an 14 generated either locally or remotely using an
15 asymmetric key. These keys are loaded onto root's 15 asymmetric key. These keys are loaded onto root's
16 keyring using keyctl, and EVM is then enabled by 16 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:
18 19
19 1: enable HMAC validation and creation 20 Bit Effect
20 2: enable digital signature validation 21 0 Enable HMAC validation and creation
21 3: enable HMAC and digital signature validation and HMAC 22 1 Enable digital signature validation
22 creation 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
23 27
24 Further writes will be blocked if HMAC support is enabled or 28 For example:
25 if bit 32 is set:
26 29
27 echo 0x80000002 ><securityfs>/evm 30 echo 1 ><securityfs>/evm
28 31
29 will enable digital signature validation and block 32 will enable HMAC validation and creation
30 further writes to <securityfs>/evm.
31 33
32 Until this is done, EVM can not create or validate the 34 echo 0x80000003 ><securityfs>/evm
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:
39 35
40 Documentation/security/keys/trusted-encrypted.rst. Both dracut 36 will enable HMAC and digital signature validation and
41 (via 97masterkey and 98integrity) and systemd (via 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
42 core/ima-setup) have support for loading keys at boot 58 core/ima-setup) have support for loading keys at boot
43 time. 59 time.
diff --git a/Documentation/ABI/testing/ima_policy b/Documentation/ABI/testing/ima_policy
index e76432b9954d..2028f2d093b2 100644
--- a/Documentation/ABI/testing/ima_policy
+++ b/Documentation/ABI/testing/ima_policy
@@ -17,7 +17,8 @@ Description:
17 17
18 rule format: action [condition ...] 18 rule format: action [condition ...]
19 19
20 action: measure | dont_measure | appraise | dont_appraise | audit 20 action: measure | dont_measure | appraise | dont_appraise |
21 audit | hash | dont_hash
21 condition:= base | lsm [option] 22 condition:= base | lsm [option]
22 base: [[func=] [mask=] [fsmagic=] [fsuuid=] [uid=] 23 base: [[func=] [mask=] [fsmagic=] [fsuuid=] [uid=]
23 [euid=] [fowner=]] 24 [euid=] [fowner=]]
diff --git a/include/linux/integrity.h b/include/linux/integrity.h
index c2d6082a1a4c..858d3f4a2241 100644
--- a/include/linux/integrity.h
+++ b/include/linux/integrity.h
@@ -14,6 +14,7 @@
14 14
15enum integrity_status { 15enum integrity_status {
16 INTEGRITY_PASS = 0, 16 INTEGRITY_PASS = 0,
17 INTEGRITY_PASS_IMMUTABLE,
17 INTEGRITY_FAIL, 18 INTEGRITY_FAIL,
18 INTEGRITY_NOLABEL, 19 INTEGRITY_NOLABEL,
19 INTEGRITY_NOXATTRS, 20 INTEGRITY_NOXATTRS,
diff --git a/security/integrity/evm/evm.h b/security/integrity/evm/evm.h
index 241aca315b0c..04825393facb 100644
--- a/security/integrity/evm/evm.h
+++ b/security/integrity/evm/evm.h
@@ -23,9 +23,12 @@
23 23
24#define EVM_INIT_HMAC 0x0001 24#define EVM_INIT_HMAC 0x0001
25#define EVM_INIT_X509 0x0002 25#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 */
27 28
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)
29 32
30extern int evm_initialized; 33extern int evm_initialized;
31extern char *evm_hmac; 34extern char *evm_hmac;
@@ -51,7 +54,7 @@ int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
51 size_t req_xattr_value_len, char *digest); 54 size_t req_xattr_value_len, char *digest);
52int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name, 55int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
53 const char *req_xattr_value, 56 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);
55int evm_init_hmac(struct inode *inode, const struct xattr *xattr, 58int evm_init_hmac(struct inode *inode, const struct xattr *xattr,
56 char *hmac_val); 59 char *hmac_val);
57int evm_init_secfs(void); 60int evm_init_secfs(void);
diff --git a/security/integrity/evm/evm_crypto.c b/security/integrity/evm/evm_crypto.c
index bcd64baf8788..691f3e09154c 100644
--- a/security/integrity/evm/evm_crypto.c
+++ b/security/integrity/evm/evm_crypto.c
@@ -138,7 +138,7 @@ out:
138 * protection.) 138 * protection.)
139 */ 139 */
140static void hmac_add_misc(struct shash_desc *desc, struct inode *inode, 140static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
141 char *digest) 141 char type, char *digest)
142{ 142{
143 struct h_misc { 143 struct h_misc {
144 unsigned long ino; 144 unsigned long ino;
@@ -149,8 +149,13 @@ static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
149 } hmac_misc; 149 } hmac_misc;
150 150
151 memset(&hmac_misc, 0, sizeof(hmac_misc)); 151 memset(&hmac_misc, 0, sizeof(hmac_misc));
152 hmac_misc.ino = inode->i_ino; 152 /* Don't include the inode or generation number in portable
153 hmac_misc.generation = inode->i_generation; 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 }
154 /* The hmac uid and gid must be encoded in the initial user 159 /* The hmac uid and gid must be encoded in the initial user
155 * namespace (not the filesystems user namespace) as encoding 160 * namespace (not the filesystems user namespace) as encoding
156 * them in the filesystems user namespace allows an attack 161 * 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,
163 hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid); 168 hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid);
164 hmac_misc.mode = inode->i_mode; 169 hmac_misc.mode = inode->i_mode;
165 crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc)); 170 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)
167 crypto_shash_update(desc, &inode->i_sb->s_uuid.b[0], 173 crypto_shash_update(desc, &inode->i_sb->s_uuid.b[0],
168 sizeof(inode->i_sb->s_uuid)); 174 sizeof(inode->i_sb->s_uuid));
169 crypto_shash_final(desc, digest); 175 crypto_shash_final(desc, digest);
@@ -189,6 +195,7 @@ static int evm_calc_hmac_or_hash(struct dentry *dentry,
189 char *xattr_value = NULL; 195 char *xattr_value = NULL;
190 int error; 196 int error;
191 int size; 197 int size;
198 bool ima_present = false;
192 199
193 if (!(inode->i_opflags & IOP_XATTR)) 200 if (!(inode->i_opflags & IOP_XATTR))
194 return -EOPNOTSUPP; 201 return -EOPNOTSUPP;
@@ -199,11 +206,18 @@ static int evm_calc_hmac_or_hash(struct dentry *dentry,
199 206
200 error = -ENODATA; 207 error = -ENODATA;
201 for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) { 208 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
202 if ((req_xattr_name && req_xattr_value) 214 if ((req_xattr_name && req_xattr_value)
203 && !strcmp(*xattrname, req_xattr_name)) { 215 && !strcmp(*xattrname, req_xattr_name)) {
204 error = 0; 216 error = 0;
205 crypto_shash_update(desc, (const u8 *)req_xattr_value, 217 crypto_shash_update(desc, (const u8 *)req_xattr_value,
206 req_xattr_value_len); 218 req_xattr_value_len);
219 if (is_ima)
220 ima_present = true;
207 continue; 221 continue;
208 } 222 }
209 size = vfs_getxattr_alloc(dentry, *xattrname, 223 size = vfs_getxattr_alloc(dentry, *xattrname,
@@ -218,9 +232,14 @@ static int evm_calc_hmac_or_hash(struct dentry *dentry,
218 error = 0; 232 error = 0;
219 xattr_size = size; 233 xattr_size = size;
220 crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size); 234 crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
235 if (is_ima)
236 ima_present = true;
221 } 237 }
222 hmac_add_misc(desc, inode, digest); 238 hmac_add_misc(desc, inode, type, digest);
223 239
240 /* Portable EVM signatures must include an IMA hash */
241 if (type == EVM_XATTR_PORTABLE_DIGSIG && !ima_present)
242 return -EPERM;
224out: 243out:
225 kfree(xattr_value); 244 kfree(xattr_value);
226 kfree(desc); 245 kfree(desc);
@@ -232,17 +251,45 @@ int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
232 char *digest) 251 char *digest)
233{ 252{
234 return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value, 253 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);
236} 255}
237 256
238int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name, 257int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
239 const char *req_xattr_value, size_t req_xattr_value_len, 258 const char *req_xattr_value, size_t req_xattr_value_len,
240 char *digest) 259 char type, char *digest)
241{ 260{
242 return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value, 261 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
265static 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;
244} 290}
245 291
292
246/* 293/*
247 * Calculate the hmac and update security.evm xattr 294 * Calculate the hmac and update security.evm xattr
248 * 295 *
@@ -255,6 +302,16 @@ int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
255 struct evm_ima_xattr_data xattr_data; 302 struct evm_ima_xattr_data xattr_data;
256 int rc = 0; 303 int rc = 0;
257 304
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
258 rc = evm_calc_hmac(dentry, xattr_name, xattr_value, 315 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
259 xattr_value_len, xattr_data.digest); 316 xattr_value_len, xattr_data.digest);
260 if (rc == 0) { 317 if (rc == 0) {
@@ -280,7 +337,7 @@ int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
280 } 337 }
281 338
282 crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len); 339 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);
284 kfree(desc); 341 kfree(desc);
285 return 0; 342 return 0;
286} 343}
diff --git a/security/integrity/evm/evm_main.c b/security/integrity/evm/evm_main.c
index 9826c02e2db8..a8d502827270 100644
--- a/security/integrity/evm/evm_main.c
+++ b/security/integrity/evm/evm_main.c
@@ -31,7 +31,7 @@
31int evm_initialized; 31int evm_initialized;
32 32
33static char *integrity_status_msg[] = { 33static char *integrity_status_msg[] = {
34 "pass", "fail", "no_label", "no_xattrs", "unknown" 34 "pass", "pass_immutable", "fail", "no_label", "no_xattrs", "unknown"
35}; 35};
36char *evm_hmac = "hmac(sha1)"; 36char *evm_hmac = "hmac(sha1)";
37char *evm_hash = "sha1"; 37char *evm_hash = "sha1";
@@ -76,6 +76,11 @@ static void __init evm_init_config(void)
76 pr_info("HMAC attrs: 0x%x\n", evm_hmac_attrs); 76 pr_info("HMAC attrs: 0x%x\n", evm_hmac_attrs);
77} 77}
78 78
79static bool evm_key_loaded(void)
80{
81 return (bool)(evm_initialized & EVM_KEY_MASK);
82}
83
79static int evm_find_protected_xattrs(struct dentry *dentry) 84static int evm_find_protected_xattrs(struct dentry *dentry)
80{ 85{
81 struct inode *inode = d_backing_inode(dentry); 86 struct inode *inode = d_backing_inode(dentry);
@@ -123,7 +128,8 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
123 enum integrity_status evm_status = INTEGRITY_PASS; 128 enum integrity_status evm_status = INTEGRITY_PASS;
124 int rc, xattr_len; 129 int rc, xattr_len;
125 130
126 if (iint && iint->evm_status == INTEGRITY_PASS) 131 if (iint && (iint->evm_status == INTEGRITY_PASS ||
132 iint->evm_status == INTEGRITY_PASS_IMMUTABLE))
127 return iint->evm_status; 133 return iint->evm_status;
128 134
129 /* if status is not PASS, try to check again - against -ENOMEM */ 135 /* if status is not PASS, try to check again - against -ENOMEM */
@@ -164,22 +170,26 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
164 rc = -EINVAL; 170 rc = -EINVAL;
165 break; 171 break;
166 case EVM_IMA_XATTR_DIGSIG: 172 case EVM_IMA_XATTR_DIGSIG:
173 case EVM_XATTR_PORTABLE_DIGSIG:
167 rc = evm_calc_hash(dentry, xattr_name, xattr_value, 174 rc = evm_calc_hash(dentry, xattr_name, xattr_value,
168 xattr_value_len, calc.digest); 175 xattr_value_len, xattr_data->type,
176 calc.digest);
169 if (rc) 177 if (rc)
170 break; 178 break;
171 rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM, 179 rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
172 (const char *)xattr_data, xattr_len, 180 (const char *)xattr_data, xattr_len,
173 calc.digest, sizeof(calc.digest)); 181 calc.digest, sizeof(calc.digest));
174 if (!rc) { 182 if (!rc) {
175 /* Replace RSA with HMAC if not mounted readonly and 183 if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG) {
176 * not immutable 184 if (iint)
177 */ 185 iint->flags |= EVM_IMMUTABLE_DIGSIG;
178 if (!IS_RDONLY(d_backing_inode(dentry)) && 186 evm_status = INTEGRITY_PASS_IMMUTABLE;
179 !IS_IMMUTABLE(d_backing_inode(dentry))) 187 } else if (!IS_RDONLY(d_backing_inode(dentry)) &&
188 !IS_IMMUTABLE(d_backing_inode(dentry))) {
180 evm_update_evmxattr(dentry, xattr_name, 189 evm_update_evmxattr(dentry, xattr_name,
181 xattr_value, 190 xattr_value,
182 xattr_value_len); 191 xattr_value_len);
192 }
183 } 193 }
184 break; 194 break;
185 default: 195 default:
@@ -241,7 +251,7 @@ enum integrity_status evm_verifyxattr(struct dentry *dentry,
241 void *xattr_value, size_t xattr_value_len, 251 void *xattr_value, size_t xattr_value_len,
242 struct integrity_iint_cache *iint) 252 struct integrity_iint_cache *iint)
243{ 253{
244 if (!evm_initialized || !evm_protected_xattr(xattr_name)) 254 if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
245 return INTEGRITY_UNKNOWN; 255 return INTEGRITY_UNKNOWN;
246 256
247 if (!iint) { 257 if (!iint) {
@@ -265,7 +275,7 @@ static enum integrity_status evm_verify_current_integrity(struct dentry *dentry)
265{ 275{
266 struct inode *inode = d_backing_inode(dentry); 276 struct inode *inode = d_backing_inode(dentry);
267 277
268 if (!evm_initialized || !S_ISREG(inode->i_mode) || evm_fixmode) 278 if (!evm_key_loaded() || !S_ISREG(inode->i_mode) || evm_fixmode)
269 return 0; 279 return 0;
270 return evm_verify_hmac(dentry, NULL, NULL, 0, NULL); 280 return evm_verify_hmac(dentry, NULL, NULL, 0, NULL);
271} 281}
@@ -280,7 +290,7 @@ static enum integrity_status evm_verify_current_integrity(struct dentry *dentry)
280 * affect security.evm. An interesting side affect of writing posix xattr 290 * affect security.evm. An interesting side affect of writing posix xattr
281 * acls is their modifying of the i_mode, which is included in security.evm. 291 * acls is their modifying of the i_mode, which is included in security.evm.
282 * For posix xattr acls only, permit security.evm, even if it currently 292 * For posix xattr acls only, permit security.evm, even if it currently
283 * doesn't exist, to be updated. 293 * doesn't exist, to be updated unless the EVM signature is immutable.
284 */ 294 */
285static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name, 295static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name,
286 const void *xattr_value, size_t xattr_value_len) 296 const void *xattr_value, size_t xattr_value_len)
@@ -299,6 +309,7 @@ static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name,
299 return 0; 309 return 0;
300 goto out; 310 goto out;
301 } 311 }
312
302 evm_status = evm_verify_current_integrity(dentry); 313 evm_status = evm_verify_current_integrity(dentry);
303 if (evm_status == INTEGRITY_NOXATTRS) { 314 if (evm_status == INTEGRITY_NOXATTRS) {
304 struct integrity_iint_cache *iint; 315 struct integrity_iint_cache *iint;
@@ -345,10 +356,17 @@ int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
345{ 356{
346 const struct evm_ima_xattr_data *xattr_data = xattr_value; 357 const struct evm_ima_xattr_data *xattr_data = xattr_value;
347 358
359 /* Policy permits modification of the protected xattrs even though
360 * there's no HMAC key loaded
361 */
362 if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
363 return 0;
364
348 if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) { 365 if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
349 if (!xattr_value_len) 366 if (!xattr_value_len)
350 return -EINVAL; 367 return -EINVAL;
351 if (xattr_data->type != EVM_IMA_XATTR_DIGSIG) 368 if (xattr_data->type != EVM_IMA_XATTR_DIGSIG &&
369 xattr_data->type != EVM_XATTR_PORTABLE_DIGSIG)
352 return -EPERM; 370 return -EPERM;
353 } 371 }
354 return evm_protect_xattr(dentry, xattr_name, xattr_value, 372 return evm_protect_xattr(dentry, xattr_name, xattr_value,
@@ -365,6 +383,12 @@ int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
365 */ 383 */
366int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name) 384int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name)
367{ 385{
386 /* Policy permits modification of the protected xattrs even though
387 * there's no HMAC key loaded
388 */
389 if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
390 return 0;
391
368 return evm_protect_xattr(dentry, xattr_name, NULL, 0); 392 return evm_protect_xattr(dentry, xattr_name, NULL, 0);
369} 393}
370 394
@@ -393,8 +417,8 @@ static void evm_reset_status(struct inode *inode)
393void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name, 417void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
394 const void *xattr_value, size_t xattr_value_len) 418 const void *xattr_value, size_t xattr_value_len)
395{ 419{
396 if (!evm_initialized || (!evm_protected_xattr(xattr_name) 420 if (!evm_key_loaded() || (!evm_protected_xattr(xattr_name)
397 && !posix_xattr_acl(xattr_name))) 421 && !posix_xattr_acl(xattr_name)))
398 return; 422 return;
399 423
400 evm_reset_status(dentry->d_inode); 424 evm_reset_status(dentry->d_inode);
@@ -414,7 +438,7 @@ void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
414 */ 438 */
415void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name) 439void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
416{ 440{
417 if (!evm_initialized || !evm_protected_xattr(xattr_name)) 441 if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
418 return; 442 return;
419 443
420 evm_reset_status(dentry->d_inode); 444 evm_reset_status(dentry->d_inode);
@@ -425,12 +449,21 @@ void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
425/** 449/**
426 * evm_inode_setattr - prevent updating an invalid EVM extended attribute 450 * evm_inode_setattr - prevent updating an invalid EVM extended attribute
427 * @dentry: pointer to the affected dentry 451 * @dentry: pointer to the affected dentry
452 *
453 * Permit update of file attributes when files have a valid EVM signature,
454 * except in the case of them having an immutable portable signature.
428 */ 455 */
429int evm_inode_setattr(struct dentry *dentry, struct iattr *attr) 456int evm_inode_setattr(struct dentry *dentry, struct iattr *attr)
430{ 457{
431 unsigned int ia_valid = attr->ia_valid; 458 unsigned int ia_valid = attr->ia_valid;
432 enum integrity_status evm_status; 459 enum integrity_status evm_status;
433 460
461 /* Policy permits modification of the protected attrs even though
462 * there's no HMAC key loaded
463 */
464 if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
465 return 0;
466
434 if (!(ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))) 467 if (!(ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)))
435 return 0; 468 return 0;
436 evm_status = evm_verify_current_integrity(dentry); 469 evm_status = evm_verify_current_integrity(dentry);
@@ -456,7 +489,7 @@ int evm_inode_setattr(struct dentry *dentry, struct iattr *attr)
456 */ 489 */
457void evm_inode_post_setattr(struct dentry *dentry, int ia_valid) 490void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
458{ 491{
459 if (!evm_initialized) 492 if (!evm_key_loaded())
460 return; 493 return;
461 494
462 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) 495 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
@@ -473,7 +506,7 @@ int evm_inode_init_security(struct inode *inode,
473 struct evm_ima_xattr_data *xattr_data; 506 struct evm_ima_xattr_data *xattr_data;
474 int rc; 507 int rc;
475 508
476 if (!evm_initialized || !evm_protected_xattr(lsm_xattr->name)) 509 if (!evm_key_loaded() || !evm_protected_xattr(lsm_xattr->name))
477 return 0; 510 return 0;
478 511
479 xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS); 512 xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);
diff --git a/security/integrity/evm/evm_secfs.c b/security/integrity/evm/evm_secfs.c
index 319cf16d6603..feba03bbedae 100644
--- a/security/integrity/evm/evm_secfs.c
+++ b/security/integrity/evm/evm_secfs.c
@@ -40,7 +40,7 @@ static ssize_t evm_read_key(struct file *filp, char __user *buf,
40 if (*ppos != 0) 40 if (*ppos != 0)
41 return 0; 41 return 0;
42 42
43 sprintf(temp, "%d", (evm_initialized & ~EVM_SETUP)); 43 sprintf(temp, "%d", (evm_initialized & ~EVM_SETUP_COMPLETE));
44 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); 44 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
45 45
46 return rc; 46 return rc;
@@ -63,7 +63,7 @@ static ssize_t evm_write_key(struct file *file, const char __user *buf,
63{ 63{
64 int i, ret; 64 int i, ret;
65 65
66 if (!capable(CAP_SYS_ADMIN) || (evm_initialized & EVM_SETUP)) 66 if (!capable(CAP_SYS_ADMIN) || (evm_initialized & EVM_SETUP_COMPLETE))
67 return -EPERM; 67 return -EPERM;
68 68
69 ret = kstrtoint_from_user(buf, count, 0, &i); 69 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,
75 if (!i || (i & ~EVM_INIT_MASK) != 0) 75 if (!i || (i & ~EVM_INIT_MASK) != 0)
76 return -EINVAL; 76 return -EINVAL;
77 77
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
78 if (i & EVM_INIT_HMAC) { 86 if (i & EVM_INIT_HMAC) {
79 ret = evm_init_key(); 87 ret = evm_init_key();
80 if (ret != 0) 88 if (ret != 0)
81 return ret; 89 return ret;
82 /* Forbid further writes after the symmetric key is loaded */ 90 /* Forbid further writes after the symmetric key is loaded */
83 i |= EVM_SETUP; 91 i |= EVM_SETUP_COMPLETE;
84 } 92 }
85 93
86 evm_initialized |= i; 94 evm_initialized |= i;
87 95
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
88 return count; 102 return count;
89} 103}
90 104
diff --git a/security/integrity/iint.c b/security/integrity/iint.c
index c84e05866052..fc38ca08dbb5 100644
--- a/security/integrity/iint.c
+++ b/security/integrity/iint.c
@@ -153,14 +153,12 @@ static void init_once(void *foo)
153 struct integrity_iint_cache *iint = foo; 153 struct integrity_iint_cache *iint = foo;
154 154
155 memset(iint, 0, sizeof(*iint)); 155 memset(iint, 0, sizeof(*iint));
156 iint->version = 0;
157 iint->flags = 0UL;
158 iint->ima_file_status = INTEGRITY_UNKNOWN; 156 iint->ima_file_status = INTEGRITY_UNKNOWN;
159 iint->ima_mmap_status = INTEGRITY_UNKNOWN; 157 iint->ima_mmap_status = INTEGRITY_UNKNOWN;
160 iint->ima_bprm_status = INTEGRITY_UNKNOWN; 158 iint->ima_bprm_status = INTEGRITY_UNKNOWN;
161 iint->ima_read_status = INTEGRITY_UNKNOWN; 159 iint->ima_read_status = INTEGRITY_UNKNOWN;
162 iint->evm_status = INTEGRITY_UNKNOWN; 160 iint->evm_status = INTEGRITY_UNKNOWN;
163 iint->measured_pcrs = 0; 161 mutex_init(&iint->mutex);
164} 162}
165 163
166static int __init integrity_iintcache_init(void) 164static int __init integrity_iintcache_init(void)
diff --git a/security/integrity/ima/ima_api.c b/security/integrity/ima/ima_api.c
index c6ae42266270..08fe405338e1 100644
--- a/security/integrity/ima/ima_api.c
+++ b/security/integrity/ima/ima_api.c
@@ -175,7 +175,7 @@ err_out:
175 */ 175 */
176int ima_get_action(struct inode *inode, int mask, enum ima_hooks func, int *pcr) 176int ima_get_action(struct inode *inode, int mask, enum ima_hooks func, int *pcr)
177{ 177{
178 int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE; 178 int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE | IMA_HASH;
179 179
180 flags &= ima_policy_flag; 180 flags &= ima_policy_flag;
181 181
diff --git a/security/integrity/ima/ima_appraise.c b/security/integrity/ima/ima_appraise.c
index 65fbcf3c32c7..f2803a40ff82 100644
--- a/security/integrity/ima/ima_appraise.c
+++ b/security/integrity/ima/ima_appraise.c
@@ -46,14 +46,15 @@ bool is_ima_appraise_enabled(void)
46/* 46/*
47 * ima_must_appraise - set appraise flag 47 * ima_must_appraise - set appraise flag
48 * 48 *
49 * Return 1 to appraise 49 * Return 1 to appraise or hash
50 */ 50 */
51int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func) 51int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
52{ 52{
53 if (!ima_appraise) 53 if (!ima_appraise)
54 return 0; 54 return 0;
55 55
56 return ima_match_policy(inode, func, mask, IMA_APPRAISE, NULL); 56 return ima_match_policy(inode, func, mask, IMA_APPRAISE | IMA_HASH,
57 NULL);
57} 58}
58 59
59static int ima_fix_xattr(struct dentry *dentry, 60static int ima_fix_xattr(struct dentry *dentry,
@@ -223,13 +224,16 @@ int ima_appraise_measurement(enum ima_hooks func,
223 if (opened & FILE_CREATED) 224 if (opened & FILE_CREATED)
224 iint->flags |= IMA_NEW_FILE; 225 iint->flags |= IMA_NEW_FILE;
225 if ((iint->flags & IMA_NEW_FILE) && 226 if ((iint->flags & IMA_NEW_FILE) &&
226 !(iint->flags & IMA_DIGSIG_REQUIRED)) 227 (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
228 (inode->i_size == 0)))
227 status = INTEGRITY_PASS; 229 status = INTEGRITY_PASS;
228 goto out; 230 goto out;
229 } 231 }
230 232
231 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint); 233 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
232 if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) { 234 if ((status != INTEGRITY_PASS) &&
235 (status != INTEGRITY_PASS_IMMUTABLE) &&
236 (status != INTEGRITY_UNKNOWN)) {
233 if ((status == INTEGRITY_NOLABEL) 237 if ((status == INTEGRITY_NOLABEL)
234 || (status == INTEGRITY_NOXATTRS)) 238 || (status == INTEGRITY_NOXATTRS))
235 cause = "missing-HMAC"; 239 cause = "missing-HMAC";
@@ -248,6 +252,7 @@ int ima_appraise_measurement(enum ima_hooks func,
248 status = INTEGRITY_FAIL; 252 status = INTEGRITY_FAIL;
249 break; 253 break;
250 } 254 }
255 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
251 if (xattr_len - sizeof(xattr_value->type) - hash_start >= 256 if (xattr_len - sizeof(xattr_value->type) - hash_start >=
252 iint->ima_hash->length) 257 iint->ima_hash->length)
253 /* xattr length may be longer. md5 hash in previous 258 /* xattr length may be longer. md5 hash in previous
@@ -266,7 +271,7 @@ int ima_appraise_measurement(enum ima_hooks func,
266 status = INTEGRITY_PASS; 271 status = INTEGRITY_PASS;
267 break; 272 break;
268 case EVM_IMA_XATTR_DIGSIG: 273 case EVM_IMA_XATTR_DIGSIG:
269 iint->flags |= IMA_DIGSIG; 274 set_bit(IMA_DIGSIG, &iint->atomic_flags);
270 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA, 275 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
271 (const char *)xattr_value, rc, 276 (const char *)xattr_value, rc,
272 iint->ima_hash->digest, 277 iint->ima_hash->digest,
@@ -317,17 +322,20 @@ void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
317 int rc = 0; 322 int rc = 0;
318 323
319 /* do not collect and update hash for digital signatures */ 324 /* do not collect and update hash for digital signatures */
320 if (iint->flags & IMA_DIGSIG) 325 if (test_bit(IMA_DIGSIG, &iint->atomic_flags))
321 return; 326 return;
322 327
323 if (iint->ima_file_status != INTEGRITY_PASS) 328 if ((iint->ima_file_status != INTEGRITY_PASS) &&
329 !(iint->flags & IMA_HASH))
324 return; 330 return;
325 331
326 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo); 332 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo);
327 if (rc < 0) 333 if (rc < 0)
328 return; 334 return;
329 335
336 inode_lock(file_inode(file));
330 ima_fix_xattr(dentry, iint); 337 ima_fix_xattr(dentry, iint);
338 inode_unlock(file_inode(file));
331} 339}
332 340
333/** 341/**
@@ -343,23 +351,21 @@ void ima_inode_post_setattr(struct dentry *dentry)
343{ 351{
344 struct inode *inode = d_backing_inode(dentry); 352 struct inode *inode = d_backing_inode(dentry);
345 struct integrity_iint_cache *iint; 353 struct integrity_iint_cache *iint;
346 int must_appraise; 354 int action;
347 355
348 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode) 356 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
349 || !(inode->i_opflags & IOP_XATTR)) 357 || !(inode->i_opflags & IOP_XATTR))
350 return; 358 return;
351 359
352 must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR); 360 action = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
361 if (!action)
362 __vfs_removexattr(dentry, XATTR_NAME_IMA);
353 iint = integrity_iint_find(inode); 363 iint = integrity_iint_find(inode);
354 if (iint) { 364 if (iint) {
355 iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED | 365 set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);
356 IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK | 366 if (!action)
357 IMA_ACTION_RULE_FLAGS); 367 clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
358 if (must_appraise)
359 iint->flags |= IMA_APPRAISE;
360 } 368 }
361 if (!must_appraise)
362 __vfs_removexattr(dentry, XATTR_NAME_IMA);
363} 369}
364 370
365/* 371/*
@@ -388,12 +394,12 @@ static void ima_reset_appraise_flags(struct inode *inode, int digsig)
388 iint = integrity_iint_find(inode); 394 iint = integrity_iint_find(inode);
389 if (!iint) 395 if (!iint)
390 return; 396 return;
391
392 iint->flags &= ~IMA_DONE_MASK;
393 iint->measured_pcrs = 0; 397 iint->measured_pcrs = 0;
398 set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);
394 if (digsig) 399 if (digsig)
395 iint->flags |= IMA_DIGSIG; 400 set_bit(IMA_DIGSIG, &iint->atomic_flags);
396 return; 401 else
402 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
397} 403}
398 404
399int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name, 405int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
diff --git a/security/integrity/ima/ima_main.c b/security/integrity/ima/ima_main.c
index 06a70c5a2329..061425dd6400 100644
--- a/security/integrity/ima/ima_main.c
+++ b/security/integrity/ima/ima_main.c
@@ -85,10 +85,10 @@ static void ima_rdwr_violation_check(struct file *file,
85 struct integrity_iint_cache *iint, 85 struct integrity_iint_cache *iint,
86 int must_measure, 86 int must_measure,
87 char **pathbuf, 87 char **pathbuf,
88 const char **pathname) 88 const char **pathname,
89 char *filename)
89{ 90{
90 struct inode *inode = file_inode(file); 91 struct inode *inode = file_inode(file);
91 char filename[NAME_MAX];
92 fmode_t mode = file->f_mode; 92 fmode_t mode = file->f_mode;
93 bool send_tomtou = false, send_writers = false; 93 bool send_tomtou = false, send_writers = false;
94 94
@@ -97,10 +97,13 @@ static void ima_rdwr_violation_check(struct file *file,
97 if (!iint) 97 if (!iint)
98 iint = integrity_iint_find(inode); 98 iint = integrity_iint_find(inode);
99 /* IMA_MEASURE is set from reader side */ 99 /* IMA_MEASURE is set from reader side */
100 if (iint && (iint->flags & IMA_MEASURE)) 100 if (iint && test_bit(IMA_MUST_MEASURE,
101 &iint->atomic_flags))
101 send_tomtou = true; 102 send_tomtou = true;
102 } 103 }
103 } else { 104 } else {
105 if (must_measure)
106 set_bit(IMA_MUST_MEASURE, &iint->atomic_flags);
104 if ((atomic_read(&inode->i_writecount) > 0) && must_measure) 107 if ((atomic_read(&inode->i_writecount) > 0) && must_measure)
105 send_writers = true; 108 send_writers = true;
106 } 109 }
@@ -122,22 +125,25 @@ static void ima_check_last_writer(struct integrity_iint_cache *iint,
122 struct inode *inode, struct file *file) 125 struct inode *inode, struct file *file)
123{ 126{
124 fmode_t mode = file->f_mode; 127 fmode_t mode = file->f_mode;
128 bool update;
125 129
126 if (!(mode & FMODE_WRITE)) 130 if (!(mode & FMODE_WRITE))
127 return; 131 return;
128 132
129 inode_lock(inode); 133 mutex_lock(&iint->mutex);
130 if (atomic_read(&inode->i_writecount) == 1) { 134 if (atomic_read(&inode->i_writecount) == 1) {
135 update = test_and_clear_bit(IMA_UPDATE_XATTR,
136 &iint->atomic_flags);
131 if (!IS_I_VERSION(inode) || 137 if (!IS_I_VERSION(inode) ||
132 inode_cmp_iversion(inode, iint->version) || 138 inode_cmp_iversion(inode, iint->version) ||
133 (iint->flags & IMA_NEW_FILE)) { 139 (iint->flags & IMA_NEW_FILE)) {
134 iint->flags &= ~(IMA_DONE_MASK | IMA_NEW_FILE); 140 iint->flags &= ~(IMA_DONE_MASK | IMA_NEW_FILE);
135 iint->measured_pcrs = 0; 141 iint->measured_pcrs = 0;
136 if (iint->flags & IMA_APPRAISE) 142 if (update)
137 ima_update_xattr(iint, file); 143 ima_update_xattr(iint, file);
138 } 144 }
139 } 145 }
140 inode_unlock(inode); 146 mutex_unlock(&iint->mutex);
141} 147}
142 148
143/** 149/**
@@ -170,7 +176,7 @@ static int process_measurement(struct file *file, char *buf, loff_t size,
170 char *pathbuf = NULL; 176 char *pathbuf = NULL;
171 char filename[NAME_MAX]; 177 char filename[NAME_MAX];
172 const char *pathname = NULL; 178 const char *pathname = NULL;
173 int rc = -ENOMEM, action, must_appraise; 179 int rc = 0, action, must_appraise = 0;
174 int pcr = CONFIG_IMA_MEASURE_PCR_IDX; 180 int pcr = CONFIG_IMA_MEASURE_PCR_IDX;
175 struct evm_ima_xattr_data *xattr_value = NULL; 181 struct evm_ima_xattr_data *xattr_value = NULL;
176 int xattr_len = 0; 182 int xattr_len = 0;
@@ -201,17 +207,31 @@ static int process_measurement(struct file *file, char *buf, loff_t size,
201 if (action) { 207 if (action) {
202 iint = integrity_inode_get(inode); 208 iint = integrity_inode_get(inode);
203 if (!iint) 209 if (!iint)
204 goto out; 210 rc = -ENOMEM;
205 } 211 }
206 212
207 if (violation_check) { 213 if (!rc && violation_check)
208 ima_rdwr_violation_check(file, iint, action & IMA_MEASURE, 214 ima_rdwr_violation_check(file, iint, action & IMA_MEASURE,
209 &pathbuf, &pathname); 215 &pathbuf, &pathname, filename);
210 if (!action) { 216
211 rc = 0; 217 inode_unlock(inode);
212 goto out_free; 218
213 } 219 if (rc)
214 } 220 goto out;
221 if (!action)
222 goto out;
223
224 mutex_lock(&iint->mutex);
225
226 if (test_and_clear_bit(IMA_CHANGE_ATTR, &iint->atomic_flags))
227 /* reset appraisal flags if ima_inode_post_setattr was called */
228 iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED |
229 IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK |
230 IMA_ACTION_FLAGS);
231
232 if (test_and_clear_bit(IMA_CHANGE_XATTR, &iint->atomic_flags))
233 /* reset all flags if ima_inode_setxattr was called */
234 iint->flags &= ~IMA_DONE_MASK;
215 235
216 /* Determine if already appraised/measured based on bitmask 236 /* Determine if already appraised/measured based on bitmask
217 * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED, 237 * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED,
@@ -225,11 +245,23 @@ static int process_measurement(struct file *file, char *buf, loff_t size,
225 if ((action & IMA_MEASURE) && (iint->measured_pcrs & (0x1 << pcr))) 245 if ((action & IMA_MEASURE) && (iint->measured_pcrs & (0x1 << pcr)))
226 action ^= IMA_MEASURE; 246 action ^= IMA_MEASURE;
227 247
248 /* HASH sets the digital signature and update flags, nothing else */
249 if ((action & IMA_HASH) &&
250 !(test_bit(IMA_DIGSIG, &iint->atomic_flags))) {
251 xattr_len = ima_read_xattr(file_dentry(file), &xattr_value);
252 if ((xattr_value && xattr_len > 2) &&
253 (xattr_value->type == EVM_IMA_XATTR_DIGSIG))
254 set_bit(IMA_DIGSIG, &iint->atomic_flags);
255 iint->flags |= IMA_HASHED;
256 action ^= IMA_HASH;
257 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
258 }
259
228 /* Nothing to do, just return existing appraised status */ 260 /* Nothing to do, just return existing appraised status */
229 if (!action) { 261 if (!action) {
230 if (must_appraise) 262 if (must_appraise)
231 rc = ima_get_cache_status(iint, func); 263 rc = ima_get_cache_status(iint, func);
232 goto out_digsig; 264 goto out_locked;
233 } 265 }
234 266
235 template_desc = ima_template_desc_current(); 267 template_desc = ima_template_desc_current();
@@ -242,7 +274,7 @@ static int process_measurement(struct file *file, char *buf, loff_t size,
242 274
243 rc = ima_collect_measurement(iint, file, buf, size, hash_algo); 275 rc = ima_collect_measurement(iint, file, buf, size, hash_algo);
244 if (rc != 0 && rc != -EBADF && rc != -EINVAL) 276 if (rc != 0 && rc != -EBADF && rc != -EINVAL)
245 goto out_digsig; 277 goto out_locked;
246 278
247 if (!pathbuf) /* ima_rdwr_violation possibly pre-fetched */ 279 if (!pathbuf) /* ima_rdwr_violation possibly pre-fetched */
248 pathname = ima_d_path(&file->f_path, &pathbuf, filename); 280 pathname = ima_d_path(&file->f_path, &pathbuf, filename);
@@ -250,26 +282,32 @@ static int process_measurement(struct file *file, char *buf, loff_t size,
250 if (action & IMA_MEASURE) 282 if (action & IMA_MEASURE)
251 ima_store_measurement(iint, file, pathname, 283 ima_store_measurement(iint, file, pathname,
252 xattr_value, xattr_len, pcr); 284 xattr_value, xattr_len, pcr);
253 if (rc == 0 && (action & IMA_APPRAISE_SUBMASK)) 285 if (rc == 0 && (action & IMA_APPRAISE_SUBMASK)) {
286 inode_lock(inode);
254 rc = ima_appraise_measurement(func, iint, file, pathname, 287 rc = ima_appraise_measurement(func, iint, file, pathname,
255 xattr_value, xattr_len, opened); 288 xattr_value, xattr_len, opened);
289 inode_unlock(inode);
290 }
256 if (action & IMA_AUDIT) 291 if (action & IMA_AUDIT)
257 ima_audit_measurement(iint, pathname); 292 ima_audit_measurement(iint, pathname);
258 293
259 if ((file->f_flags & O_DIRECT) && (iint->flags & IMA_PERMIT_DIRECTIO)) 294 if ((file->f_flags & O_DIRECT) && (iint->flags & IMA_PERMIT_DIRECTIO))
260 rc = 0; 295 rc = 0;
261out_digsig: 296out_locked:
262 if ((mask & MAY_WRITE) && (iint->flags & IMA_DIGSIG) && 297 if ((mask & MAY_WRITE) && test_bit(IMA_DIGSIG, &iint->atomic_flags) &&
263 !(iint->flags & IMA_NEW_FILE)) 298 !(iint->flags & IMA_NEW_FILE))
264 rc = -EACCES; 299 rc = -EACCES;
300 mutex_unlock(&iint->mutex);
265 kfree(xattr_value); 301 kfree(xattr_value);
266out_free: 302out:
267 if (pathbuf) 303 if (pathbuf)
268 __putname(pathbuf); 304 __putname(pathbuf);
269out: 305 if (must_appraise) {
270 inode_unlock(inode); 306 if (rc && (ima_appraise & IMA_APPRAISE_ENFORCE))
271 if ((rc && must_appraise) && (ima_appraise & IMA_APPRAISE_ENFORCE)) 307 return -EACCES;
272 return -EACCES; 308 if (file->f_mode & FMODE_WRITE)
309 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
310 }
273 return 0; 311 return 0;
274} 312}
275 313
@@ -368,8 +406,10 @@ int ima_read_file(struct file *file, enum kernel_read_file_id read_id)
368 406
369 if (!file && read_id == READING_MODULE) { 407 if (!file && read_id == READING_MODULE) {
370 if (!sig_enforce && (ima_appraise & IMA_APPRAISE_MODULES) && 408 if (!sig_enforce && (ima_appraise & IMA_APPRAISE_MODULES) &&
371 (ima_appraise & IMA_APPRAISE_ENFORCE)) 409 (ima_appraise & IMA_APPRAISE_ENFORCE)) {
410 pr_err("impossible to appraise a module without a file descriptor. sig_enforce kernel parameter might help\n");
372 return -EACCES; /* INTEGRITY_UNKNOWN */ 411 return -EACCES; /* INTEGRITY_UNKNOWN */
412 }
373 return 0; /* We rely on module signature checking */ 413 return 0; /* We rely on module signature checking */
374 } 414 }
375 return 0; 415 return 0;
diff --git a/security/integrity/ima/ima_policy.c b/security/integrity/ima/ima_policy.c
index ee4613fa5840..915f5572c6ff 100644
--- a/security/integrity/ima/ima_policy.c
+++ b/security/integrity/ima/ima_policy.c
@@ -40,6 +40,8 @@
40#define APPRAISE 0x0004 /* same as IMA_APPRAISE */ 40#define APPRAISE 0x0004 /* same as IMA_APPRAISE */
41#define DONT_APPRAISE 0x0008 41#define DONT_APPRAISE 0x0008
42#define AUDIT 0x0040 42#define AUDIT 0x0040
43#define HASH 0x0100
44#define DONT_HASH 0x0200
43 45
44#define INVALID_PCR(a) (((a) < 0) || \ 46#define INVALID_PCR(a) (((a) < 0) || \
45 (a) >= (FIELD_SIZEOF(struct integrity_iint_cache, measured_pcrs) * 8)) 47 (a) >= (FIELD_SIZEOF(struct integrity_iint_cache, measured_pcrs) * 8))
@@ -380,8 +382,10 @@ int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
380 action |= entry->flags & IMA_ACTION_FLAGS; 382 action |= entry->flags & IMA_ACTION_FLAGS;
381 383
382 action |= entry->action & IMA_DO_MASK; 384 action |= entry->action & IMA_DO_MASK;
383 if (entry->action & IMA_APPRAISE) 385 if (entry->action & IMA_APPRAISE) {
384 action |= get_subaction(entry, func); 386 action |= get_subaction(entry, func);
387 action ^= IMA_HASH;
388 }
385 389
386 if (entry->action & IMA_DO_MASK) 390 if (entry->action & IMA_DO_MASK)
387 actmask &= ~(entry->action | entry->action << 1); 391 actmask &= ~(entry->action | entry->action << 1);
@@ -521,7 +525,7 @@ enum {
521 Opt_err = -1, 525 Opt_err = -1,
522 Opt_measure = 1, Opt_dont_measure, 526 Opt_measure = 1, Opt_dont_measure,
523 Opt_appraise, Opt_dont_appraise, 527 Opt_appraise, Opt_dont_appraise,
524 Opt_audit, 528 Opt_audit, Opt_hash, Opt_dont_hash,
525 Opt_obj_user, Opt_obj_role, Opt_obj_type, 529 Opt_obj_user, Opt_obj_role, Opt_obj_type,
526 Opt_subj_user, Opt_subj_role, Opt_subj_type, 530 Opt_subj_user, Opt_subj_role, Opt_subj_type,
527 Opt_func, Opt_mask, Opt_fsmagic, 531 Opt_func, Opt_mask, Opt_fsmagic,
@@ -538,6 +542,8 @@ static match_table_t policy_tokens = {
538 {Opt_appraise, "appraise"}, 542 {Opt_appraise, "appraise"},
539 {Opt_dont_appraise, "dont_appraise"}, 543 {Opt_dont_appraise, "dont_appraise"},
540 {Opt_audit, "audit"}, 544 {Opt_audit, "audit"},
545 {Opt_hash, "hash"},
546 {Opt_dont_hash, "dont_hash"},
541 {Opt_obj_user, "obj_user=%s"}, 547 {Opt_obj_user, "obj_user=%s"},
542 {Opt_obj_role, "obj_role=%s"}, 548 {Opt_obj_role, "obj_role=%s"},
543 {Opt_obj_type, "obj_type=%s"}, 549 {Opt_obj_type, "obj_type=%s"},
@@ -671,6 +677,22 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
671 677
672 entry->action = AUDIT; 678 entry->action = AUDIT;
673 break; 679 break;
680 case Opt_hash:
681 ima_log_string(ab, "action", "hash");
682
683 if (entry->action != UNKNOWN)
684 result = -EINVAL;
685
686 entry->action = HASH;
687 break;
688 case Opt_dont_hash:
689 ima_log_string(ab, "action", "dont_hash");
690
691 if (entry->action != UNKNOWN)
692 result = -EINVAL;
693
694 entry->action = DONT_HASH;
695 break;
674 case Opt_func: 696 case Opt_func:
675 ima_log_string(ab, "func", args[0].from); 697 ima_log_string(ab, "func", args[0].from);
676 698
@@ -743,7 +765,7 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
743 case Opt_fsuuid: 765 case Opt_fsuuid:
744 ima_log_string(ab, "fsuuid", args[0].from); 766 ima_log_string(ab, "fsuuid", args[0].from);
745 767
746 if (uuid_is_null(&entry->fsuuid)) { 768 if (!uuid_is_null(&entry->fsuuid)) {
747 result = -EINVAL; 769 result = -EINVAL;
748 break; 770 break;
749 } 771 }
@@ -1040,6 +1062,10 @@ int ima_policy_show(struct seq_file *m, void *v)
1040 seq_puts(m, pt(Opt_dont_appraise)); 1062 seq_puts(m, pt(Opt_dont_appraise));
1041 if (entry->action & AUDIT) 1063 if (entry->action & AUDIT)
1042 seq_puts(m, pt(Opt_audit)); 1064 seq_puts(m, pt(Opt_audit));
1065 if (entry->action & HASH)
1066 seq_puts(m, pt(Opt_hash));
1067 if (entry->action & DONT_HASH)
1068 seq_puts(m, pt(Opt_dont_hash));
1043 1069
1044 seq_puts(m, " "); 1070 seq_puts(m, " ");
1045 1071
diff --git a/security/integrity/ima/ima_template.c b/security/integrity/ima/ima_template.c
index 7412d0291ab9..30db39b23804 100644
--- a/security/integrity/ima/ima_template.c
+++ b/security/integrity/ima/ima_template.c
@@ -377,8 +377,7 @@ int ima_restore_measurement_list(loff_t size, void *buf)
377 break; 377 break;
378 378
379 if (hdr[HDR_TEMPLATE_NAME].len >= MAX_TEMPLATE_NAME_LEN) { 379 if (hdr[HDR_TEMPLATE_NAME].len >= MAX_TEMPLATE_NAME_LEN) {
380 pr_err("attempting to restore a template name \ 380 pr_err("attempting to restore a template name that is too long\n");
381 that is too long\n");
382 ret = -EINVAL; 381 ret = -EINVAL;
383 break; 382 break;
384 } 383 }
@@ -389,8 +388,8 @@ int ima_restore_measurement_list(loff_t size, void *buf)
389 template_name[hdr[HDR_TEMPLATE_NAME].len] = 0; 388 template_name[hdr[HDR_TEMPLATE_NAME].len] = 0;
390 389
391 if (strcmp(template_name, "ima") == 0) { 390 if (strcmp(template_name, "ima") == 0) {
392 pr_err("attempting to restore an unsupported \ 391 pr_err("attempting to restore an unsupported template \"%s\" failed\n",
393 template \"%s\" failed\n", template_name); 392 template_name);
394 ret = -EINVAL; 393 ret = -EINVAL;
395 break; 394 break;
396 } 395 }
@@ -410,8 +409,8 @@ int ima_restore_measurement_list(loff_t size, void *buf)
410 &(template_desc->fields), 409 &(template_desc->fields),
411 &(template_desc->num_fields)); 410 &(template_desc->num_fields));
412 if (ret < 0) { 411 if (ret < 0) {
413 pr_err("attempting to restore the template fmt \"%s\" \ 412 pr_err("attempting to restore the template fmt \"%s\" failed\n",
414 failed\n", template_desc->fmt); 413 template_desc->fmt);
415 ret = -EINVAL; 414 ret = -EINVAL;
416 break; 415 break;
417 } 416 }
diff --git a/security/integrity/integrity.h b/security/integrity/integrity.h
index e1bf040fb110..50a8e3365df7 100644
--- a/security/integrity/integrity.h
+++ b/security/integrity/integrity.h
@@ -25,39 +25,50 @@
25#define IMA_COLLECTED 0x00000020 25#define IMA_COLLECTED 0x00000020
26#define IMA_AUDIT 0x00000040 26#define IMA_AUDIT 0x00000040
27#define IMA_AUDITED 0x00000080 27#define IMA_AUDITED 0x00000080
28#define IMA_HASH 0x00000100
29#define IMA_HASHED 0x00000200
28 30
29/* iint cache flags */ 31/* iint cache flags */
30#define IMA_ACTION_FLAGS 0xff000000 32#define IMA_ACTION_FLAGS 0xff000000
31#define IMA_ACTION_RULE_FLAGS 0x06000000 33#define IMA_ACTION_RULE_FLAGS 0x06000000
32#define IMA_DIGSIG 0x01000000 34#define IMA_DIGSIG_REQUIRED 0x01000000
33#define IMA_DIGSIG_REQUIRED 0x02000000 35#define IMA_PERMIT_DIRECTIO 0x02000000
34#define IMA_PERMIT_DIRECTIO 0x04000000 36#define IMA_NEW_FILE 0x04000000
35#define IMA_NEW_FILE 0x08000000 37#define EVM_IMMUTABLE_DIGSIG 0x08000000
36 38
37#define IMA_DO_MASK (IMA_MEASURE | IMA_APPRAISE | IMA_AUDIT | \ 39#define IMA_DO_MASK (IMA_MEASURE | IMA_APPRAISE | IMA_AUDIT | \
38 IMA_APPRAISE_SUBMASK) 40 IMA_HASH | IMA_APPRAISE_SUBMASK)
39#define IMA_DONE_MASK (IMA_MEASURED | IMA_APPRAISED | IMA_AUDITED | \ 41#define IMA_DONE_MASK (IMA_MEASURED | IMA_APPRAISED | IMA_AUDITED | \
40 IMA_COLLECTED | IMA_APPRAISED_SUBMASK) 42 IMA_HASHED | IMA_COLLECTED | \
43 IMA_APPRAISED_SUBMASK)
41 44
42/* iint subaction appraise cache flags */ 45/* iint subaction appraise cache flags */
43#define IMA_FILE_APPRAISE 0x00000100 46#define IMA_FILE_APPRAISE 0x00001000
44#define IMA_FILE_APPRAISED 0x00000200 47#define IMA_FILE_APPRAISED 0x00002000
45#define IMA_MMAP_APPRAISE 0x00000400 48#define IMA_MMAP_APPRAISE 0x00004000
46#define IMA_MMAP_APPRAISED 0x00000800 49#define IMA_MMAP_APPRAISED 0x00008000
47#define IMA_BPRM_APPRAISE 0x00001000 50#define IMA_BPRM_APPRAISE 0x00010000
48#define IMA_BPRM_APPRAISED 0x00002000 51#define IMA_BPRM_APPRAISED 0x00020000
49#define IMA_READ_APPRAISE 0x00004000 52#define IMA_READ_APPRAISE 0x00040000
50#define IMA_READ_APPRAISED 0x00008000 53#define IMA_READ_APPRAISED 0x00080000
51#define IMA_APPRAISE_SUBMASK (IMA_FILE_APPRAISE | IMA_MMAP_APPRAISE | \ 54#define IMA_APPRAISE_SUBMASK (IMA_FILE_APPRAISE | IMA_MMAP_APPRAISE | \
52 IMA_BPRM_APPRAISE | IMA_READ_APPRAISE) 55 IMA_BPRM_APPRAISE | IMA_READ_APPRAISE)
53#define IMA_APPRAISED_SUBMASK (IMA_FILE_APPRAISED | IMA_MMAP_APPRAISED | \ 56#define IMA_APPRAISED_SUBMASK (IMA_FILE_APPRAISED | IMA_MMAP_APPRAISED | \
54 IMA_BPRM_APPRAISED | IMA_READ_APPRAISED) 57 IMA_BPRM_APPRAISED | IMA_READ_APPRAISED)
55 58
59/* iint cache atomic_flags */
60#define IMA_CHANGE_XATTR 0
61#define IMA_UPDATE_XATTR 1
62#define IMA_CHANGE_ATTR 2
63#define IMA_DIGSIG 3
64#define IMA_MUST_MEASURE 4
65
56enum evm_ima_xattr_type { 66enum evm_ima_xattr_type {
57 IMA_XATTR_DIGEST = 0x01, 67 IMA_XATTR_DIGEST = 0x01,
58 EVM_XATTR_HMAC, 68 EVM_XATTR_HMAC,
59 EVM_IMA_XATTR_DIGSIG, 69 EVM_IMA_XATTR_DIGSIG,
60 IMA_XATTR_DIGEST_NG, 70 IMA_XATTR_DIGEST_NG,
71 EVM_XATTR_PORTABLE_DIGSIG,
61 IMA_XATTR_LAST 72 IMA_XATTR_LAST
62}; 73};
63 74
@@ -100,10 +111,12 @@ struct signature_v2_hdr {
100/* integrity data associated with an inode */ 111/* integrity data associated with an inode */
101struct integrity_iint_cache { 112struct integrity_iint_cache {
102 struct rb_node rb_node; /* rooted in integrity_iint_tree */ 113 struct rb_node rb_node; /* rooted in integrity_iint_tree */
114 struct mutex mutex; /* protects: version, flags, digest */
103 struct inode *inode; /* back pointer to inode in question */ 115 struct inode *inode; /* back pointer to inode in question */
104 u64 version; /* track inode changes */ 116 u64 version; /* track inode changes */
105 unsigned long flags; 117 unsigned long flags;
106 unsigned long measured_pcrs; 118 unsigned long measured_pcrs;
119 unsigned long atomic_flags;
107 enum integrity_status ima_file_status:4; 120 enum integrity_status ima_file_status:4;
108 enum integrity_status ima_mmap_status:4; 121 enum integrity_status ima_mmap_status:4;
109 enum integrity_status ima_bprm_status:4; 122 enum integrity_status ima_bprm_status:4;