]>
Commit | Line | Data |
---|---|---|
3323eec9 MZ |
1 | /* |
2 | * Copyright (C) 2005,2006,2007,2008 IBM Corporation | |
3 | * | |
4 | * Authors: | |
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> | |
9 | * | |
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 | |
13 | * License. | |
14 | * | |
15 | * File: ima_main.c | |
e0d5bd2a | 16 | * implements the IMA hooks: ima_bprm_check, ima_file_mmap, |
9bbb6cad | 17 | * and ima_file_check. |
3323eec9 MZ |
18 | */ |
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> | |
5a0e3ad6 | 24 | #include <linux/slab.h> |
2fe5d6de | 25 | #include <linux/xattr.h> |
d5813a57 | 26 | #include <linux/ima.h> |
3323eec9 MZ |
27 | |
28 | #include "ima.h" | |
29 | ||
30 | int ima_initialized; | |
31 | ||
2fe5d6de MZ |
32 | #ifdef CONFIG_IMA_APPRAISE |
33 | int ima_appraise = IMA_APPRAISE_ENFORCE; | |
34 | #else | |
35 | int ima_appraise; | |
36 | #endif | |
37 | ||
3323eec9 MZ |
38 | char *ima_hash = "sha1"; |
39 | static int __init hash_setup(char *str) | |
40 | { | |
07ff7a0b MZ |
41 | if (strncmp(str, "md5", 3) == 0) |
42 | ima_hash = "md5"; | |
3323eec9 MZ |
43 | return 1; |
44 | } | |
45 | __setup("ima_hash=", hash_setup); | |
46 | ||
8eb988c7 | 47 | /* |
890275b5 | 48 | * ima_rdwr_violation_check |
8eb988c7 | 49 | * |
890275b5 | 50 | * Only invalidate the PCR for measured files: |
8eb988c7 MZ |
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. | |
55 | * | |
56 | */ | |
890275b5 | 57 | static void ima_rdwr_violation_check(struct file *file) |
8eb988c7 MZ |
58 | { |
59 | struct dentry *dentry = file->f_path.dentry; | |
60 | struct inode *inode = dentry->d_inode; | |
61 | fmode_t mode = file->f_mode; | |
2fe5d6de | 62 | int must_measure; |
ad16ad00 | 63 | bool send_tomtou = false, send_writers = false; |
08e1b76a | 64 | unsigned char *pathname = NULL, *pathbuf = NULL; |
8eb988c7 | 65 | |
890275b5 | 66 | if (!S_ISREG(inode->i_mode) || !ima_initialized) |
8eb988c7 | 67 | return; |
a178d202 | 68 | |
890275b5 | 69 | mutex_lock(&inode->i_mutex); /* file metadata: permissions, xattr */ |
ad16ad00 | 70 | |
8eb988c7 | 71 | if (mode & FMODE_WRITE) { |
a68a27b6 | 72 | if (atomic_read(&inode->i_readcount) && IS_IMA(inode)) |
ad16ad00 | 73 | send_tomtou = true; |
8eb988c7 MZ |
74 | goto out; |
75 | } | |
ad16ad00 | 76 | |
2fe5d6de MZ |
77 | must_measure = ima_must_measure(inode, MAY_READ, FILE_CHECK); |
78 | if (!must_measure) | |
bade72d6 EP |
79 | goto out; |
80 | ||
ad16ad00 EP |
81 | if (atomic_read(&inode->i_writecount) > 0) |
82 | send_writers = true; | |
8eb988c7 | 83 | out: |
890275b5 | 84 | mutex_unlock(&inode->i_mutex); |
ad16ad00 | 85 | |
08e1b76a MZ |
86 | if (!send_tomtou && !send_writers) |
87 | return; | |
88 | ||
89 | /* We will allow 11 spaces for ' (deleted)' to be appended */ | |
90 | pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL); | |
91 | if (pathbuf) { | |
92 | pathname = d_path(&file->f_path, pathbuf, PATH_MAX + 11); | |
93 | if (IS_ERR(pathname)) | |
94 | pathname = NULL; | |
95 | else if (strlen(pathname) > IMA_EVENT_NAME_LEN_MAX) | |
96 | pathname = NULL; | |
97 | } | |
ad16ad00 | 98 | if (send_tomtou) |
08e1b76a MZ |
99 | ima_add_violation(inode, |
100 | !pathname ? dentry->d_name.name : pathname, | |
101 | "invalid_pcr", "ToMToU"); | |
ad16ad00 | 102 | if (send_writers) |
08e1b76a MZ |
103 | ima_add_violation(inode, |
104 | !pathname ? dentry->d_name.name : pathname, | |
105 | "invalid_pcr", "open_writers"); | |
106 | kfree(pathbuf); | |
8eb988c7 MZ |
107 | } |
108 | ||
f381c272 | 109 | static void ima_check_last_writer(struct integrity_iint_cache *iint, |
2fe5d6de | 110 | struct inode *inode, struct file *file) |
bc7d2a3e | 111 | { |
4b2a2c67 | 112 | fmode_t mode = file->f_mode; |
bc7d2a3e | 113 | |
2fe5d6de MZ |
114 | if (!(mode & FMODE_WRITE)) |
115 | return; | |
116 | ||
117 | mutex_lock(&inode->i_mutex); | |
118 | if (atomic_read(&inode->i_writecount) == 1 && | |
119 | iint->version != inode->i_version) { | |
120 | iint->flags &= ~(IMA_COLLECTED | IMA_APPRAISED | IMA_MEASURED); | |
121 | if (iint->flags & IMA_APPRAISE) | |
122 | ima_update_xattr(iint, file); | |
123 | } | |
124 | mutex_unlock(&inode->i_mutex); | |
bc7d2a3e EP |
125 | } |
126 | ||
3323eec9 MZ |
127 | /** |
128 | * ima_file_free - called on __fput() | |
129 | * @file: pointer to file structure being freed | |
130 | * | |
890275b5 | 131 | * Flag files that changed, based on i_version |
3323eec9 MZ |
132 | */ |
133 | void ima_file_free(struct file *file) | |
134 | { | |
135 | struct inode *inode = file->f_dentry->d_inode; | |
f381c272 | 136 | struct integrity_iint_cache *iint; |
3323eec9 | 137 | |
e950598d | 138 | if (!iint_initialized || !S_ISREG(inode->i_mode)) |
3323eec9 | 139 | return; |
196f5181 | 140 | |
f381c272 | 141 | iint = integrity_iint_find(inode); |
854fdd55 MZ |
142 | if (!iint) |
143 | return; | |
3323eec9 | 144 | |
854fdd55 | 145 | ima_check_last_writer(iint, inode, file); |
3323eec9 MZ |
146 | } |
147 | ||
3323eec9 MZ |
148 | static int process_measurement(struct file *file, const unsigned char *filename, |
149 | int mask, int function) | |
150 | { | |
151 | struct inode *inode = file->f_dentry->d_inode; | |
f381c272 | 152 | struct integrity_iint_cache *iint; |
08e1b76a | 153 | unsigned char *pathname = NULL, *pathbuf = NULL; |
2fe5d6de | 154 | int rc = -ENOMEM, action, must_appraise; |
3323eec9 MZ |
155 | |
156 | if (!ima_initialized || !S_ISREG(inode->i_mode)) | |
157 | return 0; | |
bc7d2a3e | 158 | |
2fe5d6de MZ |
159 | /* Determine if in appraise/measurement policy, |
160 | * returns IMA_MEASURE, IMA_APPRAISE bitmask. */ | |
161 | action = ima_must_appraise_or_measure(inode, mask, function); | |
162 | if (!action) | |
163 | return 0; | |
164 | ||
bc7d2a3e | 165 | retry: |
f381c272 | 166 | iint = integrity_iint_find(inode); |
bc7d2a3e | 167 | if (!iint) { |
f381c272 | 168 | rc = integrity_inode_alloc(inode); |
bc7d2a3e EP |
169 | if (!rc || rc == -EEXIST) |
170 | goto retry; | |
171 | return rc; | |
172 | } | |
3323eec9 | 173 | |
2fe5d6de | 174 | must_appraise = action & IMA_APPRAISE; |
bc7d2a3e | 175 | |
2fe5d6de MZ |
176 | mutex_lock(&inode->i_mutex); |
177 | ||
178 | /* Determine if already appraised/measured based on bitmask | |
179 | * (IMA_MEASURE, IMA_MEASURED, IMA_APPRAISE, IMA_APPRAISED) */ | |
180 | iint->flags |= action; | |
181 | action &= ~((iint->flags & (IMA_MEASURED | IMA_APPRAISED)) >> 1); | |
182 | ||
183 | /* Nothing to do, just return existing appraised status */ | |
184 | if (!action) { | |
185 | if (iint->flags & IMA_APPRAISED) | |
186 | rc = iint->ima_status; | |
3323eec9 | 187 | goto out; |
2fe5d6de | 188 | } |
3323eec9 MZ |
189 | |
190 | rc = ima_collect_measurement(iint, file); | |
08e1b76a MZ |
191 | if (rc != 0) |
192 | goto out; | |
193 | ||
194 | if (function != BPRM_CHECK) { | |
195 | /* We will allow 11 spaces for ' (deleted)' to be appended */ | |
196 | pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL); | |
197 | if (pathbuf) { | |
198 | pathname = | |
199 | d_path(&file->f_path, pathbuf, PATH_MAX + 11); | |
200 | if (IS_ERR(pathname)) | |
201 | pathname = NULL; | |
202 | } | |
203 | } | |
2fe5d6de MZ |
204 | if (action & IMA_MEASURE) |
205 | ima_store_measurement(iint, file, | |
206 | !pathname ? filename : pathname); | |
207 | if (action & IMA_APPRAISE) | |
208 | rc = ima_appraise_measurement(iint, file, | |
209 | !pathname ? filename : pathname); | |
08e1b76a | 210 | kfree(pathbuf); |
3323eec9 | 211 | out: |
2fe5d6de MZ |
212 | mutex_unlock(&inode->i_mutex); |
213 | return (rc && must_appraise) ? -EACCES : 0; | |
3323eec9 MZ |
214 | } |
215 | ||
216 | /** | |
217 | * ima_file_mmap - based on policy, collect/store measurement. | |
218 | * @file: pointer to the file to be measured (May be NULL) | |
219 | * @prot: contains the protection that will be applied by the kernel. | |
220 | * | |
221 | * Measure files being mmapped executable based on the ima_must_measure() | |
222 | * policy decision. | |
223 | * | |
224 | * Return 0 on success, an error code on failure. | |
225 | * (Based on the results of appraise_measurement().) | |
226 | */ | |
227 | int ima_file_mmap(struct file *file, unsigned long prot) | |
228 | { | |
2fe5d6de | 229 | int rc = 0; |
3323eec9 MZ |
230 | |
231 | if (!file) | |
232 | return 0; | |
233 | if (prot & PROT_EXEC) | |
234 | rc = process_measurement(file, file->f_dentry->d_name.name, | |
235 | MAY_EXEC, FILE_MMAP); | |
2fe5d6de | 236 | return (ima_appraise & IMA_APPRAISE_ENFORCE) ? rc : 0; |
3323eec9 MZ |
237 | } |
238 | ||
239 | /** | |
240 | * ima_bprm_check - based on policy, collect/store measurement. | |
241 | * @bprm: contains the linux_binprm structure | |
242 | * | |
243 | * The OS protects against an executable file, already open for write, | |
244 | * from being executed in deny_write_access() and an executable file, | |
245 | * already open for execute, from being modified in get_write_access(). | |
246 | * So we can be certain that what we verify and measure here is actually | |
247 | * what is being executed. | |
248 | * | |
249 | * Return 0 on success, an error code on failure. | |
250 | * (Based on the results of appraise_measurement().) | |
251 | */ | |
252 | int ima_bprm_check(struct linux_binprm *bprm) | |
253 | { | |
254 | int rc; | |
255 | ||
fbbb4563 MZ |
256 | rc = process_measurement(bprm->file, |
257 | (strcmp(bprm->filename, bprm->interp) == 0) ? | |
258 | bprm->filename : bprm->interp, | |
3323eec9 | 259 | MAY_EXEC, BPRM_CHECK); |
2fe5d6de | 260 | return (ima_appraise & IMA_APPRAISE_ENFORCE) ? rc : 0; |
3323eec9 MZ |
261 | } |
262 | ||
8eb988c7 MZ |
263 | /** |
264 | * ima_path_check - based on policy, collect/store measurement. | |
265 | * @file: pointer to the file to be measured | |
266 | * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE | |
267 | * | |
268 | * Measure files based on the ima_must_measure() policy decision. | |
269 | * | |
270 | * Always return 0 and audit dentry_open failures. | |
271 | * (Return code will be based upon measurement appraisal.) | |
272 | */ | |
9bbb6cad | 273 | int ima_file_check(struct file *file, int mask) |
8eb988c7 MZ |
274 | { |
275 | int rc; | |
276 | ||
890275b5 | 277 | ima_rdwr_violation_check(file); |
8eb988c7 MZ |
278 | rc = process_measurement(file, file->f_dentry->d_name.name, |
279 | mask & (MAY_READ | MAY_WRITE | MAY_EXEC), | |
1e93d005 | 280 | FILE_CHECK); |
2fe5d6de | 281 | return (ima_appraise & IMA_APPRAISE_ENFORCE) ? rc : 0; |
8eb988c7 | 282 | } |
9bbb6cad | 283 | EXPORT_SYMBOL_GPL(ima_file_check); |
8eb988c7 | 284 | |
3323eec9 MZ |
285 | static int __init init_ima(void) |
286 | { | |
287 | int error; | |
288 | ||
3323eec9 | 289 | error = ima_init(); |
7ff2267a DK |
290 | if (!error) |
291 | ima_initialized = 1; | |
3323eec9 MZ |
292 | return error; |
293 | } | |
294 | ||
295 | late_initcall(init_ima); /* Start IMA after the TPM is available */ | |
296 | ||
297 | MODULE_DESCRIPTION("Integrity Measurement Architecture"); | |
298 | MODULE_LICENSE("GPL"); |