blob: 809ba70fbbbfb3137339895db07301b39662d5a1 [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{
Mimi Zohare1f5e012017-04-24 22:06:49 -040023#ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050024 if (strncmp(str, "off", 3) == 0)
25 ima_appraise = 0;
Dmitry Kasatkin2faa6ef2014-05-08 13:11:29 +030026 else if (strncmp(str, "log", 3) == 0)
27 ima_appraise = IMA_APPRAISE_LOG;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050028 else if (strncmp(str, "fix", 3) == 0)
29 ima_appraise = IMA_APPRAISE_FIX;
Mimi Zohare1f5e012017-04-24 22:06:49 -040030#endif
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050031 return 1;
32}
33
34__setup("ima_appraise=", default_appraise_setup);
35
36/*
Mimi Zohar6f6723e2017-04-24 22:43:52 -040037 * is_ima_appraise_enabled - return appraise status
38 *
39 * Only return enabled, if not in ima_appraise="fix" or "log" modes.
40 */
41bool is_ima_appraise_enabled(void)
42{
43 return (ima_appraise & IMA_APPRAISE_ENFORCE) ? 1 : 0;
44}
45
46/*
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050047 * ima_must_appraise - set appraise flag
48 *
49 * Return 1 to appraise
50 */
Dmitry Kasatkind26e19362012-09-27 18:26:53 +030051int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050052{
Mimi Zohar07f6a792011-03-09 22:25:48 -050053 if (!ima_appraise)
54 return 0;
55
Eric Richter725de7f2016-06-01 13:14:02 -050056 return ima_match_policy(inode, func, mask, IMA_APPRAISE, NULL);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050057}
58
Dmitry Kasatkindef3e8b2012-09-20 22:38:53 +030059static int ima_fix_xattr(struct dentry *dentry,
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +030060 struct integrity_iint_cache *iint)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050061{
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +030062 int rc, offset;
63 u8 algo = iint->ima_hash->algo;
64
65 if (algo <= HASH_ALGO_SHA1) {
66 offset = 1;
67 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
68 } else {
69 offset = 0;
70 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
71 iint->ima_hash->xattr.ng.algo = algo;
72 }
73 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
74 &iint->ima_hash->xattr.data[offset],
75 (sizeof(iint->ima_hash->xattr) - offset) +
76 iint->ima_hash->length, 0);
77 return rc;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050078}
79
Mimi Zohard79d72e2012-12-03 17:08:11 -050080/* Return specific func appraised cached result */
81enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
Mimi Zohar4ad87a32016-01-14 20:59:14 -050082 enum ima_hooks func)
Mimi Zohard79d72e2012-12-03 17:08:11 -050083{
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +090084 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -050085 case MMAP_CHECK:
86 return iint->ima_mmap_status;
87 case BPRM_CHECK:
88 return iint->ima_bprm_status;
Mimi Zohard79d72e2012-12-03 17:08:11 -050089 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -050090 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -050091 return iint->ima_file_status;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -050092 case MODULE_CHECK ... MAX_CHECK - 1:
93 default:
94 return iint->ima_read_status;
Mimi Zohard79d72e2012-12-03 17:08:11 -050095 }
96}
97
98static void ima_set_cache_status(struct integrity_iint_cache *iint,
Mimi Zohar4ad87a32016-01-14 20:59:14 -050099 enum ima_hooks func,
100 enum integrity_status status)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500101{
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +0900102 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500103 case MMAP_CHECK:
104 iint->ima_mmap_status = status;
105 break;
106 case BPRM_CHECK:
107 iint->ima_bprm_status = status;
108 break;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500109 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500110 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -0500111 iint->ima_file_status = status;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500112 break;
113 case MODULE_CHECK ... MAX_CHECK - 1:
114 default:
115 iint->ima_read_status = status;
116 break;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500117 }
118}
119
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500120static void ima_cache_flags(struct integrity_iint_cache *iint,
121 enum ima_hooks func)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500122{
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +0900123 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500124 case MMAP_CHECK:
125 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
126 break;
127 case BPRM_CHECK:
128 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
129 break;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500130 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500131 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -0500132 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500133 break;
134 case MODULE_CHECK ... MAX_CHECK - 1:
135 default:
136 iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
137 break;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500138 }
139}
140
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200141enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
142 int xattr_len)
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300143{
144 struct signature_v2_hdr *sig;
Seth Forsheeb4bfec72016-08-01 08:19:10 -0500145 enum hash_algo ret;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300146
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300147 if (!xattr_value || xattr_len < 2)
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200148 /* return default hash algo */
149 return ima_hash_algo;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300150
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300151 switch (xattr_value->type) {
152 case EVM_IMA_XATTR_DIGSIG:
153 sig = (typeof(sig))xattr_value;
154 if (sig->version != 2 || xattr_len <= sizeof(*sig))
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200155 return ima_hash_algo;
156 return sig->hash_algo;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300157 break;
158 case IMA_XATTR_DIGEST_NG:
Seth Forsheeb4bfec72016-08-01 08:19:10 -0500159 ret = xattr_value->digest[0];
160 if (ret < HASH_ALGO__LAST)
161 return ret;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300162 break;
163 case IMA_XATTR_DIGEST:
164 /* this is for backward compatibility */
165 if (xattr_len == 21) {
166 unsigned int zero = 0;
167 if (!memcmp(&xattr_value->digest[16], &zero, 4))
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200168 return HASH_ALGO_MD5;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300169 else
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200170 return HASH_ALGO_SHA1;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300171 } else if (xattr_len == 17)
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200172 return HASH_ALGO_MD5;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300173 break;
174 }
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200175
176 /* return default hash algo */
177 return ima_hash_algo;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300178}
179
180int ima_read_xattr(struct dentry *dentry,
181 struct evm_ima_xattr_data **xattr_value)
182{
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200183 ssize_t ret;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300184
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200185 ret = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
186 0, GFP_NOFS);
187 if (ret == -EOPNOTSUPP)
188 ret = 0;
189 return ret;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300190}
191
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500192/*
193 * ima_appraise_measurement - appraise file measurement
194 *
195 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
196 * Assuming success, compare the xattr hash with the collected measurement.
197 *
198 * Return 0 on success, error code otherwise
199 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500200int ima_appraise_measurement(enum ima_hooks func,
201 struct integrity_iint_cache *iint,
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300202 struct file *file, const unsigned char *filename,
203 struct evm_ima_xattr_data *xattr_value,
Dmitry Kasatkin3034a142014-06-27 18:15:44 +0300204 int xattr_len, int opened)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500205{
Mimi Zohar52a13282013-12-11 14:44:04 -0500206 static const char op[] = "appraise_data";
207 char *cause = "unknown";
Miklos Szeredie71b9df2016-09-16 12:44:20 +0200208 struct dentry *dentry = file_dentry(file);
David Howellsc6f493d2015-03-17 22:26:22 +0000209 struct inode *inode = d_backing_inode(dentry);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500210 enum integrity_status status = INTEGRITY_UNKNOWN;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300211 int rc = xattr_len, hash_start = 0;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500212
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200213 if (!(inode->i_opflags & IOP_XATTR))
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500214 return INTEGRITY_UNKNOWN;
215
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500216 if (rc <= 0) {
217 if (rc && rc != -ENODATA)
218 goto out;
219
Thiago Jung Bauermann915d9d22017-06-07 22:49:12 -0300220 cause = iint->flags & IMA_DIGSIG_REQUIRED ?
221 "IMA-signature-required" : "missing-hash";
Dmitry Kasatkinb151d6b2014-06-27 18:04:27 +0300222 status = INTEGRITY_NOLABEL;
Daniel Glöckner1ac202e2017-02-24 15:05:14 +0100223 if (opened & FILE_CREATED)
Dmitry Kasatkinb151d6b2014-06-27 18:04:27 +0300224 iint->flags |= IMA_NEW_FILE;
Daniel Glöckner1ac202e2017-02-24 15:05:14 +0100225 if ((iint->flags & IMA_NEW_FILE) &&
226 !(iint->flags & IMA_DIGSIG_REQUIRED))
Dmitry Kasatkinb151d6b2014-06-27 18:04:27 +0300227 status = INTEGRITY_PASS;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500228 goto out;
229 }
230
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300231 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500232 if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) {
233 if ((status == INTEGRITY_NOLABEL)
234 || (status == INTEGRITY_NOXATTRS))
235 cause = "missing-HMAC";
236 else if (status == INTEGRITY_FAIL)
237 cause = "invalid-HMAC";
238 goto out;
239 }
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300240 switch (xattr_value->type) {
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300241 case IMA_XATTR_DIGEST_NG:
242 /* first byte contains algorithm id */
243 hash_start = 1;
Thiago Jung Bauermannbb543e32017-06-07 22:49:10 -0300244 /* fall through */
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300245 case IMA_XATTR_DIGEST:
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300246 if (iint->flags & IMA_DIGSIG_REQUIRED) {
Richard Guy Briggs7e9001f2014-06-16 15:52:07 -0400247 cause = "IMA-signature-required";
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300248 status = INTEGRITY_FAIL;
249 break;
250 }
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300251 if (xattr_len - sizeof(xattr_value->type) - hash_start >=
252 iint->ima_hash->length)
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300253 /* xattr length may be longer. md5 hash in previous
254 version occupied 20 bytes in xattr, instead of 16
255 */
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300256 rc = memcmp(&xattr_value->digest[hash_start],
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300257 iint->ima_hash->digest,
258 iint->ima_hash->length);
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300259 else
260 rc = -EINVAL;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300261 if (rc) {
262 cause = "invalid-hash";
263 status = INTEGRITY_FAIL;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300264 break;
265 }
266 status = INTEGRITY_PASS;
267 break;
268 case EVM_IMA_XATTR_DIGSIG:
269 iint->flags |= IMA_DIGSIG;
270 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
Dmitry Kasatkinb1aaab22013-10-10 16:12:03 +0900271 (const char *)xattr_value, rc,
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300272 iint->ima_hash->digest,
273 iint->ima_hash->length);
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300274 if (rc == -EOPNOTSUPP) {
275 status = INTEGRITY_UNKNOWN;
276 } else if (rc) {
277 cause = "invalid-signature";
278 status = INTEGRITY_FAIL;
279 } else {
280 status = INTEGRITY_PASS;
281 }
282 break;
283 default:
284 status = INTEGRITY_UNKNOWN;
285 cause = "unknown-ima-data";
286 break;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500287 }
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300288
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500289out:
290 if (status != INTEGRITY_PASS) {
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300291 if ((ima_appraise & IMA_APPRAISE_FIX) &&
292 (!xattr_value ||
293 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
Dmitry Kasatkindef3e8b2012-09-20 22:38:53 +0300294 if (!ima_fix_xattr(dentry, iint))
295 status = INTEGRITY_PASS;
Mimi Zohar05d1a712016-02-29 19:52:05 -0500296 } else if ((inode->i_size == 0) &&
297 (iint->flags & IMA_NEW_FILE) &&
298 (xattr_value &&
299 xattr_value->type == EVM_IMA_XATTR_DIGSIG)) {
300 status = INTEGRITY_PASS;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500301 }
302 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
303 op, cause, rc, 0);
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300304 } else {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500305 ima_cache_flags(iint, func);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500306 }
Mimi Zohard79d72e2012-12-03 17:08:11 -0500307 ima_set_cache_status(iint, func, status);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500308 return status;
309}
310
311/*
312 * ima_update_xattr - update 'security.ima' hash value
313 */
314void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
315{
Miklos Szeredie71b9df2016-09-16 12:44:20 +0200316 struct dentry *dentry = file_dentry(file);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500317 int rc = 0;
318
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300319 /* do not collect and update hash for digital signatures */
320 if (iint->flags & IMA_DIGSIG)
321 return;
322
Mimi Zoharcf222212016-01-14 17:57:47 -0500323 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500324 if (rc < 0)
325 return;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300326
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500327 ima_fix_xattr(dentry, iint);
328}
329
330/**
331 * ima_inode_post_setattr - reflect file metadata changes
332 * @dentry: pointer to the affected dentry
333 *
334 * Changes to a dentry's metadata might result in needing to appraise.
335 *
336 * This function is called from notify_change(), which expects the caller
337 * to lock the inode's i_mutex.
338 */
339void ima_inode_post_setattr(struct dentry *dentry)
340{
David Howellsc6f493d2015-03-17 22:26:22 +0000341 struct inode *inode = d_backing_inode(dentry);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500342 struct integrity_iint_cache *iint;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200343 int must_appraise;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500344
Roberto Sassua7560242014-09-12 19:35:54 +0200345 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200346 || !(inode->i_opflags & IOP_XATTR))
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500347 return;
348
349 must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
350 iint = integrity_iint_find(inode);
351 if (iint) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500352 iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED |
353 IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK |
Mimi Zohar42a4c602016-02-29 08:30:12 -0500354 IMA_ACTION_RULE_FLAGS);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500355 if (must_appraise)
356 iint->flags |= IMA_APPRAISE;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500357 }
358 if (!must_appraise)
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200359 __vfs_removexattr(dentry, XATTR_NAME_IMA);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500360}
Mimi Zohar42c63332011-03-10 18:54:15 -0500361
362/*
363 * ima_protect_xattr - protect 'security.ima'
364 *
365 * Ensure that not just anyone can modify or remove 'security.ima'.
366 */
367static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
368 const void *xattr_value, size_t xattr_value_len)
369{
370 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
371 if (!capable(CAP_SYS_ADMIN))
372 return -EPERM;
373 return 1;
374 }
375 return 0;
376}
377
Mimi Zohar060bdebfb2014-03-17 23:24:18 -0400378static void ima_reset_appraise_flags(struct inode *inode, int digsig)
Mimi Zohar42c63332011-03-10 18:54:15 -0500379{
380 struct integrity_iint_cache *iint;
381
Roberto Sassua7560242014-09-12 19:35:54 +0200382 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
Mimi Zohar42c63332011-03-10 18:54:15 -0500383 return;
384
385 iint = integrity_iint_find(inode);
386 if (!iint)
387 return;
388
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300389 iint->flags &= ~IMA_DONE_MASK;
Eric Richtera4226382016-06-01 13:14:06 -0500390 iint->measured_pcrs = 0;
Mimi Zohar060bdebfb2014-03-17 23:24:18 -0400391 if (digsig)
392 iint->flags |= IMA_DIGSIG;
Mimi Zohar42c63332011-03-10 18:54:15 -0500393 return;
394}
395
396int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
397 const void *xattr_value, size_t xattr_value_len)
398{
Mimi Zohar060bdebfb2014-03-17 23:24:18 -0400399 const struct evm_ima_xattr_data *xvalue = xattr_value;
Mimi Zohar42c63332011-03-10 18:54:15 -0500400 int result;
401
402 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
403 xattr_value_len);
404 if (result == 1) {
Dmitry Kasatkina48fda92014-10-28 13:31:22 +0200405 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
406 return -EINVAL;
Mimi Zoharf5acb3d2016-11-02 09:14:16 -0400407 ima_reset_appraise_flags(d_backing_inode(dentry),
408 (xvalue->type == EVM_IMA_XATTR_DIGSIG) ? 1 : 0);
Mimi Zohar42c63332011-03-10 18:54:15 -0500409 result = 0;
410 }
411 return result;
412}
413
414int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
415{
416 int result;
417
418 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
419 if (result == 1) {
David Howellsc6f493d2015-03-17 22:26:22 +0000420 ima_reset_appraise_flags(d_backing_inode(dentry), 0);
Mimi Zohar42c63332011-03-10 18:54:15 -0500421 result = 0;
422 }
423 return result;
424}