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