blob: b17be79b9cf24b8f2319ea2d54875b0a048dcfdf [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 Zohar8eb988c2010-01-20 15:35:41 -050057
Mimi Zohar890275b52010-11-02 10:13:07 -040058 if (!S_ISREG(inode->i_mode) || !ima_initialized)
Mimi Zohar8eb988c2010-01-20 15:35:41 -050059 return;
Eric Parisa178d202010-10-25 14:41:59 -040060
Mimi Zohar890275b52010-11-02 10:13:07 -040061 mutex_lock(&inode->i_mutex); /* file metadata: permissions, xattr */
Eric Parisad16ad02010-10-25 14:41:45 -040062
Mimi Zohar8eb988c2010-01-20 15:35:41 -050063 if (mode & FMODE_WRITE) {
Mimi Zohara68a27b2010-11-02 10:10:56 -040064 if (atomic_read(&inode->i_readcount) && IS_IMA(inode))
Eric Parisad16ad02010-10-25 14:41:45 -040065 send_tomtou = true;
Mimi Zohar8eb988c2010-01-20 15:35:41 -050066 goto out;
67 }
Eric Parisad16ad02010-10-25 14:41:45 -040068
Mimi Zohar1adace92011-02-22 10:19:43 -050069 rc = ima_must_measure(inode, MAY_READ, FILE_CHECK);
Eric Parisbade72d2010-10-25 14:42:25 -040070 if (rc < 0)
71 goto out;
72
Eric Parisad16ad02010-10-25 14:41:45 -040073 if (atomic_read(&inode->i_writecount) > 0)
74 send_writers = true;
Mimi Zohar8eb988c2010-01-20 15:35:41 -050075out:
Mimi Zohar890275b52010-11-02 10:13:07 -040076 mutex_unlock(&inode->i_mutex);
Eric Parisad16ad02010-10-25 14:41:45 -040077
78 if (send_tomtou)
79 ima_add_violation(inode, dentry->d_name.name, "invalid_pcr",
80 "ToMToU");
81 if (send_writers)
82 ima_add_violation(inode, dentry->d_name.name, "invalid_pcr",
83 "open_writers");
Mimi Zohar8eb988c2010-01-20 15:35:41 -050084}
85
Mimi Zoharf381c272011-03-09 14:13:22 -050086static void ima_check_last_writer(struct integrity_iint_cache *iint,
Eric Parisbc7d2a32010-10-25 14:42:05 -040087 struct inode *inode,
88 struct file *file)
89{
Al Viro4b2a2c62011-07-26 04:30:35 -040090 fmode_t mode = file->f_mode;
Eric Paris497f3232010-10-25 14:41:32 -040091
Mimi Zohar854fdd52010-11-02 10:14:22 -040092 mutex_lock(&iint->mutex);
Eric Parisbc7d2a32010-10-25 14:42:05 -040093 if (mode & FMODE_WRITE &&
94 atomic_read(&inode->i_writecount) == 1 &&
95 iint->version != inode->i_version)
96 iint->flags &= ~IMA_MEASURED;
Eric Parisbc7d2a32010-10-25 14:42:05 -040097 mutex_unlock(&iint->mutex);
Eric Parisbc7d2a32010-10-25 14:42:05 -040098}
99
Mimi Zohar3323eec92009-02-04 09:06:58 -0500100/**
101 * ima_file_free - called on __fput()
102 * @file: pointer to file structure being freed
103 *
Mimi Zohar890275b52010-11-02 10:13:07 -0400104 * Flag files that changed, based on i_version
Mimi Zohar3323eec92009-02-04 09:06:58 -0500105 */
106void ima_file_free(struct file *file)
107{
108 struct inode *inode = file->f_dentry->d_inode;
Mimi Zoharf381c272011-03-09 14:13:22 -0500109 struct integrity_iint_cache *iint;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500110
Mimi Zohare9505982010-08-31 09:38:51 -0400111 if (!iint_initialized || !S_ISREG(inode->i_mode))
Mimi Zohar3323eec92009-02-04 09:06:58 -0500112 return;
Eric Paris196f5182010-10-25 14:42:19 -0400113
Mimi Zoharf381c272011-03-09 14:13:22 -0500114 iint = integrity_iint_find(inode);
Mimi Zohar854fdd52010-11-02 10:14:22 -0400115 if (!iint)
116 return;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500117
Mimi Zohar854fdd52010-11-02 10:14:22 -0400118 ima_check_last_writer(iint, inode, file);
Mimi Zohar3323eec92009-02-04 09:06:58 -0500119}
120
Mimi Zohar3323eec92009-02-04 09:06:58 -0500121static int process_measurement(struct file *file, const unsigned char *filename,
122 int mask, int function)
123{
124 struct inode *inode = file->f_dentry->d_inode;
Mimi Zoharf381c272011-03-09 14:13:22 -0500125 struct integrity_iint_cache *iint;
Mimi Zohare9505982010-08-31 09:38:51 -0400126 int rc = 0;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500127
128 if (!ima_initialized || !S_ISREG(inode->i_mode))
129 return 0;
Eric Parisbc7d2a32010-10-25 14:42:05 -0400130
Mimi Zohar1adace92011-02-22 10:19:43 -0500131 rc = ima_must_measure(inode, mask, function);
Eric Parisbc7d2a32010-10-25 14:42:05 -0400132 if (rc != 0)
133 return rc;
134retry:
Mimi Zoharf381c272011-03-09 14:13:22 -0500135 iint = integrity_iint_find(inode);
Eric Parisbc7d2a32010-10-25 14:42:05 -0400136 if (!iint) {
Mimi Zoharf381c272011-03-09 14:13:22 -0500137 rc = integrity_inode_alloc(inode);
Eric Parisbc7d2a32010-10-25 14:42:05 -0400138 if (!rc || rc == -EEXIST)
139 goto retry;
140 return rc;
141 }
Mimi Zohar3323eec92009-02-04 09:06:58 -0500142
143 mutex_lock(&iint->mutex);
Eric Parisbc7d2a32010-10-25 14:42:05 -0400144
Mimi Zohar1adace92011-02-22 10:19:43 -0500145 rc = iint->flags & IMA_MEASURED ? 1 : 0;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500146 if (rc != 0)
147 goto out;
148
149 rc = ima_collect_measurement(iint, file);
150 if (!rc)
151 ima_store_measurement(iint, file, filename);
152out:
153 mutex_unlock(&iint->mutex);
Mimi Zohar3323eec92009-02-04 09:06:58 -0500154 return rc;
155}
156
157/**
158 * ima_file_mmap - based on policy, collect/store measurement.
159 * @file: pointer to the file to be measured (May be NULL)
160 * @prot: contains the protection that will be applied by the kernel.
161 *
162 * Measure files being mmapped executable based on the ima_must_measure()
163 * policy decision.
164 *
165 * Return 0 on success, an error code on failure.
166 * (Based on the results of appraise_measurement().)
167 */
168int ima_file_mmap(struct file *file, unsigned long prot)
169{
170 int rc;
171
172 if (!file)
173 return 0;
174 if (prot & PROT_EXEC)
175 rc = process_measurement(file, file->f_dentry->d_name.name,
176 MAY_EXEC, FILE_MMAP);
177 return 0;
178}
179
180/**
181 * ima_bprm_check - based on policy, collect/store measurement.
182 * @bprm: contains the linux_binprm structure
183 *
184 * The OS protects against an executable file, already open for write,
185 * from being executed in deny_write_access() and an executable file,
186 * already open for execute, from being modified in get_write_access().
187 * So we can be certain that what we verify and measure here is actually
188 * what is being executed.
189 *
190 * Return 0 on success, an error code on failure.
191 * (Based on the results of appraise_measurement().)
192 */
193int ima_bprm_check(struct linux_binprm *bprm)
194{
195 int rc;
196
Mimi Zoharfbbb4562012-05-14 21:50:11 -0400197 rc = process_measurement(bprm->file,
198 (strcmp(bprm->filename, bprm->interp) == 0) ?
199 bprm->filename : bprm->interp,
Mimi Zohar3323eec92009-02-04 09:06:58 -0500200 MAY_EXEC, BPRM_CHECK);
201 return 0;
202}
203
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500204/**
205 * ima_path_check - based on policy, collect/store measurement.
206 * @file: pointer to the file to be measured
207 * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
208 *
209 * Measure files based on the ima_must_measure() policy decision.
210 *
211 * Always return 0 and audit dentry_open failures.
212 * (Return code will be based upon measurement appraisal.)
213 */
Mimi Zohar9bbb6ca2010-01-26 17:02:40 -0500214int ima_file_check(struct file *file, int mask)
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500215{
216 int rc;
217
Mimi Zohar890275b52010-11-02 10:13:07 -0400218 ima_rdwr_violation_check(file);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500219 rc = process_measurement(file, file->f_dentry->d_name.name,
220 mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
Mimi Zohar1e93d002010-01-26 17:02:41 -0500221 FILE_CHECK);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500222 return 0;
223}
Mimi Zohar9bbb6ca2010-01-26 17:02:40 -0500224EXPORT_SYMBOL_GPL(ima_file_check);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500225
Mimi Zohar3323eec92009-02-04 09:06:58 -0500226static int __init init_ima(void)
227{
228 int error;
229
Mimi Zohar3323eec92009-02-04 09:06:58 -0500230 error = ima_init();
231 ima_initialized = 1;
232 return error;
233}
234
Mimi Zoharbab73932009-02-04 09:06:59 -0500235static void __exit cleanup_ima(void)
236{
237 ima_cleanup();
238}
239
Mimi Zohar3323eec92009-02-04 09:06:58 -0500240late_initcall(init_ima); /* Start IMA after the TPM is available */
241
242MODULE_DESCRIPTION("Integrity Measurement Architecture");
243MODULE_LICENSE("GPL");