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