blob: cb0d0ff1137ba8aca0c48f52ad33b13278fee2d5 [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;
Dmitry Kasatkin2faa6ef2014-05-08 13:11:29 +030025 else if (strncmp(str, "log", 3) == 0)
26 ima_appraise = IMA_APPRAISE_LOG;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050027 else if (strncmp(str, "fix", 3) == 0)
28 ima_appraise = IMA_APPRAISE_FIX;
29 return 1;
30}
31
32__setup("ima_appraise=", default_appraise_setup);
33
34/*
35 * ima_must_appraise - set appraise flag
36 *
37 * Return 1 to appraise
38 */
Dmitry Kasatkind26e1932012-09-27 18:26:53 +030039int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050040{
Mimi Zohar07f6a792011-03-09 22:25:48 -050041 if (!ima_appraise)
42 return 0;
43
44 return ima_match_policy(inode, func, mask, IMA_APPRAISE);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050045}
46
Dmitry Kasatkindef3e8b2012-09-20 22:38:53 +030047static int ima_fix_xattr(struct dentry *dentry,
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +030048 struct integrity_iint_cache *iint)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050049{
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +030050 int rc, offset;
51 u8 algo = iint->ima_hash->algo;
52
53 if (algo <= HASH_ALGO_SHA1) {
54 offset = 1;
55 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
56 } else {
57 offset = 0;
58 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
59 iint->ima_hash->xattr.ng.algo = algo;
60 }
61 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
62 &iint->ima_hash->xattr.data[offset],
63 (sizeof(iint->ima_hash->xattr) - offset) +
64 iint->ima_hash->length, 0);
65 return rc;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050066}
67
Mimi Zohard79d72e2012-12-03 17:08:11 -050068/* Return specific func appraised cached result */
69enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
Mimi Zohar4ad87a32016-01-14 20:59:14 -050070 enum ima_hooks func)
Mimi Zohard79d72e2012-12-03 17:08:11 -050071{
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +090072 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -050073 case MMAP_CHECK:
74 return iint->ima_mmap_status;
75 case BPRM_CHECK:
76 return iint->ima_bprm_status;
77 case MODULE_CHECK:
78 return iint->ima_module_status;
Mimi Zohar5a9196d2014-07-22 10:39:48 -040079 case FIRMWARE_CHECK:
80 return iint->ima_firmware_status;
Mimi Zohard79d72e2012-12-03 17:08:11 -050081 case FILE_CHECK:
82 default:
83 return iint->ima_file_status;
84 }
85}
86
87static void ima_set_cache_status(struct integrity_iint_cache *iint,
Mimi Zohar4ad87a32016-01-14 20:59:14 -050088 enum ima_hooks func,
89 enum integrity_status status)
Mimi Zohard79d72e2012-12-03 17:08:11 -050090{
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +090091 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -050092 case MMAP_CHECK:
93 iint->ima_mmap_status = status;
94 break;
95 case BPRM_CHECK:
96 iint->ima_bprm_status = status;
97 break;
98 case MODULE_CHECK:
99 iint->ima_module_status = status;
100 break;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400101 case FIRMWARE_CHECK:
102 iint->ima_firmware_status = status;
103 break;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500104 case FILE_CHECK:
105 default:
106 iint->ima_file_status = status;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500107 }
108}
109
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500110static void ima_cache_flags(struct integrity_iint_cache *iint,
111 enum ima_hooks func)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500112{
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +0900113 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500114 case MMAP_CHECK:
115 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
116 break;
117 case BPRM_CHECK:
118 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
119 break;
120 case MODULE_CHECK:
121 iint->flags |= (IMA_MODULE_APPRAISED | IMA_APPRAISED);
122 break;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400123 case FIRMWARE_CHECK:
124 iint->flags |= (IMA_FIRMWARE_APPRAISED | IMA_APPRAISED);
125 break;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500126 case FILE_CHECK:
127 default:
128 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
Mimi Zohard79d72e2012-12-03 17:08:11 -0500129 }
130}
131
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200132enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
133 int xattr_len)
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300134{
135 struct signature_v2_hdr *sig;
136
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300137 if (!xattr_value || xattr_len < 2)
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200138 /* return default hash algo */
139 return ima_hash_algo;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300140
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300141 switch (xattr_value->type) {
142 case EVM_IMA_XATTR_DIGSIG:
143 sig = (typeof(sig))xattr_value;
144 if (sig->version != 2 || xattr_len <= sizeof(*sig))
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200145 return ima_hash_algo;
146 return sig->hash_algo;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300147 break;
148 case IMA_XATTR_DIGEST_NG:
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200149 return xattr_value->digest[0];
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300150 break;
151 case IMA_XATTR_DIGEST:
152 /* this is for backward compatibility */
153 if (xattr_len == 21) {
154 unsigned int zero = 0;
155 if (!memcmp(&xattr_value->digest[16], &zero, 4))
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200156 return HASH_ALGO_MD5;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300157 else
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200158 return HASH_ALGO_SHA1;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300159 } else if (xattr_len == 17)
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200160 return HASH_ALGO_MD5;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300161 break;
162 }
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200163
164 /* return default hash algo */
165 return ima_hash_algo;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300166}
167
168int ima_read_xattr(struct dentry *dentry,
169 struct evm_ima_xattr_data **xattr_value)
170{
David Howellsc6f493d2015-03-17 22:26:22 +0000171 struct inode *inode = d_backing_inode(dentry);
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300172
173 if (!inode->i_op->getxattr)
174 return 0;
175
176 return vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
177 0, GFP_NOFS);
178}
179
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500180/*
181 * ima_appraise_measurement - appraise file measurement
182 *
183 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
184 * Assuming success, compare the xattr hash with the collected measurement.
185 *
186 * Return 0 on success, error code otherwise
187 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500188int ima_appraise_measurement(enum ima_hooks func,
189 struct integrity_iint_cache *iint,
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300190 struct file *file, const unsigned char *filename,
191 struct evm_ima_xattr_data *xattr_value,
Dmitry Kasatkin3034a142014-06-27 18:15:44 +0300192 int xattr_len, int opened)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500193{
Mimi Zohar52a13282013-12-11 14:44:04 -0500194 static const char op[] = "appraise_data";
195 char *cause = "unknown";
Al Virob5830432014-10-31 01:22:04 -0400196 struct dentry *dentry = file->f_path.dentry;
David Howellsc6f493d2015-03-17 22:26:22 +0000197 struct inode *inode = d_backing_inode(dentry);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500198 enum integrity_status status = INTEGRITY_UNKNOWN;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300199 int rc = xattr_len, hash_start = 0;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500200
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500201 if (!inode->i_op->getxattr)
202 return INTEGRITY_UNKNOWN;
203
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500204 if (rc <= 0) {
205 if (rc && rc != -ENODATA)
206 goto out;
207
208 cause = "missing-hash";
Dmitry Kasatkinb151d6b2014-06-27 18:04:27 +0300209 status = INTEGRITY_NOLABEL;
Dmitry Kasatkin3034a142014-06-27 18:15:44 +0300210 if (opened & FILE_CREATED) {
Dmitry Kasatkinb151d6b2014-06-27 18:04:27 +0300211 iint->flags |= IMA_NEW_FILE;
212 status = INTEGRITY_PASS;
213 }
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500214 goto out;
215 }
216
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300217 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500218 if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) {
219 if ((status == INTEGRITY_NOLABEL)
220 || (status == INTEGRITY_NOXATTRS))
221 cause = "missing-HMAC";
222 else if (status == INTEGRITY_FAIL)
223 cause = "invalid-HMAC";
224 goto out;
225 }
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300226 switch (xattr_value->type) {
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300227 case IMA_XATTR_DIGEST_NG:
228 /* first byte contains algorithm id */
229 hash_start = 1;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300230 case IMA_XATTR_DIGEST:
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300231 if (iint->flags & IMA_DIGSIG_REQUIRED) {
Richard Guy Briggs7e9001f2014-06-16 15:52:07 -0400232 cause = "IMA-signature-required";
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300233 status = INTEGRITY_FAIL;
234 break;
235 }
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300236 if (xattr_len - sizeof(xattr_value->type) - hash_start >=
237 iint->ima_hash->length)
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300238 /* xattr length may be longer. md5 hash in previous
239 version occupied 20 bytes in xattr, instead of 16
240 */
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300241 rc = memcmp(&xattr_value->digest[hash_start],
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300242 iint->ima_hash->digest,
243 iint->ima_hash->length);
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300244 else
245 rc = -EINVAL;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300246 if (rc) {
247 cause = "invalid-hash";
248 status = INTEGRITY_FAIL;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300249 break;
250 }
251 status = INTEGRITY_PASS;
252 break;
253 case EVM_IMA_XATTR_DIGSIG:
254 iint->flags |= IMA_DIGSIG;
255 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
Dmitry Kasatkinb1aaab22013-10-10 16:12:03 +0900256 (const char *)xattr_value, rc,
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300257 iint->ima_hash->digest,
258 iint->ima_hash->length);
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300259 if (rc == -EOPNOTSUPP) {
260 status = INTEGRITY_UNKNOWN;
261 } else if (rc) {
262 cause = "invalid-signature";
263 status = INTEGRITY_FAIL;
264 } else {
265 status = INTEGRITY_PASS;
266 }
267 break;
268 default:
269 status = INTEGRITY_UNKNOWN;
270 cause = "unknown-ima-data";
271 break;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500272 }
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300273
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500274out:
275 if (status != INTEGRITY_PASS) {
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300276 if ((ima_appraise & IMA_APPRAISE_FIX) &&
277 (!xattr_value ||
278 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
Dmitry Kasatkindef3e8b2012-09-20 22:38:53 +0300279 if (!ima_fix_xattr(dentry, iint))
280 status = INTEGRITY_PASS;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500281 }
282 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
283 op, cause, rc, 0);
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300284 } else {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500285 ima_cache_flags(iint, func);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500286 }
Mimi Zohard79d72e2012-12-03 17:08:11 -0500287 ima_set_cache_status(iint, func, status);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500288 return status;
289}
290
291/*
292 * ima_update_xattr - update 'security.ima' hash value
293 */
294void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
295{
Al Virob5830432014-10-31 01:22:04 -0400296 struct dentry *dentry = file->f_path.dentry;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500297 int rc = 0;
298
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300299 /* do not collect and update hash for digital signatures */
300 if (iint->flags & IMA_DIGSIG)
301 return;
302
Mimi Zoharcf222212016-01-14 17:57:47 -0500303 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500304 if (rc < 0)
305 return;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300306
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500307 ima_fix_xattr(dentry, iint);
308}
309
310/**
311 * ima_inode_post_setattr - reflect file metadata changes
312 * @dentry: pointer to the affected dentry
313 *
314 * Changes to a dentry's metadata might result in needing to appraise.
315 *
316 * This function is called from notify_change(), which expects the caller
317 * to lock the inode's i_mutex.
318 */
319void ima_inode_post_setattr(struct dentry *dentry)
320{
David Howellsc6f493d2015-03-17 22:26:22 +0000321 struct inode *inode = d_backing_inode(dentry);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500322 struct integrity_iint_cache *iint;
323 int must_appraise, rc;
324
Roberto Sassua7560242014-09-12 19:35:54 +0200325 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500326 || !inode->i_op->removexattr)
327 return;
328
329 must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
330 iint = integrity_iint_find(inode);
331 if (iint) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500332 iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED |
333 IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK |
334 IMA_ACTION_FLAGS);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500335 if (must_appraise)
336 iint->flags |= IMA_APPRAISE;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500337 }
338 if (!must_appraise)
339 rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA);
340 return;
341}
Mimi Zohar42c63332011-03-10 18:54:15 -0500342
343/*
344 * ima_protect_xattr - protect 'security.ima'
345 *
346 * Ensure that not just anyone can modify or remove 'security.ima'.
347 */
348static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
349 const void *xattr_value, size_t xattr_value_len)
350{
351 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
352 if (!capable(CAP_SYS_ADMIN))
353 return -EPERM;
354 return 1;
355 }
356 return 0;
357}
358
Mimi Zohar060bdebfb2014-03-17 23:24:18 -0400359static void ima_reset_appraise_flags(struct inode *inode, int digsig)
Mimi Zohar42c63332011-03-10 18:54:15 -0500360{
361 struct integrity_iint_cache *iint;
362
Roberto Sassua7560242014-09-12 19:35:54 +0200363 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
Mimi Zohar42c63332011-03-10 18:54:15 -0500364 return;
365
366 iint = integrity_iint_find(inode);
367 if (!iint)
368 return;
369
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300370 iint->flags &= ~IMA_DONE_MASK;
Mimi Zohar060bdebfb2014-03-17 23:24:18 -0400371 if (digsig)
372 iint->flags |= IMA_DIGSIG;
Mimi Zohar42c63332011-03-10 18:54:15 -0500373 return;
374}
375
376int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
377 const void *xattr_value, size_t xattr_value_len)
378{
Mimi Zohar060bdebfb2014-03-17 23:24:18 -0400379 const struct evm_ima_xattr_data *xvalue = xattr_value;
Mimi Zohar42c63332011-03-10 18:54:15 -0500380 int result;
381
382 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
383 xattr_value_len);
384 if (result == 1) {
Dmitry Kasatkinc68ed802014-10-23 15:53:21 +0300385 bool digsig;
386
Dmitry Kasatkina48fda92014-10-28 13:31:22 +0200387 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
388 return -EINVAL;
Dmitry Kasatkinc68ed802014-10-23 15:53:21 +0300389 digsig = (xvalue->type == EVM_IMA_XATTR_DIGSIG);
390 if (!digsig && (ima_appraise & IMA_APPRAISE_ENFORCE))
391 return -EPERM;
392 ima_reset_appraise_flags(d_backing_inode(dentry), digsig);
Mimi Zohar42c63332011-03-10 18:54:15 -0500393 result = 0;
394 }
395 return result;
396}
397
398int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
399{
400 int result;
401
402 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
403 if (result == 1) {
David Howellsc6f493d2015-03-17 22:26:22 +0000404 ima_reset_appraise_flags(d_backing_inode(dentry), 0);
Mimi Zohar42c63332011-03-10 18:54:15 -0500405 result = 0;
406 }
407 return result;
408}