Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
fork
Configure Feed
Select the types of activity you want to include in your feed.
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
16 * implements the IMA hooks: ima_bprm_check, ima_file_mmap,
17 * and ima_file_check.
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/ima.h>
26
27#include "ima.h"
28
29int ima_initialized;
30
31char *ima_hash = "sha1";
32static int __init hash_setup(char *str)
33{
34 if (strncmp(str, "md5", 3) == 0)
35 ima_hash = "md5";
36 return 1;
37}
38__setup("ima_hash=", hash_setup);
39
40/*
41 * ima_rdwr_violation_check
42 *
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.
48 *
49 */
50static void ima_rdwr_violation_check(struct file *file)
51{
52 struct dentry *dentry = file->f_path.dentry;
53 struct inode *inode = dentry->d_inode;
54 fmode_t mode = file->f_mode;
55 int rc;
56 bool send_tomtou = false, send_writers = false;
57 unsigned char *pathname = NULL, *pathbuf = NULL;
58
59 if (!S_ISREG(inode->i_mode) || !ima_initialized)
60 return;
61
62 mutex_lock(&inode->i_mutex); /* file metadata: permissions, xattr */
63
64 if (mode & FMODE_WRITE) {
65 if (atomic_read(&inode->i_readcount) && IS_IMA(inode))
66 send_tomtou = true;
67 goto out;
68 }
69
70 rc = ima_must_measure(inode, MAY_READ, FILE_CHECK);
71 if (rc < 0)
72 goto out;
73
74 if (atomic_read(&inode->i_writecount) > 0)
75 send_writers = true;
76out:
77 mutex_unlock(&inode->i_mutex);
78
79 if (!send_tomtou && !send_writers)
80 return;
81
82 /* We will allow 11 spaces for ' (deleted)' to be appended */
83 pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL);
84 if (pathbuf) {
85 pathname = d_path(&file->f_path, pathbuf, PATH_MAX + 11);
86 if (IS_ERR(pathname))
87 pathname = NULL;
88 else if (strlen(pathname) > IMA_EVENT_NAME_LEN_MAX)
89 pathname = NULL;
90 }
91 if (send_tomtou)
92 ima_add_violation(inode,
93 !pathname ? dentry->d_name.name : pathname,
94 "invalid_pcr", "ToMToU");
95 if (send_writers)
96 ima_add_violation(inode,
97 !pathname ? dentry->d_name.name : pathname,
98 "invalid_pcr", "open_writers");
99 kfree(pathbuf);
100}
101
102static void ima_check_last_writer(struct integrity_iint_cache *iint,
103 struct inode *inode,
104 struct file *file)
105{
106 fmode_t mode = file->f_mode;
107
108 mutex_lock(&iint->mutex);
109 if (mode & FMODE_WRITE &&
110 atomic_read(&inode->i_writecount) == 1 &&
111 iint->version != inode->i_version)
112 iint->flags &= ~IMA_MEASURED;
113 mutex_unlock(&iint->mutex);
114}
115
116/**
117 * ima_file_free - called on __fput()
118 * @file: pointer to file structure being freed
119 *
120 * Flag files that changed, based on i_version
121 */
122void ima_file_free(struct file *file)
123{
124 struct inode *inode = file->f_dentry->d_inode;
125 struct integrity_iint_cache *iint;
126
127 if (!iint_initialized || !S_ISREG(inode->i_mode))
128 return;
129
130 iint = integrity_iint_find(inode);
131 if (!iint)
132 return;
133
134 ima_check_last_writer(iint, inode, file);
135}
136
137static int process_measurement(struct file *file, const unsigned char *filename,
138 int mask, int function)
139{
140 struct inode *inode = file->f_dentry->d_inode;
141 struct integrity_iint_cache *iint;
142 unsigned char *pathname = NULL, *pathbuf = NULL;
143 int rc = 0;
144
145 if (!ima_initialized || !S_ISREG(inode->i_mode))
146 return 0;
147
148 rc = ima_must_measure(inode, mask, function);
149 if (rc != 0)
150 return rc;
151retry:
152 iint = integrity_iint_find(inode);
153 if (!iint) {
154 rc = integrity_inode_alloc(inode);
155 if (!rc || rc == -EEXIST)
156 goto retry;
157 return rc;
158 }
159
160 mutex_lock(&iint->mutex);
161
162 rc = iint->flags & IMA_MEASURED ? 1 : 0;
163 if (rc != 0)
164 goto out;
165
166 rc = ima_collect_measurement(iint, file);
167 if (rc != 0)
168 goto out;
169
170 if (function != BPRM_CHECK) {
171 /* We will allow 11 spaces for ' (deleted)' to be appended */
172 pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL);
173 if (pathbuf) {
174 pathname =
175 d_path(&file->f_path, pathbuf, PATH_MAX + 11);
176 if (IS_ERR(pathname))
177 pathname = NULL;
178 }
179 }
180 ima_store_measurement(iint, file, !pathname ? filename : pathname);
181 kfree(pathbuf);
182out:
183 mutex_unlock(&iint->mutex);
184 return rc;
185}
186
187/**
188 * ima_file_mmap - based on policy, collect/store measurement.
189 * @file: pointer to the file to be measured (May be NULL)
190 * @prot: contains the protection that will be applied by the kernel.
191 *
192 * Measure files being mmapped executable based on the ima_must_measure()
193 * policy decision.
194 *
195 * Return 0 on success, an error code on failure.
196 * (Based on the results of appraise_measurement().)
197 */
198int ima_file_mmap(struct file *file, unsigned long prot)
199{
200 int rc;
201
202 if (!file)
203 return 0;
204 if (prot & PROT_EXEC)
205 rc = process_measurement(file, file->f_dentry->d_name.name,
206 MAY_EXEC, FILE_MMAP);
207 return 0;
208}
209
210/**
211 * ima_bprm_check - based on policy, collect/store measurement.
212 * @bprm: contains the linux_binprm structure
213 *
214 * The OS protects against an executable file, already open for write,
215 * from being executed in deny_write_access() and an executable file,
216 * already open for execute, from being modified in get_write_access().
217 * So we can be certain that what we verify and measure here is actually
218 * what is being executed.
219 *
220 * Return 0 on success, an error code on failure.
221 * (Based on the results of appraise_measurement().)
222 */
223int ima_bprm_check(struct linux_binprm *bprm)
224{
225 int rc;
226
227 rc = process_measurement(bprm->file,
228 (strcmp(bprm->filename, bprm->interp) == 0) ?
229 bprm->filename : bprm->interp,
230 MAY_EXEC, BPRM_CHECK);
231 return 0;
232}
233
234/**
235 * ima_path_check - based on policy, collect/store measurement.
236 * @file: pointer to the file to be measured
237 * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
238 *
239 * Measure files based on the ima_must_measure() policy decision.
240 *
241 * Always return 0 and audit dentry_open failures.
242 * (Return code will be based upon measurement appraisal.)
243 */
244int ima_file_check(struct file *file, int mask)
245{
246 int rc;
247
248 ima_rdwr_violation_check(file);
249 rc = process_measurement(file, file->f_dentry->d_name.name,
250 mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
251 FILE_CHECK);
252 return 0;
253}
254EXPORT_SYMBOL_GPL(ima_file_check);
255
256static int __init init_ima(void)
257{
258 int error;
259
260 error = ima_init();
261 if (!error)
262 ima_initialized = 1;
263 return error;
264}
265
266late_initcall(init_ima); /* Start IMA after the TPM is available */
267
268MODULE_DESCRIPTION("Integrity Measurement Architecture");
269MODULE_LICENSE("GPL");