2 * Copyright (C) 2005,2006,2007,2008 IBM Corporation
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>
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
16 * implements the IMA hooks: ima_bprm_check, ima_file_mmap,
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/module.h>
23 #include <linux/file.h>
24 #include <linux/binfmts.h>
25 #include <linux/mount.h>
26 #include <linux/mman.h>
27 #include <linux/slab.h>
28 #include <linux/xattr.h>
29 #include <linux/ima.h>
30 #include <crypto/hash_info.h>
36 #ifdef CONFIG_IMA_APPRAISE
37 int ima_appraise = IMA_APPRAISE_ENFORCE;
42 int ima_hash_algo = HASH_ALGO_SHA1;
43 static int hash_setup_done;
45 static int __init hash_setup(char *str)
47 struct ima_template_desc *template_desc = ima_template_desc_current();
53 if (strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) == 0) {
54 if (strncmp(str, "sha1", 4) == 0)
55 ima_hash_algo = HASH_ALGO_SHA1;
56 else if (strncmp(str, "md5", 3) == 0)
57 ima_hash_algo = HASH_ALGO_MD5;
63 for (i = 0; i < HASH_ALGO__LAST; i++) {
64 if (strcmp(str, hash_algo_name[i]) == 0) {
69 if (i == HASH_ALGO__LAST)
75 __setup("ima_hash=", hash_setup);
78 * ima_rdwr_violation_check
80 * Only invalidate the PCR for measured files:
81 * - Opening a file for write when already open for read,
82 * results in a time of measure, time of use (ToMToU) error.
83 * - Opening a file for read when already open for write,
84 * could result in a file measurement error.
87 static void ima_rdwr_violation_check(struct file *file,
88 struct integrity_iint_cache *iint,
91 const char **pathname)
93 struct inode *inode = file_inode(file);
94 fmode_t mode = file->f_mode;
95 bool send_tomtou = false, send_writers = false;
97 if (mode & FMODE_WRITE) {
98 if (atomic_read(&inode->i_readcount) && IS_IMA(inode)) {
100 iint = integrity_iint_find(inode);
101 /* IMA_MEASURE is set from reader side */
102 if (iint && (iint->flags & IMA_MEASURE))
106 if ((atomic_read(&inode->i_writecount) > 0) && must_measure)
110 if (!send_tomtou && !send_writers)
113 *pathname = ima_d_path(&file->f_path, pathbuf);
116 ima_add_violation(file, *pathname, iint,
117 "invalid_pcr", "ToMToU");
119 ima_add_violation(file, *pathname, iint,
120 "invalid_pcr", "open_writers");
123 static void ima_check_last_writer(struct integrity_iint_cache *iint,
124 struct inode *inode, struct file *file)
126 fmode_t mode = file->f_mode;
128 if (!(mode & FMODE_WRITE))
131 mutex_lock(&inode->i_mutex);
132 if (atomic_read(&inode->i_writecount) == 1) {
133 if ((iint->version != inode->i_version) ||
134 (iint->flags & IMA_NEW_FILE)) {
135 iint->flags &= ~(IMA_DONE_MASK | IMA_NEW_FILE);
136 if (iint->flags & IMA_APPRAISE)
137 ima_update_xattr(iint, file);
140 mutex_unlock(&inode->i_mutex);
144 * ima_file_free - called on __fput()
145 * @file: pointer to file structure being freed
147 * Flag files that changed, based on i_version
149 void ima_file_free(struct file *file)
151 struct inode *inode = file_inode(file);
152 struct integrity_iint_cache *iint;
154 if (!ima_policy_flag || !S_ISREG(inode->i_mode))
157 iint = integrity_iint_find(inode);
161 ima_check_last_writer(iint, inode, file);
164 static int process_measurement(struct file *file, int mask, int function,
167 struct inode *inode = file_inode(file);
168 struct integrity_iint_cache *iint = NULL;
169 struct ima_template_desc *template_desc;
170 char *pathbuf = NULL;
171 const char *pathname = NULL;
172 int rc = -ENOMEM, action, must_appraise;
173 struct evm_ima_xattr_data *xattr_value = NULL, **xattr_ptr = NULL;
175 bool violation_check;
177 if (!ima_policy_flag || !S_ISREG(inode->i_mode))
180 /* Return an IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT action
181 * bitmask based on the appraise/audit/measurement policy.
182 * Included is the appraise submask.
184 action = ima_get_action(inode, mask, function);
185 violation_check = ((function == FILE_CHECK || function == MMAP_CHECK) &&
186 (ima_policy_flag & IMA_MEASURE));
187 if (!action && !violation_check)
190 must_appraise = action & IMA_APPRAISE;
192 /* Is the appraise rule hook specific? */
193 if (action & IMA_FILE_APPRAISE)
194 function = FILE_CHECK;
196 mutex_lock(&inode->i_mutex);
199 iint = integrity_inode_get(inode);
204 if (violation_check) {
205 ima_rdwr_violation_check(file, iint, action & IMA_MEASURE,
206 &pathbuf, &pathname);
213 /* Determine if already appraised/measured based on bitmask
214 * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED,
215 * IMA_AUDIT, IMA_AUDITED)
217 iint->flags |= action;
218 action &= IMA_DO_MASK;
219 action &= ~((iint->flags & IMA_DONE_MASK) >> 1);
221 /* Nothing to do, just return existing appraised status */
224 rc = ima_get_cache_status(iint, function);
228 template_desc = ima_template_desc_current();
229 if ((action & IMA_APPRAISE_SUBMASK) ||
230 strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) != 0)
231 xattr_ptr = &xattr_value;
233 rc = ima_collect_measurement(iint, file, xattr_ptr, &xattr_len);
235 if (file->f_flags & O_DIRECT)
236 rc = (iint->flags & IMA_PERMIT_DIRECTIO) ? 0 : -EACCES;
240 if (!pathname) /* ima_rdwr_violation possibly pre-fetched */
241 pathname = ima_d_path(&file->f_path, &pathbuf);
243 if (action & IMA_MEASURE)
244 ima_store_measurement(iint, file, pathname,
245 xattr_value, xattr_len);
246 if (action & IMA_APPRAISE_SUBMASK)
247 rc = ima_appraise_measurement(function, iint, file, pathname,
248 xattr_value, xattr_len, opened);
249 if (action & IMA_AUDIT)
250 ima_audit_measurement(iint, pathname);
253 if ((mask & MAY_WRITE) && (iint->flags & IMA_DIGSIG))
260 mutex_unlock(&inode->i_mutex);
261 if ((rc && must_appraise) && (ima_appraise & IMA_APPRAISE_ENFORCE))
267 * ima_file_mmap - based on policy, collect/store measurement.
268 * @file: pointer to the file to be measured (May be NULL)
269 * @prot: contains the protection that will be applied by the kernel.
271 * Measure files being mmapped executable based on the ima_must_measure()
274 * On success return 0. On integrity appraisal error, assuming the file
275 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
277 int ima_file_mmap(struct file *file, unsigned long prot)
279 if (file && (prot & PROT_EXEC))
280 return process_measurement(file, MAY_EXEC, MMAP_CHECK, 0);
285 * ima_bprm_check - based on policy, collect/store measurement.
286 * @bprm: contains the linux_binprm structure
288 * The OS protects against an executable file, already open for write,
289 * from being executed in deny_write_access() and an executable file,
290 * already open for execute, from being modified in get_write_access().
291 * So we can be certain that what we verify and measure here is actually
292 * what is being executed.
294 * On success return 0. On integrity appraisal error, assuming the file
295 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
297 int ima_bprm_check(struct linux_binprm *bprm)
299 return process_measurement(bprm->file, MAY_EXEC, BPRM_CHECK, 0);
303 * ima_path_check - based on policy, collect/store measurement.
304 * @file: pointer to the file to be measured
305 * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
307 * Measure files based on the ima_must_measure() policy decision.
309 * On success return 0. On integrity appraisal error, assuming the file
310 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
312 int ima_file_check(struct file *file, int mask, int opened)
314 return process_measurement(file,
315 mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
318 EXPORT_SYMBOL_GPL(ima_file_check);
321 * ima_module_check - based on policy, collect/store/appraise measurement.
322 * @file: pointer to the file to be measured/appraised
324 * Measure/appraise kernel modules based on policy.
326 * On success return 0. On integrity appraisal error, assuming the file
327 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
329 int ima_module_check(struct file *file)
332 #ifndef CONFIG_MODULE_SIG_FORCE
333 if ((ima_appraise & IMA_APPRAISE_MODULES) &&
334 (ima_appraise & IMA_APPRAISE_ENFORCE))
335 return -EACCES; /* INTEGRITY_UNKNOWN */
337 return 0; /* We rely on module signature checking */
339 return process_measurement(file, MAY_EXEC, MODULE_CHECK, 0);
342 int ima_fw_from_file(struct file *file, char *buf, size_t size)
345 if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
346 (ima_appraise & IMA_APPRAISE_ENFORCE))
347 return -EACCES; /* INTEGRITY_UNKNOWN */
350 return process_measurement(file, MAY_EXEC, FIRMWARE_CHECK, 0);
353 static int __init init_ima(void)
357 hash_setup(CONFIG_IMA_DEFAULT_HASH);
360 if (error && strcmp(hash_algo_name[ima_hash_algo],
361 CONFIG_IMA_DEFAULT_HASH) != 0) {
362 pr_info("Allocating %s failed, going to use default hash algorithm %s\n",
363 hash_algo_name[ima_hash_algo], CONFIG_IMA_DEFAULT_HASH);
365 hash_setup(CONFIG_IMA_DEFAULT_HASH);
371 ima_update_policy_flag();
376 late_initcall(init_ima); /* Start IMA after the TPM is available */
378 MODULE_DESCRIPTION("Integrity Measurement Architecture");
379 MODULE_LICENSE("GPL");