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>
30 char *ima_hash
= "sha1";
31 static int __init
hash_setup(char *str
)
33 if (strncmp(str
, "md5", 3) == 0)
37 __setup("ima_hash=", hash_setup
);
40 * ima_rdwr_violation_check
42 * Only invalidate the PCR for measured files:
43 * - Opening a file for write when already open for read,
44 * results in a time of measure, time of use (ToMToU) error.
45 * - Opening a file for read when already open for write,
46 * could result in a file measurement error.
49 static void ima_rdwr_violation_check(struct file
*file
)
51 struct dentry
*dentry
= file
->f_path
.dentry
;
52 struct inode
*inode
= dentry
->d_inode
;
53 fmode_t mode
= file
->f_mode
;
55 bool send_tomtou
= false, send_writers
= false;
57 if (!S_ISREG(inode
->i_mode
) || !ima_initialized
)
60 mutex_lock(&inode
->i_mutex
); /* file metadata: permissions, xattr */
62 if (mode
& FMODE_WRITE
) {
63 if (atomic_read(&inode
->i_readcount
) && IS_IMA(inode
))
68 rc
= ima_must_measure(inode
, MAY_READ
, FILE_CHECK
);
72 if (atomic_read(&inode
->i_writecount
) > 0)
75 mutex_unlock(&inode
->i_mutex
);
78 ima_add_violation(inode
, dentry
->d_name
.name
, "invalid_pcr",
81 ima_add_violation(inode
, dentry
->d_name
.name
, "invalid_pcr",
85 static void ima_check_last_writer(struct ima_iint_cache
*iint
,
89 mode_t mode
= file
->f_mode
;
91 mutex_lock(&iint
->mutex
);
92 if (mode
& FMODE_WRITE
&&
93 atomic_read(&inode
->i_writecount
) == 1 &&
94 iint
->version
!= inode
->i_version
)
95 iint
->flags
&= ~IMA_MEASURED
;
96 mutex_unlock(&iint
->mutex
);
100 * ima_file_free - called on __fput()
101 * @file: pointer to file structure being freed
103 * Flag files that changed, based on i_version
105 void ima_file_free(struct file
*file
)
107 struct inode
*inode
= file
->f_dentry
->d_inode
;
108 struct ima_iint_cache
*iint
;
110 if (!iint_initialized
|| !S_ISREG(inode
->i_mode
))
113 iint
= ima_iint_find(inode
);
117 ima_check_last_writer(iint
, inode
, file
);
120 static int process_measurement(struct file
*file
, const unsigned char *filename
,
121 int mask
, int function
)
123 struct inode
*inode
= file
->f_dentry
->d_inode
;
124 struct ima_iint_cache
*iint
;
127 if (!ima_initialized
|| !S_ISREG(inode
->i_mode
))
130 rc
= ima_must_measure(inode
, mask
, function
);
134 iint
= ima_iint_find(inode
);
136 rc
= ima_inode_alloc(inode
);
137 if (!rc
|| rc
== -EEXIST
)
142 mutex_lock(&iint
->mutex
);
144 rc
= iint
->flags
& IMA_MEASURED
? 1 : 0;
148 rc
= ima_collect_measurement(iint
, file
);
150 ima_store_measurement(iint
, file
, filename
);
152 mutex_unlock(&iint
->mutex
);
157 * ima_file_mmap - based on policy, collect/store measurement.
158 * @file: pointer to the file to be measured (May be NULL)
159 * @prot: contains the protection that will be applied by the kernel.
161 * Measure files being mmapped executable based on the ima_must_measure()
164 * Return 0 on success, an error code on failure.
165 * (Based on the results of appraise_measurement().)
167 int ima_file_mmap(struct file
*file
, unsigned long prot
)
173 if (prot
& PROT_EXEC
)
174 rc
= process_measurement(file
, file
->f_dentry
->d_name
.name
,
175 MAY_EXEC
, FILE_MMAP
);
180 * ima_bprm_check - based on policy, collect/store measurement.
181 * @bprm: contains the linux_binprm structure
183 * The OS protects against an executable file, already open for write,
184 * from being executed in deny_write_access() and an executable file,
185 * already open for execute, from being modified in get_write_access().
186 * So we can be certain that what we verify and measure here is actually
187 * what is being executed.
189 * Return 0 on success, an error code on failure.
190 * (Based on the results of appraise_measurement().)
192 int ima_bprm_check(struct linux_binprm
*bprm
)
196 rc
= process_measurement(bprm
->file
, bprm
->filename
,
197 MAY_EXEC
, BPRM_CHECK
);
202 * ima_path_check - based on policy, collect/store measurement.
203 * @file: pointer to the file to be measured
204 * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
206 * Measure files based on the ima_must_measure() policy decision.
208 * Always return 0 and audit dentry_open failures.
209 * (Return code will be based upon measurement appraisal.)
211 int ima_file_check(struct file
*file
, int mask
)
215 ima_rdwr_violation_check(file
);
216 rc
= process_measurement(file
, file
->f_dentry
->d_name
.name
,
217 mask
& (MAY_READ
| MAY_WRITE
| MAY_EXEC
),
221 EXPORT_SYMBOL_GPL(ima_file_check
);
223 static int __init
init_ima(void)
232 static void __exit
cleanup_ima(void)
237 late_initcall(init_ima
); /* Start IMA after the TPM is available */
239 MODULE_DESCRIPTION("Integrity Measurement Architecture");
240 MODULE_LICENSE("GPL");