]>
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; |
ea1046d4 DK |
64 | char *pathbuf = NULL; |
65 | const char *pathname; | |
8eb988c7 | 66 | |
890275b5 | 67 | if (!S_ISREG(inode->i_mode) || !ima_initialized) |
8eb988c7 | 68 | return; |
a178d202 | 69 | |
890275b5 | 70 | mutex_lock(&inode->i_mutex); /* file metadata: permissions, xattr */ |
ad16ad00 | 71 | |
8eb988c7 | 72 | if (mode & FMODE_WRITE) { |
a68a27b6 | 73 | if (atomic_read(&inode->i_readcount) && IS_IMA(inode)) |
ad16ad00 | 74 | send_tomtou = true; |
8eb988c7 MZ |
75 | goto out; |
76 | } | |
ad16ad00 | 77 | |
2fe5d6de MZ |
78 | must_measure = ima_must_measure(inode, MAY_READ, FILE_CHECK); |
79 | if (!must_measure) | |
bade72d6 EP |
80 | goto out; |
81 | ||
ad16ad00 EP |
82 | if (atomic_read(&inode->i_writecount) > 0) |
83 | send_writers = true; | |
8eb988c7 | 84 | out: |
890275b5 | 85 | mutex_unlock(&inode->i_mutex); |
ad16ad00 | 86 | |
08e1b76a MZ |
87 | if (!send_tomtou && !send_writers) |
88 | return; | |
89 | ||
ea1046d4 DK |
90 | pathname = ima_d_path(&file->f_path, &pathbuf); |
91 | if (!pathname || strlen(pathname) > IMA_EVENT_NAME_LEN_MAX) | |
92 | pathname = dentry->d_name.name; | |
93 | ||
ad16ad00 | 94 | if (send_tomtou) |
ea1046d4 | 95 | ima_add_violation(inode, pathname, |
08e1b76a | 96 | "invalid_pcr", "ToMToU"); |
ad16ad00 | 97 | if (send_writers) |
ea1046d4 | 98 | ima_add_violation(inode, pathname, |
08e1b76a MZ |
99 | "invalid_pcr", "open_writers"); |
100 | kfree(pathbuf); | |
8eb988c7 MZ |
101 | } |
102 | ||
f381c272 | 103 | static void ima_check_last_writer(struct integrity_iint_cache *iint, |
2fe5d6de | 104 | struct inode *inode, struct file *file) |
bc7d2a3e | 105 | { |
4b2a2c67 | 106 | fmode_t mode = file->f_mode; |
bc7d2a3e | 107 | |
2fe5d6de MZ |
108 | if (!(mode & FMODE_WRITE)) |
109 | return; | |
110 | ||
111 | mutex_lock(&inode->i_mutex); | |
112 | if (atomic_read(&inode->i_writecount) == 1 && | |
113 | iint->version != inode->i_version) { | |
45e2472e | 114 | iint->flags &= ~IMA_DONE_MASK; |
2fe5d6de MZ |
115 | if (iint->flags & IMA_APPRAISE) |
116 | ima_update_xattr(iint, file); | |
117 | } | |
118 | mutex_unlock(&inode->i_mutex); | |
bc7d2a3e EP |
119 | } |
120 | ||
3323eec9 MZ |
121 | /** |
122 | * ima_file_free - called on __fput() | |
123 | * @file: pointer to file structure being freed | |
124 | * | |
890275b5 | 125 | * Flag files that changed, based on i_version |
3323eec9 MZ |
126 | */ |
127 | void ima_file_free(struct file *file) | |
128 | { | |
129 | struct inode *inode = file->f_dentry->d_inode; | |
f381c272 | 130 | struct integrity_iint_cache *iint; |
3323eec9 | 131 | |
e950598d | 132 | if (!iint_initialized || !S_ISREG(inode->i_mode)) |
3323eec9 | 133 | return; |
196f5181 | 134 | |
f381c272 | 135 | iint = integrity_iint_find(inode); |
854fdd55 MZ |
136 | if (!iint) |
137 | return; | |
3323eec9 | 138 | |
854fdd55 | 139 | ima_check_last_writer(iint, inode, file); |
3323eec9 MZ |
140 | } |
141 | ||
ea1046d4 | 142 | static int process_measurement(struct file *file, const char *filename, |
3323eec9 MZ |
143 | int mask, int function) |
144 | { | |
145 | struct inode *inode = file->f_dentry->d_inode; | |
f381c272 | 146 | struct integrity_iint_cache *iint; |
ea1046d4 DK |
147 | char *pathbuf = NULL; |
148 | const char *pathname = NULL; | |
2fe5d6de | 149 | int rc = -ENOMEM, action, must_appraise; |
3323eec9 MZ |
150 | |
151 | if (!ima_initialized || !S_ISREG(inode->i_mode)) | |
152 | return 0; | |
bc7d2a3e | 153 | |
e7c568e0 PM |
154 | /* Determine if in appraise/audit/measurement policy, |
155 | * returns IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT bitmask. */ | |
d9d300cd | 156 | action = ima_get_action(inode, mask, function); |
2fe5d6de MZ |
157 | if (!action) |
158 | return 0; | |
159 | ||
2fe5d6de | 160 | must_appraise = action & IMA_APPRAISE; |
bc7d2a3e | 161 | |
2fe5d6de MZ |
162 | mutex_lock(&inode->i_mutex); |
163 | ||
bf2276d1 DK |
164 | iint = integrity_inode_get(inode); |
165 | if (!iint) | |
166 | goto out; | |
167 | ||
2fe5d6de | 168 | /* Determine if already appraised/measured based on bitmask |
e7c568e0 PM |
169 | * (IMA_MEASURE, IMA_MEASURED, IMA_APPRAISE, IMA_APPRAISED, |
170 | * IMA_AUDIT, IMA_AUDITED) */ | |
2fe5d6de | 171 | iint->flags |= action; |
45e2472e | 172 | action &= ~((iint->flags & IMA_DONE_MASK) >> 1); |
2fe5d6de MZ |
173 | |
174 | /* Nothing to do, just return existing appraised status */ | |
175 | if (!action) { | |
176 | if (iint->flags & IMA_APPRAISED) | |
177 | rc = iint->ima_status; | |
3323eec9 | 178 | goto out; |
2fe5d6de | 179 | } |
3323eec9 MZ |
180 | |
181 | rc = ima_collect_measurement(iint, file); | |
08e1b76a MZ |
182 | if (rc != 0) |
183 | goto out; | |
184 | ||
ea1046d4 DK |
185 | if (function != BPRM_CHECK) |
186 | pathname = ima_d_path(&file->f_path, &pathbuf); | |
187 | ||
188 | if (!pathname) | |
189 | pathname = filename; | |
190 | ||
2fe5d6de | 191 | if (action & IMA_MEASURE) |
ea1046d4 | 192 | ima_store_measurement(iint, file, pathname); |
2fe5d6de | 193 | if (action & IMA_APPRAISE) |
ea1046d4 | 194 | rc = ima_appraise_measurement(iint, file, pathname); |
e7c568e0 | 195 | if (action & IMA_AUDIT) |
ea1046d4 | 196 | ima_audit_measurement(iint, pathname); |
08e1b76a | 197 | kfree(pathbuf); |
3323eec9 | 198 | out: |
2fe5d6de | 199 | mutex_unlock(&inode->i_mutex); |
750943a3 DK |
200 | if ((rc && must_appraise) && (ima_appraise & IMA_APPRAISE_ENFORCE)) |
201 | return -EACCES; | |
202 | return 0; | |
3323eec9 MZ |
203 | } |
204 | ||
205 | /** | |
206 | * ima_file_mmap - based on policy, collect/store measurement. | |
207 | * @file: pointer to the file to be measured (May be NULL) | |
208 | * @prot: contains the protection that will be applied by the kernel. | |
209 | * | |
210 | * Measure files being mmapped executable based on the ima_must_measure() | |
211 | * policy decision. | |
212 | * | |
750943a3 DK |
213 | * On success return 0. On integrity appraisal error, assuming the file |
214 | * is in policy and IMA-appraisal is in enforcing mode, return -EACCES. | |
3323eec9 MZ |
215 | */ |
216 | int ima_file_mmap(struct file *file, unsigned long prot) | |
217 | { | |
750943a3 DK |
218 | if (file && (prot & PROT_EXEC)) |
219 | return process_measurement(file, file->f_dentry->d_name.name, | |
16cac49f | 220 | MAY_EXEC, MMAP_CHECK); |
750943a3 | 221 | return 0; |
3323eec9 MZ |
222 | } |
223 | ||
224 | /** | |
225 | * ima_bprm_check - based on policy, collect/store measurement. | |
226 | * @bprm: contains the linux_binprm structure | |
227 | * | |
228 | * The OS protects against an executable file, already open for write, | |
229 | * from being executed in deny_write_access() and an executable file, | |
230 | * already open for execute, from being modified in get_write_access(). | |
231 | * So we can be certain that what we verify and measure here is actually | |
232 | * what is being executed. | |
233 | * | |
750943a3 DK |
234 | * On success return 0. On integrity appraisal error, assuming the file |
235 | * is in policy and IMA-appraisal is in enforcing mode, return -EACCES. | |
3323eec9 MZ |
236 | */ |
237 | int ima_bprm_check(struct linux_binprm *bprm) | |
238 | { | |
750943a3 | 239 | return process_measurement(bprm->file, |
fbbb4563 MZ |
240 | (strcmp(bprm->filename, bprm->interp) == 0) ? |
241 | bprm->filename : bprm->interp, | |
3323eec9 | 242 | MAY_EXEC, BPRM_CHECK); |
3323eec9 MZ |
243 | } |
244 | ||
8eb988c7 MZ |
245 | /** |
246 | * ima_path_check - based on policy, collect/store measurement. | |
247 | * @file: pointer to the file to be measured | |
248 | * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE | |
249 | * | |
250 | * Measure files based on the ima_must_measure() policy decision. | |
251 | * | |
750943a3 DK |
252 | * On success return 0. On integrity appraisal error, assuming the file |
253 | * is in policy and IMA-appraisal is in enforcing mode, return -EACCES. | |
8eb988c7 | 254 | */ |
9bbb6cad | 255 | int ima_file_check(struct file *file, int mask) |
8eb988c7 | 256 | { |
890275b5 | 257 | ima_rdwr_violation_check(file); |
750943a3 | 258 | return process_measurement(file, file->f_dentry->d_name.name, |
8eb988c7 | 259 | mask & (MAY_READ | MAY_WRITE | MAY_EXEC), |
1e93d005 | 260 | FILE_CHECK); |
8eb988c7 | 261 | } |
9bbb6cad | 262 | EXPORT_SYMBOL_GPL(ima_file_check); |
8eb988c7 | 263 | |
fdf90729 MZ |
264 | /** |
265 | * ima_module_check - based on policy, collect/store/appraise measurement. | |
266 | * @file: pointer to the file to be measured/appraised | |
267 | * | |
268 | * Measure/appraise kernel modules based on policy. | |
269 | * | |
750943a3 DK |
270 | * On success return 0. On integrity appraisal error, assuming the file |
271 | * is in policy and IMA-appraisal is in enforcing mode, return -EACCES. | |
fdf90729 MZ |
272 | */ |
273 | int ima_module_check(struct file *file) | |
274 | { | |
fdf90729 | 275 | if (!file) |
750943a3 DK |
276 | return -EACCES; /* INTEGRITY_UNKNOWN */ |
277 | return process_measurement(file, file->f_dentry->d_name.name, | |
278 | MAY_EXEC, MODULE_CHECK); | |
fdf90729 MZ |
279 | } |
280 | ||
3323eec9 MZ |
281 | static int __init init_ima(void) |
282 | { | |
283 | int error; | |
284 | ||
3323eec9 | 285 | error = ima_init(); |
7ff2267a DK |
286 | if (!error) |
287 | ima_initialized = 1; | |
3323eec9 MZ |
288 | return error; |
289 | } | |
290 | ||
291 | late_initcall(init_ima); /* Start IMA after the TPM is available */ | |
292 | ||
293 | MODULE_DESCRIPTION("Integrity Measurement Architecture"); | |
294 | MODULE_LICENSE("GPL"); |