Linux 4.19.133
[linux/fpc-iii.git] / security / integrity / ima / ima_appraise.c
blobdeec1804a00aa488d02b307371cfc18475127bc3
1 /*
2 * Copyright (C) 2011 IBM Corporation
4 * Author:
5 * Mimi Zohar <zohar@us.ibm.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2 of the License.
11 #include <linux/module.h>
12 #include <linux/file.h>
13 #include <linux/fs.h>
14 #include <linux/xattr.h>
15 #include <linux/magic.h>
16 #include <linux/ima.h>
17 #include <linux/evm.h>
19 #include "ima.h"
21 static int __init default_appraise_setup(char *str)
23 #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
24 if (strncmp(str, "off", 3) == 0)
25 ima_appraise = 0;
26 else if (strncmp(str, "log", 3) == 0)
27 ima_appraise = IMA_APPRAISE_LOG;
28 else if (strncmp(str, "fix", 3) == 0)
29 ima_appraise = IMA_APPRAISE_FIX;
30 #endif
31 return 1;
34 __setup("ima_appraise=", default_appraise_setup);
37 * is_ima_appraise_enabled - return appraise status
39 * Only return enabled, if not in ima_appraise="fix" or "log" modes.
41 bool is_ima_appraise_enabled(void)
43 return ima_appraise & IMA_APPRAISE_ENFORCE;
47 * ima_must_appraise - set appraise flag
49 * Return 1 to appraise or hash
51 int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
53 u32 secid;
55 if (!ima_appraise)
56 return 0;
58 security_task_getsecid(current, &secid);
59 return ima_match_policy(inode, current_cred(), secid, func, mask,
60 IMA_APPRAISE | IMA_HASH, NULL);
63 static int ima_fix_xattr(struct dentry *dentry,
64 struct integrity_iint_cache *iint)
66 int rc, offset;
67 u8 algo = iint->ima_hash->algo;
69 if (algo <= HASH_ALGO_SHA1) {
70 offset = 1;
71 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
72 } else {
73 offset = 0;
74 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
75 iint->ima_hash->xattr.ng.algo = algo;
77 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
78 &iint->ima_hash->xattr.data[offset],
79 (sizeof(iint->ima_hash->xattr) - offset) +
80 iint->ima_hash->length, 0);
81 return rc;
84 /* Return specific func appraised cached result */
85 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
86 enum ima_hooks func)
88 switch (func) {
89 case MMAP_CHECK:
90 return iint->ima_mmap_status;
91 case BPRM_CHECK:
92 return iint->ima_bprm_status;
93 case CREDS_CHECK:
94 return iint->ima_creds_status;
95 case FILE_CHECK:
96 case POST_SETATTR:
97 return iint->ima_file_status;
98 case MODULE_CHECK ... MAX_CHECK - 1:
99 default:
100 return iint->ima_read_status;
104 static void ima_set_cache_status(struct integrity_iint_cache *iint,
105 enum ima_hooks func,
106 enum integrity_status status)
108 switch (func) {
109 case MMAP_CHECK:
110 iint->ima_mmap_status = status;
111 break;
112 case BPRM_CHECK:
113 iint->ima_bprm_status = status;
114 break;
115 case CREDS_CHECK:
116 iint->ima_creds_status = status;
117 case FILE_CHECK:
118 case POST_SETATTR:
119 iint->ima_file_status = status;
120 break;
121 case MODULE_CHECK ... MAX_CHECK - 1:
122 default:
123 iint->ima_read_status = status;
124 break;
128 static void ima_cache_flags(struct integrity_iint_cache *iint,
129 enum ima_hooks func)
131 switch (func) {
132 case MMAP_CHECK:
133 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
134 break;
135 case BPRM_CHECK:
136 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
137 break;
138 case CREDS_CHECK:
139 iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED);
140 break;
141 case FILE_CHECK:
142 case POST_SETATTR:
143 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
144 break;
145 case MODULE_CHECK ... MAX_CHECK - 1:
146 default:
147 iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
148 break;
152 enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
153 int xattr_len)
155 struct signature_v2_hdr *sig;
156 enum hash_algo ret;
158 if (!xattr_value || xattr_len < 2)
159 /* return default hash algo */
160 return ima_hash_algo;
162 switch (xattr_value->type) {
163 case EVM_IMA_XATTR_DIGSIG:
164 sig = (typeof(sig))xattr_value;
165 if (sig->version != 2 || xattr_len <= sizeof(*sig))
166 return ima_hash_algo;
167 return sig->hash_algo;
168 break;
169 case IMA_XATTR_DIGEST_NG:
170 ret = xattr_value->digest[0];
171 if (ret < HASH_ALGO__LAST)
172 return ret;
173 break;
174 case IMA_XATTR_DIGEST:
175 /* this is for backward compatibility */
176 if (xattr_len == 21) {
177 unsigned int zero = 0;
178 if (!memcmp(&xattr_value->digest[16], &zero, 4))
179 return HASH_ALGO_MD5;
180 else
181 return HASH_ALGO_SHA1;
182 } else if (xattr_len == 17)
183 return HASH_ALGO_MD5;
184 break;
187 /* return default hash algo */
188 return ima_hash_algo;
191 int ima_read_xattr(struct dentry *dentry,
192 struct evm_ima_xattr_data **xattr_value)
194 ssize_t ret;
196 ret = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
197 0, GFP_NOFS);
198 if (ret == -EOPNOTSUPP)
199 ret = 0;
200 return ret;
204 * ima_appraise_measurement - appraise file measurement
206 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
207 * Assuming success, compare the xattr hash with the collected measurement.
209 * Return 0 on success, error code otherwise
211 int ima_appraise_measurement(enum ima_hooks func,
212 struct integrity_iint_cache *iint,
213 struct file *file, const unsigned char *filename,
214 struct evm_ima_xattr_data *xattr_value,
215 int xattr_len)
217 static const char op[] = "appraise_data";
218 const char *cause = "unknown";
219 struct dentry *dentry = file_dentry(file);
220 struct inode *inode = d_backing_inode(dentry);
221 enum integrity_status status = INTEGRITY_UNKNOWN;
222 int rc = xattr_len, hash_start = 0;
224 if (!(inode->i_opflags & IOP_XATTR))
225 return INTEGRITY_UNKNOWN;
227 if (rc <= 0) {
228 if (rc && rc != -ENODATA)
229 goto out;
231 cause = iint->flags & IMA_DIGSIG_REQUIRED ?
232 "IMA-signature-required" : "missing-hash";
233 status = INTEGRITY_NOLABEL;
234 if (file->f_mode & FMODE_CREATED)
235 iint->flags |= IMA_NEW_FILE;
236 if ((iint->flags & IMA_NEW_FILE) &&
237 (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
238 (inode->i_size == 0)))
239 status = INTEGRITY_PASS;
240 goto out;
243 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
244 switch (status) {
245 case INTEGRITY_PASS:
246 case INTEGRITY_PASS_IMMUTABLE:
247 case INTEGRITY_UNKNOWN:
248 break;
249 case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */
250 case INTEGRITY_NOLABEL: /* No security.evm xattr. */
251 cause = "missing-HMAC";
252 goto out;
253 case INTEGRITY_FAIL: /* Invalid HMAC/signature. */
254 cause = "invalid-HMAC";
255 goto out;
256 default:
257 WARN_ONCE(true, "Unexpected integrity status %d\n", status);
260 switch (xattr_value->type) {
261 case IMA_XATTR_DIGEST_NG:
262 /* first byte contains algorithm id */
263 hash_start = 1;
264 /* fall through */
265 case IMA_XATTR_DIGEST:
266 if (iint->flags & IMA_DIGSIG_REQUIRED) {
267 cause = "IMA-signature-required";
268 status = INTEGRITY_FAIL;
269 break;
271 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
272 if (xattr_len - sizeof(xattr_value->type) - hash_start >=
273 iint->ima_hash->length)
274 /* xattr length may be longer. md5 hash in previous
275 version occupied 20 bytes in xattr, instead of 16
277 rc = memcmp(&xattr_value->digest[hash_start],
278 iint->ima_hash->digest,
279 iint->ima_hash->length);
280 else
281 rc = -EINVAL;
282 if (rc) {
283 cause = "invalid-hash";
284 status = INTEGRITY_FAIL;
285 break;
287 status = INTEGRITY_PASS;
288 break;
289 case EVM_IMA_XATTR_DIGSIG:
290 set_bit(IMA_DIGSIG, &iint->atomic_flags);
291 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
292 (const char *)xattr_value, rc,
293 iint->ima_hash->digest,
294 iint->ima_hash->length);
295 if (rc == -EOPNOTSUPP) {
296 status = INTEGRITY_UNKNOWN;
297 } else if (rc) {
298 cause = "invalid-signature";
299 status = INTEGRITY_FAIL;
300 } else {
301 status = INTEGRITY_PASS;
303 break;
304 default:
305 status = INTEGRITY_UNKNOWN;
306 cause = "unknown-ima-data";
307 break;
310 out:
312 * File signatures on some filesystems can not be properly verified.
313 * When such filesystems are mounted by an untrusted mounter or on a
314 * system not willing to accept such a risk, fail the file signature
315 * verification.
317 if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
318 ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||
319 (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {
320 status = INTEGRITY_FAIL;
321 cause = "unverifiable-signature";
322 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
323 op, cause, rc, 0);
324 } else if (status != INTEGRITY_PASS) {
325 /* Fix mode, but don't replace file signatures. */
326 if ((ima_appraise & IMA_APPRAISE_FIX) &&
327 (!xattr_value ||
328 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
329 if (!ima_fix_xattr(dentry, iint))
330 status = INTEGRITY_PASS;
333 /* Permit new files with file signatures, but without data. */
334 if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&
335 xattr_value && xattr_value->type == EVM_IMA_XATTR_DIGSIG) {
336 status = INTEGRITY_PASS;
339 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
340 op, cause, rc, 0);
341 } else {
342 ima_cache_flags(iint, func);
345 ima_set_cache_status(iint, func, status);
346 return status;
350 * ima_update_xattr - update 'security.ima' hash value
352 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
354 struct dentry *dentry = file_dentry(file);
355 int rc = 0;
357 /* do not collect and update hash for digital signatures */
358 if (test_bit(IMA_DIGSIG, &iint->atomic_flags))
359 return;
361 if ((iint->ima_file_status != INTEGRITY_PASS) &&
362 !(iint->flags & IMA_HASH))
363 return;
365 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo);
366 if (rc < 0)
367 return;
369 inode_lock(file_inode(file));
370 ima_fix_xattr(dentry, iint);
371 inode_unlock(file_inode(file));
375 * ima_inode_post_setattr - reflect file metadata changes
376 * @dentry: pointer to the affected dentry
378 * Changes to a dentry's metadata might result in needing to appraise.
380 * This function is called from notify_change(), which expects the caller
381 * to lock the inode's i_mutex.
383 void ima_inode_post_setattr(struct dentry *dentry)
385 struct inode *inode = d_backing_inode(dentry);
386 struct integrity_iint_cache *iint;
387 int action;
389 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
390 || !(inode->i_opflags & IOP_XATTR))
391 return;
393 action = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
394 if (!action)
395 __vfs_removexattr(dentry, XATTR_NAME_IMA);
396 iint = integrity_iint_find(inode);
397 if (iint) {
398 set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);
399 if (!action)
400 clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
405 * ima_protect_xattr - protect 'security.ima'
407 * Ensure that not just anyone can modify or remove 'security.ima'.
409 static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
410 const void *xattr_value, size_t xattr_value_len)
412 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
413 if (!capable(CAP_SYS_ADMIN))
414 return -EPERM;
415 return 1;
417 return 0;
420 static void ima_reset_appraise_flags(struct inode *inode, int digsig)
422 struct integrity_iint_cache *iint;
424 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
425 return;
427 iint = integrity_iint_find(inode);
428 if (!iint)
429 return;
430 iint->measured_pcrs = 0;
431 set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);
432 if (digsig)
433 set_bit(IMA_DIGSIG, &iint->atomic_flags);
434 else
435 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
438 int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
439 const void *xattr_value, size_t xattr_value_len)
441 const struct evm_ima_xattr_data *xvalue = xattr_value;
442 int result;
444 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
445 xattr_value_len);
446 if (result == 1) {
447 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
448 return -EINVAL;
449 ima_reset_appraise_flags(d_backing_inode(dentry),
450 xvalue->type == EVM_IMA_XATTR_DIGSIG);
451 result = 0;
453 return result;
456 int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
458 int result;
460 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
461 if (result == 1) {
462 ima_reset_appraise_flags(d_backing_inode(dentry), 0);
463 result = 0;
465 return result;