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,
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>
32 #ifdef CONFIG_IMA_APPRAISE
33 int ima_appraise = IMA_APPRAISE_ENFORCE;
38 char *ima_hash = "sha1";
39 static int __init hash_setup(char *str)
41 if (strncmp(str, "md5", 3) == 0)
45 __setup("ima_hash=", hash_setup);
48 * ima_rdwr_violation_check
50 * Only invalidate the PCR for measured files:
51 * - Opening a file for write when already open for read,
52 * results in a time of measure, time of use (ToMToU) error.
53 * - Opening a file for read when already open for write,
54 * could result in a file measurement error.
57 static void ima_rdwr_violation_check(struct file *file)
59 struct dentry *dentry = file->f_path.dentry;
60 struct inode *inode = dentry->d_inode;
61 fmode_t mode = file->f_mode;
63 bool send_tomtou = false, send_writers = false;
64 unsigned char *pathname = NULL, *pathbuf = NULL;
66 if (!S_ISREG(inode->i_mode) || !ima_initialized)
69 mutex_lock(&inode->i_mutex); /* file metadata: permissions, xattr */
71 if (mode & FMODE_WRITE) {
72 if (atomic_read(&inode->i_readcount) && IS_IMA(inode))
77 must_measure = ima_must_measure(inode, MAY_READ, FILE_CHECK);
81 if (atomic_read(&inode->i_writecount) > 0)
84 mutex_unlock(&inode->i_mutex);
86 if (!send_tomtou && !send_writers)
89 /* We will allow 11 spaces for ' (deleted)' to be appended */
90 pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL);
92 pathname = d_path(&file->f_path, pathbuf, PATH_MAX + 11);
95 else if (strlen(pathname) > IMA_EVENT_NAME_LEN_MAX)
99 ima_add_violation(inode,
100 !pathname ? dentry->d_name.name : pathname,
101 "invalid_pcr", "ToMToU");
103 ima_add_violation(inode,
104 !pathname ? dentry->d_name.name : pathname,
105 "invalid_pcr", "open_writers");
109 static void ima_check_last_writer(struct integrity_iint_cache *iint,
110 struct inode *inode, struct file *file)
112 fmode_t mode = file->f_mode;
114 if (!(mode & FMODE_WRITE))
117 mutex_lock(&inode->i_mutex);
118 if (atomic_read(&inode->i_writecount) == 1 &&
119 iint->version != inode->i_version) {
120 iint->flags &= ~IMA_DONE_MASK;
121 if (iint->flags & IMA_APPRAISE)
122 ima_update_xattr(iint, file);
124 mutex_unlock(&inode->i_mutex);
128 * ima_file_free - called on __fput()
129 * @file: pointer to file structure being freed
131 * Flag files that changed, based on i_version
133 void ima_file_free(struct file *file)
135 struct inode *inode = file->f_dentry->d_inode;
136 struct integrity_iint_cache *iint;
138 if (!iint_initialized || !S_ISREG(inode->i_mode))
141 iint = integrity_iint_find(inode);
145 ima_check_last_writer(iint, inode, file);
148 static int process_measurement(struct file *file, const unsigned char *filename,
149 int mask, int function)
151 struct inode *inode = file->f_dentry->d_inode;
152 struct integrity_iint_cache *iint;
153 unsigned char *pathname = NULL, *pathbuf = NULL;
154 int rc = -ENOMEM, action, must_appraise;
156 if (!ima_initialized || !S_ISREG(inode->i_mode))
159 /* Determine if in appraise/measurement policy,
160 * returns IMA_MEASURE, IMA_APPRAISE bitmask. */
161 action = ima_get_action(inode, mask, function);
165 must_appraise = action & IMA_APPRAISE;
167 mutex_lock(&inode->i_mutex);
169 iint = integrity_inode_get(inode);
173 /* Determine if already appraised/measured based on bitmask
174 * (IMA_MEASURE, IMA_MEASURED, IMA_APPRAISE, IMA_APPRAISED) */
175 iint->flags |= action;
176 action &= ~((iint->flags & IMA_DONE_MASK) >> 1);
178 /* Nothing to do, just return existing appraised status */
180 if (iint->flags & IMA_APPRAISED)
181 rc = iint->ima_status;
185 rc = ima_collect_measurement(iint, file);
189 if (function != BPRM_CHECK) {
190 /* We will allow 11 spaces for ' (deleted)' to be appended */
191 pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL);
194 d_path(&file->f_path, pathbuf, PATH_MAX + 11);
195 if (IS_ERR(pathname))
199 if (action & IMA_MEASURE)
200 ima_store_measurement(iint, file,
201 !pathname ? filename : pathname);
202 if (action & IMA_APPRAISE)
203 rc = ima_appraise_measurement(iint, file,
204 !pathname ? filename : pathname);
207 mutex_unlock(&inode->i_mutex);
208 return (rc && must_appraise) ? -EACCES : 0;
212 * ima_file_mmap - based on policy, collect/store measurement.
213 * @file: pointer to the file to be measured (May be NULL)
214 * @prot: contains the protection that will be applied by the kernel.
216 * Measure files being mmapped executable based on the ima_must_measure()
219 * Return 0 on success, an error code on failure.
220 * (Based on the results of appraise_measurement().)
222 int ima_file_mmap(struct file *file, unsigned long prot)
228 if (prot & PROT_EXEC)
229 rc = process_measurement(file, file->f_dentry->d_name.name,
230 MAY_EXEC, FILE_MMAP);
231 return (ima_appraise & IMA_APPRAISE_ENFORCE) ? rc : 0;
235 * ima_bprm_check - based on policy, collect/store measurement.
236 * @bprm: contains the linux_binprm structure
238 * The OS protects against an executable file, already open for write,
239 * from being executed in deny_write_access() and an executable file,
240 * already open for execute, from being modified in get_write_access().
241 * So we can be certain that what we verify and measure here is actually
242 * what is being executed.
244 * Return 0 on success, an error code on failure.
245 * (Based on the results of appraise_measurement().)
247 int ima_bprm_check(struct linux_binprm *bprm)
251 rc = process_measurement(bprm->file,
252 (strcmp(bprm->filename, bprm->interp) == 0) ?
253 bprm->filename : bprm->interp,
254 MAY_EXEC, BPRM_CHECK);
255 return (ima_appraise & IMA_APPRAISE_ENFORCE) ? rc : 0;
259 * ima_path_check - based on policy, collect/store measurement.
260 * @file: pointer to the file to be measured
261 * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
263 * Measure files based on the ima_must_measure() policy decision.
265 * Always return 0 and audit dentry_open failures.
266 * (Return code will be based upon measurement appraisal.)
268 int ima_file_check(struct file *file, int mask)
272 ima_rdwr_violation_check(file);
273 rc = process_measurement(file, file->f_dentry->d_name.name,
274 mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
276 return (ima_appraise & IMA_APPRAISE_ENFORCE) ? rc : 0;
278 EXPORT_SYMBOL_GPL(ima_file_check);
280 static int __init init_ima(void)
290 late_initcall(init_ima); /* Start IMA after the TPM is available */
292 MODULE_DESCRIPTION("Integrity Measurement Architecture");
293 MODULE_LICENSE("GPL");