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 const char *op
= "hash_setup";
33 const char *hash
= "sha1";
37 if (strncmp(str
, "md5", 3) == 0) {
40 } else if (strncmp(str
, "sha1", 4) != 0) {
41 hash
= "invalid_hash_type";
44 integrity_audit_msg(AUDIT_INTEGRITY_HASH
, NULL
, NULL
, op
, hash
,
48 __setup("ima_hash=", hash_setup
);
51 * ima_file_free - called on __fput()
52 * @file: pointer to file structure being freed
54 * Flag files that changed, based on i_version;
55 * and decrement the iint readcount/writecount.
57 void ima_file_free(struct file
*file
)
59 struct inode
*inode
= file
->f_dentry
->d_inode
;
60 struct ima_iint_cache
*iint
;
62 if (!ima_initialized
|| !S_ISREG(inode
->i_mode
))
64 iint
= ima_iint_find_get(inode
);
68 mutex_lock(&iint
->mutex
);
69 if ((file
->f_mode
& (FMODE_READ
| FMODE_WRITE
)) == FMODE_READ
)
72 if (file
->f_mode
& FMODE_WRITE
) {
74 if (iint
->writecount
== 0) {
75 if (iint
->version
!= inode
->i_version
)
76 iint
->flags
&= ~IMA_MEASURED
;
79 mutex_unlock(&iint
->mutex
);
80 kref_put(&iint
->refcount
, iint_free
);
83 /* ima_read_write_check - reflect possible reading/writing errors in the PCR.
85 * When opening a file for read, if the file is already open for write,
86 * the file could change, resulting in a file measurement error.
88 * Opening a file for write, if the file is already open for read, results
89 * in a time of measure, time of use (ToMToU) error.
91 * In either case invalidate the PCR.
93 enum iint_pcr_error
{ TOMTOU
, OPEN_WRITERS
};
94 static void ima_read_write_check(enum iint_pcr_error error
,
95 struct ima_iint_cache
*iint
,
97 const unsigned char *filename
)
101 if (iint
->readcount
> 0)
102 ima_add_violation(inode
, filename
, "invalid_pcr",
106 if (iint
->writecount
> 0)
107 ima_add_violation(inode
, filename
, "invalid_pcr",
113 static int get_path_measurement(struct ima_iint_cache
*iint
, struct file
*file
,
114 const unsigned char *filename
)
119 pr_info("%s dentry_open failed\n", filename
);
124 rc
= ima_collect_measurement(iint
, file
);
126 ima_store_measurement(iint
, file
, filename
);
131 * ima_path_check - based on policy, collect/store measurement.
132 * @path: contains a pointer to the path to be measured
133 * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
135 * Measure the file being open for readonly, based on the
136 * ima_must_measure() policy decision.
138 * Keep read/write counters for all files, but only
139 * invalidate the PCR for measured files:
140 * - Opening a file for write when already open for read,
141 * results in a time of measure, time of use (ToMToU) error.
142 * - Opening a file for read when already open for write,
143 * could result in a file measurement error.
145 * Return 0 on success, an error code on failure.
146 * (Based on the results of appraise_measurement().)
148 int ima_path_check(struct path
*path
, int mask
)
150 struct inode
*inode
= path
->dentry
->d_inode
;
151 struct ima_iint_cache
*iint
;
152 struct file
*file
= NULL
;
155 if (!ima_initialized
|| !S_ISREG(inode
->i_mode
))
157 iint
= ima_iint_find_insert_get(inode
);
161 mutex_lock(&iint
->mutex
);
162 if ((mask
& MAY_WRITE
) || (mask
== 0))
164 else if (mask
& (MAY_READ
| MAY_EXEC
))
167 rc
= ima_must_measure(iint
, inode
, MAY_READ
, PATH_CHECK
);
171 if ((mask
& MAY_WRITE
) || (mask
== 0))
172 ima_read_write_check(TOMTOU
, iint
, inode
,
173 path
->dentry
->d_name
.name
);
175 if ((mask
& (MAY_WRITE
| MAY_READ
| MAY_EXEC
)) != MAY_READ
)
178 ima_read_write_check(OPEN_WRITERS
, iint
, inode
,
179 path
->dentry
->d_name
.name
);
180 if (!(iint
->flags
& IMA_MEASURED
)) {
181 struct dentry
*dentry
= dget(path
->dentry
);
182 struct vfsmount
*mnt
= mntget(path
->mnt
);
184 file
= dentry_open(dentry
, mnt
, O_RDONLY
, current
->cred
);
185 rc
= get_path_measurement(iint
, file
, dentry
->d_name
.name
);
188 mutex_unlock(&iint
->mutex
);
191 kref_put(&iint
->refcount
, iint_free
);
195 static int process_measurement(struct file
*file
, const unsigned char *filename
,
196 int mask
, int function
)
198 struct inode
*inode
= file
->f_dentry
->d_inode
;
199 struct ima_iint_cache
*iint
;
202 if (!ima_initialized
|| !S_ISREG(inode
->i_mode
))
204 iint
= ima_iint_find_insert_get(inode
);
208 mutex_lock(&iint
->mutex
);
209 rc
= ima_must_measure(iint
, inode
, mask
, function
);
213 rc
= ima_collect_measurement(iint
, file
);
215 ima_store_measurement(iint
, file
, filename
);
217 mutex_unlock(&iint
->mutex
);
218 kref_put(&iint
->refcount
, iint_free
);
223 * ima_file_mmap - based on policy, collect/store measurement.
224 * @file: pointer to the file to be measured (May be NULL)
225 * @prot: contains the protection that will be applied by the kernel.
227 * Measure files being mmapped executable based on the ima_must_measure()
230 * Return 0 on success, an error code on failure.
231 * (Based on the results of appraise_measurement().)
233 int ima_file_mmap(struct file
*file
, unsigned long prot
)
239 if (prot
& PROT_EXEC
)
240 rc
= process_measurement(file
, file
->f_dentry
->d_name
.name
,
241 MAY_EXEC
, FILE_MMAP
);
246 * ima_bprm_check - based on policy, collect/store measurement.
247 * @bprm: contains the linux_binprm structure
249 * The OS protects against an executable file, already open for write,
250 * from being executed in deny_write_access() and an executable file,
251 * already open for execute, from being modified in get_write_access().
252 * So we can be certain that what we verify and measure here is actually
253 * what is being executed.
255 * Return 0 on success, an error code on failure.
256 * (Based on the results of appraise_measurement().)
258 int ima_bprm_check(struct linux_binprm
*bprm
)
262 rc
= process_measurement(bprm
->file
, bprm
->filename
,
263 MAY_EXEC
, BPRM_CHECK
);
267 static int __init
init_ima(void)
271 ima_iintcache_init();
277 late_initcall(init_ima
); /* Start IMA after the TPM is available */
279 MODULE_DESCRIPTION("Integrity Measurement Architecture");
280 MODULE_LICENSE("GPL");