blob: 39d66dc2b8e98e221d73327f82c1a07599bb297b [file] [log] [blame]
Mimi Zohar3323eec2009-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 Zohar3323eec2009-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>
Mimi Zohar3323eec2009-02-04 09:06:58 -050025
26#include "ima.h"
27
28int ima_initialized;
29
30char *ima_hash = "sha1";
31static int __init hash_setup(char *str)
32{
Mimi Zohar07ff7a02009-05-05 13:13:10 -040033 if (strncmp(str, "md5", 3) == 0)
34 ima_hash = "md5";
Mimi Zohar3323eec2009-02-04 09:06:58 -050035 return 1;
36}
37__setup("ima_hash=", hash_setup);
38
Eric Parise0d5bd22009-12-04 15:48:00 -050039/*
Mimi Zohar890275b2010-11-02 10:13:07 -040040 * ima_rdwr_violation_check
Mimi Zohar8eb988c2010-01-20 15:35:41 -050041 *
Mimi Zohar890275b2010-11-02 10:13:07 -040042 * Only invalidate the PCR for measured files:
Mimi Zohar8eb988c2010-01-20 15:35:41 -050043 * - Opening a file for write when already open for read,
44 * results in a time of measure, time of use (ToMToU) error.
45 * - Opening a file for read when already open for write,
46 * could result in a file measurement error.
47 *
48 */
Mimi Zohar890275b2010-11-02 10:13:07 -040049static void ima_rdwr_violation_check(struct file *file)
Mimi Zohar8eb988c2010-01-20 15:35:41 -050050{
51 struct dentry *dentry = file->f_path.dentry;
52 struct inode *inode = dentry->d_inode;
53 fmode_t mode = file->f_mode;
Mimi Zohar8eb988c2010-01-20 15:35:41 -050054 int rc;
Eric Parisad16ad02010-10-25 14:41:45 -040055 bool send_tomtou = false, send_writers = false;
Mimi Zohar8eb988c2010-01-20 15:35:41 -050056
Mimi Zohar890275b2010-11-02 10:13:07 -040057 if (!S_ISREG(inode->i_mode) || !ima_initialized)
Mimi Zohar8eb988c2010-01-20 15:35:41 -050058 return;
Eric Parisa178d202010-10-25 14:41:59 -040059
Mimi Zohar890275b2010-11-02 10:13:07 -040060 mutex_lock(&inode->i_mutex); /* file metadata: permissions, xattr */
Eric Parisad16ad02010-10-25 14:41:45 -040061
Mimi Zohar8eb988c2010-01-20 15:35:41 -050062 if (mode & FMODE_WRITE) {
Mimi Zohara68a27b2010-11-02 10:10:56 -040063 if (atomic_read(&inode->i_readcount) && IS_IMA(inode))
Eric Parisad16ad02010-10-25 14:41:45 -040064 send_tomtou = true;
Mimi Zohar8eb988c2010-01-20 15:35:41 -050065 goto out;
66 }
Eric Parisad16ad02010-10-25 14:41:45 -040067
Mimi Zohar1adace92011-02-22 10:19:43 -050068 rc = ima_must_measure(inode, MAY_READ, FILE_CHECK);
Eric Parisbade72d2010-10-25 14:42:25 -040069 if (rc < 0)
70 goto out;
71
Eric Parisad16ad02010-10-25 14:41:45 -040072 if (atomic_read(&inode->i_writecount) > 0)
73 send_writers = true;
Mimi Zohar8eb988c2010-01-20 15:35:41 -050074out:
Mimi Zohar890275b2010-11-02 10:13:07 -040075 mutex_unlock(&inode->i_mutex);
Eric Parisad16ad02010-10-25 14:41:45 -040076
77 if (send_tomtou)
78 ima_add_violation(inode, dentry->d_name.name, "invalid_pcr",
79 "ToMToU");
80 if (send_writers)
81 ima_add_violation(inode, dentry->d_name.name, "invalid_pcr",
82 "open_writers");
Mimi Zohar8eb988c2010-01-20 15:35:41 -050083}
84
Eric Parisbc7d2a32010-10-25 14:42:05 -040085static void ima_check_last_writer(struct ima_iint_cache *iint,
86 struct inode *inode,
87 struct file *file)
88{
89 mode_t mode = file->f_mode;
Eric Paris497f3232010-10-25 14:41:32 -040090
Mimi Zohar854fdd52010-11-02 10:14:22 -040091 mutex_lock(&iint->mutex);
Eric Parisbc7d2a32010-10-25 14:42:05 -040092 if (mode & FMODE_WRITE &&
93 atomic_read(&inode->i_writecount) == 1 &&
94 iint->version != inode->i_version)
95 iint->flags &= ~IMA_MEASURED;
Eric Parisbc7d2a32010-10-25 14:42:05 -040096 mutex_unlock(&iint->mutex);
Eric Parisbc7d2a32010-10-25 14:42:05 -040097}
98
Mimi Zohar3323eec2009-02-04 09:06:58 -050099/**
100 * ima_file_free - called on __fput()
101 * @file: pointer to file structure being freed
102 *
Mimi Zohar890275b2010-11-02 10:13:07 -0400103 * Flag files that changed, based on i_version
Mimi Zohar3323eec2009-02-04 09:06:58 -0500104 */
105void ima_file_free(struct file *file)
106{
107 struct inode *inode = file->f_dentry->d_inode;
108 struct ima_iint_cache *iint;
109
Mimi Zohare9505982010-08-31 09:38:51 -0400110 if (!iint_initialized || !S_ISREG(inode->i_mode))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500111 return;
Eric Paris196f5182010-10-25 14:42:19 -0400112
Eric Paris64c62f02010-10-25 14:42:12 -0400113 iint = ima_iint_find(inode);
Mimi Zohar854fdd52010-11-02 10:14:22 -0400114 if (!iint)
115 return;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500116
Mimi Zohar854fdd52010-11-02 10:14:22 -0400117 ima_check_last_writer(iint, inode, file);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500118}
119
Mimi Zohar3323eec2009-02-04 09:06:58 -0500120static int process_measurement(struct file *file, const unsigned char *filename,
121 int mask, int function)
122{
123 struct inode *inode = file->f_dentry->d_inode;
124 struct ima_iint_cache *iint;
Mimi Zohare9505982010-08-31 09:38:51 -0400125 int rc = 0;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500126
127 if (!ima_initialized || !S_ISREG(inode->i_mode))
128 return 0;
Eric Parisbc7d2a32010-10-25 14:42:05 -0400129
Mimi Zohar1adace92011-02-22 10:19:43 -0500130 rc = ima_must_measure(inode, mask, function);
Eric Parisbc7d2a32010-10-25 14:42:05 -0400131 if (rc != 0)
132 return rc;
133retry:
Eric Paris64c62f02010-10-25 14:42:12 -0400134 iint = ima_iint_find(inode);
Eric Parisbc7d2a32010-10-25 14:42:05 -0400135 if (!iint) {
136 rc = ima_inode_alloc(inode);
137 if (!rc || rc == -EEXIST)
138 goto retry;
139 return rc;
140 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500141
142 mutex_lock(&iint->mutex);
Eric Parisbc7d2a32010-10-25 14:42:05 -0400143
Mimi Zohar1adace92011-02-22 10:19:43 -0500144 rc = iint->flags & IMA_MEASURED ? 1 : 0;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500145 if (rc != 0)
146 goto out;
147
148 rc = ima_collect_measurement(iint, file);
149 if (!rc)
150 ima_store_measurement(iint, file, filename);
151out:
152 mutex_unlock(&iint->mutex);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500153 return rc;
154}
155
156/**
157 * ima_file_mmap - based on policy, collect/store measurement.
158 * @file: pointer to the file to be measured (May be NULL)
159 * @prot: contains the protection that will be applied by the kernel.
160 *
161 * Measure files being mmapped executable based on the ima_must_measure()
162 * policy decision.
163 *
164 * Return 0 on success, an error code on failure.
165 * (Based on the results of appraise_measurement().)
166 */
167int ima_file_mmap(struct file *file, unsigned long prot)
168{
169 int rc;
170
171 if (!file)
172 return 0;
173 if (prot & PROT_EXEC)
174 rc = process_measurement(file, file->f_dentry->d_name.name,
175 MAY_EXEC, FILE_MMAP);
176 return 0;
177}
178
179/**
180 * ima_bprm_check - based on policy, collect/store measurement.
181 * @bprm: contains the linux_binprm structure
182 *
183 * The OS protects against an executable file, already open for write,
184 * from being executed in deny_write_access() and an executable file,
185 * already open for execute, from being modified in get_write_access().
186 * So we can be certain that what we verify and measure here is actually
187 * what is being executed.
188 *
189 * Return 0 on success, an error code on failure.
190 * (Based on the results of appraise_measurement().)
191 */
192int ima_bprm_check(struct linux_binprm *bprm)
193{
194 int rc;
195
196 rc = process_measurement(bprm->file, bprm->filename,
197 MAY_EXEC, BPRM_CHECK);
198 return 0;
199}
200
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500201/**
202 * ima_path_check - based on policy, collect/store measurement.
203 * @file: pointer to the file to be measured
204 * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
205 *
206 * Measure files based on the ima_must_measure() policy decision.
207 *
208 * Always return 0 and audit dentry_open failures.
209 * (Return code will be based upon measurement appraisal.)
210 */
Mimi Zohar9bbb6ca2010-01-26 17:02:40 -0500211int ima_file_check(struct file *file, int mask)
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500212{
213 int rc;
214
Mimi Zohar890275b2010-11-02 10:13:07 -0400215 ima_rdwr_violation_check(file);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500216 rc = process_measurement(file, file->f_dentry->d_name.name,
217 mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
Mimi Zohar1e93d002010-01-26 17:02:41 -0500218 FILE_CHECK);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500219 return 0;
220}
Mimi Zohar9bbb6ca2010-01-26 17:02:40 -0500221EXPORT_SYMBOL_GPL(ima_file_check);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500222
Mimi Zohar3323eec2009-02-04 09:06:58 -0500223static int __init init_ima(void)
224{
225 int error;
226
Mimi Zohar3323eec2009-02-04 09:06:58 -0500227 error = ima_init();
228 ima_initialized = 1;
229 return error;
230}
231
Mimi Zoharbab73932009-02-04 09:06:59 -0500232static void __exit cleanup_ima(void)
233{
234 ima_cleanup();
235}
236
Mimi Zohar3323eec2009-02-04 09:06:58 -0500237late_initcall(init_ima); /* Start IMA after the TPM is available */
238
239MODULE_DESCRIPTION("Integrity Measurement Architecture");
240MODULE_LICENSE("GPL");