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