Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / security / integrity / ima / ima_appraise.c
blobf2803a40ff82ceb81160ca32d01eaf39263c1079
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 if (!ima_appraise)
54 return 0;
56 return ima_match_policy(inode, func, mask, IMA_APPRAISE | IMA_HASH,
57 NULL);
60 static int ima_fix_xattr(struct dentry *dentry,
61 struct integrity_iint_cache *iint)
63 int rc, offset;
64 u8 algo = iint->ima_hash->algo;
66 if (algo <= HASH_ALGO_SHA1) {
67 offset = 1;
68 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
69 } else {
70 offset = 0;
71 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
72 iint->ima_hash->xattr.ng.algo = algo;
74 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
75 &iint->ima_hash->xattr.data[offset],
76 (sizeof(iint->ima_hash->xattr) - offset) +
77 iint->ima_hash->length, 0);
78 return rc;
81 /* Return specific func appraised cached result */
82 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
83 enum ima_hooks func)
85 switch (func) {
86 case MMAP_CHECK:
87 return iint->ima_mmap_status;
88 case BPRM_CHECK:
89 return iint->ima_bprm_status;
90 case FILE_CHECK:
91 case POST_SETATTR:
92 return iint->ima_file_status;
93 case MODULE_CHECK ... MAX_CHECK - 1:
94 default:
95 return iint->ima_read_status;
99 static void ima_set_cache_status(struct integrity_iint_cache *iint,
100 enum ima_hooks func,
101 enum integrity_status status)
103 switch (func) {
104 case MMAP_CHECK:
105 iint->ima_mmap_status = status;
106 break;
107 case BPRM_CHECK:
108 iint->ima_bprm_status = status;
109 break;
110 case FILE_CHECK:
111 case POST_SETATTR:
112 iint->ima_file_status = status;
113 break;
114 case MODULE_CHECK ... MAX_CHECK - 1:
115 default:
116 iint->ima_read_status = status;
117 break;
121 static void ima_cache_flags(struct integrity_iint_cache *iint,
122 enum ima_hooks func)
124 switch (func) {
125 case MMAP_CHECK:
126 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
127 break;
128 case BPRM_CHECK:
129 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
130 break;
131 case FILE_CHECK:
132 case POST_SETATTR:
133 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
134 break;
135 case MODULE_CHECK ... MAX_CHECK - 1:
136 default:
137 iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
138 break;
142 enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
143 int xattr_len)
145 struct signature_v2_hdr *sig;
146 enum hash_algo ret;
148 if (!xattr_value || xattr_len < 2)
149 /* return default hash algo */
150 return ima_hash_algo;
152 switch (xattr_value->type) {
153 case EVM_IMA_XATTR_DIGSIG:
154 sig = (typeof(sig))xattr_value;
155 if (sig->version != 2 || xattr_len <= sizeof(*sig))
156 return ima_hash_algo;
157 return sig->hash_algo;
158 break;
159 case IMA_XATTR_DIGEST_NG:
160 ret = xattr_value->digest[0];
161 if (ret < HASH_ALGO__LAST)
162 return ret;
163 break;
164 case IMA_XATTR_DIGEST:
165 /* this is for backward compatibility */
166 if (xattr_len == 21) {
167 unsigned int zero = 0;
168 if (!memcmp(&xattr_value->digest[16], &zero, 4))
169 return HASH_ALGO_MD5;
170 else
171 return HASH_ALGO_SHA1;
172 } else if (xattr_len == 17)
173 return HASH_ALGO_MD5;
174 break;
177 /* return default hash algo */
178 return ima_hash_algo;
181 int ima_read_xattr(struct dentry *dentry,
182 struct evm_ima_xattr_data **xattr_value)
184 ssize_t ret;
186 ret = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
187 0, GFP_NOFS);
188 if (ret == -EOPNOTSUPP)
189 ret = 0;
190 return ret;
194 * ima_appraise_measurement - appraise file measurement
196 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
197 * Assuming success, compare the xattr hash with the collected measurement.
199 * Return 0 on success, error code otherwise
201 int ima_appraise_measurement(enum ima_hooks func,
202 struct integrity_iint_cache *iint,
203 struct file *file, const unsigned char *filename,
204 struct evm_ima_xattr_data *xattr_value,
205 int xattr_len, int opened)
207 static const char op[] = "appraise_data";
208 char *cause = "unknown";
209 struct dentry *dentry = file_dentry(file);
210 struct inode *inode = d_backing_inode(dentry);
211 enum integrity_status status = INTEGRITY_UNKNOWN;
212 int rc = xattr_len, hash_start = 0;
214 if (!(inode->i_opflags & IOP_XATTR))
215 return INTEGRITY_UNKNOWN;
217 if (rc <= 0) {
218 if (rc && rc != -ENODATA)
219 goto out;
221 cause = iint->flags & IMA_DIGSIG_REQUIRED ?
222 "IMA-signature-required" : "missing-hash";
223 status = INTEGRITY_NOLABEL;
224 if (opened & FILE_CREATED)
225 iint->flags |= IMA_NEW_FILE;
226 if ((iint->flags & IMA_NEW_FILE) &&
227 (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
228 (inode->i_size == 0)))
229 status = INTEGRITY_PASS;
230 goto out;
233 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
234 if ((status != INTEGRITY_PASS) &&
235 (status != INTEGRITY_PASS_IMMUTABLE) &&
236 (status != INTEGRITY_UNKNOWN)) {
237 if ((status == INTEGRITY_NOLABEL)
238 || (status == INTEGRITY_NOXATTRS))
239 cause = "missing-HMAC";
240 else if (status == INTEGRITY_FAIL)
241 cause = "invalid-HMAC";
242 goto out;
244 switch (xattr_value->type) {
245 case IMA_XATTR_DIGEST_NG:
246 /* first byte contains algorithm id */
247 hash_start = 1;
248 /* fall through */
249 case IMA_XATTR_DIGEST:
250 if (iint->flags & IMA_DIGSIG_REQUIRED) {
251 cause = "IMA-signature-required";
252 status = INTEGRITY_FAIL;
253 break;
255 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
256 if (xattr_len - sizeof(xattr_value->type) - hash_start >=
257 iint->ima_hash->length)
258 /* xattr length may be longer. md5 hash in previous
259 version occupied 20 bytes in xattr, instead of 16
261 rc = memcmp(&xattr_value->digest[hash_start],
262 iint->ima_hash->digest,
263 iint->ima_hash->length);
264 else
265 rc = -EINVAL;
266 if (rc) {
267 cause = "invalid-hash";
268 status = INTEGRITY_FAIL;
269 break;
271 status = INTEGRITY_PASS;
272 break;
273 case EVM_IMA_XATTR_DIGSIG:
274 set_bit(IMA_DIGSIG, &iint->atomic_flags);
275 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
276 (const char *)xattr_value, rc,
277 iint->ima_hash->digest,
278 iint->ima_hash->length);
279 if (rc == -EOPNOTSUPP) {
280 status = INTEGRITY_UNKNOWN;
281 } else if (rc) {
282 cause = "invalid-signature";
283 status = INTEGRITY_FAIL;
284 } else {
285 status = INTEGRITY_PASS;
287 break;
288 default:
289 status = INTEGRITY_UNKNOWN;
290 cause = "unknown-ima-data";
291 break;
294 out:
295 if (status != INTEGRITY_PASS) {
296 if ((ima_appraise & IMA_APPRAISE_FIX) &&
297 (!xattr_value ||
298 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
299 if (!ima_fix_xattr(dentry, iint))
300 status = INTEGRITY_PASS;
301 } else if ((inode->i_size == 0) &&
302 (iint->flags & IMA_NEW_FILE) &&
303 (xattr_value &&
304 xattr_value->type == EVM_IMA_XATTR_DIGSIG)) {
305 status = INTEGRITY_PASS;
307 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
308 op, cause, rc, 0);
309 } else {
310 ima_cache_flags(iint, func);
312 ima_set_cache_status(iint, func, status);
313 return status;
317 * ima_update_xattr - update 'security.ima' hash value
319 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
321 struct dentry *dentry = file_dentry(file);
322 int rc = 0;
324 /* do not collect and update hash for digital signatures */
325 if (test_bit(IMA_DIGSIG, &iint->atomic_flags))
326 return;
328 if ((iint->ima_file_status != INTEGRITY_PASS) &&
329 !(iint->flags & IMA_HASH))
330 return;
332 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo);
333 if (rc < 0)
334 return;
336 inode_lock(file_inode(file));
337 ima_fix_xattr(dentry, iint);
338 inode_unlock(file_inode(file));
342 * ima_inode_post_setattr - reflect file metadata changes
343 * @dentry: pointer to the affected dentry
345 * Changes to a dentry's metadata might result in needing to appraise.
347 * This function is called from notify_change(), which expects the caller
348 * to lock the inode's i_mutex.
350 void ima_inode_post_setattr(struct dentry *dentry)
352 struct inode *inode = d_backing_inode(dentry);
353 struct integrity_iint_cache *iint;
354 int action;
356 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
357 || !(inode->i_opflags & IOP_XATTR))
358 return;
360 action = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
361 if (!action)
362 __vfs_removexattr(dentry, XATTR_NAME_IMA);
363 iint = integrity_iint_find(inode);
364 if (iint) {
365 set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);
366 if (!action)
367 clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
372 * ima_protect_xattr - protect 'security.ima'
374 * Ensure that not just anyone can modify or remove 'security.ima'.
376 static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
377 const void *xattr_value, size_t xattr_value_len)
379 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
380 if (!capable(CAP_SYS_ADMIN))
381 return -EPERM;
382 return 1;
384 return 0;
387 static void ima_reset_appraise_flags(struct inode *inode, int digsig)
389 struct integrity_iint_cache *iint;
391 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
392 return;
394 iint = integrity_iint_find(inode);
395 if (!iint)
396 return;
397 iint->measured_pcrs = 0;
398 set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);
399 if (digsig)
400 set_bit(IMA_DIGSIG, &iint->atomic_flags);
401 else
402 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
405 int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
406 const void *xattr_value, size_t xattr_value_len)
408 const struct evm_ima_xattr_data *xvalue = xattr_value;
409 int result;
411 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
412 xattr_value_len);
413 if (result == 1) {
414 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
415 return -EINVAL;
416 ima_reset_appraise_flags(d_backing_inode(dentry),
417 xvalue->type == EVM_IMA_XATTR_DIGSIG);
418 result = 0;
420 return result;
423 int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
425 int result;
427 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
428 if (result == 1) {
429 ima_reset_appraise_flags(d_backing_inode(dentry), 0);
430 result = 0;
432 return result;