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