]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - security/integrity/ima/ima_main.c
ima: sparse fix: make ima_open_policy static
[mirror_ubuntu-artful-kernel.git] / security / integrity / ima / ima_main.c
CommitLineData
3323eec9
MZ
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
e0d5bd2a 16 * implements the IMA hooks: ima_bprm_check, ima_file_mmap,
9bbb6cad 17 * and ima_file_check.
3323eec9
MZ
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>
5a0e3ad6 24#include <linux/slab.h>
3323eec9
MZ
25
26#include "ima.h"
27
28int ima_initialized;
29
30char *ima_hash = "sha1";
31static int __init hash_setup(char *str)
32{
07ff7a0b
MZ
33 if (strncmp(str, "md5", 3) == 0)
34 ima_hash = "md5";
3323eec9
MZ
35 return 1;
36}
37__setup("ima_hash=", hash_setup);
38
8eb988c7 39/*
890275b5 40 * ima_rdwr_violation_check
8eb988c7 41 *
890275b5 42 * Only invalidate the PCR for measured files:
8eb988c7
MZ
43 * - 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 */
890275b5 49static void ima_rdwr_violation_check(struct file *file)
8eb988c7
MZ
50{
51 struct dentry *dentry = file->f_path.dentry;
52 struct inode *inode = dentry->d_inode;
53 fmode_t mode = file->f_mode;
8eb988c7 54 int rc;
ad16ad00 55 bool send_tomtou = false, send_writers = false;
8eb988c7 56
890275b5 57 if (!S_ISREG(inode->i_mode) || !ima_initialized)
8eb988c7 58 return;
a178d202 59
890275b5 60 mutex_lock(&inode->i_mutex); /* file metadata: permissions, xattr */
ad16ad00 61
8eb988c7 62 if (mode & FMODE_WRITE) {
a68a27b6 63 if (atomic_read(&inode->i_readcount) && IS_IMA(inode))
ad16ad00 64 send_tomtou = true;
8eb988c7
MZ
65 goto out;
66 }
ad16ad00 67
1adace9b 68 rc = ima_must_measure(inode, MAY_READ, FILE_CHECK);
bade72d6
EP
69 if (rc < 0)
70 goto out;
71
ad16ad00
EP
72 if (atomic_read(&inode->i_writecount) > 0)
73 send_writers = true;
8eb988c7 74out:
890275b5 75 mutex_unlock(&inode->i_mutex);
ad16ad00
EP
76
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");
8eb988c7
MZ
83}
84
f381c272 85static void ima_check_last_writer(struct integrity_iint_cache *iint,
bc7d2a3e
EP
86 struct inode *inode,
87 struct file *file)
88{
4b2a2c67 89 fmode_t mode = file->f_mode;
bc7d2a3e 90
854fdd55 91 mutex_lock(&iint->mutex);
bc7d2a3e
EP
92 if (mode & FMODE_WRITE &&
93 atomic_read(&inode->i_writecount) == 1 &&
94 iint->version != inode->i_version)
95 iint->flags &= ~IMA_MEASURED;
bc7d2a3e 96 mutex_unlock(&iint->mutex);
bc7d2a3e
EP
97}
98
3323eec9
MZ
99/**
100 * ima_file_free - called on __fput()
101 * @file: pointer to file structure being freed
102 *
890275b5 103 * Flag files that changed, based on i_version
3323eec9
MZ
104 */
105void ima_file_free(struct file *file)
106{
107 struct inode *inode = file->f_dentry->d_inode;
f381c272 108 struct integrity_iint_cache *iint;
3323eec9 109
e950598d 110 if (!iint_initialized || !S_ISREG(inode->i_mode))
3323eec9 111 return;
196f5181 112
f381c272 113 iint = integrity_iint_find(inode);
854fdd55
MZ
114 if (!iint)
115 return;
3323eec9 116
854fdd55 117 ima_check_last_writer(iint, inode, file);
3323eec9
MZ
118}
119
3323eec9
MZ
120static 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;
f381c272 124 struct integrity_iint_cache *iint;
e950598d 125 int rc = 0;
3323eec9
MZ
126
127 if (!ima_initialized || !S_ISREG(inode->i_mode))
128 return 0;
bc7d2a3e 129
1adace9b 130 rc = ima_must_measure(inode, mask, function);
bc7d2a3e
EP
131 if (rc != 0)
132 return rc;
133retry:
f381c272 134 iint = integrity_iint_find(inode);
bc7d2a3e 135 if (!iint) {
f381c272 136 rc = integrity_inode_alloc(inode);
bc7d2a3e
EP
137 if (!rc || rc == -EEXIST)
138 goto retry;
139 return rc;
140 }
3323eec9
MZ
141
142 mutex_lock(&iint->mutex);
bc7d2a3e 143
1adace9b 144 rc = iint->flags & IMA_MEASURED ? 1 : 0;
3323eec9
MZ
145 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);
3323eec9
MZ
153 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
8eb988c7
MZ
201/**
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 */
9bbb6cad 211int ima_file_check(struct file *file, int mask)
8eb988c7
MZ
212{
213 int rc;
214
890275b5 215 ima_rdwr_violation_check(file);
8eb988c7
MZ
216 rc = process_measurement(file, file->f_dentry->d_name.name,
217 mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
1e93d005 218 FILE_CHECK);
8eb988c7
MZ
219 return 0;
220}
9bbb6cad 221EXPORT_SYMBOL_GPL(ima_file_check);
8eb988c7 222
3323eec9
MZ
223static int __init init_ima(void)
224{
225 int error;
226
3323eec9
MZ
227 error = ima_init();
228 ima_initialized = 1;
229 return error;
230}
231
bab73937
MZ
232static void __exit cleanup_ima(void)
233{
234 ima_cleanup();
235}
236
3323eec9
MZ
237late_initcall(init_ima); /* Start IMA after the TPM is available */
238
239MODULE_DESCRIPTION("Integrity Measurement Architecture");
240MODULE_LICENSE("GPL");