blob: 17232bbfb9f96c541804e0c0e0a2ff45f73943f1 [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 */
Ard Biesheuvelb000d5c2020-10-13 10:18:04 +02008#include <linux/module.h>
Paul Gortmaker876979c2018-12-09 15:36:29 -05009#include <linux/init.h>
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050010#include <linux/file.h>
11#include <linux/fs.h>
12#include <linux/xattr.h>
13#include <linux/magic.h>
14#include <linux/ima.h>
15#include <linux/evm.h>
Nayna Jain273df862019-10-30 23:31:32 -040016#include <keys/system_keyring.h>
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050017
18#include "ima.h"
19
Mimi Zohare1f5e012017-04-24 22:06:49 -040020#ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
Ard Biesheuvelb000d5c2020-10-13 10:18:04 +020021static char *ima_appraise_cmdline_default __initdata;
22core_param(ima_appraise, ima_appraise_cmdline_default, charp, 0);
23
24void __init ima_appraise_parse_cmdline(void)
25{
26 const char *str = ima_appraise_cmdline_default;
Bruno Meneguelee4d7e2d2020-09-04 22:20:20 -030027 bool sb_state = arch_ima_get_secureboot();
28 int appraisal_state = ima_appraise;
Bruno Meneguele311aa6a2020-07-13 13:48:30 -030029
Ard Biesheuvelb000d5c2020-10-13 10:18:04 +020030 if (!str)
31 return;
32
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050033 if (strncmp(str, "off", 3) == 0)
Bruno Meneguelee4d7e2d2020-09-04 22:20:20 -030034 appraisal_state = 0;
Dmitry Kasatkin2faa6ef2014-05-08 13:11:29 +030035 else if (strncmp(str, "log", 3) == 0)
Bruno Meneguelee4d7e2d2020-09-04 22:20:20 -030036 appraisal_state = IMA_APPRAISE_LOG;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050037 else if (strncmp(str, "fix", 3) == 0)
Bruno Meneguelee4d7e2d2020-09-04 22:20:20 -030038 appraisal_state = IMA_APPRAISE_FIX;
Bruno Meneguele4afb28a2020-09-04 16:40:57 -030039 else if (strncmp(str, "enforce", 7) == 0)
Bruno Meneguelee4d7e2d2020-09-04 22:20:20 -030040 appraisal_state = IMA_APPRAISE_ENFORCE;
Bruno Meneguele7fe2bb72020-09-04 16:40:58 -030041 else
42 pr_err("invalid \"%s\" appraise option", str);
Bruno Meneguelee4d7e2d2020-09-04 22:20:20 -030043
44 /* If appraisal state was changed, but secure boot is enabled,
45 * keep its default */
46 if (sb_state) {
47 if (!(appraisal_state & IMA_APPRAISE_ENFORCE))
48 pr_info("Secure boot enabled: ignoring ima_appraise=%s option",
49 str);
50 } else {
51 ima_appraise = appraisal_state;
52 }
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050053}
Ard Biesheuvelb000d5c2020-10-13 10:18:04 +020054#endif
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050055
56/*
Mimi Zohar6f6723e2017-04-24 22:43:52 -040057 * is_ima_appraise_enabled - return appraise status
58 *
59 * Only return enabled, if not in ima_appraise="fix" or "log" modes.
60 */
61bool is_ima_appraise_enabled(void)
62{
Thiago Jung Bauermanne5729f82017-10-17 22:53:14 -020063 return ima_appraise & IMA_APPRAISE_ENFORCE;
Mimi Zohar6f6723e2017-04-24 22:43:52 -040064}
65
66/*
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050067 * ima_must_appraise - set appraise flag
68 *
Mimi Zoharda1b0022016-09-29 10:04:52 -040069 * Return 1 to appraise or hash
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050070 */
Christian Braunera2d23292021-01-21 14:19:45 +010071int ima_must_appraise(struct user_namespace *mnt_userns, struct inode *inode,
72 int mask, enum ima_hooks func)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050073{
Matthew Garrettd906c102018-01-08 13:36:20 -080074 u32 secid;
75
Mimi Zohar07f6a792011-03-09 22:25:48 -050076 if (!ima_appraise)
77 return 0;
78
Paul Moore63269482021-09-29 11:01:21 -040079 security_current_getsecid_subj(&secid);
THOBY Simon1624dc02021-08-16 08:11:00 +000080 return ima_match_policy(mnt_userns, inode, current_cred(), secid,
81 func, mask, IMA_APPRAISE | IMA_HASH, NULL,
82 NULL, NULL, NULL);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050083}
84
Dmitry Kasatkindef3e8b2012-09-20 22:38:53 +030085static int ima_fix_xattr(struct dentry *dentry,
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +030086 struct integrity_iint_cache *iint)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050087{
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +030088 int rc, offset;
89 u8 algo = iint->ima_hash->algo;
90
91 if (algo <= HASH_ALGO_SHA1) {
92 offset = 1;
93 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
94 } else {
95 offset = 0;
96 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
97 iint->ima_hash->xattr.ng.algo = algo;
98 }
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +010099 rc = __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_IMA,
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300100 &iint->ima_hash->xattr.data[offset],
101 (sizeof(iint->ima_hash->xattr) - offset) +
102 iint->ima_hash->length, 0);
103 return rc;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500104}
105
Mimi Zohard79d72e2012-12-03 17:08:11 -0500106/* Return specific func appraised cached result */
107enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500108 enum ima_hooks func)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500109{
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +0900110 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500111 case MMAP_CHECK:
112 return iint->ima_mmap_status;
113 case BPRM_CHECK:
114 return iint->ima_bprm_status;
Matthew Garrettd906c102018-01-08 13:36:20 -0800115 case CREDS_CHECK:
116 return iint->ima_creds_status;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500117 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500118 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -0500119 return iint->ima_file_status;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500120 case MODULE_CHECK ... MAX_CHECK - 1:
121 default:
122 return iint->ima_read_status;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500123 }
124}
125
126static void ima_set_cache_status(struct integrity_iint_cache *iint,
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500127 enum ima_hooks func,
128 enum integrity_status status)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500129{
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +0900130 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500131 case MMAP_CHECK:
132 iint->ima_mmap_status = status;
133 break;
134 case BPRM_CHECK:
135 iint->ima_bprm_status = status;
136 break;
Matthew Garrettd906c102018-01-08 13:36:20 -0800137 case CREDS_CHECK:
138 iint->ima_creds_status = status;
Gustavo A. R. Silva09186e52019-02-08 14:54:53 -0600139 break;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500140 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500141 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -0500142 iint->ima_file_status = status;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500143 break;
144 case MODULE_CHECK ... MAX_CHECK - 1:
145 default:
146 iint->ima_read_status = status;
147 break;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500148 }
149}
150
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500151static void ima_cache_flags(struct integrity_iint_cache *iint,
152 enum ima_hooks func)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500153{
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +0900154 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500155 case MMAP_CHECK:
156 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
157 break;
158 case BPRM_CHECK:
159 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
160 break;
Matthew Garrettd906c102018-01-08 13:36:20 -0800161 case CREDS_CHECK:
162 iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED);
163 break;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500164 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500165 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -0500166 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500167 break;
168 case MODULE_CHECK ... MAX_CHECK - 1:
169 default:
170 iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
171 break;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500172 }
173}
174
THOBY Simon50f742d2021-08-16 08:10:59 +0000175enum hash_algo ima_get_hash_algo(const struct evm_ima_xattr_data *xattr_value,
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200176 int xattr_len)
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300177{
178 struct signature_v2_hdr *sig;
Seth Forsheeb4bfec72016-08-01 08:19:10 -0500179 enum hash_algo ret;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300180
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300181 if (!xattr_value || xattr_len < 2)
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200182 /* return default hash algo */
183 return ima_hash_algo;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300184
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300185 switch (xattr_value->type) {
186 case EVM_IMA_XATTR_DIGSIG:
187 sig = (typeof(sig))xattr_value;
THOBY Simoncb181da2021-08-22 08:55:26 +0000188 if (sig->version != 2 || xattr_len <= sizeof(*sig)
189 || sig->hash_algo >= HASH_ALGO__LAST)
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200190 return ima_hash_algo;
191 return sig->hash_algo;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300192 break;
193 case IMA_XATTR_DIGEST_NG:
Thiago Jung Bauermann650b29d2019-06-11 03:28:08 -0300194 /* first byte contains algorithm id */
195 ret = xattr_value->data[0];
Seth Forsheeb4bfec72016-08-01 08:19:10 -0500196 if (ret < HASH_ALGO__LAST)
197 return ret;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300198 break;
199 case IMA_XATTR_DIGEST:
200 /* this is for backward compatibility */
201 if (xattr_len == 21) {
202 unsigned int zero = 0;
Thiago Jung Bauermann650b29d2019-06-11 03:28:08 -0300203 if (!memcmp(&xattr_value->data[16], &zero, 4))
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200204 return HASH_ALGO_MD5;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300205 else
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200206 return HASH_ALGO_SHA1;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300207 } else if (xattr_len == 17)
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200208 return HASH_ALGO_MD5;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300209 break;
210 }
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200211
212 /* return default hash algo */
213 return ima_hash_algo;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300214}
215
216int ima_read_xattr(struct dentry *dentry,
217 struct evm_ima_xattr_data **xattr_value)
218{
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200219 ssize_t ret;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300220
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +0100221 ret = vfs_getxattr_alloc(&init_user_ns, dentry, XATTR_NAME_IMA,
222 (char **)xattr_value, 0, GFP_NOFS);
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200223 if (ret == -EOPNOTSUPP)
224 ret = 0;
225 return ret;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300226}
227
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500228/*
Thiago Jung Bauermanna5fbeb62019-06-27 23:19:29 -0300229 * xattr_verify - verify xattr digest or signature
230 *
231 * Verify whether the hash or signature matches the file contents.
232 *
233 * Return 0 on success, error code otherwise.
234 */
235static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint,
236 struct evm_ima_xattr_data *xattr_value, int xattr_len,
237 enum integrity_status *status, const char **cause)
238{
239 int rc = -EINVAL, hash_start = 0;
240
241 switch (xattr_value->type) {
242 case IMA_XATTR_DIGEST_NG:
243 /* first byte contains algorithm id */
244 hash_start = 1;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500245 fallthrough;
Thiago Jung Bauermanna5fbeb62019-06-27 23:19:29 -0300246 case IMA_XATTR_DIGEST:
Roberto Sassu7aa57832021-05-14 17:27:51 +0200247 if (*status != INTEGRITY_PASS_IMMUTABLE) {
248 if (iint->flags & IMA_DIGSIG_REQUIRED) {
249 *cause = "IMA-signature-required";
250 *status = INTEGRITY_FAIL;
251 break;
252 }
253 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
254 } else {
255 set_bit(IMA_DIGSIG, &iint->atomic_flags);
Thiago Jung Bauermanna5fbeb62019-06-27 23:19:29 -0300256 }
Thiago Jung Bauermanna5fbeb62019-06-27 23:19:29 -0300257 if (xattr_len - sizeof(xattr_value->type) - hash_start >=
258 iint->ima_hash->length)
259 /*
260 * xattr length may be longer. md5 hash in previous
261 * version occupied 20 bytes in xattr, instead of 16
262 */
263 rc = memcmp(&xattr_value->data[hash_start],
264 iint->ima_hash->digest,
265 iint->ima_hash->length);
266 else
267 rc = -EINVAL;
268 if (rc) {
269 *cause = "invalid-hash";
270 *status = INTEGRITY_FAIL;
271 break;
272 }
273 *status = INTEGRITY_PASS;
274 break;
275 case EVM_IMA_XATTR_DIGSIG:
276 set_bit(IMA_DIGSIG, &iint->atomic_flags);
277 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
278 (const char *)xattr_value,
279 xattr_len,
280 iint->ima_hash->digest,
281 iint->ima_hash->length);
282 if (rc == -EOPNOTSUPP) {
283 *status = INTEGRITY_UNKNOWN;
284 break;
285 }
286 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
287 func == KEXEC_KERNEL_CHECK)
288 rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM,
289 (const char *)xattr_value,
290 xattr_len,
291 iint->ima_hash->digest,
292 iint->ima_hash->length);
293 if (rc) {
294 *cause = "invalid-signature";
295 *status = INTEGRITY_FAIL;
296 } else {
297 *status = INTEGRITY_PASS;
298 }
299 break;
300 default:
301 *status = INTEGRITY_UNKNOWN;
302 *cause = "unknown-ima-data";
303 break;
304 }
305
306 return rc;
307}
308
309/*
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -0300310 * modsig_verify - verify modsig signature
311 *
312 * Verify whether the signature matches the file contents.
313 *
314 * Return 0 on success, error code otherwise.
315 */
316static int modsig_verify(enum ima_hooks func, const struct modsig *modsig,
317 enum integrity_status *status, const char **cause)
318{
319 int rc;
320
321 rc = integrity_modsig_verify(INTEGRITY_KEYRING_IMA, modsig);
322 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
323 func == KEXEC_KERNEL_CHECK)
324 rc = integrity_modsig_verify(INTEGRITY_KEYRING_PLATFORM,
325 modsig);
326 if (rc) {
327 *cause = "invalid-signature";
328 *status = INTEGRITY_FAIL;
329 } else {
330 *status = INTEGRITY_PASS;
331 }
332
333 return rc;
334}
335
336/*
Nayna Jain273df862019-10-30 23:31:32 -0400337 * ima_check_blacklist - determine if the binary is blacklisted.
338 *
339 * Add the hash of the blacklisted binary to the measurement list, based
340 * on policy.
341 *
342 * Returns -EPERM if the hash is blacklisted.
343 */
344int ima_check_blacklist(struct integrity_iint_cache *iint,
345 const struct modsig *modsig, int pcr)
346{
347 enum hash_algo hash_algo;
348 const u8 *digest = NULL;
349 u32 digestsize = 0;
350 int rc = 0;
351
352 if (!(iint->flags & IMA_CHECK_BLACKLIST))
353 return 0;
354
355 if (iint->flags & IMA_MODSIG_ALLOWED && modsig) {
356 ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize);
357
358 rc = is_binary_blacklisted(digest, digestsize);
359 if ((rc == -EPERM) && (iint->flags & IMA_MEASURE))
Christian Braunera2d23292021-01-21 14:19:45 +0100360 process_buffer_measurement(&init_user_ns, NULL, digest, digestsize,
Nayna Jain273df862019-10-30 23:31:32 -0400361 "blacklisted-hash", NONE,
Roberto Sassuca3c9bd2021-07-23 10:53:04 +0200362 pcr, NULL, false, NULL, 0);
Nayna Jain273df862019-10-30 23:31:32 -0400363 }
364
365 return rc;
366}
367
368/*
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500369 * ima_appraise_measurement - appraise file measurement
370 *
371 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
372 * Assuming success, compare the xattr hash with the collected measurement.
373 *
374 * Return 0 on success, error code otherwise
375 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500376int ima_appraise_measurement(enum ima_hooks func,
377 struct integrity_iint_cache *iint,
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300378 struct file *file, const unsigned char *filename,
379 struct evm_ima_xattr_data *xattr_value,
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -0300380 int xattr_len, const struct modsig *modsig)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500381{
Mimi Zohar52a13282013-12-11 14:44:04 -0500382 static const char op[] = "appraise_data";
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300383 const char *cause = "unknown";
Miklos Szeredie71b9df2016-09-16 12:44:20 +0200384 struct dentry *dentry = file_dentry(file);
David Howellsc6f493d2015-03-17 22:26:22 +0000385 struct inode *inode = d_backing_inode(dentry);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500386 enum integrity_status status = INTEGRITY_UNKNOWN;
Thiago Jung Bauermanna5fbeb62019-06-27 23:19:29 -0300387 int rc = xattr_len;
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -0300388 bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500389
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -0300390 /* If not appraising a modsig, we need an xattr. */
391 if (!(inode->i_opflags & IOP_XATTR) && !try_modsig)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500392 return INTEGRITY_UNKNOWN;
393
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -0300394 /* If reading the xattr failed and there's no modsig, error out. */
395 if (rc <= 0 && !try_modsig) {
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500396 if (rc && rc != -ENODATA)
397 goto out;
398
Thiago Jung Bauermann915d9d22017-06-07 22:49:12 -0300399 cause = iint->flags & IMA_DIGSIG_REQUIRED ?
400 "IMA-signature-required" : "missing-hash";
Dmitry Kasatkinb151d6b2014-06-27 18:04:27 +0300401 status = INTEGRITY_NOLABEL;
Al Viro6035a272018-06-08 13:40:10 -0400402 if (file->f_mode & FMODE_CREATED)
Dmitry Kasatkinb151d6b2014-06-27 18:04:27 +0300403 iint->flags |= IMA_NEW_FILE;
Daniel Glöckner1ac202e2017-02-24 15:05:14 +0100404 if ((iint->flags & IMA_NEW_FILE) &&
Mimi Zoharb7e27bc2017-11-08 07:38:28 -0500405 (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
406 (inode->i_size == 0)))
Dmitry Kasatkinb151d6b2014-06-27 18:04:27 +0300407 status = INTEGRITY_PASS;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500408 goto out;
409 }
410
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300411 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300412 switch (status) {
413 case INTEGRITY_PASS:
414 case INTEGRITY_PASS_IMMUTABLE:
415 case INTEGRITY_UNKNOWN:
416 break;
417 case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -0300418 /* It's fine not to have xattrs when using a modsig. */
419 if (try_modsig)
420 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500421 fallthrough;
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300422 case INTEGRITY_NOLABEL: /* No security.evm xattr. */
423 cause = "missing-HMAC";
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500424 goto out;
Roberto Sassucdef6852021-05-14 17:27:47 +0200425 case INTEGRITY_FAIL_IMMUTABLE:
Roberto Sassu7aa57832021-05-14 17:27:51 +0200426 set_bit(IMA_DIGSIG, &iint->atomic_flags);
Mimi Zohar55748ac62021-06-02 16:33:39 -0400427 cause = "invalid-fail-immutable";
428 goto out;
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300429 case INTEGRITY_FAIL: /* Invalid HMAC/signature. */
430 cause = "invalid-HMAC";
431 goto out;
432 default:
433 WARN_ONCE(true, "Unexpected integrity status %d\n", status);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500434 }
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300435
Thiago Jung Bauermanna5fbeb62019-06-27 23:19:29 -0300436 if (xattr_value)
437 rc = xattr_verify(func, iint, xattr_value, xattr_len, &status,
438 &cause);
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300439
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -0300440 /*
441 * If we have a modsig and either no imasig or the imasig's key isn't
442 * known, then try verifying the modsig.
443 */
444 if (try_modsig &&
445 (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG ||
446 rc == -ENOKEY))
447 rc = modsig_verify(func, modsig, &status, &cause);
448
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500449out:
Mimi Zohar57b56ac2018-02-21 11:33:37 -0500450 /*
451 * File signatures on some filesystems can not be properly verified.
Mimi Zohar9e670282018-02-21 11:36:32 -0500452 * When such filesystems are mounted by an untrusted mounter or on a
453 * system not willing to accept such a risk, fail the file signature
454 * verification.
Mimi Zohar57b56ac2018-02-21 11:33:37 -0500455 */
Mimi Zohar9e670282018-02-21 11:36:32 -0500456 if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
457 ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||
458 (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {
Mimi Zohar57b56ac2018-02-21 11:33:37 -0500459 status = INTEGRITY_FAIL;
460 cause = "unverifiable-signature";
461 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
462 op, cause, rc, 0);
463 } else if (status != INTEGRITY_PASS) {
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300464 /* Fix mode, but don't replace file signatures. */
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -0300465 if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig &&
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300466 (!xattr_value ||
467 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
Dmitry Kasatkindef3e8b2012-09-20 22:38:53 +0300468 if (!ima_fix_xattr(dentry, iint))
469 status = INTEGRITY_PASS;
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300470 }
471
Roberto Sassu7aa57832021-05-14 17:27:51 +0200472 /*
473 * Permit new files with file/EVM portable signatures, but
474 * without data.
475 */
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300476 if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&
Roberto Sassu7aa57832021-05-14 17:27:51 +0200477 test_bit(IMA_DIGSIG, &iint->atomic_flags)) {
Mimi Zohar05d1a712016-02-29 19:52:05 -0500478 status = INTEGRITY_PASS;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500479 }
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300480
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500481 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
482 op, cause, rc, 0);
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300483 } else {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500484 ima_cache_flags(iint, func);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500485 }
Mimi Zohar57b56ac2018-02-21 11:33:37 -0500486
Mimi Zohard79d72e2012-12-03 17:08:11 -0500487 ima_set_cache_status(iint, func, status);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500488 return status;
489}
490
491/*
492 * ima_update_xattr - update 'security.ima' hash value
493 */
494void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
495{
Miklos Szeredie71b9df2016-09-16 12:44:20 +0200496 struct dentry *dentry = file_dentry(file);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500497 int rc = 0;
498
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300499 /* do not collect and update hash for digital signatures */
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200500 if (test_bit(IMA_DIGSIG, &iint->atomic_flags))
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300501 return;
502
Mimi Zoharda1b0022016-09-29 10:04:52 -0400503 if ((iint->ima_file_status != INTEGRITY_PASS) &&
504 !(iint->flags & IMA_HASH))
Roberto Sassu020aae32017-11-07 11:37:07 +0100505 return;
506
Thiago Jung Bauermann15588222019-06-27 23:19:31 -0300507 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500508 if (rc < 0)
509 return;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300510
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200511 inode_lock(file_inode(file));
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500512 ima_fix_xattr(dentry, iint);
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200513 inode_unlock(file_inode(file));
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500514}
515
516/**
517 * ima_inode_post_setattr - reflect file metadata changes
Christian Braunera2d23292021-01-21 14:19:45 +0100518 * @mnt_userns: user namespace of the mount the inode was found from
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500519 * @dentry: pointer to the affected dentry
520 *
521 * Changes to a dentry's metadata might result in needing to appraise.
522 *
523 * This function is called from notify_change(), which expects the caller
524 * to lock the inode's i_mutex.
525 */
Christian Braunera2d23292021-01-21 14:19:45 +0100526void ima_inode_post_setattr(struct user_namespace *mnt_userns,
527 struct dentry *dentry)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500528{
David Howellsc6f493d2015-03-17 22:26:22 +0000529 struct inode *inode = d_backing_inode(dentry);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500530 struct integrity_iint_cache *iint;
Mimi Zoharda1b0022016-09-29 10:04:52 -0400531 int action;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500532
Roberto Sassua7560242014-09-12 19:35:54 +0200533 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200534 || !(inode->i_opflags & IOP_XATTR))
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500535 return;
536
Christian Braunera2d23292021-01-21 14:19:45 +0100537 action = ima_must_appraise(mnt_userns, inode, MAY_ACCESS, POST_SETATTR);
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200538 iint = integrity_iint_find(inode);
539 if (iint) {
540 set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);
Mimi Zoharda1b0022016-09-29 10:04:52 -0400541 if (!action)
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200542 clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
543 }
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500544}
Mimi Zohar42c63332011-03-10 18:54:15 -0500545
546/*
547 * ima_protect_xattr - protect 'security.ima'
548 *
549 * Ensure that not just anyone can modify or remove 'security.ima'.
550 */
551static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
552 const void *xattr_value, size_t xattr_value_len)
553{
554 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
555 if (!capable(CAP_SYS_ADMIN))
556 return -EPERM;
557 return 1;
558 }
559 return 0;
560}
561
Mimi Zohar060bdebfb2014-03-17 23:24:18 -0400562static void ima_reset_appraise_flags(struct inode *inode, int digsig)
Mimi Zohar42c63332011-03-10 18:54:15 -0500563{
564 struct integrity_iint_cache *iint;
565
Roberto Sassua7560242014-09-12 19:35:54 +0200566 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
Mimi Zohar42c63332011-03-10 18:54:15 -0500567 return;
568
569 iint = integrity_iint_find(inode);
570 if (!iint)
571 return;
Eric Richtera4226382016-06-01 13:14:06 -0500572 iint->measured_pcrs = 0;
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200573 set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);
Mimi Zohar060bdebfb2014-03-17 23:24:18 -0400574 if (digsig)
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200575 set_bit(IMA_DIGSIG, &iint->atomic_flags);
576 else
577 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
Mimi Zohar42c63332011-03-10 18:54:15 -0500578}
579
THOBY Simon50f742d2021-08-16 08:10:59 +0000580/**
581 * validate_hash_algo() - Block setxattr with unsupported hash algorithms
582 * @dentry: object of the setxattr()
583 * @xattr_value: userland supplied xattr value
584 * @xattr_value_len: length of xattr_value
585 *
586 * The xattr value is mapped to its hash algorithm, and this algorithm
587 * must be built in the kernel for the setxattr to be allowed.
588 *
589 * Emit an audit message when the algorithm is invalid.
590 *
591 * Return: 0 on success, else an error.
592 */
593static int validate_hash_algo(struct dentry *dentry,
594 const struct evm_ima_xattr_data *xattr_value,
595 size_t xattr_value_len)
596{
597 char *path = NULL, *pathbuf = NULL;
598 enum hash_algo xattr_hash_algo;
THOBY Simon4f2946a2021-08-16 08:11:01 +0000599 const char *errmsg = "unavailable-hash-algorithm";
600 unsigned int allowed_hashes;
THOBY Simon50f742d2021-08-16 08:10:59 +0000601
602 xattr_hash_algo = ima_get_hash_algo(xattr_value, xattr_value_len);
603
THOBY Simon4f2946a2021-08-16 08:11:01 +0000604 allowed_hashes = atomic_read(&ima_setxattr_allowed_hash_algorithms);
605
606 if (allowed_hashes) {
607 /* success if the algorithm is allowed in the ima policy */
608 if (allowed_hashes & (1U << xattr_hash_algo))
609 return 0;
610
611 /*
612 * We use a different audit message when the hash algorithm
613 * is denied by a policy rule, instead of not being built
614 * in the kernel image
615 */
616 errmsg = "denied-hash-algorithm";
617 } else {
618 if (likely(xattr_hash_algo == ima_hash_algo))
619 return 0;
620
621 /* allow any xattr using an algorithm built in the kernel */
622 if (crypto_has_alg(hash_algo_name[xattr_hash_algo], 0, 0))
623 return 0;
624 }
THOBY Simon50f742d2021-08-16 08:10:59 +0000625
626 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
627 if (!pathbuf)
628 return -EACCES;
629
630 path = dentry_path(dentry, pathbuf, PATH_MAX);
631
632 integrity_audit_msg(AUDIT_INTEGRITY_DATA, d_inode(dentry), path,
THOBY Simon4f2946a2021-08-16 08:11:01 +0000633 "set_data", errmsg, -EACCES, 0);
THOBY Simon50f742d2021-08-16 08:10:59 +0000634
635 kfree(pathbuf);
636
637 return -EACCES;
638}
639
Mimi Zohar42c63332011-03-10 18:54:15 -0500640int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
641 const void *xattr_value, size_t xattr_value_len)
642{
Mimi Zohar060bdebfb2014-03-17 23:24:18 -0400643 const struct evm_ima_xattr_data *xvalue = xattr_value;
Roberto Sassue3ccfe12021-05-14 17:27:45 +0200644 int digsig = 0;
Mimi Zohar42c63332011-03-10 18:54:15 -0500645 int result;
646
647 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
648 xattr_value_len);
649 if (result == 1) {
Dmitry Kasatkina48fda92014-10-28 13:31:22 +0200650 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
651 return -EINVAL;
Roberto Sassue3ccfe12021-05-14 17:27:45 +0200652 digsig = (xvalue->type == EVM_IMA_XATTR_DIGSIG);
Roberto Sassu7aa57832021-05-14 17:27:51 +0200653 } else if (!strcmp(xattr_name, XATTR_NAME_EVM) && xattr_value_len > 0) {
654 digsig = (xvalue->type == EVM_XATTR_PORTABLE_DIGSIG);
Roberto Sassue3ccfe12021-05-14 17:27:45 +0200655 }
656 if (result == 1 || evm_revalidate_status(xattr_name)) {
THOBY Simon50f742d2021-08-16 08:10:59 +0000657 result = validate_hash_algo(dentry, xvalue, xattr_value_len);
658 if (result)
659 return result;
660
Roberto Sassue3ccfe12021-05-14 17:27:45 +0200661 ima_reset_appraise_flags(d_backing_inode(dentry), digsig);
Mimi Zohar42c63332011-03-10 18:54:15 -0500662 }
663 return result;
664}
665
666int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
667{
668 int result;
669
670 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
Roberto Sassue3ccfe12021-05-14 17:27:45 +0200671 if (result == 1 || evm_revalidate_status(xattr_name)) {
David Howellsc6f493d2015-03-17 22:26:22 +0000672 ima_reset_appraise_flags(d_backing_inode(dentry), 0);
Roberto Sassue3ccfe12021-05-14 17:27:45 +0200673 if (result == 1)
674 result = 0;
Mimi Zohar42c63332011-03-10 18:54:15 -0500675 }
676 return result;
677}