blob: 3833b0fa71084576eda8562187baaaa08a52c9a5 [file] [log] [blame]
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -05001/*
2 * Copyright (C) 2011 IBM Corporation
3 *
4 * Author:
5 * Mimi Zohar <zohar@us.ibm.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2 of the License.
10 */
11#include <linux/module.h>
12#include <linux/file.h>
13#include <linux/fs.h>
14#include <linux/xattr.h>
15#include <linux/magic.h>
16#include <linux/ima.h>
17#include <linux/evm.h>
18
19#include "ima.h"
20
21static int __init default_appraise_setup(char *str)
22{
23 if (strncmp(str, "off", 3) == 0)
24 ima_appraise = 0;
25 else if (strncmp(str, "fix", 3) == 0)
26 ima_appraise = IMA_APPRAISE_FIX;
27 return 1;
28}
29
30__setup("ima_appraise=", default_appraise_setup);
31
32/*
33 * ima_must_appraise - set appraise flag
34 *
35 * Return 1 to appraise
36 */
Dmitry Kasatkind26e1932012-09-27 18:26:53 +030037int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050038{
Mimi Zohar07f6a792011-03-09 22:25:48 -050039 if (!ima_appraise)
40 return 0;
41
42 return ima_match_policy(inode, func, mask, IMA_APPRAISE);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050043}
44
Dmitry Kasatkindef3e8b2012-09-20 22:38:53 +030045static int ima_fix_xattr(struct dentry *dentry,
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +030046 struct integrity_iint_cache *iint)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050047{
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +030048 iint->ima_hash.type = IMA_XATTR_DIGEST;
Dmitry Kasatkindef3e8b2012-09-20 22:38:53 +030049 return __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +030050 &iint->ima_hash.type,
51 1 + iint->ima_hash.length, 0);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050052}
53
Mimi Zohard79d72e2012-12-03 17:08:11 -050054/* Return specific func appraised cached result */
55enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
56 int func)
57{
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +090058 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -050059 case MMAP_CHECK:
60 return iint->ima_mmap_status;
61 case BPRM_CHECK:
62 return iint->ima_bprm_status;
63 case MODULE_CHECK:
64 return iint->ima_module_status;
65 case FILE_CHECK:
66 default:
67 return iint->ima_file_status;
68 }
69}
70
71static void ima_set_cache_status(struct integrity_iint_cache *iint,
72 int func, enum integrity_status status)
73{
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +090074 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -050075 case MMAP_CHECK:
76 iint->ima_mmap_status = status;
77 break;
78 case BPRM_CHECK:
79 iint->ima_bprm_status = status;
80 break;
81 case MODULE_CHECK:
82 iint->ima_module_status = status;
83 break;
84 case FILE_CHECK:
85 default:
86 iint->ima_file_status = status;
87 break;
88 }
89}
90
91static void ima_cache_flags(struct integrity_iint_cache *iint, int func)
92{
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +090093 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -050094 case MMAP_CHECK:
95 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
96 break;
97 case BPRM_CHECK:
98 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
99 break;
100 case MODULE_CHECK:
101 iint->flags |= (IMA_MODULE_APPRAISED | IMA_APPRAISED);
102 break;
103 case FILE_CHECK:
104 default:
105 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
106 break;
107 }
108}
109
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500110/*
111 * ima_appraise_measurement - appraise file measurement
112 *
113 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
114 * Assuming success, compare the xattr hash with the collected measurement.
115 *
116 * Return 0 on success, error code otherwise
117 */
Mimi Zohard79d72e2012-12-03 17:08:11 -0500118int ima_appraise_measurement(int func, struct integrity_iint_cache *iint,
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500119 struct file *file, const unsigned char *filename)
120{
121 struct dentry *dentry = file->f_dentry;
122 struct inode *inode = dentry->d_inode;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300123 struct evm_ima_xattr_data *xattr_value = NULL;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500124 enum integrity_status status = INTEGRITY_UNKNOWN;
125 const char *op = "appraise_data";
126 char *cause = "unknown";
127 int rc;
128
129 if (!ima_appraise)
130 return 0;
131 if (!inode->i_op->getxattr)
132 return INTEGRITY_UNKNOWN;
133
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300134 rc = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)&xattr_value,
135 0, GFP_NOFS);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500136 if (rc <= 0) {
137 if (rc && rc != -ENODATA)
138 goto out;
139
140 cause = "missing-hash";
141 status =
142 (inode->i_size == 0) ? INTEGRITY_PASS : INTEGRITY_NOLABEL;
143 goto out;
144 }
145
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300146 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500147 if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) {
148 if ((status == INTEGRITY_NOLABEL)
149 || (status == INTEGRITY_NOXATTRS))
150 cause = "missing-HMAC";
151 else if (status == INTEGRITY_FAIL)
152 cause = "invalid-HMAC";
153 goto out;
154 }
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300155 switch (xattr_value->type) {
156 case IMA_XATTR_DIGEST:
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300157 if (iint->flags & IMA_DIGSIG_REQUIRED) {
158 cause = "IMA signature required";
159 status = INTEGRITY_FAIL;
160 break;
161 }
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300162 if (rc - 1 == iint->ima_hash.length)
163 rc = memcmp(xattr_value->digest,
164 iint->ima_hash.digest,
165 iint->ima_hash.length);
166 else
167 rc = -EINVAL;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300168 if (rc) {
169 cause = "invalid-hash";
170 status = INTEGRITY_FAIL;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300171 break;
172 }
173 status = INTEGRITY_PASS;
174 break;
175 case EVM_IMA_XATTR_DIGSIG:
176 iint->flags |= IMA_DIGSIG;
177 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
178 xattr_value->digest, rc - 1,
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300179 iint->ima_hash.digest,
180 iint->ima_hash.length);
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300181 if (rc == -EOPNOTSUPP) {
182 status = INTEGRITY_UNKNOWN;
183 } else if (rc) {
184 cause = "invalid-signature";
185 status = INTEGRITY_FAIL;
186 } else {
187 status = INTEGRITY_PASS;
188 }
189 break;
190 default:
191 status = INTEGRITY_UNKNOWN;
192 cause = "unknown-ima-data";
193 break;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500194 }
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300195
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500196out:
197 if (status != INTEGRITY_PASS) {
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300198 if ((ima_appraise & IMA_APPRAISE_FIX) &&
199 (!xattr_value ||
200 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
Dmitry Kasatkindef3e8b2012-09-20 22:38:53 +0300201 if (!ima_fix_xattr(dentry, iint))
202 status = INTEGRITY_PASS;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500203 }
204 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
205 op, cause, rc, 0);
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300206 } else {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500207 ima_cache_flags(iint, func);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500208 }
Mimi Zohard79d72e2012-12-03 17:08:11 -0500209 ima_set_cache_status(iint, func, status);
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300210 kfree(xattr_value);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500211 return status;
212}
213
214/*
215 * ima_update_xattr - update 'security.ima' hash value
216 */
217void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
218{
219 struct dentry *dentry = file->f_dentry;
220 int rc = 0;
221
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300222 /* do not collect and update hash for digital signatures */
223 if (iint->flags & IMA_DIGSIG)
224 return;
225
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500226 rc = ima_collect_measurement(iint, file);
227 if (rc < 0)
228 return;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300229
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500230 ima_fix_xattr(dentry, iint);
231}
232
233/**
234 * ima_inode_post_setattr - reflect file metadata changes
235 * @dentry: pointer to the affected dentry
236 *
237 * Changes to a dentry's metadata might result in needing to appraise.
238 *
239 * This function is called from notify_change(), which expects the caller
240 * to lock the inode's i_mutex.
241 */
242void ima_inode_post_setattr(struct dentry *dentry)
243{
244 struct inode *inode = dentry->d_inode;
245 struct integrity_iint_cache *iint;
246 int must_appraise, rc;
247
248 if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode)
249 || !inode->i_op->removexattr)
250 return;
251
252 must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
253 iint = integrity_iint_find(inode);
254 if (iint) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500255 iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED |
256 IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK |
257 IMA_ACTION_FLAGS);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500258 if (must_appraise)
259 iint->flags |= IMA_APPRAISE;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500260 }
261 if (!must_appraise)
262 rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA);
263 return;
264}
Mimi Zohar42c63332011-03-10 18:54:15 -0500265
266/*
267 * ima_protect_xattr - protect 'security.ima'
268 *
269 * Ensure that not just anyone can modify or remove 'security.ima'.
270 */
271static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
272 const void *xattr_value, size_t xattr_value_len)
273{
274 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
275 if (!capable(CAP_SYS_ADMIN))
276 return -EPERM;
277 return 1;
278 }
279 return 0;
280}
281
282static void ima_reset_appraise_flags(struct inode *inode)
283{
284 struct integrity_iint_cache *iint;
285
286 if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode))
287 return;
288
289 iint = integrity_iint_find(inode);
290 if (!iint)
291 return;
292
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300293 iint->flags &= ~IMA_DONE_MASK;
Mimi Zohar42c63332011-03-10 18:54:15 -0500294 return;
295}
296
297int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
298 const void *xattr_value, size_t xattr_value_len)
299{
300 int result;
301
302 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
303 xattr_value_len);
304 if (result == 1) {
305 ima_reset_appraise_flags(dentry->d_inode);
306 result = 0;
307 }
308 return result;
309}
310
311int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
312{
313 int result;
314
315 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
316 if (result == 1) {
317 ima_reset_appraise_flags(dentry->d_inode);
318 result = 0;
319 }
320 return result;
321}