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/ima.h>
31 char *ima_hash
= "sha1";
32 static int __init
hash_setup(char *str
)
34 if (strncmp(str
, "md5", 3) == 0)
38 __setup("ima_hash=", hash_setup
);
41 * ima_rdwr_violation_check
43 * Only invalidate the PCR for measured files:
44 * - Opening a file for write when already open for read,
45 * results in a time of measure, time of use (ToMToU) error.
46 * - Opening a file for read when already open for write,
47 * could result in a file measurement error.
50 static void ima_rdwr_violation_check(struct file
*file
)
52 struct dentry
*dentry
= file
->f_path
.dentry
;
53 struct inode
*inode
= dentry
->d_inode
;
54 fmode_t mode
= file
->f_mode
;
56 bool send_tomtou
= false, send_writers
= false;
58 if (!S_ISREG(inode
->i_mode
) || !ima_initialized
)
61 mutex_lock(&inode
->i_mutex
); /* file metadata: permissions, xattr */
63 if (mode
& FMODE_WRITE
) {
64 if (atomic_read(&inode
->i_readcount
) && IS_IMA(inode
))
69 rc
= ima_must_measure(inode
, MAY_READ
, FILE_CHECK
);
73 if (atomic_read(&inode
->i_writecount
) > 0)
76 mutex_unlock(&inode
->i_mutex
);
79 ima_add_violation(inode
, dentry
->d_name
.name
, "invalid_pcr",
82 ima_add_violation(inode
, dentry
->d_name
.name
, "invalid_pcr",
86 static void ima_check_last_writer(struct integrity_iint_cache
*iint
,
90 fmode_t mode
= file
->f_mode
;
92 mutex_lock(&iint
->mutex
);
93 if (mode
& FMODE_WRITE
&&
94 atomic_read(&inode
->i_writecount
) == 1 &&
95 iint
->version
!= inode
->i_version
)
96 iint
->flags
&= ~IMA_MEASURED
;
97 mutex_unlock(&iint
->mutex
);
101 * ima_file_free - called on __fput()
102 * @file: pointer to file structure being freed
104 * Flag files that changed, based on i_version
106 void ima_file_free(struct file
*file
)
108 struct inode
*inode
= file
->f_dentry
->d_inode
;
109 struct integrity_iint_cache
*iint
;
111 if (!iint_initialized
|| !S_ISREG(inode
->i_mode
))
114 iint
= integrity_iint_find(inode
);
118 ima_check_last_writer(iint
, inode
, file
);
121 static int process_measurement(struct file
*file
, const unsigned char *filename
,
122 int mask
, int function
)
124 struct inode
*inode
= file
->f_dentry
->d_inode
;
125 struct integrity_iint_cache
*iint
;
128 if (!ima_initialized
|| !S_ISREG(inode
->i_mode
))
131 rc
= ima_must_measure(inode
, mask
, function
);
135 iint
= integrity_iint_find(inode
);
137 rc
= integrity_inode_alloc(inode
);
138 if (!rc
|| rc
== -EEXIST
)
143 mutex_lock(&iint
->mutex
);
145 rc
= iint
->flags
& IMA_MEASURED
? 1 : 0;
149 rc
= ima_collect_measurement(iint
, file
);
151 ima_store_measurement(iint
, file
, filename
);
153 mutex_unlock(&iint
->mutex
);
158 * ima_file_mmap - based on policy, collect/store measurement.
159 * @file: pointer to the file to be measured (May be NULL)
160 * @prot: contains the protection that will be applied by the kernel.
162 * Measure files being mmapped executable based on the ima_must_measure()
165 * Return 0 on success, an error code on failure.
166 * (Based on the results of appraise_measurement().)
168 int ima_file_mmap(struct file
*file
, unsigned long prot
)
174 if (prot
& PROT_EXEC
)
175 rc
= process_measurement(file
, file
->f_dentry
->d_name
.name
,
176 MAY_EXEC
, FILE_MMAP
);
181 * ima_bprm_check - based on policy, collect/store measurement.
182 * @bprm: contains the linux_binprm structure
184 * The OS protects against an executable file, already open for write,
185 * from being executed in deny_write_access() and an executable file,
186 * already open for execute, from being modified in get_write_access().
187 * So we can be certain that what we verify and measure here is actually
188 * what is being executed.
190 * Return 0 on success, an error code on failure.
191 * (Based on the results of appraise_measurement().)
193 int ima_bprm_check(struct linux_binprm
*bprm
)
197 rc
= process_measurement(bprm
->file
, bprm
->filename
,
198 MAY_EXEC
, BPRM_CHECK
);
203 * ima_path_check - based on policy, collect/store measurement.
204 * @file: pointer to the file to be measured
205 * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
207 * Measure files based on the ima_must_measure() policy decision.
209 * Always return 0 and audit dentry_open failures.
210 * (Return code will be based upon measurement appraisal.)
212 int ima_file_check(struct file
*file
, int mask
)
216 ima_rdwr_violation_check(file
);
217 rc
= process_measurement(file
, file
->f_dentry
->d_name
.name
,
218 mask
& (MAY_READ
| MAY_WRITE
| MAY_EXEC
),
222 EXPORT_SYMBOL_GPL(ima_file_check
);
224 static int __init
init_ima(void)
233 static void __exit
cleanup_ima(void)
238 late_initcall(init_ima
); /* Start IMA after the TPM is available */
240 MODULE_DESCRIPTION("Integrity Measurement Architecture");
241 MODULE_LICENSE("GPL");