blob: d3113d4aaa3c3f41d0395f4b0862f37389b8997f [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>
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +030018#include <crypto/hash_info.h>
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050019
20#include "ima.h"
21
22static int __init default_appraise_setup(char *str)
23{
24 if (strncmp(str, "off", 3) == 0)
25 ima_appraise = 0;
26 else if (strncmp(str, "fix", 3) == 0)
27 ima_appraise = IMA_APPRAISE_FIX;
28 return 1;
29}
30
31__setup("ima_appraise=", default_appraise_setup);
32
33/*
34 * ima_must_appraise - set appraise flag
35 *
36 * Return 1 to appraise
37 */
Dmitry Kasatkind26e19362012-09-27 18:26:53 +030038int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050039{
Mimi Zohar07f6a792011-03-09 22:25:48 -050040 if (!ima_appraise)
41 return 0;
42
43 return ima_match_policy(inode, func, mask, IMA_APPRAISE);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050044}
45
Dmitry Kasatkindef3e8b2012-09-20 22:38:53 +030046static int ima_fix_xattr(struct dentry *dentry,
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +030047 struct integrity_iint_cache *iint)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050048{
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +030049 int rc, offset;
50 u8 algo = iint->ima_hash->algo;
51
52 if (algo <= HASH_ALGO_SHA1) {
53 offset = 1;
54 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
55 } else {
56 offset = 0;
57 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
58 iint->ima_hash->xattr.ng.algo = algo;
59 }
60 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
61 &iint->ima_hash->xattr.data[offset],
62 (sizeof(iint->ima_hash->xattr) - offset) +
63 iint->ima_hash->length, 0);
64 return rc;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050065}
66
Mimi Zohard79d72e2012-12-03 17:08:11 -050067/* Return specific func appraised cached result */
68enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
69 int func)
70{
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +090071 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -050072 case MMAP_CHECK:
73 return iint->ima_mmap_status;
74 case BPRM_CHECK:
75 return iint->ima_bprm_status;
76 case MODULE_CHECK:
77 return iint->ima_module_status;
78 case FILE_CHECK:
79 default:
80 return iint->ima_file_status;
81 }
82}
83
84static void ima_set_cache_status(struct integrity_iint_cache *iint,
85 int func, enum integrity_status status)
86{
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +090087 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -050088 case MMAP_CHECK:
89 iint->ima_mmap_status = status;
90 break;
91 case BPRM_CHECK:
92 iint->ima_bprm_status = status;
93 break;
94 case MODULE_CHECK:
95 iint->ima_module_status = status;
96 break;
97 case FILE_CHECK:
98 default:
99 iint->ima_file_status = status;
100 break;
101 }
102}
103
104static void ima_cache_flags(struct integrity_iint_cache *iint, int func)
105{
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +0900106 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500107 case MMAP_CHECK:
108 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
109 break;
110 case BPRM_CHECK:
111 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
112 break;
113 case MODULE_CHECK:
114 iint->flags |= (IMA_MODULE_APPRAISED | IMA_APPRAISED);
115 break;
116 case FILE_CHECK:
117 default:
118 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
119 break;
120 }
121}
122
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300123void ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len,
124 struct ima_digest_data *hash)
125{
126 struct signature_v2_hdr *sig;
127
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300128 if (!xattr_value || xattr_len < 2)
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300129 return;
130
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300131 switch (xattr_value->type) {
132 case EVM_IMA_XATTR_DIGSIG:
133 sig = (typeof(sig))xattr_value;
134 if (sig->version != 2 || xattr_len <= sizeof(*sig))
135 return;
136 hash->algo = sig->hash_algo;
137 break;
138 case IMA_XATTR_DIGEST_NG:
139 hash->algo = xattr_value->digest[0];
140 break;
141 case IMA_XATTR_DIGEST:
142 /* this is for backward compatibility */
143 if (xattr_len == 21) {
144 unsigned int zero = 0;
145 if (!memcmp(&xattr_value->digest[16], &zero, 4))
146 hash->algo = HASH_ALGO_MD5;
147 else
148 hash->algo = HASH_ALGO_SHA1;
149 } else if (xattr_len == 17)
150 hash->algo = HASH_ALGO_MD5;
151 break;
152 }
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300153}
154
155int ima_read_xattr(struct dentry *dentry,
156 struct evm_ima_xattr_data **xattr_value)
157{
158 struct inode *inode = dentry->d_inode;
159
160 if (!inode->i_op->getxattr)
161 return 0;
162
163 return vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
164 0, GFP_NOFS);
165}
166
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500167/*
168 * ima_appraise_measurement - appraise file measurement
169 *
170 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
171 * Assuming success, compare the xattr hash with the collected measurement.
172 *
173 * Return 0 on success, error code otherwise
174 */
Mimi Zohard79d72e2012-12-03 17:08:11 -0500175int ima_appraise_measurement(int func, struct integrity_iint_cache *iint,
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300176 struct file *file, const unsigned char *filename,
177 struct evm_ima_xattr_data *xattr_value,
178 int xattr_len)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500179{
Mimi Zohar52a13282013-12-11 14:44:04 -0500180 static const char op[] = "appraise_data";
181 char *cause = "unknown";
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500182 struct dentry *dentry = file->f_dentry;
183 struct inode *inode = dentry->d_inode;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500184 enum integrity_status status = INTEGRITY_UNKNOWN;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300185 int rc = xattr_len, hash_start = 0;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500186
187 if (!ima_appraise)
188 return 0;
189 if (!inode->i_op->getxattr)
190 return INTEGRITY_UNKNOWN;
191
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500192 if (rc <= 0) {
193 if (rc && rc != -ENODATA)
194 goto out;
195
196 cause = "missing-hash";
197 status =
198 (inode->i_size == 0) ? INTEGRITY_PASS : INTEGRITY_NOLABEL;
199 goto out;
200 }
201
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300202 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500203 if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) {
204 if ((status == INTEGRITY_NOLABEL)
205 || (status == INTEGRITY_NOXATTRS))
206 cause = "missing-HMAC";
207 else if (status == INTEGRITY_FAIL)
208 cause = "invalid-HMAC";
209 goto out;
210 }
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300211 switch (xattr_value->type) {
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300212 case IMA_XATTR_DIGEST_NG:
213 /* first byte contains algorithm id */
214 hash_start = 1;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300215 case IMA_XATTR_DIGEST:
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300216 if (iint->flags & IMA_DIGSIG_REQUIRED) {
217 cause = "IMA signature required";
218 status = INTEGRITY_FAIL;
219 break;
220 }
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300221 if (xattr_len - sizeof(xattr_value->type) - hash_start >=
222 iint->ima_hash->length)
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300223 /* xattr length may be longer. md5 hash in previous
224 version occupied 20 bytes in xattr, instead of 16
225 */
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300226 rc = memcmp(&xattr_value->digest[hash_start],
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300227 iint->ima_hash->digest,
228 iint->ima_hash->length);
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300229 else
230 rc = -EINVAL;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300231 if (rc) {
232 cause = "invalid-hash";
233 status = INTEGRITY_FAIL;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300234 break;
235 }
236 status = INTEGRITY_PASS;
237 break;
238 case EVM_IMA_XATTR_DIGSIG:
239 iint->flags |= IMA_DIGSIG;
240 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
Dmitry Kasatkinb1aaab22013-10-10 16:12:03 +0900241 (const char *)xattr_value, rc,
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300242 iint->ima_hash->digest,
243 iint->ima_hash->length);
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300244 if (rc == -EOPNOTSUPP) {
245 status = INTEGRITY_UNKNOWN;
246 } else if (rc) {
247 cause = "invalid-signature";
248 status = INTEGRITY_FAIL;
249 } else {
250 status = INTEGRITY_PASS;
251 }
252 break;
253 default:
254 status = INTEGRITY_UNKNOWN;
255 cause = "unknown-ima-data";
256 break;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500257 }
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300258
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500259out:
260 if (status != INTEGRITY_PASS) {
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300261 if ((ima_appraise & IMA_APPRAISE_FIX) &&
262 (!xattr_value ||
263 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
Dmitry Kasatkindef3e8b2012-09-20 22:38:53 +0300264 if (!ima_fix_xattr(dentry, iint))
265 status = INTEGRITY_PASS;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500266 }
267 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
268 op, cause, rc, 0);
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300269 } else {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500270 ima_cache_flags(iint, func);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500271 }
Mimi Zohard79d72e2012-12-03 17:08:11 -0500272 ima_set_cache_status(iint, func, status);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500273 return status;
274}
275
276/*
277 * ima_update_xattr - update 'security.ima' hash value
278 */
279void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
280{
281 struct dentry *dentry = file->f_dentry;
282 int rc = 0;
283
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300284 /* do not collect and update hash for digital signatures */
285 if (iint->flags & IMA_DIGSIG)
286 return;
287
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300288 rc = ima_collect_measurement(iint, file, NULL, NULL);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500289 if (rc < 0)
290 return;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300291
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500292 ima_fix_xattr(dentry, iint);
293}
294
295/**
296 * ima_inode_post_setattr - reflect file metadata changes
297 * @dentry: pointer to the affected dentry
298 *
299 * Changes to a dentry's metadata might result in needing to appraise.
300 *
301 * This function is called from notify_change(), which expects the caller
302 * to lock the inode's i_mutex.
303 */
304void ima_inode_post_setattr(struct dentry *dentry)
305{
306 struct inode *inode = dentry->d_inode;
307 struct integrity_iint_cache *iint;
308 int must_appraise, rc;
309
310 if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode)
311 || !inode->i_op->removexattr)
312 return;
313
314 must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
315 iint = integrity_iint_find(inode);
316 if (iint) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500317 iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED |
318 IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK |
319 IMA_ACTION_FLAGS);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500320 if (must_appraise)
321 iint->flags |= IMA_APPRAISE;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500322 }
323 if (!must_appraise)
324 rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA);
325 return;
326}
Mimi Zohar42c63332011-03-10 18:54:15 -0500327
328/*
329 * ima_protect_xattr - protect 'security.ima'
330 *
331 * Ensure that not just anyone can modify or remove 'security.ima'.
332 */
333static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
334 const void *xattr_value, size_t xattr_value_len)
335{
336 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
337 if (!capable(CAP_SYS_ADMIN))
338 return -EPERM;
339 return 1;
340 }
341 return 0;
342}
343
Mimi Zohar060bdebfb2014-03-17 23:24:18 -0400344static void ima_reset_appraise_flags(struct inode *inode, int digsig)
Mimi Zohar42c63332011-03-10 18:54:15 -0500345{
346 struct integrity_iint_cache *iint;
347
348 if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode))
349 return;
350
351 iint = integrity_iint_find(inode);
352 if (!iint)
353 return;
354
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300355 iint->flags &= ~IMA_DONE_MASK;
Mimi Zohar060bdebfb2014-03-17 23:24:18 -0400356 if (digsig)
357 iint->flags |= IMA_DIGSIG;
Mimi Zohar42c63332011-03-10 18:54:15 -0500358 return;
359}
360
361int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
362 const void *xattr_value, size_t xattr_value_len)
363{
Mimi Zohar060bdebfb2014-03-17 23:24:18 -0400364 const struct evm_ima_xattr_data *xvalue = xattr_value;
Mimi Zohar42c63332011-03-10 18:54:15 -0500365 int result;
366
367 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
368 xattr_value_len);
369 if (result == 1) {
Mimi Zohar060bdebfb2014-03-17 23:24:18 -0400370 ima_reset_appraise_flags(dentry->d_inode,
371 (xvalue->type == EVM_IMA_XATTR_DIGSIG) ? 1 : 0);
Mimi Zohar42c63332011-03-10 18:54:15 -0500372 result = 0;
373 }
374 return result;
375}
376
377int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
378{
379 int result;
380
381 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
382 if (result == 1) {
Mimi Zohar060bdebfb2014-03-17 23:24:18 -0400383 ima_reset_appraise_flags(dentry->d_inode, 0);
Mimi Zohar42c63332011-03-10 18:54:15 -0500384 result = 0;
385 }
386 return result;
387}