blob: f0cd67cab6aa2506ce094131f10cfc2bb28235b7 [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -05002/*
3 * Copyright (C) 2011 IBM Corporation
4 *
5 * Author:
6 * Mimi Zohar <zohar@us.ibm.com>
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -05007 */
Paul Gortmaker876979c2018-12-09 15:36:29 -05008#include <linux/init.h>
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -05009#include <linux/file.h>
10#include <linux/fs.h>
11#include <linux/xattr.h>
12#include <linux/magic.h>
13#include <linux/ima.h>
14#include <linux/evm.h>
15
16#include "ima.h"
17
18static int __init default_appraise_setup(char *str)
19{
Mimi Zohare1f5e012017-04-24 22:06:49 -040020#ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050021 if (strncmp(str, "off", 3) == 0)
22 ima_appraise = 0;
Dmitry Kasatkin2faa6ef2014-05-08 13:11:29 +030023 else if (strncmp(str, "log", 3) == 0)
24 ima_appraise = IMA_APPRAISE_LOG;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050025 else if (strncmp(str, "fix", 3) == 0)
26 ima_appraise = IMA_APPRAISE_FIX;
Mimi Zohare1f5e012017-04-24 22:06:49 -040027#endif
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050028 return 1;
29}
30
31__setup("ima_appraise=", default_appraise_setup);
32
33/*
Mimi Zohar6f6723e2017-04-24 22:43:52 -040034 * is_ima_appraise_enabled - return appraise status
35 *
36 * Only return enabled, if not in ima_appraise="fix" or "log" modes.
37 */
38bool is_ima_appraise_enabled(void)
39{
Thiago Jung Bauermanne5729f82017-10-17 22:53:14 -020040 return ima_appraise & IMA_APPRAISE_ENFORCE;
Mimi Zohar6f6723e2017-04-24 22:43:52 -040041}
42
43/*
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050044 * ima_must_appraise - set appraise flag
45 *
Mimi Zoharda1b0022016-09-29 10:04:52 -040046 * Return 1 to appraise or hash
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050047 */
Dmitry Kasatkind26e1932012-09-27 18:26:53 +030048int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050049{
Matthew Garrettd906c102018-01-08 13:36:20 -080050 u32 secid;
51
Mimi Zohar07f6a792011-03-09 22:25:48 -050052 if (!ima_appraise)
53 return 0;
54
Matthew Garrettd906c102018-01-08 13:36:20 -080055 security_task_getsecid(current, &secid);
56 return ima_match_policy(inode, current_cred(), secid, func, mask,
57 IMA_APPRAISE | IMA_HASH, NULL);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050058}
59
Dmitry Kasatkindef3e8b2012-09-20 22:38:53 +030060static int ima_fix_xattr(struct dentry *dentry,
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +030061 struct integrity_iint_cache *iint)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050062{
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +030063 int rc, offset;
64 u8 algo = iint->ima_hash->algo;
65
66 if (algo <= HASH_ALGO_SHA1) {
67 offset = 1;
68 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
69 } else {
70 offset = 0;
71 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
72 iint->ima_hash->xattr.ng.algo = algo;
73 }
74 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
75 &iint->ima_hash->xattr.data[offset],
76 (sizeof(iint->ima_hash->xattr) - offset) +
77 iint->ima_hash->length, 0);
78 return rc;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050079}
80
Mimi Zohard79d72e2012-12-03 17:08:11 -050081/* Return specific func appraised cached result */
82enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
Mimi Zohar4ad87a32016-01-14 20:59:14 -050083 enum ima_hooks func)
Mimi Zohard79d72e2012-12-03 17:08:11 -050084{
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +090085 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -050086 case MMAP_CHECK:
87 return iint->ima_mmap_status;
88 case BPRM_CHECK:
89 return iint->ima_bprm_status;
Matthew Garrettd906c102018-01-08 13:36:20 -080090 case CREDS_CHECK:
91 return iint->ima_creds_status;
Mimi Zohard79d72e2012-12-03 17:08:11 -050092 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -050093 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -050094 return iint->ima_file_status;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -050095 case MODULE_CHECK ... MAX_CHECK - 1:
96 default:
97 return iint->ima_read_status;
Mimi Zohard79d72e2012-12-03 17:08:11 -050098 }
99}
100
101static void ima_set_cache_status(struct integrity_iint_cache *iint,
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500102 enum ima_hooks func,
103 enum integrity_status status)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500104{
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +0900105 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500106 case MMAP_CHECK:
107 iint->ima_mmap_status = status;
108 break;
109 case BPRM_CHECK:
110 iint->ima_bprm_status = status;
111 break;
Matthew Garrettd906c102018-01-08 13:36:20 -0800112 case CREDS_CHECK:
113 iint->ima_creds_status = status;
Gustavo A. R. Silva09186e52019-02-08 14:54:53 -0600114 break;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500115 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500116 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -0500117 iint->ima_file_status = status;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500118 break;
119 case MODULE_CHECK ... MAX_CHECK - 1:
120 default:
121 iint->ima_read_status = status;
122 break;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500123 }
124}
125
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500126static void ima_cache_flags(struct integrity_iint_cache *iint,
127 enum ima_hooks func)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500128{
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +0900129 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500130 case MMAP_CHECK:
131 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
132 break;
133 case BPRM_CHECK:
134 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
135 break;
Matthew Garrettd906c102018-01-08 13:36:20 -0800136 case CREDS_CHECK:
137 iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED);
138 break;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500139 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500140 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -0500141 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500142 break;
143 case MODULE_CHECK ... MAX_CHECK - 1:
144 default:
145 iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
146 break;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500147 }
148}
149
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200150enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
151 int xattr_len)
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300152{
153 struct signature_v2_hdr *sig;
Seth Forsheeb4bfec72016-08-01 08:19:10 -0500154 enum hash_algo ret;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300155
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300156 if (!xattr_value || xattr_len < 2)
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200157 /* return default hash algo */
158 return ima_hash_algo;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300159
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300160 switch (xattr_value->type) {
161 case EVM_IMA_XATTR_DIGSIG:
162 sig = (typeof(sig))xattr_value;
163 if (sig->version != 2 || xattr_len <= sizeof(*sig))
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200164 return ima_hash_algo;
165 return sig->hash_algo;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300166 break;
167 case IMA_XATTR_DIGEST_NG:
Seth Forsheeb4bfec72016-08-01 08:19:10 -0500168 ret = xattr_value->digest[0];
169 if (ret < HASH_ALGO__LAST)
170 return ret;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300171 break;
172 case IMA_XATTR_DIGEST:
173 /* this is for backward compatibility */
174 if (xattr_len == 21) {
175 unsigned int zero = 0;
176 if (!memcmp(&xattr_value->digest[16], &zero, 4))
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200177 return HASH_ALGO_MD5;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300178 else
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200179 return HASH_ALGO_SHA1;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300180 } else if (xattr_len == 17)
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200181 return HASH_ALGO_MD5;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300182 break;
183 }
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200184
185 /* return default hash algo */
186 return ima_hash_algo;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300187}
188
189int ima_read_xattr(struct dentry *dentry,
190 struct evm_ima_xattr_data **xattr_value)
191{
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200192 ssize_t ret;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300193
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200194 ret = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
195 0, GFP_NOFS);
196 if (ret == -EOPNOTSUPP)
197 ret = 0;
198 return ret;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300199}
200
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500201/*
202 * ima_appraise_measurement - appraise file measurement
203 *
204 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
205 * Assuming success, compare the xattr hash with the collected measurement.
206 *
207 * Return 0 on success, error code otherwise
208 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500209int ima_appraise_measurement(enum ima_hooks func,
210 struct integrity_iint_cache *iint,
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300211 struct file *file, const unsigned char *filename,
212 struct evm_ima_xattr_data *xattr_value,
Al Viro6035a272018-06-08 13:40:10 -0400213 int xattr_len)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500214{
Mimi Zohar52a13282013-12-11 14:44:04 -0500215 static const char op[] = "appraise_data";
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300216 const char *cause = "unknown";
Miklos Szeredie71b9df2016-09-16 12:44:20 +0200217 struct dentry *dentry = file_dentry(file);
David Howellsc6f493d2015-03-17 22:26:22 +0000218 struct inode *inode = d_backing_inode(dentry);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500219 enum integrity_status status = INTEGRITY_UNKNOWN;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300220 int rc = xattr_len, hash_start = 0;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500221
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200222 if (!(inode->i_opflags & IOP_XATTR))
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500223 return INTEGRITY_UNKNOWN;
224
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500225 if (rc <= 0) {
226 if (rc && rc != -ENODATA)
227 goto out;
228
Thiago Jung Bauermann915d9d22017-06-07 22:49:12 -0300229 cause = iint->flags & IMA_DIGSIG_REQUIRED ?
230 "IMA-signature-required" : "missing-hash";
Dmitry Kasatkinb151d6b2014-06-27 18:04:27 +0300231 status = INTEGRITY_NOLABEL;
Al Viro6035a272018-06-08 13:40:10 -0400232 if (file->f_mode & FMODE_CREATED)
Dmitry Kasatkinb151d6b2014-06-27 18:04:27 +0300233 iint->flags |= IMA_NEW_FILE;
Daniel Glöckner1ac202e2017-02-24 15:05:14 +0100234 if ((iint->flags & IMA_NEW_FILE) &&
Mimi Zoharb7e27bc2017-11-08 07:38:28 -0500235 (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
236 (inode->i_size == 0)))
Dmitry Kasatkinb151d6b2014-06-27 18:04:27 +0300237 status = INTEGRITY_PASS;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500238 goto out;
239 }
240
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300241 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300242 switch (status) {
243 case INTEGRITY_PASS:
244 case INTEGRITY_PASS_IMMUTABLE:
245 case INTEGRITY_UNKNOWN:
246 break;
247 case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */
248 case INTEGRITY_NOLABEL: /* No security.evm xattr. */
249 cause = "missing-HMAC";
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500250 goto out;
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300251 case INTEGRITY_FAIL: /* Invalid HMAC/signature. */
252 cause = "invalid-HMAC";
253 goto out;
254 default:
255 WARN_ONCE(true, "Unexpected integrity status %d\n", status);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500256 }
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300257
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300258 switch (xattr_value->type) {
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300259 case IMA_XATTR_DIGEST_NG:
260 /* first byte contains algorithm id */
261 hash_start = 1;
Thiago Jung Bauermannbb543e32017-06-07 22:49:10 -0300262 /* fall through */
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300263 case IMA_XATTR_DIGEST:
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300264 if (iint->flags & IMA_DIGSIG_REQUIRED) {
Richard Guy Briggs7e9001f2014-06-16 15:52:07 -0400265 cause = "IMA-signature-required";
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300266 status = INTEGRITY_FAIL;
267 break;
268 }
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200269 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300270 if (xattr_len - sizeof(xattr_value->type) - hash_start >=
271 iint->ima_hash->length)
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300272 /* xattr length may be longer. md5 hash in previous
273 version occupied 20 bytes in xattr, instead of 16
274 */
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300275 rc = memcmp(&xattr_value->digest[hash_start],
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300276 iint->ima_hash->digest,
277 iint->ima_hash->length);
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300278 else
279 rc = -EINVAL;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300280 if (rc) {
281 cause = "invalid-hash";
282 status = INTEGRITY_FAIL;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300283 break;
284 }
285 status = INTEGRITY_PASS;
286 break;
287 case EVM_IMA_XATTR_DIGSIG:
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200288 set_bit(IMA_DIGSIG, &iint->atomic_flags);
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300289 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
Nayna Jaind7cecb62018-12-09 01:57:05 +0530290 (const char *)xattr_value,
291 xattr_len,
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300292 iint->ima_hash->digest,
293 iint->ima_hash->length);
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300294 if (rc == -EOPNOTSUPP) {
295 status = INTEGRITY_UNKNOWN;
Nayna Jaind7cecb62018-12-09 01:57:05 +0530296 break;
297 }
298 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
299 func == KEXEC_KERNEL_CHECK)
300 rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM,
301 (const char *)xattr_value,
302 xattr_len,
303 iint->ima_hash->digest,
304 iint->ima_hash->length);
305 if (rc) {
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300306 cause = "invalid-signature";
307 status = INTEGRITY_FAIL;
308 } else {
309 status = INTEGRITY_PASS;
310 }
311 break;
312 default:
313 status = INTEGRITY_UNKNOWN;
314 cause = "unknown-ima-data";
315 break;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500316 }
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300317
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500318out:
Mimi Zohar57b56ac2018-02-21 11:33:37 -0500319 /*
320 * File signatures on some filesystems can not be properly verified.
Mimi Zohar9e670282018-02-21 11:36:32 -0500321 * When such filesystems are mounted by an untrusted mounter or on a
322 * system not willing to accept such a risk, fail the file signature
323 * verification.
Mimi Zohar57b56ac2018-02-21 11:33:37 -0500324 */
Mimi Zohar9e670282018-02-21 11:36:32 -0500325 if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
326 ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||
327 (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {
Mimi Zohar57b56ac2018-02-21 11:33:37 -0500328 status = INTEGRITY_FAIL;
329 cause = "unverifiable-signature";
330 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
331 op, cause, rc, 0);
332 } else if (status != INTEGRITY_PASS) {
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300333 /* Fix mode, but don't replace file signatures. */
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300334 if ((ima_appraise & IMA_APPRAISE_FIX) &&
335 (!xattr_value ||
336 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
Dmitry Kasatkindef3e8b2012-09-20 22:38:53 +0300337 if (!ima_fix_xattr(dentry, iint))
338 status = INTEGRITY_PASS;
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300339 }
340
341 /* Permit new files with file signatures, but without data. */
342 if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&
343 xattr_value && xattr_value->type == EVM_IMA_XATTR_DIGSIG) {
Mimi Zohar05d1a712016-02-29 19:52:05 -0500344 status = INTEGRITY_PASS;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500345 }
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300346
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500347 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
348 op, cause, rc, 0);
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300349 } else {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500350 ima_cache_flags(iint, func);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500351 }
Mimi Zohar57b56ac2018-02-21 11:33:37 -0500352
Mimi Zohard79d72e2012-12-03 17:08:11 -0500353 ima_set_cache_status(iint, func, status);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500354 return status;
355}
356
357/*
358 * ima_update_xattr - update 'security.ima' hash value
359 */
360void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
361{
Miklos Szeredie71b9df2016-09-16 12:44:20 +0200362 struct dentry *dentry = file_dentry(file);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500363 int rc = 0;
364
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300365 /* do not collect and update hash for digital signatures */
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200366 if (test_bit(IMA_DIGSIG, &iint->atomic_flags))
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300367 return;
368
Mimi Zoharda1b0022016-09-29 10:04:52 -0400369 if ((iint->ima_file_status != INTEGRITY_PASS) &&
370 !(iint->flags & IMA_HASH))
Roberto Sassu020aae32017-11-07 11:37:07 +0100371 return;
372
Mimi Zoharcf222212016-01-14 17:57:47 -0500373 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500374 if (rc < 0)
375 return;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300376
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200377 inode_lock(file_inode(file));
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500378 ima_fix_xattr(dentry, iint);
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200379 inode_unlock(file_inode(file));
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500380}
381
382/**
383 * ima_inode_post_setattr - reflect file metadata changes
384 * @dentry: pointer to the affected dentry
385 *
386 * Changes to a dentry's metadata might result in needing to appraise.
387 *
388 * This function is called from notify_change(), which expects the caller
389 * to lock the inode's i_mutex.
390 */
391void ima_inode_post_setattr(struct dentry *dentry)
392{
David Howellsc6f493d2015-03-17 22:26:22 +0000393 struct inode *inode = d_backing_inode(dentry);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500394 struct integrity_iint_cache *iint;
Mimi Zoharda1b0022016-09-29 10:04:52 -0400395 int action;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500396
Roberto Sassua7560242014-09-12 19:35:54 +0200397 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200398 || !(inode->i_opflags & IOP_XATTR))
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500399 return;
400
Mimi Zoharda1b0022016-09-29 10:04:52 -0400401 action = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
402 if (!action)
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200403 __vfs_removexattr(dentry, XATTR_NAME_IMA);
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200404 iint = integrity_iint_find(inode);
405 if (iint) {
406 set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);
Mimi Zoharda1b0022016-09-29 10:04:52 -0400407 if (!action)
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200408 clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
409 }
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500410}
Mimi Zohar42c63332011-03-10 18:54:15 -0500411
412/*
413 * ima_protect_xattr - protect 'security.ima'
414 *
415 * Ensure that not just anyone can modify or remove 'security.ima'.
416 */
417static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
418 const void *xattr_value, size_t xattr_value_len)
419{
420 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
421 if (!capable(CAP_SYS_ADMIN))
422 return -EPERM;
423 return 1;
424 }
425 return 0;
426}
427
Mimi Zohar060bdebfb2014-03-17 23:24:18 -0400428static void ima_reset_appraise_flags(struct inode *inode, int digsig)
Mimi Zohar42c63332011-03-10 18:54:15 -0500429{
430 struct integrity_iint_cache *iint;
431
Roberto Sassua7560242014-09-12 19:35:54 +0200432 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
Mimi Zohar42c63332011-03-10 18:54:15 -0500433 return;
434
435 iint = integrity_iint_find(inode);
436 if (!iint)
437 return;
Eric Richtera4226382016-06-01 13:14:06 -0500438 iint->measured_pcrs = 0;
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200439 set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);
Mimi Zohar060bdebfb2014-03-17 23:24:18 -0400440 if (digsig)
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200441 set_bit(IMA_DIGSIG, &iint->atomic_flags);
442 else
443 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
Mimi Zohar42c63332011-03-10 18:54:15 -0500444}
445
446int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
447 const void *xattr_value, size_t xattr_value_len)
448{
Mimi Zohar060bdebfb2014-03-17 23:24:18 -0400449 const struct evm_ima_xattr_data *xvalue = xattr_value;
Mimi Zohar42c63332011-03-10 18:54:15 -0500450 int result;
451
452 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
453 xattr_value_len);
454 if (result == 1) {
Dmitry Kasatkina48fda92014-10-28 13:31:22 +0200455 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
456 return -EINVAL;
Mimi Zoharf5acb3d2016-11-02 09:14:16 -0400457 ima_reset_appraise_flags(d_backing_inode(dentry),
Thiago Jung Bauermanne5729f82017-10-17 22:53:14 -0200458 xvalue->type == EVM_IMA_XATTR_DIGSIG);
Mimi Zohar42c63332011-03-10 18:54:15 -0500459 result = 0;
460 }
461 return result;
462}
463
464int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
465{
466 int result;
467
468 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
469 if (result == 1) {
David Howellsc6f493d2015-03-17 22:26:22 +0000470 ima_reset_appraise_flags(d_backing_inode(dentry), 0);
Mimi Zohar42c63332011-03-10 18:54:15 -0500471 result = 0;
472 }
473 return result;
474}