1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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>
24#include <linux/slab.h>
25#include <linux/xattr.h>
26#include <linux/ima.h>
27
28#include "ima.h"
29
30int ima_initialized;
31
32#ifdef CONFIG_IMA_APPRAISE
33int ima_appraise = IMA_APPRAISE_ENFORCE;
34#else
35int ima_appraise;
36#endif
37
38char *ima_hash = "sha1";
39static int __init hash_setup(char *str)
40{
41 if (strncmp(str, "md5", 3) == 0)
42 ima_hash = "md5";
43 return 1;
44}
45__setup("ima_hash=", hash_setup);
46
47
48
49
50
51
52
53
54
55
56
57static void ima_rdwr_violation_check(struct file *file)
58{
59 struct dentry *dentry = file->f_path.dentry;
60 struct inode *inode = dentry->d_inode;
61 fmode_t mode = file->f_mode;
62 int must_measure;
63 bool send_tomtou = false, send_writers = false;
64 unsigned char *pathname = NULL, *pathbuf = NULL;
65
66 if (!S_ISREG(inode->i_mode) || !ima_initialized)
67 return;
68
69 mutex_lock(&inode->i_mutex);
70
71 if (mode & FMODE_WRITE) {
72 if (atomic_read(&inode->i_readcount) && IS_IMA(inode))
73 send_tomtou = true;
74 goto out;
75 }
76
77 must_measure = ima_must_measure(inode, MAY_READ, FILE_CHECK);
78 if (!must_measure)
79 goto out;
80
81 if (atomic_read(&inode->i_writecount) > 0)
82 send_writers = true;
83out:
84 mutex_unlock(&inode->i_mutex);
85
86 if (!send_tomtou && !send_writers)
87 return;
88
89
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 }
98 if (send_tomtou)
99 ima_add_violation(inode,
100 !pathname ? dentry->d_name.name : pathname,
101 "invalid_pcr", "ToMToU");
102 if (send_writers)
103 ima_add_violation(inode,
104 !pathname ? dentry->d_name.name : pathname,
105 "invalid_pcr", "open_writers");
106 kfree(pathbuf);
107}
108
109static void ima_check_last_writer(struct integrity_iint_cache *iint,
110 struct inode *inode, struct file *file)
111{
112 fmode_t mode = file->f_mode;
113
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_DONE_MASK;
121 if (iint->flags & IMA_APPRAISE)
122 ima_update_xattr(iint, file);
123 }
124 mutex_unlock(&inode->i_mutex);
125}
126
127
128
129
130
131
132
133void ima_file_free(struct file *file)
134{
135 struct inode *inode = file->f_dentry->d_inode;
136 struct integrity_iint_cache *iint;
137
138 if (!iint_initialized || !S_ISREG(inode->i_mode))
139 return;
140
141 iint = integrity_iint_find(inode);
142 if (!iint)
143 return;
144
145 ima_check_last_writer(iint, inode, file);
146}
147
148static 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;
152 struct integrity_iint_cache *iint;
153 unsigned char *pathname = NULL, *pathbuf = NULL;
154 int rc = -ENOMEM, action, must_appraise;
155
156 if (!ima_initialized || !S_ISREG(inode->i_mode))
157 return 0;
158
159
160
161 action = ima_get_action(inode, mask, function);
162 if (!action)
163 return 0;
164
165 must_appraise = action & IMA_APPRAISE;
166
167 mutex_lock(&inode->i_mutex);
168
169 iint = integrity_inode_get(inode);
170 if (!iint)
171 goto out;
172
173
174
175
176 iint->flags |= action;
177 action &= ~((iint->flags & IMA_DONE_MASK) >> 1);
178
179
180 if (!action) {
181 if (iint->flags & IMA_APPRAISED)
182 rc = iint->ima_status;
183 goto out;
184 }
185
186 rc = ima_collect_measurement(iint, file);
187 if (rc != 0)
188 goto out;
189
190 if (function != BPRM_CHECK) {
191
192 pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL);
193 if (pathbuf) {
194 pathname =
195 d_path(&file->f_path, pathbuf, PATH_MAX + 11);
196 if (IS_ERR(pathname))
197 pathname = NULL;
198 }
199 }
200 if (action & IMA_MEASURE)
201 ima_store_measurement(iint, file,
202 !pathname ? filename : pathname);
203 if (action & IMA_APPRAISE)
204 rc = ima_appraise_measurement(iint, file,
205 !pathname ? filename : pathname);
206 if (action & IMA_AUDIT)
207 ima_audit_measurement(iint, !pathname ? filename : pathname);
208 kfree(pathbuf);
209out:
210 mutex_unlock(&inode->i_mutex);
211 return (rc && must_appraise) ? -EACCES : 0;
212}
213
214
215
216
217
218
219
220
221
222
223
224
225int ima_file_mmap(struct file *file, unsigned long prot)
226{
227 int rc = 0;
228
229 if (!file)
230 return 0;
231 if (prot & PROT_EXEC)
232 rc = process_measurement(file, file->f_dentry->d_name.name,
233 MAY_EXEC, FILE_MMAP);
234 return (ima_appraise & IMA_APPRAISE_ENFORCE) ? rc : 0;
235}
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250int ima_bprm_check(struct linux_binprm *bprm)
251{
252 int rc;
253
254 rc = process_measurement(bprm->file,
255 (strcmp(bprm->filename, bprm->interp) == 0) ?
256 bprm->filename : bprm->interp,
257 MAY_EXEC, BPRM_CHECK);
258 return (ima_appraise & IMA_APPRAISE_ENFORCE) ? rc : 0;
259}
260
261
262
263
264
265
266
267
268
269
270
271int ima_file_check(struct file *file, int mask)
272{
273 int rc;
274
275 ima_rdwr_violation_check(file);
276 rc = process_measurement(file, file->f_dentry->d_name.name,
277 mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
278 FILE_CHECK);
279 return (ima_appraise & IMA_APPRAISE_ENFORCE) ? rc : 0;
280}
281EXPORT_SYMBOL_GPL(ima_file_check);
282
283static int __init init_ima(void)
284{
285 int error;
286
287 error = ima_init();
288 if (!error)
289 ima_initialized = 1;
290 return error;
291}
292
293late_initcall(init_ima);
294
295MODULE_DESCRIPTION("Integrity Measurement Architecture");
296MODULE_LICENSE("GPL");
297