1 /*
2  * Copyright (C) 2005,2006,2007,2008 IBM Corporation
3  *
4  * Authors:
5  * Reiner Sailer <sailer@watson.ibm.com>
6  * Serge Hallyn <serue@us.ibm.com>
7  * Kylene Hall <kylene@us.ibm.com>
8  * Mimi Zohar <zohar@us.ibm.com>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation, version 2 of the
13  * License.
14  *
15  * File: ima_main.c
16  *	implements the IMA hooks: ima_bprm_check, ima_file_mmap,
17  *	and ima_file_check.
18  */
19 #include <linux/module.h>
20 #include <linux/file.h>
21 #include <linux/binfmts.h>
22 #include <linux/mount.h>
23 #include <linux/mman.h>
24 #include <linux/slab.h>
25 #include <linux/xattr.h>
26 #include <linux/ima.h>
27 
28 #include "ima.h"
29 
30 int ima_initialized;
31 
32 #ifdef CONFIG_IMA_APPRAISE
33 int ima_appraise = IMA_APPRAISE_ENFORCE;
34 #else
35 int ima_appraise;
36 #endif
37 
38 int ima_hash_algo = HASH_ALGO_SHA1;
39 static int hash_setup_done;
40 
41 static int __init hash_setup(char *str)
42 {
43 	struct ima_template_desc *template_desc = ima_template_desc_current();
44 	int i;
45 
46 	if (hash_setup_done)
47 		return 1;
48 
49 	if (strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) == 0) {
50 		if (strncmp(str, "sha1", 4) == 0)
51 			ima_hash_algo = HASH_ALGO_SHA1;
52 		else if (strncmp(str, "md5", 3) == 0)
53 			ima_hash_algo = HASH_ALGO_MD5;
54 		goto out;
55 	}
56 
57 	for (i = 0; i < HASH_ALGO__LAST; i++) {
58 		if (strcmp(str, hash_algo_name[i]) == 0) {
59 			ima_hash_algo = i;
60 			break;
61 		}
62 	}
63 out:
64 	hash_setup_done = 1;
65 	return 1;
66 }
67 __setup("ima_hash=", hash_setup);
68 
69 /*
70  * ima_rdwr_violation_check
71  *
72  * Only invalidate the PCR for measured files:
73  *	- Opening a file for write when already open for read,
74  *	  results in a time of measure, time of use (ToMToU) error.
75  *	- Opening a file for read when already open for write,
76  *	  could result in a file measurement error.
77  *
78  */
79 static void ima_rdwr_violation_check(struct file *file,
80 				     struct integrity_iint_cache *iint,
81 				     int must_measure,
82 				     char **pathbuf,
83 				     const char **pathname)
84 {
85 	struct inode *inode = file_inode(file);
86 	fmode_t mode = file->f_mode;
87 	bool send_tomtou = false, send_writers = false;
88 
89 	if (mode & FMODE_WRITE) {
90 		if (atomic_read(&inode->i_readcount) && IS_IMA(inode)) {
91 			if (!iint)
92 				iint = integrity_iint_find(inode);
93 			/* IMA_MEASURE is set from reader side */
94 			if (iint && (iint->flags & IMA_MEASURE))
95 				send_tomtou = true;
96 		}
97 	} else {
98 		if ((atomic_read(&inode->i_writecount) > 0) && must_measure)
99 			send_writers = true;
100 	}
101 
102 	if (!send_tomtou && !send_writers)
103 		return;
104 
105 	*pathname = ima_d_path(&file->f_path, pathbuf);
106 
107 	if (send_tomtou)
108 		ima_add_violation(file, *pathname, iint,
109 				  "invalid_pcr", "ToMToU");
110 	if (send_writers)
111 		ima_add_violation(file, *pathname, iint,
112 				  "invalid_pcr", "open_writers");
113 }
114 
115 static void ima_check_last_writer(struct integrity_iint_cache *iint,
116 				  struct inode *inode, struct file *file)
117 {
118 	fmode_t mode = file->f_mode;
119 
120 	if (!(mode & FMODE_WRITE))
121 		return;
122 
123 	inode_lock(inode);
124 	if (atomic_read(&inode->i_writecount) == 1) {
125 		if ((iint->version != inode->i_version) ||
126 		    (iint->flags & IMA_NEW_FILE)) {
127 			iint->flags &= ~(IMA_DONE_MASK | IMA_NEW_FILE);
128 			iint->measured_pcrs = 0;
129 			if (iint->flags & IMA_APPRAISE)
130 				ima_update_xattr(iint, file);
131 		}
132 	}
133 	inode_unlock(inode);
134 }
135 
136 /**
137  * ima_file_free - called on __fput()
138  * @file: pointer to file structure being freed
139  *
140  * Flag files that changed, based on i_version
141  */
142 void ima_file_free(struct file *file)
143 {
144 	struct inode *inode = file_inode(file);
145 	struct integrity_iint_cache *iint;
146 
147 	if (!ima_policy_flag || !S_ISREG(inode->i_mode))
148 		return;
149 
150 	iint = integrity_iint_find(inode);
151 	if (!iint)
152 		return;
153 
154 	ima_check_last_writer(iint, inode, file);
155 }
156 
157 static int process_measurement(struct file *file, char *buf, loff_t size,
158 			       int mask, enum ima_hooks func, int opened)
159 {
160 	struct inode *inode = file_inode(file);
161 	struct integrity_iint_cache *iint = NULL;
162 	struct ima_template_desc *template_desc;
163 	char *pathbuf = NULL;
164 	const char *pathname = NULL;
165 	int rc = -ENOMEM, action, must_appraise;
166 	int pcr = CONFIG_IMA_MEASURE_PCR_IDX;
167 	struct evm_ima_xattr_data *xattr_value = NULL;
168 	int xattr_len = 0;
169 	bool violation_check;
170 	enum hash_algo hash_algo;
171 
172 	if (!ima_policy_flag || !S_ISREG(inode->i_mode))
173 		return 0;
174 
175 	/* Return an IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT action
176 	 * bitmask based on the appraise/audit/measurement policy.
177 	 * Included is the appraise submask.
178 	 */
179 	action = ima_get_action(inode, mask, func, &pcr);
180 	violation_check = ((func == FILE_CHECK || func == MMAP_CHECK) &&
181 			   (ima_policy_flag & IMA_MEASURE));
182 	if (!action && !violation_check)
183 		return 0;
184 
185 	must_appraise = action & IMA_APPRAISE;
186 
187 	/*  Is the appraise rule hook specific?  */
188 	if (action & IMA_FILE_APPRAISE)
189 		func = FILE_CHECK;
190 
191 	inode_lock(inode);
192 
193 	if (action) {
194 		iint = integrity_inode_get(inode);
195 		if (!iint)
196 			goto out;
197 	}
198 
199 	if (violation_check) {
200 		ima_rdwr_violation_check(file, iint, action & IMA_MEASURE,
201 					 &pathbuf, &pathname);
202 		if (!action) {
203 			rc = 0;
204 			goto out_free;
205 		}
206 	}
207 
208 	/* Determine if already appraised/measured based on bitmask
209 	 * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED,
210 	 *  IMA_AUDIT, IMA_AUDITED)
211 	 */
212 	iint->flags |= action;
213 	action &= IMA_DO_MASK;
214 	action &= ~((iint->flags & (IMA_DONE_MASK ^ IMA_MEASURED)) >> 1);
215 
216 	/* If target pcr is already measured, unset IMA_MEASURE action */
217 	if ((action & IMA_MEASURE) && (iint->measured_pcrs & (0x1 << pcr)))
218 		action ^= IMA_MEASURE;
219 
220 	/* Nothing to do, just return existing appraised status */
221 	if (!action) {
222 		if (must_appraise)
223 			rc = ima_get_cache_status(iint, func);
224 		goto out_digsig;
225 	}
226 
227 	template_desc = ima_template_desc_current();
228 	if ((action & IMA_APPRAISE_SUBMASK) ||
229 		    strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) != 0)
230 		/* read 'security.ima' */
231 		xattr_len = ima_read_xattr(file_dentry(file), &xattr_value);
232 
233 	hash_algo = ima_get_hash_algo(xattr_value, xattr_len);
234 
235 	rc = ima_collect_measurement(iint, file, buf, size, hash_algo);
236 	if (rc != 0) {
237 		if (file->f_flags & O_DIRECT)
238 			rc = (iint->flags & IMA_PERMIT_DIRECTIO) ? 0 : -EACCES;
239 		goto out_digsig;
240 	}
241 
242 	if (!pathname)	/* ima_rdwr_violation possibly pre-fetched */
243 		pathname = ima_d_path(&file->f_path, &pathbuf);
244 
245 	if (action & IMA_MEASURE)
246 		ima_store_measurement(iint, file, pathname,
247 				      xattr_value, xattr_len, pcr);
248 	if (action & IMA_APPRAISE_SUBMASK)
249 		rc = ima_appraise_measurement(func, iint, file, pathname,
250 					      xattr_value, xattr_len, opened);
251 	if (action & IMA_AUDIT)
252 		ima_audit_measurement(iint, pathname);
253 
254 out_digsig:
255 	if ((mask & MAY_WRITE) && (iint->flags & IMA_DIGSIG) &&
256 	     !(iint->flags & IMA_NEW_FILE))
257 		rc = -EACCES;
258 	kfree(xattr_value);
259 out_free:
260 	if (pathbuf)
261 		__putname(pathbuf);
262 out:
263 	inode_unlock(inode);
264 	if ((rc && must_appraise) && (ima_appraise & IMA_APPRAISE_ENFORCE))
265 		return -EACCES;
266 	return 0;
267 }
268 
269 /**
270  * ima_file_mmap - based on policy, collect/store measurement.
271  * @file: pointer to the file to be measured (May be NULL)
272  * @prot: contains the protection that will be applied by the kernel.
273  *
274  * Measure files being mmapped executable based on the ima_must_measure()
275  * policy decision.
276  *
277  * On success return 0.  On integrity appraisal error, assuming the file
278  * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
279  */
280 int ima_file_mmap(struct file *file, unsigned long prot)
281 {
282 	if (file && (prot & PROT_EXEC))
283 		return process_measurement(file, NULL, 0, MAY_EXEC,
284 					   MMAP_CHECK, 0);
285 	return 0;
286 }
287 
288 /**
289  * ima_bprm_check - based on policy, collect/store measurement.
290  * @bprm: contains the linux_binprm structure
291  *
292  * The OS protects against an executable file, already open for write,
293  * from being executed in deny_write_access() and an executable file,
294  * already open for execute, from being modified in get_write_access().
295  * So we can be certain that what we verify and measure here is actually
296  * what is being executed.
297  *
298  * On success return 0.  On integrity appraisal error, assuming the file
299  * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
300  */
301 int ima_bprm_check(struct linux_binprm *bprm)
302 {
303 	return process_measurement(bprm->file, NULL, 0, MAY_EXEC,
304 				   BPRM_CHECK, 0);
305 }
306 
307 /**
308  * ima_path_check - based on policy, collect/store measurement.
309  * @file: pointer to the file to be measured
310  * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
311  *
312  * Measure files based on the ima_must_measure() policy decision.
313  *
314  * On success return 0.  On integrity appraisal error, assuming the file
315  * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
316  */
317 int ima_file_check(struct file *file, int mask, int opened)
318 {
319 	return process_measurement(file, NULL, 0,
320 				   mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
321 				   FILE_CHECK, opened);
322 }
323 EXPORT_SYMBOL_GPL(ima_file_check);
324 
325 /**
326  * ima_post_path_mknod - mark as a new inode
327  * @dentry: newly created dentry
328  *
329  * Mark files created via the mknodat syscall as new, so that the
330  * file data can be written later.
331  */
332 void ima_post_path_mknod(struct dentry *dentry)
333 {
334 	struct integrity_iint_cache *iint;
335 	struct inode *inode = dentry->d_inode;
336 	int must_appraise;
337 
338 	must_appraise = ima_must_appraise(inode, MAY_ACCESS, FILE_CHECK);
339 	if (!must_appraise)
340 		return;
341 
342 	iint = integrity_inode_get(inode);
343 	if (iint)
344 		iint->flags |= IMA_NEW_FILE;
345 }
346 
347 /**
348  * ima_read_file - pre-measure/appraise hook decision based on policy
349  * @file: pointer to the file to be measured/appraised/audit
350  * @read_id: caller identifier
351  *
352  * Permit reading a file based on policy. The policy rules are written
353  * in terms of the policy identifier.  Appraising the integrity of
354  * a file requires a file descriptor.
355  *
356  * For permission return 0, otherwise return -EACCES.
357  */
358 int ima_read_file(struct file *file, enum kernel_read_file_id read_id)
359 {
360 	if (!file && read_id == READING_MODULE) {
361 #ifndef CONFIG_MODULE_SIG_FORCE
362 		if ((ima_appraise & IMA_APPRAISE_MODULES) &&
363 		    (ima_appraise & IMA_APPRAISE_ENFORCE))
364 			return -EACCES;	/* INTEGRITY_UNKNOWN */
365 #endif
366 		return 0;	/* We rely on module signature checking */
367 	}
368 	return 0;
369 }
370 
371 static int read_idmap[READING_MAX_ID] = {
372 	[READING_FIRMWARE] = FIRMWARE_CHECK,
373 	[READING_MODULE] = MODULE_CHECK,
374 	[READING_KEXEC_IMAGE] = KEXEC_KERNEL_CHECK,
375 	[READING_KEXEC_INITRAMFS] = KEXEC_INITRAMFS_CHECK,
376 	[READING_POLICY] = POLICY_CHECK
377 };
378 
379 /**
380  * ima_post_read_file - in memory collect/appraise/audit measurement
381  * @file: pointer to the file to be measured/appraised/audit
382  * @buf: pointer to in memory file contents
383  * @size: size of in memory file contents
384  * @read_id: caller identifier
385  *
386  * Measure/appraise/audit in memory file based on policy.  Policy rules
387  * are written in terms of a policy identifier.
388  *
389  * On success return 0.  On integrity appraisal error, assuming the file
390  * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
391  */
392 int ima_post_read_file(struct file *file, void *buf, loff_t size,
393 		       enum kernel_read_file_id read_id)
394 {
395 	enum ima_hooks func;
396 
397 	if (!file && read_id == READING_FIRMWARE) {
398 		if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
399 		    (ima_appraise & IMA_APPRAISE_ENFORCE))
400 			return -EACCES;	/* INTEGRITY_UNKNOWN */
401 		return 0;
402 	}
403 
404 	if (!file && read_id == READING_MODULE) /* MODULE_SIG_FORCE enabled */
405 		return 0;
406 
407 	if (!file || !buf || size == 0) { /* should never happen */
408 		if (ima_appraise & IMA_APPRAISE_ENFORCE)
409 			return -EACCES;
410 		return 0;
411 	}
412 
413 	func = read_idmap[read_id] ?: FILE_CHECK;
414 	return process_measurement(file, buf, size, MAY_READ, func, 0);
415 }
416 
417 static int __init init_ima(void)
418 {
419 	int error;
420 
421 	hash_setup(CONFIG_IMA_DEFAULT_HASH);
422 	error = ima_init();
423 	if (!error) {
424 		ima_initialized = 1;
425 		ima_update_policy_flag();
426 	}
427 	return error;
428 }
429 
430 late_initcall(init_ima);	/* Start IMA after the TPM is available */
431 
432 MODULE_DESCRIPTION("Integrity Measurement Architecture");
433 MODULE_LICENSE("GPL");
434