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 | |
| 10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 11 | |
Paul Gortmaker | 876979c | 2018-12-09 15:36:29 -0500 | [diff] [blame] | 12 | #include <linux/init.h> |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 13 | #include <linux/list.h> |
Mimi Zohar | cf22221 | 2016-01-14 17:57:47 -0500 | [diff] [blame] | 14 | #include <linux/fs.h> |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 15 | #include <linux/security.h> |
| 16 | #include <linux/magic.h> |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 17 | #include <linux/parser.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 19 | #include <linux/rculist.h> |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 20 | #include <linux/genhd.h> |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 21 | #include <linux/seq_file.h> |
Nayna Jain | 6191706 | 2018-10-09 23:00:36 +0530 | [diff] [blame] | 22 | #include <linux/ima.h> |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 23 | |
| 24 | #include "ima.h" |
| 25 | |
| 26 | /* flags definitions */ |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 27 | #define IMA_FUNC 0x0001 |
| 28 | #define IMA_MASK 0x0002 |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 29 | #define IMA_FSMAGIC 0x0004 |
| 30 | #define IMA_UID 0x0008 |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 31 | #define IMA_FOWNER 0x0010 |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 32 | #define IMA_FSUUID 0x0020 |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 33 | #define IMA_INMASK 0x0040 |
Mimi Zohar | 139069e | 2014-11-05 07:48:36 -0500 | [diff] [blame] | 34 | #define IMA_EUID 0x0080 |
Eric Richter | 0260643 | 2016-06-01 13:14:01 -0500 | [diff] [blame] | 35 | #define IMA_PCR 0x0100 |
Mimi Zohar | f1b08bb | 2018-01-15 11:20:36 -0500 | [diff] [blame] | 36 | #define IMA_FSNAME 0x0200 |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 37 | |
Dmitry Kasatkin | 45e2472 | 2012-09-12 20:51:32 +0300 | [diff] [blame] | 38 | #define UNKNOWN 0 |
| 39 | #define MEASURE 0x0001 /* same as IMA_MEASURE */ |
| 40 | #define DONT_MEASURE 0x0002 |
| 41 | #define APPRAISE 0x0004 /* same as IMA_APPRAISE */ |
| 42 | #define DONT_APPRAISE 0x0008 |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 43 | #define AUDIT 0x0040 |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 44 | #define HASH 0x0100 |
| 45 | #define DONT_HASH 0x0200 |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 46 | |
Eric Richter | 0260643 | 2016-06-01 13:14:01 -0500 | [diff] [blame] | 47 | #define INVALID_PCR(a) (((a) < 0) || \ |
Pankaj Bharadiya | c593642 | 2019-12-09 10:31:43 -0800 | [diff] [blame^] | 48 | (a) >= (sizeof_field(struct integrity_iint_cache, measured_pcrs) * 8)) |
Eric Richter | 0260643 | 2016-06-01 13:14:01 -0500 | [diff] [blame] | 49 | |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 50 | int ima_policy_flag; |
Mimi Zohar | 6ad6afa | 2015-12-07 14:35:47 -0500 | [diff] [blame] | 51 | static int temp_ima_appraise; |
Mimi Zohar | ef96837 | 2018-07-13 14:06:01 -0400 | [diff] [blame] | 52 | static int build_ima_appraise __ro_after_init; |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 53 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 54 | #define MAX_LSM_RULES 6 |
| 55 | enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE, |
| 56 | LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE |
| 57 | }; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 58 | |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 59 | enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB }; |
| 60 | |
Nayna Jain | c52657d | 2018-10-09 23:00:35 +0530 | [diff] [blame] | 61 | enum policy_rule_list { IMA_DEFAULT_POLICY = 1, IMA_CUSTOM_POLICY }; |
| 62 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 63 | struct ima_rule_entry { |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 64 | struct list_head list; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 65 | int action; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 66 | unsigned int flags; |
| 67 | enum ima_hooks func; |
| 68 | int mask; |
| 69 | unsigned long fsmagic; |
Christoph Hellwig | 787d8c5 | 2017-06-01 07:00:26 +0200 | [diff] [blame] | 70 | uuid_t fsuuid; |
Eric W. Biederman | 8b94eea | 2012-05-25 18:24:12 -0600 | [diff] [blame] | 71 | kuid_t uid; |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 72 | kuid_t fowner; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 73 | bool (*uid_op)(kuid_t, kuid_t); /* Handlers for operators */ |
| 74 | bool (*fowner_op)(kuid_t, kuid_t); /* uid_eq(), uid_gt(), uid_lt() */ |
Eric Richter | 0260643 | 2016-06-01 13:14:01 -0500 | [diff] [blame] | 75 | int pcr; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 76 | struct { |
| 77 | void *rule; /* LSM file metadata specific */ |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 78 | void *args_p; /* audit value */ |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 79 | int type; /* audit type */ |
| 80 | } lsm[MAX_LSM_RULES]; |
Mimi Zohar | f1b08bb | 2018-01-15 11:20:36 -0500 | [diff] [blame] | 81 | char *fsname; |
Matthew Garrett | 19453ce0 | 2019-06-19 15:46:11 -0700 | [diff] [blame] | 82 | struct ima_template_desc *template; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 83 | }; |
| 84 | |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 85 | /* |
| 86 | * Without LSM specific knowledge, the default policy can only be |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 87 | * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 88 | */ |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * The minimum rule set to allow for full TCB coverage. Measures all files |
| 92 | * opened or mmap for exec and everything read by root. Dangerous because |
| 93 | * normal users can easily run the machine out of memory simply building |
| 94 | * and running executables. |
| 95 | */ |
James Morris | bad4417 | 2017-02-13 16:34:35 +1100 | [diff] [blame] | 96 | static struct ima_rule_entry dont_measure_rules[] __ro_after_init = { |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 97 | {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 98 | {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 99 | {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 100 | {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 101 | {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 102 | {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 103 | {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 104 | {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC}, |
Martin Townsend | 1c070b1 | 2018-03-22 13:46:01 +0000 | [diff] [blame] | 105 | {.action = DONT_MEASURE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC}, |
Roberto Sassu | 6438de9 | 2015-04-11 17:13:06 +0200 | [diff] [blame] | 106 | {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC, |
| 107 | .flags = IMA_FSMAGIC}, |
Laura Abbott | 82e3bb4 | 2017-05-09 11:25:27 -0700 | [diff] [blame] | 108 | {.action = DONT_MEASURE, .fsmagic = CGROUP2_SUPER_MAGIC, |
| 109 | .flags = IMA_FSMAGIC}, |
Mimi Zohar | 060190f | 2018-11-14 17:24:13 -0500 | [diff] [blame] | 110 | {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 111 | {.action = DONT_MEASURE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC} |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 112 | }; |
| 113 | |
James Morris | bad4417 | 2017-02-13 16:34:35 +1100 | [diff] [blame] | 114 | static struct ima_rule_entry original_measurement_rules[] __ro_after_init = { |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 115 | {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC, |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 116 | .flags = IMA_FUNC | IMA_MASK}, |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 117 | {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC, |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 118 | .flags = IMA_FUNC | IMA_MASK}, |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 119 | {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ, |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 120 | .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq, |
| 121 | .flags = IMA_FUNC | IMA_MASK | IMA_UID}, |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 122 | {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC}, |
| 123 | {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC}, |
| 124 | }; |
| 125 | |
James Morris | bad4417 | 2017-02-13 16:34:35 +1100 | [diff] [blame] | 126 | static struct ima_rule_entry default_measurement_rules[] __ro_after_init = { |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 127 | {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC, |
| 128 | .flags = IMA_FUNC | IMA_MASK}, |
| 129 | {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC, |
| 130 | .flags = IMA_FUNC | IMA_MASK}, |
| 131 | {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ, |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 132 | .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq, |
| 133 | .flags = IMA_FUNC | IMA_INMASK | IMA_EUID}, |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 134 | {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ, |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 135 | .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq, |
| 136 | .flags = IMA_FUNC | IMA_INMASK | IMA_UID}, |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 137 | {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC}, |
Mimi Zohar | 5a9196d | 2014-07-22 10:39:48 -0400 | [diff] [blame] | 138 | {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC}, |
Mimi Zohar | 19f8a84 | 2016-01-15 10:17:12 -0500 | [diff] [blame] | 139 | {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC}, |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 140 | }; |
| 141 | |
James Morris | bad4417 | 2017-02-13 16:34:35 +1100 | [diff] [blame] | 142 | static struct ima_rule_entry default_appraise_rules[] __ro_after_init = { |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 143 | {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 144 | {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 145 | {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 146 | {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 147 | {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 148 | {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 149 | {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 150 | {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 151 | {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC}, |
Martin Townsend | 1c070b1 | 2018-03-22 13:46:01 +0000 | [diff] [blame] | 152 | {.action = DONT_APPRAISE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC}, |
Mimi Zohar | cd025f7 | 2015-04-21 16:54:24 -0400 | [diff] [blame] | 153 | {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC}, |
Mimi Zohar | 060190f | 2018-11-14 17:24:13 -0500 | [diff] [blame] | 154 | {.action = DONT_APPRAISE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC}, |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 155 | {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
Laura Abbott | 82e3bb4 | 2017-05-09 11:25:27 -0700 | [diff] [blame] | 156 | {.action = DONT_APPRAISE, .fsmagic = CGROUP2_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
Mimi Zohar | 95ee08f | 2015-12-07 15:08:01 -0500 | [diff] [blame] | 157 | #ifdef CONFIG_IMA_WRITE_POLICY |
| 158 | {.action = APPRAISE, .func = POLICY_CHECK, |
| 159 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 160 | #endif |
Dmitry Kasatkin | c57782c | 2014-11-05 17:01:16 +0200 | [diff] [blame] | 161 | #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 162 | {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq, |
| 163 | .flags = IMA_FOWNER}, |
Dmitry Kasatkin | c57782c | 2014-11-05 17:01:16 +0200 | [diff] [blame] | 164 | #else |
| 165 | /* force signature */ |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 166 | {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq, |
Dmitry Kasatkin | c57782c | 2014-11-05 17:01:16 +0200 | [diff] [blame] | 167 | .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED}, |
| 168 | #endif |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 169 | }; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 170 | |
Mimi Zohar | ef96837 | 2018-07-13 14:06:01 -0400 | [diff] [blame] | 171 | static struct ima_rule_entry build_appraise_rules[] __ro_after_init = { |
| 172 | #ifdef CONFIG_IMA_APPRAISE_REQUIRE_MODULE_SIGS |
| 173 | {.action = APPRAISE, .func = MODULE_CHECK, |
| 174 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 175 | #endif |
| 176 | #ifdef CONFIG_IMA_APPRAISE_REQUIRE_FIRMWARE_SIGS |
| 177 | {.action = APPRAISE, .func = FIRMWARE_CHECK, |
| 178 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 179 | #endif |
| 180 | #ifdef CONFIG_IMA_APPRAISE_REQUIRE_KEXEC_SIGS |
| 181 | {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK, |
| 182 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 183 | #endif |
| 184 | #ifdef CONFIG_IMA_APPRAISE_REQUIRE_POLICY_SIGS |
| 185 | {.action = APPRAISE, .func = POLICY_CHECK, |
| 186 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 187 | #endif |
| 188 | }; |
| 189 | |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 190 | static struct ima_rule_entry secure_boot_rules[] __ro_after_init = { |
| 191 | {.action = APPRAISE, .func = MODULE_CHECK, |
| 192 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 193 | {.action = APPRAISE, .func = FIRMWARE_CHECK, |
| 194 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 195 | {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK, |
| 196 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 197 | {.action = APPRAISE, .func = POLICY_CHECK, |
| 198 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 199 | }; |
| 200 | |
Nayna Jain | 6191706 | 2018-10-09 23:00:36 +0530 | [diff] [blame] | 201 | /* An array of architecture specific rules */ |
YueHaibing | 68f2529 | 2019-06-11 21:40:32 +0800 | [diff] [blame] | 202 | static struct ima_rule_entry *arch_policy_entry __ro_after_init; |
Nayna Jain | 6191706 | 2018-10-09 23:00:36 +0530 | [diff] [blame] | 203 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 204 | static LIST_HEAD(ima_default_rules); |
| 205 | static LIST_HEAD(ima_policy_rules); |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 206 | static LIST_HEAD(ima_temp_rules); |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 207 | static struct list_head *ima_rules; |
| 208 | |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 209 | static int ima_policy __initdata; |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 210 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 211 | static int __init default_measure_policy_setup(char *str) |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 212 | { |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 213 | if (ima_policy) |
| 214 | return 1; |
| 215 | |
| 216 | ima_policy = ORIGINAL_TCB; |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 217 | return 1; |
| 218 | } |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 219 | __setup("ima_tcb", default_measure_policy_setup); |
| 220 | |
Mimi Zohar | 33ce954 | 2017-04-24 12:04:09 -0400 | [diff] [blame] | 221 | static bool ima_use_appraise_tcb __initdata; |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 222 | static bool ima_use_secure_boot __initdata; |
Mimi Zohar | 9e67028 | 2018-02-21 11:36:32 -0500 | [diff] [blame] | 223 | static bool ima_fail_unverifiable_sigs __ro_after_init; |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 224 | static int __init policy_setup(char *str) |
| 225 | { |
Mimi Zohar | 33ce954 | 2017-04-24 12:04:09 -0400 | [diff] [blame] | 226 | char *p; |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 227 | |
Mimi Zohar | 33ce954 | 2017-04-24 12:04:09 -0400 | [diff] [blame] | 228 | while ((p = strsep(&str, " |\n")) != NULL) { |
| 229 | if (*p == ' ') |
| 230 | continue; |
| 231 | if ((strcmp(p, "tcb") == 0) && !ima_policy) |
| 232 | ima_policy = DEFAULT_TCB; |
| 233 | else if (strcmp(p, "appraise_tcb") == 0) |
Thomas Meyer | 39adb92 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 234 | ima_use_appraise_tcb = true; |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 235 | else if (strcmp(p, "secure_boot") == 0) |
Thomas Meyer | 39adb92 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 236 | ima_use_secure_boot = true; |
Mimi Zohar | 9e67028 | 2018-02-21 11:36:32 -0500 | [diff] [blame] | 237 | else if (strcmp(p, "fail_securely") == 0) |
| 238 | ima_fail_unverifiable_sigs = true; |
Mimi Zohar | 33ce954 | 2017-04-24 12:04:09 -0400 | [diff] [blame] | 239 | } |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 240 | |
| 241 | return 1; |
| 242 | } |
| 243 | __setup("ima_policy=", policy_setup); |
| 244 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 245 | static int __init default_appraise_policy_setup(char *str) |
| 246 | { |
Thomas Meyer | 39adb92 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 247 | ima_use_appraise_tcb = true; |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 248 | return 1; |
| 249 | } |
| 250 | __setup("ima_appraise_tcb", default_appraise_policy_setup); |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 251 | |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 252 | static void ima_lsm_free_rule(struct ima_rule_entry *entry) |
| 253 | { |
| 254 | int i; |
| 255 | |
| 256 | for (i = 0; i < MAX_LSM_RULES; i++) { |
| 257 | kfree(entry->lsm[i].rule); |
| 258 | kfree(entry->lsm[i].args_p); |
| 259 | } |
| 260 | kfree(entry); |
| 261 | } |
| 262 | |
| 263 | static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry) |
| 264 | { |
| 265 | struct ima_rule_entry *nentry; |
| 266 | int i, result; |
| 267 | |
| 268 | nentry = kmalloc(sizeof(*nentry), GFP_KERNEL); |
| 269 | if (!nentry) |
| 270 | return NULL; |
| 271 | |
| 272 | /* |
| 273 | * Immutable elements are copied over as pointers and data; only |
| 274 | * lsm rules can change |
| 275 | */ |
| 276 | memcpy(nentry, entry, sizeof(*nentry)); |
Pankaj Bharadiya | c593642 | 2019-12-09 10:31:43 -0800 | [diff] [blame^] | 277 | memset(nentry->lsm, 0, sizeof_field(struct ima_rule_entry, lsm)); |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 278 | |
| 279 | for (i = 0; i < MAX_LSM_RULES; i++) { |
| 280 | if (!entry->lsm[i].rule) |
| 281 | continue; |
| 282 | |
| 283 | nentry->lsm[i].type = entry->lsm[i].type; |
| 284 | nentry->lsm[i].args_p = kstrdup(entry->lsm[i].args_p, |
| 285 | GFP_KERNEL); |
| 286 | if (!nentry->lsm[i].args_p) |
| 287 | goto out_err; |
| 288 | |
| 289 | result = security_filter_rule_init(nentry->lsm[i].type, |
| 290 | Audit_equal, |
| 291 | nentry->lsm[i].args_p, |
| 292 | &nentry->lsm[i].rule); |
| 293 | if (result == -EINVAL) |
| 294 | pr_warn("ima: rule for LSM \'%d\' is undefined\n", |
| 295 | entry->lsm[i].type); |
| 296 | } |
| 297 | return nentry; |
| 298 | |
| 299 | out_err: |
| 300 | ima_lsm_free_rule(nentry); |
| 301 | return NULL; |
| 302 | } |
| 303 | |
| 304 | static int ima_lsm_update_rule(struct ima_rule_entry *entry) |
| 305 | { |
| 306 | struct ima_rule_entry *nentry; |
| 307 | |
| 308 | nentry = ima_lsm_copy_rule(entry); |
| 309 | if (!nentry) |
| 310 | return -ENOMEM; |
| 311 | |
| 312 | list_replace_rcu(&entry->list, &nentry->list); |
| 313 | synchronize_rcu(); |
| 314 | ima_lsm_free_rule(entry); |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 319 | /* |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 320 | * The LSM policy can be reloaded, leaving the IMA LSM based rules referring |
| 321 | * 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] | 322 | * the reloaded LSM policy. |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 323 | */ |
| 324 | static void ima_lsm_update_rules(void) |
| 325 | { |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 326 | struct ima_rule_entry *entry, *e; |
| 327 | int i, result, needs_update; |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 328 | |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 329 | list_for_each_entry_safe(entry, e, &ima_policy_rules, list) { |
| 330 | needs_update = 0; |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 331 | for (i = 0; i < MAX_LSM_RULES; i++) { |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 332 | if (entry->lsm[i].rule) { |
| 333 | needs_update = 1; |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 | if (!needs_update) |
| 338 | continue; |
| 339 | |
| 340 | result = ima_lsm_update_rule(entry); |
| 341 | if (result) { |
| 342 | pr_err("ima: lsm rule update error %d\n", |
| 343 | result); |
| 344 | return; |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 345 | } |
| 346 | } |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 347 | } |
| 348 | |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 349 | int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event, |
| 350 | void *lsm_data) |
| 351 | { |
| 352 | if (event != LSM_POLICY_CHANGE) |
| 353 | return NOTIFY_DONE; |
| 354 | |
| 355 | ima_lsm_update_rules(); |
| 356 | return NOTIFY_OK; |
| 357 | } |
| 358 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 359 | /** |
| 360 | * ima_match_rules - determine whether an inode matches the measure rule. |
| 361 | * @rule: a pointer to a rule |
| 362 | * @inode: a pointer to an inode |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 363 | * @cred: a pointer to a credentials structure for user validation |
| 364 | * @secid: the secid of the task to be validated |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 365 | * @func: LIM hook identifier |
| 366 | * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC) |
| 367 | * |
| 368 | * Returns true on rule match, false on failure. |
| 369 | */ |
Mimi Zohar | 4ad87a3 | 2016-01-14 20:59:14 -0500 | [diff] [blame] | 370 | static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode, |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 371 | const struct cred *cred, u32 secid, |
Mimi Zohar | 4ad87a3 | 2016-01-14 20:59:14 -0500 | [diff] [blame] | 372 | enum ima_hooks func, int mask) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 373 | { |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 374 | int i; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 375 | |
Prakhar Srivastava | b093512 | 2019-06-23 23:23:29 -0700 | [diff] [blame] | 376 | if (func == KEXEC_CMDLINE) { |
| 377 | if ((rule->flags & IMA_FUNC) && (rule->func == func)) |
| 378 | return true; |
| 379 | return false; |
| 380 | } |
Dmitry Kasatkin | 09b1148 | 2013-11-13 23:42:39 +0200 | [diff] [blame] | 381 | if ((rule->flags & IMA_FUNC) && |
| 382 | (rule->func != func && func != POST_SETATTR)) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 383 | return false; |
Dmitry Kasatkin | 09b1148 | 2013-11-13 23:42:39 +0200 | [diff] [blame] | 384 | if ((rule->flags & IMA_MASK) && |
| 385 | (rule->mask != mask && func != POST_SETATTR)) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 386 | return false; |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 387 | if ((rule->flags & IMA_INMASK) && |
| 388 | (!(rule->mask & mask) && func != POST_SETATTR)) |
| 389 | return false; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 390 | if ((rule->flags & IMA_FSMAGIC) |
| 391 | && rule->fsmagic != inode->i_sb->s_magic) |
| 392 | return false; |
Mimi Zohar | f1b08bb | 2018-01-15 11:20:36 -0500 | [diff] [blame] | 393 | if ((rule->flags & IMA_FSNAME) |
| 394 | && strcmp(rule->fsname, inode->i_sb->s_type->name)) |
| 395 | return false; |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 396 | if ((rule->flags & IMA_FSUUID) && |
Christoph Hellwig | 8578709 | 2017-05-10 15:06:33 +0200 | [diff] [blame] | 397 | !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid)) |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 398 | return false; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 399 | if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid)) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 400 | return false; |
Mimi Zohar | 139069e | 2014-11-05 07:48:36 -0500 | [diff] [blame] | 401 | if (rule->flags & IMA_EUID) { |
| 402 | if (has_capability_noaudit(current, CAP_SETUID)) { |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 403 | if (!rule->uid_op(cred->euid, rule->uid) |
| 404 | && !rule->uid_op(cred->suid, rule->uid) |
| 405 | && !rule->uid_op(cred->uid, rule->uid)) |
Mimi Zohar | 139069e | 2014-11-05 07:48:36 -0500 | [diff] [blame] | 406 | return false; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 407 | } else if (!rule->uid_op(cred->euid, rule->uid)) |
Mimi Zohar | 139069e | 2014-11-05 07:48:36 -0500 | [diff] [blame] | 408 | return false; |
| 409 | } |
| 410 | |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 411 | if ((rule->flags & IMA_FOWNER) && |
| 412 | !rule->fowner_op(inode->i_uid, rule->fowner)) |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 413 | return false; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 414 | for (i = 0; i < MAX_LSM_RULES; i++) { |
Mimi Zohar | 53fc0e2 | 2009-05-05 13:12:48 -0400 | [diff] [blame] | 415 | int rc = 0; |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 416 | u32 osid; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 417 | |
| 418 | if (!rule->lsm[i].rule) |
| 419 | continue; |
Janne Karhunen | b169424 | 2019-06-14 15:20:15 +0300 | [diff] [blame] | 420 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 421 | switch (i) { |
| 422 | case LSM_OBJ_USER: |
| 423 | case LSM_OBJ_ROLE: |
| 424 | case LSM_OBJ_TYPE: |
| 425 | security_inode_getsecid(inode, &osid); |
| 426 | rc = security_filter_rule_match(osid, |
| 427 | rule->lsm[i].type, |
Mimi Zohar | 53fc0e2 | 2009-05-05 13:12:48 -0400 | [diff] [blame] | 428 | Audit_equal, |
Richard Guy Briggs | 90462a5 | 2019-01-31 11:52:11 -0500 | [diff] [blame] | 429 | rule->lsm[i].rule); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 430 | break; |
| 431 | case LSM_SUBJ_USER: |
| 432 | case LSM_SUBJ_ROLE: |
| 433 | case LSM_SUBJ_TYPE: |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 434 | rc = security_filter_rule_match(secid, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 435 | rule->lsm[i].type, |
Mimi Zohar | 53fc0e2 | 2009-05-05 13:12:48 -0400 | [diff] [blame] | 436 | Audit_equal, |
Richard Guy Briggs | 90462a5 | 2019-01-31 11:52:11 -0500 | [diff] [blame] | 437 | rule->lsm[i].rule); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 438 | default: |
| 439 | break; |
| 440 | } |
| 441 | if (!rc) |
| 442 | return false; |
| 443 | } |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 444 | return true; |
| 445 | } |
| 446 | |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 447 | /* |
| 448 | * 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] | 449 | * we need to differentiate between calling hooks, for hook specific rules. |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 450 | */ |
Mimi Zohar | 4ad87a3 | 2016-01-14 20:59:14 -0500 | [diff] [blame] | 451 | 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] | 452 | { |
Mimi Zohar | 5a73fcf | 2012-12-05 15:14:38 -0500 | [diff] [blame] | 453 | if (!(rule->flags & IMA_FUNC)) |
| 454 | return IMA_FILE_APPRAISE; |
| 455 | |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 456 | switch (func) { |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 457 | case MMAP_CHECK: |
| 458 | return IMA_MMAP_APPRAISE; |
| 459 | case BPRM_CHECK: |
| 460 | return IMA_BPRM_APPRAISE; |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 461 | case CREDS_CHECK: |
| 462 | return IMA_CREDS_APPRAISE; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 463 | case FILE_CHECK: |
Mimi Zohar | c6af8ef | 2015-11-19 12:39:22 -0500 | [diff] [blame] | 464 | case POST_SETATTR: |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 465 | return IMA_FILE_APPRAISE; |
Mimi Zohar | c6af8ef | 2015-11-19 12:39:22 -0500 | [diff] [blame] | 466 | case MODULE_CHECK ... MAX_CHECK - 1: |
| 467 | default: |
| 468 | return IMA_READ_APPRAISE; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 472 | /** |
| 473 | * ima_match_policy - decision based on LSM and other conditions |
| 474 | * @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] | 475 | * @cred: pointer to a credentials structure for which the policy decision is |
| 476 | * being made |
| 477 | * @secid: LSM secid of the task to be validated |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 478 | * @func: IMA hook identifier |
| 479 | * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC) |
Eric Richter | 725de7f | 2016-06-01 13:14:02 -0500 | [diff] [blame] | 480 | * @pcr: set the pcr to extend |
Matthew Garrett | 19453ce0 | 2019-06-19 15:46:11 -0700 | [diff] [blame] | 481 | * @template_desc: the template that should be used for this rule |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 482 | * |
| 483 | * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type) |
| 484 | * conditions. |
| 485 | * |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 486 | * Since the IMA policy may be updated multiple times we need to lock the |
| 487 | * list when walking it. Reads are many orders of magnitude more numerous |
| 488 | * than writes so ima_match_policy() is classical RCU candidate. |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 489 | */ |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 490 | int ima_match_policy(struct inode *inode, const struct cred *cred, u32 secid, |
Matthew Garrett | 19453ce0 | 2019-06-19 15:46:11 -0700 | [diff] [blame] | 491 | enum ima_hooks func, int mask, int flags, int *pcr, |
| 492 | struct ima_template_desc **template_desc) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 493 | { |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 494 | struct ima_rule_entry *entry; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 495 | int action = 0, actmask = flags | (flags << 1); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 496 | |
Mimi Zohar | b36f281 | 2019-07-19 07:16:57 -0400 | [diff] [blame] | 497 | if (template_desc) |
| 498 | *template_desc = ima_template_desc_current(); |
| 499 | |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 500 | rcu_read_lock(); |
| 501 | list_for_each_entry_rcu(entry, ima_rules, list) { |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 502 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 503 | if (!(entry->action & actmask)) |
| 504 | continue; |
| 505 | |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 506 | if (!ima_match_rules(entry, inode, cred, secid, func, mask)) |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 507 | continue; |
| 508 | |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 509 | action |= entry->flags & IMA_ACTION_FLAGS; |
| 510 | |
Dmitry Kasatkin | 45e2472 | 2012-09-12 20:51:32 +0300 | [diff] [blame] | 511 | action |= entry->action & IMA_DO_MASK; |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 512 | if (entry->action & IMA_APPRAISE) { |
Mimi Zohar | 5a73fcf | 2012-12-05 15:14:38 -0500 | [diff] [blame] | 513 | action |= get_subaction(entry, func); |
Mimi Zohar | a9a4935 | 2018-03-10 23:07:34 -0500 | [diff] [blame] | 514 | action &= ~IMA_HASH; |
Mimi Zohar | 9e67028 | 2018-02-21 11:36:32 -0500 | [diff] [blame] | 515 | if (ima_fail_unverifiable_sigs) |
| 516 | action |= IMA_FAIL_UNVERIFIABLE_SIGS; |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 517 | } |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 518 | |
Mimi Zohar | b36f281 | 2019-07-19 07:16:57 -0400 | [diff] [blame] | 519 | |
Dmitry Kasatkin | 45e2472 | 2012-09-12 20:51:32 +0300 | [diff] [blame] | 520 | if (entry->action & IMA_DO_MASK) |
| 521 | actmask &= ~(entry->action | entry->action << 1); |
| 522 | else |
| 523 | actmask &= ~(entry->action | entry->action >> 1); |
| 524 | |
Eric Richter | 725de7f | 2016-06-01 13:14:02 -0500 | [diff] [blame] | 525 | if ((pcr) && (entry->flags & IMA_PCR)) |
| 526 | *pcr = entry->pcr; |
| 527 | |
Matthew Garrett | 19453ce0 | 2019-06-19 15:46:11 -0700 | [diff] [blame] | 528 | if (template_desc && entry->template) |
| 529 | *template_desc = entry->template; |
Matthew Garrett | 19453ce0 | 2019-06-19 15:46:11 -0700 | [diff] [blame] | 530 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 531 | if (!actmask) |
| 532 | break; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 533 | } |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 534 | rcu_read_unlock(); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 535 | |
| 536 | return action; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 537 | } |
| 538 | |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 539 | /* |
| 540 | * Initialize the ima_policy_flag variable based on the currently |
| 541 | * loaded policy. Based on this flag, the decision to short circuit |
| 542 | * out of a function or not call the function in the first place |
| 543 | * can be made earlier. |
| 544 | */ |
| 545 | void ima_update_policy_flag(void) |
| 546 | { |
| 547 | struct ima_rule_entry *entry; |
| 548 | |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 549 | list_for_each_entry(entry, ima_rules, list) { |
| 550 | if (entry->action & IMA_DO_MASK) |
| 551 | ima_policy_flag |= entry->action; |
| 552 | } |
| 553 | |
Mimi Zohar | ef96837 | 2018-07-13 14:06:01 -0400 | [diff] [blame] | 554 | ima_appraise |= (build_ima_appraise | temp_ima_appraise); |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 555 | if (!ima_appraise) |
| 556 | ima_policy_flag &= ~IMA_APPRAISE; |
| 557 | } |
| 558 | |
Mimi Zohar | 6f0911a | 2018-04-12 00:15:22 -0400 | [diff] [blame] | 559 | static int ima_appraise_flag(enum ima_hooks func) |
| 560 | { |
| 561 | if (func == MODULE_CHECK) |
| 562 | return IMA_APPRAISE_MODULES; |
| 563 | else if (func == FIRMWARE_CHECK) |
| 564 | return IMA_APPRAISE_FIRMWARE; |
| 565 | else if (func == POLICY_CHECK) |
| 566 | return IMA_APPRAISE_POLICY; |
Mimi Zohar | 16c267a | 2018-07-13 14:05:58 -0400 | [diff] [blame] | 567 | else if (func == KEXEC_KERNEL_CHECK) |
| 568 | return IMA_APPRAISE_KEXEC; |
Mimi Zohar | 6f0911a | 2018-04-12 00:15:22 -0400 | [diff] [blame] | 569 | return 0; |
| 570 | } |
| 571 | |
Nayna Jain | c52657d | 2018-10-09 23:00:35 +0530 | [diff] [blame] | 572 | static void add_rules(struct ima_rule_entry *entries, int count, |
| 573 | enum policy_rule_list policy_rule) |
| 574 | { |
| 575 | int i = 0; |
| 576 | |
| 577 | for (i = 0; i < count; i++) { |
| 578 | struct ima_rule_entry *entry; |
| 579 | |
| 580 | if (policy_rule & IMA_DEFAULT_POLICY) |
| 581 | list_add_tail(&entries[i].list, &ima_default_rules); |
| 582 | |
| 583 | if (policy_rule & IMA_CUSTOM_POLICY) { |
| 584 | entry = kmemdup(&entries[i], sizeof(*entry), |
| 585 | GFP_KERNEL); |
| 586 | if (!entry) |
| 587 | continue; |
| 588 | |
| 589 | list_add_tail(&entry->list, &ima_policy_rules); |
| 590 | } |
Petr Vorel | f400194 | 2019-05-15 08:18:07 +0200 | [diff] [blame] | 591 | if (entries[i].action == APPRAISE) { |
Nayna Jain | c52657d | 2018-10-09 23:00:35 +0530 | [diff] [blame] | 592 | temp_ima_appraise |= ima_appraise_flag(entries[i].func); |
Petr Vorel | f400194 | 2019-05-15 08:18:07 +0200 | [diff] [blame] | 593 | if (entries[i].func == POLICY_CHECK) |
| 594 | temp_ima_appraise |= IMA_APPRAISE_POLICY; |
| 595 | } |
Nayna Jain | c52657d | 2018-10-09 23:00:35 +0530 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
Nayna Jain | 6191706 | 2018-10-09 23:00:36 +0530 | [diff] [blame] | 599 | static int ima_parse_rule(char *rule, struct ima_rule_entry *entry); |
| 600 | |
| 601 | static int __init ima_init_arch_policy(void) |
| 602 | { |
| 603 | const char * const *arch_rules; |
| 604 | const char * const *rules; |
| 605 | int arch_entries = 0; |
| 606 | int i = 0; |
| 607 | |
| 608 | arch_rules = arch_get_ima_policy(); |
| 609 | if (!arch_rules) |
| 610 | return arch_entries; |
| 611 | |
| 612 | /* Get number of rules */ |
| 613 | for (rules = arch_rules; *rules != NULL; rules++) |
| 614 | arch_entries++; |
| 615 | |
| 616 | arch_policy_entry = kcalloc(arch_entries + 1, |
| 617 | sizeof(*arch_policy_entry), GFP_KERNEL); |
| 618 | if (!arch_policy_entry) |
| 619 | return 0; |
| 620 | |
| 621 | /* Convert each policy string rules to struct ima_rule_entry format */ |
| 622 | for (rules = arch_rules, i = 0; *rules != NULL; rules++) { |
| 623 | char rule[255]; |
| 624 | int result; |
| 625 | |
| 626 | result = strlcpy(rule, *rules, sizeof(rule)); |
| 627 | |
| 628 | INIT_LIST_HEAD(&arch_policy_entry[i].list); |
| 629 | result = ima_parse_rule(rule, &arch_policy_entry[i]); |
| 630 | if (result) { |
| 631 | pr_warn("Skipping unknown architecture policy rule: %s\n", |
| 632 | rule); |
| 633 | memset(&arch_policy_entry[i], 0, |
| 634 | sizeof(*arch_policy_entry)); |
| 635 | continue; |
| 636 | } |
| 637 | i++; |
| 638 | } |
| 639 | return i; |
| 640 | } |
| 641 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 642 | /** |
| 643 | * ima_init_policy - initialize the default measure rules. |
| 644 | * |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 645 | * ima_rules points to either the ima_default_rules or the |
| 646 | * the new ima_policy_rules. |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 647 | */ |
Eric Paris | 932995f | 2009-05-21 15:43:32 -0400 | [diff] [blame] | 648 | void __init ima_init_policy(void) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 649 | { |
Nayna Jain | 6191706 | 2018-10-09 23:00:36 +0530 | [diff] [blame] | 650 | int build_appraise_entries, arch_entries; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 651 | |
Nayna Jain | c52657d | 2018-10-09 23:00:35 +0530 | [diff] [blame] | 652 | /* if !ima_policy, we load NO default rules */ |
| 653 | if (ima_policy) |
| 654 | add_rules(dont_measure_rules, ARRAY_SIZE(dont_measure_rules), |
| 655 | IMA_DEFAULT_POLICY); |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 656 | |
| 657 | switch (ima_policy) { |
| 658 | case ORIGINAL_TCB: |
Nayna Jain | c52657d | 2018-10-09 23:00:35 +0530 | [diff] [blame] | 659 | add_rules(original_measurement_rules, |
| 660 | ARRAY_SIZE(original_measurement_rules), |
| 661 | IMA_DEFAULT_POLICY); |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 662 | break; |
| 663 | case DEFAULT_TCB: |
Nayna Jain | c52657d | 2018-10-09 23:00:35 +0530 | [diff] [blame] | 664 | add_rules(default_measurement_rules, |
| 665 | ARRAY_SIZE(default_measurement_rules), |
| 666 | IMA_DEFAULT_POLICY); |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 667 | default: |
| 668 | break; |
| 669 | } |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 670 | |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 671 | /* |
Nayna Jain | 6191706 | 2018-10-09 23:00:36 +0530 | [diff] [blame] | 672 | * Based on runtime secure boot flags, insert arch specific measurement |
| 673 | * and appraise rules requiring file signatures for both the initial |
| 674 | * and custom policies, prior to other appraise rules. |
| 675 | * (Highest priority) |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 676 | */ |
Nayna Jain | 6191706 | 2018-10-09 23:00:36 +0530 | [diff] [blame] | 677 | arch_entries = ima_init_arch_policy(); |
| 678 | if (!arch_entries) |
| 679 | pr_info("No architecture policies found\n"); |
| 680 | else |
| 681 | add_rules(arch_policy_entry, arch_entries, |
| 682 | IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY); |
| 683 | |
| 684 | /* |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 685 | * Insert the builtin "secure_boot" policy rules requiring file |
Nayna Jain | 6191706 | 2018-10-09 23:00:36 +0530 | [diff] [blame] | 686 | * signatures, prior to other appraise rules. |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 687 | */ |
Nayna Jain | c52657d | 2018-10-09 23:00:35 +0530 | [diff] [blame] | 688 | if (ima_use_secure_boot) |
| 689 | add_rules(secure_boot_rules, ARRAY_SIZE(secure_boot_rules), |
| 690 | IMA_DEFAULT_POLICY); |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 691 | |
Mimi Zohar | ef96837 | 2018-07-13 14:06:01 -0400 | [diff] [blame] | 692 | /* |
| 693 | * Insert the build time appraise rules requiring file signatures |
| 694 | * for both the initial and custom policies, prior to other appraise |
Nayna Jain | c52657d | 2018-10-09 23:00:35 +0530 | [diff] [blame] | 695 | * rules. As the secure boot rules includes all of the build time |
| 696 | * 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] | 697 | */ |
Nayna Jain | c52657d | 2018-10-09 23:00:35 +0530 | [diff] [blame] | 698 | build_appraise_entries = ARRAY_SIZE(build_appraise_rules); |
| 699 | if (build_appraise_entries) { |
| 700 | if (ima_use_secure_boot) |
| 701 | add_rules(build_appraise_rules, build_appraise_entries, |
| 702 | IMA_CUSTOM_POLICY); |
| 703 | else |
| 704 | add_rules(build_appraise_rules, build_appraise_entries, |
| 705 | IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY); |
Mimi Zohar | ef96837 | 2018-07-13 14:06:01 -0400 | [diff] [blame] | 706 | } |
| 707 | |
Nayna Jain | c52657d | 2018-10-09 23:00:35 +0530 | [diff] [blame] | 708 | if (ima_use_appraise_tcb) |
| 709 | add_rules(default_appraise_rules, |
| 710 | ARRAY_SIZE(default_appraise_rules), |
| 711 | IMA_DEFAULT_POLICY); |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 712 | |
| 713 | ima_rules = &ima_default_rules; |
Mimi Zohar | 95ee08f | 2015-12-07 15:08:01 -0500 | [diff] [blame] | 714 | ima_update_policy_flag(); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 715 | } |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 716 | |
Sasha Levin | 0112721 | 2015-12-22 08:51:23 -0500 | [diff] [blame] | 717 | /* 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] | 718 | int ima_check_policy(void) |
Sasha Levin | 0112721 | 2015-12-22 08:51:23 -0500 | [diff] [blame] | 719 | { |
| 720 | if (list_empty(&ima_temp_rules)) |
| 721 | return -EINVAL; |
| 722 | return 0; |
| 723 | } |
| 724 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 725 | /** |
| 726 | * ima_update_policy - update default_rules with new measure rules |
| 727 | * |
| 728 | * 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] | 729 | * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so |
| 730 | * they make a queue. The policy may be updated multiple times and this is the |
| 731 | * RCU updater. |
| 732 | * |
| 733 | * Policy rules are never deleted so ima_policy_flag gets zeroed only once when |
| 734 | * we switch from the default policy to user defined. |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 735 | */ |
| 736 | void ima_update_policy(void) |
| 737 | { |
Petko Manolov | 53b626f | 2018-05-22 17:06:55 +0300 | [diff] [blame] | 738 | struct list_head *policy = &ima_policy_rules; |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 739 | |
Petko Manolov | 53b626f | 2018-05-22 17:06:55 +0300 | [diff] [blame] | 740 | list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu); |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 741 | |
| 742 | if (ima_rules != policy) { |
| 743 | ima_policy_flag = 0; |
| 744 | ima_rules = policy; |
Nayna Jain | 6191706 | 2018-10-09 23:00:36 +0530 | [diff] [blame] | 745 | |
| 746 | /* |
| 747 | * IMA architecture specific policy rules are specified |
| 748 | * as strings and converted to an array of ima_entry_rules |
| 749 | * on boot. After loading a custom policy, free the |
| 750 | * architecture specific rules stored as an array. |
| 751 | */ |
| 752 | kfree(arch_policy_entry); |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 753 | } |
Dmitry Kasatkin | 0716abb | 2014-10-03 14:40:21 +0300 | [diff] [blame] | 754 | ima_update_policy_flag(); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 755 | } |
| 756 | |
Mimi Zohar | 1a9430d | 2018-12-17 19:14:49 -0500 | [diff] [blame] | 757 | /* Keep the enumeration in sync with the policy_tokens! */ |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 758 | enum { |
Mimi Zohar | 1a9430d | 2018-12-17 19:14:49 -0500 | [diff] [blame] | 759 | Opt_measure, Opt_dont_measure, |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 760 | Opt_appraise, Opt_dont_appraise, |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 761 | Opt_audit, Opt_hash, Opt_dont_hash, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 762 | Opt_obj_user, Opt_obj_role, Opt_obj_type, |
| 763 | Opt_subj_user, Opt_subj_role, Opt_subj_type, |
Mimi Zohar | f1b08bb | 2018-01-15 11:20:36 -0500 | [diff] [blame] | 764 | Opt_func, Opt_mask, Opt_fsmagic, Opt_fsname, |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 765 | Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq, |
| 766 | Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt, |
| 767 | Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt, |
Nayna Jain | 273df86 | 2019-10-30 23:31:32 -0400 | [diff] [blame] | 768 | Opt_appraise_type, Opt_appraise_flag, |
| 769 | Opt_permit_directio, Opt_pcr, Opt_template, Opt_err |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 770 | }; |
| 771 | |
Mimi Zohar | 1a9430d | 2018-12-17 19:14:49 -0500 | [diff] [blame] | 772 | static const match_table_t policy_tokens = { |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 773 | {Opt_measure, "measure"}, |
| 774 | {Opt_dont_measure, "dont_measure"}, |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 775 | {Opt_appraise, "appraise"}, |
| 776 | {Opt_dont_appraise, "dont_appraise"}, |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 777 | {Opt_audit, "audit"}, |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 778 | {Opt_hash, "hash"}, |
| 779 | {Opt_dont_hash, "dont_hash"}, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 780 | {Opt_obj_user, "obj_user=%s"}, |
| 781 | {Opt_obj_role, "obj_role=%s"}, |
| 782 | {Opt_obj_type, "obj_type=%s"}, |
| 783 | {Opt_subj_user, "subj_user=%s"}, |
| 784 | {Opt_subj_role, "subj_role=%s"}, |
| 785 | {Opt_subj_type, "subj_type=%s"}, |
| 786 | {Opt_func, "func=%s"}, |
| 787 | {Opt_mask, "mask=%s"}, |
| 788 | {Opt_fsmagic, "fsmagic=%s"}, |
Mimi Zohar | f1b08bb | 2018-01-15 11:20:36 -0500 | [diff] [blame] | 789 | {Opt_fsname, "fsname=%s"}, |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 790 | {Opt_fsuuid, "fsuuid=%s"}, |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 791 | {Opt_uid_eq, "uid=%s"}, |
| 792 | {Opt_euid_eq, "euid=%s"}, |
| 793 | {Opt_fowner_eq, "fowner=%s"}, |
| 794 | {Opt_uid_gt, "uid>%s"}, |
| 795 | {Opt_euid_gt, "euid>%s"}, |
| 796 | {Opt_fowner_gt, "fowner>%s"}, |
| 797 | {Opt_uid_lt, "uid<%s"}, |
| 798 | {Opt_euid_lt, "euid<%s"}, |
| 799 | {Opt_fowner_lt, "fowner<%s"}, |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 800 | {Opt_appraise_type, "appraise_type=%s"}, |
Nayna Jain | 273df86 | 2019-10-30 23:31:32 -0400 | [diff] [blame] | 801 | {Opt_appraise_flag, "appraise_flag=%s"}, |
Mimi Zohar | f9b2a73 | 2014-05-12 09:28:11 -0400 | [diff] [blame] | 802 | {Opt_permit_directio, "permit_directio"}, |
Eric Richter | 0260643 | 2016-06-01 13:14:01 -0500 | [diff] [blame] | 803 | {Opt_pcr, "pcr=%s"}, |
Matthew Garrett | 19453ce0 | 2019-06-19 15:46:11 -0700 | [diff] [blame] | 804 | {Opt_template, "template=%s"}, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 805 | {Opt_err, NULL} |
| 806 | }; |
| 807 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 808 | static int ima_lsm_rule_init(struct ima_rule_entry *entry, |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 809 | substring_t *args, int lsm_rule, int audit_type) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 810 | { |
| 811 | int result; |
| 812 | |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 813 | if (entry->lsm[lsm_rule].rule) |
| 814 | return -EINVAL; |
| 815 | |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 816 | entry->lsm[lsm_rule].args_p = match_strdup(args); |
| 817 | if (!entry->lsm[lsm_rule].args_p) |
| 818 | return -ENOMEM; |
| 819 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 820 | entry->lsm[lsm_rule].type = audit_type; |
| 821 | result = security_filter_rule_init(entry->lsm[lsm_rule].type, |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 822 | Audit_equal, |
| 823 | entry->lsm[lsm_rule].args_p, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 824 | &entry->lsm[lsm_rule].rule); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 825 | if (!entry->lsm[lsm_rule].rule) { |
| 826 | kfree(entry->lsm[lsm_rule].args_p); |
Mimi Zohar | 867c202 | 2011-01-03 14:59:10 -0800 | [diff] [blame] | 827 | return -EINVAL; |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 828 | } |
| 829 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 830 | return result; |
| 831 | } |
| 832 | |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 833 | static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value, |
| 834 | bool (*rule_operator)(kuid_t, kuid_t)) |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 835 | { |
Stefan Berger | 2afd020 | 2018-06-04 16:54:54 -0400 | [diff] [blame] | 836 | if (!ab) |
| 837 | return; |
| 838 | |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 839 | if (rule_operator == &uid_gt) |
| 840 | audit_log_format(ab, "%s>", key); |
| 841 | else if (rule_operator == &uid_lt) |
| 842 | audit_log_format(ab, "%s<", key); |
| 843 | else |
| 844 | audit_log_format(ab, "%s=", key); |
Stefan Berger | 3d2859d | 2018-06-04 16:54:53 -0400 | [diff] [blame] | 845 | audit_log_format(ab, "%s ", value); |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 846 | } |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 847 | static void ima_log_string(struct audit_buffer *ab, char *key, char *value) |
| 848 | { |
| 849 | ima_log_string_op(ab, key, value, NULL); |
| 850 | } |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 851 | |
Thiago Jung Bauermann | 3878d50 | 2019-06-27 23:19:32 -0300 | [diff] [blame] | 852 | /* |
| 853 | * Validating the appended signature included in the measurement list requires |
| 854 | * the file hash calculated without the appended signature (i.e., the 'd-modsig' |
| 855 | * field). Therefore, notify the user if they have the 'modsig' field but not |
| 856 | * the 'd-modsig' field in the template. |
| 857 | */ |
| 858 | static void check_template_modsig(const struct ima_template_desc *template) |
| 859 | { |
| 860 | #define MSG "template with 'modsig' field also needs 'd-modsig' field\n" |
| 861 | bool has_modsig, has_dmodsig; |
| 862 | static bool checked; |
| 863 | int i; |
| 864 | |
| 865 | /* We only need to notify the user once. */ |
| 866 | if (checked) |
| 867 | return; |
| 868 | |
| 869 | has_modsig = has_dmodsig = false; |
| 870 | for (i = 0; i < template->num_fields; i++) { |
| 871 | if (!strcmp(template->fields[i]->field_id, "modsig")) |
| 872 | has_modsig = true; |
| 873 | else if (!strcmp(template->fields[i]->field_id, "d-modsig")) |
| 874 | has_dmodsig = true; |
| 875 | } |
| 876 | |
| 877 | if (has_modsig && !has_dmodsig) |
| 878 | pr_notice(MSG); |
| 879 | |
| 880 | checked = true; |
| 881 | #undef MSG |
| 882 | } |
| 883 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 884 | static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 885 | { |
| 886 | struct audit_buffer *ab; |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 887 | char *from; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 888 | char *p; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 889 | bool uid_token; |
Matthew Garrett | 19453ce0 | 2019-06-19 15:46:11 -0700 | [diff] [blame] | 890 | struct ima_template_desc *template_desc; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 891 | int result = 0; |
| 892 | |
Stefan Berger | dba31ee | 2018-06-04 16:54:55 -0400 | [diff] [blame] | 893 | ab = integrity_audit_log_start(audit_context(), GFP_KERNEL, |
| 894 | AUDIT_INTEGRITY_POLICY_RULE); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 895 | |
Eric W. Biederman | 8b94eea | 2012-05-25 18:24:12 -0600 | [diff] [blame] | 896 | entry->uid = INVALID_UID; |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 897 | entry->fowner = INVALID_UID; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 898 | entry->uid_op = &uid_eq; |
| 899 | entry->fowner_op = &uid_eq; |
Eric Paris | b9035b1 | 2010-04-20 10:21:07 -0400 | [diff] [blame] | 900 | entry->action = UNKNOWN; |
Eric Paris | 28ef400 | 2010-04-20 10:21:18 -0400 | [diff] [blame] | 901 | while ((p = strsep(&rule, " \t")) != NULL) { |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 902 | substring_t args[MAX_OPT_ARGS]; |
| 903 | int token; |
| 904 | unsigned long lnum; |
| 905 | |
| 906 | if (result < 0) |
| 907 | break; |
Eric Paris | 28ef400 | 2010-04-20 10:21:18 -0400 | [diff] [blame] | 908 | if ((*p == '\0') || (*p == ' ') || (*p == '\t')) |
| 909 | continue; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 910 | token = match_token(p, policy_tokens, args); |
| 911 | switch (token) { |
| 912 | case Opt_measure: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 913 | ima_log_string(ab, "action", "measure"); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 914 | |
| 915 | if (entry->action != UNKNOWN) |
| 916 | result = -EINVAL; |
| 917 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 918 | entry->action = MEASURE; |
| 919 | break; |
| 920 | case Opt_dont_measure: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 921 | ima_log_string(ab, "action", "dont_measure"); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 922 | |
| 923 | if (entry->action != UNKNOWN) |
| 924 | result = -EINVAL; |
| 925 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 926 | entry->action = DONT_MEASURE; |
| 927 | break; |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 928 | case Opt_appraise: |
| 929 | ima_log_string(ab, "action", "appraise"); |
| 930 | |
| 931 | if (entry->action != UNKNOWN) |
| 932 | result = -EINVAL; |
| 933 | |
| 934 | entry->action = APPRAISE; |
| 935 | break; |
| 936 | case Opt_dont_appraise: |
| 937 | ima_log_string(ab, "action", "dont_appraise"); |
| 938 | |
| 939 | if (entry->action != UNKNOWN) |
| 940 | result = -EINVAL; |
| 941 | |
| 942 | entry->action = DONT_APPRAISE; |
| 943 | break; |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 944 | case Opt_audit: |
| 945 | ima_log_string(ab, "action", "audit"); |
| 946 | |
| 947 | if (entry->action != UNKNOWN) |
| 948 | result = -EINVAL; |
| 949 | |
| 950 | entry->action = AUDIT; |
| 951 | break; |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 952 | case Opt_hash: |
| 953 | ima_log_string(ab, "action", "hash"); |
| 954 | |
| 955 | if (entry->action != UNKNOWN) |
| 956 | result = -EINVAL; |
| 957 | |
| 958 | entry->action = HASH; |
| 959 | break; |
| 960 | case Opt_dont_hash: |
| 961 | ima_log_string(ab, "action", "dont_hash"); |
| 962 | |
| 963 | if (entry->action != UNKNOWN) |
| 964 | result = -EINVAL; |
| 965 | |
| 966 | entry->action = DONT_HASH; |
| 967 | break; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 968 | case Opt_func: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 969 | ima_log_string(ab, "func", args[0].from); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 970 | |
| 971 | if (entry->func) |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 972 | result = -EINVAL; |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 973 | |
Mimi Zohar | 1e93d00 | 2010-01-26 17:02:41 -0500 | [diff] [blame] | 974 | if (strcmp(args[0].from, "FILE_CHECK") == 0) |
| 975 | entry->func = FILE_CHECK; |
| 976 | /* PATH_CHECK is for backwards compat */ |
| 977 | else if (strcmp(args[0].from, "PATH_CHECK") == 0) |
| 978 | entry->func = FILE_CHECK; |
Mimi Zohar | fdf9072 | 2012-10-16 12:40:08 +1030 | [diff] [blame] | 979 | else if (strcmp(args[0].from, "MODULE_CHECK") == 0) |
| 980 | entry->func = MODULE_CHECK; |
Mimi Zohar | 5a9196d | 2014-07-22 10:39:48 -0400 | [diff] [blame] | 981 | else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0) |
| 982 | entry->func = FIRMWARE_CHECK; |
Mimi Zohar | 16cac49 | 2012-12-13 11:15:04 -0500 | [diff] [blame] | 983 | else if ((strcmp(args[0].from, "FILE_MMAP") == 0) |
| 984 | || (strcmp(args[0].from, "MMAP_CHECK") == 0)) |
| 985 | entry->func = MMAP_CHECK; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 986 | else if (strcmp(args[0].from, "BPRM_CHECK") == 0) |
| 987 | entry->func = BPRM_CHECK; |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 988 | else if (strcmp(args[0].from, "CREDS_CHECK") == 0) |
| 989 | entry->func = CREDS_CHECK; |
Mimi Zohar | d9ddf07 | 2016-01-14 20:59:14 -0500 | [diff] [blame] | 990 | else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") == |
| 991 | 0) |
| 992 | entry->func = KEXEC_KERNEL_CHECK; |
| 993 | else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK") |
| 994 | == 0) |
| 995 | entry->func = KEXEC_INITRAMFS_CHECK; |
Mimi Zohar | 19f8a84 | 2016-01-15 10:17:12 -0500 | [diff] [blame] | 996 | else if (strcmp(args[0].from, "POLICY_CHECK") == 0) |
| 997 | entry->func = POLICY_CHECK; |
Prakhar Srivastava | b093512 | 2019-06-23 23:23:29 -0700 | [diff] [blame] | 998 | else if (strcmp(args[0].from, "KEXEC_CMDLINE") == 0) |
| 999 | entry->func = KEXEC_CMDLINE; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1000 | else |
| 1001 | result = -EINVAL; |
| 1002 | if (!result) |
| 1003 | entry->flags |= IMA_FUNC; |
| 1004 | break; |
| 1005 | case Opt_mask: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1006 | ima_log_string(ab, "mask", args[0].from); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 1007 | |
| 1008 | if (entry->mask) |
| 1009 | result = -EINVAL; |
| 1010 | |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 1011 | from = args[0].from; |
| 1012 | if (*from == '^') |
| 1013 | from++; |
| 1014 | |
| 1015 | if ((strcmp(from, "MAY_EXEC")) == 0) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1016 | entry->mask = MAY_EXEC; |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 1017 | else if (strcmp(from, "MAY_WRITE") == 0) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1018 | entry->mask = MAY_WRITE; |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 1019 | else if (strcmp(from, "MAY_READ") == 0) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1020 | entry->mask = MAY_READ; |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 1021 | else if (strcmp(from, "MAY_APPEND") == 0) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1022 | entry->mask = MAY_APPEND; |
| 1023 | else |
| 1024 | result = -EINVAL; |
| 1025 | if (!result) |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 1026 | entry->flags |= (*args[0].from == '^') |
| 1027 | ? IMA_INMASK : IMA_MASK; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1028 | break; |
| 1029 | case Opt_fsmagic: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1030 | ima_log_string(ab, "fsmagic", args[0].from); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 1031 | |
| 1032 | if (entry->fsmagic) { |
| 1033 | result = -EINVAL; |
| 1034 | break; |
| 1035 | } |
| 1036 | |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 1037 | result = kstrtoul(args[0].from, 16, &entry->fsmagic); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1038 | if (!result) |
| 1039 | entry->flags |= IMA_FSMAGIC; |
| 1040 | break; |
Mimi Zohar | f1b08bb | 2018-01-15 11:20:36 -0500 | [diff] [blame] | 1041 | case Opt_fsname: |
| 1042 | ima_log_string(ab, "fsname", args[0].from); |
| 1043 | |
| 1044 | entry->fsname = kstrdup(args[0].from, GFP_KERNEL); |
| 1045 | if (!entry->fsname) { |
| 1046 | result = -ENOMEM; |
| 1047 | break; |
| 1048 | } |
| 1049 | result = 0; |
| 1050 | entry->flags |= IMA_FSNAME; |
| 1051 | break; |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 1052 | case Opt_fsuuid: |
| 1053 | ima_log_string(ab, "fsuuid", args[0].from); |
| 1054 | |
Mike Rapoport | 3644745 | 2018-01-17 20:27:11 +0200 | [diff] [blame] | 1055 | if (!uuid_is_null(&entry->fsuuid)) { |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 1056 | result = -EINVAL; |
| 1057 | break; |
| 1058 | } |
| 1059 | |
Christoph Hellwig | 787d8c5 | 2017-06-01 07:00:26 +0200 | [diff] [blame] | 1060 | result = uuid_parse(args[0].from, &entry->fsuuid); |
Mimi Zohar | 446d64e | 2013-02-24 23:42:37 -0500 | [diff] [blame] | 1061 | if (!result) |
| 1062 | entry->flags |= IMA_FSUUID; |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 1063 | break; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1064 | case Opt_uid_gt: |
| 1065 | case Opt_euid_gt: |
| 1066 | entry->uid_op = &uid_gt; |
Gustavo A. R. Silva | 09186e5 | 2019-02-08 14:54:53 -0600 | [diff] [blame] | 1067 | /* fall through */ |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1068 | case Opt_uid_lt: |
| 1069 | case Opt_euid_lt: |
| 1070 | if ((token == Opt_uid_lt) || (token == Opt_euid_lt)) |
| 1071 | entry->uid_op = &uid_lt; |
Gustavo A. R. Silva | 09186e5 | 2019-02-08 14:54:53 -0600 | [diff] [blame] | 1072 | /* fall through */ |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1073 | case Opt_uid_eq: |
| 1074 | case Opt_euid_eq: |
| 1075 | uid_token = (token == Opt_uid_eq) || |
| 1076 | (token == Opt_uid_gt) || |
| 1077 | (token == Opt_uid_lt); |
| 1078 | |
| 1079 | ima_log_string_op(ab, uid_token ? "uid" : "euid", |
| 1080 | args[0].from, entry->uid_op); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 1081 | |
Eric W. Biederman | 8b94eea | 2012-05-25 18:24:12 -0600 | [diff] [blame] | 1082 | if (uid_valid(entry->uid)) { |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 1083 | result = -EINVAL; |
| 1084 | break; |
| 1085 | } |
| 1086 | |
Jingoo Han | 29707b2 | 2014-02-05 15:13:14 +0900 | [diff] [blame] | 1087 | result = kstrtoul(args[0].from, 10, &lnum); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1088 | if (!result) { |
Mimi Zohar | 139069e | 2014-11-05 07:48:36 -0500 | [diff] [blame] | 1089 | entry->uid = make_kuid(current_user_ns(), |
| 1090 | (uid_t) lnum); |
| 1091 | if (!uid_valid(entry->uid) || |
| 1092 | (uid_t)lnum != lnum) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1093 | result = -EINVAL; |
| 1094 | else |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1095 | entry->flags |= uid_token |
Mimi Zohar | 139069e | 2014-11-05 07:48:36 -0500 | [diff] [blame] | 1096 | ? IMA_UID : IMA_EUID; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1097 | } |
| 1098 | break; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1099 | case Opt_fowner_gt: |
| 1100 | entry->fowner_op = &uid_gt; |
Gustavo A. R. Silva | 09186e5 | 2019-02-08 14:54:53 -0600 | [diff] [blame] | 1101 | /* fall through */ |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1102 | case Opt_fowner_lt: |
| 1103 | if (token == Opt_fowner_lt) |
| 1104 | entry->fowner_op = &uid_lt; |
Gustavo A. R. Silva | 09186e5 | 2019-02-08 14:54:53 -0600 | [diff] [blame] | 1105 | /* fall through */ |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1106 | case Opt_fowner_eq: |
| 1107 | ima_log_string_op(ab, "fowner", args[0].from, |
| 1108 | entry->fowner_op); |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 1109 | |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 1110 | if (uid_valid(entry->fowner)) { |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 1111 | result = -EINVAL; |
| 1112 | break; |
| 1113 | } |
| 1114 | |
Jingoo Han | 29707b2 | 2014-02-05 15:13:14 +0900 | [diff] [blame] | 1115 | result = kstrtoul(args[0].from, 10, &lnum); |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 1116 | if (!result) { |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 1117 | entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum); |
| 1118 | if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum)) |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 1119 | result = -EINVAL; |
| 1120 | else |
| 1121 | entry->flags |= IMA_FOWNER; |
| 1122 | } |
| 1123 | break; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1124 | case Opt_obj_user: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1125 | ima_log_string(ab, "obj_user", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 1126 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1127 | LSM_OBJ_USER, |
| 1128 | AUDIT_OBJ_USER); |
| 1129 | break; |
| 1130 | case Opt_obj_role: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1131 | ima_log_string(ab, "obj_role", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 1132 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1133 | LSM_OBJ_ROLE, |
| 1134 | AUDIT_OBJ_ROLE); |
| 1135 | break; |
| 1136 | case Opt_obj_type: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1137 | ima_log_string(ab, "obj_type", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 1138 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1139 | LSM_OBJ_TYPE, |
| 1140 | AUDIT_OBJ_TYPE); |
| 1141 | break; |
| 1142 | case Opt_subj_user: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1143 | ima_log_string(ab, "subj_user", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 1144 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1145 | LSM_SUBJ_USER, |
| 1146 | AUDIT_SUBJ_USER); |
| 1147 | break; |
| 1148 | case Opt_subj_role: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1149 | ima_log_string(ab, "subj_role", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 1150 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1151 | LSM_SUBJ_ROLE, |
| 1152 | AUDIT_SUBJ_ROLE); |
| 1153 | break; |
| 1154 | case Opt_subj_type: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1155 | ima_log_string(ab, "subj_type", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 1156 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1157 | LSM_SUBJ_TYPE, |
| 1158 | AUDIT_SUBJ_TYPE); |
| 1159 | break; |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 1160 | case Opt_appraise_type: |
| 1161 | if (entry->action != APPRAISE) { |
| 1162 | result = -EINVAL; |
| 1163 | break; |
| 1164 | } |
| 1165 | |
| 1166 | ima_log_string(ab, "appraise_type", args[0].from); |
| 1167 | if ((strcmp(args[0].from, "imasig")) == 0) |
| 1168 | entry->flags |= IMA_DIGSIG_REQUIRED; |
Thiago Jung Bauermann | 9044d62 | 2019-06-27 23:19:28 -0300 | [diff] [blame] | 1169 | else if (ima_hook_supports_modsig(entry->func) && |
| 1170 | strcmp(args[0].from, "imasig|modsig") == 0) |
| 1171 | entry->flags |= IMA_DIGSIG_REQUIRED | |
| 1172 | IMA_MODSIG_ALLOWED; |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 1173 | else |
| 1174 | result = -EINVAL; |
| 1175 | break; |
Nayna Jain | 273df86 | 2019-10-30 23:31:32 -0400 | [diff] [blame] | 1176 | case Opt_appraise_flag: |
| 1177 | ima_log_string(ab, "appraise_flag", args[0].from); |
| 1178 | if (strstr(args[0].from, "blacklist")) |
| 1179 | entry->flags |= IMA_CHECK_BLACKLIST; |
| 1180 | break; |
Mimi Zohar | f9b2a73 | 2014-05-12 09:28:11 -0400 | [diff] [blame] | 1181 | case Opt_permit_directio: |
| 1182 | entry->flags |= IMA_PERMIT_DIRECTIO; |
| 1183 | break; |
Eric Richter | 0260643 | 2016-06-01 13:14:01 -0500 | [diff] [blame] | 1184 | case Opt_pcr: |
| 1185 | if (entry->action != MEASURE) { |
| 1186 | result = -EINVAL; |
| 1187 | break; |
| 1188 | } |
| 1189 | ima_log_string(ab, "pcr", args[0].from); |
| 1190 | |
| 1191 | result = kstrtoint(args[0].from, 10, &entry->pcr); |
| 1192 | if (result || INVALID_PCR(entry->pcr)) |
| 1193 | result = -EINVAL; |
| 1194 | else |
| 1195 | entry->flags |= IMA_PCR; |
| 1196 | |
| 1197 | break; |
Matthew Garrett | 19453ce0 | 2019-06-19 15:46:11 -0700 | [diff] [blame] | 1198 | case Opt_template: |
| 1199 | ima_log_string(ab, "template", args[0].from); |
| 1200 | if (entry->action != MEASURE) { |
| 1201 | result = -EINVAL; |
| 1202 | break; |
| 1203 | } |
| 1204 | template_desc = lookup_template_desc(args[0].from); |
| 1205 | if (!template_desc || entry->template) { |
| 1206 | result = -EINVAL; |
| 1207 | break; |
| 1208 | } |
| 1209 | |
| 1210 | /* |
| 1211 | * template_desc_init_fields() does nothing if |
| 1212 | * the template is already initialised, so |
| 1213 | * it's safe to do this unconditionally |
| 1214 | */ |
| 1215 | template_desc_init_fields(template_desc->fmt, |
| 1216 | &(template_desc->fields), |
| 1217 | &(template_desc->num_fields)); |
| 1218 | entry->template = template_desc; |
| 1219 | break; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1220 | case Opt_err: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 1221 | ima_log_string(ab, "UNKNOWN", p); |
Eric Paris | e9d393b | 2010-04-20 10:21:13 -0400 | [diff] [blame] | 1222 | result = -EINVAL; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1223 | break; |
| 1224 | } |
| 1225 | } |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 1226 | if (!result && (entry->action == UNKNOWN)) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1227 | result = -EINVAL; |
Mimi Zohar | 6f0911a | 2018-04-12 00:15:22 -0400 | [diff] [blame] | 1228 | else if (entry->action == APPRAISE) |
| 1229 | temp_ima_appraise |= ima_appraise_flag(entry->func); |
| 1230 | |
Thiago Jung Bauermann | 3878d50 | 2019-06-27 23:19:32 -0300 | [diff] [blame] | 1231 | if (!result && entry->flags & IMA_MODSIG_ALLOWED) { |
| 1232 | template_desc = entry->template ? entry->template : |
| 1233 | ima_template_desc_current(); |
| 1234 | check_template_modsig(template_desc); |
| 1235 | } |
| 1236 | |
Eric Paris | b0d5de4 | 2012-02-14 17:11:07 -0500 | [diff] [blame] | 1237 | audit_log_format(ab, "res=%d", !result); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1238 | audit_log_end(ab); |
| 1239 | return result; |
| 1240 | } |
| 1241 | |
| 1242 | /** |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 1243 | * ima_parse_add_rule - add a rule to ima_policy_rules |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1244 | * @rule - ima measurement policy rule |
| 1245 | * |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 1246 | * 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] | 1247 | * Returns the length of the rule parsed, an error code on failure |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1248 | */ |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 1249 | ssize_t ima_parse_add_rule(char *rule) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1250 | { |
Mimi Zohar | 52a1328 | 2013-12-11 14:44:04 -0500 | [diff] [blame] | 1251 | static const char op[] = "update_policy"; |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 1252 | char *p; |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 1253 | struct ima_rule_entry *entry; |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 1254 | ssize_t result, len; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1255 | int audit_info = 0; |
| 1256 | |
Dmitry Kasatkin | 272a6e9 | 2014-10-03 14:40:19 +0300 | [diff] [blame] | 1257 | p = strsep(&rule, "\n"); |
| 1258 | len = strlen(p) + 1; |
Dmitry Kasatkin | 7178784 | 2014-10-03 14:40:20 +0300 | [diff] [blame] | 1259 | p += strspn(p, " \t"); |
Dmitry Kasatkin | 272a6e9 | 2014-10-03 14:40:19 +0300 | [diff] [blame] | 1260 | |
Dmitry Kasatkin | 7178784 | 2014-10-03 14:40:20 +0300 | [diff] [blame] | 1261 | if (*p == '#' || *p == '\0') |
Dmitry Kasatkin | 272a6e9 | 2014-10-03 14:40:19 +0300 | [diff] [blame] | 1262 | return len; |
| 1263 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1264 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 1265 | if (!entry) { |
| 1266 | integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, |
| 1267 | NULL, op, "-ENOMEM", -ENOMEM, audit_info); |
| 1268 | return -ENOMEM; |
| 1269 | } |
| 1270 | |
| 1271 | INIT_LIST_HEAD(&entry->list); |
| 1272 | |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 1273 | result = ima_parse_rule(p, entry); |
Eric Paris | 7233e3e | 2010-04-20 10:21:24 -0400 | [diff] [blame] | 1274 | if (result) { |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1275 | kfree(entry); |
Mimi Zohar | 523979a | 2009-02-11 11:12:28 -0500 | [diff] [blame] | 1276 | integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, |
Richard Guy Briggs | 7e9001f | 2014-06-16 15:52:07 -0400 | [diff] [blame] | 1277 | NULL, op, "invalid-policy", result, |
Mimi Zohar | 523979a | 2009-02-11 11:12:28 -0500 | [diff] [blame] | 1278 | audit_info); |
Eric Paris | 7233e3e | 2010-04-20 10:21:24 -0400 | [diff] [blame] | 1279 | return result; |
Mimi Zohar | 523979a | 2009-02-11 11:12:28 -0500 | [diff] [blame] | 1280 | } |
Eric Paris | 7233e3e | 2010-04-20 10:21:24 -0400 | [diff] [blame] | 1281 | |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 1282 | list_add_tail(&entry->list, &ima_temp_rules); |
Eric Paris | 7233e3e | 2010-04-20 10:21:24 -0400 | [diff] [blame] | 1283 | |
| 1284 | return len; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1285 | } |
| 1286 | |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 1287 | /** |
| 1288 | * ima_delete_rules() called to cleanup invalid in-flight policy. |
| 1289 | * We don't need locking as we operate on the temp list, which is |
| 1290 | * different from the active one. There is also only one user of |
| 1291 | * ima_delete_rules() at a time. |
| 1292 | */ |
James Morris | 64c61d8 | 2009-02-05 09:28:26 +1100 | [diff] [blame] | 1293 | void ima_delete_rules(void) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1294 | { |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 1295 | struct ima_rule_entry *entry, *tmp; |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 1296 | int i; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1297 | |
Mimi Zohar | 6ad6afa | 2015-12-07 14:35:47 -0500 | [diff] [blame] | 1298 | temp_ima_appraise = 0; |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 1299 | list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) { |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 1300 | for (i = 0; i < MAX_LSM_RULES; i++) |
| 1301 | kfree(entry->lsm[i].args_p); |
| 1302 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1303 | list_del(&entry->list); |
| 1304 | kfree(entry); |
| 1305 | } |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1306 | } |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1307 | |
Thiago Jung Bauermann | 39b0709 | 2019-06-27 23:19:30 -0300 | [diff] [blame] | 1308 | #define __ima_hook_stringify(str) (#str), |
| 1309 | |
| 1310 | const char *const func_tokens[] = { |
| 1311 | __ima_hooks(__ima_hook_stringify) |
| 1312 | }; |
| 1313 | |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1314 | #ifdef CONFIG_IMA_READ_POLICY |
| 1315 | enum { |
| 1316 | mask_exec = 0, mask_write, mask_read, mask_append |
| 1317 | }; |
| 1318 | |
Thiago Jung Bauermann | bb543e3 | 2017-06-07 22:49:10 -0300 | [diff] [blame] | 1319 | static const char *const mask_tokens[] = { |
Roberto Sassu | 8cdc23a | 2019-05-29 15:30:35 +0200 | [diff] [blame] | 1320 | "^MAY_EXEC", |
| 1321 | "^MAY_WRITE", |
| 1322 | "^MAY_READ", |
| 1323 | "^MAY_APPEND" |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1324 | }; |
| 1325 | |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1326 | void *ima_policy_start(struct seq_file *m, loff_t *pos) |
| 1327 | { |
| 1328 | loff_t l = *pos; |
| 1329 | struct ima_rule_entry *entry; |
| 1330 | |
| 1331 | rcu_read_lock(); |
| 1332 | list_for_each_entry_rcu(entry, ima_rules, list) { |
| 1333 | if (!l--) { |
| 1334 | rcu_read_unlock(); |
| 1335 | return entry; |
| 1336 | } |
| 1337 | } |
| 1338 | rcu_read_unlock(); |
| 1339 | return NULL; |
| 1340 | } |
| 1341 | |
| 1342 | void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos) |
| 1343 | { |
| 1344 | struct ima_rule_entry *entry = v; |
| 1345 | |
| 1346 | rcu_read_lock(); |
| 1347 | entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list); |
| 1348 | rcu_read_unlock(); |
| 1349 | (*pos)++; |
| 1350 | |
| 1351 | return (&entry->list == ima_rules) ? NULL : entry; |
| 1352 | } |
| 1353 | |
| 1354 | void ima_policy_stop(struct seq_file *m, void *v) |
| 1355 | { |
| 1356 | } |
| 1357 | |
Mimi Zohar | 1a9430d | 2018-12-17 19:14:49 -0500 | [diff] [blame] | 1358 | #define pt(token) policy_tokens[token].pattern |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1359 | #define mt(token) mask_tokens[token] |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1360 | |
Mimi Zohar | b5269ab | 2016-01-28 13:10:36 -0500 | [diff] [blame] | 1361 | /* |
| 1362 | * policy_func_show - display the ima_hooks policy rule |
| 1363 | */ |
| 1364 | static void policy_func_show(struct seq_file *m, enum ima_hooks func) |
| 1365 | { |
Thiago Jung Bauermann | 2663218 | 2017-06-07 22:49:11 -0300 | [diff] [blame] | 1366 | if (func > 0 && func < MAX_CHECK) |
| 1367 | seq_printf(m, "func=%s ", func_tokens[func]); |
| 1368 | else |
| 1369 | seq_printf(m, "func=%d ", func); |
Mimi Zohar | b5269ab | 2016-01-28 13:10:36 -0500 | [diff] [blame] | 1370 | } |
| 1371 | |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1372 | int ima_policy_show(struct seq_file *m, void *v) |
| 1373 | { |
| 1374 | struct ima_rule_entry *entry = v; |
Andy Shevchenko | b8b5727 | 2016-05-20 17:00:57 -0700 | [diff] [blame] | 1375 | int i; |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1376 | char tbuf[64] = {0,}; |
Roberto Sassu | 8cdc23a | 2019-05-29 15:30:35 +0200 | [diff] [blame] | 1377 | int offset = 0; |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1378 | |
| 1379 | rcu_read_lock(); |
| 1380 | |
| 1381 | if (entry->action & MEASURE) |
| 1382 | seq_puts(m, pt(Opt_measure)); |
| 1383 | if (entry->action & DONT_MEASURE) |
| 1384 | seq_puts(m, pt(Opt_dont_measure)); |
| 1385 | if (entry->action & APPRAISE) |
| 1386 | seq_puts(m, pt(Opt_appraise)); |
| 1387 | if (entry->action & DONT_APPRAISE) |
| 1388 | seq_puts(m, pt(Opt_dont_appraise)); |
| 1389 | if (entry->action & AUDIT) |
| 1390 | seq_puts(m, pt(Opt_audit)); |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 1391 | if (entry->action & HASH) |
| 1392 | seq_puts(m, pt(Opt_hash)); |
| 1393 | if (entry->action & DONT_HASH) |
| 1394 | seq_puts(m, pt(Opt_dont_hash)); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1395 | |
| 1396 | seq_puts(m, " "); |
| 1397 | |
Mimi Zohar | b5269ab | 2016-01-28 13:10:36 -0500 | [diff] [blame] | 1398 | if (entry->flags & IMA_FUNC) |
| 1399 | policy_func_show(m, entry->func); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1400 | |
Roberto Sassu | 8cdc23a | 2019-05-29 15:30:35 +0200 | [diff] [blame] | 1401 | if ((entry->flags & IMA_MASK) || (entry->flags & IMA_INMASK)) { |
| 1402 | if (entry->flags & IMA_MASK) |
| 1403 | offset = 1; |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1404 | if (entry->mask & MAY_EXEC) |
Roberto Sassu | 8cdc23a | 2019-05-29 15:30:35 +0200 | [diff] [blame] | 1405 | seq_printf(m, pt(Opt_mask), mt(mask_exec) + offset); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1406 | if (entry->mask & MAY_WRITE) |
Roberto Sassu | 8cdc23a | 2019-05-29 15:30:35 +0200 | [diff] [blame] | 1407 | seq_printf(m, pt(Opt_mask), mt(mask_write) + offset); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1408 | if (entry->mask & MAY_READ) |
Roberto Sassu | 8cdc23a | 2019-05-29 15:30:35 +0200 | [diff] [blame] | 1409 | seq_printf(m, pt(Opt_mask), mt(mask_read) + offset); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1410 | if (entry->mask & MAY_APPEND) |
Roberto Sassu | 8cdc23a | 2019-05-29 15:30:35 +0200 | [diff] [blame] | 1411 | seq_printf(m, pt(Opt_mask), mt(mask_append) + offset); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1412 | seq_puts(m, " "); |
| 1413 | } |
| 1414 | |
| 1415 | if (entry->flags & IMA_FSMAGIC) { |
| 1416 | snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic); |
| 1417 | seq_printf(m, pt(Opt_fsmagic), tbuf); |
| 1418 | seq_puts(m, " "); |
| 1419 | } |
| 1420 | |
Mimi Zohar | f1b08bb | 2018-01-15 11:20:36 -0500 | [diff] [blame] | 1421 | if (entry->flags & IMA_FSNAME) { |
| 1422 | snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname); |
| 1423 | seq_printf(m, pt(Opt_fsname), tbuf); |
| 1424 | seq_puts(m, " "); |
| 1425 | } |
| 1426 | |
Eric Richter | 0260643 | 2016-06-01 13:14:01 -0500 | [diff] [blame] | 1427 | if (entry->flags & IMA_PCR) { |
| 1428 | snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr); |
| 1429 | seq_printf(m, pt(Opt_pcr), tbuf); |
| 1430 | seq_puts(m, " "); |
| 1431 | } |
| 1432 | |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1433 | if (entry->flags & IMA_FSUUID) { |
Christoph Hellwig | 787d8c5 | 2017-06-01 07:00:26 +0200 | [diff] [blame] | 1434 | seq_printf(m, "fsuuid=%pU", &entry->fsuuid); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1435 | seq_puts(m, " "); |
| 1436 | } |
| 1437 | |
| 1438 | if (entry->flags & IMA_UID) { |
| 1439 | snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid)); |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1440 | if (entry->uid_op == &uid_gt) |
| 1441 | seq_printf(m, pt(Opt_uid_gt), tbuf); |
| 1442 | else if (entry->uid_op == &uid_lt) |
| 1443 | seq_printf(m, pt(Opt_uid_lt), tbuf); |
| 1444 | else |
| 1445 | seq_printf(m, pt(Opt_uid_eq), tbuf); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1446 | seq_puts(m, " "); |
| 1447 | } |
| 1448 | |
| 1449 | if (entry->flags & IMA_EUID) { |
| 1450 | snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid)); |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1451 | if (entry->uid_op == &uid_gt) |
| 1452 | seq_printf(m, pt(Opt_euid_gt), tbuf); |
| 1453 | else if (entry->uid_op == &uid_lt) |
| 1454 | seq_printf(m, pt(Opt_euid_lt), tbuf); |
| 1455 | else |
| 1456 | seq_printf(m, pt(Opt_euid_eq), tbuf); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1457 | seq_puts(m, " "); |
| 1458 | } |
| 1459 | |
| 1460 | if (entry->flags & IMA_FOWNER) { |
| 1461 | snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner)); |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1462 | if (entry->fowner_op == &uid_gt) |
| 1463 | seq_printf(m, pt(Opt_fowner_gt), tbuf); |
| 1464 | else if (entry->fowner_op == &uid_lt) |
| 1465 | seq_printf(m, pt(Opt_fowner_lt), tbuf); |
| 1466 | else |
| 1467 | seq_printf(m, pt(Opt_fowner_eq), tbuf); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1468 | seq_puts(m, " "); |
| 1469 | } |
| 1470 | |
| 1471 | for (i = 0; i < MAX_LSM_RULES; i++) { |
| 1472 | if (entry->lsm[i].rule) { |
| 1473 | switch (i) { |
| 1474 | case LSM_OBJ_USER: |
| 1475 | seq_printf(m, pt(Opt_obj_user), |
| 1476 | (char *)entry->lsm[i].args_p); |
| 1477 | break; |
| 1478 | case LSM_OBJ_ROLE: |
| 1479 | seq_printf(m, pt(Opt_obj_role), |
| 1480 | (char *)entry->lsm[i].args_p); |
| 1481 | break; |
| 1482 | case LSM_OBJ_TYPE: |
| 1483 | seq_printf(m, pt(Opt_obj_type), |
| 1484 | (char *)entry->lsm[i].args_p); |
| 1485 | break; |
| 1486 | case LSM_SUBJ_USER: |
| 1487 | seq_printf(m, pt(Opt_subj_user), |
| 1488 | (char *)entry->lsm[i].args_p); |
| 1489 | break; |
| 1490 | case LSM_SUBJ_ROLE: |
| 1491 | seq_printf(m, pt(Opt_subj_role), |
| 1492 | (char *)entry->lsm[i].args_p); |
| 1493 | break; |
| 1494 | case LSM_SUBJ_TYPE: |
| 1495 | seq_printf(m, pt(Opt_subj_type), |
| 1496 | (char *)entry->lsm[i].args_p); |
| 1497 | break; |
| 1498 | } |
| 1499 | } |
| 1500 | } |
Matthew Garrett | 19453ce0 | 2019-06-19 15:46:11 -0700 | [diff] [blame] | 1501 | if (entry->template) |
| 1502 | seq_printf(m, "template=%s ", entry->template->name); |
Thiago Jung Bauermann | 9044d62 | 2019-06-27 23:19:28 -0300 | [diff] [blame] | 1503 | if (entry->flags & IMA_DIGSIG_REQUIRED) { |
| 1504 | if (entry->flags & IMA_MODSIG_ALLOWED) |
| 1505 | seq_puts(m, "appraise_type=imasig|modsig "); |
| 1506 | else |
| 1507 | seq_puts(m, "appraise_type=imasig "); |
| 1508 | } |
Nayna Jain | 273df86 | 2019-10-30 23:31:32 -0400 | [diff] [blame] | 1509 | if (entry->flags & IMA_CHECK_BLACKLIST) |
| 1510 | seq_puts(m, "appraise_flag=check_blacklist "); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1511 | if (entry->flags & IMA_PERMIT_DIRECTIO) |
| 1512 | seq_puts(m, "permit_directio "); |
| 1513 | rcu_read_unlock(); |
| 1514 | seq_puts(m, "\n"); |
| 1515 | return 0; |
| 1516 | } |
| 1517 | #endif /* CONFIG_IMA_READ_POLICY */ |
Matthew Garrett | 29d3c1c | 2019-08-19 17:18:01 -0700 | [diff] [blame] | 1518 | |
| 1519 | #if defined(CONFIG_IMA_APPRAISE) && defined(CONFIG_INTEGRITY_TRUSTED_KEYRING) |
| 1520 | /* |
| 1521 | * ima_appraise_signature: whether IMA will appraise a given function using |
| 1522 | * an IMA digital signature. This is restricted to cases where the kernel |
| 1523 | * has a set of built-in trusted keys in order to avoid an attacker simply |
| 1524 | * loading additional keys. |
| 1525 | */ |
| 1526 | bool ima_appraise_signature(enum kernel_read_file_id id) |
| 1527 | { |
| 1528 | struct ima_rule_entry *entry; |
| 1529 | bool found = false; |
| 1530 | enum ima_hooks func; |
| 1531 | |
| 1532 | if (id >= READING_MAX_ID) |
| 1533 | return false; |
| 1534 | |
| 1535 | func = read_idmap[id] ?: FILE_CHECK; |
| 1536 | |
| 1537 | rcu_read_lock(); |
| 1538 | list_for_each_entry_rcu(entry, ima_rules, list) { |
| 1539 | if (entry->action != APPRAISE) |
| 1540 | continue; |
| 1541 | |
| 1542 | /* |
| 1543 | * A generic entry will match, but otherwise require that it |
| 1544 | * match the func we're looking for |
| 1545 | */ |
| 1546 | if (entry->func && entry->func != func) |
| 1547 | continue; |
| 1548 | |
| 1549 | /* |
| 1550 | * We require this to be a digital signature, not a raw IMA |
| 1551 | * hash. |
| 1552 | */ |
| 1553 | if (entry->flags & IMA_DIGSIG_REQUIRED) |
| 1554 | found = true; |
| 1555 | |
| 1556 | /* |
| 1557 | * We've found a rule that matches, so break now even if it |
| 1558 | * didn't require a digital signature - a later rule that does |
| 1559 | * won't override it, so would be a false positive. |
| 1560 | */ |
| 1561 | break; |
| 1562 | } |
| 1563 | |
| 1564 | rcu_read_unlock(); |
| 1565 | return found; |
| 1566 | } |
| 1567 | #endif /* CONFIG_IMA_APPRAISE && CONFIG_INTEGRITY_TRUSTED_KEYRING */ |