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>
29 char *ima_hash = "sha1";
30 static int __init hash_setup(char *str)
32 if (strncmp(str, "md5", 3) == 0)
36 __setup("ima_hash=", hash_setup);
39 * ima_file_free - called on __fput()
40 * @file: pointer to file structure being freed
42 * Flag files that changed, based on i_version;
43 * and decrement the iint readcount/writecount.
45 void ima_file_free(struct file *file)
47 struct inode *inode = file->f_dentry->d_inode;
48 struct ima_iint_cache *iint;
50 if (!ima_initialized || !S_ISREG(inode->i_mode))
52 iint = ima_iint_find_get(inode);
56 mutex_lock(&iint->mutex);
57 if (iint->opencount <= 0) {
59 "%s: %s open/free imbalance (r:%ld w:%ld o:%ld f:%ld)\n",
60 __FUNCTION__, file->f_dentry->d_name.name,
61 iint->readcount, iint->writecount,
62 iint->opencount, atomic_long_read(&file->f_count));
63 if (!(iint->flags & IMA_IINT_DUMP_STACK)) {
65 iint->flags |= IMA_IINT_DUMP_STACK;
70 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
73 if (file->f_mode & FMODE_WRITE) {
75 if (iint->writecount == 0) {
76 if (iint->version != inode->i_version)
77 iint->flags &= ~IMA_MEASURED;
80 mutex_unlock(&iint->mutex);
81 kref_put(&iint->refcount, iint_free);
84 /* ima_read_write_check - reflect possible reading/writing errors in the PCR.
86 * When opening a file for read, if the file is already open for write,
87 * the file could change, resulting in a file measurement error.
89 * Opening a file for write, if the file is already open for read, results
90 * in a time of measure, time of use (ToMToU) error.
92 * In either case invalidate the PCR.
94 enum iint_pcr_error { TOMTOU, OPEN_WRITERS };
95 static void ima_read_write_check(enum iint_pcr_error error,
96 struct ima_iint_cache *iint,
98 const unsigned char *filename)
102 if (iint->readcount > 0)
103 ima_add_violation(inode, filename, "invalid_pcr",
107 if (iint->writecount > 0)
108 ima_add_violation(inode, filename, "invalid_pcr",
114 static int get_path_measurement(struct ima_iint_cache *iint, struct file *file,
115 const unsigned char *filename)
122 rc = ima_collect_measurement(iint, file);
124 ima_store_measurement(iint, file, filename);
128 static void ima_update_counts(struct ima_iint_cache *iint, int mask)
131 if ((mask & MAY_WRITE) || (mask == 0))
133 else if (mask & (MAY_READ | MAY_EXEC))
138 * ima_path_check - based on policy, collect/store measurement.
139 * @path: contains a pointer to the path to be measured
140 * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
142 * Measure the file being open for readonly, based on the
143 * ima_must_measure() policy decision.
145 * Keep read/write counters for all files, but only
146 * invalidate the PCR for measured files:
147 * - Opening a file for write when already open for read,
148 * results in a time of measure, time of use (ToMToU) error.
149 * - Opening a file for read when already open for write,
150 * could result in a file measurement error.
152 * Always return 0 and audit dentry_open failures.
153 * (Return code will be based upon measurement appraisal.)
155 int ima_path_check(struct path *path, int mask, int update_counts)
157 struct inode *inode = path->dentry->d_inode;
158 struct ima_iint_cache *iint;
159 struct file *file = NULL;
162 if (!ima_initialized || !S_ISREG(inode->i_mode))
164 iint = ima_iint_find_insert_get(inode);
168 mutex_lock(&iint->mutex);
170 ima_update_counts(iint, mask);
172 rc = ima_must_measure(iint, inode, MAY_READ, PATH_CHECK);
176 if ((mask & MAY_WRITE) || (mask == 0))
177 ima_read_write_check(TOMTOU, iint, inode,
178 path->dentry->d_name.name);
180 if ((mask & (MAY_WRITE | MAY_READ | MAY_EXEC)) != MAY_READ)
183 ima_read_write_check(OPEN_WRITERS, iint, inode,
184 path->dentry->d_name.name);
185 if (!(iint->flags & IMA_MEASURED)) {
186 struct dentry *dentry = dget(path->dentry);
187 struct vfsmount *mnt = mntget(path->mnt);
189 file = dentry_open(dentry, mnt, O_RDONLY | O_LARGEFILE,
194 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode,
197 "dentry_open failed",
202 rc = get_path_measurement(iint, file, dentry->d_name.name);
205 mutex_unlock(&iint->mutex);
208 kref_put(&iint->refcount, iint_free);
211 EXPORT_SYMBOL_GPL(ima_path_check);
213 static int process_measurement(struct file *file, const unsigned char *filename,
214 int mask, int function)
216 struct inode *inode = file->f_dentry->d_inode;
217 struct ima_iint_cache *iint;
220 if (!ima_initialized || !S_ISREG(inode->i_mode))
222 iint = ima_iint_find_insert_get(inode);
226 mutex_lock(&iint->mutex);
227 rc = ima_must_measure(iint, inode, mask, function);
231 rc = ima_collect_measurement(iint, file);
233 ima_store_measurement(iint, file, filename);
235 mutex_unlock(&iint->mutex);
236 kref_put(&iint->refcount, iint_free);
241 * ima_counts_put - decrement file counts
243 * File counts are incremented in ima_path_check. On file open
244 * error, such as ETXTBSY, decrement the counts to prevent
245 * unnecessary imbalance messages.
247 void ima_counts_put(struct path *path, int mask)
249 struct inode *inode = path->dentry->d_inode;
250 struct ima_iint_cache *iint;
252 if (!ima_initialized || !S_ISREG(inode->i_mode))
254 iint = ima_iint_find_insert_get(inode);
258 mutex_lock(&iint->mutex);
260 if ((mask & MAY_WRITE) || (mask == 0))
262 else if (mask & (MAY_READ | MAY_EXEC))
264 mutex_unlock(&iint->mutex);
268 * ima_counts_get - increment file counts
270 * - for IPC shm and shmat file.
271 * - for nfsd exported files.
273 * Increment the counts for these files to prevent unnecessary
274 * imbalance messages.
276 void ima_counts_get(struct file *file)
278 struct inode *inode = file->f_dentry->d_inode;
279 struct ima_iint_cache *iint;
281 if (!ima_initialized || !S_ISREG(inode->i_mode))
283 iint = ima_iint_find_insert_get(inode);
286 mutex_lock(&iint->mutex);
288 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
291 if (file->f_mode & FMODE_WRITE)
293 mutex_unlock(&iint->mutex);
295 EXPORT_SYMBOL_GPL(ima_counts_get);
298 * ima_file_mmap - based on policy, collect/store measurement.
299 * @file: pointer to the file to be measured (May be NULL)
300 * @prot: contains the protection that will be applied by the kernel.
302 * Measure files being mmapped executable based on the ima_must_measure()
305 * Return 0 on success, an error code on failure.
306 * (Based on the results of appraise_measurement().)
308 int ima_file_mmap(struct file *file, unsigned long prot)
314 if (prot & PROT_EXEC)
315 rc = process_measurement(file, file->f_dentry->d_name.name,
316 MAY_EXEC, FILE_MMAP);
321 * ima_bprm_check - based on policy, collect/store measurement.
322 * @bprm: contains the linux_binprm structure
324 * The OS protects against an executable file, already open for write,
325 * from being executed in deny_write_access() and an executable file,
326 * already open for execute, from being modified in get_write_access().
327 * So we can be certain that what we verify and measure here is actually
328 * what is being executed.
330 * Return 0 on success, an error code on failure.
331 * (Based on the results of appraise_measurement().)
333 int ima_bprm_check(struct linux_binprm *bprm)
337 rc = process_measurement(bprm->file, bprm->filename,
338 MAY_EXEC, BPRM_CHECK);
342 static int __init init_ima(void)
346 ima_iintcache_init();
352 static void __exit cleanup_ima(void)
357 late_initcall(init_ima); /* Start IMA after the TPM is available */
359 MODULE_DESCRIPTION("Integrity Measurement Architecture");
360 MODULE_LICENSE("GPL");