blob: 40557c06300ff642e717c75134458262e362708a [file] [log] [blame]
Mimi Zohar3323eec2009-02-04 09:06:58 -05001/*
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 Kasatkin2bb930a2014-03-04 18:04:20 +020010 * - initialize default measure policy rules
Mimi Zohar3323eec2009-02-04 09:06:58 -050011 *
12 */
13#include <linux/module.h>
14#include <linux/list.h>
Mimi Zoharcf222212016-01-14 17:57:47 -050015#include <linux/fs.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050016#include <linux/security.h>
17#include <linux/magic.h>
Mimi Zohar4af46622009-02-04 09:07:00 -050018#include <linux/parser.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Petko Manolov38d859f2015-12-02 17:47:54 +020020#include <linux/rculist.h>
Dmitry Kasatkin85865c12012-09-03 23:23:13 +030021#include <linux/genhd.h>
Petko Manolov80eae202015-12-02 17:47:56 +020022#include <linux/seq_file.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050023
24#include "ima.h"
25
26/* flags definitions */
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020027#define IMA_FUNC 0x0001
28#define IMA_MASK 0x0002
Mimi Zohar3323eec2009-02-04 09:06:58 -050029#define IMA_FSMAGIC 0x0004
30#define IMA_UID 0x0008
Mimi Zohar07f6a792011-03-09 22:25:48 -050031#define IMA_FOWNER 0x0010
Dmitry Kasatkin85865c12012-09-03 23:23:13 +030032#define IMA_FSUUID 0x0020
Mimi Zohar4351c292014-11-05 07:53:55 -050033#define IMA_INMASK 0x0040
Mimi Zohar139069e2014-11-05 07:48:36 -050034#define IMA_EUID 0x0080
Eric Richter02606432016-06-01 13:14:01 -050035#define IMA_PCR 0x0100
Mimi Zohar3323eec2009-02-04 09:06:58 -050036
Dmitry Kasatkin45e24722012-09-12 20:51:32 +030037#define UNKNOWN 0
38#define MEASURE 0x0001 /* same as IMA_MEASURE */
39#define DONT_MEASURE 0x0002
40#define APPRAISE 0x0004 /* same as IMA_APPRAISE */
41#define DONT_APPRAISE 0x0008
Peter Moodye7c568e2012-06-14 10:04:36 -070042#define AUDIT 0x0040
Mimi Zoharda1b0022016-09-29 10:04:52 -040043#define HASH 0x0100
44#define DONT_HASH 0x0200
Mimi Zohar4af46622009-02-04 09:07:00 -050045
Eric Richter02606432016-06-01 13:14:01 -050046#define INVALID_PCR(a) (((a) < 0) || \
47 (a) >= (FIELD_SIZEOF(struct integrity_iint_cache, measured_pcrs) * 8))
48
Roberto Sassua7560242014-09-12 19:35:54 +020049int ima_policy_flag;
Mimi Zohar6ad6afa2015-12-07 14:35:47 -050050static int temp_ima_appraise;
Roberto Sassua7560242014-09-12 19:35:54 +020051
Mimi Zohar4af46622009-02-04 09:07:00 -050052#define MAX_LSM_RULES 6
53enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
54 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
55};
Mimi Zohar3323eec2009-02-04 09:06:58 -050056
Mimi Zohar24fd03c2015-06-11 20:48:33 -040057enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB };
58
Mimi Zohar07f6a792011-03-09 22:25:48 -050059struct ima_rule_entry {
Mimi Zohar3323eec2009-02-04 09:06:58 -050060 struct list_head list;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050061 int action;
Mimi Zohar3323eec2009-02-04 09:06:58 -050062 unsigned int flags;
63 enum ima_hooks func;
64 int mask;
65 unsigned long fsmagic;
Christoph Hellwig787d8c52017-06-01 07:00:26 +020066 uuid_t fsuuid;
Eric W. Biederman8b94eea2012-05-25 18:24:12 -060067 kuid_t uid;
Linus Torvalds882653222012-10-02 21:38:48 -070068 kuid_t fowner;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +030069 bool (*uid_op)(kuid_t, kuid_t); /* Handlers for operators */
70 bool (*fowner_op)(kuid_t, kuid_t); /* uid_eq(), uid_gt(), uid_lt() */
Eric Richter02606432016-06-01 13:14:01 -050071 int pcr;
Mimi Zohar4af46622009-02-04 09:07:00 -050072 struct {
73 void *rule; /* LSM file metadata specific */
Mimi Zohar7163a992013-01-03 14:19:09 -050074 void *args_p; /* audit value */
Mimi Zohar4af46622009-02-04 09:07:00 -050075 int type; /* audit type */
76 } lsm[MAX_LSM_RULES];
Mimi Zohar3323eec2009-02-04 09:06:58 -050077};
78
Eric Paris5789ba32009-05-21 15:47:06 -040079/*
80 * Without LSM specific knowledge, the default policy can only be
Mimi Zohar07f6a792011-03-09 22:25:48 -050081 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
Mimi Zohar4af46622009-02-04 09:07:00 -050082 */
Eric Paris5789ba32009-05-21 15:47:06 -040083
84/*
85 * The minimum rule set to allow for full TCB coverage. Measures all files
86 * opened or mmap for exec and everything read by root. Dangerous because
87 * normal users can easily run the machine out of memory simply building
88 * and running executables.
89 */
James Morrisbad44172017-02-13 16:34:35 +110090static struct ima_rule_entry dont_measure_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020091 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
92 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
93 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
94 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
95 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
96 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
97 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
98 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
Roberto Sassu6438de92015-04-11 17:13:06 +020099 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
100 .flags = IMA_FSMAGIC},
Laura Abbott82e3bb42017-05-09 11:25:27 -0700101 {.action = DONT_MEASURE, .fsmagic = CGROUP2_SUPER_MAGIC,
102 .flags = IMA_FSMAGIC},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400103 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC}
104};
105
James Morrisbad44172017-02-13 16:34:35 +1100106static struct ima_rule_entry original_measurement_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200107 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500108 .flags = IMA_FUNC | IMA_MASK},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200109 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500110 .flags = IMA_FUNC | IMA_MASK},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400111 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300112 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
113 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400114 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
115 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
116};
117
James Morrisbad44172017-02-13 16:34:35 +1100118static struct ima_rule_entry default_measurement_rules[] __ro_after_init = {
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400119 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
120 .flags = IMA_FUNC | IMA_MASK},
121 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
122 .flags = IMA_FUNC | IMA_MASK},
123 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300124 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
125 .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400126 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300127 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
128 .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200129 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400130 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
Mimi Zohar19f8a842016-01-15 10:17:12 -0500131 {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
Mimi Zohar3323eec2009-02-04 09:06:58 -0500132};
133
James Morrisbad44172017-02-13 16:34:35 +1100134static struct ima_rule_entry default_appraise_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200135 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
136 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
137 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
138 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
139 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
140 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
141 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
142 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
143 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zoharcd025f72015-04-21 16:54:24 -0400144 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200145 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
Laura Abbott82e3bb42017-05-09 11:25:27 -0700146 {.action = DONT_APPRAISE, .fsmagic = CGROUP2_SUPER_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zohar95ee08f2015-12-07 15:08:01 -0500147#ifdef CONFIG_IMA_WRITE_POLICY
148 {.action = APPRAISE, .func = POLICY_CHECK,
149 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
150#endif
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200151#ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300152 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
153 .flags = IMA_FOWNER},
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200154#else
155 /* force signature */
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300156 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200157 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
158#endif
Mimi Zohar07f6a792011-03-09 22:25:48 -0500159};
Mimi Zohar3323eec2009-02-04 09:06:58 -0500160
Mimi Zohar503ceae2017-04-21 18:58:27 -0400161static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
162 {.action = APPRAISE, .func = MODULE_CHECK,
163 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
164 {.action = APPRAISE, .func = FIRMWARE_CHECK,
165 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
166 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
167 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
168 {.action = APPRAISE, .func = POLICY_CHECK,
169 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
170};
171
Mimi Zohar07f6a792011-03-09 22:25:48 -0500172static LIST_HEAD(ima_default_rules);
173static LIST_HEAD(ima_policy_rules);
Petko Manolov38d859f2015-12-02 17:47:54 +0200174static LIST_HEAD(ima_temp_rules);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500175static struct list_head *ima_rules;
176
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400177static int ima_policy __initdata;
Petko Manolov38d859f2015-12-02 17:47:54 +0200178
Mimi Zohar07f6a792011-03-09 22:25:48 -0500179static int __init default_measure_policy_setup(char *str)
Eric Paris5789ba32009-05-21 15:47:06 -0400180{
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400181 if (ima_policy)
182 return 1;
183
184 ima_policy = ORIGINAL_TCB;
Eric Paris5789ba32009-05-21 15:47:06 -0400185 return 1;
186}
Mimi Zohar07f6a792011-03-09 22:25:48 -0500187__setup("ima_tcb", default_measure_policy_setup);
188
Mimi Zohar33ce9542017-04-24 12:04:09 -0400189static bool ima_use_appraise_tcb __initdata;
Mimi Zohar503ceae2017-04-21 18:58:27 -0400190static bool ima_use_secure_boot __initdata;
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400191static int __init policy_setup(char *str)
192{
Mimi Zohar33ce9542017-04-24 12:04:09 -0400193 char *p;
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400194
Mimi Zohar33ce9542017-04-24 12:04:09 -0400195 while ((p = strsep(&str, " |\n")) != NULL) {
196 if (*p == ' ')
197 continue;
198 if ((strcmp(p, "tcb") == 0) && !ima_policy)
199 ima_policy = DEFAULT_TCB;
200 else if (strcmp(p, "appraise_tcb") == 0)
Thomas Meyer39adb922017-10-07 16:02:21 +0200201 ima_use_appraise_tcb = true;
Mimi Zohar503ceae2017-04-21 18:58:27 -0400202 else if (strcmp(p, "secure_boot") == 0)
Thomas Meyer39adb922017-10-07 16:02:21 +0200203 ima_use_secure_boot = true;
Mimi Zohar33ce9542017-04-24 12:04:09 -0400204 }
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400205
206 return 1;
207}
208__setup("ima_policy=", policy_setup);
209
Mimi Zohar07f6a792011-03-09 22:25:48 -0500210static int __init default_appraise_policy_setup(char *str)
211{
Thomas Meyer39adb922017-10-07 16:02:21 +0200212 ima_use_appraise_tcb = true;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500213 return 1;
214}
215__setup("ima_appraise_tcb", default_appraise_policy_setup);
Eric Paris5789ba32009-05-21 15:47:06 -0400216
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200217/*
Petko Manolov38d859f2015-12-02 17:47:54 +0200218 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
219 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
220 * the reloaded LSM policy. We assume the rules still exist; and BUG_ON() if
221 * they don't.
Mimi Zohar7163a992013-01-03 14:19:09 -0500222 */
223static void ima_lsm_update_rules(void)
224{
Petko Manolov38d859f2015-12-02 17:47:54 +0200225 struct ima_rule_entry *entry;
Mimi Zohar7163a992013-01-03 14:19:09 -0500226 int result;
227 int i;
228
Petko Manolov38d859f2015-12-02 17:47:54 +0200229 list_for_each_entry(entry, &ima_policy_rules, list) {
Mimi Zohar7163a992013-01-03 14:19:09 -0500230 for (i = 0; i < MAX_LSM_RULES; i++) {
231 if (!entry->lsm[i].rule)
232 continue;
233 result = security_filter_rule_init(entry->lsm[i].type,
234 Audit_equal,
235 entry->lsm[i].args_p,
236 &entry->lsm[i].rule);
237 BUG_ON(!entry->lsm[i].rule);
238 }
239 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500240}
241
Mimi Zohar3323eec2009-02-04 09:06:58 -0500242/**
243 * ima_match_rules - determine whether an inode matches the measure rule.
244 * @rule: a pointer to a rule
245 * @inode: a pointer to an inode
Matthew Garrettd906c102018-01-08 13:36:20 -0800246 * @cred: a pointer to a credentials structure for user validation
247 * @secid: the secid of the task to be validated
Mimi Zohar3323eec2009-02-04 09:06:58 -0500248 * @func: LIM hook identifier
249 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
250 *
251 * Returns true on rule match, false on failure.
252 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500253static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
Matthew Garrettd906c102018-01-08 13:36:20 -0800254 const struct cred *cred, u32 secid,
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500255 enum ima_hooks func, int mask)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500256{
Mimi Zohar4af46622009-02-04 09:07:00 -0500257 int i;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500258
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200259 if ((rule->flags & IMA_FUNC) &&
260 (rule->func != func && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500261 return false;
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200262 if ((rule->flags & IMA_MASK) &&
263 (rule->mask != mask && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500264 return false;
Mimi Zohar4351c292014-11-05 07:53:55 -0500265 if ((rule->flags & IMA_INMASK) &&
266 (!(rule->mask & mask) && func != POST_SETATTR))
267 return false;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500268 if ((rule->flags & IMA_FSMAGIC)
269 && rule->fsmagic != inode->i_sb->s_magic)
270 return false;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300271 if ((rule->flags & IMA_FSUUID) &&
Christoph Hellwig85787092017-05-10 15:06:33 +0200272 !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid))
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300273 return false;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300274 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500275 return false;
Mimi Zohar139069e2014-11-05 07:48:36 -0500276 if (rule->flags & IMA_EUID) {
277 if (has_capability_noaudit(current, CAP_SETUID)) {
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300278 if (!rule->uid_op(cred->euid, rule->uid)
279 && !rule->uid_op(cred->suid, rule->uid)
280 && !rule->uid_op(cred->uid, rule->uid))
Mimi Zohar139069e2014-11-05 07:48:36 -0500281 return false;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300282 } else if (!rule->uid_op(cred->euid, rule->uid))
Mimi Zohar139069e2014-11-05 07:48:36 -0500283 return false;
284 }
285
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300286 if ((rule->flags & IMA_FOWNER) &&
287 !rule->fowner_op(inode->i_uid, rule->fowner))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500288 return false;
Mimi Zohar4af46622009-02-04 09:07:00 -0500289 for (i = 0; i < MAX_LSM_RULES; i++) {
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400290 int rc = 0;
Matthew Garrettd906c102018-01-08 13:36:20 -0800291 u32 osid;
Mimi Zohar7163a992013-01-03 14:19:09 -0500292 int retried = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500293
294 if (!rule->lsm[i].rule)
295 continue;
Mimi Zohar7163a992013-01-03 14:19:09 -0500296retry:
Mimi Zohar4af46622009-02-04 09:07:00 -0500297 switch (i) {
298 case LSM_OBJ_USER:
299 case LSM_OBJ_ROLE:
300 case LSM_OBJ_TYPE:
301 security_inode_getsecid(inode, &osid);
302 rc = security_filter_rule_match(osid,
303 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400304 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500305 rule->lsm[i].rule,
306 NULL);
307 break;
308 case LSM_SUBJ_USER:
309 case LSM_SUBJ_ROLE:
310 case LSM_SUBJ_TYPE:
Matthew Garrettd906c102018-01-08 13:36:20 -0800311 rc = security_filter_rule_match(secid,
Mimi Zohar4af46622009-02-04 09:07:00 -0500312 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400313 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500314 rule->lsm[i].rule,
315 NULL);
316 default:
317 break;
318 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500319 if ((rc < 0) && (!retried)) {
320 retried = 1;
321 ima_lsm_update_rules();
322 goto retry;
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200323 }
Mimi Zohar4af46622009-02-04 09:07:00 -0500324 if (!rc)
325 return false;
326 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500327 return true;
328}
329
Mimi Zohard79d72e2012-12-03 17:08:11 -0500330/*
331 * In addition to knowing that we need to appraise the file in general,
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500332 * we need to differentiate between calling hooks, for hook specific rules.
Mimi Zohard79d72e2012-12-03 17:08:11 -0500333 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500334static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500335{
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500336 if (!(rule->flags & IMA_FUNC))
337 return IMA_FILE_APPRAISE;
338
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200339 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500340 case MMAP_CHECK:
341 return IMA_MMAP_APPRAISE;
342 case BPRM_CHECK:
343 return IMA_BPRM_APPRAISE;
Matthew Garrettd906c102018-01-08 13:36:20 -0800344 case CREDS_CHECK:
345 return IMA_CREDS_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500346 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500347 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -0500348 return IMA_FILE_APPRAISE;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500349 case MODULE_CHECK ... MAX_CHECK - 1:
350 default:
351 return IMA_READ_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500352 }
353}
354
Mimi Zohar3323eec2009-02-04 09:06:58 -0500355/**
356 * ima_match_policy - decision based on LSM and other conditions
357 * @inode: pointer to an inode for which the policy decision is being made
Matthew Garrettd906c102018-01-08 13:36:20 -0800358 * @cred: pointer to a credentials structure for which the policy decision is
359 * being made
360 * @secid: LSM secid of the task to be validated
Mimi Zohar3323eec2009-02-04 09:06:58 -0500361 * @func: IMA hook identifier
362 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
Eric Richter725de7f2016-06-01 13:14:02 -0500363 * @pcr: set the pcr to extend
Mimi Zohar3323eec2009-02-04 09:06:58 -0500364 *
365 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
366 * conditions.
367 *
Petko Manolov38d859f2015-12-02 17:47:54 +0200368 * Since the IMA policy may be updated multiple times we need to lock the
369 * list when walking it. Reads are many orders of magnitude more numerous
370 * than writes so ima_match_policy() is classical RCU candidate.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500371 */
Matthew Garrettd906c102018-01-08 13:36:20 -0800372int ima_match_policy(struct inode *inode, const struct cred *cred, u32 secid,
373 enum ima_hooks func, int mask, int flags, int *pcr)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500374{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500375 struct ima_rule_entry *entry;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500376 int action = 0, actmask = flags | (flags << 1);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500377
Petko Manolov38d859f2015-12-02 17:47:54 +0200378 rcu_read_lock();
379 list_for_each_entry_rcu(entry, ima_rules, list) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500380
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500381 if (!(entry->action & actmask))
382 continue;
383
Matthew Garrettd906c102018-01-08 13:36:20 -0800384 if (!ima_match_rules(entry, inode, cred, secid, func, mask))
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500385 continue;
386
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300387 action |= entry->flags & IMA_ACTION_FLAGS;
388
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300389 action |= entry->action & IMA_DO_MASK;
Mimi Zoharda1b0022016-09-29 10:04:52 -0400390 if (entry->action & IMA_APPRAISE) {
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500391 action |= get_subaction(entry, func);
Mimi Zohara9a49352018-03-10 23:07:34 -0500392 action &= ~IMA_HASH;
Mimi Zoharda1b0022016-09-29 10:04:52 -0400393 }
Mimi Zohard79d72e2012-12-03 17:08:11 -0500394
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300395 if (entry->action & IMA_DO_MASK)
396 actmask &= ~(entry->action | entry->action << 1);
397 else
398 actmask &= ~(entry->action | entry->action >> 1);
399
Eric Richter725de7f2016-06-01 13:14:02 -0500400 if ((pcr) && (entry->flags & IMA_PCR))
401 *pcr = entry->pcr;
402
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500403 if (!actmask)
404 break;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500405 }
Petko Manolov38d859f2015-12-02 17:47:54 +0200406 rcu_read_unlock();
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500407
408 return action;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500409}
410
Roberto Sassua7560242014-09-12 19:35:54 +0200411/*
412 * Initialize the ima_policy_flag variable based on the currently
413 * loaded policy. Based on this flag, the decision to short circuit
414 * out of a function or not call the function in the first place
415 * can be made earlier.
416 */
417void ima_update_policy_flag(void)
418{
419 struct ima_rule_entry *entry;
420
Roberto Sassua7560242014-09-12 19:35:54 +0200421 list_for_each_entry(entry, ima_rules, list) {
422 if (entry->action & IMA_DO_MASK)
423 ima_policy_flag |= entry->action;
424 }
425
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500426 ima_appraise |= temp_ima_appraise;
Roberto Sassua7560242014-09-12 19:35:54 +0200427 if (!ima_appraise)
428 ima_policy_flag &= ~IMA_APPRAISE;
429}
430
Mimi Zohar3323eec2009-02-04 09:06:58 -0500431/**
432 * ima_init_policy - initialize the default measure rules.
433 *
Mimi Zohar07f6a792011-03-09 22:25:48 -0500434 * ima_rules points to either the ima_default_rules or the
435 * the new ima_policy_rules.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500436 */
Eric Paris932995f2009-05-21 15:43:32 -0400437void __init ima_init_policy(void)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500438{
Mimi Zohar503ceae2017-04-21 18:58:27 -0400439 int i, measure_entries, appraise_entries, secure_boot_entries;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500440
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400441 /* if !ima_policy set entries = 0 so we load NO default rules */
442 measure_entries = ima_policy ? ARRAY_SIZE(dont_measure_rules) : 0;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500443 appraise_entries = ima_use_appraise_tcb ?
444 ARRAY_SIZE(default_appraise_rules) : 0;
Mimi Zohar503ceae2017-04-21 18:58:27 -0400445 secure_boot_entries = ima_use_secure_boot ?
446 ARRAY_SIZE(secure_boot_rules) : 0;
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200447
Dan Carpenter55778572015-04-07 12:22:11 +0300448 for (i = 0; i < measure_entries; i++)
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400449 list_add_tail(&dont_measure_rules[i].list, &ima_default_rules);
450
451 switch (ima_policy) {
452 case ORIGINAL_TCB:
453 for (i = 0; i < ARRAY_SIZE(original_measurement_rules); i++)
454 list_add_tail(&original_measurement_rules[i].list,
455 &ima_default_rules);
456 break;
457 case DEFAULT_TCB:
458 for (i = 0; i < ARRAY_SIZE(default_measurement_rules); i++)
459 list_add_tail(&default_measurement_rules[i].list,
460 &ima_default_rules);
461 default:
462 break;
463 }
Eric Paris5789ba32009-05-21 15:47:06 -0400464
Mimi Zohar503ceae2017-04-21 18:58:27 -0400465 /*
466 * Insert the appraise rules requiring file signatures, prior to
467 * any other appraise rules.
468 */
469 for (i = 0; i < secure_boot_entries; i++)
470 list_add_tail(&secure_boot_rules[i].list,
471 &ima_default_rules);
472
Dan Carpenter55778572015-04-07 12:22:11 +0300473 for (i = 0; i < appraise_entries; i++) {
474 list_add_tail(&default_appraise_rules[i].list,
475 &ima_default_rules);
Mimi Zohar95ee08f2015-12-07 15:08:01 -0500476 if (default_appraise_rules[i].func == POLICY_CHECK)
477 temp_ima_appraise |= IMA_APPRAISE_POLICY;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500478 }
479
480 ima_rules = &ima_default_rules;
Mimi Zohar95ee08f2015-12-07 15:08:01 -0500481 ima_update_policy_flag();
Mimi Zohar3323eec2009-02-04 09:06:58 -0500482}
Mimi Zohar4af46622009-02-04 09:07:00 -0500483
Sasha Levin01127212015-12-22 08:51:23 -0500484/* Make sure we have a valid policy, at least containing some rules. */
Colin Ian Kingc75d8e92016-01-20 11:13:46 +0000485int ima_check_policy(void)
Sasha Levin01127212015-12-22 08:51:23 -0500486{
487 if (list_empty(&ima_temp_rules))
488 return -EINVAL;
489 return 0;
490}
491
Mimi Zohar4af46622009-02-04 09:07:00 -0500492/**
493 * ima_update_policy - update default_rules with new measure rules
494 *
495 * Called on file .release to update the default rules with a complete new
Petko Manolov38d859f2015-12-02 17:47:54 +0200496 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
497 * they make a queue. The policy may be updated multiple times and this is the
498 * RCU updater.
499 *
500 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
501 * we switch from the default policy to user defined.
Mimi Zohar4af46622009-02-04 09:07:00 -0500502 */
503void ima_update_policy(void)
504{
Petko Manolov38d859f2015-12-02 17:47:54 +0200505 struct list_head *first, *last, *policy;
506
507 /* append current policy with the new rules */
508 first = (&ima_temp_rules)->next;
509 last = (&ima_temp_rules)->prev;
510 policy = &ima_policy_rules;
511
512 synchronize_rcu();
513
514 last->next = policy;
515 rcu_assign_pointer(list_next_rcu(policy->prev), first);
516 first->prev = policy->prev;
517 policy->prev = last;
518
519 /* prepare for the next policy rules addition */
520 INIT_LIST_HEAD(&ima_temp_rules);
521
522 if (ima_rules != policy) {
523 ima_policy_flag = 0;
524 ima_rules = policy;
525 }
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300526 ima_update_policy_flag();
Mimi Zohar4af46622009-02-04 09:07:00 -0500527}
528
529enum {
530 Opt_err = -1,
531 Opt_measure = 1, Opt_dont_measure,
Mimi Zohar07f6a792011-03-09 22:25:48 -0500532 Opt_appraise, Opt_dont_appraise,
Mimi Zoharda1b0022016-09-29 10:04:52 -0400533 Opt_audit, Opt_hash, Opt_dont_hash,
Mimi Zohar4af46622009-02-04 09:07:00 -0500534 Opt_obj_user, Opt_obj_role, Opt_obj_type,
535 Opt_subj_user, Opt_subj_role, Opt_subj_type,
Mimi Zohar139069e2014-11-05 07:48:36 -0500536 Opt_func, Opt_mask, Opt_fsmagic,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300537 Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq,
538 Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt,
539 Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt,
Eric Richter02606432016-06-01 13:14:01 -0500540 Opt_appraise_type, Opt_permit_directio,
541 Opt_pcr
Mimi Zohar4af46622009-02-04 09:07:00 -0500542};
543
544static match_table_t policy_tokens = {
545 {Opt_measure, "measure"},
546 {Opt_dont_measure, "dont_measure"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500547 {Opt_appraise, "appraise"},
548 {Opt_dont_appraise, "dont_appraise"},
Peter Moodye7c568e2012-06-14 10:04:36 -0700549 {Opt_audit, "audit"},
Mimi Zoharda1b0022016-09-29 10:04:52 -0400550 {Opt_hash, "hash"},
551 {Opt_dont_hash, "dont_hash"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500552 {Opt_obj_user, "obj_user=%s"},
553 {Opt_obj_role, "obj_role=%s"},
554 {Opt_obj_type, "obj_type=%s"},
555 {Opt_subj_user, "subj_user=%s"},
556 {Opt_subj_role, "subj_role=%s"},
557 {Opt_subj_type, "subj_type=%s"},
558 {Opt_func, "func=%s"},
559 {Opt_mask, "mask=%s"},
560 {Opt_fsmagic, "fsmagic=%s"},
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300561 {Opt_fsuuid, "fsuuid=%s"},
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300562 {Opt_uid_eq, "uid=%s"},
563 {Opt_euid_eq, "euid=%s"},
564 {Opt_fowner_eq, "fowner=%s"},
565 {Opt_uid_gt, "uid>%s"},
566 {Opt_euid_gt, "euid>%s"},
567 {Opt_fowner_gt, "fowner>%s"},
568 {Opt_uid_lt, "uid<%s"},
569 {Opt_euid_lt, "euid<%s"},
570 {Opt_fowner_lt, "fowner<%s"},
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300571 {Opt_appraise_type, "appraise_type=%s"},
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400572 {Opt_permit_directio, "permit_directio"},
Eric Richter02606432016-06-01 13:14:01 -0500573 {Opt_pcr, "pcr=%s"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500574 {Opt_err, NULL}
575};
576
Mimi Zohar07f6a792011-03-09 22:25:48 -0500577static int ima_lsm_rule_init(struct ima_rule_entry *entry,
Mimi Zohar7163a992013-01-03 14:19:09 -0500578 substring_t *args, int lsm_rule, int audit_type)
Mimi Zohar4af46622009-02-04 09:07:00 -0500579{
580 int result;
581
Eric Paris7b62e162010-04-20 10:21:01 -0400582 if (entry->lsm[lsm_rule].rule)
583 return -EINVAL;
584
Mimi Zohar7163a992013-01-03 14:19:09 -0500585 entry->lsm[lsm_rule].args_p = match_strdup(args);
586 if (!entry->lsm[lsm_rule].args_p)
587 return -ENOMEM;
588
Mimi Zohar4af46622009-02-04 09:07:00 -0500589 entry->lsm[lsm_rule].type = audit_type;
590 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
Mimi Zohar7163a992013-01-03 14:19:09 -0500591 Audit_equal,
592 entry->lsm[lsm_rule].args_p,
Mimi Zohar4af46622009-02-04 09:07:00 -0500593 &entry->lsm[lsm_rule].rule);
Mimi Zohar7163a992013-01-03 14:19:09 -0500594 if (!entry->lsm[lsm_rule].rule) {
595 kfree(entry->lsm[lsm_rule].args_p);
Mimi Zohar867c2022011-01-03 14:59:10 -0800596 return -EINVAL;
Mimi Zohar7163a992013-01-03 14:19:09 -0500597 }
598
Mimi Zohar4af46622009-02-04 09:07:00 -0500599 return result;
600}
601
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300602static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
603 bool (*rule_operator)(kuid_t, kuid_t))
Eric Paris2f1506c2010-04-20 10:21:30 -0400604{
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300605 if (rule_operator == &uid_gt)
606 audit_log_format(ab, "%s>", key);
607 else if (rule_operator == &uid_lt)
608 audit_log_format(ab, "%s<", key);
609 else
610 audit_log_format(ab, "%s=", key);
Eric Paris2f1506c2010-04-20 10:21:30 -0400611 audit_log_untrustedstring(ab, value);
612 audit_log_format(ab, " ");
613}
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300614static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
615{
616 ima_log_string_op(ab, key, value, NULL);
617}
Eric Paris2f1506c2010-04-20 10:21:30 -0400618
Mimi Zohar07f6a792011-03-09 22:25:48 -0500619static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
Mimi Zohar4af46622009-02-04 09:07:00 -0500620{
621 struct audit_buffer *ab;
Mimi Zohar4351c292014-11-05 07:53:55 -0500622 char *from;
Mimi Zohar4af46622009-02-04 09:07:00 -0500623 char *p;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300624 bool uid_token;
Mimi Zohar4af46622009-02-04 09:07:00 -0500625 int result = 0;
626
Mimi Zohar523979a2009-02-11 11:12:28 -0500627 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
Mimi Zohar4af46622009-02-04 09:07:00 -0500628
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600629 entry->uid = INVALID_UID;
Linus Torvalds882653222012-10-02 21:38:48 -0700630 entry->fowner = INVALID_UID;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300631 entry->uid_op = &uid_eq;
632 entry->fowner_op = &uid_eq;
Eric Parisb9035b12010-04-20 10:21:07 -0400633 entry->action = UNKNOWN;
Eric Paris28ef4002010-04-20 10:21:18 -0400634 while ((p = strsep(&rule, " \t")) != NULL) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500635 substring_t args[MAX_OPT_ARGS];
636 int token;
637 unsigned long lnum;
638
639 if (result < 0)
640 break;
Eric Paris28ef4002010-04-20 10:21:18 -0400641 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
642 continue;
Mimi Zohar4af46622009-02-04 09:07:00 -0500643 token = match_token(p, policy_tokens, args);
644 switch (token) {
645 case Opt_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400646 ima_log_string(ab, "action", "measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400647
648 if (entry->action != UNKNOWN)
649 result = -EINVAL;
650
Mimi Zohar4af46622009-02-04 09:07:00 -0500651 entry->action = MEASURE;
652 break;
653 case Opt_dont_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400654 ima_log_string(ab, "action", "dont_measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400655
656 if (entry->action != UNKNOWN)
657 result = -EINVAL;
658
Mimi Zohar4af46622009-02-04 09:07:00 -0500659 entry->action = DONT_MEASURE;
660 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500661 case Opt_appraise:
662 ima_log_string(ab, "action", "appraise");
663
664 if (entry->action != UNKNOWN)
665 result = -EINVAL;
666
667 entry->action = APPRAISE;
668 break;
669 case Opt_dont_appraise:
670 ima_log_string(ab, "action", "dont_appraise");
671
672 if (entry->action != UNKNOWN)
673 result = -EINVAL;
674
675 entry->action = DONT_APPRAISE;
676 break;
Peter Moodye7c568e2012-06-14 10:04:36 -0700677 case Opt_audit:
678 ima_log_string(ab, "action", "audit");
679
680 if (entry->action != UNKNOWN)
681 result = -EINVAL;
682
683 entry->action = AUDIT;
684 break;
Mimi Zoharda1b0022016-09-29 10:04:52 -0400685 case Opt_hash:
686 ima_log_string(ab, "action", "hash");
687
688 if (entry->action != UNKNOWN)
689 result = -EINVAL;
690
691 entry->action = HASH;
692 break;
693 case Opt_dont_hash:
694 ima_log_string(ab, "action", "dont_hash");
695
696 if (entry->action != UNKNOWN)
697 result = -EINVAL;
698
699 entry->action = DONT_HASH;
700 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500701 case Opt_func:
Eric Paris2f1506c2010-04-20 10:21:30 -0400702 ima_log_string(ab, "func", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400703
704 if (entry->func)
Mimi Zohar07f6a792011-03-09 22:25:48 -0500705 result = -EINVAL;
Eric Paris7b62e162010-04-20 10:21:01 -0400706
Mimi Zohar1e93d002010-01-26 17:02:41 -0500707 if (strcmp(args[0].from, "FILE_CHECK") == 0)
708 entry->func = FILE_CHECK;
709 /* PATH_CHECK is for backwards compat */
710 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
711 entry->func = FILE_CHECK;
Mimi Zoharfdf90722012-10-16 12:40:08 +1030712 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
713 entry->func = MODULE_CHECK;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400714 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
715 entry->func = FIRMWARE_CHECK;
Mimi Zohar16cac492012-12-13 11:15:04 -0500716 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
717 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
718 entry->func = MMAP_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500719 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
720 entry->func = BPRM_CHECK;
Matthew Garrettd906c102018-01-08 13:36:20 -0800721 else if (strcmp(args[0].from, "CREDS_CHECK") == 0)
722 entry->func = CREDS_CHECK;
Mimi Zohard9ddf072016-01-14 20:59:14 -0500723 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
724 0)
725 entry->func = KEXEC_KERNEL_CHECK;
726 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
727 == 0)
728 entry->func = KEXEC_INITRAMFS_CHECK;
Mimi Zohar19f8a842016-01-15 10:17:12 -0500729 else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
730 entry->func = POLICY_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500731 else
732 result = -EINVAL;
733 if (!result)
734 entry->flags |= IMA_FUNC;
735 break;
736 case Opt_mask:
Eric Paris2f1506c2010-04-20 10:21:30 -0400737 ima_log_string(ab, "mask", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400738
739 if (entry->mask)
740 result = -EINVAL;
741
Mimi Zohar4351c292014-11-05 07:53:55 -0500742 from = args[0].from;
743 if (*from == '^')
744 from++;
745
746 if ((strcmp(from, "MAY_EXEC")) == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500747 entry->mask = MAY_EXEC;
Mimi Zohar4351c292014-11-05 07:53:55 -0500748 else if (strcmp(from, "MAY_WRITE") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500749 entry->mask = MAY_WRITE;
Mimi Zohar4351c292014-11-05 07:53:55 -0500750 else if (strcmp(from, "MAY_READ") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500751 entry->mask = MAY_READ;
Mimi Zohar4351c292014-11-05 07:53:55 -0500752 else if (strcmp(from, "MAY_APPEND") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500753 entry->mask = MAY_APPEND;
754 else
755 result = -EINVAL;
756 if (!result)
Mimi Zohar4351c292014-11-05 07:53:55 -0500757 entry->flags |= (*args[0].from == '^')
758 ? IMA_INMASK : IMA_MASK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500759 break;
760 case Opt_fsmagic:
Eric Paris2f1506c2010-04-20 10:21:30 -0400761 ima_log_string(ab, "fsmagic", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400762
763 if (entry->fsmagic) {
764 result = -EINVAL;
765 break;
766 }
767
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200768 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
Mimi Zohar4af46622009-02-04 09:07:00 -0500769 if (!result)
770 entry->flags |= IMA_FSMAGIC;
771 break;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300772 case Opt_fsuuid:
773 ima_log_string(ab, "fsuuid", args[0].from);
774
Mike Rapoport36447452018-01-17 20:27:11 +0200775 if (!uuid_is_null(&entry->fsuuid)) {
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300776 result = -EINVAL;
777 break;
778 }
779
Christoph Hellwig787d8c52017-06-01 07:00:26 +0200780 result = uuid_parse(args[0].from, &entry->fsuuid);
Mimi Zohar446d64e2013-02-24 23:42:37 -0500781 if (!result)
782 entry->flags |= IMA_FSUUID;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300783 break;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300784 case Opt_uid_gt:
785 case Opt_euid_gt:
786 entry->uid_op = &uid_gt;
787 case Opt_uid_lt:
788 case Opt_euid_lt:
789 if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
790 entry->uid_op = &uid_lt;
791 case Opt_uid_eq:
792 case Opt_euid_eq:
793 uid_token = (token == Opt_uid_eq) ||
794 (token == Opt_uid_gt) ||
795 (token == Opt_uid_lt);
796
797 ima_log_string_op(ab, uid_token ? "uid" : "euid",
798 args[0].from, entry->uid_op);
Eric Paris7b62e162010-04-20 10:21:01 -0400799
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600800 if (uid_valid(entry->uid)) {
Eric Paris7b62e162010-04-20 10:21:01 -0400801 result = -EINVAL;
802 break;
803 }
804
Jingoo Han29707b22014-02-05 15:13:14 +0900805 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar4af46622009-02-04 09:07:00 -0500806 if (!result) {
Mimi Zohar139069e2014-11-05 07:48:36 -0500807 entry->uid = make_kuid(current_user_ns(),
808 (uid_t) lnum);
809 if (!uid_valid(entry->uid) ||
810 (uid_t)lnum != lnum)
Mimi Zohar4af46622009-02-04 09:07:00 -0500811 result = -EINVAL;
812 else
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300813 entry->flags |= uid_token
Mimi Zohar139069e2014-11-05 07:48:36 -0500814 ? IMA_UID : IMA_EUID;
Mimi Zohar4af46622009-02-04 09:07:00 -0500815 }
816 break;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300817 case Opt_fowner_gt:
818 entry->fowner_op = &uid_gt;
819 case Opt_fowner_lt:
820 if (token == Opt_fowner_lt)
821 entry->fowner_op = &uid_lt;
822 case Opt_fowner_eq:
823 ima_log_string_op(ab, "fowner", args[0].from,
824 entry->fowner_op);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500825
Linus Torvalds882653222012-10-02 21:38:48 -0700826 if (uid_valid(entry->fowner)) {
Mimi Zohar07f6a792011-03-09 22:25:48 -0500827 result = -EINVAL;
828 break;
829 }
830
Jingoo Han29707b22014-02-05 15:13:14 +0900831 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500832 if (!result) {
Linus Torvalds882653222012-10-02 21:38:48 -0700833 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
834 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500835 result = -EINVAL;
836 else
837 entry->flags |= IMA_FOWNER;
838 }
839 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500840 case Opt_obj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400841 ima_log_string(ab, "obj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500842 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500843 LSM_OBJ_USER,
844 AUDIT_OBJ_USER);
845 break;
846 case Opt_obj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400847 ima_log_string(ab, "obj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500848 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500849 LSM_OBJ_ROLE,
850 AUDIT_OBJ_ROLE);
851 break;
852 case Opt_obj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400853 ima_log_string(ab, "obj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500854 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500855 LSM_OBJ_TYPE,
856 AUDIT_OBJ_TYPE);
857 break;
858 case Opt_subj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400859 ima_log_string(ab, "subj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500860 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500861 LSM_SUBJ_USER,
862 AUDIT_SUBJ_USER);
863 break;
864 case Opt_subj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400865 ima_log_string(ab, "subj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500866 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500867 LSM_SUBJ_ROLE,
868 AUDIT_SUBJ_ROLE);
869 break;
870 case Opt_subj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400871 ima_log_string(ab, "subj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500872 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500873 LSM_SUBJ_TYPE,
874 AUDIT_SUBJ_TYPE);
875 break;
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300876 case Opt_appraise_type:
877 if (entry->action != APPRAISE) {
878 result = -EINVAL;
879 break;
880 }
881
882 ima_log_string(ab, "appraise_type", args[0].from);
883 if ((strcmp(args[0].from, "imasig")) == 0)
884 entry->flags |= IMA_DIGSIG_REQUIRED;
885 else
886 result = -EINVAL;
887 break;
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400888 case Opt_permit_directio:
889 entry->flags |= IMA_PERMIT_DIRECTIO;
890 break;
Eric Richter02606432016-06-01 13:14:01 -0500891 case Opt_pcr:
892 if (entry->action != MEASURE) {
893 result = -EINVAL;
894 break;
895 }
896 ima_log_string(ab, "pcr", args[0].from);
897
898 result = kstrtoint(args[0].from, 10, &entry->pcr);
899 if (result || INVALID_PCR(entry->pcr))
900 result = -EINVAL;
901 else
902 entry->flags |= IMA_PCR;
903
904 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500905 case Opt_err:
Eric Paris2f1506c2010-04-20 10:21:30 -0400906 ima_log_string(ab, "UNKNOWN", p);
Eric Parise9d393b2010-04-20 10:21:13 -0400907 result = -EINVAL;
Mimi Zohar4af46622009-02-04 09:07:00 -0500908 break;
909 }
910 }
Eric Paris7b62e162010-04-20 10:21:01 -0400911 if (!result && (entry->action == UNKNOWN))
Mimi Zohar4af46622009-02-04 09:07:00 -0500912 result = -EINVAL;
Mimi Zohara7f2a362012-12-21 08:34:21 -0500913 else if (entry->func == MODULE_CHECK)
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500914 temp_ima_appraise |= IMA_APPRAISE_MODULES;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400915 else if (entry->func == FIRMWARE_CHECK)
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500916 temp_ima_appraise |= IMA_APPRAISE_FIRMWARE;
Mimi Zohar19f8a842016-01-15 10:17:12 -0500917 else if (entry->func == POLICY_CHECK)
918 temp_ima_appraise |= IMA_APPRAISE_POLICY;
Eric Parisb0d5de42012-02-14 17:11:07 -0500919 audit_log_format(ab, "res=%d", !result);
Mimi Zohar4af46622009-02-04 09:07:00 -0500920 audit_log_end(ab);
921 return result;
922}
923
924/**
Mimi Zohar07f6a792011-03-09 22:25:48 -0500925 * ima_parse_add_rule - add a rule to ima_policy_rules
Mimi Zohar4af46622009-02-04 09:07:00 -0500926 * @rule - ima measurement policy rule
927 *
Petko Manolov38d859f2015-12-02 17:47:54 +0200928 * Avoid locking by allowing just one writer at a time in ima_write_policy()
Eric Paris6ccd0452010-04-20 10:20:54 -0400929 * Returns the length of the rule parsed, an error code on failure
Mimi Zohar4af46622009-02-04 09:07:00 -0500930 */
Eric Paris6ccd0452010-04-20 10:20:54 -0400931ssize_t ima_parse_add_rule(char *rule)
Mimi Zohar4af46622009-02-04 09:07:00 -0500932{
Mimi Zohar52a13282013-12-11 14:44:04 -0500933 static const char op[] = "update_policy";
Eric Paris6ccd0452010-04-20 10:20:54 -0400934 char *p;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500935 struct ima_rule_entry *entry;
Eric Paris6ccd0452010-04-20 10:20:54 -0400936 ssize_t result, len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500937 int audit_info = 0;
938
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300939 p = strsep(&rule, "\n");
940 len = strlen(p) + 1;
Dmitry Kasatkin71787842014-10-03 14:40:20 +0300941 p += strspn(p, " \t");
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300942
Dmitry Kasatkin71787842014-10-03 14:40:20 +0300943 if (*p == '#' || *p == '\0')
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300944 return len;
945
Mimi Zohar4af46622009-02-04 09:07:00 -0500946 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
947 if (!entry) {
948 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
949 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
950 return -ENOMEM;
951 }
952
953 INIT_LIST_HEAD(&entry->list);
954
Eric Paris6ccd0452010-04-20 10:20:54 -0400955 result = ima_parse_rule(p, entry);
Eric Paris7233e3e2010-04-20 10:21:24 -0400956 if (result) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500957 kfree(entry);
Mimi Zohar523979a2009-02-11 11:12:28 -0500958 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
Richard Guy Briggs7e9001f2014-06-16 15:52:07 -0400959 NULL, op, "invalid-policy", result,
Mimi Zohar523979a2009-02-11 11:12:28 -0500960 audit_info);
Eric Paris7233e3e2010-04-20 10:21:24 -0400961 return result;
Mimi Zohar523979a2009-02-11 11:12:28 -0500962 }
Eric Paris7233e3e2010-04-20 10:21:24 -0400963
Petko Manolov38d859f2015-12-02 17:47:54 +0200964 list_add_tail(&entry->list, &ima_temp_rules);
Eric Paris7233e3e2010-04-20 10:21:24 -0400965
966 return len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500967}
968
Petko Manolov38d859f2015-12-02 17:47:54 +0200969/**
970 * ima_delete_rules() called to cleanup invalid in-flight policy.
971 * We don't need locking as we operate on the temp list, which is
972 * different from the active one. There is also only one user of
973 * ima_delete_rules() at a time.
974 */
James Morris64c61d82009-02-05 09:28:26 +1100975void ima_delete_rules(void)
Mimi Zohar4af46622009-02-04 09:07:00 -0500976{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500977 struct ima_rule_entry *entry, *tmp;
Mimi Zohar7163a992013-01-03 14:19:09 -0500978 int i;
Mimi Zohar4af46622009-02-04 09:07:00 -0500979
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500980 temp_ima_appraise = 0;
Petko Manolov38d859f2015-12-02 17:47:54 +0200981 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
Mimi Zohar7163a992013-01-03 14:19:09 -0500982 for (i = 0; i < MAX_LSM_RULES; i++)
983 kfree(entry->lsm[i].args_p);
984
Mimi Zohar4af46622009-02-04 09:07:00 -0500985 list_del(&entry->list);
986 kfree(entry);
987 }
Mimi Zohar4af46622009-02-04 09:07:00 -0500988}
Petko Manolov80eae202015-12-02 17:47:56 +0200989
990#ifdef CONFIG_IMA_READ_POLICY
991enum {
992 mask_exec = 0, mask_write, mask_read, mask_append
993};
994
Thiago Jung Bauermannbb543e32017-06-07 22:49:10 -0300995static const char *const mask_tokens[] = {
Petko Manolov80eae202015-12-02 17:47:56 +0200996 "MAY_EXEC",
997 "MAY_WRITE",
998 "MAY_READ",
999 "MAY_APPEND"
1000};
1001
Thiago Jung Bauermann26632182017-06-07 22:49:11 -03001002#define __ima_hook_stringify(str) (#str),
Petko Manolov80eae202015-12-02 17:47:56 +02001003
Thiago Jung Bauermannbb543e32017-06-07 22:49:10 -03001004static const char *const func_tokens[] = {
Thiago Jung Bauermann26632182017-06-07 22:49:11 -03001005 __ima_hooks(__ima_hook_stringify)
Petko Manolov80eae202015-12-02 17:47:56 +02001006};
1007
1008void *ima_policy_start(struct seq_file *m, loff_t *pos)
1009{
1010 loff_t l = *pos;
1011 struct ima_rule_entry *entry;
1012
1013 rcu_read_lock();
1014 list_for_each_entry_rcu(entry, ima_rules, list) {
1015 if (!l--) {
1016 rcu_read_unlock();
1017 return entry;
1018 }
1019 }
1020 rcu_read_unlock();
1021 return NULL;
1022}
1023
1024void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
1025{
1026 struct ima_rule_entry *entry = v;
1027
1028 rcu_read_lock();
1029 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
1030 rcu_read_unlock();
1031 (*pos)++;
1032
1033 return (&entry->list == ima_rules) ? NULL : entry;
1034}
1035
1036void ima_policy_stop(struct seq_file *m, void *v)
1037{
1038}
1039
1040#define pt(token) policy_tokens[token + Opt_err].pattern
1041#define mt(token) mask_tokens[token]
Petko Manolov80eae202015-12-02 17:47:56 +02001042
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001043/*
1044 * policy_func_show - display the ima_hooks policy rule
1045 */
1046static void policy_func_show(struct seq_file *m, enum ima_hooks func)
1047{
Thiago Jung Bauermann26632182017-06-07 22:49:11 -03001048 if (func > 0 && func < MAX_CHECK)
1049 seq_printf(m, "func=%s ", func_tokens[func]);
1050 else
1051 seq_printf(m, "func=%d ", func);
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001052}
1053
Petko Manolov80eae202015-12-02 17:47:56 +02001054int ima_policy_show(struct seq_file *m, void *v)
1055{
1056 struct ima_rule_entry *entry = v;
Andy Shevchenkob8b57272016-05-20 17:00:57 -07001057 int i;
Petko Manolov80eae202015-12-02 17:47:56 +02001058 char tbuf[64] = {0,};
1059
1060 rcu_read_lock();
1061
1062 if (entry->action & MEASURE)
1063 seq_puts(m, pt(Opt_measure));
1064 if (entry->action & DONT_MEASURE)
1065 seq_puts(m, pt(Opt_dont_measure));
1066 if (entry->action & APPRAISE)
1067 seq_puts(m, pt(Opt_appraise));
1068 if (entry->action & DONT_APPRAISE)
1069 seq_puts(m, pt(Opt_dont_appraise));
1070 if (entry->action & AUDIT)
1071 seq_puts(m, pt(Opt_audit));
Mimi Zoharda1b0022016-09-29 10:04:52 -04001072 if (entry->action & HASH)
1073 seq_puts(m, pt(Opt_hash));
1074 if (entry->action & DONT_HASH)
1075 seq_puts(m, pt(Opt_dont_hash));
Petko Manolov80eae202015-12-02 17:47:56 +02001076
1077 seq_puts(m, " ");
1078
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001079 if (entry->flags & IMA_FUNC)
1080 policy_func_show(m, entry->func);
Petko Manolov80eae202015-12-02 17:47:56 +02001081
1082 if (entry->flags & IMA_MASK) {
1083 if (entry->mask & MAY_EXEC)
1084 seq_printf(m, pt(Opt_mask), mt(mask_exec));
1085 if (entry->mask & MAY_WRITE)
1086 seq_printf(m, pt(Opt_mask), mt(mask_write));
1087 if (entry->mask & MAY_READ)
1088 seq_printf(m, pt(Opt_mask), mt(mask_read));
1089 if (entry->mask & MAY_APPEND)
1090 seq_printf(m, pt(Opt_mask), mt(mask_append));
1091 seq_puts(m, " ");
1092 }
1093
1094 if (entry->flags & IMA_FSMAGIC) {
1095 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
1096 seq_printf(m, pt(Opt_fsmagic), tbuf);
1097 seq_puts(m, " ");
1098 }
1099
Eric Richter02606432016-06-01 13:14:01 -05001100 if (entry->flags & IMA_PCR) {
1101 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
1102 seq_printf(m, pt(Opt_pcr), tbuf);
1103 seq_puts(m, " ");
1104 }
1105
Petko Manolov80eae202015-12-02 17:47:56 +02001106 if (entry->flags & IMA_FSUUID) {
Christoph Hellwig787d8c52017-06-01 07:00:26 +02001107 seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
Petko Manolov80eae202015-12-02 17:47:56 +02001108 seq_puts(m, " ");
1109 }
1110
1111 if (entry->flags & IMA_UID) {
1112 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001113 if (entry->uid_op == &uid_gt)
1114 seq_printf(m, pt(Opt_uid_gt), tbuf);
1115 else if (entry->uid_op == &uid_lt)
1116 seq_printf(m, pt(Opt_uid_lt), tbuf);
1117 else
1118 seq_printf(m, pt(Opt_uid_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02001119 seq_puts(m, " ");
1120 }
1121
1122 if (entry->flags & IMA_EUID) {
1123 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001124 if (entry->uid_op == &uid_gt)
1125 seq_printf(m, pt(Opt_euid_gt), tbuf);
1126 else if (entry->uid_op == &uid_lt)
1127 seq_printf(m, pt(Opt_euid_lt), tbuf);
1128 else
1129 seq_printf(m, pt(Opt_euid_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02001130 seq_puts(m, " ");
1131 }
1132
1133 if (entry->flags & IMA_FOWNER) {
1134 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001135 if (entry->fowner_op == &uid_gt)
1136 seq_printf(m, pt(Opt_fowner_gt), tbuf);
1137 else if (entry->fowner_op == &uid_lt)
1138 seq_printf(m, pt(Opt_fowner_lt), tbuf);
1139 else
1140 seq_printf(m, pt(Opt_fowner_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02001141 seq_puts(m, " ");
1142 }
1143
1144 for (i = 0; i < MAX_LSM_RULES; i++) {
1145 if (entry->lsm[i].rule) {
1146 switch (i) {
1147 case LSM_OBJ_USER:
1148 seq_printf(m, pt(Opt_obj_user),
1149 (char *)entry->lsm[i].args_p);
1150 break;
1151 case LSM_OBJ_ROLE:
1152 seq_printf(m, pt(Opt_obj_role),
1153 (char *)entry->lsm[i].args_p);
1154 break;
1155 case LSM_OBJ_TYPE:
1156 seq_printf(m, pt(Opt_obj_type),
1157 (char *)entry->lsm[i].args_p);
1158 break;
1159 case LSM_SUBJ_USER:
1160 seq_printf(m, pt(Opt_subj_user),
1161 (char *)entry->lsm[i].args_p);
1162 break;
1163 case LSM_SUBJ_ROLE:
1164 seq_printf(m, pt(Opt_subj_role),
1165 (char *)entry->lsm[i].args_p);
1166 break;
1167 case LSM_SUBJ_TYPE:
1168 seq_printf(m, pt(Opt_subj_type),
1169 (char *)entry->lsm[i].args_p);
1170 break;
1171 }
1172 }
1173 }
1174 if (entry->flags & IMA_DIGSIG_REQUIRED)
1175 seq_puts(m, "appraise_type=imasig ");
1176 if (entry->flags & IMA_PERMIT_DIRECTIO)
1177 seq_puts(m, "permit_directio ");
1178 rcu_read_unlock();
1179 seq_puts(m, "\n");
1180 return 0;
1181}
1182#endif /* CONFIG_IMA_READ_POLICY */