blob: d89bebf85421e7134aff233a52545218efabf512 [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},
Martin Townsend1c070b12018-03-22 13:46:01 +000099 {.action = DONT_MEASURE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
Roberto Sassu6438de92015-04-11 17:13:06 +0200100 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
101 .flags = IMA_FSMAGIC},
Laura Abbott82e3bb42017-05-09 11:25:27 -0700102 {.action = DONT_MEASURE, .fsmagic = CGROUP2_SUPER_MAGIC,
103 .flags = IMA_FSMAGIC},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400104 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC}
105};
106
James Morrisbad44172017-02-13 16:34:35 +1100107static struct ima_rule_entry original_measurement_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200108 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500109 .flags = IMA_FUNC | IMA_MASK},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200110 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500111 .flags = IMA_FUNC | IMA_MASK},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400112 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300113 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
114 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400115 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
116 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
117};
118
James Morrisbad44172017-02-13 16:34:35 +1100119static struct ima_rule_entry default_measurement_rules[] __ro_after_init = {
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400120 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
121 .flags = IMA_FUNC | IMA_MASK},
122 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
123 .flags = IMA_FUNC | IMA_MASK},
124 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300125 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
126 .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400127 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300128 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
129 .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200130 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400131 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
Mimi Zohar19f8a842016-01-15 10:17:12 -0500132 {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
Mimi Zohar3323eec2009-02-04 09:06:58 -0500133};
134
James Morrisbad44172017-02-13 16:34:35 +1100135static struct ima_rule_entry default_appraise_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200136 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
137 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
138 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
139 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
140 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
141 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
142 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
143 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
144 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
Martin Townsend1c070b12018-03-22 13:46:01 +0000145 {.action = DONT_APPRAISE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zoharcd025f72015-04-21 16:54:24 -0400146 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200147 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
Laura Abbott82e3bb42017-05-09 11:25:27 -0700148 {.action = DONT_APPRAISE, .fsmagic = CGROUP2_SUPER_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zohar95ee08f2015-12-07 15:08:01 -0500149#ifdef CONFIG_IMA_WRITE_POLICY
150 {.action = APPRAISE, .func = POLICY_CHECK,
151 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
152#endif
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200153#ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300154 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
155 .flags = IMA_FOWNER},
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200156#else
157 /* force signature */
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300158 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200159 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
160#endif
Mimi Zohar07f6a792011-03-09 22:25:48 -0500161};
Mimi Zohar3323eec2009-02-04 09:06:58 -0500162
Mimi Zohar503ceae2017-04-21 18:58:27 -0400163static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
164 {.action = APPRAISE, .func = MODULE_CHECK,
165 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
166 {.action = APPRAISE, .func = FIRMWARE_CHECK,
167 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
168 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
169 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
170 {.action = APPRAISE, .func = POLICY_CHECK,
171 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
172};
173
Mimi Zohar07f6a792011-03-09 22:25:48 -0500174static LIST_HEAD(ima_default_rules);
175static LIST_HEAD(ima_policy_rules);
Petko Manolov38d859f2015-12-02 17:47:54 +0200176static LIST_HEAD(ima_temp_rules);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500177static struct list_head *ima_rules;
178
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400179static int ima_policy __initdata;
Petko Manolov38d859f2015-12-02 17:47:54 +0200180
Mimi Zohar07f6a792011-03-09 22:25:48 -0500181static int __init default_measure_policy_setup(char *str)
Eric Paris5789ba32009-05-21 15:47:06 -0400182{
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400183 if (ima_policy)
184 return 1;
185
186 ima_policy = ORIGINAL_TCB;
Eric Paris5789ba32009-05-21 15:47:06 -0400187 return 1;
188}
Mimi Zohar07f6a792011-03-09 22:25:48 -0500189__setup("ima_tcb", default_measure_policy_setup);
190
Mimi Zohar33ce9542017-04-24 12:04:09 -0400191static bool ima_use_appraise_tcb __initdata;
Mimi Zohar503ceae2017-04-21 18:58:27 -0400192static bool ima_use_secure_boot __initdata;
Mimi Zohar9e670282018-02-21 11:36:32 -0500193static bool ima_fail_unverifiable_sigs __ro_after_init;
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400194static int __init policy_setup(char *str)
195{
Mimi Zohar33ce9542017-04-24 12:04:09 -0400196 char *p;
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400197
Mimi Zohar33ce9542017-04-24 12:04:09 -0400198 while ((p = strsep(&str, " |\n")) != NULL) {
199 if (*p == ' ')
200 continue;
201 if ((strcmp(p, "tcb") == 0) && !ima_policy)
202 ima_policy = DEFAULT_TCB;
203 else if (strcmp(p, "appraise_tcb") == 0)
Thomas Meyer39adb922017-10-07 16:02:21 +0200204 ima_use_appraise_tcb = true;
Mimi Zohar503ceae2017-04-21 18:58:27 -0400205 else if (strcmp(p, "secure_boot") == 0)
Thomas Meyer39adb922017-10-07 16:02:21 +0200206 ima_use_secure_boot = true;
Mimi Zohar9e670282018-02-21 11:36:32 -0500207 else if (strcmp(p, "fail_securely") == 0)
208 ima_fail_unverifiable_sigs = true;
Mimi Zohar33ce9542017-04-24 12:04:09 -0400209 }
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400210
211 return 1;
212}
213__setup("ima_policy=", policy_setup);
214
Mimi Zohar07f6a792011-03-09 22:25:48 -0500215static int __init default_appraise_policy_setup(char *str)
216{
Thomas Meyer39adb922017-10-07 16:02:21 +0200217 ima_use_appraise_tcb = true;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500218 return 1;
219}
220__setup("ima_appraise_tcb", default_appraise_policy_setup);
Eric Paris5789ba32009-05-21 15:47:06 -0400221
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200222/*
Petko Manolov38d859f2015-12-02 17:47:54 +0200223 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
224 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
225 * the reloaded LSM policy. We assume the rules still exist; and BUG_ON() if
226 * they don't.
Mimi Zohar7163a992013-01-03 14:19:09 -0500227 */
228static void ima_lsm_update_rules(void)
229{
Petko Manolov38d859f2015-12-02 17:47:54 +0200230 struct ima_rule_entry *entry;
Mimi Zohar7163a992013-01-03 14:19:09 -0500231 int result;
232 int i;
233
Petko Manolov38d859f2015-12-02 17:47:54 +0200234 list_for_each_entry(entry, &ima_policy_rules, list) {
Mimi Zohar7163a992013-01-03 14:19:09 -0500235 for (i = 0; i < MAX_LSM_RULES; i++) {
236 if (!entry->lsm[i].rule)
237 continue;
238 result = security_filter_rule_init(entry->lsm[i].type,
239 Audit_equal,
240 entry->lsm[i].args_p,
241 &entry->lsm[i].rule);
242 BUG_ON(!entry->lsm[i].rule);
243 }
244 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500245}
246
Mimi Zohar3323eec2009-02-04 09:06:58 -0500247/**
248 * ima_match_rules - determine whether an inode matches the measure rule.
249 * @rule: a pointer to a rule
250 * @inode: a pointer to an inode
Matthew Garrettd906c102018-01-08 13:36:20 -0800251 * @cred: a pointer to a credentials structure for user validation
252 * @secid: the secid of the task to be validated
Mimi Zohar3323eec2009-02-04 09:06:58 -0500253 * @func: LIM hook identifier
254 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
255 *
256 * Returns true on rule match, false on failure.
257 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500258static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
Matthew Garrettd906c102018-01-08 13:36:20 -0800259 const struct cred *cred, u32 secid,
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500260 enum ima_hooks func, int mask)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500261{
Mimi Zohar4af46622009-02-04 09:07:00 -0500262 int i;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500263
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200264 if ((rule->flags & IMA_FUNC) &&
265 (rule->func != func && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500266 return false;
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200267 if ((rule->flags & IMA_MASK) &&
268 (rule->mask != mask && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500269 return false;
Mimi Zohar4351c292014-11-05 07:53:55 -0500270 if ((rule->flags & IMA_INMASK) &&
271 (!(rule->mask & mask) && func != POST_SETATTR))
272 return false;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500273 if ((rule->flags & IMA_FSMAGIC)
274 && rule->fsmagic != inode->i_sb->s_magic)
275 return false;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300276 if ((rule->flags & IMA_FSUUID) &&
Christoph Hellwig85787092017-05-10 15:06:33 +0200277 !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid))
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300278 return false;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300279 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500280 return false;
Mimi Zohar139069e2014-11-05 07:48:36 -0500281 if (rule->flags & IMA_EUID) {
282 if (has_capability_noaudit(current, CAP_SETUID)) {
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300283 if (!rule->uid_op(cred->euid, rule->uid)
284 && !rule->uid_op(cred->suid, rule->uid)
285 && !rule->uid_op(cred->uid, rule->uid))
Mimi Zohar139069e2014-11-05 07:48:36 -0500286 return false;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300287 } else if (!rule->uid_op(cred->euid, rule->uid))
Mimi Zohar139069e2014-11-05 07:48:36 -0500288 return false;
289 }
290
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300291 if ((rule->flags & IMA_FOWNER) &&
292 !rule->fowner_op(inode->i_uid, rule->fowner))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500293 return false;
Mimi Zohar4af46622009-02-04 09:07:00 -0500294 for (i = 0; i < MAX_LSM_RULES; i++) {
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400295 int rc = 0;
Matthew Garrettd906c102018-01-08 13:36:20 -0800296 u32 osid;
Mimi Zohar7163a992013-01-03 14:19:09 -0500297 int retried = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500298
299 if (!rule->lsm[i].rule)
300 continue;
Mimi Zohar7163a992013-01-03 14:19:09 -0500301retry:
Mimi Zohar4af46622009-02-04 09:07:00 -0500302 switch (i) {
303 case LSM_OBJ_USER:
304 case LSM_OBJ_ROLE:
305 case LSM_OBJ_TYPE:
306 security_inode_getsecid(inode, &osid);
307 rc = security_filter_rule_match(osid,
308 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400309 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500310 rule->lsm[i].rule,
311 NULL);
312 break;
313 case LSM_SUBJ_USER:
314 case LSM_SUBJ_ROLE:
315 case LSM_SUBJ_TYPE:
Matthew Garrettd906c102018-01-08 13:36:20 -0800316 rc = security_filter_rule_match(secid,
Mimi Zohar4af46622009-02-04 09:07:00 -0500317 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400318 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500319 rule->lsm[i].rule,
320 NULL);
321 default:
322 break;
323 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500324 if ((rc < 0) && (!retried)) {
325 retried = 1;
326 ima_lsm_update_rules();
327 goto retry;
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200328 }
Mimi Zohar4af46622009-02-04 09:07:00 -0500329 if (!rc)
330 return false;
331 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500332 return true;
333}
334
Mimi Zohard79d72e2012-12-03 17:08:11 -0500335/*
336 * In addition to knowing that we need to appraise the file in general,
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500337 * we need to differentiate between calling hooks, for hook specific rules.
Mimi Zohard79d72e2012-12-03 17:08:11 -0500338 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500339static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500340{
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500341 if (!(rule->flags & IMA_FUNC))
342 return IMA_FILE_APPRAISE;
343
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200344 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500345 case MMAP_CHECK:
346 return IMA_MMAP_APPRAISE;
347 case BPRM_CHECK:
348 return IMA_BPRM_APPRAISE;
Matthew Garrettd906c102018-01-08 13:36:20 -0800349 case CREDS_CHECK:
350 return IMA_CREDS_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500351 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500352 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -0500353 return IMA_FILE_APPRAISE;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500354 case MODULE_CHECK ... MAX_CHECK - 1:
355 default:
356 return IMA_READ_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500357 }
358}
359
Mimi Zohar3323eec2009-02-04 09:06:58 -0500360/**
361 * ima_match_policy - decision based on LSM and other conditions
362 * @inode: pointer to an inode for which the policy decision is being made
Matthew Garrettd906c102018-01-08 13:36:20 -0800363 * @cred: pointer to a credentials structure for which the policy decision is
364 * being made
365 * @secid: LSM secid of the task to be validated
Mimi Zohar3323eec2009-02-04 09:06:58 -0500366 * @func: IMA hook identifier
367 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
Eric Richter725de7f2016-06-01 13:14:02 -0500368 * @pcr: set the pcr to extend
Mimi Zohar3323eec2009-02-04 09:06:58 -0500369 *
370 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
371 * conditions.
372 *
Petko Manolov38d859f2015-12-02 17:47:54 +0200373 * Since the IMA policy may be updated multiple times we need to lock the
374 * list when walking it. Reads are many orders of magnitude more numerous
375 * than writes so ima_match_policy() is classical RCU candidate.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500376 */
Matthew Garrettd906c102018-01-08 13:36:20 -0800377int ima_match_policy(struct inode *inode, const struct cred *cred, u32 secid,
378 enum ima_hooks func, int mask, int flags, int *pcr)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500379{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500380 struct ima_rule_entry *entry;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500381 int action = 0, actmask = flags | (flags << 1);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500382
Petko Manolov38d859f2015-12-02 17:47:54 +0200383 rcu_read_lock();
384 list_for_each_entry_rcu(entry, ima_rules, list) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500385
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500386 if (!(entry->action & actmask))
387 continue;
388
Matthew Garrettd906c102018-01-08 13:36:20 -0800389 if (!ima_match_rules(entry, inode, cred, secid, func, mask))
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500390 continue;
391
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300392 action |= entry->flags & IMA_ACTION_FLAGS;
393
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300394 action |= entry->action & IMA_DO_MASK;
Mimi Zoharda1b0022016-09-29 10:04:52 -0400395 if (entry->action & IMA_APPRAISE) {
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500396 action |= get_subaction(entry, func);
Mimi Zohara9a49352018-03-10 23:07:34 -0500397 action &= ~IMA_HASH;
Mimi Zohar9e670282018-02-21 11:36:32 -0500398 if (ima_fail_unverifiable_sigs)
399 action |= IMA_FAIL_UNVERIFIABLE_SIGS;
Mimi Zoharda1b0022016-09-29 10:04:52 -0400400 }
Mimi Zohard79d72e2012-12-03 17:08:11 -0500401
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300402 if (entry->action & IMA_DO_MASK)
403 actmask &= ~(entry->action | entry->action << 1);
404 else
405 actmask &= ~(entry->action | entry->action >> 1);
406
Eric Richter725de7f2016-06-01 13:14:02 -0500407 if ((pcr) && (entry->flags & IMA_PCR))
408 *pcr = entry->pcr;
409
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500410 if (!actmask)
411 break;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500412 }
Petko Manolov38d859f2015-12-02 17:47:54 +0200413 rcu_read_unlock();
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500414
415 return action;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500416}
417
Roberto Sassua7560242014-09-12 19:35:54 +0200418/*
419 * Initialize the ima_policy_flag variable based on the currently
420 * loaded policy. Based on this flag, the decision to short circuit
421 * out of a function or not call the function in the first place
422 * can be made earlier.
423 */
424void ima_update_policy_flag(void)
425{
426 struct ima_rule_entry *entry;
427
Roberto Sassua7560242014-09-12 19:35:54 +0200428 list_for_each_entry(entry, ima_rules, list) {
429 if (entry->action & IMA_DO_MASK)
430 ima_policy_flag |= entry->action;
431 }
432
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500433 ima_appraise |= temp_ima_appraise;
Roberto Sassua7560242014-09-12 19:35:54 +0200434 if (!ima_appraise)
435 ima_policy_flag &= ~IMA_APPRAISE;
436}
437
Mimi Zohar3323eec2009-02-04 09:06:58 -0500438/**
439 * ima_init_policy - initialize the default measure rules.
440 *
Mimi Zohar07f6a792011-03-09 22:25:48 -0500441 * ima_rules points to either the ima_default_rules or the
442 * the new ima_policy_rules.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500443 */
Eric Paris932995f2009-05-21 15:43:32 -0400444void __init ima_init_policy(void)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500445{
Mimi Zohar503ceae2017-04-21 18:58:27 -0400446 int i, measure_entries, appraise_entries, secure_boot_entries;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500447
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400448 /* if !ima_policy set entries = 0 so we load NO default rules */
449 measure_entries = ima_policy ? ARRAY_SIZE(dont_measure_rules) : 0;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500450 appraise_entries = ima_use_appraise_tcb ?
451 ARRAY_SIZE(default_appraise_rules) : 0;
Mimi Zohar503ceae2017-04-21 18:58:27 -0400452 secure_boot_entries = ima_use_secure_boot ?
453 ARRAY_SIZE(secure_boot_rules) : 0;
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200454
Dan Carpenter55778572015-04-07 12:22:11 +0300455 for (i = 0; i < measure_entries; i++)
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400456 list_add_tail(&dont_measure_rules[i].list, &ima_default_rules);
457
458 switch (ima_policy) {
459 case ORIGINAL_TCB:
460 for (i = 0; i < ARRAY_SIZE(original_measurement_rules); i++)
461 list_add_tail(&original_measurement_rules[i].list,
462 &ima_default_rules);
463 break;
464 case DEFAULT_TCB:
465 for (i = 0; i < ARRAY_SIZE(default_measurement_rules); i++)
466 list_add_tail(&default_measurement_rules[i].list,
467 &ima_default_rules);
468 default:
469 break;
470 }
Eric Paris5789ba32009-05-21 15:47:06 -0400471
Mimi Zohar503ceae2017-04-21 18:58:27 -0400472 /*
473 * Insert the appraise rules requiring file signatures, prior to
474 * any other appraise rules.
475 */
476 for (i = 0; i < secure_boot_entries; i++)
477 list_add_tail(&secure_boot_rules[i].list,
478 &ima_default_rules);
479
Dan Carpenter55778572015-04-07 12:22:11 +0300480 for (i = 0; i < appraise_entries; i++) {
481 list_add_tail(&default_appraise_rules[i].list,
482 &ima_default_rules);
Mimi Zohar95ee08f2015-12-07 15:08:01 -0500483 if (default_appraise_rules[i].func == POLICY_CHECK)
484 temp_ima_appraise |= IMA_APPRAISE_POLICY;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500485 }
486
487 ima_rules = &ima_default_rules;
Mimi Zohar95ee08f2015-12-07 15:08:01 -0500488 ima_update_policy_flag();
Mimi Zohar3323eec2009-02-04 09:06:58 -0500489}
Mimi Zohar4af46622009-02-04 09:07:00 -0500490
Sasha Levin01127212015-12-22 08:51:23 -0500491/* Make sure we have a valid policy, at least containing some rules. */
Colin Ian Kingc75d8e92016-01-20 11:13:46 +0000492int ima_check_policy(void)
Sasha Levin01127212015-12-22 08:51:23 -0500493{
494 if (list_empty(&ima_temp_rules))
495 return -EINVAL;
496 return 0;
497}
498
Mimi Zohar4af46622009-02-04 09:07:00 -0500499/**
500 * ima_update_policy - update default_rules with new measure rules
501 *
502 * Called on file .release to update the default rules with a complete new
Petko Manolov38d859f2015-12-02 17:47:54 +0200503 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
504 * they make a queue. The policy may be updated multiple times and this is the
505 * RCU updater.
506 *
507 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
508 * we switch from the default policy to user defined.
Mimi Zohar4af46622009-02-04 09:07:00 -0500509 */
510void ima_update_policy(void)
511{
Petko Manolov38d859f2015-12-02 17:47:54 +0200512 struct list_head *first, *last, *policy;
513
514 /* append current policy with the new rules */
515 first = (&ima_temp_rules)->next;
516 last = (&ima_temp_rules)->prev;
517 policy = &ima_policy_rules;
518
519 synchronize_rcu();
520
521 last->next = policy;
522 rcu_assign_pointer(list_next_rcu(policy->prev), first);
523 first->prev = policy->prev;
524 policy->prev = last;
525
526 /* prepare for the next policy rules addition */
527 INIT_LIST_HEAD(&ima_temp_rules);
528
529 if (ima_rules != policy) {
530 ima_policy_flag = 0;
531 ima_rules = policy;
532 }
Dmitry Kasatkin0716abb2014-10-03 14:40:21 +0300533 ima_update_policy_flag();
Mimi Zohar4af46622009-02-04 09:07:00 -0500534}
535
536enum {
537 Opt_err = -1,
538 Opt_measure = 1, Opt_dont_measure,
Mimi Zohar07f6a792011-03-09 22:25:48 -0500539 Opt_appraise, Opt_dont_appraise,
Mimi Zoharda1b0022016-09-29 10:04:52 -0400540 Opt_audit, Opt_hash, Opt_dont_hash,
Mimi Zohar4af46622009-02-04 09:07:00 -0500541 Opt_obj_user, Opt_obj_role, Opt_obj_type,
542 Opt_subj_user, Opt_subj_role, Opt_subj_type,
Mimi Zohar139069e2014-11-05 07:48:36 -0500543 Opt_func, Opt_mask, Opt_fsmagic,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300544 Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq,
545 Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt,
546 Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt,
Eric Richter02606432016-06-01 13:14:01 -0500547 Opt_appraise_type, Opt_permit_directio,
548 Opt_pcr
Mimi Zohar4af46622009-02-04 09:07:00 -0500549};
550
551static match_table_t policy_tokens = {
552 {Opt_measure, "measure"},
553 {Opt_dont_measure, "dont_measure"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500554 {Opt_appraise, "appraise"},
555 {Opt_dont_appraise, "dont_appraise"},
Peter Moodye7c568e2012-06-14 10:04:36 -0700556 {Opt_audit, "audit"},
Mimi Zoharda1b0022016-09-29 10:04:52 -0400557 {Opt_hash, "hash"},
558 {Opt_dont_hash, "dont_hash"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500559 {Opt_obj_user, "obj_user=%s"},
560 {Opt_obj_role, "obj_role=%s"},
561 {Opt_obj_type, "obj_type=%s"},
562 {Opt_subj_user, "subj_user=%s"},
563 {Opt_subj_role, "subj_role=%s"},
564 {Opt_subj_type, "subj_type=%s"},
565 {Opt_func, "func=%s"},
566 {Opt_mask, "mask=%s"},
567 {Opt_fsmagic, "fsmagic=%s"},
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300568 {Opt_fsuuid, "fsuuid=%s"},
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300569 {Opt_uid_eq, "uid=%s"},
570 {Opt_euid_eq, "euid=%s"},
571 {Opt_fowner_eq, "fowner=%s"},
572 {Opt_uid_gt, "uid>%s"},
573 {Opt_euid_gt, "euid>%s"},
574 {Opt_fowner_gt, "fowner>%s"},
575 {Opt_uid_lt, "uid<%s"},
576 {Opt_euid_lt, "euid<%s"},
577 {Opt_fowner_lt, "fowner<%s"},
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300578 {Opt_appraise_type, "appraise_type=%s"},
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400579 {Opt_permit_directio, "permit_directio"},
Eric Richter02606432016-06-01 13:14:01 -0500580 {Opt_pcr, "pcr=%s"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500581 {Opt_err, NULL}
582};
583
Mimi Zohar07f6a792011-03-09 22:25:48 -0500584static int ima_lsm_rule_init(struct ima_rule_entry *entry,
Mimi Zohar7163a992013-01-03 14:19:09 -0500585 substring_t *args, int lsm_rule, int audit_type)
Mimi Zohar4af46622009-02-04 09:07:00 -0500586{
587 int result;
588
Eric Paris7b62e162010-04-20 10:21:01 -0400589 if (entry->lsm[lsm_rule].rule)
590 return -EINVAL;
591
Mimi Zohar7163a992013-01-03 14:19:09 -0500592 entry->lsm[lsm_rule].args_p = match_strdup(args);
593 if (!entry->lsm[lsm_rule].args_p)
594 return -ENOMEM;
595
Mimi Zohar4af46622009-02-04 09:07:00 -0500596 entry->lsm[lsm_rule].type = audit_type;
597 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
Mimi Zohar7163a992013-01-03 14:19:09 -0500598 Audit_equal,
599 entry->lsm[lsm_rule].args_p,
Mimi Zohar4af46622009-02-04 09:07:00 -0500600 &entry->lsm[lsm_rule].rule);
Mimi Zohar7163a992013-01-03 14:19:09 -0500601 if (!entry->lsm[lsm_rule].rule) {
602 kfree(entry->lsm[lsm_rule].args_p);
Mimi Zohar867c2022011-01-03 14:59:10 -0800603 return -EINVAL;
Mimi Zohar7163a992013-01-03 14:19:09 -0500604 }
605
Mimi Zohar4af46622009-02-04 09:07:00 -0500606 return result;
607}
608
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300609static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
610 bool (*rule_operator)(kuid_t, kuid_t))
Eric Paris2f1506c2010-04-20 10:21:30 -0400611{
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300612 if (rule_operator == &uid_gt)
613 audit_log_format(ab, "%s>", key);
614 else if (rule_operator == &uid_lt)
615 audit_log_format(ab, "%s<", key);
616 else
617 audit_log_format(ab, "%s=", key);
Eric Paris2f1506c2010-04-20 10:21:30 -0400618 audit_log_untrustedstring(ab, value);
619 audit_log_format(ab, " ");
620}
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300621static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
622{
623 ima_log_string_op(ab, key, value, NULL);
624}
Eric Paris2f1506c2010-04-20 10:21:30 -0400625
Mimi Zohar07f6a792011-03-09 22:25:48 -0500626static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
Mimi Zohar4af46622009-02-04 09:07:00 -0500627{
628 struct audit_buffer *ab;
Mimi Zohar4351c292014-11-05 07:53:55 -0500629 char *from;
Mimi Zohar4af46622009-02-04 09:07:00 -0500630 char *p;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300631 bool uid_token;
Mimi Zohar4af46622009-02-04 09:07:00 -0500632 int result = 0;
633
Mimi Zohar523979a2009-02-11 11:12:28 -0500634 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
Mimi Zohar4af46622009-02-04 09:07:00 -0500635
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600636 entry->uid = INVALID_UID;
Linus Torvalds882653222012-10-02 21:38:48 -0700637 entry->fowner = INVALID_UID;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300638 entry->uid_op = &uid_eq;
639 entry->fowner_op = &uid_eq;
Eric Parisb9035b12010-04-20 10:21:07 -0400640 entry->action = UNKNOWN;
Eric Paris28ef4002010-04-20 10:21:18 -0400641 while ((p = strsep(&rule, " \t")) != NULL) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500642 substring_t args[MAX_OPT_ARGS];
643 int token;
644 unsigned long lnum;
645
646 if (result < 0)
647 break;
Eric Paris28ef4002010-04-20 10:21:18 -0400648 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
649 continue;
Mimi Zohar4af46622009-02-04 09:07:00 -0500650 token = match_token(p, policy_tokens, args);
651 switch (token) {
652 case Opt_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400653 ima_log_string(ab, "action", "measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400654
655 if (entry->action != UNKNOWN)
656 result = -EINVAL;
657
Mimi Zohar4af46622009-02-04 09:07:00 -0500658 entry->action = MEASURE;
659 break;
660 case Opt_dont_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400661 ima_log_string(ab, "action", "dont_measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400662
663 if (entry->action != UNKNOWN)
664 result = -EINVAL;
665
Mimi Zohar4af46622009-02-04 09:07:00 -0500666 entry->action = DONT_MEASURE;
667 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500668 case Opt_appraise:
669 ima_log_string(ab, "action", "appraise");
670
671 if (entry->action != UNKNOWN)
672 result = -EINVAL;
673
674 entry->action = APPRAISE;
675 break;
676 case Opt_dont_appraise:
677 ima_log_string(ab, "action", "dont_appraise");
678
679 if (entry->action != UNKNOWN)
680 result = -EINVAL;
681
682 entry->action = DONT_APPRAISE;
683 break;
Peter Moodye7c568e2012-06-14 10:04:36 -0700684 case Opt_audit:
685 ima_log_string(ab, "action", "audit");
686
687 if (entry->action != UNKNOWN)
688 result = -EINVAL;
689
690 entry->action = AUDIT;
691 break;
Mimi Zoharda1b0022016-09-29 10:04:52 -0400692 case Opt_hash:
693 ima_log_string(ab, "action", "hash");
694
695 if (entry->action != UNKNOWN)
696 result = -EINVAL;
697
698 entry->action = HASH;
699 break;
700 case Opt_dont_hash:
701 ima_log_string(ab, "action", "dont_hash");
702
703 if (entry->action != UNKNOWN)
704 result = -EINVAL;
705
706 entry->action = DONT_HASH;
707 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500708 case Opt_func:
Eric Paris2f1506c2010-04-20 10:21:30 -0400709 ima_log_string(ab, "func", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400710
711 if (entry->func)
Mimi Zohar07f6a792011-03-09 22:25:48 -0500712 result = -EINVAL;
Eric Paris7b62e162010-04-20 10:21:01 -0400713
Mimi Zohar1e93d002010-01-26 17:02:41 -0500714 if (strcmp(args[0].from, "FILE_CHECK") == 0)
715 entry->func = FILE_CHECK;
716 /* PATH_CHECK is for backwards compat */
717 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
718 entry->func = FILE_CHECK;
Mimi Zoharfdf90722012-10-16 12:40:08 +1030719 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
720 entry->func = MODULE_CHECK;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400721 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
722 entry->func = FIRMWARE_CHECK;
Mimi Zohar16cac492012-12-13 11:15:04 -0500723 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
724 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
725 entry->func = MMAP_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500726 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
727 entry->func = BPRM_CHECK;
Matthew Garrettd906c102018-01-08 13:36:20 -0800728 else if (strcmp(args[0].from, "CREDS_CHECK") == 0)
729 entry->func = CREDS_CHECK;
Mimi Zohard9ddf072016-01-14 20:59:14 -0500730 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
731 0)
732 entry->func = KEXEC_KERNEL_CHECK;
733 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
734 == 0)
735 entry->func = KEXEC_INITRAMFS_CHECK;
Mimi Zohar19f8a842016-01-15 10:17:12 -0500736 else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
737 entry->func = POLICY_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500738 else
739 result = -EINVAL;
740 if (!result)
741 entry->flags |= IMA_FUNC;
742 break;
743 case Opt_mask:
Eric Paris2f1506c2010-04-20 10:21:30 -0400744 ima_log_string(ab, "mask", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400745
746 if (entry->mask)
747 result = -EINVAL;
748
Mimi Zohar4351c292014-11-05 07:53:55 -0500749 from = args[0].from;
750 if (*from == '^')
751 from++;
752
753 if ((strcmp(from, "MAY_EXEC")) == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500754 entry->mask = MAY_EXEC;
Mimi Zohar4351c292014-11-05 07:53:55 -0500755 else if (strcmp(from, "MAY_WRITE") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500756 entry->mask = MAY_WRITE;
Mimi Zohar4351c292014-11-05 07:53:55 -0500757 else if (strcmp(from, "MAY_READ") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500758 entry->mask = MAY_READ;
Mimi Zohar4351c292014-11-05 07:53:55 -0500759 else if (strcmp(from, "MAY_APPEND") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -0500760 entry->mask = MAY_APPEND;
761 else
762 result = -EINVAL;
763 if (!result)
Mimi Zohar4351c292014-11-05 07:53:55 -0500764 entry->flags |= (*args[0].from == '^')
765 ? IMA_INMASK : IMA_MASK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500766 break;
767 case Opt_fsmagic:
Eric Paris2f1506c2010-04-20 10:21:30 -0400768 ima_log_string(ab, "fsmagic", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400769
770 if (entry->fsmagic) {
771 result = -EINVAL;
772 break;
773 }
774
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200775 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
Mimi Zohar4af46622009-02-04 09:07:00 -0500776 if (!result)
777 entry->flags |= IMA_FSMAGIC;
778 break;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300779 case Opt_fsuuid:
780 ima_log_string(ab, "fsuuid", args[0].from);
781
Mike Rapoport36447452018-01-17 20:27:11 +0200782 if (!uuid_is_null(&entry->fsuuid)) {
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300783 result = -EINVAL;
784 break;
785 }
786
Christoph Hellwig787d8c52017-06-01 07:00:26 +0200787 result = uuid_parse(args[0].from, &entry->fsuuid);
Mimi Zohar446d64e2013-02-24 23:42:37 -0500788 if (!result)
789 entry->flags |= IMA_FSUUID;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300790 break;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300791 case Opt_uid_gt:
792 case Opt_euid_gt:
793 entry->uid_op = &uid_gt;
794 case Opt_uid_lt:
795 case Opt_euid_lt:
796 if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
797 entry->uid_op = &uid_lt;
798 case Opt_uid_eq:
799 case Opt_euid_eq:
800 uid_token = (token == Opt_uid_eq) ||
801 (token == Opt_uid_gt) ||
802 (token == Opt_uid_lt);
803
804 ima_log_string_op(ab, uid_token ? "uid" : "euid",
805 args[0].from, entry->uid_op);
Eric Paris7b62e162010-04-20 10:21:01 -0400806
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600807 if (uid_valid(entry->uid)) {
Eric Paris7b62e162010-04-20 10:21:01 -0400808 result = -EINVAL;
809 break;
810 }
811
Jingoo Han29707b22014-02-05 15:13:14 +0900812 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar4af46622009-02-04 09:07:00 -0500813 if (!result) {
Mimi Zohar139069e2014-11-05 07:48:36 -0500814 entry->uid = make_kuid(current_user_ns(),
815 (uid_t) lnum);
816 if (!uid_valid(entry->uid) ||
817 (uid_t)lnum != lnum)
Mimi Zohar4af46622009-02-04 09:07:00 -0500818 result = -EINVAL;
819 else
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300820 entry->flags |= uid_token
Mimi Zohar139069e2014-11-05 07:48:36 -0500821 ? IMA_UID : IMA_EUID;
Mimi Zohar4af46622009-02-04 09:07:00 -0500822 }
823 break;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300824 case Opt_fowner_gt:
825 entry->fowner_op = &uid_gt;
826 case Opt_fowner_lt:
827 if (token == Opt_fowner_lt)
828 entry->fowner_op = &uid_lt;
829 case Opt_fowner_eq:
830 ima_log_string_op(ab, "fowner", args[0].from,
831 entry->fowner_op);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500832
Linus Torvalds882653222012-10-02 21:38:48 -0700833 if (uid_valid(entry->fowner)) {
Mimi Zohar07f6a792011-03-09 22:25:48 -0500834 result = -EINVAL;
835 break;
836 }
837
Jingoo Han29707b22014-02-05 15:13:14 +0900838 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500839 if (!result) {
Linus Torvalds882653222012-10-02 21:38:48 -0700840 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
841 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500842 result = -EINVAL;
843 else
844 entry->flags |= IMA_FOWNER;
845 }
846 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500847 case Opt_obj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400848 ima_log_string(ab, "obj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500849 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500850 LSM_OBJ_USER,
851 AUDIT_OBJ_USER);
852 break;
853 case Opt_obj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400854 ima_log_string(ab, "obj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500855 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500856 LSM_OBJ_ROLE,
857 AUDIT_OBJ_ROLE);
858 break;
859 case Opt_obj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400860 ima_log_string(ab, "obj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500861 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500862 LSM_OBJ_TYPE,
863 AUDIT_OBJ_TYPE);
864 break;
865 case Opt_subj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400866 ima_log_string(ab, "subj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500867 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500868 LSM_SUBJ_USER,
869 AUDIT_SUBJ_USER);
870 break;
871 case Opt_subj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400872 ima_log_string(ab, "subj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500873 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500874 LSM_SUBJ_ROLE,
875 AUDIT_SUBJ_ROLE);
876 break;
877 case Opt_subj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400878 ima_log_string(ab, "subj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500879 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500880 LSM_SUBJ_TYPE,
881 AUDIT_SUBJ_TYPE);
882 break;
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300883 case Opt_appraise_type:
884 if (entry->action != APPRAISE) {
885 result = -EINVAL;
886 break;
887 }
888
889 ima_log_string(ab, "appraise_type", args[0].from);
890 if ((strcmp(args[0].from, "imasig")) == 0)
891 entry->flags |= IMA_DIGSIG_REQUIRED;
892 else
893 result = -EINVAL;
894 break;
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400895 case Opt_permit_directio:
896 entry->flags |= IMA_PERMIT_DIRECTIO;
897 break;
Eric Richter02606432016-06-01 13:14:01 -0500898 case Opt_pcr:
899 if (entry->action != MEASURE) {
900 result = -EINVAL;
901 break;
902 }
903 ima_log_string(ab, "pcr", args[0].from);
904
905 result = kstrtoint(args[0].from, 10, &entry->pcr);
906 if (result || INVALID_PCR(entry->pcr))
907 result = -EINVAL;
908 else
909 entry->flags |= IMA_PCR;
910
911 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500912 case Opt_err:
Eric Paris2f1506c2010-04-20 10:21:30 -0400913 ima_log_string(ab, "UNKNOWN", p);
Eric Parise9d393b2010-04-20 10:21:13 -0400914 result = -EINVAL;
Mimi Zohar4af46622009-02-04 09:07:00 -0500915 break;
916 }
917 }
Eric Paris7b62e162010-04-20 10:21:01 -0400918 if (!result && (entry->action == UNKNOWN))
Mimi Zohar4af46622009-02-04 09:07:00 -0500919 result = -EINVAL;
Mimi Zohara7f2a362012-12-21 08:34:21 -0500920 else if (entry->func == MODULE_CHECK)
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500921 temp_ima_appraise |= IMA_APPRAISE_MODULES;
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400922 else if (entry->func == FIRMWARE_CHECK)
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500923 temp_ima_appraise |= IMA_APPRAISE_FIRMWARE;
Mimi Zohar19f8a842016-01-15 10:17:12 -0500924 else if (entry->func == POLICY_CHECK)
925 temp_ima_appraise |= IMA_APPRAISE_POLICY;
Eric Parisb0d5de42012-02-14 17:11:07 -0500926 audit_log_format(ab, "res=%d", !result);
Mimi Zohar4af46622009-02-04 09:07:00 -0500927 audit_log_end(ab);
928 return result;
929}
930
931/**
Mimi Zohar07f6a792011-03-09 22:25:48 -0500932 * ima_parse_add_rule - add a rule to ima_policy_rules
Mimi Zohar4af46622009-02-04 09:07:00 -0500933 * @rule - ima measurement policy rule
934 *
Petko Manolov38d859f2015-12-02 17:47:54 +0200935 * Avoid locking by allowing just one writer at a time in ima_write_policy()
Eric Paris6ccd0452010-04-20 10:20:54 -0400936 * Returns the length of the rule parsed, an error code on failure
Mimi Zohar4af46622009-02-04 09:07:00 -0500937 */
Eric Paris6ccd0452010-04-20 10:20:54 -0400938ssize_t ima_parse_add_rule(char *rule)
Mimi Zohar4af46622009-02-04 09:07:00 -0500939{
Mimi Zohar52a13282013-12-11 14:44:04 -0500940 static const char op[] = "update_policy";
Eric Paris6ccd0452010-04-20 10:20:54 -0400941 char *p;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500942 struct ima_rule_entry *entry;
Eric Paris6ccd0452010-04-20 10:20:54 -0400943 ssize_t result, len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500944 int audit_info = 0;
945
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300946 p = strsep(&rule, "\n");
947 len = strlen(p) + 1;
Dmitry Kasatkin71787842014-10-03 14:40:20 +0300948 p += strspn(p, " \t");
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300949
Dmitry Kasatkin71787842014-10-03 14:40:20 +0300950 if (*p == '#' || *p == '\0')
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +0300951 return len;
952
Mimi Zohar4af46622009-02-04 09:07:00 -0500953 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
954 if (!entry) {
955 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
956 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
957 return -ENOMEM;
958 }
959
960 INIT_LIST_HEAD(&entry->list);
961
Eric Paris6ccd0452010-04-20 10:20:54 -0400962 result = ima_parse_rule(p, entry);
Eric Paris7233e3e2010-04-20 10:21:24 -0400963 if (result) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500964 kfree(entry);
Mimi Zohar523979a2009-02-11 11:12:28 -0500965 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
Richard Guy Briggs7e9001f2014-06-16 15:52:07 -0400966 NULL, op, "invalid-policy", result,
Mimi Zohar523979a2009-02-11 11:12:28 -0500967 audit_info);
Eric Paris7233e3e2010-04-20 10:21:24 -0400968 return result;
Mimi Zohar523979a2009-02-11 11:12:28 -0500969 }
Eric Paris7233e3e2010-04-20 10:21:24 -0400970
Petko Manolov38d859f2015-12-02 17:47:54 +0200971 list_add_tail(&entry->list, &ima_temp_rules);
Eric Paris7233e3e2010-04-20 10:21:24 -0400972
973 return len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500974}
975
Petko Manolov38d859f2015-12-02 17:47:54 +0200976/**
977 * ima_delete_rules() called to cleanup invalid in-flight policy.
978 * We don't need locking as we operate on the temp list, which is
979 * different from the active one. There is also only one user of
980 * ima_delete_rules() at a time.
981 */
James Morris64c61d82009-02-05 09:28:26 +1100982void ima_delete_rules(void)
Mimi Zohar4af46622009-02-04 09:07:00 -0500983{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500984 struct ima_rule_entry *entry, *tmp;
Mimi Zohar7163a992013-01-03 14:19:09 -0500985 int i;
Mimi Zohar4af46622009-02-04 09:07:00 -0500986
Mimi Zohar6ad6afa2015-12-07 14:35:47 -0500987 temp_ima_appraise = 0;
Petko Manolov38d859f2015-12-02 17:47:54 +0200988 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
Mimi Zohar7163a992013-01-03 14:19:09 -0500989 for (i = 0; i < MAX_LSM_RULES; i++)
990 kfree(entry->lsm[i].args_p);
991
Mimi Zohar4af46622009-02-04 09:07:00 -0500992 list_del(&entry->list);
993 kfree(entry);
994 }
Mimi Zohar4af46622009-02-04 09:07:00 -0500995}
Petko Manolov80eae202015-12-02 17:47:56 +0200996
997#ifdef CONFIG_IMA_READ_POLICY
998enum {
999 mask_exec = 0, mask_write, mask_read, mask_append
1000};
1001
Thiago Jung Bauermannbb543e32017-06-07 22:49:10 -03001002static const char *const mask_tokens[] = {
Petko Manolov80eae202015-12-02 17:47:56 +02001003 "MAY_EXEC",
1004 "MAY_WRITE",
1005 "MAY_READ",
1006 "MAY_APPEND"
1007};
1008
Thiago Jung Bauermann26632182017-06-07 22:49:11 -03001009#define __ima_hook_stringify(str) (#str),
Petko Manolov80eae202015-12-02 17:47:56 +02001010
Thiago Jung Bauermannbb543e32017-06-07 22:49:10 -03001011static const char *const func_tokens[] = {
Thiago Jung Bauermann26632182017-06-07 22:49:11 -03001012 __ima_hooks(__ima_hook_stringify)
Petko Manolov80eae202015-12-02 17:47:56 +02001013};
1014
1015void *ima_policy_start(struct seq_file *m, loff_t *pos)
1016{
1017 loff_t l = *pos;
1018 struct ima_rule_entry *entry;
1019
1020 rcu_read_lock();
1021 list_for_each_entry_rcu(entry, ima_rules, list) {
1022 if (!l--) {
1023 rcu_read_unlock();
1024 return entry;
1025 }
1026 }
1027 rcu_read_unlock();
1028 return NULL;
1029}
1030
1031void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
1032{
1033 struct ima_rule_entry *entry = v;
1034
1035 rcu_read_lock();
1036 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
1037 rcu_read_unlock();
1038 (*pos)++;
1039
1040 return (&entry->list == ima_rules) ? NULL : entry;
1041}
1042
1043void ima_policy_stop(struct seq_file *m, void *v)
1044{
1045}
1046
1047#define pt(token) policy_tokens[token + Opt_err].pattern
1048#define mt(token) mask_tokens[token]
Petko Manolov80eae202015-12-02 17:47:56 +02001049
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001050/*
1051 * policy_func_show - display the ima_hooks policy rule
1052 */
1053static void policy_func_show(struct seq_file *m, enum ima_hooks func)
1054{
Thiago Jung Bauermann26632182017-06-07 22:49:11 -03001055 if (func > 0 && func < MAX_CHECK)
1056 seq_printf(m, "func=%s ", func_tokens[func]);
1057 else
1058 seq_printf(m, "func=%d ", func);
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001059}
1060
Petko Manolov80eae202015-12-02 17:47:56 +02001061int ima_policy_show(struct seq_file *m, void *v)
1062{
1063 struct ima_rule_entry *entry = v;
Andy Shevchenkob8b57272016-05-20 17:00:57 -07001064 int i;
Petko Manolov80eae202015-12-02 17:47:56 +02001065 char tbuf[64] = {0,};
1066
1067 rcu_read_lock();
1068
1069 if (entry->action & MEASURE)
1070 seq_puts(m, pt(Opt_measure));
1071 if (entry->action & DONT_MEASURE)
1072 seq_puts(m, pt(Opt_dont_measure));
1073 if (entry->action & APPRAISE)
1074 seq_puts(m, pt(Opt_appraise));
1075 if (entry->action & DONT_APPRAISE)
1076 seq_puts(m, pt(Opt_dont_appraise));
1077 if (entry->action & AUDIT)
1078 seq_puts(m, pt(Opt_audit));
Mimi Zoharda1b0022016-09-29 10:04:52 -04001079 if (entry->action & HASH)
1080 seq_puts(m, pt(Opt_hash));
1081 if (entry->action & DONT_HASH)
1082 seq_puts(m, pt(Opt_dont_hash));
Petko Manolov80eae202015-12-02 17:47:56 +02001083
1084 seq_puts(m, " ");
1085
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001086 if (entry->flags & IMA_FUNC)
1087 policy_func_show(m, entry->func);
Petko Manolov80eae202015-12-02 17:47:56 +02001088
1089 if (entry->flags & IMA_MASK) {
1090 if (entry->mask & MAY_EXEC)
1091 seq_printf(m, pt(Opt_mask), mt(mask_exec));
1092 if (entry->mask & MAY_WRITE)
1093 seq_printf(m, pt(Opt_mask), mt(mask_write));
1094 if (entry->mask & MAY_READ)
1095 seq_printf(m, pt(Opt_mask), mt(mask_read));
1096 if (entry->mask & MAY_APPEND)
1097 seq_printf(m, pt(Opt_mask), mt(mask_append));
1098 seq_puts(m, " ");
1099 }
1100
1101 if (entry->flags & IMA_FSMAGIC) {
1102 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
1103 seq_printf(m, pt(Opt_fsmagic), tbuf);
1104 seq_puts(m, " ");
1105 }
1106
Eric Richter02606432016-06-01 13:14:01 -05001107 if (entry->flags & IMA_PCR) {
1108 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
1109 seq_printf(m, pt(Opt_pcr), tbuf);
1110 seq_puts(m, " ");
1111 }
1112
Petko Manolov80eae202015-12-02 17:47:56 +02001113 if (entry->flags & IMA_FSUUID) {
Christoph Hellwig787d8c52017-06-01 07:00:26 +02001114 seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
Petko Manolov80eae202015-12-02 17:47:56 +02001115 seq_puts(m, " ");
1116 }
1117
1118 if (entry->flags & IMA_UID) {
1119 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001120 if (entry->uid_op == &uid_gt)
1121 seq_printf(m, pt(Opt_uid_gt), tbuf);
1122 else if (entry->uid_op == &uid_lt)
1123 seq_printf(m, pt(Opt_uid_lt), tbuf);
1124 else
1125 seq_printf(m, pt(Opt_uid_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02001126 seq_puts(m, " ");
1127 }
1128
1129 if (entry->flags & IMA_EUID) {
1130 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001131 if (entry->uid_op == &uid_gt)
1132 seq_printf(m, pt(Opt_euid_gt), tbuf);
1133 else if (entry->uid_op == &uid_lt)
1134 seq_printf(m, pt(Opt_euid_lt), tbuf);
1135 else
1136 seq_printf(m, pt(Opt_euid_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02001137 seq_puts(m, " ");
1138 }
1139
1140 if (entry->flags & IMA_FOWNER) {
1141 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001142 if (entry->fowner_op == &uid_gt)
1143 seq_printf(m, pt(Opt_fowner_gt), tbuf);
1144 else if (entry->fowner_op == &uid_lt)
1145 seq_printf(m, pt(Opt_fowner_lt), tbuf);
1146 else
1147 seq_printf(m, pt(Opt_fowner_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02001148 seq_puts(m, " ");
1149 }
1150
1151 for (i = 0; i < MAX_LSM_RULES; i++) {
1152 if (entry->lsm[i].rule) {
1153 switch (i) {
1154 case LSM_OBJ_USER:
1155 seq_printf(m, pt(Opt_obj_user),
1156 (char *)entry->lsm[i].args_p);
1157 break;
1158 case LSM_OBJ_ROLE:
1159 seq_printf(m, pt(Opt_obj_role),
1160 (char *)entry->lsm[i].args_p);
1161 break;
1162 case LSM_OBJ_TYPE:
1163 seq_printf(m, pt(Opt_obj_type),
1164 (char *)entry->lsm[i].args_p);
1165 break;
1166 case LSM_SUBJ_USER:
1167 seq_printf(m, pt(Opt_subj_user),
1168 (char *)entry->lsm[i].args_p);
1169 break;
1170 case LSM_SUBJ_ROLE:
1171 seq_printf(m, pt(Opt_subj_role),
1172 (char *)entry->lsm[i].args_p);
1173 break;
1174 case LSM_SUBJ_TYPE:
1175 seq_printf(m, pt(Opt_subj_type),
1176 (char *)entry->lsm[i].args_p);
1177 break;
1178 }
1179 }
1180 }
1181 if (entry->flags & IMA_DIGSIG_REQUIRED)
1182 seq_puts(m, "appraise_type=imasig ");
1183 if (entry->flags & IMA_PERMIT_DIRECTIO)
1184 seq_puts(m, "permit_directio ");
1185 rcu_read_unlock();
1186 seq_puts(m, "\n");
1187 return 0;
1188}
1189#endif /* CONFIG_IMA_READ_POLICY */