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