blob: be8294915cf7f65524a21ab2dd3101ecb0a6edd9 [file] [log] [blame]
Mimi Zohar3323eec92009-02-04 09:06:58 -05001/*
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
Eric Parise0d5bd22009-12-04 15:48:00 -050016 * implements the IMA hooks: ima_bprm_check, ima_file_mmap,
Mimi Zohar9bbb6ca2010-01-26 17:02:40 -050017 * and ima_file_check.
Mimi Zohar3323eec92009-02-04 09:06:58 -050018 */
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
James Morrisd5813a52011-08-30 10:19:50 +100025#include <linux/ima.h>
Mimi Zohar3323eec92009-02-04 09:06:58 -050026
27#include "ima.h"
28
29int ima_initialized;
30
31char *ima_hash = "sha1";
32static int __init hash_setup(char *str)
33{
Mimi Zohar07ff7a02009-05-05 13:13:10 -040034 if (strncmp(str, "md5", 3) == 0)
35 ima_hash = "md5";
Mimi Zohar3323eec92009-02-04 09:06:58 -050036 return 1;
37}
38__setup("ima_hash=", hash_setup);
39
Eric Parise0d5bd22009-12-04 15:48:00 -050040/*
Mimi Zohar890275b52010-11-02 10:13:07 -040041 * ima_rdwr_violation_check
Mimi Zohar8eb988c2010-01-20 15:35:41 -050042 *
Mimi Zohar890275b52010-11-02 10:13:07 -040043 * Only invalidate the PCR for measured files:
Mimi Zohar8eb988c2010-01-20 15:35:41 -050044 * - 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 */
Mimi Zohar890275b52010-11-02 10:13:07 -040050static void ima_rdwr_violation_check(struct file *file)
Mimi Zohar8eb988c2010-01-20 15:35:41 -050051{
52 struct dentry *dentry = file->f_path.dentry;
53 struct inode *inode = dentry->d_inode;
54 fmode_t mode = file->f_mode;
Mimi Zohar8eb988c2010-01-20 15:35:41 -050055 int rc;
Eric Parisad16ad02010-10-25 14:41:45 -040056 bool send_tomtou = false, send_writers = false;
Mimi Zohar08e1b762012-06-20 09:32:55 -040057 unsigned char *pathname = NULL, *pathbuf = NULL;
Mimi Zohar8eb988c2010-01-20 15:35:41 -050058
Mimi Zohar890275b52010-11-02 10:13:07 -040059 if (!S_ISREG(inode->i_mode) || !ima_initialized)
Mimi Zohar8eb988c2010-01-20 15:35:41 -050060 return;
Eric Parisa178d202010-10-25 14:41:59 -040061
Mimi Zohar890275b52010-11-02 10:13:07 -040062 mutex_lock(&inode->i_mutex); /* file metadata: permissions, xattr */
Eric Parisad16ad02010-10-25 14:41:45 -040063
Mimi Zohar8eb988c2010-01-20 15:35:41 -050064 if (mode & FMODE_WRITE) {
Mimi Zohara68a27b2010-11-02 10:10:56 -040065 if (atomic_read(&inode->i_readcount) && IS_IMA(inode))
Eric Parisad16ad02010-10-25 14:41:45 -040066 send_tomtou = true;
Mimi Zohar8eb988c2010-01-20 15:35:41 -050067 goto out;
68 }
Eric Parisad16ad02010-10-25 14:41:45 -040069
Mimi Zohar1adace92011-02-22 10:19:43 -050070 rc = ima_must_measure(inode, MAY_READ, FILE_CHECK);
Eric Parisbade72d2010-10-25 14:42:25 -040071 if (rc < 0)
72 goto out;
73
Eric Parisad16ad02010-10-25 14:41:45 -040074 if (atomic_read(&inode->i_writecount) > 0)
75 send_writers = true;
Mimi Zohar8eb988c2010-01-20 15:35:41 -050076out:
Mimi Zohar890275b52010-11-02 10:13:07 -040077 mutex_unlock(&inode->i_mutex);
Eric Parisad16ad02010-10-25 14:41:45 -040078
Mimi Zohar08e1b762012-06-20 09:32:55 -040079 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 }
Eric Parisad16ad02010-10-25 14:41:45 -040091 if (send_tomtou)
Mimi Zohar08e1b762012-06-20 09:32:55 -040092 ima_add_violation(inode,
93 !pathname ? dentry->d_name.name : pathname,
94 "invalid_pcr", "ToMToU");
Eric Parisad16ad02010-10-25 14:41:45 -040095 if (send_writers)
Mimi Zohar08e1b762012-06-20 09:32:55 -040096 ima_add_violation(inode,
97 !pathname ? dentry->d_name.name : pathname,
98 "invalid_pcr", "open_writers");
99 kfree(pathbuf);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500100}
101
Mimi Zoharf381c272011-03-09 14:13:22 -0500102static void ima_check_last_writer(struct integrity_iint_cache *iint,
Eric Parisbc7d2a32010-10-25 14:42:05 -0400103 struct inode *inode,
104 struct file *file)
105{
Al Viro4b2a2c62011-07-26 04:30:35 -0400106 fmode_t mode = file->f_mode;
Eric Paris497f3232010-10-25 14:41:32 -0400107
Mimi Zohar854fdd52010-11-02 10:14:22 -0400108 mutex_lock(&iint->mutex);
Eric Parisbc7d2a32010-10-25 14:42:05 -0400109 if (mode & FMODE_WRITE &&
110 atomic_read(&inode->i_writecount) == 1 &&
111 iint->version != inode->i_version)
112 iint->flags &= ~IMA_MEASURED;
Eric Parisbc7d2a32010-10-25 14:42:05 -0400113 mutex_unlock(&iint->mutex);
Eric Parisbc7d2a32010-10-25 14:42:05 -0400114}
115
Mimi Zohar3323eec92009-02-04 09:06:58 -0500116/**
117 * ima_file_free - called on __fput()
118 * @file: pointer to file structure being freed
119 *
Mimi Zohar890275b52010-11-02 10:13:07 -0400120 * Flag files that changed, based on i_version
Mimi Zohar3323eec92009-02-04 09:06:58 -0500121 */
122void ima_file_free(struct file *file)
123{
124 struct inode *inode = file->f_dentry->d_inode;
Mimi Zoharf381c272011-03-09 14:13:22 -0500125 struct integrity_iint_cache *iint;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500126
Mimi Zohare9505982010-08-31 09:38:51 -0400127 if (!iint_initialized || !S_ISREG(inode->i_mode))
Mimi Zohar3323eec92009-02-04 09:06:58 -0500128 return;
Eric Paris196f5182010-10-25 14:42:19 -0400129
Mimi Zoharf381c272011-03-09 14:13:22 -0500130 iint = integrity_iint_find(inode);
Mimi Zohar854fdd52010-11-02 10:14:22 -0400131 if (!iint)
132 return;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500133
Mimi Zohar854fdd52010-11-02 10:14:22 -0400134 ima_check_last_writer(iint, inode, file);
Mimi Zohar3323eec92009-02-04 09:06:58 -0500135}
136
Mimi Zohar3323eec92009-02-04 09:06:58 -0500137static 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;
Mimi Zoharf381c272011-03-09 14:13:22 -0500141 struct integrity_iint_cache *iint;
Mimi Zohar08e1b762012-06-20 09:32:55 -0400142 unsigned char *pathname = NULL, *pathbuf = NULL;
Mimi Zohare9505982010-08-31 09:38:51 -0400143 int rc = 0;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500144
145 if (!ima_initialized || !S_ISREG(inode->i_mode))
146 return 0;
Eric Parisbc7d2a32010-10-25 14:42:05 -0400147
Mimi Zohar1adace92011-02-22 10:19:43 -0500148 rc = ima_must_measure(inode, mask, function);
Eric Parisbc7d2a32010-10-25 14:42:05 -0400149 if (rc != 0)
150 return rc;
151retry:
Mimi Zoharf381c272011-03-09 14:13:22 -0500152 iint = integrity_iint_find(inode);
Eric Parisbc7d2a32010-10-25 14:42:05 -0400153 if (!iint) {
Mimi Zoharf381c272011-03-09 14:13:22 -0500154 rc = integrity_inode_alloc(inode);
Eric Parisbc7d2a32010-10-25 14:42:05 -0400155 if (!rc || rc == -EEXIST)
156 goto retry;
157 return rc;
158 }
Mimi Zohar3323eec92009-02-04 09:06:58 -0500159
160 mutex_lock(&iint->mutex);
Eric Parisbc7d2a32010-10-25 14:42:05 -0400161
Mimi Zohar1adace92011-02-22 10:19:43 -0500162 rc = iint->flags & IMA_MEASURED ? 1 : 0;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500163 if (rc != 0)
164 goto out;
165
166 rc = ima_collect_measurement(iint, file);
Mimi Zohar08e1b762012-06-20 09:32:55 -0400167 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);
Mimi Zohar3323eec92009-02-04 09:06:58 -0500182out:
183 mutex_unlock(&iint->mutex);
Mimi Zohar3323eec92009-02-04 09:06:58 -0500184 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
Mimi Zoharfbbb4562012-05-14 21:50:11 -0400227 rc = process_measurement(bprm->file,
228 (strcmp(bprm->filename, bprm->interp) == 0) ?
229 bprm->filename : bprm->interp,
Mimi Zohar3323eec92009-02-04 09:06:58 -0500230 MAY_EXEC, BPRM_CHECK);
231 return 0;
232}
233
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500234/**
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 */
Mimi Zohar9bbb6ca2010-01-26 17:02:40 -0500244int ima_file_check(struct file *file, int mask)
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500245{
246 int rc;
247
Mimi Zohar890275b52010-11-02 10:13:07 -0400248 ima_rdwr_violation_check(file);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500249 rc = process_measurement(file, file->f_dentry->d_name.name,
250 mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
Mimi Zohar1e93d002010-01-26 17:02:41 -0500251 FILE_CHECK);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500252 return 0;
253}
Mimi Zohar9bbb6ca2010-01-26 17:02:40 -0500254EXPORT_SYMBOL_GPL(ima_file_check);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500255
Mimi Zohar3323eec92009-02-04 09:06:58 -0500256static int __init init_ima(void)
257{
258 int error;
259
Mimi Zohar3323eec92009-02-04 09:06:58 -0500260 error = ima_init();
Dmitry Kasatkin7ff22672012-06-25 12:18:11 +0300261 if (!error)
262 ima_initialized = 1;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500263 return error;
264}
265
Mimi Zohar3323eec92009-02-04 09:06:58 -0500266late_initcall(init_ima); /* Start IMA after the TPM is available */
267
268MODULE_DESCRIPTION("Integrity Measurement Architecture");
269MODULE_LICENSE("GPL");