Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 IBM Corporation |
| 3 | * Author: Mimi Zohar <zohar@us.ibm.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, version 2 of the License. |
| 8 | * |
| 9 | * ima_policy.c |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 10 | * - initialize default measure policy rules |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 11 | * |
| 12 | */ |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/list.h> |
Mimi Zohar | cf22221 | 2016-01-14 17:57:47 -0500 | [diff] [blame] | 15 | #include <linux/fs.h> |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 16 | #include <linux/security.h> |
| 17 | #include <linux/magic.h> |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 18 | #include <linux/parser.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 20 | #include <linux/rculist.h> |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 21 | #include <linux/genhd.h> |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 22 | #include <linux/seq_file.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) || \ |
| 48 | (a) >= (FIELD_SIZEOF(struct integrity_iint_cache, measured_pcrs) * 8)) |
| 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 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 61 | struct ima_rule_entry { |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 62 | struct list_head list; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 63 | int action; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 64 | unsigned int flags; |
| 65 | enum ima_hooks func; |
| 66 | int mask; |
| 67 | unsigned long fsmagic; |
Christoph Hellwig | 787d8c5 | 2017-06-01 07:00:26 +0200 | [diff] [blame] | 68 | uuid_t fsuuid; |
Eric W. Biederman | 8b94eea | 2012-05-25 18:24:12 -0600 | [diff] [blame] | 69 | kuid_t uid; |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 70 | kuid_t fowner; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 71 | bool (*uid_op)(kuid_t, kuid_t); /* Handlers for operators */ |
| 72 | 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] | 73 | int pcr; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 74 | struct { |
| 75 | void *rule; /* LSM file metadata specific */ |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 76 | void *args_p; /* audit value */ |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 77 | int type; /* audit type */ |
| 78 | } lsm[MAX_LSM_RULES]; |
Mimi Zohar | f1b08bb | 2018-01-15 11:20:36 -0500 | [diff] [blame] | 79 | char *fsname; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 80 | }; |
| 81 | |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 82 | /* |
| 83 | * Without LSM specific knowledge, the default policy can only be |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 84 | * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 85 | */ |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * The minimum rule set to allow for full TCB coverage. Measures all files |
| 89 | * opened or mmap for exec and everything read by root. Dangerous because |
| 90 | * normal users can easily run the machine out of memory simply building |
| 91 | * and running executables. |
| 92 | */ |
James Morris | bad4417 | 2017-02-13 16:34:35 +1100 | [diff] [blame] | 93 | static struct ima_rule_entry dont_measure_rules[] __ro_after_init = { |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 94 | {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 95 | {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 96 | {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 97 | {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 98 | {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 99 | {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 100 | {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 101 | {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC}, |
Martin Townsend | 1c070b1 | 2018-03-22 13:46:01 +0000 | [diff] [blame] | 102 | {.action = DONT_MEASURE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC}, |
Roberto Sassu | 6438de9 | 2015-04-11 17:13:06 +0200 | [diff] [blame] | 103 | {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC, |
| 104 | .flags = IMA_FSMAGIC}, |
Laura Abbott | 82e3bb4 | 2017-05-09 11:25:27 -0700 | [diff] [blame] | 105 | {.action = DONT_MEASURE, .fsmagic = CGROUP2_SUPER_MAGIC, |
| 106 | .flags = IMA_FSMAGIC}, |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 107 | {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC} |
| 108 | }; |
| 109 | |
James Morris | bad4417 | 2017-02-13 16:34:35 +1100 | [diff] [blame] | 110 | static struct ima_rule_entry original_measurement_rules[] __ro_after_init = { |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 111 | {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC, |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 112 | .flags = IMA_FUNC | IMA_MASK}, |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 113 | {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC, |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 114 | .flags = IMA_FUNC | IMA_MASK}, |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 115 | {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ, |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 116 | .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq, |
| 117 | .flags = IMA_FUNC | IMA_MASK | IMA_UID}, |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 118 | {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC}, |
| 119 | {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC}, |
| 120 | }; |
| 121 | |
James Morris | bad4417 | 2017-02-13 16:34:35 +1100 | [diff] [blame] | 122 | static struct ima_rule_entry default_measurement_rules[] __ro_after_init = { |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 123 | {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC, |
| 124 | .flags = IMA_FUNC | IMA_MASK}, |
| 125 | {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC, |
| 126 | .flags = IMA_FUNC | IMA_MASK}, |
| 127 | {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ, |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 128 | .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq, |
| 129 | .flags = IMA_FUNC | IMA_INMASK | IMA_EUID}, |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 130 | {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ, |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 131 | .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq, |
| 132 | .flags = IMA_FUNC | IMA_INMASK | IMA_UID}, |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 133 | {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC}, |
Mimi Zohar | 5a9196d | 2014-07-22 10:39:48 -0400 | [diff] [blame] | 134 | {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC}, |
Mimi Zohar | 19f8a84 | 2016-01-15 10:17:12 -0500 | [diff] [blame] | 135 | {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC}, |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 136 | }; |
| 137 | |
James Morris | bad4417 | 2017-02-13 16:34:35 +1100 | [diff] [blame] | 138 | static struct ima_rule_entry default_appraise_rules[] __ro_after_init = { |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 139 | {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 140 | {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 141 | {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 142 | {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 143 | {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 144 | {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 145 | {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 146 | {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 147 | {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC}, |
Martin Townsend | 1c070b1 | 2018-03-22 13:46:01 +0000 | [diff] [blame] | 148 | {.action = DONT_APPRAISE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC}, |
Mimi Zohar | cd025f7 | 2015-04-21 16:54:24 -0400 | [diff] [blame] | 149 | {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC}, |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 150 | {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
Laura Abbott | 82e3bb4 | 2017-05-09 11:25:27 -0700 | [diff] [blame] | 151 | {.action = DONT_APPRAISE, .fsmagic = CGROUP2_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
Mimi Zohar | 95ee08f | 2015-12-07 15:08:01 -0500 | [diff] [blame] | 152 | #ifdef CONFIG_IMA_WRITE_POLICY |
| 153 | {.action = APPRAISE, .func = POLICY_CHECK, |
| 154 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 155 | #endif |
Dmitry Kasatkin | c57782c | 2014-11-05 17:01:16 +0200 | [diff] [blame] | 156 | #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 157 | {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq, |
| 158 | .flags = IMA_FOWNER}, |
Dmitry Kasatkin | c57782c | 2014-11-05 17:01:16 +0200 | [diff] [blame] | 159 | #else |
| 160 | /* force signature */ |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 161 | {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq, |
Dmitry Kasatkin | c57782c | 2014-11-05 17:01:16 +0200 | [diff] [blame] | 162 | .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED}, |
| 163 | #endif |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 164 | }; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 165 | |
Mimi Zohar | ef96837 | 2018-07-13 14:06:01 -0400 | [diff] [blame] | 166 | static struct ima_rule_entry build_appraise_rules[] __ro_after_init = { |
| 167 | #ifdef CONFIG_IMA_APPRAISE_REQUIRE_MODULE_SIGS |
| 168 | {.action = APPRAISE, .func = MODULE_CHECK, |
| 169 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 170 | #endif |
| 171 | #ifdef CONFIG_IMA_APPRAISE_REQUIRE_FIRMWARE_SIGS |
| 172 | {.action = APPRAISE, .func = FIRMWARE_CHECK, |
| 173 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 174 | #endif |
| 175 | #ifdef CONFIG_IMA_APPRAISE_REQUIRE_KEXEC_SIGS |
| 176 | {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK, |
| 177 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 178 | #endif |
| 179 | #ifdef CONFIG_IMA_APPRAISE_REQUIRE_POLICY_SIGS |
| 180 | {.action = APPRAISE, .func = POLICY_CHECK, |
| 181 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 182 | #endif |
| 183 | }; |
| 184 | |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 185 | static struct ima_rule_entry secure_boot_rules[] __ro_after_init = { |
| 186 | {.action = APPRAISE, .func = MODULE_CHECK, |
| 187 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 188 | {.action = APPRAISE, .func = FIRMWARE_CHECK, |
| 189 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 190 | {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK, |
| 191 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 192 | {.action = APPRAISE, .func = POLICY_CHECK, |
| 193 | .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED}, |
| 194 | }; |
| 195 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 196 | static LIST_HEAD(ima_default_rules); |
| 197 | static LIST_HEAD(ima_policy_rules); |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 198 | static LIST_HEAD(ima_temp_rules); |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 199 | static struct list_head *ima_rules; |
| 200 | |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 201 | static int ima_policy __initdata; |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 202 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 203 | static int __init default_measure_policy_setup(char *str) |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 204 | { |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 205 | if (ima_policy) |
| 206 | return 1; |
| 207 | |
| 208 | ima_policy = ORIGINAL_TCB; |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 209 | return 1; |
| 210 | } |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 211 | __setup("ima_tcb", default_measure_policy_setup); |
| 212 | |
Mimi Zohar | 33ce954 | 2017-04-24 12:04:09 -0400 | [diff] [blame] | 213 | static bool ima_use_appraise_tcb __initdata; |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 214 | static bool ima_use_secure_boot __initdata; |
Mimi Zohar | 9e67028 | 2018-02-21 11:36:32 -0500 | [diff] [blame] | 215 | static bool ima_fail_unverifiable_sigs __ro_after_init; |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 216 | static int __init policy_setup(char *str) |
| 217 | { |
Mimi Zohar | 33ce954 | 2017-04-24 12:04:09 -0400 | [diff] [blame] | 218 | char *p; |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 219 | |
Mimi Zohar | 33ce954 | 2017-04-24 12:04:09 -0400 | [diff] [blame] | 220 | while ((p = strsep(&str, " |\n")) != NULL) { |
| 221 | if (*p == ' ') |
| 222 | continue; |
| 223 | if ((strcmp(p, "tcb") == 0) && !ima_policy) |
| 224 | ima_policy = DEFAULT_TCB; |
| 225 | else if (strcmp(p, "appraise_tcb") == 0) |
Thomas Meyer | 39adb92 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 226 | ima_use_appraise_tcb = true; |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 227 | else if (strcmp(p, "secure_boot") == 0) |
Thomas Meyer | 39adb92 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 228 | ima_use_secure_boot = true; |
Mimi Zohar | 9e67028 | 2018-02-21 11:36:32 -0500 | [diff] [blame] | 229 | else if (strcmp(p, "fail_securely") == 0) |
| 230 | ima_fail_unverifiable_sigs = true; |
Mimi Zohar | 33ce954 | 2017-04-24 12:04:09 -0400 | [diff] [blame] | 231 | } |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 232 | |
| 233 | return 1; |
| 234 | } |
| 235 | __setup("ima_policy=", policy_setup); |
| 236 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 237 | static int __init default_appraise_policy_setup(char *str) |
| 238 | { |
Thomas Meyer | 39adb92 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 239 | ima_use_appraise_tcb = true; |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 240 | return 1; |
| 241 | } |
| 242 | __setup("ima_appraise_tcb", default_appraise_policy_setup); |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 243 | |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 244 | /* |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 245 | * The LSM policy can be reloaded, leaving the IMA LSM based rules referring |
| 246 | * to the old, stale LSM policy. Update the IMA LSM based rules to reflect |
| 247 | * the reloaded LSM policy. We assume the rules still exist; and BUG_ON() if |
| 248 | * they don't. |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 249 | */ |
| 250 | static void ima_lsm_update_rules(void) |
| 251 | { |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 252 | struct ima_rule_entry *entry; |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 253 | int result; |
| 254 | int i; |
| 255 | |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 256 | list_for_each_entry(entry, &ima_policy_rules, list) { |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 257 | for (i = 0; i < MAX_LSM_RULES; i++) { |
| 258 | if (!entry->lsm[i].rule) |
| 259 | continue; |
| 260 | result = security_filter_rule_init(entry->lsm[i].type, |
| 261 | Audit_equal, |
| 262 | entry->lsm[i].args_p, |
| 263 | &entry->lsm[i].rule); |
| 264 | BUG_ON(!entry->lsm[i].rule); |
| 265 | } |
| 266 | } |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 267 | } |
| 268 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 269 | /** |
| 270 | * ima_match_rules - determine whether an inode matches the measure rule. |
| 271 | * @rule: a pointer to a rule |
| 272 | * @inode: a pointer to an inode |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 273 | * @cred: a pointer to a credentials structure for user validation |
| 274 | * @secid: the secid of the task to be validated |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 275 | * @func: LIM hook identifier |
| 276 | * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC) |
| 277 | * |
| 278 | * Returns true on rule match, false on failure. |
| 279 | */ |
Mimi Zohar | 4ad87a3 | 2016-01-14 20:59:14 -0500 | [diff] [blame] | 280 | 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] | 281 | const struct cred *cred, u32 secid, |
Mimi Zohar | 4ad87a3 | 2016-01-14 20:59:14 -0500 | [diff] [blame] | 282 | enum ima_hooks func, int mask) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 283 | { |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 284 | int i; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 285 | |
Dmitry Kasatkin | 09b1148 | 2013-11-13 23:42:39 +0200 | [diff] [blame] | 286 | if ((rule->flags & IMA_FUNC) && |
| 287 | (rule->func != func && func != POST_SETATTR)) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 288 | return false; |
Dmitry Kasatkin | 09b1148 | 2013-11-13 23:42:39 +0200 | [diff] [blame] | 289 | if ((rule->flags & IMA_MASK) && |
| 290 | (rule->mask != mask && func != POST_SETATTR)) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 291 | return false; |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 292 | if ((rule->flags & IMA_INMASK) && |
| 293 | (!(rule->mask & mask) && func != POST_SETATTR)) |
| 294 | return false; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 295 | if ((rule->flags & IMA_FSMAGIC) |
| 296 | && rule->fsmagic != inode->i_sb->s_magic) |
| 297 | return false; |
Mimi Zohar | f1b08bb | 2018-01-15 11:20:36 -0500 | [diff] [blame] | 298 | if ((rule->flags & IMA_FSNAME) |
| 299 | && strcmp(rule->fsname, inode->i_sb->s_type->name)) |
| 300 | return false; |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 301 | if ((rule->flags & IMA_FSUUID) && |
Christoph Hellwig | 8578709 | 2017-05-10 15:06:33 +0200 | [diff] [blame] | 302 | !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid)) |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 303 | return false; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 304 | if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid)) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 305 | return false; |
Mimi Zohar | 139069e | 2014-11-05 07:48:36 -0500 | [diff] [blame] | 306 | if (rule->flags & IMA_EUID) { |
| 307 | if (has_capability_noaudit(current, CAP_SETUID)) { |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 308 | if (!rule->uid_op(cred->euid, rule->uid) |
| 309 | && !rule->uid_op(cred->suid, rule->uid) |
| 310 | && !rule->uid_op(cred->uid, rule->uid)) |
Mimi Zohar | 139069e | 2014-11-05 07:48:36 -0500 | [diff] [blame] | 311 | return false; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 312 | } else if (!rule->uid_op(cred->euid, rule->uid)) |
Mimi Zohar | 139069e | 2014-11-05 07:48:36 -0500 | [diff] [blame] | 313 | return false; |
| 314 | } |
| 315 | |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 316 | if ((rule->flags & IMA_FOWNER) && |
| 317 | !rule->fowner_op(inode->i_uid, rule->fowner)) |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 318 | return false; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 319 | for (i = 0; i < MAX_LSM_RULES; i++) { |
Mimi Zohar | 53fc0e2 | 2009-05-05 13:12:48 -0400 | [diff] [blame] | 320 | int rc = 0; |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 321 | u32 osid; |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 322 | int retried = 0; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 323 | |
| 324 | if (!rule->lsm[i].rule) |
| 325 | continue; |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 326 | retry: |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 327 | switch (i) { |
| 328 | case LSM_OBJ_USER: |
| 329 | case LSM_OBJ_ROLE: |
| 330 | case LSM_OBJ_TYPE: |
| 331 | security_inode_getsecid(inode, &osid); |
| 332 | rc = security_filter_rule_match(osid, |
| 333 | rule->lsm[i].type, |
Mimi Zohar | 53fc0e2 | 2009-05-05 13:12:48 -0400 | [diff] [blame] | 334 | Audit_equal, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 335 | rule->lsm[i].rule, |
| 336 | NULL); |
| 337 | break; |
| 338 | case LSM_SUBJ_USER: |
| 339 | case LSM_SUBJ_ROLE: |
| 340 | case LSM_SUBJ_TYPE: |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 341 | rc = security_filter_rule_match(secid, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 342 | rule->lsm[i].type, |
Mimi Zohar | 53fc0e2 | 2009-05-05 13:12:48 -0400 | [diff] [blame] | 343 | Audit_equal, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 344 | rule->lsm[i].rule, |
| 345 | NULL); |
| 346 | default: |
| 347 | break; |
| 348 | } |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 349 | if ((rc < 0) && (!retried)) { |
| 350 | retried = 1; |
| 351 | ima_lsm_update_rules(); |
| 352 | goto retry; |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 353 | } |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 354 | if (!rc) |
| 355 | return false; |
| 356 | } |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 357 | return true; |
| 358 | } |
| 359 | |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 360 | /* |
| 361 | * 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] | 362 | * we need to differentiate between calling hooks, for hook specific rules. |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 363 | */ |
Mimi Zohar | 4ad87a3 | 2016-01-14 20:59:14 -0500 | [diff] [blame] | 364 | 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] | 365 | { |
Mimi Zohar | 5a73fcf | 2012-12-05 15:14:38 -0500 | [diff] [blame] | 366 | if (!(rule->flags & IMA_FUNC)) |
| 367 | return IMA_FILE_APPRAISE; |
| 368 | |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 369 | switch (func) { |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 370 | case MMAP_CHECK: |
| 371 | return IMA_MMAP_APPRAISE; |
| 372 | case BPRM_CHECK: |
| 373 | return IMA_BPRM_APPRAISE; |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 374 | case CREDS_CHECK: |
| 375 | return IMA_CREDS_APPRAISE; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 376 | case FILE_CHECK: |
Mimi Zohar | c6af8ef | 2015-11-19 12:39:22 -0500 | [diff] [blame] | 377 | case POST_SETATTR: |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 378 | return IMA_FILE_APPRAISE; |
Mimi Zohar | c6af8ef | 2015-11-19 12:39:22 -0500 | [diff] [blame] | 379 | case MODULE_CHECK ... MAX_CHECK - 1: |
| 380 | default: |
| 381 | return IMA_READ_APPRAISE; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 385 | /** |
| 386 | * ima_match_policy - decision based on LSM and other conditions |
| 387 | * @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] | 388 | * @cred: pointer to a credentials structure for which the policy decision is |
| 389 | * being made |
| 390 | * @secid: LSM secid of the task to be validated |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 391 | * @func: IMA hook identifier |
| 392 | * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC) |
Eric Richter | 725de7f | 2016-06-01 13:14:02 -0500 | [diff] [blame] | 393 | * @pcr: set the pcr to extend |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 394 | * |
| 395 | * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type) |
| 396 | * conditions. |
| 397 | * |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 398 | * Since the IMA policy may be updated multiple times we need to lock the |
| 399 | * list when walking it. Reads are many orders of magnitude more numerous |
| 400 | * than writes so ima_match_policy() is classical RCU candidate. |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 401 | */ |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 402 | int ima_match_policy(struct inode *inode, const struct cred *cred, u32 secid, |
| 403 | enum ima_hooks func, int mask, int flags, int *pcr) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 404 | { |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 405 | struct ima_rule_entry *entry; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 406 | int action = 0, actmask = flags | (flags << 1); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 407 | |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 408 | rcu_read_lock(); |
| 409 | list_for_each_entry_rcu(entry, ima_rules, list) { |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 410 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 411 | if (!(entry->action & actmask)) |
| 412 | continue; |
| 413 | |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 414 | if (!ima_match_rules(entry, inode, cred, secid, func, mask)) |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 415 | continue; |
| 416 | |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 417 | action |= entry->flags & IMA_ACTION_FLAGS; |
| 418 | |
Dmitry Kasatkin | 45e2472 | 2012-09-12 20:51:32 +0300 | [diff] [blame] | 419 | action |= entry->action & IMA_DO_MASK; |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 420 | if (entry->action & IMA_APPRAISE) { |
Mimi Zohar | 5a73fcf | 2012-12-05 15:14:38 -0500 | [diff] [blame] | 421 | action |= get_subaction(entry, func); |
Mimi Zohar | a9a4935 | 2018-03-10 23:07:34 -0500 | [diff] [blame] | 422 | action &= ~IMA_HASH; |
Mimi Zohar | 9e67028 | 2018-02-21 11:36:32 -0500 | [diff] [blame] | 423 | if (ima_fail_unverifiable_sigs) |
| 424 | action |= IMA_FAIL_UNVERIFIABLE_SIGS; |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 425 | } |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 426 | |
Dmitry Kasatkin | 45e2472 | 2012-09-12 20:51:32 +0300 | [diff] [blame] | 427 | if (entry->action & IMA_DO_MASK) |
| 428 | actmask &= ~(entry->action | entry->action << 1); |
| 429 | else |
| 430 | actmask &= ~(entry->action | entry->action >> 1); |
| 431 | |
Eric Richter | 725de7f | 2016-06-01 13:14:02 -0500 | [diff] [blame] | 432 | if ((pcr) && (entry->flags & IMA_PCR)) |
| 433 | *pcr = entry->pcr; |
| 434 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 435 | if (!actmask) |
| 436 | break; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 437 | } |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 438 | rcu_read_unlock(); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 439 | |
| 440 | return action; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 441 | } |
| 442 | |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 443 | /* |
| 444 | * Initialize the ima_policy_flag variable based on the currently |
| 445 | * loaded policy. Based on this flag, the decision to short circuit |
| 446 | * out of a function or not call the function in the first place |
| 447 | * can be made earlier. |
| 448 | */ |
| 449 | void ima_update_policy_flag(void) |
| 450 | { |
| 451 | struct ima_rule_entry *entry; |
| 452 | |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 453 | list_for_each_entry(entry, ima_rules, list) { |
| 454 | if (entry->action & IMA_DO_MASK) |
| 455 | ima_policy_flag |= entry->action; |
| 456 | } |
| 457 | |
Mimi Zohar | ef96837 | 2018-07-13 14:06:01 -0400 | [diff] [blame] | 458 | ima_appraise |= (build_ima_appraise | temp_ima_appraise); |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 459 | if (!ima_appraise) |
| 460 | ima_policy_flag &= ~IMA_APPRAISE; |
| 461 | } |
| 462 | |
Mimi Zohar | 6f0911a | 2018-04-12 00:15:22 -0400 | [diff] [blame] | 463 | static int ima_appraise_flag(enum ima_hooks func) |
| 464 | { |
| 465 | if (func == MODULE_CHECK) |
| 466 | return IMA_APPRAISE_MODULES; |
| 467 | else if (func == FIRMWARE_CHECK) |
| 468 | return IMA_APPRAISE_FIRMWARE; |
| 469 | else if (func == POLICY_CHECK) |
| 470 | return IMA_APPRAISE_POLICY; |
Mimi Zohar | 16c267a | 2018-07-13 14:05:58 -0400 | [diff] [blame] | 471 | else if (func == KEXEC_KERNEL_CHECK) |
| 472 | return IMA_APPRAISE_KEXEC; |
Mimi Zohar | 6f0911a | 2018-04-12 00:15:22 -0400 | [diff] [blame] | 473 | return 0; |
| 474 | } |
| 475 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 476 | /** |
| 477 | * ima_init_policy - initialize the default measure rules. |
| 478 | * |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 479 | * ima_rules points to either the ima_default_rules or the |
| 480 | * the new ima_policy_rules. |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 481 | */ |
Eric Paris | 932995f | 2009-05-21 15:43:32 -0400 | [diff] [blame] | 482 | void __init ima_init_policy(void) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 483 | { |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 484 | int i, measure_entries, appraise_entries, secure_boot_entries; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 485 | |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 486 | /* if !ima_policy set entries = 0 so we load NO default rules */ |
| 487 | measure_entries = ima_policy ? ARRAY_SIZE(dont_measure_rules) : 0; |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 488 | appraise_entries = ima_use_appraise_tcb ? |
| 489 | ARRAY_SIZE(default_appraise_rules) : 0; |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 490 | secure_boot_entries = ima_use_secure_boot ? |
| 491 | ARRAY_SIZE(secure_boot_rules) : 0; |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 492 | |
Dan Carpenter | 5577857 | 2015-04-07 12:22:11 +0300 | [diff] [blame] | 493 | for (i = 0; i < measure_entries; i++) |
Mimi Zohar | 24fd03c | 2015-06-11 20:48:33 -0400 | [diff] [blame] | 494 | list_add_tail(&dont_measure_rules[i].list, &ima_default_rules); |
| 495 | |
| 496 | switch (ima_policy) { |
| 497 | case ORIGINAL_TCB: |
| 498 | for (i = 0; i < ARRAY_SIZE(original_measurement_rules); i++) |
| 499 | list_add_tail(&original_measurement_rules[i].list, |
| 500 | &ima_default_rules); |
| 501 | break; |
| 502 | case DEFAULT_TCB: |
| 503 | for (i = 0; i < ARRAY_SIZE(default_measurement_rules); i++) |
| 504 | list_add_tail(&default_measurement_rules[i].list, |
| 505 | &ima_default_rules); |
| 506 | default: |
| 507 | break; |
| 508 | } |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 509 | |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 510 | /* |
Mimi Zohar | ef96837 | 2018-07-13 14:06:01 -0400 | [diff] [blame] | 511 | * Insert the builtin "secure_boot" policy rules requiring file |
| 512 | * signatures, prior to any other appraise rules. |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 513 | */ |
Mimi Zohar | 6f0911a | 2018-04-12 00:15:22 -0400 | [diff] [blame] | 514 | for (i = 0; i < secure_boot_entries; i++) { |
| 515 | list_add_tail(&secure_boot_rules[i].list, &ima_default_rules); |
| 516 | temp_ima_appraise |= |
| 517 | ima_appraise_flag(secure_boot_rules[i].func); |
| 518 | } |
Mimi Zohar | 503ceae | 2017-04-21 18:58:27 -0400 | [diff] [blame] | 519 | |
Mimi Zohar | ef96837 | 2018-07-13 14:06:01 -0400 | [diff] [blame] | 520 | /* |
| 521 | * Insert the build time appraise rules requiring file signatures |
| 522 | * for both the initial and custom policies, prior to other appraise |
| 523 | * rules. |
| 524 | */ |
| 525 | for (i = 0; i < ARRAY_SIZE(build_appraise_rules); i++) { |
| 526 | struct ima_rule_entry *entry; |
| 527 | |
| 528 | if (!secure_boot_entries) |
| 529 | list_add_tail(&build_appraise_rules[i].list, |
| 530 | &ima_default_rules); |
| 531 | |
| 532 | entry = kmemdup(&build_appraise_rules[i], sizeof(*entry), |
| 533 | GFP_KERNEL); |
| 534 | if (entry) |
| 535 | list_add_tail(&entry->list, &ima_policy_rules); |
| 536 | build_ima_appraise |= |
| 537 | ima_appraise_flag(build_appraise_rules[i].func); |
| 538 | } |
| 539 | |
Dan Carpenter | 5577857 | 2015-04-07 12:22:11 +0300 | [diff] [blame] | 540 | for (i = 0; i < appraise_entries; i++) { |
| 541 | list_add_tail(&default_appraise_rules[i].list, |
| 542 | &ima_default_rules); |
Mimi Zohar | 95ee08f | 2015-12-07 15:08:01 -0500 | [diff] [blame] | 543 | if (default_appraise_rules[i].func == POLICY_CHECK) |
| 544 | temp_ima_appraise |= IMA_APPRAISE_POLICY; |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | ima_rules = &ima_default_rules; |
Mimi Zohar | 95ee08f | 2015-12-07 15:08:01 -0500 | [diff] [blame] | 548 | ima_update_policy_flag(); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 549 | } |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 550 | |
Sasha Levin | 0112721 | 2015-12-22 08:51:23 -0500 | [diff] [blame] | 551 | /* 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] | 552 | int ima_check_policy(void) |
Sasha Levin | 0112721 | 2015-12-22 08:51:23 -0500 | [diff] [blame] | 553 | { |
| 554 | if (list_empty(&ima_temp_rules)) |
| 555 | return -EINVAL; |
| 556 | return 0; |
| 557 | } |
| 558 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 559 | /** |
| 560 | * ima_update_policy - update default_rules with new measure rules |
| 561 | * |
| 562 | * 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] | 563 | * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so |
| 564 | * they make a queue. The policy may be updated multiple times and this is the |
| 565 | * RCU updater. |
| 566 | * |
| 567 | * Policy rules are never deleted so ima_policy_flag gets zeroed only once when |
| 568 | * we switch from the default policy to user defined. |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 569 | */ |
| 570 | void ima_update_policy(void) |
| 571 | { |
Petko Manolov | 53b626f | 2018-05-22 17:06:55 +0300 | [diff] [blame] | 572 | struct list_head *policy = &ima_policy_rules; |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 573 | |
Petko Manolov | 53b626f | 2018-05-22 17:06:55 +0300 | [diff] [blame] | 574 | list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu); |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 575 | |
| 576 | if (ima_rules != policy) { |
| 577 | ima_policy_flag = 0; |
| 578 | ima_rules = policy; |
| 579 | } |
Dmitry Kasatkin | 0716abb | 2014-10-03 14:40:21 +0300 | [diff] [blame] | 580 | ima_update_policy_flag(); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | enum { |
| 584 | Opt_err = -1, |
| 585 | Opt_measure = 1, Opt_dont_measure, |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 586 | Opt_appraise, Opt_dont_appraise, |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 587 | Opt_audit, Opt_hash, Opt_dont_hash, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 588 | Opt_obj_user, Opt_obj_role, Opt_obj_type, |
| 589 | Opt_subj_user, Opt_subj_role, Opt_subj_type, |
Mimi Zohar | f1b08bb | 2018-01-15 11:20:36 -0500 | [diff] [blame] | 590 | Opt_func, Opt_mask, Opt_fsmagic, Opt_fsname, |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 591 | Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq, |
| 592 | Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt, |
| 593 | Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt, |
Eric Richter | 0260643 | 2016-06-01 13:14:01 -0500 | [diff] [blame] | 594 | Opt_appraise_type, Opt_permit_directio, |
| 595 | Opt_pcr |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 596 | }; |
| 597 | |
| 598 | static match_table_t policy_tokens = { |
| 599 | {Opt_measure, "measure"}, |
| 600 | {Opt_dont_measure, "dont_measure"}, |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 601 | {Opt_appraise, "appraise"}, |
| 602 | {Opt_dont_appraise, "dont_appraise"}, |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 603 | {Opt_audit, "audit"}, |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 604 | {Opt_hash, "hash"}, |
| 605 | {Opt_dont_hash, "dont_hash"}, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 606 | {Opt_obj_user, "obj_user=%s"}, |
| 607 | {Opt_obj_role, "obj_role=%s"}, |
| 608 | {Opt_obj_type, "obj_type=%s"}, |
| 609 | {Opt_subj_user, "subj_user=%s"}, |
| 610 | {Opt_subj_role, "subj_role=%s"}, |
| 611 | {Opt_subj_type, "subj_type=%s"}, |
| 612 | {Opt_func, "func=%s"}, |
| 613 | {Opt_mask, "mask=%s"}, |
| 614 | {Opt_fsmagic, "fsmagic=%s"}, |
Mimi Zohar | f1b08bb | 2018-01-15 11:20:36 -0500 | [diff] [blame] | 615 | {Opt_fsname, "fsname=%s"}, |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 616 | {Opt_fsuuid, "fsuuid=%s"}, |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 617 | {Opt_uid_eq, "uid=%s"}, |
| 618 | {Opt_euid_eq, "euid=%s"}, |
| 619 | {Opt_fowner_eq, "fowner=%s"}, |
| 620 | {Opt_uid_gt, "uid>%s"}, |
| 621 | {Opt_euid_gt, "euid>%s"}, |
| 622 | {Opt_fowner_gt, "fowner>%s"}, |
| 623 | {Opt_uid_lt, "uid<%s"}, |
| 624 | {Opt_euid_lt, "euid<%s"}, |
| 625 | {Opt_fowner_lt, "fowner<%s"}, |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 626 | {Opt_appraise_type, "appraise_type=%s"}, |
Mimi Zohar | f9b2a73 | 2014-05-12 09:28:11 -0400 | [diff] [blame] | 627 | {Opt_permit_directio, "permit_directio"}, |
Eric Richter | 0260643 | 2016-06-01 13:14:01 -0500 | [diff] [blame] | 628 | {Opt_pcr, "pcr=%s"}, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 629 | {Opt_err, NULL} |
| 630 | }; |
| 631 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 632 | static int ima_lsm_rule_init(struct ima_rule_entry *entry, |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 633 | substring_t *args, int lsm_rule, int audit_type) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 634 | { |
| 635 | int result; |
| 636 | |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 637 | if (entry->lsm[lsm_rule].rule) |
| 638 | return -EINVAL; |
| 639 | |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 640 | entry->lsm[lsm_rule].args_p = match_strdup(args); |
| 641 | if (!entry->lsm[lsm_rule].args_p) |
| 642 | return -ENOMEM; |
| 643 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 644 | entry->lsm[lsm_rule].type = audit_type; |
| 645 | result = security_filter_rule_init(entry->lsm[lsm_rule].type, |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 646 | Audit_equal, |
| 647 | entry->lsm[lsm_rule].args_p, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 648 | &entry->lsm[lsm_rule].rule); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 649 | if (!entry->lsm[lsm_rule].rule) { |
| 650 | kfree(entry->lsm[lsm_rule].args_p); |
Mimi Zohar | 867c202 | 2011-01-03 14:59:10 -0800 | [diff] [blame] | 651 | return -EINVAL; |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 652 | } |
| 653 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 654 | return result; |
| 655 | } |
| 656 | |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 657 | static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value, |
| 658 | bool (*rule_operator)(kuid_t, kuid_t)) |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 659 | { |
Stefan Berger | 2afd020 | 2018-06-04 16:54:54 -0400 | [diff] [blame] | 660 | if (!ab) |
| 661 | return; |
| 662 | |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 663 | if (rule_operator == &uid_gt) |
| 664 | audit_log_format(ab, "%s>", key); |
| 665 | else if (rule_operator == &uid_lt) |
| 666 | audit_log_format(ab, "%s<", key); |
| 667 | else |
| 668 | audit_log_format(ab, "%s=", key); |
Stefan Berger | 3d2859d | 2018-06-04 16:54:53 -0400 | [diff] [blame] | 669 | audit_log_format(ab, "%s ", value); |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 670 | } |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 671 | static void ima_log_string(struct audit_buffer *ab, char *key, char *value) |
| 672 | { |
| 673 | ima_log_string_op(ab, key, value, NULL); |
| 674 | } |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 675 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 676 | static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 677 | { |
| 678 | struct audit_buffer *ab; |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 679 | char *from; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 680 | char *p; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 681 | bool uid_token; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 682 | int result = 0; |
| 683 | |
Stefan Berger | dba31ee | 2018-06-04 16:54:55 -0400 | [diff] [blame] | 684 | ab = integrity_audit_log_start(audit_context(), GFP_KERNEL, |
| 685 | AUDIT_INTEGRITY_POLICY_RULE); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 686 | |
Eric W. Biederman | 8b94eea | 2012-05-25 18:24:12 -0600 | [diff] [blame] | 687 | entry->uid = INVALID_UID; |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 688 | entry->fowner = INVALID_UID; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 689 | entry->uid_op = &uid_eq; |
| 690 | entry->fowner_op = &uid_eq; |
Eric Paris | b9035b1 | 2010-04-20 10:21:07 -0400 | [diff] [blame] | 691 | entry->action = UNKNOWN; |
Eric Paris | 28ef400 | 2010-04-20 10:21:18 -0400 | [diff] [blame] | 692 | while ((p = strsep(&rule, " \t")) != NULL) { |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 693 | substring_t args[MAX_OPT_ARGS]; |
| 694 | int token; |
| 695 | unsigned long lnum; |
| 696 | |
| 697 | if (result < 0) |
| 698 | break; |
Eric Paris | 28ef400 | 2010-04-20 10:21:18 -0400 | [diff] [blame] | 699 | if ((*p == '\0') || (*p == ' ') || (*p == '\t')) |
| 700 | continue; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 701 | token = match_token(p, policy_tokens, args); |
| 702 | switch (token) { |
| 703 | case Opt_measure: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 704 | ima_log_string(ab, "action", "measure"); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 705 | |
| 706 | if (entry->action != UNKNOWN) |
| 707 | result = -EINVAL; |
| 708 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 709 | entry->action = MEASURE; |
| 710 | break; |
| 711 | case Opt_dont_measure: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 712 | ima_log_string(ab, "action", "dont_measure"); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 713 | |
| 714 | if (entry->action != UNKNOWN) |
| 715 | result = -EINVAL; |
| 716 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 717 | entry->action = DONT_MEASURE; |
| 718 | break; |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 719 | case Opt_appraise: |
| 720 | ima_log_string(ab, "action", "appraise"); |
| 721 | |
| 722 | if (entry->action != UNKNOWN) |
| 723 | result = -EINVAL; |
| 724 | |
| 725 | entry->action = APPRAISE; |
| 726 | break; |
| 727 | case Opt_dont_appraise: |
| 728 | ima_log_string(ab, "action", "dont_appraise"); |
| 729 | |
| 730 | if (entry->action != UNKNOWN) |
| 731 | result = -EINVAL; |
| 732 | |
| 733 | entry->action = DONT_APPRAISE; |
| 734 | break; |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 735 | case Opt_audit: |
| 736 | ima_log_string(ab, "action", "audit"); |
| 737 | |
| 738 | if (entry->action != UNKNOWN) |
| 739 | result = -EINVAL; |
| 740 | |
| 741 | entry->action = AUDIT; |
| 742 | break; |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 743 | case Opt_hash: |
| 744 | ima_log_string(ab, "action", "hash"); |
| 745 | |
| 746 | if (entry->action != UNKNOWN) |
| 747 | result = -EINVAL; |
| 748 | |
| 749 | entry->action = HASH; |
| 750 | break; |
| 751 | case Opt_dont_hash: |
| 752 | ima_log_string(ab, "action", "dont_hash"); |
| 753 | |
| 754 | if (entry->action != UNKNOWN) |
| 755 | result = -EINVAL; |
| 756 | |
| 757 | entry->action = DONT_HASH; |
| 758 | break; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 759 | case Opt_func: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 760 | ima_log_string(ab, "func", args[0].from); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 761 | |
| 762 | if (entry->func) |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 763 | result = -EINVAL; |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 764 | |
Mimi Zohar | 1e93d00 | 2010-01-26 17:02:41 -0500 | [diff] [blame] | 765 | if (strcmp(args[0].from, "FILE_CHECK") == 0) |
| 766 | entry->func = FILE_CHECK; |
| 767 | /* PATH_CHECK is for backwards compat */ |
| 768 | else if (strcmp(args[0].from, "PATH_CHECK") == 0) |
| 769 | entry->func = FILE_CHECK; |
Mimi Zohar | fdf9072 | 2012-10-16 12:40:08 +1030 | [diff] [blame] | 770 | else if (strcmp(args[0].from, "MODULE_CHECK") == 0) |
| 771 | entry->func = MODULE_CHECK; |
Mimi Zohar | 5a9196d | 2014-07-22 10:39:48 -0400 | [diff] [blame] | 772 | else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0) |
| 773 | entry->func = FIRMWARE_CHECK; |
Mimi Zohar | 16cac49 | 2012-12-13 11:15:04 -0500 | [diff] [blame] | 774 | else if ((strcmp(args[0].from, "FILE_MMAP") == 0) |
| 775 | || (strcmp(args[0].from, "MMAP_CHECK") == 0)) |
| 776 | entry->func = MMAP_CHECK; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 777 | else if (strcmp(args[0].from, "BPRM_CHECK") == 0) |
| 778 | entry->func = BPRM_CHECK; |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 779 | else if (strcmp(args[0].from, "CREDS_CHECK") == 0) |
| 780 | entry->func = CREDS_CHECK; |
Mimi Zohar | d9ddf07 | 2016-01-14 20:59:14 -0500 | [diff] [blame] | 781 | else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") == |
| 782 | 0) |
| 783 | entry->func = KEXEC_KERNEL_CHECK; |
| 784 | else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK") |
| 785 | == 0) |
| 786 | entry->func = KEXEC_INITRAMFS_CHECK; |
Mimi Zohar | 19f8a84 | 2016-01-15 10:17:12 -0500 | [diff] [blame] | 787 | else if (strcmp(args[0].from, "POLICY_CHECK") == 0) |
| 788 | entry->func = POLICY_CHECK; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 789 | else |
| 790 | result = -EINVAL; |
| 791 | if (!result) |
| 792 | entry->flags |= IMA_FUNC; |
| 793 | break; |
| 794 | case Opt_mask: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 795 | ima_log_string(ab, "mask", args[0].from); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 796 | |
| 797 | if (entry->mask) |
| 798 | result = -EINVAL; |
| 799 | |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 800 | from = args[0].from; |
| 801 | if (*from == '^') |
| 802 | from++; |
| 803 | |
| 804 | if ((strcmp(from, "MAY_EXEC")) == 0) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 805 | entry->mask = MAY_EXEC; |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 806 | else if (strcmp(from, "MAY_WRITE") == 0) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 807 | entry->mask = MAY_WRITE; |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 808 | else if (strcmp(from, "MAY_READ") == 0) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 809 | entry->mask = MAY_READ; |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 810 | else if (strcmp(from, "MAY_APPEND") == 0) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 811 | entry->mask = MAY_APPEND; |
| 812 | else |
| 813 | result = -EINVAL; |
| 814 | if (!result) |
Mimi Zohar | 4351c29 | 2014-11-05 07:53:55 -0500 | [diff] [blame] | 815 | entry->flags |= (*args[0].from == '^') |
| 816 | ? IMA_INMASK : IMA_MASK; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 817 | break; |
| 818 | case Opt_fsmagic: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 819 | ima_log_string(ab, "fsmagic", args[0].from); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 820 | |
| 821 | if (entry->fsmagic) { |
| 822 | result = -EINVAL; |
| 823 | break; |
| 824 | } |
| 825 | |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 826 | result = kstrtoul(args[0].from, 16, &entry->fsmagic); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 827 | if (!result) |
| 828 | entry->flags |= IMA_FSMAGIC; |
| 829 | break; |
Mimi Zohar | f1b08bb | 2018-01-15 11:20:36 -0500 | [diff] [blame] | 830 | case Opt_fsname: |
| 831 | ima_log_string(ab, "fsname", args[0].from); |
| 832 | |
| 833 | entry->fsname = kstrdup(args[0].from, GFP_KERNEL); |
| 834 | if (!entry->fsname) { |
| 835 | result = -ENOMEM; |
| 836 | break; |
| 837 | } |
| 838 | result = 0; |
| 839 | entry->flags |= IMA_FSNAME; |
| 840 | break; |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 841 | case Opt_fsuuid: |
| 842 | ima_log_string(ab, "fsuuid", args[0].from); |
| 843 | |
Mike Rapoport | 3644745 | 2018-01-17 20:27:11 +0200 | [diff] [blame] | 844 | if (!uuid_is_null(&entry->fsuuid)) { |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 845 | result = -EINVAL; |
| 846 | break; |
| 847 | } |
| 848 | |
Christoph Hellwig | 787d8c5 | 2017-06-01 07:00:26 +0200 | [diff] [blame] | 849 | result = uuid_parse(args[0].from, &entry->fsuuid); |
Mimi Zohar | 446d64e | 2013-02-24 23:42:37 -0500 | [diff] [blame] | 850 | if (!result) |
| 851 | entry->flags |= IMA_FSUUID; |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 852 | break; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 853 | case Opt_uid_gt: |
| 854 | case Opt_euid_gt: |
| 855 | entry->uid_op = &uid_gt; |
| 856 | case Opt_uid_lt: |
| 857 | case Opt_euid_lt: |
| 858 | if ((token == Opt_uid_lt) || (token == Opt_euid_lt)) |
| 859 | entry->uid_op = &uid_lt; |
| 860 | case Opt_uid_eq: |
| 861 | case Opt_euid_eq: |
| 862 | uid_token = (token == Opt_uid_eq) || |
| 863 | (token == Opt_uid_gt) || |
| 864 | (token == Opt_uid_lt); |
| 865 | |
| 866 | ima_log_string_op(ab, uid_token ? "uid" : "euid", |
| 867 | args[0].from, entry->uid_op); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 868 | |
Eric W. Biederman | 8b94eea | 2012-05-25 18:24:12 -0600 | [diff] [blame] | 869 | if (uid_valid(entry->uid)) { |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 870 | result = -EINVAL; |
| 871 | break; |
| 872 | } |
| 873 | |
Jingoo Han | 29707b2 | 2014-02-05 15:13:14 +0900 | [diff] [blame] | 874 | result = kstrtoul(args[0].from, 10, &lnum); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 875 | if (!result) { |
Mimi Zohar | 139069e | 2014-11-05 07:48:36 -0500 | [diff] [blame] | 876 | entry->uid = make_kuid(current_user_ns(), |
| 877 | (uid_t) lnum); |
| 878 | if (!uid_valid(entry->uid) || |
| 879 | (uid_t)lnum != lnum) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 880 | result = -EINVAL; |
| 881 | else |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 882 | entry->flags |= uid_token |
Mimi Zohar | 139069e | 2014-11-05 07:48:36 -0500 | [diff] [blame] | 883 | ? IMA_UID : IMA_EUID; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 884 | } |
| 885 | break; |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 886 | case Opt_fowner_gt: |
| 887 | entry->fowner_op = &uid_gt; |
| 888 | case Opt_fowner_lt: |
| 889 | if (token == Opt_fowner_lt) |
| 890 | entry->fowner_op = &uid_lt; |
| 891 | case Opt_fowner_eq: |
| 892 | ima_log_string_op(ab, "fowner", args[0].from, |
| 893 | entry->fowner_op); |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 894 | |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 895 | if (uid_valid(entry->fowner)) { |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 896 | result = -EINVAL; |
| 897 | break; |
| 898 | } |
| 899 | |
Jingoo Han | 29707b2 | 2014-02-05 15:13:14 +0900 | [diff] [blame] | 900 | result = kstrtoul(args[0].from, 10, &lnum); |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 901 | if (!result) { |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 902 | entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum); |
| 903 | if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum)) |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 904 | result = -EINVAL; |
| 905 | else |
| 906 | entry->flags |= IMA_FOWNER; |
| 907 | } |
| 908 | break; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 909 | case Opt_obj_user: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 910 | ima_log_string(ab, "obj_user", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 911 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 912 | LSM_OBJ_USER, |
| 913 | AUDIT_OBJ_USER); |
| 914 | break; |
| 915 | case Opt_obj_role: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 916 | ima_log_string(ab, "obj_role", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 917 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 918 | LSM_OBJ_ROLE, |
| 919 | AUDIT_OBJ_ROLE); |
| 920 | break; |
| 921 | case Opt_obj_type: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 922 | ima_log_string(ab, "obj_type", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 923 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 924 | LSM_OBJ_TYPE, |
| 925 | AUDIT_OBJ_TYPE); |
| 926 | break; |
| 927 | case Opt_subj_user: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 928 | ima_log_string(ab, "subj_user", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 929 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 930 | LSM_SUBJ_USER, |
| 931 | AUDIT_SUBJ_USER); |
| 932 | break; |
| 933 | case Opt_subj_role: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 934 | ima_log_string(ab, "subj_role", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 935 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 936 | LSM_SUBJ_ROLE, |
| 937 | AUDIT_SUBJ_ROLE); |
| 938 | break; |
| 939 | case Opt_subj_type: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 940 | ima_log_string(ab, "subj_type", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 941 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 942 | LSM_SUBJ_TYPE, |
| 943 | AUDIT_SUBJ_TYPE); |
| 944 | break; |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 945 | case Opt_appraise_type: |
| 946 | if (entry->action != APPRAISE) { |
| 947 | result = -EINVAL; |
| 948 | break; |
| 949 | } |
| 950 | |
| 951 | ima_log_string(ab, "appraise_type", args[0].from); |
| 952 | if ((strcmp(args[0].from, "imasig")) == 0) |
| 953 | entry->flags |= IMA_DIGSIG_REQUIRED; |
| 954 | else |
| 955 | result = -EINVAL; |
| 956 | break; |
Mimi Zohar | f9b2a73 | 2014-05-12 09:28:11 -0400 | [diff] [blame] | 957 | case Opt_permit_directio: |
| 958 | entry->flags |= IMA_PERMIT_DIRECTIO; |
| 959 | break; |
Eric Richter | 0260643 | 2016-06-01 13:14:01 -0500 | [diff] [blame] | 960 | case Opt_pcr: |
| 961 | if (entry->action != MEASURE) { |
| 962 | result = -EINVAL; |
| 963 | break; |
| 964 | } |
| 965 | ima_log_string(ab, "pcr", args[0].from); |
| 966 | |
| 967 | result = kstrtoint(args[0].from, 10, &entry->pcr); |
| 968 | if (result || INVALID_PCR(entry->pcr)) |
| 969 | result = -EINVAL; |
| 970 | else |
| 971 | entry->flags |= IMA_PCR; |
| 972 | |
| 973 | break; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 974 | case Opt_err: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 975 | ima_log_string(ab, "UNKNOWN", p); |
Eric Paris | e9d393b | 2010-04-20 10:21:13 -0400 | [diff] [blame] | 976 | result = -EINVAL; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 977 | break; |
| 978 | } |
| 979 | } |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 980 | if (!result && (entry->action == UNKNOWN)) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 981 | result = -EINVAL; |
Mimi Zohar | 6f0911a | 2018-04-12 00:15:22 -0400 | [diff] [blame] | 982 | else if (entry->action == APPRAISE) |
| 983 | temp_ima_appraise |= ima_appraise_flag(entry->func); |
| 984 | |
Eric Paris | b0d5de4 | 2012-02-14 17:11:07 -0500 | [diff] [blame] | 985 | audit_log_format(ab, "res=%d", !result); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 986 | audit_log_end(ab); |
| 987 | return result; |
| 988 | } |
| 989 | |
| 990 | /** |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 991 | * ima_parse_add_rule - add a rule to ima_policy_rules |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 992 | * @rule - ima measurement policy rule |
| 993 | * |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 994 | * 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] | 995 | * Returns the length of the rule parsed, an error code on failure |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 996 | */ |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 997 | ssize_t ima_parse_add_rule(char *rule) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 998 | { |
Mimi Zohar | 52a1328 | 2013-12-11 14:44:04 -0500 | [diff] [blame] | 999 | static const char op[] = "update_policy"; |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 1000 | char *p; |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 1001 | struct ima_rule_entry *entry; |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 1002 | ssize_t result, len; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1003 | int audit_info = 0; |
| 1004 | |
Dmitry Kasatkin | 272a6e9 | 2014-10-03 14:40:19 +0300 | [diff] [blame] | 1005 | p = strsep(&rule, "\n"); |
| 1006 | len = strlen(p) + 1; |
Dmitry Kasatkin | 7178784 | 2014-10-03 14:40:20 +0300 | [diff] [blame] | 1007 | p += strspn(p, " \t"); |
Dmitry Kasatkin | 272a6e9 | 2014-10-03 14:40:19 +0300 | [diff] [blame] | 1008 | |
Dmitry Kasatkin | 7178784 | 2014-10-03 14:40:20 +0300 | [diff] [blame] | 1009 | if (*p == '#' || *p == '\0') |
Dmitry Kasatkin | 272a6e9 | 2014-10-03 14:40:19 +0300 | [diff] [blame] | 1010 | return len; |
| 1011 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1012 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 1013 | if (!entry) { |
| 1014 | integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, |
| 1015 | NULL, op, "-ENOMEM", -ENOMEM, audit_info); |
| 1016 | return -ENOMEM; |
| 1017 | } |
| 1018 | |
| 1019 | INIT_LIST_HEAD(&entry->list); |
| 1020 | |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 1021 | result = ima_parse_rule(p, entry); |
Eric Paris | 7233e3e | 2010-04-20 10:21:24 -0400 | [diff] [blame] | 1022 | if (result) { |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1023 | kfree(entry); |
Mimi Zohar | 523979a | 2009-02-11 11:12:28 -0500 | [diff] [blame] | 1024 | integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, |
Richard Guy Briggs | 7e9001f | 2014-06-16 15:52:07 -0400 | [diff] [blame] | 1025 | NULL, op, "invalid-policy", result, |
Mimi Zohar | 523979a | 2009-02-11 11:12:28 -0500 | [diff] [blame] | 1026 | audit_info); |
Eric Paris | 7233e3e | 2010-04-20 10:21:24 -0400 | [diff] [blame] | 1027 | return result; |
Mimi Zohar | 523979a | 2009-02-11 11:12:28 -0500 | [diff] [blame] | 1028 | } |
Eric Paris | 7233e3e | 2010-04-20 10:21:24 -0400 | [diff] [blame] | 1029 | |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 1030 | list_add_tail(&entry->list, &ima_temp_rules); |
Eric Paris | 7233e3e | 2010-04-20 10:21:24 -0400 | [diff] [blame] | 1031 | |
| 1032 | return len; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1033 | } |
| 1034 | |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 1035 | /** |
| 1036 | * ima_delete_rules() called to cleanup invalid in-flight policy. |
| 1037 | * We don't need locking as we operate on the temp list, which is |
| 1038 | * different from the active one. There is also only one user of |
| 1039 | * ima_delete_rules() at a time. |
| 1040 | */ |
James Morris | 64c61d8 | 2009-02-05 09:28:26 +1100 | [diff] [blame] | 1041 | void ima_delete_rules(void) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1042 | { |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 1043 | struct ima_rule_entry *entry, *tmp; |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 1044 | int i; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1045 | |
Mimi Zohar | 6ad6afa | 2015-12-07 14:35:47 -0500 | [diff] [blame] | 1046 | temp_ima_appraise = 0; |
Petko Manolov | 38d859f | 2015-12-02 17:47:54 +0200 | [diff] [blame] | 1047 | list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) { |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 1048 | for (i = 0; i < MAX_LSM_RULES; i++) |
| 1049 | kfree(entry->lsm[i].args_p); |
| 1050 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1051 | list_del(&entry->list); |
| 1052 | kfree(entry); |
| 1053 | } |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 1054 | } |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1055 | |
| 1056 | #ifdef CONFIG_IMA_READ_POLICY |
| 1057 | enum { |
| 1058 | mask_exec = 0, mask_write, mask_read, mask_append |
| 1059 | }; |
| 1060 | |
Thiago Jung Bauermann | bb543e3 | 2017-06-07 22:49:10 -0300 | [diff] [blame] | 1061 | static const char *const mask_tokens[] = { |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1062 | "MAY_EXEC", |
| 1063 | "MAY_WRITE", |
| 1064 | "MAY_READ", |
| 1065 | "MAY_APPEND" |
| 1066 | }; |
| 1067 | |
Thiago Jung Bauermann | 2663218 | 2017-06-07 22:49:11 -0300 | [diff] [blame] | 1068 | #define __ima_hook_stringify(str) (#str), |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1069 | |
Thiago Jung Bauermann | bb543e3 | 2017-06-07 22:49:10 -0300 | [diff] [blame] | 1070 | static const char *const func_tokens[] = { |
Thiago Jung Bauermann | 2663218 | 2017-06-07 22:49:11 -0300 | [diff] [blame] | 1071 | __ima_hooks(__ima_hook_stringify) |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1072 | }; |
| 1073 | |
| 1074 | void *ima_policy_start(struct seq_file *m, loff_t *pos) |
| 1075 | { |
| 1076 | loff_t l = *pos; |
| 1077 | struct ima_rule_entry *entry; |
| 1078 | |
| 1079 | rcu_read_lock(); |
| 1080 | list_for_each_entry_rcu(entry, ima_rules, list) { |
| 1081 | if (!l--) { |
| 1082 | rcu_read_unlock(); |
| 1083 | return entry; |
| 1084 | } |
| 1085 | } |
| 1086 | rcu_read_unlock(); |
| 1087 | return NULL; |
| 1088 | } |
| 1089 | |
| 1090 | void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos) |
| 1091 | { |
| 1092 | struct ima_rule_entry *entry = v; |
| 1093 | |
| 1094 | rcu_read_lock(); |
| 1095 | entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list); |
| 1096 | rcu_read_unlock(); |
| 1097 | (*pos)++; |
| 1098 | |
| 1099 | return (&entry->list == ima_rules) ? NULL : entry; |
| 1100 | } |
| 1101 | |
| 1102 | void ima_policy_stop(struct seq_file *m, void *v) |
| 1103 | { |
| 1104 | } |
| 1105 | |
| 1106 | #define pt(token) policy_tokens[token + Opt_err].pattern |
| 1107 | #define mt(token) mask_tokens[token] |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1108 | |
Mimi Zohar | b5269ab | 2016-01-28 13:10:36 -0500 | [diff] [blame] | 1109 | /* |
| 1110 | * policy_func_show - display the ima_hooks policy rule |
| 1111 | */ |
| 1112 | static void policy_func_show(struct seq_file *m, enum ima_hooks func) |
| 1113 | { |
Thiago Jung Bauermann | 2663218 | 2017-06-07 22:49:11 -0300 | [diff] [blame] | 1114 | if (func > 0 && func < MAX_CHECK) |
| 1115 | seq_printf(m, "func=%s ", func_tokens[func]); |
| 1116 | else |
| 1117 | seq_printf(m, "func=%d ", func); |
Mimi Zohar | b5269ab | 2016-01-28 13:10:36 -0500 | [diff] [blame] | 1118 | } |
| 1119 | |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1120 | int ima_policy_show(struct seq_file *m, void *v) |
| 1121 | { |
| 1122 | struct ima_rule_entry *entry = v; |
Andy Shevchenko | b8b5727 | 2016-05-20 17:00:57 -0700 | [diff] [blame] | 1123 | int i; |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1124 | char tbuf[64] = {0,}; |
| 1125 | |
| 1126 | rcu_read_lock(); |
| 1127 | |
| 1128 | if (entry->action & MEASURE) |
| 1129 | seq_puts(m, pt(Opt_measure)); |
| 1130 | if (entry->action & DONT_MEASURE) |
| 1131 | seq_puts(m, pt(Opt_dont_measure)); |
| 1132 | if (entry->action & APPRAISE) |
| 1133 | seq_puts(m, pt(Opt_appraise)); |
| 1134 | if (entry->action & DONT_APPRAISE) |
| 1135 | seq_puts(m, pt(Opt_dont_appraise)); |
| 1136 | if (entry->action & AUDIT) |
| 1137 | seq_puts(m, pt(Opt_audit)); |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 1138 | if (entry->action & HASH) |
| 1139 | seq_puts(m, pt(Opt_hash)); |
| 1140 | if (entry->action & DONT_HASH) |
| 1141 | seq_puts(m, pt(Opt_dont_hash)); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1142 | |
| 1143 | seq_puts(m, " "); |
| 1144 | |
Mimi Zohar | b5269ab | 2016-01-28 13:10:36 -0500 | [diff] [blame] | 1145 | if (entry->flags & IMA_FUNC) |
| 1146 | policy_func_show(m, entry->func); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1147 | |
| 1148 | if (entry->flags & IMA_MASK) { |
| 1149 | if (entry->mask & MAY_EXEC) |
| 1150 | seq_printf(m, pt(Opt_mask), mt(mask_exec)); |
| 1151 | if (entry->mask & MAY_WRITE) |
| 1152 | seq_printf(m, pt(Opt_mask), mt(mask_write)); |
| 1153 | if (entry->mask & MAY_READ) |
| 1154 | seq_printf(m, pt(Opt_mask), mt(mask_read)); |
| 1155 | if (entry->mask & MAY_APPEND) |
| 1156 | seq_printf(m, pt(Opt_mask), mt(mask_append)); |
| 1157 | seq_puts(m, " "); |
| 1158 | } |
| 1159 | |
| 1160 | if (entry->flags & IMA_FSMAGIC) { |
| 1161 | snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic); |
| 1162 | seq_printf(m, pt(Opt_fsmagic), tbuf); |
| 1163 | seq_puts(m, " "); |
| 1164 | } |
| 1165 | |
Mimi Zohar | f1b08bb | 2018-01-15 11:20:36 -0500 | [diff] [blame] | 1166 | if (entry->flags & IMA_FSNAME) { |
| 1167 | snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname); |
| 1168 | seq_printf(m, pt(Opt_fsname), tbuf); |
| 1169 | seq_puts(m, " "); |
| 1170 | } |
| 1171 | |
Eric Richter | 0260643 | 2016-06-01 13:14:01 -0500 | [diff] [blame] | 1172 | if (entry->flags & IMA_PCR) { |
| 1173 | snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr); |
| 1174 | seq_printf(m, pt(Opt_pcr), tbuf); |
| 1175 | seq_puts(m, " "); |
| 1176 | } |
| 1177 | |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1178 | if (entry->flags & IMA_FSUUID) { |
Christoph Hellwig | 787d8c5 | 2017-06-01 07:00:26 +0200 | [diff] [blame] | 1179 | seq_printf(m, "fsuuid=%pU", &entry->fsuuid); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1180 | seq_puts(m, " "); |
| 1181 | } |
| 1182 | |
| 1183 | if (entry->flags & IMA_UID) { |
| 1184 | snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid)); |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1185 | if (entry->uid_op == &uid_gt) |
| 1186 | seq_printf(m, pt(Opt_uid_gt), tbuf); |
| 1187 | else if (entry->uid_op == &uid_lt) |
| 1188 | seq_printf(m, pt(Opt_uid_lt), tbuf); |
| 1189 | else |
| 1190 | seq_printf(m, pt(Opt_uid_eq), tbuf); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1191 | seq_puts(m, " "); |
| 1192 | } |
| 1193 | |
| 1194 | if (entry->flags & IMA_EUID) { |
| 1195 | snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid)); |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1196 | if (entry->uid_op == &uid_gt) |
| 1197 | seq_printf(m, pt(Opt_euid_gt), tbuf); |
| 1198 | else if (entry->uid_op == &uid_lt) |
| 1199 | seq_printf(m, pt(Opt_euid_lt), tbuf); |
| 1200 | else |
| 1201 | seq_printf(m, pt(Opt_euid_eq), tbuf); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1202 | seq_puts(m, " "); |
| 1203 | } |
| 1204 | |
| 1205 | if (entry->flags & IMA_FOWNER) { |
| 1206 | snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner)); |
Mikhail Kurinnoi | 3dd0c8d | 2017-01-27 19:23:01 +0300 | [diff] [blame] | 1207 | if (entry->fowner_op == &uid_gt) |
| 1208 | seq_printf(m, pt(Opt_fowner_gt), tbuf); |
| 1209 | else if (entry->fowner_op == &uid_lt) |
| 1210 | seq_printf(m, pt(Opt_fowner_lt), tbuf); |
| 1211 | else |
| 1212 | seq_printf(m, pt(Opt_fowner_eq), tbuf); |
Petko Manolov | 80eae20 | 2015-12-02 17:47:56 +0200 | [diff] [blame] | 1213 | seq_puts(m, " "); |
| 1214 | } |
| 1215 | |
| 1216 | for (i = 0; i < MAX_LSM_RULES; i++) { |
| 1217 | if (entry->lsm[i].rule) { |
| 1218 | switch (i) { |
| 1219 | case LSM_OBJ_USER: |
| 1220 | seq_printf(m, pt(Opt_obj_user), |
| 1221 | (char *)entry->lsm[i].args_p); |
| 1222 | break; |
| 1223 | case LSM_OBJ_ROLE: |
| 1224 | seq_printf(m, pt(Opt_obj_role), |
| 1225 | (char *)entry->lsm[i].args_p); |
| 1226 | break; |
| 1227 | case LSM_OBJ_TYPE: |
| 1228 | seq_printf(m, pt(Opt_obj_type), |
| 1229 | (char *)entry->lsm[i].args_p); |
| 1230 | break; |
| 1231 | case LSM_SUBJ_USER: |
| 1232 | seq_printf(m, pt(Opt_subj_user), |
| 1233 | (char *)entry->lsm[i].args_p); |
| 1234 | break; |
| 1235 | case LSM_SUBJ_ROLE: |
| 1236 | seq_printf(m, pt(Opt_subj_role), |
| 1237 | (char *)entry->lsm[i].args_p); |
| 1238 | break; |
| 1239 | case LSM_SUBJ_TYPE: |
| 1240 | seq_printf(m, pt(Opt_subj_type), |
| 1241 | (char *)entry->lsm[i].args_p); |
| 1242 | break; |
| 1243 | } |
| 1244 | } |
| 1245 | } |
| 1246 | if (entry->flags & IMA_DIGSIG_REQUIRED) |
| 1247 | seq_puts(m, "appraise_type=imasig "); |
| 1248 | if (entry->flags & IMA_PERMIT_DIRECTIO) |
| 1249 | seq_puts(m, "permit_directio "); |
| 1250 | rcu_read_unlock(); |
| 1251 | seq_puts(m, "\n"); |
| 1252 | return 0; |
| 1253 | } |
| 1254 | #endif /* CONFIG_IMA_READ_POLICY */ |