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