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> |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 19 | #include <linux/genhd.h> |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 20 | |
| 21 | #include "ima.h" |
| 22 | |
| 23 | /* flags definitions */ |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 24 | #define IMA_FUNC 0x0001 |
| 25 | #define IMA_MASK 0x0002 |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 26 | #define IMA_FSMAGIC 0x0004 |
| 27 | #define IMA_UID 0x0008 |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 28 | #define IMA_FOWNER 0x0010 |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 29 | #define IMA_FSUUID 0x0020 |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 30 | |
Dmitry Kasatkin | 45e2472 | 2012-09-12 20:51:32 +0300 | [diff] [blame] | 31 | #define UNKNOWN 0 |
| 32 | #define MEASURE 0x0001 /* same as IMA_MEASURE */ |
| 33 | #define DONT_MEASURE 0x0002 |
| 34 | #define APPRAISE 0x0004 /* same as IMA_APPRAISE */ |
| 35 | #define DONT_APPRAISE 0x0008 |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 36 | #define AUDIT 0x0040 |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 37 | |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 38 | int ima_policy_flag; |
| 39 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 40 | #define MAX_LSM_RULES 6 |
| 41 | enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE, |
| 42 | LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE |
| 43 | }; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 44 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 45 | struct ima_rule_entry { |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 46 | struct list_head list; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 47 | int action; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 48 | unsigned int flags; |
| 49 | enum ima_hooks func; |
| 50 | int mask; |
| 51 | unsigned long fsmagic; |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 52 | u8 fsuuid[16]; |
Eric W. Biederman | 8b94eea | 2012-05-25 18:24:12 -0600 | [diff] [blame] | 53 | kuid_t uid; |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 54 | kuid_t fowner; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 55 | struct { |
| 56 | void *rule; /* LSM file metadata specific */ |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 57 | void *args_p; /* audit value */ |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 58 | int type; /* audit type */ |
| 59 | } lsm[MAX_LSM_RULES]; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 60 | }; |
| 61 | |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 62 | /* |
| 63 | * Without LSM specific knowledge, the default policy can only be |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 64 | * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 65 | */ |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | * The minimum rule set to allow for full TCB coverage. Measures all files |
| 69 | * opened or mmap for exec and everything read by root. Dangerous because |
| 70 | * normal users can easily run the machine out of memory simply building |
| 71 | * and running executables. |
| 72 | */ |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 73 | static struct ima_rule_entry default_rules[] = { |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 74 | {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 75 | {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 76 | {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 77 | {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 78 | {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 79 | {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 80 | {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 81 | {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC}, |
| 82 | {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC, |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 83 | .flags = IMA_FUNC | IMA_MASK}, |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 84 | {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC, |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 85 | .flags = IMA_FUNC | IMA_MASK}, |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 86 | {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ, .uid = GLOBAL_ROOT_UID, |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 87 | .flags = IMA_FUNC | IMA_MASK | IMA_UID}, |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 88 | {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC}, |
Mimi Zohar | 5a9196d | 2014-07-22 10:39:48 -0400 | [diff] [blame] | 89 | {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC}, |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 90 | }; |
| 91 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 92 | static struct ima_rule_entry default_appraise_rules[] = { |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 93 | {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 94 | {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 95 | {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 96 | {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 97 | {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 98 | {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 99 | {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 100 | {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC}, |
| 101 | {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC}, |
| 102 | {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC}, |
| 103 | {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .flags = IMA_FOWNER}, |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 104 | }; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 105 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 106 | static LIST_HEAD(ima_default_rules); |
| 107 | static LIST_HEAD(ima_policy_rules); |
| 108 | static struct list_head *ima_rules; |
| 109 | |
| 110 | static DEFINE_MUTEX(ima_rules_mutex); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 111 | |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 112 | static bool ima_use_tcb __initdata; |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 113 | static int __init default_measure_policy_setup(char *str) |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 114 | { |
| 115 | ima_use_tcb = 1; |
| 116 | return 1; |
| 117 | } |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 118 | __setup("ima_tcb", default_measure_policy_setup); |
| 119 | |
| 120 | static bool ima_use_appraise_tcb __initdata; |
| 121 | static int __init default_appraise_policy_setup(char *str) |
| 122 | { |
| 123 | ima_use_appraise_tcb = 1; |
| 124 | return 1; |
| 125 | } |
| 126 | __setup("ima_appraise_tcb", default_appraise_policy_setup); |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 127 | |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 128 | /* |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 129 | * Although the IMA policy does not change, the LSM policy can be |
| 130 | * reloaded, leaving the IMA LSM based rules referring to the old, |
| 131 | * stale LSM policy. |
| 132 | * |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 133 | * Update the IMA LSM based rules to reflect the reloaded LSM policy. |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 134 | * We assume the rules still exist; and BUG_ON() if they don't. |
| 135 | */ |
| 136 | static void ima_lsm_update_rules(void) |
| 137 | { |
| 138 | struct ima_rule_entry *entry, *tmp; |
| 139 | int result; |
| 140 | int i; |
| 141 | |
| 142 | mutex_lock(&ima_rules_mutex); |
| 143 | list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) { |
| 144 | for (i = 0; i < MAX_LSM_RULES; i++) { |
| 145 | if (!entry->lsm[i].rule) |
| 146 | continue; |
| 147 | result = security_filter_rule_init(entry->lsm[i].type, |
| 148 | Audit_equal, |
| 149 | entry->lsm[i].args_p, |
| 150 | &entry->lsm[i].rule); |
| 151 | BUG_ON(!entry->lsm[i].rule); |
| 152 | } |
| 153 | } |
| 154 | mutex_unlock(&ima_rules_mutex); |
| 155 | } |
| 156 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 157 | /** |
| 158 | * ima_match_rules - determine whether an inode matches the measure rule. |
| 159 | * @rule: a pointer to a rule |
| 160 | * @inode: a pointer to an inode |
| 161 | * @func: LIM hook identifier |
| 162 | * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC) |
| 163 | * |
| 164 | * Returns true on rule match, false on failure. |
| 165 | */ |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 166 | static bool ima_match_rules(struct ima_rule_entry *rule, |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 167 | struct inode *inode, enum ima_hooks func, int mask) |
| 168 | { |
| 169 | struct task_struct *tsk = current; |
Mimi Zohar | 3db59dd | 2012-01-17 22:11:28 -0500 | [diff] [blame] | 170 | const struct cred *cred = current_cred(); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 171 | int i; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 172 | |
Dmitry Kasatkin | 09b1148 | 2013-11-13 23:42:39 +0200 | [diff] [blame] | 173 | if ((rule->flags & IMA_FUNC) && |
| 174 | (rule->func != func && func != POST_SETATTR)) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 175 | return false; |
Dmitry Kasatkin | 09b1148 | 2013-11-13 23:42:39 +0200 | [diff] [blame] | 176 | if ((rule->flags & IMA_MASK) && |
| 177 | (rule->mask != mask && func != POST_SETATTR)) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 178 | return false; |
| 179 | if ((rule->flags & IMA_FSMAGIC) |
| 180 | && rule->fsmagic != inode->i_sb->s_magic) |
| 181 | return false; |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 182 | if ((rule->flags & IMA_FSUUID) && |
Mimi Zohar | 446d64e | 2013-02-24 23:42:37 -0500 | [diff] [blame] | 183 | memcmp(rule->fsuuid, inode->i_sb->s_uuid, sizeof(rule->fsuuid))) |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 184 | return false; |
Eric W. Biederman | 8b94eea | 2012-05-25 18:24:12 -0600 | [diff] [blame] | 185 | if ((rule->flags & IMA_UID) && !uid_eq(rule->uid, cred->uid)) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 186 | return false; |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 187 | if ((rule->flags & IMA_FOWNER) && !uid_eq(rule->fowner, inode->i_uid)) |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 188 | return false; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 189 | for (i = 0; i < MAX_LSM_RULES; i++) { |
Mimi Zohar | 53fc0e2 | 2009-05-05 13:12:48 -0400 | [diff] [blame] | 190 | int rc = 0; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 191 | u32 osid, sid; |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 192 | int retried = 0; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 193 | |
| 194 | if (!rule->lsm[i].rule) |
| 195 | continue; |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 196 | retry: |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 197 | switch (i) { |
| 198 | case LSM_OBJ_USER: |
| 199 | case LSM_OBJ_ROLE: |
| 200 | case LSM_OBJ_TYPE: |
| 201 | security_inode_getsecid(inode, &osid); |
| 202 | rc = security_filter_rule_match(osid, |
| 203 | rule->lsm[i].type, |
Mimi Zohar | 53fc0e2 | 2009-05-05 13:12:48 -0400 | [diff] [blame] | 204 | Audit_equal, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 205 | rule->lsm[i].rule, |
| 206 | NULL); |
| 207 | break; |
| 208 | case LSM_SUBJ_USER: |
| 209 | case LSM_SUBJ_ROLE: |
| 210 | case LSM_SUBJ_TYPE: |
| 211 | security_task_getsecid(tsk, &sid); |
| 212 | rc = security_filter_rule_match(sid, |
| 213 | rule->lsm[i].type, |
Mimi Zohar | 53fc0e2 | 2009-05-05 13:12:48 -0400 | [diff] [blame] | 214 | Audit_equal, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 215 | rule->lsm[i].rule, |
| 216 | NULL); |
| 217 | default: |
| 218 | break; |
| 219 | } |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 220 | if ((rc < 0) && (!retried)) { |
| 221 | retried = 1; |
| 222 | ima_lsm_update_rules(); |
| 223 | goto retry; |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 224 | } |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 225 | if (!rc) |
| 226 | return false; |
| 227 | } |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 228 | return true; |
| 229 | } |
| 230 | |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 231 | /* |
| 232 | * 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] | 233 | * we need to differentiate between calling hooks, for hook specific rules. |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 234 | */ |
Mimi Zohar | 5a73fcf | 2012-12-05 15:14:38 -0500 | [diff] [blame] | 235 | static int get_subaction(struct ima_rule_entry *rule, int func) |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 236 | { |
Mimi Zohar | 5a73fcf | 2012-12-05 15:14:38 -0500 | [diff] [blame] | 237 | if (!(rule->flags & IMA_FUNC)) |
| 238 | return IMA_FILE_APPRAISE; |
| 239 | |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 240 | switch (func) { |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 241 | case MMAP_CHECK: |
| 242 | return IMA_MMAP_APPRAISE; |
| 243 | case BPRM_CHECK: |
| 244 | return IMA_BPRM_APPRAISE; |
| 245 | case MODULE_CHECK: |
| 246 | return IMA_MODULE_APPRAISE; |
Mimi Zohar | 5a9196d | 2014-07-22 10:39:48 -0400 | [diff] [blame] | 247 | case FIRMWARE_CHECK: |
| 248 | return IMA_FIRMWARE_APPRAISE; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 249 | case FILE_CHECK: |
| 250 | default: |
| 251 | return IMA_FILE_APPRAISE; |
| 252 | } |
| 253 | } |
| 254 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 255 | /** |
| 256 | * ima_match_policy - decision based on LSM and other conditions |
| 257 | * @inode: pointer to an inode for which the policy decision is being made |
| 258 | * @func: IMA hook identifier |
| 259 | * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC) |
| 260 | * |
| 261 | * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type) |
| 262 | * conditions. |
| 263 | * |
| 264 | * (There is no need for locking when walking the policy list, |
| 265 | * as elements in the list are never deleted, nor does the list |
| 266 | * change.) |
| 267 | */ |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 268 | int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask, |
| 269 | int flags) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 270 | { |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 271 | struct ima_rule_entry *entry; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 272 | int action = 0, actmask = flags | (flags << 1); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 273 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 274 | list_for_each_entry(entry, ima_rules, list) { |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 275 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 276 | if (!(entry->action & actmask)) |
| 277 | continue; |
| 278 | |
| 279 | if (!ima_match_rules(entry, inode, func, mask)) |
| 280 | continue; |
| 281 | |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 282 | action |= entry->flags & IMA_ACTION_FLAGS; |
| 283 | |
Dmitry Kasatkin | 45e2472 | 2012-09-12 20:51:32 +0300 | [diff] [blame] | 284 | action |= entry->action & IMA_DO_MASK; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 285 | if (entry->action & IMA_APPRAISE) |
Mimi Zohar | 5a73fcf | 2012-12-05 15:14:38 -0500 | [diff] [blame] | 286 | action |= get_subaction(entry, func); |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 287 | |
Dmitry Kasatkin | 45e2472 | 2012-09-12 20:51:32 +0300 | [diff] [blame] | 288 | if (entry->action & IMA_DO_MASK) |
| 289 | actmask &= ~(entry->action | entry->action << 1); |
| 290 | else |
| 291 | actmask &= ~(entry->action | entry->action >> 1); |
| 292 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 293 | if (!actmask) |
| 294 | break; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 295 | } |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 296 | |
| 297 | return action; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 298 | } |
| 299 | |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 300 | /* |
| 301 | * Initialize the ima_policy_flag variable based on the currently |
| 302 | * loaded policy. Based on this flag, the decision to short circuit |
| 303 | * out of a function or not call the function in the first place |
| 304 | * can be made earlier. |
| 305 | */ |
| 306 | void ima_update_policy_flag(void) |
| 307 | { |
| 308 | struct ima_rule_entry *entry; |
| 309 | |
| 310 | ima_policy_flag = 0; |
| 311 | list_for_each_entry(entry, ima_rules, list) { |
| 312 | if (entry->action & IMA_DO_MASK) |
| 313 | ima_policy_flag |= entry->action; |
| 314 | } |
| 315 | |
| 316 | if (!ima_appraise) |
| 317 | ima_policy_flag &= ~IMA_APPRAISE; |
| 318 | } |
| 319 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 320 | /** |
| 321 | * ima_init_policy - initialize the default measure rules. |
| 322 | * |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 323 | * ima_rules points to either the ima_default_rules or the |
| 324 | * the new ima_policy_rules. |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 325 | */ |
Eric Paris | 932995f | 2009-05-21 15:43:32 -0400 | [diff] [blame] | 326 | void __init ima_init_policy(void) |
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 | int i, measure_entries, appraise_entries; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 329 | |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 330 | /* if !ima_use_tcb set entries = 0 so we load NO default rules */ |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 331 | measure_entries = ima_use_tcb ? ARRAY_SIZE(default_rules) : 0; |
| 332 | appraise_entries = ima_use_appraise_tcb ? |
| 333 | ARRAY_SIZE(default_appraise_rules) : 0; |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 334 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 335 | for (i = 0; i < measure_entries + appraise_entries; i++) { |
| 336 | if (i < measure_entries) |
| 337 | list_add_tail(&default_rules[i].list, |
| 338 | &ima_default_rules); |
| 339 | else { |
| 340 | int j = i - measure_entries; |
Eric Paris | 5789ba3 | 2009-05-21 15:47:06 -0400 | [diff] [blame] | 341 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 342 | list_add_tail(&default_appraise_rules[j].list, |
| 343 | &ima_default_rules); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | ima_rules = &ima_default_rules; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 348 | } |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 349 | |
| 350 | /** |
| 351 | * ima_update_policy - update default_rules with new measure rules |
| 352 | * |
| 353 | * Called on file .release to update the default rules with a complete new |
| 354 | * policy. Once updated, the policy is locked, no additional rules can be |
| 355 | * added to the policy. |
| 356 | */ |
| 357 | void ima_update_policy(void) |
| 358 | { |
Mimi Zohar | 52a1328 | 2013-12-11 14:44:04 -0500 | [diff] [blame] | 359 | static const char op[] = "policy_update"; |
Richard Guy Briggs | 7e9001f | 2014-06-16 15:52:07 -0400 | [diff] [blame] | 360 | const char *cause = "already-exists"; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 361 | int result = 1; |
| 362 | int audit_info = 0; |
| 363 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 364 | if (ima_rules == &ima_default_rules) { |
| 365 | ima_rules = &ima_policy_rules; |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 366 | ima_update_policy_flag(); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 367 | cause = "complete"; |
| 368 | result = 0; |
| 369 | } |
| 370 | integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, |
| 371 | NULL, op, cause, result, audit_info); |
| 372 | } |
| 373 | |
| 374 | enum { |
| 375 | Opt_err = -1, |
| 376 | Opt_measure = 1, Opt_dont_measure, |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 377 | Opt_appraise, Opt_dont_appraise, |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 378 | Opt_audit, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 379 | Opt_obj_user, Opt_obj_role, Opt_obj_type, |
| 380 | Opt_subj_user, Opt_subj_role, Opt_subj_type, |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 381 | Opt_func, Opt_mask, Opt_fsmagic, Opt_uid, Opt_fowner, |
Mimi Zohar | f9b2a73 | 2014-05-12 09:28:11 -0400 | [diff] [blame] | 382 | Opt_appraise_type, Opt_fsuuid, Opt_permit_directio |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 383 | }; |
| 384 | |
| 385 | static match_table_t policy_tokens = { |
| 386 | {Opt_measure, "measure"}, |
| 387 | {Opt_dont_measure, "dont_measure"}, |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 388 | {Opt_appraise, "appraise"}, |
| 389 | {Opt_dont_appraise, "dont_appraise"}, |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 390 | {Opt_audit, "audit"}, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 391 | {Opt_obj_user, "obj_user=%s"}, |
| 392 | {Opt_obj_role, "obj_role=%s"}, |
| 393 | {Opt_obj_type, "obj_type=%s"}, |
| 394 | {Opt_subj_user, "subj_user=%s"}, |
| 395 | {Opt_subj_role, "subj_role=%s"}, |
| 396 | {Opt_subj_type, "subj_type=%s"}, |
| 397 | {Opt_func, "func=%s"}, |
| 398 | {Opt_mask, "mask=%s"}, |
| 399 | {Opt_fsmagic, "fsmagic=%s"}, |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 400 | {Opt_fsuuid, "fsuuid=%s"}, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 401 | {Opt_uid, "uid=%s"}, |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 402 | {Opt_fowner, "fowner=%s"}, |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 403 | {Opt_appraise_type, "appraise_type=%s"}, |
Mimi Zohar | f9b2a73 | 2014-05-12 09:28:11 -0400 | [diff] [blame] | 404 | {Opt_permit_directio, "permit_directio"}, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 405 | {Opt_err, NULL} |
| 406 | }; |
| 407 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 408 | static int ima_lsm_rule_init(struct ima_rule_entry *entry, |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 409 | substring_t *args, int lsm_rule, int audit_type) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 410 | { |
| 411 | int result; |
| 412 | |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 413 | if (entry->lsm[lsm_rule].rule) |
| 414 | return -EINVAL; |
| 415 | |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 416 | entry->lsm[lsm_rule].args_p = match_strdup(args); |
| 417 | if (!entry->lsm[lsm_rule].args_p) |
| 418 | return -ENOMEM; |
| 419 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 420 | entry->lsm[lsm_rule].type = audit_type; |
| 421 | result = security_filter_rule_init(entry->lsm[lsm_rule].type, |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 422 | Audit_equal, |
| 423 | entry->lsm[lsm_rule].args_p, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 424 | &entry->lsm[lsm_rule].rule); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 425 | if (!entry->lsm[lsm_rule].rule) { |
| 426 | kfree(entry->lsm[lsm_rule].args_p); |
Mimi Zohar | 867c202 | 2011-01-03 14:59:10 -0800 | [diff] [blame] | 427 | return -EINVAL; |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 428 | } |
| 429 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 430 | return result; |
| 431 | } |
| 432 | |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 433 | static void ima_log_string(struct audit_buffer *ab, char *key, char *value) |
| 434 | { |
| 435 | audit_log_format(ab, "%s=", key); |
| 436 | audit_log_untrustedstring(ab, value); |
| 437 | audit_log_format(ab, " "); |
| 438 | } |
| 439 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 440 | static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 441 | { |
| 442 | struct audit_buffer *ab; |
| 443 | char *p; |
| 444 | int result = 0; |
| 445 | |
Mimi Zohar | 523979a | 2009-02-11 11:12:28 -0500 | [diff] [blame] | 446 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 447 | |
Eric W. Biederman | 8b94eea | 2012-05-25 18:24:12 -0600 | [diff] [blame] | 448 | entry->uid = INVALID_UID; |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 449 | entry->fowner = INVALID_UID; |
Eric Paris | b9035b1 | 2010-04-20 10:21:07 -0400 | [diff] [blame] | 450 | entry->action = UNKNOWN; |
Eric Paris | 28ef400 | 2010-04-20 10:21:18 -0400 | [diff] [blame] | 451 | while ((p = strsep(&rule, " \t")) != NULL) { |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 452 | substring_t args[MAX_OPT_ARGS]; |
| 453 | int token; |
| 454 | unsigned long lnum; |
| 455 | |
| 456 | if (result < 0) |
| 457 | break; |
Eric Paris | 28ef400 | 2010-04-20 10:21:18 -0400 | [diff] [blame] | 458 | if ((*p == '\0') || (*p == ' ') || (*p == '\t')) |
| 459 | continue; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 460 | token = match_token(p, policy_tokens, args); |
| 461 | switch (token) { |
| 462 | case Opt_measure: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 463 | ima_log_string(ab, "action", "measure"); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 464 | |
| 465 | if (entry->action != UNKNOWN) |
| 466 | result = -EINVAL; |
| 467 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 468 | entry->action = MEASURE; |
| 469 | break; |
| 470 | case Opt_dont_measure: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 471 | ima_log_string(ab, "action", "dont_measure"); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 472 | |
| 473 | if (entry->action != UNKNOWN) |
| 474 | result = -EINVAL; |
| 475 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 476 | entry->action = DONT_MEASURE; |
| 477 | break; |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 478 | case Opt_appraise: |
| 479 | ima_log_string(ab, "action", "appraise"); |
| 480 | |
| 481 | if (entry->action != UNKNOWN) |
| 482 | result = -EINVAL; |
| 483 | |
| 484 | entry->action = APPRAISE; |
| 485 | break; |
| 486 | case Opt_dont_appraise: |
| 487 | ima_log_string(ab, "action", "dont_appraise"); |
| 488 | |
| 489 | if (entry->action != UNKNOWN) |
| 490 | result = -EINVAL; |
| 491 | |
| 492 | entry->action = DONT_APPRAISE; |
| 493 | break; |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 494 | case Opt_audit: |
| 495 | ima_log_string(ab, "action", "audit"); |
| 496 | |
| 497 | if (entry->action != UNKNOWN) |
| 498 | result = -EINVAL; |
| 499 | |
| 500 | entry->action = AUDIT; |
| 501 | break; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 502 | case Opt_func: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 503 | ima_log_string(ab, "func", args[0].from); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 504 | |
| 505 | if (entry->func) |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 506 | result = -EINVAL; |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 507 | |
Mimi Zohar | 1e93d00 | 2010-01-26 17:02:41 -0500 | [diff] [blame] | 508 | if (strcmp(args[0].from, "FILE_CHECK") == 0) |
| 509 | entry->func = FILE_CHECK; |
| 510 | /* PATH_CHECK is for backwards compat */ |
| 511 | else if (strcmp(args[0].from, "PATH_CHECK") == 0) |
| 512 | entry->func = FILE_CHECK; |
Mimi Zohar | fdf9072 | 2012-10-16 12:40:08 +1030 | [diff] [blame] | 513 | else if (strcmp(args[0].from, "MODULE_CHECK") == 0) |
| 514 | entry->func = MODULE_CHECK; |
Mimi Zohar | 5a9196d | 2014-07-22 10:39:48 -0400 | [diff] [blame] | 515 | else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0) |
| 516 | entry->func = FIRMWARE_CHECK; |
Mimi Zohar | 16cac49 | 2012-12-13 11:15:04 -0500 | [diff] [blame] | 517 | else if ((strcmp(args[0].from, "FILE_MMAP") == 0) |
| 518 | || (strcmp(args[0].from, "MMAP_CHECK") == 0)) |
| 519 | entry->func = MMAP_CHECK; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 520 | else if (strcmp(args[0].from, "BPRM_CHECK") == 0) |
| 521 | entry->func = BPRM_CHECK; |
| 522 | else |
| 523 | result = -EINVAL; |
| 524 | if (!result) |
| 525 | entry->flags |= IMA_FUNC; |
| 526 | break; |
| 527 | case Opt_mask: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 528 | ima_log_string(ab, "mask", args[0].from); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 529 | |
| 530 | if (entry->mask) |
| 531 | result = -EINVAL; |
| 532 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 533 | if ((strcmp(args[0].from, "MAY_EXEC")) == 0) |
| 534 | entry->mask = MAY_EXEC; |
| 535 | else if (strcmp(args[0].from, "MAY_WRITE") == 0) |
| 536 | entry->mask = MAY_WRITE; |
| 537 | else if (strcmp(args[0].from, "MAY_READ") == 0) |
| 538 | entry->mask = MAY_READ; |
| 539 | else if (strcmp(args[0].from, "MAY_APPEND") == 0) |
| 540 | entry->mask = MAY_APPEND; |
| 541 | else |
| 542 | result = -EINVAL; |
| 543 | if (!result) |
| 544 | entry->flags |= IMA_MASK; |
| 545 | break; |
| 546 | case Opt_fsmagic: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 547 | ima_log_string(ab, "fsmagic", args[0].from); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 548 | |
| 549 | if (entry->fsmagic) { |
| 550 | result = -EINVAL; |
| 551 | break; |
| 552 | } |
| 553 | |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 554 | result = kstrtoul(args[0].from, 16, &entry->fsmagic); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 555 | if (!result) |
| 556 | entry->flags |= IMA_FSMAGIC; |
| 557 | break; |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 558 | case Opt_fsuuid: |
| 559 | ima_log_string(ab, "fsuuid", args[0].from); |
| 560 | |
| 561 | if (memchr_inv(entry->fsuuid, 0x00, |
Mimi Zohar | 446d64e | 2013-02-24 23:42:37 -0500 | [diff] [blame] | 562 | sizeof(entry->fsuuid))) { |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 563 | result = -EINVAL; |
| 564 | break; |
| 565 | } |
| 566 | |
Mimi Zohar | 446d64e | 2013-02-24 23:42:37 -0500 | [diff] [blame] | 567 | result = blk_part_pack_uuid(args[0].from, |
| 568 | entry->fsuuid); |
| 569 | if (!result) |
| 570 | entry->flags |= IMA_FSUUID; |
Dmitry Kasatkin | 85865c1 | 2012-09-03 23:23:13 +0300 | [diff] [blame] | 571 | break; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 572 | case Opt_uid: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 573 | ima_log_string(ab, "uid", args[0].from); |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 574 | |
Eric W. Biederman | 8b94eea | 2012-05-25 18:24:12 -0600 | [diff] [blame] | 575 | if (uid_valid(entry->uid)) { |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 576 | result = -EINVAL; |
| 577 | break; |
| 578 | } |
| 579 | |
Jingoo Han | 29707b2 | 2014-02-05 15:13:14 +0900 | [diff] [blame] | 580 | result = kstrtoul(args[0].from, 10, &lnum); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 581 | if (!result) { |
Eric W. Biederman | 8b94eea | 2012-05-25 18:24:12 -0600 | [diff] [blame] | 582 | entry->uid = make_kuid(current_user_ns(), (uid_t)lnum); |
| 583 | if (!uid_valid(entry->uid) || (((uid_t)lnum) != lnum)) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 584 | result = -EINVAL; |
| 585 | else |
| 586 | entry->flags |= IMA_UID; |
| 587 | } |
| 588 | break; |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 589 | case Opt_fowner: |
| 590 | ima_log_string(ab, "fowner", args[0].from); |
| 591 | |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 592 | if (uid_valid(entry->fowner)) { |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 593 | result = -EINVAL; |
| 594 | break; |
| 595 | } |
| 596 | |
Jingoo Han | 29707b2 | 2014-02-05 15:13:14 +0900 | [diff] [blame] | 597 | result = kstrtoul(args[0].from, 10, &lnum); |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 598 | if (!result) { |
Linus Torvalds | 88265322 | 2012-10-02 21:38:48 -0700 | [diff] [blame] | 599 | entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum); |
| 600 | if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum)) |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 601 | result = -EINVAL; |
| 602 | else |
| 603 | entry->flags |= IMA_FOWNER; |
| 604 | } |
| 605 | break; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 606 | case Opt_obj_user: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 607 | ima_log_string(ab, "obj_user", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 608 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 609 | LSM_OBJ_USER, |
| 610 | AUDIT_OBJ_USER); |
| 611 | break; |
| 612 | case Opt_obj_role: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 613 | ima_log_string(ab, "obj_role", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 614 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 615 | LSM_OBJ_ROLE, |
| 616 | AUDIT_OBJ_ROLE); |
| 617 | break; |
| 618 | case Opt_obj_type: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 619 | ima_log_string(ab, "obj_type", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 620 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 621 | LSM_OBJ_TYPE, |
| 622 | AUDIT_OBJ_TYPE); |
| 623 | break; |
| 624 | case Opt_subj_user: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 625 | ima_log_string(ab, "subj_user", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 626 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 627 | LSM_SUBJ_USER, |
| 628 | AUDIT_SUBJ_USER); |
| 629 | break; |
| 630 | case Opt_subj_role: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 631 | ima_log_string(ab, "subj_role", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 632 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 633 | LSM_SUBJ_ROLE, |
| 634 | AUDIT_SUBJ_ROLE); |
| 635 | break; |
| 636 | case Opt_subj_type: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 637 | ima_log_string(ab, "subj_type", args[0].from); |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 638 | result = ima_lsm_rule_init(entry, args, |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 639 | LSM_SUBJ_TYPE, |
| 640 | AUDIT_SUBJ_TYPE); |
| 641 | break; |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 642 | case Opt_appraise_type: |
| 643 | if (entry->action != APPRAISE) { |
| 644 | result = -EINVAL; |
| 645 | break; |
| 646 | } |
| 647 | |
| 648 | ima_log_string(ab, "appraise_type", args[0].from); |
| 649 | if ((strcmp(args[0].from, "imasig")) == 0) |
| 650 | entry->flags |= IMA_DIGSIG_REQUIRED; |
| 651 | else |
| 652 | result = -EINVAL; |
| 653 | break; |
Mimi Zohar | f9b2a73 | 2014-05-12 09:28:11 -0400 | [diff] [blame] | 654 | case Opt_permit_directio: |
| 655 | entry->flags |= IMA_PERMIT_DIRECTIO; |
| 656 | break; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 657 | case Opt_err: |
Eric Paris | 2f1506c | 2010-04-20 10:21:30 -0400 | [diff] [blame] | 658 | ima_log_string(ab, "UNKNOWN", p); |
Eric Paris | e9d393b | 2010-04-20 10:21:13 -0400 | [diff] [blame] | 659 | result = -EINVAL; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 660 | break; |
| 661 | } |
| 662 | } |
Eric Paris | 7b62e16 | 2010-04-20 10:21:01 -0400 | [diff] [blame] | 663 | if (!result && (entry->action == UNKNOWN)) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 664 | result = -EINVAL; |
Mimi Zohar | a7f2a36 | 2012-12-21 08:34:21 -0500 | [diff] [blame] | 665 | else if (entry->func == MODULE_CHECK) |
| 666 | ima_appraise |= IMA_APPRAISE_MODULES; |
Mimi Zohar | 5a9196d | 2014-07-22 10:39:48 -0400 | [diff] [blame] | 667 | else if (entry->func == FIRMWARE_CHECK) |
| 668 | ima_appraise |= IMA_APPRAISE_FIRMWARE; |
Eric Paris | b0d5de4 | 2012-02-14 17:11:07 -0500 | [diff] [blame] | 669 | audit_log_format(ab, "res=%d", !result); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 670 | audit_log_end(ab); |
| 671 | return result; |
| 672 | } |
| 673 | |
| 674 | /** |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 675 | * ima_parse_add_rule - add a rule to ima_policy_rules |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 676 | * @rule - ima measurement policy rule |
| 677 | * |
| 678 | * Uses a mutex to protect the policy list from multiple concurrent writers. |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 679 | * Returns the length of the rule parsed, an error code on failure |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 680 | */ |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 681 | ssize_t ima_parse_add_rule(char *rule) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 682 | { |
Mimi Zohar | 52a1328 | 2013-12-11 14:44:04 -0500 | [diff] [blame] | 683 | static const char op[] = "update_policy"; |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 684 | char *p; |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 685 | struct ima_rule_entry *entry; |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 686 | ssize_t result, len; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 687 | int audit_info = 0; |
| 688 | |
| 689 | /* Prevent installed policy from changing */ |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 690 | if (ima_rules != &ima_default_rules) { |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 691 | integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, |
Richard Guy Briggs | 7e9001f | 2014-06-16 15:52:07 -0400 | [diff] [blame] | 692 | NULL, op, "already-exists", |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 693 | -EACCES, audit_info); |
| 694 | return -EACCES; |
| 695 | } |
| 696 | |
Dmitry Kasatkin | 272a6e9 | 2014-10-03 14:40:19 +0300 | [diff] [blame] | 697 | p = strsep(&rule, "\n"); |
| 698 | len = strlen(p) + 1; |
Dmitry Kasatkin | 7178784 | 2014-10-03 14:40:20 +0300 | [diff] [blame^] | 699 | p += strspn(p, " \t"); |
Dmitry Kasatkin | 272a6e9 | 2014-10-03 14:40:19 +0300 | [diff] [blame] | 700 | |
Dmitry Kasatkin | 7178784 | 2014-10-03 14:40:20 +0300 | [diff] [blame^] | 701 | if (*p == '#' || *p == '\0') |
Dmitry Kasatkin | 272a6e9 | 2014-10-03 14:40:19 +0300 | [diff] [blame] | 702 | return len; |
| 703 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 704 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 705 | if (!entry) { |
| 706 | integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, |
| 707 | NULL, op, "-ENOMEM", -ENOMEM, audit_info); |
| 708 | return -ENOMEM; |
| 709 | } |
| 710 | |
| 711 | INIT_LIST_HEAD(&entry->list); |
| 712 | |
Eric Paris | 6ccd045 | 2010-04-20 10:20:54 -0400 | [diff] [blame] | 713 | result = ima_parse_rule(p, entry); |
Eric Paris | 7233e3e | 2010-04-20 10:21:24 -0400 | [diff] [blame] | 714 | if (result) { |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 715 | kfree(entry); |
Mimi Zohar | 523979a | 2009-02-11 11:12:28 -0500 | [diff] [blame] | 716 | integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, |
Richard Guy Briggs | 7e9001f | 2014-06-16 15:52:07 -0400 | [diff] [blame] | 717 | NULL, op, "invalid-policy", result, |
Mimi Zohar | 523979a | 2009-02-11 11:12:28 -0500 | [diff] [blame] | 718 | audit_info); |
Eric Paris | 7233e3e | 2010-04-20 10:21:24 -0400 | [diff] [blame] | 719 | return result; |
Mimi Zohar | 523979a | 2009-02-11 11:12:28 -0500 | [diff] [blame] | 720 | } |
Eric Paris | 7233e3e | 2010-04-20 10:21:24 -0400 | [diff] [blame] | 721 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 722 | mutex_lock(&ima_rules_mutex); |
| 723 | list_add_tail(&entry->list, &ima_policy_rules); |
| 724 | mutex_unlock(&ima_rules_mutex); |
Eric Paris | 7233e3e | 2010-04-20 10:21:24 -0400 | [diff] [blame] | 725 | |
| 726 | return len; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | /* ima_delete_rules called to cleanup invalid policy */ |
James Morris | 64c61d8 | 2009-02-05 09:28:26 +1100 | [diff] [blame] | 730 | void ima_delete_rules(void) |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 731 | { |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 732 | struct ima_rule_entry *entry, *tmp; |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 733 | int i; |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 734 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 735 | mutex_lock(&ima_rules_mutex); |
| 736 | list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) { |
Mimi Zohar | 7163a99 | 2013-01-03 14:19:09 -0500 | [diff] [blame] | 737 | for (i = 0; i < MAX_LSM_RULES; i++) |
| 738 | kfree(entry->lsm[i].args_p); |
| 739 | |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 740 | list_del(&entry->list); |
| 741 | kfree(entry); |
| 742 | } |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 743 | mutex_unlock(&ima_rules_mutex); |
Mimi Zohar | 4af4662 | 2009-02-04 09:07:00 -0500 | [diff] [blame] | 744 | } |