blob: dba965de90d3f838c82e1a93202bf585fa1e5cd6 [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 Zohar2fe5d6d2012-02-13 10:15:05 -050025#include <linux/xattr.h>
James Morrisd5813a52011-08-30 10:19:50 +100026#include <linux/ima.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050027
28#include "ima.h"
29
30int ima_initialized;
31
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050032#ifdef CONFIG_IMA_APPRAISE
33int ima_appraise = IMA_APPRAISE_ENFORCE;
34#else
35int ima_appraise;
36#endif
37
Mimi Zohar3323eec2009-02-04 09:06:58 -050038char *ima_hash = "sha1";
39static int __init hash_setup(char *str)
40{
Mimi Zohar07ff7a02009-05-05 13:13:10 -040041 if (strncmp(str, "md5", 3) == 0)
42 ima_hash = "md5";
Mimi Zohar3323eec2009-02-04 09:06:58 -050043 return 1;
44}
45__setup("ima_hash=", hash_setup);
46
Eric Parise0d5bd22009-12-04 15:48:00 -050047/*
Mimi Zohar890275b52010-11-02 10:13:07 -040048 * ima_rdwr_violation_check
Mimi Zohar8eb988c2010-01-20 15:35:41 -050049 *
Mimi Zohar890275b52010-11-02 10:13:07 -040050 * Only invalidate the PCR for measured files:
Mimi Zohar8eb988c2010-01-20 15:35:41 -050051 * - Opening a file for write when already open for read,
52 * results in a time of measure, time of use (ToMToU) error.
53 * - Opening a file for read when already open for write,
54 * could result in a file measurement error.
55 *
56 */
Mimi Zohar890275b52010-11-02 10:13:07 -040057static void ima_rdwr_violation_check(struct file *file)
Mimi Zohar8eb988c2010-01-20 15:35:41 -050058{
59 struct dentry *dentry = file->f_path.dentry;
60 struct inode *inode = dentry->d_inode;
61 fmode_t mode = file->f_mode;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050062 int must_measure;
Eric Parisad16ad02010-10-25 14:41:45 -040063 bool send_tomtou = false, send_writers = false;
Mimi Zohar08e1b762012-06-20 09:32:55 -040064 unsigned char *pathname = NULL, *pathbuf = NULL;
Mimi Zohar8eb988c2010-01-20 15:35:41 -050065
Mimi Zohar890275b52010-11-02 10:13:07 -040066 if (!S_ISREG(inode->i_mode) || !ima_initialized)
Mimi Zohar8eb988c2010-01-20 15:35:41 -050067 return;
Eric Parisa178d202010-10-25 14:41:59 -040068
Mimi Zohar890275b52010-11-02 10:13:07 -040069 mutex_lock(&inode->i_mutex); /* file metadata: permissions, xattr */
Eric Parisad16ad02010-10-25 14:41:45 -040070
Mimi Zohar8eb988c2010-01-20 15:35:41 -050071 if (mode & FMODE_WRITE) {
Mimi Zohara68a27b2010-11-02 10:10:56 -040072 if (atomic_read(&inode->i_readcount) && IS_IMA(inode))
Eric Parisad16ad02010-10-25 14:41:45 -040073 send_tomtou = true;
Mimi Zohar8eb988c2010-01-20 15:35:41 -050074 goto out;
75 }
Eric Parisad16ad02010-10-25 14:41:45 -040076
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050077 must_measure = ima_must_measure(inode, MAY_READ, FILE_CHECK);
78 if (!must_measure)
Eric Parisbade72d2010-10-25 14:42:25 -040079 goto out;
80
Eric Parisad16ad02010-10-25 14:41:45 -040081 if (atomic_read(&inode->i_writecount) > 0)
82 send_writers = true;
Mimi Zohar8eb988c2010-01-20 15:35:41 -050083out:
Mimi Zohar890275b52010-11-02 10:13:07 -040084 mutex_unlock(&inode->i_mutex);
Eric Parisad16ad02010-10-25 14:41:45 -040085
Mimi Zohar08e1b762012-06-20 09:32:55 -040086 if (!send_tomtou && !send_writers)
87 return;
88
89 /* We will allow 11 spaces for ' (deleted)' to be appended */
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 }
Eric Parisad16ad02010-10-25 14:41:45 -040098 if (send_tomtou)
Mimi Zohar08e1b762012-06-20 09:32:55 -040099 ima_add_violation(inode,
100 !pathname ? dentry->d_name.name : pathname,
101 "invalid_pcr", "ToMToU");
Eric Parisad16ad02010-10-25 14:41:45 -0400102 if (send_writers)
Mimi Zohar08e1b762012-06-20 09:32:55 -0400103 ima_add_violation(inode,
104 !pathname ? dentry->d_name.name : pathname,
105 "invalid_pcr", "open_writers");
106 kfree(pathbuf);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500107}
108
Mimi Zoharf381c272011-03-09 14:13:22 -0500109static void ima_check_last_writer(struct integrity_iint_cache *iint,
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500110 struct inode *inode, struct file *file)
Eric Parisbc7d2a32010-10-25 14:42:05 -0400111{
Al Viro4b2a2c62011-07-26 04:30:35 -0400112 fmode_t mode = file->f_mode;
Eric Paris497f3232010-10-25 14:41:32 -0400113
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500114 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) {
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300120 iint->flags &= ~IMA_DONE_MASK;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500121 if (iint->flags & IMA_APPRAISE)
122 ima_update_xattr(iint, file);
123 }
124 mutex_unlock(&inode->i_mutex);
Eric Parisbc7d2a32010-10-25 14:42:05 -0400125}
126
Mimi Zohar3323eec2009-02-04 09:06:58 -0500127/**
128 * ima_file_free - called on __fput()
129 * @file: pointer to file structure being freed
130 *
Mimi Zohar890275b52010-11-02 10:13:07 -0400131 * Flag files that changed, based on i_version
Mimi Zohar3323eec2009-02-04 09:06:58 -0500132 */
133void ima_file_free(struct file *file)
134{
135 struct inode *inode = file->f_dentry->d_inode;
Mimi Zoharf381c272011-03-09 14:13:22 -0500136 struct integrity_iint_cache *iint;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500137
Mimi Zohare9505982010-08-31 09:38:51 -0400138 if (!iint_initialized || !S_ISREG(inode->i_mode))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500139 return;
Eric Paris196f5182010-10-25 14:42:19 -0400140
Mimi Zoharf381c272011-03-09 14:13:22 -0500141 iint = integrity_iint_find(inode);
Mimi Zohar854fdd52010-11-02 10:14:22 -0400142 if (!iint)
143 return;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500144
Mimi Zohar854fdd52010-11-02 10:14:22 -0400145 ima_check_last_writer(iint, inode, file);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500146}
147
Mimi Zohar3323eec2009-02-04 09:06:58 -0500148static 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;
Mimi Zoharf381c272011-03-09 14:13:22 -0500152 struct integrity_iint_cache *iint;
Mimi Zohar08e1b762012-06-20 09:32:55 -0400153 unsigned char *pathname = NULL, *pathbuf = NULL;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500154 int rc = -ENOMEM, action, must_appraise;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500155
156 if (!ima_initialized || !S_ISREG(inode->i_mode))
157 return 0;
Eric Parisbc7d2a32010-10-25 14:42:05 -0400158
Peter Moodye7c568e2012-06-14 10:04:36 -0700159 /* Determine if in appraise/audit/measurement policy,
160 * returns IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT bitmask. */
Dmitry Kasatkind9d300c2012-06-27 11:26:14 +0300161 action = ima_get_action(inode, mask, function);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500162 if (!action)
163 return 0;
164
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500165 must_appraise = action & IMA_APPRAISE;
Eric Parisbc7d2a32010-10-25 14:42:05 -0400166
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500167 mutex_lock(&inode->i_mutex);
168
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300169 iint = integrity_inode_get(inode);
170 if (!iint)
171 goto out;
172
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500173 /* Determine if already appraised/measured based on bitmask
Peter Moodye7c568e2012-06-14 10:04:36 -0700174 * (IMA_MEASURE, IMA_MEASURED, IMA_APPRAISE, IMA_APPRAISED,
175 * IMA_AUDIT, IMA_AUDITED) */
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500176 iint->flags |= action;
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300177 action &= ~((iint->flags & IMA_DONE_MASK) >> 1);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500178
179 /* Nothing to do, just return existing appraised status */
180 if (!action) {
181 if (iint->flags & IMA_APPRAISED)
182 rc = iint->ima_status;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500183 goto out;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500184 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500185
186 rc = ima_collect_measurement(iint, file);
Mimi Zohar08e1b762012-06-20 09:32:55 -0400187 if (rc != 0)
188 goto out;
189
190 if (function != BPRM_CHECK) {
191 /* We will allow 11 spaces for ' (deleted)' to be appended */
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 }
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500200 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);
Peter Moodye7c568e2012-06-14 10:04:36 -0700206 if (action & IMA_AUDIT)
207 ima_audit_measurement(iint, !pathname ? filename : pathname);
Mimi Zohar08e1b762012-06-20 09:32:55 -0400208 kfree(pathbuf);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500209out:
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500210 mutex_unlock(&inode->i_mutex);
211 return (rc && must_appraise) ? -EACCES : 0;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500212}
213
214/**
215 * ima_file_mmap - based on policy, collect/store measurement.
216 * @file: pointer to the file to be measured (May be NULL)
217 * @prot: contains the protection that will be applied by the kernel.
218 *
219 * Measure files being mmapped executable based on the ima_must_measure()
220 * policy decision.
221 *
222 * Return 0 on success, an error code on failure.
223 * (Based on the results of appraise_measurement().)
224 */
225int ima_file_mmap(struct file *file, unsigned long prot)
226{
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500227 int rc = 0;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500228
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);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500234 return (ima_appraise & IMA_APPRAISE_ENFORCE) ? rc : 0;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500235}
236
237/**
238 * ima_bprm_check - based on policy, collect/store measurement.
239 * @bprm: contains the linux_binprm structure
240 *
241 * The OS protects against an executable file, already open for write,
242 * from being executed in deny_write_access() and an executable file,
243 * already open for execute, from being modified in get_write_access().
244 * So we can be certain that what we verify and measure here is actually
245 * what is being executed.
246 *
247 * Return 0 on success, an error code on failure.
248 * (Based on the results of appraise_measurement().)
249 */
250int ima_bprm_check(struct linux_binprm *bprm)
251{
252 int rc;
253
Mimi Zoharfbbb4562012-05-14 21:50:11 -0400254 rc = process_measurement(bprm->file,
255 (strcmp(bprm->filename, bprm->interp) == 0) ?
256 bprm->filename : bprm->interp,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500257 MAY_EXEC, BPRM_CHECK);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500258 return (ima_appraise & IMA_APPRAISE_ENFORCE) ? rc : 0;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500259}
260
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500261/**
262 * ima_path_check - based on policy, collect/store measurement.
263 * @file: pointer to the file to be measured
264 * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
265 *
266 * Measure files based on the ima_must_measure() policy decision.
267 *
268 * Always return 0 and audit dentry_open failures.
269 * (Return code will be based upon measurement appraisal.)
270 */
Mimi Zohar9bbb6ca2010-01-26 17:02:40 -0500271int ima_file_check(struct file *file, int mask)
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500272{
273 int rc;
274
Mimi Zohar890275b52010-11-02 10:13:07 -0400275 ima_rdwr_violation_check(file);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500276 rc = process_measurement(file, file->f_dentry->d_name.name,
277 mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
Mimi Zohar1e93d002010-01-26 17:02:41 -0500278 FILE_CHECK);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500279 return (ima_appraise & IMA_APPRAISE_ENFORCE) ? rc : 0;
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500280}
Mimi Zohar9bbb6ca2010-01-26 17:02:40 -0500281EXPORT_SYMBOL_GPL(ima_file_check);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500282
Mimi Zoharfdf90722012-10-16 12:40:08 +1030283/**
284 * ima_module_check - based on policy, collect/store/appraise measurement.
285 * @file: pointer to the file to be measured/appraised
286 *
287 * Measure/appraise kernel modules based on policy.
288 *
289 * Always return 0 and audit dentry_open failures.
290 * Return code is based upon measurement appraisal.
291 */
292int ima_module_check(struct file *file)
293{
Mimi Zohara7f2a362012-12-21 08:34:21 -0500294 int rc = 0;
Mimi Zoharfdf90722012-10-16 12:40:08 +1030295
Mimi Zohara7f2a362012-12-21 08:34:21 -0500296 if (!file) {
297 if (ima_appraise & IMA_APPRAISE_MODULES) {
298#ifndef CONFIG_MODULE_SIG_FORCE
299 rc = -EACCES; /* INTEGRITY_UNKNOWN */
300#endif
301 }
302 } else
Mimi Zoharfdf90722012-10-16 12:40:08 +1030303 rc = process_measurement(file, file->f_dentry->d_name.name,
304 MAY_EXEC, MODULE_CHECK);
305 return (ima_appraise & IMA_APPRAISE_ENFORCE) ? rc : 0;
306}
307
Mimi Zohar3323eec2009-02-04 09:06:58 -0500308static int __init init_ima(void)
309{
310 int error;
311
Mimi Zohar3323eec2009-02-04 09:06:58 -0500312 error = ima_init();
Dmitry Kasatkin7ff22672012-06-25 12:18:11 +0300313 if (!error)
314 ima_initialized = 1;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500315 return error;
316}
317
Mimi Zohar3323eec2009-02-04 09:06:58 -0500318late_initcall(init_ima); /* Start IMA after the TPM is available */
319
320MODULE_DESCRIPTION("Integrity Measurement Architecture");
321MODULE_LICENSE("GPL");