evm: properly handle INTEGRITY_NOXATTRS EVM status
[deliverable/linux.git] / security / integrity / ima / ima_appraise.c
CommitLineData
2fe5d6de
MZ
1/*
2 * Copyright (C) 2011 IBM Corporation
3 *
4 * Author:
5 * Mimi Zohar <zohar@us.ibm.com>
6 *
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.
10 */
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>
3ea7a560 18#include <crypto/hash_info.h>
2fe5d6de
MZ
19
20#include "ima.h"
21
22static int __init default_appraise_setup(char *str)
23{
24 if (strncmp(str, "off", 3) == 0)
25 ima_appraise = 0;
26 else if (strncmp(str, "fix", 3) == 0)
27 ima_appraise = IMA_APPRAISE_FIX;
28 return 1;
29}
30
31__setup("ima_appraise=", default_appraise_setup);
32
33/*
34 * ima_must_appraise - set appraise flag
35 *
36 * Return 1 to appraise
37 */
d26e1936 38int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
2fe5d6de 39{
07f6a794
MZ
40 if (!ima_appraise)
41 return 0;
42
43 return ima_match_policy(inode, func, mask, IMA_APPRAISE);
2fe5d6de
MZ
44}
45
def3e8b9 46static int ima_fix_xattr(struct dentry *dentry,
c7c8bb23 47 struct integrity_iint_cache *iint)
2fe5d6de 48{
3ea7a560
DK
49 int rc, offset;
50 u8 algo = iint->ima_hash->algo;
51
52 if (algo <= HASH_ALGO_SHA1) {
53 offset = 1;
54 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
55 } else {
56 offset = 0;
57 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
58 iint->ima_hash->xattr.ng.algo = algo;
59 }
60 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
61 &iint->ima_hash->xattr.data[offset],
62 (sizeof(iint->ima_hash->xattr) - offset) +
63 iint->ima_hash->length, 0);
64 return rc;
2fe5d6de
MZ
65}
66
d79d72e0
MZ
67/* Return specific func appraised cached result */
68enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
69 int func)
70{
089bc8e9 71 switch (func) {
d79d72e0
MZ
72 case MMAP_CHECK:
73 return iint->ima_mmap_status;
74 case BPRM_CHECK:
75 return iint->ima_bprm_status;
76 case MODULE_CHECK:
77 return iint->ima_module_status;
5a9196d7
MZ
78 case FIRMWARE_CHECK:
79 return iint->ima_firmware_status;
d79d72e0
MZ
80 case FILE_CHECK:
81 default:
82 return iint->ima_file_status;
83 }
84}
85
86static void ima_set_cache_status(struct integrity_iint_cache *iint,
87 int func, enum integrity_status status)
88{
089bc8e9 89 switch (func) {
d79d72e0
MZ
90 case MMAP_CHECK:
91 iint->ima_mmap_status = status;
92 break;
93 case BPRM_CHECK:
94 iint->ima_bprm_status = status;
95 break;
96 case MODULE_CHECK:
97 iint->ima_module_status = status;
98 break;
5a9196d7
MZ
99 case FIRMWARE_CHECK:
100 iint->ima_firmware_status = status;
101 break;
d79d72e0
MZ
102 case FILE_CHECK:
103 default:
104 iint->ima_file_status = status;
105 break;
106 }
107}
108
109static void ima_cache_flags(struct integrity_iint_cache *iint, int func)
110{
089bc8e9 111 switch (func) {
d79d72e0
MZ
112 case MMAP_CHECK:
113 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
114 break;
115 case BPRM_CHECK:
116 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
117 break;
118 case MODULE_CHECK:
119 iint->flags |= (IMA_MODULE_APPRAISED | IMA_APPRAISED);
120 break;
5a9196d7
MZ
121 case FIRMWARE_CHECK:
122 iint->flags |= (IMA_FIRMWARE_APPRAISED | IMA_APPRAISED);
123 break;
d79d72e0
MZ
124 case FILE_CHECK:
125 default:
126 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
127 break;
128 }
129}
130
d3634d0f
DK
131void ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len,
132 struct ima_digest_data *hash)
133{
134 struct signature_v2_hdr *sig;
135
3ea7a560 136 if (!xattr_value || xattr_len < 2)
d3634d0f
DK
137 return;
138
3ea7a560
DK
139 switch (xattr_value->type) {
140 case EVM_IMA_XATTR_DIGSIG:
141 sig = (typeof(sig))xattr_value;
142 if (sig->version != 2 || xattr_len <= sizeof(*sig))
143 return;
144 hash->algo = sig->hash_algo;
145 break;
146 case IMA_XATTR_DIGEST_NG:
147 hash->algo = xattr_value->digest[0];
148 break;
149 case IMA_XATTR_DIGEST:
150 /* this is for backward compatibility */
151 if (xattr_len == 21) {
152 unsigned int zero = 0;
153 if (!memcmp(&xattr_value->digest[16], &zero, 4))
154 hash->algo = HASH_ALGO_MD5;
155 else
156 hash->algo = HASH_ALGO_SHA1;
157 } else if (xattr_len == 17)
158 hash->algo = HASH_ALGO_MD5;
159 break;
160 }
d3634d0f
DK
161}
162
163int ima_read_xattr(struct dentry *dentry,
164 struct evm_ima_xattr_data **xattr_value)
165{
166 struct inode *inode = dentry->d_inode;
167
168 if (!inode->i_op->getxattr)
169 return 0;
170
171 return vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
172 0, GFP_NOFS);
173}
174
2fe5d6de
MZ
175/*
176 * ima_appraise_measurement - appraise file measurement
177 *
178 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
179 * Assuming success, compare the xattr hash with the collected measurement.
180 *
181 * Return 0 on success, error code otherwise
182 */
d79d72e0 183int ima_appraise_measurement(int func, struct integrity_iint_cache *iint,
d3634d0f
DK
184 struct file *file, const unsigned char *filename,
185 struct evm_ima_xattr_data *xattr_value,
186 int xattr_len)
2fe5d6de 187{
52a13284
MZ
188 static const char op[] = "appraise_data";
189 char *cause = "unknown";
2fe5d6de
MZ
190 struct dentry *dentry = file->f_dentry;
191 struct inode *inode = dentry->d_inode;
2fe5d6de 192 enum integrity_status status = INTEGRITY_UNKNOWN;
3ea7a560 193 int rc = xattr_len, hash_start = 0;
2fe5d6de
MZ
194
195 if (!ima_appraise)
196 return 0;
197 if (!inode->i_op->getxattr)
198 return INTEGRITY_UNKNOWN;
199
2fe5d6de
MZ
200 if (rc <= 0) {
201 if (rc && rc != -ENODATA)
202 goto out;
203
204 cause = "missing-hash";
b151d6b0
DK
205 status = INTEGRITY_NOLABEL;
206 if (inode->i_size == 0) {
207 iint->flags |= IMA_NEW_FILE;
208 status = INTEGRITY_PASS;
209 }
2fe5d6de
MZ
210 goto out;
211 }
212
8606404f 213 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
2fe5d6de
MZ
214 if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) {
215 if ((status == INTEGRITY_NOLABEL)
216 || (status == INTEGRITY_NOXATTRS))
217 cause = "missing-HMAC";
218 else if (status == INTEGRITY_FAIL)
219 cause = "invalid-HMAC";
220 goto out;
221 }
8606404f 222 switch (xattr_value->type) {
3ea7a560
DK
223 case IMA_XATTR_DIGEST_NG:
224 /* first byte contains algorithm id */
225 hash_start = 1;
8606404f 226 case IMA_XATTR_DIGEST:
0e5a247c 227 if (iint->flags & IMA_DIGSIG_REQUIRED) {
7e9001f6 228 cause = "IMA-signature-required";
0e5a247c
DK
229 status = INTEGRITY_FAIL;
230 break;
231 }
3ea7a560
DK
232 if (xattr_len - sizeof(xattr_value->type) - hash_start >=
233 iint->ima_hash->length)
d3634d0f
DK
234 /* xattr length may be longer. md5 hash in previous
235 version occupied 20 bytes in xattr, instead of 16
236 */
3ea7a560 237 rc = memcmp(&xattr_value->digest[hash_start],
a35c3fb6
DK
238 iint->ima_hash->digest,
239 iint->ima_hash->length);
c7c8bb23
DK
240 else
241 rc = -EINVAL;
8606404f
DK
242 if (rc) {
243 cause = "invalid-hash";
244 status = INTEGRITY_FAIL;
8606404f
DK
245 break;
246 }
247 status = INTEGRITY_PASS;
248 break;
249 case EVM_IMA_XATTR_DIGSIG:
250 iint->flags |= IMA_DIGSIG;
251 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
b1aaab22 252 (const char *)xattr_value, rc,
a35c3fb6
DK
253 iint->ima_hash->digest,
254 iint->ima_hash->length);
8606404f
DK
255 if (rc == -EOPNOTSUPP) {
256 status = INTEGRITY_UNKNOWN;
257 } else if (rc) {
258 cause = "invalid-signature";
259 status = INTEGRITY_FAIL;
260 } else {
261 status = INTEGRITY_PASS;
262 }
263 break;
264 default:
265 status = INTEGRITY_UNKNOWN;
266 cause = "unknown-ima-data";
267 break;
2fe5d6de 268 }
8606404f 269
2fe5d6de
MZ
270out:
271 if (status != INTEGRITY_PASS) {
8606404f
DK
272 if ((ima_appraise & IMA_APPRAISE_FIX) &&
273 (!xattr_value ||
274 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
def3e8b9
DK
275 if (!ima_fix_xattr(dentry, iint))
276 status = INTEGRITY_PASS;
2fe5d6de
MZ
277 }
278 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
279 op, cause, rc, 0);
8606404f 280 } else {
d79d72e0 281 ima_cache_flags(iint, func);
2fe5d6de 282 }
d79d72e0 283 ima_set_cache_status(iint, func, status);
2fe5d6de
MZ
284 return status;
285}
286
287/*
288 * ima_update_xattr - update 'security.ima' hash value
289 */
290void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
291{
292 struct dentry *dentry = file->f_dentry;
293 int rc = 0;
294
8606404f
DK
295 /* do not collect and update hash for digital signatures */
296 if (iint->flags & IMA_DIGSIG)
297 return;
298
d3634d0f 299 rc = ima_collect_measurement(iint, file, NULL, NULL);
2fe5d6de
MZ
300 if (rc < 0)
301 return;
8606404f 302
2fe5d6de
MZ
303 ima_fix_xattr(dentry, iint);
304}
305
306/**
307 * ima_inode_post_setattr - reflect file metadata changes
308 * @dentry: pointer to the affected dentry
309 *
310 * Changes to a dentry's metadata might result in needing to appraise.
311 *
312 * This function is called from notify_change(), which expects the caller
313 * to lock the inode's i_mutex.
314 */
315void ima_inode_post_setattr(struct dentry *dentry)
316{
317 struct inode *inode = dentry->d_inode;
318 struct integrity_iint_cache *iint;
319 int must_appraise, rc;
320
321 if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode)
322 || !inode->i_op->removexattr)
323 return;
324
325 must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
326 iint = integrity_iint_find(inode);
327 if (iint) {
d79d72e0
MZ
328 iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED |
329 IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK |
330 IMA_ACTION_FLAGS);
2fe5d6de
MZ
331 if (must_appraise)
332 iint->flags |= IMA_APPRAISE;
2fe5d6de
MZ
333 }
334 if (!must_appraise)
335 rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA);
336 return;
337}
42c63330
MZ
338
339/*
340 * ima_protect_xattr - protect 'security.ima'
341 *
342 * Ensure that not just anyone can modify or remove 'security.ima'.
343 */
344static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
345 const void *xattr_value, size_t xattr_value_len)
346{
347 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
348 if (!capable(CAP_SYS_ADMIN))
349 return -EPERM;
350 return 1;
351 }
352 return 0;
353}
354
060bdebf 355static void ima_reset_appraise_flags(struct inode *inode, int digsig)
42c63330
MZ
356{
357 struct integrity_iint_cache *iint;
358
359 if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode))
360 return;
361
362 iint = integrity_iint_find(inode);
363 if (!iint)
364 return;
365
45e2472e 366 iint->flags &= ~IMA_DONE_MASK;
060bdebf
MZ
367 if (digsig)
368 iint->flags |= IMA_DIGSIG;
42c63330
MZ
369 return;
370}
371
372int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
373 const void *xattr_value, size_t xattr_value_len)
374{
060bdebf 375 const struct evm_ima_xattr_data *xvalue = xattr_value;
42c63330
MZ
376 int result;
377
378 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
379 xattr_value_len);
380 if (result == 1) {
060bdebf
MZ
381 ima_reset_appraise_flags(dentry->d_inode,
382 (xvalue->type == EVM_IMA_XATTR_DIGSIG) ? 1 : 0);
42c63330
MZ
383 result = 0;
384 }
385 return result;
386}
387
388int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
389{
390 int result;
391
392 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
393 if (result == 1) {
060bdebf 394 ima_reset_appraise_flags(dentry->d_inode, 0);
42c63330
MZ
395 result = 0;
396 }
397 return result;
398}
This page took 0.104901 seconds and 5 git commands to generate.