Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2008 IBM Corporation |
| 4 | * Author: Mimi Zohar <zohar@us.ibm.com> |
| 5 | * |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 6 | * ima_policy.c |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 7 | * - initialize default measure policy rules |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 8 | */ |
Thiago Jung Bauermann | 3878d50 | 2019-06-27 23:19:32 -0300 | [diff] [blame] | 9 | |
Paul Gortmaker | 876979c | 2018-12-09 15:36:29 -0500 | [diff] [blame] | 10 | #include <linux/init.h> |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 11 | #include <linux/list.h> |
Scott Branden | b89999d0 | 2020-10-02 10:38:15 -0700 | [diff] [blame] | 12 | #include <linux/kernel_read_file.h> |
Mimi Zohar | cf22221 | 2016-01-14 17:57:47 -0500 | [diff] [blame] | 13 | #include <linux/fs.h> |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 14 | #include <linux/security.h> |
| 15 | #include <linux/magic.h> |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 16 | #include <linux/parser.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 18 | #include <linux/rculist.h> |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 19 | #include <linux/genhd.h> |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 20 | #include <linux/seq_file.h> |
Nayna Jain | 6191706 | 2018-10-09 23:00:36 +0530 | [diff] [blame] | 21 | #include <linux/ima.h> |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 22 | |
| 23 | #include "ima.h" |
| 24 | |
| 25 | /* flags definitions */ |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 26 | #define IMA_FUNC 0x0001 |
| 27 | #define IMA_MASK 0x0002 |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 28 | #define IMA_FSMAGIC 0x0004 |
| 29 | #define IMA_UID 0x0008 |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 30 | #define IMA_FOWNER 0x0010 |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 31 | #define IMA_FSUUID 0x0020 |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 32 | #define IMA_INMASK 0x0040 |
Mimi Zohar | 139069e | 2014-11-05 07:48:36 -0500 | [diff] [blame] | 33 | #define IMA_EUID 0x0080 |
Eric Richter | 0260643 | 2016-06-01 13:14:01 -0500 | [diff] [blame] | 34 | #define IMA_PCR 0x0100 |
Mimi Zohar | f1b08bb | 2018-01-15 11:20:36 -0500 | [diff] [blame] | 35 | #define IMA_FSNAME 0x0200 |
Lakshmi Ramasubramanian | 2b60c0e | 2019-12-11 08:47:07 -0800 | [diff] [blame] | 36 | #define IMA_KEYRINGS 0x0400 |
Tushar Sugandhi | 47d76a4 | 2021-01-07 20:07:05 -0800 | [diff] [blame] | 37 | #define IMA_LABEL 0x0800 |
THOBY Simon | 1624dc0 | 2021-08-16 08:11:00 +0000 | [diff] [blame] | 38 | #define IMA_VALIDATE_ALGOS 0x1000 |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 39 | |
Dmitry Kasatkin | 45e2472 | 2012-09-12 20:51:32 +0300 | [diff] [blame] | 40 | #define UNKNOWN 0 |
| 41 | #define MEASURE 0x0001 /* same as IMA_MEASURE */ |
| 42 | #define DONT_MEASURE 0x0002 |
| 43 | #define APPRAISE 0x0004 /* same as IMA_APPRAISE */ |
| 44 | #define DONT_APPRAISE 0x0008 |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 45 | #define AUDIT 0x0040 |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 46 | #define HASH 0x0100 |
| 47 | #define DONT_HASH 0x0200 |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 48 | |
Eric Richter | 0260643 | 2016-06-01 13:14:01 -0500 | [diff] [blame] | 49 | #define INVALID_PCR(a) (((a) < 0) || \ |
Pankaj Bharadiya | c593642 | 2019-12-09 10:31:43 -0800 | [diff] [blame] | 50 | (a) >= (sizeof_field(struct integrity_iint_cache, measured_pcrs) * 8)) |
Eric Richter | 0260643 | 2016-06-01 13:14:01 -0500 | [diff] [blame] | 51 | |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 52 | int ima_policy_flag; |
Mimi Zohar | 6ad6afa | 2015-12-07 14:35:47 -0500 | [diff] [blame] | 53 | static int temp_ima_appraise; |
Mimi Zohar | ef96837 | 2018-07-13 14:06:01 -0400 | [diff] [blame] | 54 | static int build_ima_appraise __ro_after_init; |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 55 | |
THOBY Simon | 4f2946a | 2021-08-16 08:11:01 +0000 | [diff] [blame] | 56 | atomic_t ima_setxattr_allowed_hash_algorithms; |
| 57 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 58 | #define MAX_LSM_RULES 6 |
| 59 | enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE, |
| 60 | LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE |
| 61 | }; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 62 | |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 63 | enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB }; |
| 64 | |
Nayna Jain | c52657d | 2018-10-09 23:00:35 +0530 | [diff] [blame] | 65 | enum policy_rule_list { IMA_DEFAULT_POLICY = 1, IMA_CUSTOM_POLICY }; |
| 66 | |
Tyler Hicks | 176377d | 2020-08-11 14:26:20 -0500 | [diff] [blame] | 67 | struct ima_rule_opt_list { |
| 68 | size_t count; |
| 69 | char *items[]; |
| 70 | }; |
| 71 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 72 | struct ima_rule_entry { |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 73 | struct list_head list; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 74 | int action; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 75 | unsigned int flags; |
| 76 | enum ima_hooks func; |
| 77 | int mask; |
| 78 | unsigned long fsmagic; |
Christoph Hellwig | 787d8c5 | 2017-06-01 07:00:26 +0200 | [diff] [blame] | 79 | uuid_t fsuuid; |
Eric W. Biederman | 8b94eea | 2012-05-25 18:24:12 -0600 | [diff] [blame] | 80 | kuid_t uid; |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 81 | kuid_t fowner; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 82 | bool (*uid_op)(kuid_t, kuid_t); /* Handlers for operators */ |
| 83 | bool (*fowner_op)(kuid_t, kuid_t); /* uid_eq(), uid_gt(), uid_lt() */ |
Eric Richter | 0260643 | 2016-06-01 13:14:01 -0500 | [diff] [blame] | 84 | int pcr; |
THOBY Simon | 1624dc0 | 2021-08-16 08:11:00 +0000 | [diff] [blame] | 85 | unsigned int allowed_algos; /* bitfield of allowed hash algorithms */ |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 86 | struct { |
| 87 | void *rule; /* LSM file metadata specific */ |
Tyler Hicks | aa0c022 | 2020-07-09 01:19:08 -0500 | [diff] [blame] | 88 | char *args_p; /* audit value */ |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 89 | int type; /* audit type */ |
| 90 | } lsm[MAX_LSM_RULES]; |
Mimi Zohar | f1b08bb | 2018-01-15 11:20:36 -0500 | [diff] [blame] | 91 | char *fsname; |
Tyler Hicks | 176377d | 2020-08-11 14:26:20 -0500 | [diff] [blame] | 92 | struct ima_rule_opt_list *keyrings; /* Measure keys added to these keyrings */ |
Tushar Sugandhi | 47d76a4 | 2021-01-07 20:07:05 -0800 | [diff] [blame] | 93 | struct ima_rule_opt_list *label; /* Measure data grouped under this label */ |
Matthew Garrett | 19453ce0 | 2019-06-19 15:46:11 -0700 | [diff] [blame] | 94 | struct ima_template_desc *template; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 95 | }; |
| 96 | |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 97 | /* |
THOBY Simon | 1624dc0 | 2021-08-16 08:11:00 +0000 | [diff] [blame] | 98 | * sanity check in case the kernels gains more hash algorithms that can |
| 99 | * fit in an unsigned int |
| 100 | */ |
| 101 | static_assert( |
| 102 | 8 * sizeof(unsigned int) >= HASH_ALGO__LAST, |
| 103 | "The bitfield allowed_algos in ima_rule_entry is too small to contain all the supported hash algorithms, consider using a bigger type"); |
| 104 | |
| 105 | /* |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 106 | * Without LSM specific knowledge, the default policy can only be |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 107 | * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 108 | */ |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 109 | |
| 110 | /* |
| 111 | * The minimum rule set to allow for full TCB coverage. Measures all files |
| 112 | * opened or mmap for exec and everything read by root. Dangerous because |
| 113 | * normal users can easily run the machine out of memory simply building |
| 114 | * and running executables. |
| 115 | */ |
James Morris | bad4417 | 2017-02-13 16:34:35 +1100 | [diff] [blame] | 116 | static struct ima_rule_entry dont_measure_rules[] __ro_after_init = { |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 117 | {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 118 | {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 119 | {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 120 | {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 121 | {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 122 | {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 123 | {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 124 | {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC}, |
Martin Townsend | 1c070b1 | 2018-03-22 13:46:01 +0000 | [diff] [blame] | 125 | {.action = DONT_MEASURE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC}, |
Roberto Sassu | 6438de9 | 2015-04-11 17:13:06 +0200 | [diff] [blame] | 126 | {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC, |
| 127 | .flags = IMA_FSMAGIC}, |
Laura Abbott | 82e3bb4 | 2017-05-09 11:25:27 -0700 | [diff] [blame] | 128 | {.action = DONT_MEASURE, .fsmagic = CGROUP2_SUPER_MAGIC, |
| 129 | .flags = IMA_FSMAGIC}, |
Mimi Zohar | 060190f | 2018-11-14 17:24:13 -0500 | [diff] [blame] | 130 | {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 131 | {.action = DONT_MEASURE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC} |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 132 | }; |
| 133 | |
James Morris | bad4417 | 2017-02-13 16:34:35 +1100 | [diff] [blame] | 134 | static struct ima_rule_entry original_measurement_rules[] __ro_after_init = { |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 135 | {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC, |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 136 | .flags = IMA_FUNC | IMA_MASK}, |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 137 | {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC, |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 138 | .flags = IMA_FUNC | IMA_MASK}, |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 139 | {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ, |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 140 | .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq, |
| 141 | .flags = IMA_FUNC | IMA_MASK | IMA_UID}, |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 142 | {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC}, |
| 143 | {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC}, |
| 144 | }; |
| 145 | |
James Morris | bad4417 | 2017-02-13 16:34:35 +1100 | [diff] [blame] | 146 | static struct ima_rule_entry default_measurement_rules[] __ro_after_init = { |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 147 | {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC, |
| 148 | .flags = IMA_FUNC | IMA_MASK}, |
| 149 | {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC, |
| 150 | .flags = IMA_FUNC | IMA_MASK}, |
| 151 | {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ, |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 152 | .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq, |
| 153 | .flags = IMA_FUNC | IMA_INMASK | IMA_EUID}, |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 154 | {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ, |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 155 | .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq, |
| 156 | .flags = IMA_FUNC | IMA_INMASK | IMA_UID}, |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 157 | {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC}, |
Mimi Zohar | 5a9196d | 2014-07-22 10:39:48 -0400 | [diff] [blame] | 158 | {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC}, |
Mimi Zohar | 19f8a84 | 2016-01-15 10:17:12 -0500 | [diff] [blame] | 159 | {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC}, |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 160 | }; |
| 161 | |
James Morris | bad4417 | 2017-02-13 16:34:35 +1100 | [diff] [blame] | 162 | static struct ima_rule_entry default_appraise_rules[] __ro_after_init = { |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 163 | {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 164 | {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 165 | {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 166 | {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 167 | {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 168 | {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 169 | {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 170 | {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 171 | {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC}, |
Martin Townsend | 1c070b1 | 2018-03-22 13:46:01 +0000 | [diff] [blame] | 172 | {.action = DONT_APPRAISE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC}, |
Mimi Zohar | cd025f7 | 2015-04-21 16:54:24 -0400 | [diff] [blame] | 173 | {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC}, |
Mimi Zohar | 060190f | 2018-11-14 17:24:13 -0500 | [diff] [blame] | 174 | {.action = DONT_APPRAISE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC}, |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 175 | {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
Laura Abbott | 82e3bb4 | 2017-05-09 11:25:27 -0700 | [diff] [blame] | 176 | {.action = DONT_APPRAISE, .fsmagic = CGROUP2_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
Mimi Zohar | 95ee08f | 2015-12-07 15:08:01 -0500 | [diff] [blame] | 177 | #ifdef CONFIG_IMA_WRITE_POLICY |
| 178 | {.action = APPRAISE, .func = POLICY_CHECK, |
| 179 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 180 | #endif |
Dmitry Kasatkin | c57782c | 2014-11-05 17:01:16 +0200 | [diff] [blame] | 181 | #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 182 | {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq, |
| 183 | .flags = IMA_FOWNER}, |
Dmitry Kasatkin | c57782c | 2014-11-05 17:01:16 +0200 | [diff] [blame] | 184 | #else |
| 185 | /* force signature */ |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 186 | {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq, |
Dmitry Kasatkin | c57782c | 2014-11-05 17:01:16 +0200 | [diff] [blame] | 187 | .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED}, |
| 188 | #endif |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 189 | }; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 190 | |
Mimi Zohar | ef96837 | 2018-07-13 14:06:01 -0400 | [diff] [blame] | 191 | static struct ima_rule_entry build_appraise_rules[] __ro_after_init = { |
| 192 | #ifdef CONFIG_IMA_APPRAISE_REQUIRE_MODULE_SIGS |
| 193 | {.action = APPRAISE, .func = MODULE_CHECK, |
| 194 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 195 | #endif |
| 196 | #ifdef CONFIG_IMA_APPRAISE_REQUIRE_FIRMWARE_SIGS |
| 197 | {.action = APPRAISE, .func = FIRMWARE_CHECK, |
| 198 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 199 | #endif |
| 200 | #ifdef CONFIG_IMA_APPRAISE_REQUIRE_KEXEC_SIGS |
| 201 | {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK, |
| 202 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 203 | #endif |
| 204 | #ifdef CONFIG_IMA_APPRAISE_REQUIRE_POLICY_SIGS |
| 205 | {.action = APPRAISE, .func = POLICY_CHECK, |
| 206 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 207 | #endif |
| 208 | }; |
| 209 | |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 210 | static struct ima_rule_entry secure_boot_rules[] __ro_after_init = { |
| 211 | {.action = APPRAISE, .func = MODULE_CHECK, |
| 212 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 213 | {.action = APPRAISE, .func = FIRMWARE_CHECK, |
| 214 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 215 | {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK, |
| 216 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 217 | {.action = APPRAISE, .func = POLICY_CHECK, |
| 218 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 219 | }; |
| 220 | |
Lakshmi Ramasubramanian | 03cee16 | 2021-01-07 20:07:07 -0800 | [diff] [blame] | 221 | static struct ima_rule_entry critical_data_rules[] __ro_after_init = { |
| 222 | {.action = MEASURE, .func = CRITICAL_DATA, .flags = IMA_FUNC}, |
| 223 | }; |
| 224 | |
Nayna Jain | 6191706 | 2018-10-09 23:00:36 +0530 | [diff] [blame] | 225 | /* An array of architecture specific rules */ |
YueHaibing | 68f2529 | 2019-06-11 21:40:32 +0800 | [diff] [blame] | 226 | static struct ima_rule_entry *arch_policy_entry __ro_after_init; |
Nayna Jain | 6191706 | 2018-10-09 23:00:36 +0530 | [diff] [blame] | 227 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 228 | static LIST_HEAD(ima_default_rules); |
| 229 | static LIST_HEAD(ima_policy_rules); |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 230 | static LIST_HEAD(ima_temp_rules); |
Roberto Sassu | 067a436 | 2020-06-03 17:08:20 +0200 | [diff] [blame] | 231 | static struct list_head *ima_rules = &ima_default_rules; |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 232 | |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 233 | static int ima_policy __initdata; |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 234 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 235 | static int __init default_measure_policy_setup(char *str) |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 236 | { |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 237 | if (ima_policy) |
| 238 | return 1; |
| 239 | |
| 240 | ima_policy = ORIGINAL_TCB; |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 241 | return 1; |
| 242 | } |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 243 | __setup("ima_tcb", default_measure_policy_setup); |
| 244 | |
Mimi Zohar | 33ce954 | 2017-04-24 12:04:09 -0400 | [diff] [blame] | 245 | static bool ima_use_appraise_tcb __initdata; |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 246 | static bool ima_use_secure_boot __initdata; |
Lakshmi Ramasubramanian | 03cee16 | 2021-01-07 20:07:07 -0800 | [diff] [blame] | 247 | static bool ima_use_critical_data __initdata; |
Mimi Zohar | 9e67028 | 2018-02-21 11:36:32 -0500 | [diff] [blame] | 248 | static bool ima_fail_unverifiable_sigs __ro_after_init; |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 249 | static int __init policy_setup(char *str) |
| 250 | { |
Mimi Zohar | 33ce954 | 2017-04-24 12:04:09 -0400 | [diff] [blame] | 251 | char *p; |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 252 | |
Mimi Zohar | 33ce954 | 2017-04-24 12:04:09 -0400 | [diff] [blame] | 253 | while ((p = strsep(&str, " |\n")) != NULL) { |
| 254 | if (*p == ' ') |
| 255 | continue; |
| 256 | if ((strcmp(p, "tcb") == 0) && !ima_policy) |
| 257 | ima_policy = DEFAULT_TCB; |
| 258 | else if (strcmp(p, "appraise_tcb") == 0) |
Thomas Meyer | 39adb92 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 259 | ima_use_appraise_tcb = true; |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 260 | else if (strcmp(p, "secure_boot") == 0) |
Thomas Meyer | 39adb92 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 261 | ima_use_secure_boot = true; |
Lakshmi Ramasubramanian | 03cee16 | 2021-01-07 20:07:07 -0800 | [diff] [blame] | 262 | else if (strcmp(p, "critical_data") == 0) |
| 263 | ima_use_critical_data = true; |
Mimi Zohar | 9e67028 | 2018-02-21 11:36:32 -0500 | [diff] [blame] | 264 | else if (strcmp(p, "fail_securely") == 0) |
| 265 | ima_fail_unverifiable_sigs = true; |
Bruno Meneguele | 7fe2bb7 | 2020-09-04 16:40:58 -0300 | [diff] [blame] | 266 | else |
| 267 | pr_err("policy \"%s\" not found", p); |
Mimi Zohar | 33ce954 | 2017-04-24 12:04:09 -0400 | [diff] [blame] | 268 | } |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 269 | |
| 270 | return 1; |
| 271 | } |
| 272 | __setup("ima_policy=", policy_setup); |
| 273 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 274 | static int __init default_appraise_policy_setup(char *str) |
| 275 | { |
Thomas Meyer | 39adb92 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 276 | ima_use_appraise_tcb = true; |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 277 | return 1; |
| 278 | } |
| 279 | __setup("ima_appraise_tcb", default_appraise_policy_setup); |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 280 | |
Tyler Hicks | 176377d | 2020-08-11 14:26:20 -0500 | [diff] [blame] | 281 | static struct ima_rule_opt_list *ima_alloc_rule_opt_list(const substring_t *src) |
| 282 | { |
| 283 | struct ima_rule_opt_list *opt_list; |
| 284 | size_t count = 0; |
| 285 | char *src_copy; |
| 286 | char *cur, *next; |
| 287 | size_t i; |
| 288 | |
| 289 | src_copy = match_strdup(src); |
| 290 | if (!src_copy) |
| 291 | return ERR_PTR(-ENOMEM); |
| 292 | |
| 293 | next = src_copy; |
| 294 | while ((cur = strsep(&next, "|"))) { |
| 295 | /* Don't accept an empty list item */ |
| 296 | if (!(*cur)) { |
| 297 | kfree(src_copy); |
| 298 | return ERR_PTR(-EINVAL); |
| 299 | } |
| 300 | count++; |
| 301 | } |
| 302 | |
| 303 | /* Don't accept an empty list */ |
| 304 | if (!count) { |
| 305 | kfree(src_copy); |
| 306 | return ERR_PTR(-EINVAL); |
| 307 | } |
| 308 | |
| 309 | opt_list = kzalloc(struct_size(opt_list, items, count), GFP_KERNEL); |
| 310 | if (!opt_list) { |
| 311 | kfree(src_copy); |
| 312 | return ERR_PTR(-ENOMEM); |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * strsep() has already replaced all instances of '|' with '\0', |
| 317 | * leaving a byte sequence of NUL-terminated strings. Reference each |
| 318 | * string with the array of items. |
| 319 | * |
| 320 | * IMPORTANT: Ownership of the allocated buffer is transferred from |
| 321 | * src_copy to the first element in the items array. To free the |
| 322 | * buffer, kfree() must only be called on the first element of the |
| 323 | * array. |
| 324 | */ |
| 325 | for (i = 0, cur = src_copy; i < count; i++) { |
| 326 | opt_list->items[i] = cur; |
| 327 | cur = strchr(cur, '\0') + 1; |
| 328 | } |
| 329 | opt_list->count = count; |
| 330 | |
| 331 | return opt_list; |
| 332 | } |
| 333 | |
| 334 | static void ima_free_rule_opt_list(struct ima_rule_opt_list *opt_list) |
| 335 | { |
| 336 | if (!opt_list) |
| 337 | return; |
| 338 | |
| 339 | if (opt_list->count) { |
| 340 | kfree(opt_list->items[0]); |
| 341 | opt_list->count = 0; |
| 342 | } |
| 343 | |
| 344 | kfree(opt_list); |
| 345 | } |
| 346 | |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 347 | static void ima_lsm_free_rule(struct ima_rule_entry *entry) |
| 348 | { |
| 349 | int i; |
| 350 | |
| 351 | for (i = 0; i < MAX_LSM_RULES; i++) { |
Tyler Hicks | b8867ee | 2020-07-10 15:37:50 -0500 | [diff] [blame] | 352 | ima_filter_rule_free(entry->lsm[i].rule); |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 353 | kfree(entry->lsm[i].args_p); |
| 354 | } |
Tyler Hicks | 465aee7 | 2020-07-09 01:19:01 -0500 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | static void ima_free_rule(struct ima_rule_entry *entry) |
| 358 | { |
| 359 | if (!entry) |
| 360 | return; |
| 361 | |
| 362 | /* |
| 363 | * entry->template->fields may be allocated in ima_parse_rule() but that |
| 364 | * reference is owned by the corresponding ima_template_desc element in |
| 365 | * the defined_templates list and cannot be freed here |
| 366 | */ |
| 367 | kfree(entry->fsname); |
Tyler Hicks | 176377d | 2020-08-11 14:26:20 -0500 | [diff] [blame] | 368 | ima_free_rule_opt_list(entry->keyrings); |
Tyler Hicks | 465aee7 | 2020-07-09 01:19:01 -0500 | [diff] [blame] | 369 | ima_lsm_free_rule(entry); |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 370 | kfree(entry); |
| 371 | } |
| 372 | |
| 373 | static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry) |
| 374 | { |
| 375 | struct ima_rule_entry *nentry; |
Janne Karhunen | 483ec26 | 2020-01-15 17:42:30 +0200 | [diff] [blame] | 376 | int i; |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 377 | |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 378 | /* |
| 379 | * Immutable elements are copied over as pointers and data; only |
| 380 | * lsm rules can change |
| 381 | */ |
Alex Dewar | f60c826 | 2020-09-09 20:09:06 +0100 | [diff] [blame] | 382 | nentry = kmemdup(entry, sizeof(*nentry), GFP_KERNEL); |
| 383 | if (!nentry) |
| 384 | return NULL; |
| 385 | |
Pankaj Bharadiya | c593642 | 2019-12-09 10:31:43 -0800 | [diff] [blame] | 386 | memset(nentry->lsm, 0, sizeof_field(struct ima_rule_entry, lsm)); |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 387 | |
| 388 | for (i = 0; i < MAX_LSM_RULES; i++) { |
Janne Karhunen | 483ec26 | 2020-01-15 17:42:30 +0200 | [diff] [blame] | 389 | if (!entry->lsm[i].args_p) |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 390 | continue; |
| 391 | |
| 392 | nentry->lsm[i].type = entry->lsm[i].type; |
Tyler Hicks | 39e5993 | 2020-07-09 01:19:07 -0500 | [diff] [blame] | 393 | nentry->lsm[i].args_p = entry->lsm[i].args_p; |
| 394 | /* |
| 395 | * Remove the reference from entry so that the associated |
| 396 | * memory will not be freed during a later call to |
| 397 | * ima_lsm_free_rule(entry). |
| 398 | */ |
| 399 | entry->lsm[i].args_p = NULL; |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 400 | |
Tyler Hicks | b8867ee | 2020-07-10 15:37:50 -0500 | [diff] [blame] | 401 | ima_filter_rule_init(nentry->lsm[i].type, Audit_equal, |
| 402 | nentry->lsm[i].args_p, |
| 403 | &nentry->lsm[i].rule); |
Janne Karhunen | 483ec26 | 2020-01-15 17:42:30 +0200 | [diff] [blame] | 404 | if (!nentry->lsm[i].rule) |
| 405 | pr_warn("rule for LSM \'%s\' is undefined\n", |
Tyler Hicks | aa0c022 | 2020-07-09 01:19:08 -0500 | [diff] [blame] | 406 | nentry->lsm[i].args_p); |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 407 | } |
| 408 | return nentry; |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | static int ima_lsm_update_rule(struct ima_rule_entry *entry) |
| 412 | { |
| 413 | struct ima_rule_entry *nentry; |
| 414 | |
| 415 | nentry = ima_lsm_copy_rule(entry); |
| 416 | if (!nentry) |
| 417 | return -ENOMEM; |
| 418 | |
| 419 | list_replace_rcu(&entry->list, &nentry->list); |
| 420 | synchronize_rcu(); |
Tyler Hicks | 465aee7 | 2020-07-09 01:19:01 -0500 | [diff] [blame] | 421 | /* |
| 422 | * ima_lsm_copy_rule() shallow copied all references, except for the |
| 423 | * LSM references, from entry to nentry so we only want to free the LSM |
| 424 | * references and the entry itself. All other memory refrences will now |
| 425 | * be owned by nentry. |
| 426 | */ |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 427 | ima_lsm_free_rule(entry); |
Tyler Hicks | 465aee7 | 2020-07-09 01:19:01 -0500 | [diff] [blame] | 428 | kfree(entry); |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
Tyler Hicks | db2045f | 2020-07-09 01:19:04 -0500 | [diff] [blame] | 433 | static bool ima_rule_contains_lsm_cond(struct ima_rule_entry *entry) |
| 434 | { |
| 435 | int i; |
| 436 | |
| 437 | for (i = 0; i < MAX_LSM_RULES; i++) |
| 438 | if (entry->lsm[i].args_p) |
| 439 | return true; |
| 440 | |
| 441 | return false; |
| 442 | } |
| 443 | |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 444 | /* |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 445 | * The LSM policy can be reloaded, leaving the IMA LSM based rules referring |
| 446 | * to the old, stale LSM policy. Update the IMA LSM based rules to reflect |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 447 | * the reloaded LSM policy. |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 448 | */ |
| 449 | static void ima_lsm_update_rules(void) |
| 450 | { |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 451 | struct ima_rule_entry *entry, *e; |
Tyler Hicks | 592b24c | 2020-07-09 01:19:10 -0500 | [diff] [blame] | 452 | int result; |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 453 | |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 454 | list_for_each_entry_safe(entry, e, &ima_policy_rules, list) { |
Tyler Hicks | 592b24c | 2020-07-09 01:19:10 -0500 | [diff] [blame] | 455 | if (!ima_rule_contains_lsm_cond(entry)) |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 456 | continue; |
| 457 | |
| 458 | result = ima_lsm_update_rule(entry); |
| 459 | if (result) { |
Janne Karhunen | 483ec26 | 2020-01-15 17:42:30 +0200 | [diff] [blame] | 460 | pr_err("lsm rule update error %d\n", result); |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 461 | return; |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 462 | } |
| 463 | } |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 464 | } |
| 465 | |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 466 | int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event, |
| 467 | void *lsm_data) |
| 468 | { |
| 469 | if (event != LSM_POLICY_CHANGE) |
| 470 | return NOTIFY_DONE; |
| 471 | |
| 472 | ima_lsm_update_rules(); |
| 473 | return NOTIFY_OK; |
| 474 | } |
| 475 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 476 | /** |
Tushar Sugandhi | 2b4a247 | 2021-01-07 20:07:01 -0800 | [diff] [blame] | 477 | * ima_match_rule_data - determine whether func_data matches the policy rule |
Lakshmi Ramasubramanian | e9085e0 | 2019-12-11 08:47:06 -0800 | [diff] [blame] | 478 | * @rule: a pointer to a rule |
Tushar Sugandhi | 2b4a247 | 2021-01-07 20:07:01 -0800 | [diff] [blame] | 479 | * @func_data: data to match against the measure rule data |
Lakshmi Ramasubramanian | e9085e0 | 2019-12-11 08:47:06 -0800 | [diff] [blame] | 480 | * @cred: a pointer to a credentials structure for user validation |
| 481 | * |
Tushar Sugandhi | 2b4a247 | 2021-01-07 20:07:01 -0800 | [diff] [blame] | 482 | * Returns true if func_data matches one in the rule, false otherwise. |
Lakshmi Ramasubramanian | e9085e0 | 2019-12-11 08:47:06 -0800 | [diff] [blame] | 483 | */ |
Tushar Sugandhi | 2b4a247 | 2021-01-07 20:07:01 -0800 | [diff] [blame] | 484 | static bool ima_match_rule_data(struct ima_rule_entry *rule, |
| 485 | const char *func_data, |
| 486 | const struct cred *cred) |
Lakshmi Ramasubramanian | e9085e0 | 2019-12-11 08:47:06 -0800 | [diff] [blame] | 487 | { |
Tushar Sugandhi | 2b4a247 | 2021-01-07 20:07:01 -0800 | [diff] [blame] | 488 | const struct ima_rule_opt_list *opt_list = NULL; |
Lakshmi Ramasubramanian | e9085e0 | 2019-12-11 08:47:06 -0800 | [diff] [blame] | 489 | bool matched = false; |
Tyler Hicks | 176377d | 2020-08-11 14:26:20 -0500 | [diff] [blame] | 490 | size_t i; |
Lakshmi Ramasubramanian | e9085e0 | 2019-12-11 08:47:06 -0800 | [diff] [blame] | 491 | |
| 492 | if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid)) |
| 493 | return false; |
| 494 | |
Tushar Sugandhi | 2b4a247 | 2021-01-07 20:07:01 -0800 | [diff] [blame] | 495 | switch (rule->func) { |
| 496 | case KEY_CHECK: |
| 497 | if (!rule->keyrings) |
| 498 | return true; |
Lakshmi Ramasubramanian | e9085e0 | 2019-12-11 08:47:06 -0800 | [diff] [blame] | 499 | |
Tushar Sugandhi | 2b4a247 | 2021-01-07 20:07:01 -0800 | [diff] [blame] | 500 | opt_list = rule->keyrings; |
| 501 | break; |
Tushar Sugandhi | c4e43aa | 2021-01-07 20:07:04 -0800 | [diff] [blame] | 502 | case CRITICAL_DATA: |
Tushar Sugandhi | 47d76a4 | 2021-01-07 20:07:05 -0800 | [diff] [blame] | 503 | if (!rule->label) |
| 504 | return true; |
| 505 | |
| 506 | opt_list = rule->label; |
| 507 | break; |
Tushar Sugandhi | 2b4a247 | 2021-01-07 20:07:01 -0800 | [diff] [blame] | 508 | default: |
| 509 | return false; |
| 510 | } |
| 511 | |
| 512 | if (!func_data) |
Lakshmi Ramasubramanian | e9085e0 | 2019-12-11 08:47:06 -0800 | [diff] [blame] | 513 | return false; |
| 514 | |
Tushar Sugandhi | 2b4a247 | 2021-01-07 20:07:01 -0800 | [diff] [blame] | 515 | for (i = 0; i < opt_list->count; i++) { |
| 516 | if (!strcmp(opt_list->items[i], func_data)) { |
Lakshmi Ramasubramanian | e9085e0 | 2019-12-11 08:47:06 -0800 | [diff] [blame] | 517 | matched = true; |
| 518 | break; |
| 519 | } |
| 520 | } |
| 521 | |
Lakshmi Ramasubramanian | e9085e0 | 2019-12-11 08:47:06 -0800 | [diff] [blame] | 522 | return matched; |
| 523 | } |
| 524 | |
| 525 | /** |
Janne Karhunen | 483ec26 | 2020-01-15 17:42:30 +0200 | [diff] [blame] | 526 | * ima_match_rules - determine whether an inode matches the policy rule. |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 527 | * @rule: a pointer to a rule |
Christian Brauner | a2d2329 | 2021-01-21 14:19:45 +0100 | [diff] [blame] | 528 | * @mnt_userns: user namespace of the mount the inode was found from |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 529 | * @inode: a pointer to an inode |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 530 | * @cred: a pointer to a credentials structure for user validation |
| 531 | * @secid: the secid of the task to be validated |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 532 | * @func: LIM hook identifier |
| 533 | * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC) |
Tushar Sugandhi | 2b4a247 | 2021-01-07 20:07:01 -0800 | [diff] [blame] | 534 | * @func_data: func specific data, may be NULL |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 535 | * |
| 536 | * Returns true on rule match, false on failure. |
| 537 | */ |
Christian Brauner | a2d2329 | 2021-01-21 14:19:45 +0100 | [diff] [blame] | 538 | static bool ima_match_rules(struct ima_rule_entry *rule, |
| 539 | struct user_namespace *mnt_userns, |
| 540 | struct inode *inode, const struct cred *cred, |
| 541 | u32 secid, enum ima_hooks func, int mask, |
Tushar Sugandhi | 2b4a247 | 2021-01-07 20:07:01 -0800 | [diff] [blame] | 542 | const char *func_data) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 543 | { |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 544 | int i; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 545 | |
Dmitry Kasatkin | 09b1148 | 2013-11-13 23:42:39 +0200 | [diff] [blame] | 546 | if ((rule->flags & IMA_FUNC) && |
| 547 | (rule->func != func && func != POST_SETATTR)) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 548 | return false; |
Tushar Sugandhi | c4e43aa | 2021-01-07 20:07:04 -0800 | [diff] [blame] | 549 | |
| 550 | switch (func) { |
| 551 | case KEY_CHECK: |
| 552 | case CRITICAL_DATA: |
| 553 | return ((rule->func == func) && |
| 554 | ima_match_rule_data(rule, func_data, cred)); |
| 555 | default: |
| 556 | break; |
| 557 | } |
| 558 | |
Dmitry Kasatkin | 09b1148 | 2013-11-13 23:42:39 +0200 | [diff] [blame] | 559 | if ((rule->flags & IMA_MASK) && |
| 560 | (rule->mask != mask && func != POST_SETATTR)) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 561 | return false; |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 562 | if ((rule->flags & IMA_INMASK) && |
| 563 | (!(rule->mask & mask) && func != POST_SETATTR)) |
| 564 | return false; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 565 | if ((rule->flags & IMA_FSMAGIC) |
| 566 | && rule->fsmagic != inode->i_sb->s_magic) |
| 567 | return false; |
Mimi Zohar | f1b08bb | 2018-01-15 11:20:36 -0500 | [diff] [blame] | 568 | if ((rule->flags & IMA_FSNAME) |
| 569 | && strcmp(rule->fsname, inode->i_sb->s_type->name)) |
| 570 | return false; |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 571 | if ((rule->flags & IMA_FSUUID) && |
Christoph Hellwig | 8578709 | 2017-05-10 15:06:33 +0200 | [diff] [blame] | 572 | !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid)) |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 573 | return false; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 574 | if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid)) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 575 | return false; |
Mimi Zohar | 139069e | 2014-11-05 07:48:36 -0500 | [diff] [blame] | 576 | if (rule->flags & IMA_EUID) { |
| 577 | if (has_capability_noaudit(current, CAP_SETUID)) { |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 578 | if (!rule->uid_op(cred->euid, rule->uid) |
| 579 | && !rule->uid_op(cred->suid, rule->uid) |
| 580 | && !rule->uid_op(cred->uid, rule->uid)) |
Mimi Zohar | 139069e | 2014-11-05 07:48:36 -0500 | [diff] [blame] | 581 | return false; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 582 | } else if (!rule->uid_op(cred->euid, rule->uid)) |
Mimi Zohar | 139069e | 2014-11-05 07:48:36 -0500 | [diff] [blame] | 583 | return false; |
| 584 | } |
| 585 | |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 586 | if ((rule->flags & IMA_FOWNER) && |
Christian Brauner | a2d2329 | 2021-01-21 14:19:45 +0100 | [diff] [blame] | 587 | !rule->fowner_op(i_uid_into_mnt(mnt_userns, inode), rule->fowner)) |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 588 | return false; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 589 | for (i = 0; i < MAX_LSM_RULES; i++) { |
Mimi Zohar | 53fc0e2 | 2009-05-05 13:12:48 -0400 | [diff] [blame] | 590 | int rc = 0; |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 591 | u32 osid; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 592 | |
Janne Karhunen | 483ec26 | 2020-01-15 17:42:30 +0200 | [diff] [blame] | 593 | if (!rule->lsm[i].rule) { |
| 594 | if (!rule->lsm[i].args_p) |
| 595 | continue; |
| 596 | else |
| 597 | return false; |
| 598 | } |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 599 | switch (i) { |
| 600 | case LSM_OBJ_USER: |
| 601 | case LSM_OBJ_ROLE: |
| 602 | case LSM_OBJ_TYPE: |
| 603 | security_inode_getsecid(inode, &osid); |
Tyler Hicks | b8867ee | 2020-07-10 15:37:50 -0500 | [diff] [blame] | 604 | rc = ima_filter_rule_match(osid, rule->lsm[i].type, |
| 605 | Audit_equal, |
| 606 | rule->lsm[i].rule); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 607 | break; |
| 608 | case LSM_SUBJ_USER: |
| 609 | case LSM_SUBJ_ROLE: |
| 610 | case LSM_SUBJ_TYPE: |
Tyler Hicks | b8867ee | 2020-07-10 15:37:50 -0500 | [diff] [blame] | 611 | rc = ima_filter_rule_match(secid, rule->lsm[i].type, |
| 612 | Audit_equal, |
| 613 | rule->lsm[i].rule); |
Gustavo A. R. Silva | 28073eb | 2020-11-20 12:25:46 -0600 | [diff] [blame] | 614 | break; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 615 | default: |
| 616 | break; |
| 617 | } |
| 618 | if (!rc) |
| 619 | return false; |
| 620 | } |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 621 | return true; |
| 622 | } |
| 623 | |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 624 | /* |
| 625 | * In addition to knowing that we need to appraise the file in general, |
Mimi Zohar | 5a73fcf | 2012-12-05 15:14:38 -0500 | [diff] [blame] | 626 | * we need to differentiate between calling hooks, for hook specific rules. |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 627 | */ |
Mimi Zohar | 4ad87a3 | 2016-01-14 20:59:14 -0500 | [diff] [blame] | 628 | static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func) |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 629 | { |
Mimi Zohar | 5a73fcf | 2012-12-05 15:14:38 -0500 | [diff] [blame] | 630 | if (!(rule->flags & IMA_FUNC)) |
| 631 | return IMA_FILE_APPRAISE; |
| 632 | |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 633 | switch (func) { |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 634 | case MMAP_CHECK: |
| 635 | return IMA_MMAP_APPRAISE; |
| 636 | case BPRM_CHECK: |
| 637 | return IMA_BPRM_APPRAISE; |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 638 | case CREDS_CHECK: |
| 639 | return IMA_CREDS_APPRAISE; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 640 | case FILE_CHECK: |
Mimi Zohar | c6af8ef | 2015-11-19 12:39:22 -0500 | [diff] [blame] | 641 | case POST_SETATTR: |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 642 | return IMA_FILE_APPRAISE; |
Mimi Zohar | c6af8ef | 2015-11-19 12:39:22 -0500 | [diff] [blame] | 643 | case MODULE_CHECK ... MAX_CHECK - 1: |
| 644 | default: |
| 645 | return IMA_READ_APPRAISE; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 649 | /** |
| 650 | * ima_match_policy - decision based on LSM and other conditions |
Christian Brauner | a2d2329 | 2021-01-21 14:19:45 +0100 | [diff] [blame] | 651 | * @mnt_userns: user namespace of the mount the inode was found from |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 652 | * @inode: pointer to an inode for which the policy decision is being made |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 653 | * @cred: pointer to a credentials structure for which the policy decision is |
| 654 | * being made |
| 655 | * @secid: LSM secid of the task to be validated |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 656 | * @func: IMA hook identifier |
| 657 | * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC) |
Eric Richter | 725de7f | 2016-06-01 13:14:02 -0500 | [diff] [blame] | 658 | * @pcr: set the pcr to extend |
Matthew Garrett | 19453ce0 | 2019-06-19 15:46:11 -0700 | [diff] [blame] | 659 | * @template_desc: the template that should be used for this rule |
Tushar Sugandhi | 2b4a247 | 2021-01-07 20:07:01 -0800 | [diff] [blame] | 660 | * @func_data: func specific data, may be NULL |
THOBY Simon | 1624dc0 | 2021-08-16 08:11:00 +0000 | [diff] [blame] | 661 | * @allowed_algos: allowlist of hash algorithms for the IMA xattr |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 662 | * |
| 663 | * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type) |
| 664 | * conditions. |
| 665 | * |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 666 | * Since the IMA policy may be updated multiple times we need to lock the |
| 667 | * list when walking it. Reads are many orders of magnitude more numerous |
| 668 | * than writes so ima_match_policy() is classical RCU candidate. |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 669 | */ |
Christian Brauner | a2d2329 | 2021-01-21 14:19:45 +0100 | [diff] [blame] | 670 | int ima_match_policy(struct user_namespace *mnt_userns, struct inode *inode, |
| 671 | const struct cred *cred, u32 secid, enum ima_hooks func, |
| 672 | int mask, int flags, int *pcr, |
Lakshmi Ramasubramanian | e9085e0 | 2019-12-11 08:47:06 -0800 | [diff] [blame] | 673 | struct ima_template_desc **template_desc, |
THOBY Simon | 1624dc0 | 2021-08-16 08:11:00 +0000 | [diff] [blame] | 674 | const char *func_data, unsigned int *allowed_algos) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 675 | { |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 676 | struct ima_rule_entry *entry; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 677 | int action = 0, actmask = flags | (flags << 1); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 678 | |
Lakshmi Ramasubramanian | dea87d0 | 2020-11-12 12:39:59 -0800 | [diff] [blame] | 679 | if (template_desc && !*template_desc) |
Mimi Zohar | b36f281 | 2019-07-19 07:16:57 -0400 | [diff] [blame] | 680 | *template_desc = ima_template_desc_current(); |
| 681 | |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 682 | rcu_read_lock(); |
| 683 | list_for_each_entry_rcu(entry, ima_rules, list) { |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 684 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 685 | if (!(entry->action & actmask)) |
| 686 | continue; |
| 687 | |
Christian Brauner | a2d2329 | 2021-01-21 14:19:45 +0100 | [diff] [blame] | 688 | if (!ima_match_rules(entry, mnt_userns, inode, cred, secid, |
Linus Torvalds | 7d6beb7 | 2021-02-23 13:39:45 -0800 | [diff] [blame] | 689 | func, mask, func_data)) |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 690 | continue; |
| 691 | |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 692 | action |= entry->flags & IMA_ACTION_FLAGS; |
| 693 | |
Dmitry Kasatkin | 45e2472 | 2012-09-12 20:51:32 +0300 | [diff] [blame] | 694 | action |= entry->action & IMA_DO_MASK; |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 695 | if (entry->action & IMA_APPRAISE) { |
Mimi Zohar | 5a73fcf | 2012-12-05 15:14:38 -0500 | [diff] [blame] | 696 | action |= get_subaction(entry, func); |
Mimi Zohar | a9a4935 | 2018-03-10 23:07:34 -0500 | [diff] [blame] | 697 | action &= ~IMA_HASH; |
Mimi Zohar | 9e67028 | 2018-02-21 11:36:32 -0500 | [diff] [blame] | 698 | if (ima_fail_unverifiable_sigs) |
| 699 | action |= IMA_FAIL_UNVERIFIABLE_SIGS; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 700 | |
THOBY Simon | 1624dc0 | 2021-08-16 08:11:00 +0000 | [diff] [blame] | 701 | if (allowed_algos && |
| 702 | entry->flags & IMA_VALIDATE_ALGOS) |
| 703 | *allowed_algos = entry->allowed_algos; |
| 704 | } |
Mimi Zohar | b36f281 | 2019-07-19 07:16:57 -0400 | [diff] [blame] | 705 | |
Dmitry Kasatkin | 45e2472 | 2012-09-12 20:51:32 +0300 | [diff] [blame] | 706 | if (entry->action & IMA_DO_MASK) |
| 707 | actmask &= ~(entry->action | entry->action << 1); |
| 708 | else |
| 709 | actmask &= ~(entry->action | entry->action >> 1); |
| 710 | |
Eric Richter | 725de7f | 2016-06-01 13:14:02 -0500 | [diff] [blame] | 711 | if ((pcr) && (entry->flags & IMA_PCR)) |
| 712 | *pcr = entry->pcr; |
| 713 | |
Matthew Garrett | 19453ce0 | 2019-06-19 15:46:11 -0700 | [diff] [blame] | 714 | if (template_desc && entry->template) |
| 715 | *template_desc = entry->template; |
Matthew Garrett | 19453ce0 | 2019-06-19 15:46:11 -0700 | [diff] [blame] | 716 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 717 | if (!actmask) |
| 718 | break; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 719 | } |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 720 | rcu_read_unlock(); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 721 | |
| 722 | return action; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 723 | } |
| 724 | |
THOBY Simon | 4f2946a | 2021-08-16 08:11:01 +0000 | [diff] [blame] | 725 | /** |
| 726 | * ima_update_policy_flags() - Update global IMA variables |
| 727 | * |
| 728 | * Update ima_policy_flag and ima_setxattr_allowed_hash_algorithms |
| 729 | * based on the currently loaded policy. |
| 730 | * |
| 731 | * With ima_policy_flag, the decision to short circuit out of a function |
| 732 | * or not call the function in the first place can be made earlier. |
| 733 | * |
| 734 | * With ima_setxattr_allowed_hash_algorithms, the policy can restrict the |
| 735 | * set of hash algorithms accepted when updating the security.ima xattr of |
| 736 | * a file. |
| 737 | * |
| 738 | * Context: called after a policy update and at system initialization. |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 739 | */ |
THOBY Simon | 4f2946a | 2021-08-16 08:11:01 +0000 | [diff] [blame] | 740 | void ima_update_policy_flags(void) |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 741 | { |
| 742 | struct ima_rule_entry *entry; |
THOBY Simon | 4f2946a | 2021-08-16 08:11:01 +0000 | [diff] [blame] | 743 | int new_policy_flag = 0; |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 744 | |
THOBY Simon | 4f2946a | 2021-08-16 08:11:01 +0000 | [diff] [blame] | 745 | rcu_read_lock(); |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 746 | list_for_each_entry(entry, ima_rules, list) { |
THOBY Simon | 4f2946a | 2021-08-16 08:11:01 +0000 | [diff] [blame] | 747 | /* |
| 748 | * SETXATTR_CHECK rules do not implement a full policy check |
| 749 | * because rule checking would probably have an important |
| 750 | * performance impact on setxattr(). As a consequence, only one |
| 751 | * SETXATTR_CHECK can be active at a given time. |
| 752 | * Because we want to preserve that property, we set out to use |
| 753 | * atomic_cmpxchg. Either: |
| 754 | * - the atomic was non-zero: a setxattr hash policy is |
| 755 | * already enforced, we do nothing |
| 756 | * - the atomic was zero: no setxattr policy was set, enable |
| 757 | * the setxattr hash policy |
| 758 | */ |
| 759 | if (entry->func == SETXATTR_CHECK) { |
| 760 | atomic_cmpxchg(&ima_setxattr_allowed_hash_algorithms, |
| 761 | 0, entry->allowed_algos); |
| 762 | /* SETXATTR_CHECK doesn't impact ima_policy_flag */ |
| 763 | continue; |
| 764 | } |
| 765 | |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 766 | if (entry->action & IMA_DO_MASK) |
THOBY Simon | 4f2946a | 2021-08-16 08:11:01 +0000 | [diff] [blame] | 767 | new_policy_flag |= entry->action; |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 768 | } |
THOBY Simon | 4f2946a | 2021-08-16 08:11:01 +0000 | [diff] [blame] | 769 | rcu_read_unlock(); |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 770 | |
Mimi Zohar | ef96837 | 2018-07-13 14:06:01 -0400 | [diff] [blame] | 771 | ima_appraise |= (build_ima_appraise | temp_ima_appraise); |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 772 | if (!ima_appraise) |
THOBY Simon | 4f2946a | 2021-08-16 08:11:01 +0000 | [diff] [blame] | 773 | new_policy_flag &= ~IMA_APPRAISE; |
| 774 | |
| 775 | ima_policy_flag = new_policy_flag; |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 776 | } |
| 777 | |
Mimi Zohar | 6f0911a | 2018-04-12 00:15:22 -0400 | [diff] [blame] | 778 | static int ima_appraise_flag(enum ima_hooks func) |
| 779 | { |
| 780 | if (func == MODULE_CHECK) |
| 781 | return IMA_APPRAISE_MODULES; |
| 782 | else if (func == FIRMWARE_CHECK) |
| 783 | return IMA_APPRAISE_FIRMWARE; |
| 784 | else if (func == POLICY_CHECK) |
| 785 | return IMA_APPRAISE_POLICY; |
Mimi Zohar | 16c267a | 2018-07-13 14:05:58 -0400 | [diff] [blame] | 786 | else if (func == KEXEC_KERNEL_CHECK) |
| 787 | return IMA_APPRAISE_KEXEC; |
Mimi Zohar | 6f0911a | 2018-04-12 00:15:22 -0400 | [diff] [blame] | 788 | return 0; |
| 789 | } |
| 790 | |
Nayna Jain | c52657d | 2018-10-09 23:00:35 +0530 | [diff] [blame] | 791 | static void add_rules(struct ima_rule_entry *entries, int count, |
| 792 | enum policy_rule_list policy_rule) |
| 793 | { |
| 794 | int i = 0; |
| 795 | |
| 796 | for (i = 0; i < count; i++) { |
| 797 | struct ima_rule_entry *entry; |
| 798 | |
| 799 | if (policy_rule & IMA_DEFAULT_POLICY) |
| 800 | list_add_tail(&entries[i].list, &ima_default_rules); |
| 801 | |
| 802 | if (policy_rule & IMA_CUSTOM_POLICY) { |
| 803 | entry = kmemdup(&entries[i], sizeof(*entry), |
| 804 | GFP_KERNEL); |
| 805 | if (!entry) |
| 806 | continue; |
| 807 | |
| 808 | list_add_tail(&entry->list, &ima_policy_rules); |
| 809 | } |
Krzysztof Struczynski | b59fda4 | 2020-04-27 12:28:59 +0200 | [diff] [blame] | 810 | if (entries[i].action == APPRAISE) { |
| 811 | if (entries != build_appraise_rules) |
| 812 | temp_ima_appraise |= |
| 813 | ima_appraise_flag(entries[i].func); |
| 814 | else |
| 815 | build_ima_appraise |= |
| 816 | ima_appraise_flag(entries[i].func); |
| 817 | } |
Nayna Jain | c52657d | 2018-10-09 23:00:35 +0530 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | |
Nayna Jain | 6191706 | 2018-10-09 23:00:36 +0530 | [diff] [blame] | 821 | static int ima_parse_rule(char *rule, struct ima_rule_entry *entry); |
| 822 | |
| 823 | static int __init ima_init_arch_policy(void) |
| 824 | { |
| 825 | const char * const *arch_rules; |
| 826 | const char * const *rules; |
| 827 | int arch_entries = 0; |
| 828 | int i = 0; |
| 829 | |
| 830 | arch_rules = arch_get_ima_policy(); |
| 831 | if (!arch_rules) |
| 832 | return arch_entries; |
| 833 | |
| 834 | /* Get number of rules */ |
| 835 | for (rules = arch_rules; *rules != NULL; rules++) |
| 836 | arch_entries++; |
| 837 | |
| 838 | arch_policy_entry = kcalloc(arch_entries + 1, |
| 839 | sizeof(*arch_policy_entry), GFP_KERNEL); |
| 840 | if (!arch_policy_entry) |
| 841 | return 0; |
| 842 | |
| 843 | /* Convert each policy string rules to struct ima_rule_entry format */ |
| 844 | for (rules = arch_rules, i = 0; *rules != NULL; rules++) { |
| 845 | char rule[255]; |
| 846 | int result; |
| 847 | |
| 848 | result = strlcpy(rule, *rules, sizeof(rule)); |
| 849 | |
| 850 | INIT_LIST_HEAD(&arch_policy_entry[i].list); |
| 851 | result = ima_parse_rule(rule, &arch_policy_entry[i]); |
| 852 | if (result) { |
| 853 | pr_warn("Skipping unknown architecture policy rule: %s\n", |
| 854 | rule); |
| 855 | memset(&arch_policy_entry[i], 0, |
| 856 | sizeof(*arch_policy_entry)); |
| 857 | continue; |
| 858 | } |
| 859 | i++; |
| 860 | } |
| 861 | return i; |
| 862 | } |
| 863 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 864 | /** |
| 865 | * ima_init_policy - initialize the default measure rules. |
| 866 | * |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 867 | * ima_rules points to either the ima_default_rules or the |
| 868 | * the new ima_policy_rules. |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 869 | */ |
Eric Paris | 932995f | 2009-05-21 15:43:32 -0400 | [diff] [blame] | 870 | void __init ima_init_policy(void) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 871 | { |
Nayna Jain | 6191706 | 2018-10-09 23:00:36 +0530 | [diff] [blame] | 872 | int build_appraise_entries, arch_entries; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 873 | |
Nayna Jain | c52657d | 2018-10-09 23:00:35 +0530 | [diff] [blame] | 874 | /* if !ima_policy, we load NO default rules */ |
| 875 | if (ima_policy) |
| 876 | add_rules(dont_measure_rules, ARRAY_SIZE(dont_measure_rules), |
| 877 | IMA_DEFAULT_POLICY); |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 878 | |
| 879 | switch (ima_policy) { |
| 880 | case ORIGINAL_TCB: |
Nayna Jain | c52657d | 2018-10-09 23:00:35 +0530 | [diff] [blame] | 881 | add_rules(original_measurement_rules, |
| 882 | ARRAY_SIZE(original_measurement_rules), |
| 883 | IMA_DEFAULT_POLICY); |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 884 | break; |
| 885 | case DEFAULT_TCB: |
Nayna Jain | c52657d | 2018-10-09 23:00:35 +0530 | [diff] [blame] | 886 | add_rules(default_measurement_rules, |
| 887 | ARRAY_SIZE(default_measurement_rules), |
| 888 | IMA_DEFAULT_POLICY); |
Gustavo A. R. Silva | 28073eb | 2020-11-20 12:25:46 -0600 | [diff] [blame] | 889 | break; |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 890 | default: |
| 891 | break; |
| 892 | } |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 893 | |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 894 | /* |
Nayna Jain | 6191706 | 2018-10-09 23:00:36 +0530 | [diff] [blame] | 895 | * Based on runtime secure boot flags, insert arch specific measurement |
| 896 | * and appraise rules requiring file signatures for both the initial |
| 897 | * and custom policies, prior to other appraise rules. |
| 898 | * (Highest priority) |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 899 | */ |
Nayna Jain | 6191706 | 2018-10-09 23:00:36 +0530 | [diff] [blame] | 900 | arch_entries = ima_init_arch_policy(); |
| 901 | if (!arch_entries) |
| 902 | pr_info("No architecture policies found\n"); |
| 903 | else |
| 904 | add_rules(arch_policy_entry, arch_entries, |
| 905 | IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY); |
| 906 | |
| 907 | /* |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 908 | * Insert the builtin "secure_boot" policy rules requiring file |
Nayna Jain | 6191706 | 2018-10-09 23:00:36 +0530 | [diff] [blame] | 909 | * signatures, prior to other appraise rules. |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 910 | */ |
Nayna Jain | c52657d | 2018-10-09 23:00:35 +0530 | [diff] [blame] | 911 | if (ima_use_secure_boot) |
| 912 | add_rules(secure_boot_rules, ARRAY_SIZE(secure_boot_rules), |
| 913 | IMA_DEFAULT_POLICY); |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 914 | |
Mimi Zohar | ef96837 | 2018-07-13 14:06:01 -0400 | [diff] [blame] | 915 | /* |
| 916 | * Insert the build time appraise rules requiring file signatures |
| 917 | * for both the initial and custom policies, prior to other appraise |
Nayna Jain | c52657d | 2018-10-09 23:00:35 +0530 | [diff] [blame] | 918 | * rules. As the secure boot rules includes all of the build time |
| 919 | * rules, include either one or the other set of rules, but not both. |
Mimi Zohar | ef96837 | 2018-07-13 14:06:01 -0400 | [diff] [blame] | 920 | */ |
Nayna Jain | c52657d | 2018-10-09 23:00:35 +0530 | [diff] [blame] | 921 | build_appraise_entries = ARRAY_SIZE(build_appraise_rules); |
| 922 | if (build_appraise_entries) { |
| 923 | if (ima_use_secure_boot) |
| 924 | add_rules(build_appraise_rules, build_appraise_entries, |
| 925 | IMA_CUSTOM_POLICY); |
| 926 | else |
| 927 | add_rules(build_appraise_rules, build_appraise_entries, |
| 928 | IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY); |
Mimi Zohar | ef96837 | 2018-07-13 14:06:01 -0400 | [diff] [blame] | 929 | } |
| 930 | |
Nayna Jain | c52657d | 2018-10-09 23:00:35 +0530 | [diff] [blame] | 931 | if (ima_use_appraise_tcb) |
| 932 | add_rules(default_appraise_rules, |
| 933 | ARRAY_SIZE(default_appraise_rules), |
| 934 | IMA_DEFAULT_POLICY); |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 935 | |
Lakshmi Ramasubramanian | 03cee16 | 2021-01-07 20:07:07 -0800 | [diff] [blame] | 936 | if (ima_use_critical_data) |
| 937 | add_rules(critical_data_rules, |
| 938 | ARRAY_SIZE(critical_data_rules), |
| 939 | IMA_DEFAULT_POLICY); |
| 940 | |
THOBY Simon | 4f2946a | 2021-08-16 08:11:01 +0000 | [diff] [blame] | 941 | atomic_set(&ima_setxattr_allowed_hash_algorithms, 0); |
| 942 | |
| 943 | ima_update_policy_flags(); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 944 | } |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 945 | |
Sasha Levin | 0112721 | 2015-12-22 08:51:23 -0500 | [diff] [blame] | 946 | /* Make sure we have a valid policy, at least containing some rules. */ |
Colin Ian King | c75d8e9 | 2016-01-20 11:13:46 +0000 | [diff] [blame] | 947 | int ima_check_policy(void) |
Sasha Levin | 0112721 | 2015-12-22 08:51:23 -0500 | [diff] [blame] | 948 | { |
| 949 | if (list_empty(&ima_temp_rules)) |
| 950 | return -EINVAL; |
| 951 | return 0; |
| 952 | } |
| 953 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 954 | /** |
| 955 | * ima_update_policy - update default_rules with new measure rules |
| 956 | * |
| 957 | * Called on file .release to update the default rules with a complete new |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 958 | * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so |
| 959 | * they make a queue. The policy may be updated multiple times and this is the |
| 960 | * RCU updater. |
| 961 | * |
| 962 | * Policy rules are never deleted so ima_policy_flag gets zeroed only once when |
| 963 | * we switch from the default policy to user defined. |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 964 | */ |
| 965 | void ima_update_policy(void) |
| 966 | { |
Petko Manolov | 53b626f | 2018-05-22 17:06:55 +0300 | [diff] [blame] | 967 | struct list_head *policy = &ima_policy_rules; |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 968 | |
Petko Manolov | 53b626f | 2018-05-22 17:06:55 +0300 | [diff] [blame] | 969 | list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu); |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 970 | |
| 971 | if (ima_rules != policy) { |
| 972 | ima_policy_flag = 0; |
| 973 | ima_rules = policy; |
Nayna Jain | 6191706 | 2018-10-09 23:00:36 +0530 | [diff] [blame] | 974 | |
| 975 | /* |
| 976 | * IMA architecture specific policy rules are specified |
| 977 | * as strings and converted to an array of ima_entry_rules |
| 978 | * on boot. After loading a custom policy, free the |
| 979 | * architecture specific rules stored as an array. |
| 980 | */ |
| 981 | kfree(arch_policy_entry); |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 982 | } |
THOBY Simon | 4f2946a | 2021-08-16 08:11:01 +0000 | [diff] [blame] | 983 | ima_update_policy_flags(); |
Lakshmi Ramasubramanian | 450d0fd | 2020-01-22 17:32:05 -0800 | [diff] [blame] | 984 | |
| 985 | /* Custom IMA policy has been loaded */ |
| 986 | ima_process_queued_keys(); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 987 | } |
| 988 | |
Mimi Zohar | 1a9430d | 2018-12-17 19:14:49 -0500 | [diff] [blame] | 989 | /* Keep the enumeration in sync with the policy_tokens! */ |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 990 | enum { |
Mimi Zohar | 1a9430d | 2018-12-17 19:14:49 -0500 | [diff] [blame] | 991 | Opt_measure, Opt_dont_measure, |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 992 | Opt_appraise, Opt_dont_appraise, |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 993 | Opt_audit, Opt_hash, Opt_dont_hash, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 994 | Opt_obj_user, Opt_obj_role, Opt_obj_type, |
| 995 | Opt_subj_user, Opt_subj_role, Opt_subj_type, |
Mimi Zohar | f1b08bb | 2018-01-15 11:20:36 -0500 | [diff] [blame] | 996 | Opt_func, Opt_mask, Opt_fsmagic, Opt_fsname, |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 997 | Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq, |
| 998 | Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt, |
| 999 | Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt, |
THOBY Simon | 583a80a | 2021-08-16 08:11:00 +0000 | [diff] [blame] | 1000 | Opt_appraise_type, Opt_appraise_flag, Opt_appraise_algos, |
Lakshmi Ramasubramanian | 2b60c0e | 2019-12-11 08:47:07 -0800 | [diff] [blame] | 1001 | Opt_permit_directio, Opt_pcr, Opt_template, Opt_keyrings, |
Tushar Sugandhi | 47d76a4 | 2021-01-07 20:07:05 -0800 | [diff] [blame] | 1002 | Opt_label, Opt_err |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1003 | }; |
| 1004 | |
Mimi Zohar | 1a9430d | 2018-12-17 19:14:49 -0500 | [diff] [blame] | 1005 | static const match_table_t policy_tokens = { |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1006 | {Opt_measure, "measure"}, |
| 1007 | {Opt_dont_measure, "dont_measure"}, |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 1008 | {Opt_appraise, "appraise"}, |
| 1009 | {Opt_dont_appraise, "dont_appraise"}, |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 1010 | {Opt_audit, "audit"}, |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 1011 | {Opt_hash, "hash"}, |
| 1012 | {Opt_dont_hash, "dont_hash"}, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1013 | {Opt_obj_user, "obj_user=%s"}, |
| 1014 | {Opt_obj_role, "obj_role=%s"}, |
| 1015 | {Opt_obj_type, "obj_type=%s"}, |
| 1016 | {Opt_subj_user, "subj_user=%s"}, |
| 1017 | {Opt_subj_role, "subj_role=%s"}, |
| 1018 | {Opt_subj_type, "subj_type=%s"}, |
| 1019 | {Opt_func, "func=%s"}, |
| 1020 | {Opt_mask, "mask=%s"}, |
| 1021 | {Opt_fsmagic, "fsmagic=%s"}, |
Mimi Zohar | f1b08bb | 2018-01-15 11:20:36 -0500 | [diff] [blame] | 1022 | {Opt_fsname, "fsname=%s"}, |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 1023 | {Opt_fsuuid, "fsuuid=%s"}, |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1024 | {Opt_uid_eq, "uid=%s"}, |
| 1025 | {Opt_euid_eq, "euid=%s"}, |
| 1026 | {Opt_fowner_eq, "fowner=%s"}, |
| 1027 | {Opt_uid_gt, "uid>%s"}, |
| 1028 | {Opt_euid_gt, "euid>%s"}, |
| 1029 | {Opt_fowner_gt, "fowner>%s"}, |
| 1030 | {Opt_uid_lt, "uid<%s"}, |
| 1031 | {Opt_euid_lt, "euid<%s"}, |
| 1032 | {Opt_fowner_lt, "fowner<%s"}, |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 1033 | {Opt_appraise_type, "appraise_type=%s"}, |
Nayna Jain | 273df86 | 2019-10-30 23:31:32 -0400 | [diff] [blame] | 1034 | {Opt_appraise_flag, "appraise_flag=%s"}, |
THOBY Simon | 583a80a | 2021-08-16 08:11:00 +0000 | [diff] [blame] | 1035 | {Opt_appraise_algos, "appraise_algos=%s"}, |
Mimi Zohar | f9b2a73 | 2014-05-12 09:28:11 -0400 | [diff] [blame] | 1036 | {Opt_permit_directio, "permit_directio"}, |
Eric Richter | 0260643 | 2016-06-01 13:14:01 -0500 | [diff] [blame] | 1037 | {Opt_pcr, "pcr=%s"}, |
Matthew Garrett | 19453ce0 | 2019-06-19 15:46:11 -0700 | [diff] [blame] | 1038 | {Opt_template, "template=%s"}, |
Lakshmi Ramasubramanian | 2b60c0e | 2019-12-11 08:47:07 -0800 | [diff] [blame] | 1039 | {Opt_keyrings, "keyrings=%s"}, |
Tushar Sugandhi | 47d76a4 | 2021-01-07 20:07:05 -0800 | [diff] [blame] | 1040 | {Opt_label, "label=%s"}, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1041 | {Opt_err, NULL} |
| 1042 | }; |
| 1043 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 1044 | static int ima_lsm_rule_init(struct ima_rule_entry *entry, |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 1045 | substring_t *args, int lsm_rule, int audit_type) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1046 | { |
| 1047 | int result; |
| 1048 | |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 1049 | if (entry->lsm[lsm_rule].rule) |
| 1050 | return -EINVAL; |
| 1051 | |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 1052 | entry->lsm[lsm_rule].args_p = match_strdup(args); |
| 1053 | if (!entry->lsm[lsm_rule].args_p) |
| 1054 | return -ENOMEM; |
| 1055 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1056 | entry->lsm[lsm_rule].type = audit_type; |
Tyler Hicks | b8867ee | 2020-07-10 15:37:50 -0500 | [diff] [blame] | 1057 | result = ima_filter_rule_init(entry->lsm[lsm_rule].type, Audit_equal, |
| 1058 | entry->lsm[lsm_rule].args_p, |
| 1059 | &entry->lsm[lsm_rule].rule); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 1060 | if (!entry->lsm[lsm_rule].rule) { |
Janne Karhunen | 483ec26 | 2020-01-15 17:42:30 +0200 | [diff] [blame] | 1061 | pr_warn("rule for LSM \'%s\' is undefined\n", |
Tyler Hicks | aa0c022 | 2020-07-09 01:19:08 -0500 | [diff] [blame] | 1062 | entry->lsm[lsm_rule].args_p); |
Janne Karhunen | 483ec26 | 2020-01-15 17:42:30 +0200 | [diff] [blame] | 1063 | |
| 1064 | if (ima_rules == &ima_default_rules) { |
| 1065 | kfree(entry->lsm[lsm_rule].args_p); |
Tyler Hicks | 2bdd737 | 2020-07-09 01:19:02 -0500 | [diff] [blame] | 1066 | entry->lsm[lsm_rule].args_p = NULL; |
Janne Karhunen | 483ec26 | 2020-01-15 17:42:30 +0200 | [diff] [blame] | 1067 | result = -EINVAL; |
| 1068 | } else |
| 1069 | result = 0; |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 1070 | } |
| 1071 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1072 | return result; |
| 1073 | } |
| 1074 | |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1075 | static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value, |
| 1076 | bool (*rule_operator)(kuid_t, kuid_t)) |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1077 | { |
Stefan Berger | 2afd020 | 2018-06-04 16:54:54 -0400 | [diff] [blame] | 1078 | if (!ab) |
| 1079 | return; |
| 1080 | |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1081 | if (rule_operator == &uid_gt) |
| 1082 | audit_log_format(ab, "%s>", key); |
| 1083 | else if (rule_operator == &uid_lt) |
| 1084 | audit_log_format(ab, "%s<", key); |
| 1085 | else |
| 1086 | audit_log_format(ab, "%s=", key); |
Stefan Berger | 3d2859d | 2018-06-04 16:54:53 -0400 | [diff] [blame] | 1087 | audit_log_format(ab, "%s ", value); |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1088 | } |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1089 | static void ima_log_string(struct audit_buffer *ab, char *key, char *value) |
| 1090 | { |
| 1091 | ima_log_string_op(ab, key, value, NULL); |
| 1092 | } |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1093 | |
Thiago Jung Bauermann | 3878d50 | 2019-06-27 23:19:32 -0300 | [diff] [blame] | 1094 | /* |
| 1095 | * Validating the appended signature included in the measurement list requires |
| 1096 | * the file hash calculated without the appended signature (i.e., the 'd-modsig' |
| 1097 | * field). Therefore, notify the user if they have the 'modsig' field but not |
| 1098 | * the 'd-modsig' field in the template. |
| 1099 | */ |
| 1100 | static void check_template_modsig(const struct ima_template_desc *template) |
| 1101 | { |
| 1102 | #define MSG "template with 'modsig' field also needs 'd-modsig' field\n" |
| 1103 | bool has_modsig, has_dmodsig; |
| 1104 | static bool checked; |
| 1105 | int i; |
| 1106 | |
| 1107 | /* We only need to notify the user once. */ |
| 1108 | if (checked) |
| 1109 | return; |
| 1110 | |
| 1111 | has_modsig = has_dmodsig = false; |
| 1112 | for (i = 0; i < template->num_fields; i++) { |
| 1113 | if (!strcmp(template->fields[i]->field_id, "modsig")) |
| 1114 | has_modsig = true; |
| 1115 | else if (!strcmp(template->fields[i]->field_id, "d-modsig")) |
| 1116 | has_dmodsig = true; |
| 1117 | } |
| 1118 | |
| 1119 | if (has_modsig && !has_dmodsig) |
| 1120 | pr_notice(MSG); |
| 1121 | |
| 1122 | checked = true; |
| 1123 | #undef MSG |
| 1124 | } |
| 1125 | |
Tyler Hicks | 71218343 | 2020-07-09 01:19:03 -0500 | [diff] [blame] | 1126 | static bool ima_validate_rule(struct ima_rule_entry *entry) |
| 1127 | { |
Tyler Hicks | 30031b0 | 2020-07-09 01:19:09 -0500 | [diff] [blame] | 1128 | /* Ensure that the action is set and is compatible with the flags */ |
Tyler Hicks | 71218343 | 2020-07-09 01:19:03 -0500 | [diff] [blame] | 1129 | if (entry->action == UNKNOWN) |
| 1130 | return false; |
| 1131 | |
Tyler Hicks | 30031b0 | 2020-07-09 01:19:09 -0500 | [diff] [blame] | 1132 | if (entry->action != MEASURE && entry->flags & IMA_PCR) |
| 1133 | return false; |
| 1134 | |
| 1135 | if (entry->action != APPRAISE && |
THOBY Simon | 583a80a | 2021-08-16 08:11:00 +0000 | [diff] [blame] | 1136 | entry->flags & (IMA_DIGSIG_REQUIRED | IMA_MODSIG_ALLOWED | |
| 1137 | IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS)) |
Tyler Hicks | 30031b0 | 2020-07-09 01:19:09 -0500 | [diff] [blame] | 1138 | return false; |
| 1139 | |
| 1140 | /* |
| 1141 | * The IMA_FUNC bit must be set if and only if there's a valid hook |
| 1142 | * function specified, and vice versa. Enforcing this property allows |
| 1143 | * for the NONE case below to validate a rule without an explicit hook |
| 1144 | * function. |
| 1145 | */ |
| 1146 | if (((entry->flags & IMA_FUNC) && entry->func == NONE) || |
| 1147 | (!(entry->flags & IMA_FUNC) && entry->func != NONE)) |
| 1148 | return false; |
| 1149 | |
Tyler Hicks | 71218343 | 2020-07-09 01:19:03 -0500 | [diff] [blame] | 1150 | /* |
| 1151 | * Ensure that the hook function is compatible with the other |
| 1152 | * components of the rule |
| 1153 | */ |
| 1154 | switch (entry->func) { |
| 1155 | case NONE: |
| 1156 | case FILE_CHECK: |
| 1157 | case MMAP_CHECK: |
| 1158 | case BPRM_CHECK: |
| 1159 | case CREDS_CHECK: |
| 1160 | case POST_SETATTR: |
Tyler Hicks | 71218343 | 2020-07-09 01:19:03 -0500 | [diff] [blame] | 1161 | case FIRMWARE_CHECK: |
Tyler Hicks | 30031b0 | 2020-07-09 01:19:09 -0500 | [diff] [blame] | 1162 | case POLICY_CHECK: |
| 1163 | if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC | |
| 1164 | IMA_UID | IMA_FOWNER | IMA_FSUUID | |
| 1165 | IMA_INMASK | IMA_EUID | IMA_PCR | |
| 1166 | IMA_FSNAME | IMA_DIGSIG_REQUIRED | |
THOBY Simon | 583a80a | 2021-08-16 08:11:00 +0000 | [diff] [blame] | 1167 | IMA_PERMIT_DIRECTIO | IMA_VALIDATE_ALGOS)) |
Tyler Hicks | 30031b0 | 2020-07-09 01:19:09 -0500 | [diff] [blame] | 1168 | return false; |
| 1169 | |
| 1170 | break; |
| 1171 | case MODULE_CHECK: |
Tyler Hicks | 71218343 | 2020-07-09 01:19:03 -0500 | [diff] [blame] | 1172 | case KEXEC_KERNEL_CHECK: |
| 1173 | case KEXEC_INITRAMFS_CHECK: |
Tyler Hicks | 30031b0 | 2020-07-09 01:19:09 -0500 | [diff] [blame] | 1174 | if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC | |
| 1175 | IMA_UID | IMA_FOWNER | IMA_FSUUID | |
| 1176 | IMA_INMASK | IMA_EUID | IMA_PCR | |
| 1177 | IMA_FSNAME | IMA_DIGSIG_REQUIRED | |
| 1178 | IMA_PERMIT_DIRECTIO | IMA_MODSIG_ALLOWED | |
THOBY Simon | 583a80a | 2021-08-16 08:11:00 +0000 | [diff] [blame] | 1179 | IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS)) |
Tyler Hicks | 30031b0 | 2020-07-09 01:19:09 -0500 | [diff] [blame] | 1180 | return false; |
| 1181 | |
Tyler Hicks | 71218343 | 2020-07-09 01:19:03 -0500 | [diff] [blame] | 1182 | break; |
| 1183 | case KEXEC_CMDLINE: |
Tyler Hicks | db2045f | 2020-07-09 01:19:04 -0500 | [diff] [blame] | 1184 | if (entry->action & ~(MEASURE | DONT_MEASURE)) |
| 1185 | return false; |
| 1186 | |
Tyler Hicks | 4834177 | 2020-07-09 01:19:11 -0500 | [diff] [blame] | 1187 | if (entry->flags & ~(IMA_FUNC | IMA_FSMAGIC | IMA_UID | |
| 1188 | IMA_FOWNER | IMA_FSUUID | IMA_EUID | |
| 1189 | IMA_PCR | IMA_FSNAME)) |
Tyler Hicks | db2045f | 2020-07-09 01:19:04 -0500 | [diff] [blame] | 1190 | return false; |
| 1191 | |
| 1192 | break; |
Tyler Hicks | 71218343 | 2020-07-09 01:19:03 -0500 | [diff] [blame] | 1193 | case KEY_CHECK: |
| 1194 | if (entry->action & ~(MEASURE | DONT_MEASURE)) |
| 1195 | return false; |
| 1196 | |
Tyler Hicks | eb624fe | 2020-07-09 01:19:05 -0500 | [diff] [blame] | 1197 | if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_PCR | |
| 1198 | IMA_KEYRINGS)) |
| 1199 | return false; |
| 1200 | |
| 1201 | if (ima_rule_contains_lsm_cond(entry)) |
| 1202 | return false; |
| 1203 | |
Tyler Hicks | 71218343 | 2020-07-09 01:19:03 -0500 | [diff] [blame] | 1204 | break; |
Tushar Sugandhi | c4e43aa | 2021-01-07 20:07:04 -0800 | [diff] [blame] | 1205 | case CRITICAL_DATA: |
| 1206 | if (entry->action & ~(MEASURE | DONT_MEASURE)) |
| 1207 | return false; |
| 1208 | |
Tushar Sugandhi | 47d76a4 | 2021-01-07 20:07:05 -0800 | [diff] [blame] | 1209 | if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_PCR | |
| 1210 | IMA_LABEL)) |
Tushar Sugandhi | c4e43aa | 2021-01-07 20:07:04 -0800 | [diff] [blame] | 1211 | return false; |
| 1212 | |
| 1213 | if (ima_rule_contains_lsm_cond(entry)) |
| 1214 | return false; |
| 1215 | |
| 1216 | break; |
THOBY Simon | 4f2946a | 2021-08-16 08:11:01 +0000 | [diff] [blame] | 1217 | case SETXATTR_CHECK: |
| 1218 | /* any action other than APPRAISE is unsupported */ |
| 1219 | if (entry->action != APPRAISE) |
| 1220 | return false; |
| 1221 | |
| 1222 | /* SETXATTR_CHECK requires an appraise_algos parameter */ |
| 1223 | if (!(entry->flags & IMA_VALIDATE_ALGOS)) |
| 1224 | return false; |
| 1225 | |
| 1226 | /* |
| 1227 | * full policies are not supported, they would have too |
| 1228 | * much of a performance impact |
| 1229 | */ |
| 1230 | if (entry->flags & ~(IMA_FUNC | IMA_VALIDATE_ALGOS)) |
| 1231 | return false; |
| 1232 | |
| 1233 | break; |
Tyler Hicks | 71218343 | 2020-07-09 01:19:03 -0500 | [diff] [blame] | 1234 | default: |
| 1235 | return false; |
| 1236 | } |
| 1237 | |
Tyler Hicks | 5f3e926 | 2020-07-09 01:19:06 -0500 | [diff] [blame] | 1238 | /* Ensure that combinations of flags are compatible with each other */ |
| 1239 | if (entry->flags & IMA_CHECK_BLACKLIST && |
| 1240 | !(entry->flags & IMA_MODSIG_ALLOWED)) |
| 1241 | return false; |
| 1242 | |
Tyler Hicks | 71218343 | 2020-07-09 01:19:03 -0500 | [diff] [blame] | 1243 | return true; |
| 1244 | } |
| 1245 | |
THOBY Simon | 583a80a | 2021-08-16 08:11:00 +0000 | [diff] [blame] | 1246 | static unsigned int ima_parse_appraise_algos(char *arg) |
| 1247 | { |
| 1248 | unsigned int res = 0; |
| 1249 | int idx; |
| 1250 | char *token; |
| 1251 | |
| 1252 | while ((token = strsep(&arg, ",")) != NULL) { |
| 1253 | idx = match_string(hash_algo_name, HASH_ALGO__LAST, token); |
| 1254 | |
| 1255 | if (idx < 0) { |
| 1256 | pr_err("unknown hash algorithm \"%s\"", |
| 1257 | token); |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
THOBY Simon | 8ecd39c | 2021-08-16 08:11:01 +0000 | [diff] [blame] | 1261 | if (!crypto_has_alg(hash_algo_name[idx], 0, 0)) { |
| 1262 | pr_err("unavailable hash algorithm \"%s\", check your kernel configuration", |
| 1263 | token); |
| 1264 | return 0; |
| 1265 | } |
| 1266 | |
THOBY Simon | 583a80a | 2021-08-16 08:11:00 +0000 | [diff] [blame] | 1267 | /* Add the hash algorithm to the 'allowed' bitfield */ |
| 1268 | res |= (1U << idx); |
| 1269 | } |
| 1270 | |
| 1271 | return res; |
| 1272 | } |
| 1273 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 1274 | static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1275 | { |
| 1276 | struct audit_buffer *ab; |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 1277 | char *from; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1278 | char *p; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1279 | bool uid_token; |
Matthew Garrett | 19453ce0 | 2019-06-19 15:46:11 -0700 | [diff] [blame] | 1280 | struct ima_template_desc *template_desc; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1281 | int result = 0; |
| 1282 | |
Stefan Berger | dba31ee | 2018-06-04 16:54:55 -0400 | [diff] [blame] | 1283 | ab = integrity_audit_log_start(audit_context(), GFP_KERNEL, |
| 1284 | AUDIT_INTEGRITY_POLICY_RULE); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1285 | |
Eric W. Biederman | 8b94eea | 2012-05-25 18:24:12 -0600 | [diff] [blame] | 1286 | entry->uid = INVALID_UID; |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 1287 | entry->fowner = INVALID_UID; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1288 | entry->uid_op = &uid_eq; |
| 1289 | entry->fowner_op = &uid_eq; |
Eric Paris | b9035b1 | 2010-04-20 10:21:07 -0400 | [diff] [blame] | 1290 | entry->action = UNKNOWN; |
Eric Paris | 28ef400 | 2010-04-20 10:21:18 -0400 | [diff] [blame] | 1291 | while ((p = strsep(&rule, " \t")) != NULL) { |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1292 | substring_t args[MAX_OPT_ARGS]; |
| 1293 | int token; |
| 1294 | unsigned long lnum; |
| 1295 | |
| 1296 | if (result < 0) |
| 1297 | break; |
Eric Paris | 28ef400 | 2010-04-20 10:21:18 -0400 | [diff] [blame] | 1298 | if ((*p == '\0') || (*p == ' ') || (*p == '\t')) |
| 1299 | continue; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1300 | token = match_token(p, policy_tokens, args); |
| 1301 | switch (token) { |
| 1302 | case Opt_measure: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1303 | ima_log_string(ab, "action", "measure"); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 1304 | |
| 1305 | if (entry->action != UNKNOWN) |
| 1306 | result = -EINVAL; |
| 1307 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1308 | entry->action = MEASURE; |
| 1309 | break; |
| 1310 | case Opt_dont_measure: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1311 | ima_log_string(ab, "action", "dont_measure"); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 1312 | |
| 1313 | if (entry->action != UNKNOWN) |
| 1314 | result = -EINVAL; |
| 1315 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1316 | entry->action = DONT_MEASURE; |
| 1317 | break; |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 1318 | case Opt_appraise: |
| 1319 | ima_log_string(ab, "action", "appraise"); |
| 1320 | |
| 1321 | if (entry->action != UNKNOWN) |
| 1322 | result = -EINVAL; |
| 1323 | |
| 1324 | entry->action = APPRAISE; |
| 1325 | break; |
| 1326 | case Opt_dont_appraise: |
| 1327 | ima_log_string(ab, "action", "dont_appraise"); |
| 1328 | |
| 1329 | if (entry->action != UNKNOWN) |
| 1330 | result = -EINVAL; |
| 1331 | |
| 1332 | entry->action = DONT_APPRAISE; |
| 1333 | break; |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 1334 | case Opt_audit: |
| 1335 | ima_log_string(ab, "action", "audit"); |
| 1336 | |
| 1337 | if (entry->action != UNKNOWN) |
| 1338 | result = -EINVAL; |
| 1339 | |
| 1340 | entry->action = AUDIT; |
| 1341 | break; |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 1342 | case Opt_hash: |
| 1343 | ima_log_string(ab, "action", "hash"); |
| 1344 | |
| 1345 | if (entry->action != UNKNOWN) |
| 1346 | result = -EINVAL; |
| 1347 | |
| 1348 | entry->action = HASH; |
| 1349 | break; |
| 1350 | case Opt_dont_hash: |
| 1351 | ima_log_string(ab, "action", "dont_hash"); |
| 1352 | |
| 1353 | if (entry->action != UNKNOWN) |
| 1354 | result = -EINVAL; |
| 1355 | |
| 1356 | entry->action = DONT_HASH; |
| 1357 | break; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1358 | case Opt_func: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1359 | ima_log_string(ab, "func", args[0].from); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 1360 | |
| 1361 | if (entry->func) |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 1362 | result = -EINVAL; |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 1363 | |
Mimi Zohar | 1e93d00 | 2010-01-26 17:02:41 -0500 | [diff] [blame] | 1364 | if (strcmp(args[0].from, "FILE_CHECK") == 0) |
| 1365 | entry->func = FILE_CHECK; |
| 1366 | /* PATH_CHECK is for backwards compat */ |
| 1367 | else if (strcmp(args[0].from, "PATH_CHECK") == 0) |
| 1368 | entry->func = FILE_CHECK; |
Mimi Zohar | fdf9072 | 2012-10-16 12:40:08 +1030 | [diff] [blame] | 1369 | else if (strcmp(args[0].from, "MODULE_CHECK") == 0) |
| 1370 | entry->func = MODULE_CHECK; |
Mimi Zohar | 5a9196d | 2014-07-22 10:39:48 -0400 | [diff] [blame] | 1371 | else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0) |
| 1372 | entry->func = FIRMWARE_CHECK; |
Mimi Zohar | 16cac49 | 2012-12-13 11:15:04 -0500 | [diff] [blame] | 1373 | else if ((strcmp(args[0].from, "FILE_MMAP") == 0) |
| 1374 | || (strcmp(args[0].from, "MMAP_CHECK") == 0)) |
| 1375 | entry->func = MMAP_CHECK; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1376 | else if (strcmp(args[0].from, "BPRM_CHECK") == 0) |
| 1377 | entry->func = BPRM_CHECK; |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 1378 | else if (strcmp(args[0].from, "CREDS_CHECK") == 0) |
| 1379 | entry->func = CREDS_CHECK; |
Mimi Zohar | d9ddf07 | 2016-01-14 20:59:14 -0500 | [diff] [blame] | 1380 | else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") == |
| 1381 | 0) |
| 1382 | entry->func = KEXEC_KERNEL_CHECK; |
| 1383 | else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK") |
| 1384 | == 0) |
| 1385 | entry->func = KEXEC_INITRAMFS_CHECK; |
Mimi Zohar | 19f8a84 | 2016-01-15 10:17:12 -0500 | [diff] [blame] | 1386 | else if (strcmp(args[0].from, "POLICY_CHECK") == 0) |
| 1387 | entry->func = POLICY_CHECK; |
Prakhar Srivastava | b093512 | 2019-06-23 23:23:29 -0700 | [diff] [blame] | 1388 | else if (strcmp(args[0].from, "KEXEC_CMDLINE") == 0) |
| 1389 | entry->func = KEXEC_CMDLINE; |
Tyler Hicks | 48ce1dd | 2020-08-11 14:26:21 -0500 | [diff] [blame] | 1390 | else if (IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) && |
| 1391 | strcmp(args[0].from, "KEY_CHECK") == 0) |
Lakshmi Ramasubramanian | 5808611 | 2019-12-11 08:47:03 -0800 | [diff] [blame] | 1392 | entry->func = KEY_CHECK; |
Tushar Sugandhi | c4e43aa | 2021-01-07 20:07:04 -0800 | [diff] [blame] | 1393 | else if (strcmp(args[0].from, "CRITICAL_DATA") == 0) |
| 1394 | entry->func = CRITICAL_DATA; |
THOBY Simon | 4f2946a | 2021-08-16 08:11:01 +0000 | [diff] [blame] | 1395 | else if (strcmp(args[0].from, "SETXATTR_CHECK") == 0) |
| 1396 | entry->func = SETXATTR_CHECK; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1397 | else |
| 1398 | result = -EINVAL; |
| 1399 | if (!result) |
| 1400 | entry->flags |= IMA_FUNC; |
| 1401 | break; |
| 1402 | case Opt_mask: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1403 | ima_log_string(ab, "mask", args[0].from); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 1404 | |
| 1405 | if (entry->mask) |
| 1406 | result = -EINVAL; |
| 1407 | |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 1408 | from = args[0].from; |
| 1409 | if (*from == '^') |
| 1410 | from++; |
| 1411 | |
| 1412 | if ((strcmp(from, "MAY_EXEC")) == 0) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1413 | entry->mask = MAY_EXEC; |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 1414 | else if (strcmp(from, "MAY_WRITE") == 0) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1415 | entry->mask = MAY_WRITE; |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 1416 | else if (strcmp(from, "MAY_READ") == 0) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1417 | entry->mask = MAY_READ; |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 1418 | else if (strcmp(from, "MAY_APPEND") == 0) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1419 | entry->mask = MAY_APPEND; |
| 1420 | else |
| 1421 | result = -EINVAL; |
| 1422 | if (!result) |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 1423 | entry->flags |= (*args[0].from == '^') |
| 1424 | ? IMA_INMASK : IMA_MASK; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1425 | break; |
| 1426 | case Opt_fsmagic: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1427 | ima_log_string(ab, "fsmagic", args[0].from); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 1428 | |
| 1429 | if (entry->fsmagic) { |
| 1430 | result = -EINVAL; |
| 1431 | break; |
| 1432 | } |
| 1433 | |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 1434 | result = kstrtoul(args[0].from, 16, &entry->fsmagic); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1435 | if (!result) |
| 1436 | entry->flags |= IMA_FSMAGIC; |
| 1437 | break; |
Mimi Zohar | f1b08bb | 2018-01-15 11:20:36 -0500 | [diff] [blame] | 1438 | case Opt_fsname: |
| 1439 | ima_log_string(ab, "fsname", args[0].from); |
| 1440 | |
| 1441 | entry->fsname = kstrdup(args[0].from, GFP_KERNEL); |
| 1442 | if (!entry->fsname) { |
| 1443 | result = -ENOMEM; |
| 1444 | break; |
| 1445 | } |
| 1446 | result = 0; |
| 1447 | entry->flags |= IMA_FSNAME; |
| 1448 | break; |
Lakshmi Ramasubramanian | 2b60c0e | 2019-12-11 08:47:07 -0800 | [diff] [blame] | 1449 | case Opt_keyrings: |
| 1450 | ima_log_string(ab, "keyrings", args[0].from); |
| 1451 | |
Tyler Hicks | 48ce1dd | 2020-08-11 14:26:21 -0500 | [diff] [blame] | 1452 | if (!IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) || |
| 1453 | entry->keyrings) { |
Lakshmi Ramasubramanian | 2b60c0e | 2019-12-11 08:47:07 -0800 | [diff] [blame] | 1454 | result = -EINVAL; |
| 1455 | break; |
| 1456 | } |
Lakshmi Ramasubramanian | 5c7bac9 | 2020-01-16 18:18:21 -0800 | [diff] [blame] | 1457 | |
Tyler Hicks | 176377d | 2020-08-11 14:26:20 -0500 | [diff] [blame] | 1458 | entry->keyrings = ima_alloc_rule_opt_list(args); |
| 1459 | if (IS_ERR(entry->keyrings)) { |
| 1460 | result = PTR_ERR(entry->keyrings); |
| 1461 | entry->keyrings = NULL; |
Lakshmi Ramasubramanian | 2b60c0e | 2019-12-11 08:47:07 -0800 | [diff] [blame] | 1462 | break; |
| 1463 | } |
Tyler Hicks | 176377d | 2020-08-11 14:26:20 -0500 | [diff] [blame] | 1464 | |
Lakshmi Ramasubramanian | 2b60c0e | 2019-12-11 08:47:07 -0800 | [diff] [blame] | 1465 | entry->flags |= IMA_KEYRINGS; |
| 1466 | break; |
Tushar Sugandhi | 47d76a4 | 2021-01-07 20:07:05 -0800 | [diff] [blame] | 1467 | case Opt_label: |
| 1468 | ima_log_string(ab, "label", args[0].from); |
| 1469 | |
| 1470 | if (entry->label) { |
| 1471 | result = -EINVAL; |
| 1472 | break; |
| 1473 | } |
| 1474 | |
| 1475 | entry->label = ima_alloc_rule_opt_list(args); |
| 1476 | if (IS_ERR(entry->label)) { |
| 1477 | result = PTR_ERR(entry->label); |
| 1478 | entry->label = NULL; |
| 1479 | break; |
| 1480 | } |
| 1481 | |
| 1482 | entry->flags |= IMA_LABEL; |
| 1483 | break; |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 1484 | case Opt_fsuuid: |
| 1485 | ima_log_string(ab, "fsuuid", args[0].from); |
| 1486 | |
Mike Rapoport | 3644745 | 2018-01-17 20:27:11 +0200 | [diff] [blame] | 1487 | if (!uuid_is_null(&entry->fsuuid)) { |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 1488 | result = -EINVAL; |
| 1489 | break; |
| 1490 | } |
| 1491 | |
Christoph Hellwig | 787d8c5 | 2017-06-01 07:00:26 +0200 | [diff] [blame] | 1492 | result = uuid_parse(args[0].from, &entry->fsuuid); |
Mimi Zohar | 446d64e | 2013-02-24 23:42:37 -0500 | [diff] [blame] | 1493 | if (!result) |
| 1494 | entry->flags |= IMA_FSUUID; |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 1495 | break; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1496 | case Opt_uid_gt: |
| 1497 | case Opt_euid_gt: |
| 1498 | entry->uid_op = &uid_gt; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1499 | fallthrough; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1500 | case Opt_uid_lt: |
| 1501 | case Opt_euid_lt: |
| 1502 | if ((token == Opt_uid_lt) || (token == Opt_euid_lt)) |
| 1503 | entry->uid_op = &uid_lt; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1504 | fallthrough; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1505 | case Opt_uid_eq: |
| 1506 | case Opt_euid_eq: |
| 1507 | uid_token = (token == Opt_uid_eq) || |
| 1508 | (token == Opt_uid_gt) || |
| 1509 | (token == Opt_uid_lt); |
| 1510 | |
| 1511 | ima_log_string_op(ab, uid_token ? "uid" : "euid", |
| 1512 | args[0].from, entry->uid_op); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 1513 | |
Eric W. Biederman | 8b94eea | 2012-05-25 18:24:12 -0600 | [diff] [blame] | 1514 | if (uid_valid(entry->uid)) { |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 1515 | result = -EINVAL; |
| 1516 | break; |
| 1517 | } |
| 1518 | |
Jingoo Han | 29707b2 | 2014-02-05 15:13:14 +0900 | [diff] [blame] | 1519 | result = kstrtoul(args[0].from, 10, &lnum); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1520 | if (!result) { |
Mimi Zohar | 139069e | 2014-11-05 07:48:36 -0500 | [diff] [blame] | 1521 | entry->uid = make_kuid(current_user_ns(), |
| 1522 | (uid_t) lnum); |
| 1523 | if (!uid_valid(entry->uid) || |
| 1524 | (uid_t)lnum != lnum) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1525 | result = -EINVAL; |
| 1526 | else |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1527 | entry->flags |= uid_token |
Mimi Zohar | 139069e | 2014-11-05 07:48:36 -0500 | [diff] [blame] | 1528 | ? IMA_UID : IMA_EUID; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1529 | } |
| 1530 | break; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1531 | case Opt_fowner_gt: |
| 1532 | entry->fowner_op = &uid_gt; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1533 | fallthrough; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1534 | case Opt_fowner_lt: |
| 1535 | if (token == Opt_fowner_lt) |
| 1536 | entry->fowner_op = &uid_lt; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1537 | fallthrough; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1538 | case Opt_fowner_eq: |
| 1539 | ima_log_string_op(ab, "fowner", args[0].from, |
| 1540 | entry->fowner_op); |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 1541 | |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 1542 | if (uid_valid(entry->fowner)) { |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 1543 | result = -EINVAL; |
| 1544 | break; |
| 1545 | } |
| 1546 | |
Jingoo Han | 29707b2 | 2014-02-05 15:13:14 +0900 | [diff] [blame] | 1547 | result = kstrtoul(args[0].from, 10, &lnum); |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 1548 | if (!result) { |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 1549 | entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum); |
| 1550 | if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum)) |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 1551 | result = -EINVAL; |
| 1552 | else |
| 1553 | entry->flags |= IMA_FOWNER; |
| 1554 | } |
| 1555 | break; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1556 | case Opt_obj_user: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1557 | ima_log_string(ab, "obj_user", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 1558 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1559 | LSM_OBJ_USER, |
| 1560 | AUDIT_OBJ_USER); |
| 1561 | break; |
| 1562 | case Opt_obj_role: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1563 | ima_log_string(ab, "obj_role", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 1564 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1565 | LSM_OBJ_ROLE, |
| 1566 | AUDIT_OBJ_ROLE); |
| 1567 | break; |
| 1568 | case Opt_obj_type: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1569 | ima_log_string(ab, "obj_type", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 1570 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1571 | LSM_OBJ_TYPE, |
| 1572 | AUDIT_OBJ_TYPE); |
| 1573 | break; |
| 1574 | case Opt_subj_user: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1575 | ima_log_string(ab, "subj_user", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 1576 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1577 | LSM_SUBJ_USER, |
| 1578 | AUDIT_SUBJ_USER); |
| 1579 | break; |
| 1580 | case Opt_subj_role: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1581 | ima_log_string(ab, "subj_role", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 1582 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1583 | LSM_SUBJ_ROLE, |
| 1584 | AUDIT_SUBJ_ROLE); |
| 1585 | break; |
| 1586 | case Opt_subj_type: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1587 | ima_log_string(ab, "subj_type", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 1588 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1589 | LSM_SUBJ_TYPE, |
| 1590 | AUDIT_SUBJ_TYPE); |
| 1591 | break; |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 1592 | case Opt_appraise_type: |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 1593 | ima_log_string(ab, "appraise_type", args[0].from); |
| 1594 | if ((strcmp(args[0].from, "imasig")) == 0) |
| 1595 | entry->flags |= IMA_DIGSIG_REQUIRED; |
Tyler Hicks | 30031b0 | 2020-07-09 01:19:09 -0500 | [diff] [blame] | 1596 | else if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) && |
Thiago Jung Bauermann | 9044d62 | 2019-06-27 23:19:28 -0300 | [diff] [blame] | 1597 | strcmp(args[0].from, "imasig|modsig") == 0) |
| 1598 | entry->flags |= IMA_DIGSIG_REQUIRED | |
| 1599 | IMA_MODSIG_ALLOWED; |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 1600 | else |
| 1601 | result = -EINVAL; |
| 1602 | break; |
Nayna Jain | 273df86 | 2019-10-30 23:31:32 -0400 | [diff] [blame] | 1603 | case Opt_appraise_flag: |
| 1604 | ima_log_string(ab, "appraise_flag", args[0].from); |
Tyler Hicks | 5f3e926 | 2020-07-09 01:19:06 -0500 | [diff] [blame] | 1605 | if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) && |
| 1606 | strstr(args[0].from, "blacklist")) |
Nayna Jain | 273df86 | 2019-10-30 23:31:32 -0400 | [diff] [blame] | 1607 | entry->flags |= IMA_CHECK_BLACKLIST; |
Tyler Hicks | 5f3e926 | 2020-07-09 01:19:06 -0500 | [diff] [blame] | 1608 | else |
| 1609 | result = -EINVAL; |
Nayna Jain | 273df86 | 2019-10-30 23:31:32 -0400 | [diff] [blame] | 1610 | break; |
THOBY Simon | 583a80a | 2021-08-16 08:11:00 +0000 | [diff] [blame] | 1611 | case Opt_appraise_algos: |
| 1612 | ima_log_string(ab, "appraise_algos", args[0].from); |
| 1613 | |
| 1614 | if (entry->allowed_algos) { |
| 1615 | result = -EINVAL; |
| 1616 | break; |
| 1617 | } |
| 1618 | |
| 1619 | entry->allowed_algos = |
| 1620 | ima_parse_appraise_algos(args[0].from); |
| 1621 | /* invalid or empty list of algorithms */ |
| 1622 | if (!entry->allowed_algos) { |
| 1623 | result = -EINVAL; |
| 1624 | break; |
| 1625 | } |
| 1626 | |
| 1627 | entry->flags |= IMA_VALIDATE_ALGOS; |
| 1628 | |
| 1629 | break; |
Mimi Zohar | f9b2a73 | 2014-05-12 09:28:11 -0400 | [diff] [blame] | 1630 | case Opt_permit_directio: |
| 1631 | entry->flags |= IMA_PERMIT_DIRECTIO; |
| 1632 | break; |
Eric Richter | 0260643 | 2016-06-01 13:14:01 -0500 | [diff] [blame] | 1633 | case Opt_pcr: |
Eric Richter | 0260643 | 2016-06-01 13:14:01 -0500 | [diff] [blame] | 1634 | ima_log_string(ab, "pcr", args[0].from); |
| 1635 | |
| 1636 | result = kstrtoint(args[0].from, 10, &entry->pcr); |
| 1637 | if (result || INVALID_PCR(entry->pcr)) |
| 1638 | result = -EINVAL; |
| 1639 | else |
| 1640 | entry->flags |= IMA_PCR; |
| 1641 | |
| 1642 | break; |
Matthew Garrett | 19453ce0 | 2019-06-19 15:46:11 -0700 | [diff] [blame] | 1643 | case Opt_template: |
| 1644 | ima_log_string(ab, "template", args[0].from); |
| 1645 | if (entry->action != MEASURE) { |
| 1646 | result = -EINVAL; |
| 1647 | break; |
| 1648 | } |
| 1649 | template_desc = lookup_template_desc(args[0].from); |
| 1650 | if (!template_desc || entry->template) { |
| 1651 | result = -EINVAL; |
| 1652 | break; |
| 1653 | } |
| 1654 | |
| 1655 | /* |
| 1656 | * template_desc_init_fields() does nothing if |
| 1657 | * the template is already initialised, so |
| 1658 | * it's safe to do this unconditionally |
| 1659 | */ |
| 1660 | template_desc_init_fields(template_desc->fmt, |
| 1661 | &(template_desc->fields), |
| 1662 | &(template_desc->num_fields)); |
| 1663 | entry->template = template_desc; |
| 1664 | break; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1665 | case Opt_err: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1666 | ima_log_string(ab, "UNKNOWN", p); |
Eric Paris | e9d393b | 2010-04-20 10:21:13 -0400 | [diff] [blame] | 1667 | result = -EINVAL; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1668 | break; |
| 1669 | } |
| 1670 | } |
Tyler Hicks | 71218343 | 2020-07-09 01:19:03 -0500 | [diff] [blame] | 1671 | if (!result && !ima_validate_rule(entry)) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1672 | result = -EINVAL; |
Mimi Zohar | 6f0911a | 2018-04-12 00:15:22 -0400 | [diff] [blame] | 1673 | else if (entry->action == APPRAISE) |
| 1674 | temp_ima_appraise |= ima_appraise_flag(entry->func); |
| 1675 | |
Thiago Jung Bauermann | 3878d50 | 2019-06-27 23:19:32 -0300 | [diff] [blame] | 1676 | if (!result && entry->flags & IMA_MODSIG_ALLOWED) { |
| 1677 | template_desc = entry->template ? entry->template : |
| 1678 | ima_template_desc_current(); |
| 1679 | check_template_modsig(template_desc); |
| 1680 | } |
| 1681 | |
Eric Paris | b0d5de4 | 2012-02-14 17:11:07 -0500 | [diff] [blame] | 1682 | audit_log_format(ab, "res=%d", !result); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1683 | audit_log_end(ab); |
| 1684 | return result; |
| 1685 | } |
| 1686 | |
| 1687 | /** |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 1688 | * ima_parse_add_rule - add a rule to ima_policy_rules |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1689 | * @rule - ima measurement policy rule |
| 1690 | * |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 1691 | * Avoid locking by allowing just one writer at a time in ima_write_policy() |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 1692 | * Returns the length of the rule parsed, an error code on failure |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1693 | */ |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 1694 | ssize_t ima_parse_add_rule(char *rule) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1695 | { |
Mimi Zohar | 52a1328 | 2013-12-11 14:44:04 -0500 | [diff] [blame] | 1696 | static const char op[] = "update_policy"; |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 1697 | char *p; |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 1698 | struct ima_rule_entry *entry; |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 1699 | ssize_t result, len; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1700 | int audit_info = 0; |
| 1701 | |
Dmitry Kasatkin | 272a6e9 | 2014-10-03 14:40:19 +0300 | [diff] [blame] | 1702 | p = strsep(&rule, "\n"); |
| 1703 | len = strlen(p) + 1; |
Dmitry Kasatkin | 7178784 | 2014-10-03 14:40:20 +0300 | [diff] [blame] | 1704 | p += strspn(p, " \t"); |
Dmitry Kasatkin | 272a6e9 | 2014-10-03 14:40:19 +0300 | [diff] [blame] | 1705 | |
Dmitry Kasatkin | 7178784 | 2014-10-03 14:40:20 +0300 | [diff] [blame] | 1706 | if (*p == '#' || *p == '\0') |
Dmitry Kasatkin | 272a6e9 | 2014-10-03 14:40:19 +0300 | [diff] [blame] | 1707 | return len; |
| 1708 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1709 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 1710 | if (!entry) { |
| 1711 | integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, |
| 1712 | NULL, op, "-ENOMEM", -ENOMEM, audit_info); |
| 1713 | return -ENOMEM; |
| 1714 | } |
| 1715 | |
| 1716 | INIT_LIST_HEAD(&entry->list); |
| 1717 | |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 1718 | result = ima_parse_rule(p, entry); |
Eric Paris | 7233e3e | 2010-04-20 10:21:24 -0400 | [diff] [blame] | 1719 | if (result) { |
Tyler Hicks | 2bdd737 | 2020-07-09 01:19:02 -0500 | [diff] [blame] | 1720 | ima_free_rule(entry); |
Mimi Zohar | 523979a | 2009-02-11 11:12:28 -0500 | [diff] [blame] | 1721 | integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, |
Richard Guy Briggs | 7e9001f | 2014-06-16 15:52:07 -0400 | [diff] [blame] | 1722 | NULL, op, "invalid-policy", result, |
Mimi Zohar | 523979a | 2009-02-11 11:12:28 -0500 | [diff] [blame] | 1723 | audit_info); |
Eric Paris | 7233e3e | 2010-04-20 10:21:24 -0400 | [diff] [blame] | 1724 | return result; |
Mimi Zohar | 523979a | 2009-02-11 11:12:28 -0500 | [diff] [blame] | 1725 | } |
Eric Paris | 7233e3e | 2010-04-20 10:21:24 -0400 | [diff] [blame] | 1726 | |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 1727 | list_add_tail(&entry->list, &ima_temp_rules); |
Eric Paris | 7233e3e | 2010-04-20 10:21:24 -0400 | [diff] [blame] | 1728 | |
| 1729 | return len; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1730 | } |
| 1731 | |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 1732 | /** |
| 1733 | * ima_delete_rules() called to cleanup invalid in-flight policy. |
| 1734 | * We don't need locking as we operate on the temp list, which is |
| 1735 | * different from the active one. There is also only one user of |
| 1736 | * ima_delete_rules() at a time. |
| 1737 | */ |
James Morris | 64c61d8 | 2009-02-05 09:28:26 +1100 | [diff] [blame] | 1738 | void ima_delete_rules(void) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1739 | { |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 1740 | struct ima_rule_entry *entry, *tmp; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1741 | |
Mimi Zohar | 6ad6afa | 2015-12-07 14:35:47 -0500 | [diff] [blame] | 1742 | temp_ima_appraise = 0; |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 1743 | list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) { |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1744 | list_del(&entry->list); |
Tyler Hicks | 465aee7 | 2020-07-09 01:19:01 -0500 | [diff] [blame] | 1745 | ima_free_rule(entry); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1746 | } |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1747 | } |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1748 | |
Lakshmi Ramasubramanian | 34e980b | 2020-06-18 14:10:12 -0700 | [diff] [blame] | 1749 | #define __ima_hook_stringify(func, str) (#func), |
Thiago Jung Bauermann | 39b0709 | 2019-06-27 23:19:30 -0300 | [diff] [blame] | 1750 | |
| 1751 | const char *const func_tokens[] = { |
| 1752 | __ima_hooks(__ima_hook_stringify) |
| 1753 | }; |
| 1754 | |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1755 | #ifdef CONFIG_IMA_READ_POLICY |
| 1756 | enum { |
| 1757 | mask_exec = 0, mask_write, mask_read, mask_append |
| 1758 | }; |
| 1759 | |
Thiago Jung Bauermann | bb543e3 | 2017-06-07 22:49:10 -0300 | [diff] [blame] | 1760 | static const char *const mask_tokens[] = { |
Roberto Sassu | 8cdc23a | 2019-05-29 15:30:35 +0200 | [diff] [blame] | 1761 | "^MAY_EXEC", |
| 1762 | "^MAY_WRITE", |
| 1763 | "^MAY_READ", |
| 1764 | "^MAY_APPEND" |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1765 | }; |
| 1766 | |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1767 | void *ima_policy_start(struct seq_file *m, loff_t *pos) |
| 1768 | { |
| 1769 | loff_t l = *pos; |
| 1770 | struct ima_rule_entry *entry; |
| 1771 | |
| 1772 | rcu_read_lock(); |
| 1773 | list_for_each_entry_rcu(entry, ima_rules, list) { |
| 1774 | if (!l--) { |
| 1775 | rcu_read_unlock(); |
| 1776 | return entry; |
| 1777 | } |
| 1778 | } |
| 1779 | rcu_read_unlock(); |
| 1780 | return NULL; |
| 1781 | } |
| 1782 | |
| 1783 | void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos) |
| 1784 | { |
| 1785 | struct ima_rule_entry *entry = v; |
| 1786 | |
| 1787 | rcu_read_lock(); |
| 1788 | entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list); |
| 1789 | rcu_read_unlock(); |
| 1790 | (*pos)++; |
| 1791 | |
| 1792 | return (&entry->list == ima_rules) ? NULL : entry; |
| 1793 | } |
| 1794 | |
| 1795 | void ima_policy_stop(struct seq_file *m, void *v) |
| 1796 | { |
| 1797 | } |
| 1798 | |
Mimi Zohar | 1a9430d | 2018-12-17 19:14:49 -0500 | [diff] [blame] | 1799 | #define pt(token) policy_tokens[token].pattern |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1800 | #define mt(token) mask_tokens[token] |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1801 | |
Mimi Zohar | b5269ab | 2016-01-28 13:10:36 -0500 | [diff] [blame] | 1802 | /* |
| 1803 | * policy_func_show - display the ima_hooks policy rule |
| 1804 | */ |
| 1805 | static void policy_func_show(struct seq_file *m, enum ima_hooks func) |
| 1806 | { |
Thiago Jung Bauermann | 2663218 | 2017-06-07 22:49:11 -0300 | [diff] [blame] | 1807 | if (func > 0 && func < MAX_CHECK) |
| 1808 | seq_printf(m, "func=%s ", func_tokens[func]); |
| 1809 | else |
| 1810 | seq_printf(m, "func=%d ", func); |
Mimi Zohar | b5269ab | 2016-01-28 13:10:36 -0500 | [diff] [blame] | 1811 | } |
| 1812 | |
Tyler Hicks | 176377d | 2020-08-11 14:26:20 -0500 | [diff] [blame] | 1813 | static void ima_show_rule_opt_list(struct seq_file *m, |
| 1814 | const struct ima_rule_opt_list *opt_list) |
| 1815 | { |
| 1816 | size_t i; |
| 1817 | |
| 1818 | for (i = 0; i < opt_list->count; i++) |
| 1819 | seq_printf(m, "%s%s", i ? "|" : "", opt_list->items[i]); |
| 1820 | } |
| 1821 | |
THOBY Simon | 583a80a | 2021-08-16 08:11:00 +0000 | [diff] [blame] | 1822 | static void ima_policy_show_appraise_algos(struct seq_file *m, |
| 1823 | unsigned int allowed_hashes) |
| 1824 | { |
| 1825 | int idx, list_size = 0; |
| 1826 | |
| 1827 | for (idx = 0; idx < HASH_ALGO__LAST; idx++) { |
| 1828 | if (!(allowed_hashes & (1U << idx))) |
| 1829 | continue; |
| 1830 | |
| 1831 | /* only add commas if the list contains multiple entries */ |
| 1832 | if (list_size++) |
| 1833 | seq_puts(m, ","); |
| 1834 | |
| 1835 | seq_puts(m, hash_algo_name[idx]); |
| 1836 | } |
| 1837 | } |
| 1838 | |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1839 | int ima_policy_show(struct seq_file *m, void *v) |
| 1840 | { |
| 1841 | struct ima_rule_entry *entry = v; |
Andy Shevchenko | b8b5727 | 2016-05-20 17:00:57 -0700 | [diff] [blame] | 1842 | int i; |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1843 | char tbuf[64] = {0,}; |
Roberto Sassu | 8cdc23a | 2019-05-29 15:30:35 +0200 | [diff] [blame] | 1844 | int offset = 0; |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1845 | |
| 1846 | rcu_read_lock(); |
| 1847 | |
| 1848 | if (entry->action & MEASURE) |
| 1849 | seq_puts(m, pt(Opt_measure)); |
| 1850 | if (entry->action & DONT_MEASURE) |
| 1851 | seq_puts(m, pt(Opt_dont_measure)); |
| 1852 | if (entry->action & APPRAISE) |
| 1853 | seq_puts(m, pt(Opt_appraise)); |
| 1854 | if (entry->action & DONT_APPRAISE) |
| 1855 | seq_puts(m, pt(Opt_dont_appraise)); |
| 1856 | if (entry->action & AUDIT) |
| 1857 | seq_puts(m, pt(Opt_audit)); |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 1858 | if (entry->action & HASH) |
| 1859 | seq_puts(m, pt(Opt_hash)); |
| 1860 | if (entry->action & DONT_HASH) |
| 1861 | seq_puts(m, pt(Opt_dont_hash)); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1862 | |
| 1863 | seq_puts(m, " "); |
| 1864 | |
Mimi Zohar | b5269ab | 2016-01-28 13:10:36 -0500 | [diff] [blame] | 1865 | if (entry->flags & IMA_FUNC) |
| 1866 | policy_func_show(m, entry->func); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1867 | |
Roberto Sassu | 8cdc23a | 2019-05-29 15:30:35 +0200 | [diff] [blame] | 1868 | if ((entry->flags & IMA_MASK) || (entry->flags & IMA_INMASK)) { |
| 1869 | if (entry->flags & IMA_MASK) |
| 1870 | offset = 1; |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1871 | if (entry->mask & MAY_EXEC) |
Roberto Sassu | 8cdc23a | 2019-05-29 15:30:35 +0200 | [diff] [blame] | 1872 | seq_printf(m, pt(Opt_mask), mt(mask_exec) + offset); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1873 | if (entry->mask & MAY_WRITE) |
Roberto Sassu | 8cdc23a | 2019-05-29 15:30:35 +0200 | [diff] [blame] | 1874 | seq_printf(m, pt(Opt_mask), mt(mask_write) + offset); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1875 | if (entry->mask & MAY_READ) |
Roberto Sassu | 8cdc23a | 2019-05-29 15:30:35 +0200 | [diff] [blame] | 1876 | seq_printf(m, pt(Opt_mask), mt(mask_read) + offset); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1877 | if (entry->mask & MAY_APPEND) |
Roberto Sassu | 8cdc23a | 2019-05-29 15:30:35 +0200 | [diff] [blame] | 1878 | seq_printf(m, pt(Opt_mask), mt(mask_append) + offset); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1879 | seq_puts(m, " "); |
| 1880 | } |
| 1881 | |
| 1882 | if (entry->flags & IMA_FSMAGIC) { |
| 1883 | snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic); |
| 1884 | seq_printf(m, pt(Opt_fsmagic), tbuf); |
| 1885 | seq_puts(m, " "); |
| 1886 | } |
| 1887 | |
Mimi Zohar | f1b08bb | 2018-01-15 11:20:36 -0500 | [diff] [blame] | 1888 | if (entry->flags & IMA_FSNAME) { |
| 1889 | snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname); |
| 1890 | seq_printf(m, pt(Opt_fsname), tbuf); |
| 1891 | seq_puts(m, " "); |
| 1892 | } |
| 1893 | |
Lakshmi Ramasubramanian | 2b60c0e | 2019-12-11 08:47:07 -0800 | [diff] [blame] | 1894 | if (entry->flags & IMA_KEYRINGS) { |
Tyler Hicks | 176377d | 2020-08-11 14:26:20 -0500 | [diff] [blame] | 1895 | seq_puts(m, "keyrings="); |
| 1896 | ima_show_rule_opt_list(m, entry->keyrings); |
Lakshmi Ramasubramanian | 2b60c0e | 2019-12-11 08:47:07 -0800 | [diff] [blame] | 1897 | seq_puts(m, " "); |
| 1898 | } |
| 1899 | |
Tushar Sugandhi | 47d76a4 | 2021-01-07 20:07:05 -0800 | [diff] [blame] | 1900 | if (entry->flags & IMA_LABEL) { |
| 1901 | seq_puts(m, "label="); |
| 1902 | ima_show_rule_opt_list(m, entry->label); |
| 1903 | seq_puts(m, " "); |
| 1904 | } |
| 1905 | |
Eric Richter | 0260643 | 2016-06-01 13:14:01 -0500 | [diff] [blame] | 1906 | if (entry->flags & IMA_PCR) { |
| 1907 | snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr); |
| 1908 | seq_printf(m, pt(Opt_pcr), tbuf); |
| 1909 | seq_puts(m, " "); |
| 1910 | } |
| 1911 | |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1912 | if (entry->flags & IMA_FSUUID) { |
Christoph Hellwig | 787d8c5 | 2017-06-01 07:00:26 +0200 | [diff] [blame] | 1913 | seq_printf(m, "fsuuid=%pU", &entry->fsuuid); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1914 | seq_puts(m, " "); |
| 1915 | } |
| 1916 | |
| 1917 | if (entry->flags & IMA_UID) { |
| 1918 | snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid)); |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1919 | if (entry->uid_op == &uid_gt) |
| 1920 | seq_printf(m, pt(Opt_uid_gt), tbuf); |
| 1921 | else if (entry->uid_op == &uid_lt) |
| 1922 | seq_printf(m, pt(Opt_uid_lt), tbuf); |
| 1923 | else |
| 1924 | seq_printf(m, pt(Opt_uid_eq), tbuf); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1925 | seq_puts(m, " "); |
| 1926 | } |
| 1927 | |
| 1928 | if (entry->flags & IMA_EUID) { |
| 1929 | snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid)); |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1930 | if (entry->uid_op == &uid_gt) |
| 1931 | seq_printf(m, pt(Opt_euid_gt), tbuf); |
| 1932 | else if (entry->uid_op == &uid_lt) |
| 1933 | seq_printf(m, pt(Opt_euid_lt), tbuf); |
| 1934 | else |
| 1935 | seq_printf(m, pt(Opt_euid_eq), tbuf); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1936 | seq_puts(m, " "); |
| 1937 | } |
| 1938 | |
| 1939 | if (entry->flags & IMA_FOWNER) { |
| 1940 | snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner)); |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1941 | if (entry->fowner_op == &uid_gt) |
| 1942 | seq_printf(m, pt(Opt_fowner_gt), tbuf); |
| 1943 | else if (entry->fowner_op == &uid_lt) |
| 1944 | seq_printf(m, pt(Opt_fowner_lt), tbuf); |
| 1945 | else |
| 1946 | seq_printf(m, pt(Opt_fowner_eq), tbuf); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1947 | seq_puts(m, " "); |
| 1948 | } |
| 1949 | |
THOBY Simon | 583a80a | 2021-08-16 08:11:00 +0000 | [diff] [blame] | 1950 | if (entry->flags & IMA_VALIDATE_ALGOS) { |
| 1951 | seq_puts(m, "appraise_algos="); |
| 1952 | ima_policy_show_appraise_algos(m, entry->allowed_algos); |
| 1953 | seq_puts(m, " "); |
| 1954 | } |
| 1955 | |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1956 | for (i = 0; i < MAX_LSM_RULES; i++) { |
| 1957 | if (entry->lsm[i].rule) { |
| 1958 | switch (i) { |
| 1959 | case LSM_OBJ_USER: |
| 1960 | seq_printf(m, pt(Opt_obj_user), |
Tyler Hicks | aa0c022 | 2020-07-09 01:19:08 -0500 | [diff] [blame] | 1961 | entry->lsm[i].args_p); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1962 | break; |
| 1963 | case LSM_OBJ_ROLE: |
| 1964 | seq_printf(m, pt(Opt_obj_role), |
Tyler Hicks | aa0c022 | 2020-07-09 01:19:08 -0500 | [diff] [blame] | 1965 | entry->lsm[i].args_p); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1966 | break; |
| 1967 | case LSM_OBJ_TYPE: |
| 1968 | seq_printf(m, pt(Opt_obj_type), |
Tyler Hicks | aa0c022 | 2020-07-09 01:19:08 -0500 | [diff] [blame] | 1969 | entry->lsm[i].args_p); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1970 | break; |
| 1971 | case LSM_SUBJ_USER: |
| 1972 | seq_printf(m, pt(Opt_subj_user), |
Tyler Hicks | aa0c022 | 2020-07-09 01:19:08 -0500 | [diff] [blame] | 1973 | entry->lsm[i].args_p); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1974 | break; |
| 1975 | case LSM_SUBJ_ROLE: |
| 1976 | seq_printf(m, pt(Opt_subj_role), |
Tyler Hicks | aa0c022 | 2020-07-09 01:19:08 -0500 | [diff] [blame] | 1977 | entry->lsm[i].args_p); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1978 | break; |
| 1979 | case LSM_SUBJ_TYPE: |
| 1980 | seq_printf(m, pt(Opt_subj_type), |
Tyler Hicks | aa0c022 | 2020-07-09 01:19:08 -0500 | [diff] [blame] | 1981 | entry->lsm[i].args_p); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1982 | break; |
| 1983 | } |
Clay Chang | 5350ceb | 2020-01-05 09:18:13 +0800 | [diff] [blame] | 1984 | seq_puts(m, " "); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1985 | } |
| 1986 | } |
Matthew Garrett | 19453ce0 | 2019-06-19 15:46:11 -0700 | [diff] [blame] | 1987 | if (entry->template) |
| 1988 | seq_printf(m, "template=%s ", entry->template->name); |
Thiago Jung Bauermann | 9044d62 | 2019-06-27 23:19:28 -0300 | [diff] [blame] | 1989 | if (entry->flags & IMA_DIGSIG_REQUIRED) { |
| 1990 | if (entry->flags & IMA_MODSIG_ALLOWED) |
| 1991 | seq_puts(m, "appraise_type=imasig|modsig "); |
| 1992 | else |
| 1993 | seq_puts(m, "appraise_type=imasig "); |
| 1994 | } |
Nayna Jain | 273df86 | 2019-10-30 23:31:32 -0400 | [diff] [blame] | 1995 | if (entry->flags & IMA_CHECK_BLACKLIST) |
| 1996 | seq_puts(m, "appraise_flag=check_blacklist "); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1997 | if (entry->flags & IMA_PERMIT_DIRECTIO) |
| 1998 | seq_puts(m, "permit_directio "); |
| 1999 | rcu_read_unlock(); |
| 2000 | seq_puts(m, "\n"); |
| 2001 | return 0; |
| 2002 | } |
| 2003 | #endif /* CONFIG_IMA_READ_POLICY */ |
Matthew Garrett | 29d3c1c | 2019-08-19 17:18:01 -0700 | [diff] [blame] | 2004 | |
| 2005 | #if defined(CONFIG_IMA_APPRAISE) && defined(CONFIG_INTEGRITY_TRUSTED_KEYRING) |
| 2006 | /* |
| 2007 | * ima_appraise_signature: whether IMA will appraise a given function using |
| 2008 | * an IMA digital signature. This is restricted to cases where the kernel |
| 2009 | * has a set of built-in trusted keys in order to avoid an attacker simply |
| 2010 | * loading additional keys. |
| 2011 | */ |
| 2012 | bool ima_appraise_signature(enum kernel_read_file_id id) |
| 2013 | { |
| 2014 | struct ima_rule_entry *entry; |
| 2015 | bool found = false; |
| 2016 | enum ima_hooks func; |
| 2017 | |
| 2018 | if (id >= READING_MAX_ID) |
| 2019 | return false; |
| 2020 | |
| 2021 | func = read_idmap[id] ?: FILE_CHECK; |
| 2022 | |
| 2023 | rcu_read_lock(); |
| 2024 | list_for_each_entry_rcu(entry, ima_rules, list) { |
| 2025 | if (entry->action != APPRAISE) |
| 2026 | continue; |
| 2027 | |
| 2028 | /* |
| 2029 | * A generic entry will match, but otherwise require that it |
| 2030 | * match the func we're looking for |
| 2031 | */ |
| 2032 | if (entry->func && entry->func != func) |
| 2033 | continue; |
| 2034 | |
| 2035 | /* |
| 2036 | * We require this to be a digital signature, not a raw IMA |
| 2037 | * hash. |
| 2038 | */ |
| 2039 | if (entry->flags & IMA_DIGSIG_REQUIRED) |
| 2040 | found = true; |
| 2041 | |
| 2042 | /* |
| 2043 | * We've found a rule that matches, so break now even if it |
| 2044 | * didn't require a digital signature - a later rule that does |
| 2045 | * won't override it, so would be a false positive. |
| 2046 | */ |
| 2047 | break; |
| 2048 | } |
| 2049 | |
| 2050 | rcu_read_unlock(); |
| 2051 | return found; |
| 2052 | } |
| 2053 | #endif /* CONFIG_IMA_APPRAISE && CONFIG_INTEGRITY_TRUSTED_KEYRING */ |