blob: 2a1f6418b10a62f93b8100434eafd100419e2588 [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Mimi Zohar3323eec2009-02-04 09:06:58 -05002/*
3 * Copyright (C) 2008 IBM Corporation
4 * Author: Mimi Zohar <zohar@us.ibm.com>
5 *
Mimi Zohar3323eec2009-02-04 09:06:58 -05006 * ima_policy.c
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +02007 * - initialize default measure policy rules
Mimi Zohar3323eec2009-02-04 09:06:58 -05008 */
Thiago Jung Bauermann3878d502019-06-27 23:19:32 -03009
Paul Gortmaker876979c2018-12-09 15:36:29 -050010#include <linux/init.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050011#include <linux/list.h>
Scott Brandenb89999d02020-10-02 10:38:15 -070012#include <linux/kernel_read_file.h>
Mimi Zoharcf222212016-01-14 17:57:47 -050013#include <linux/fs.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050014#include <linux/security.h>
15#include <linux/magic.h>
Mimi Zohar4af46622009-02-04 09:07:00 -050016#include <linux/parser.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Petko Manolov38d859f2015-12-02 17:47:54 +020018#include <linux/rculist.h>
Dmitry Kasatkin85865c12012-09-03 23:23:13 +030019#include <linux/genhd.h>
Petko Manolov80eae202015-12-02 17:47:56 +020020#include <linux/seq_file.h>
Nayna Jain61917062018-10-09 23:00:36 +053021#include <linux/ima.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050022
23#include "ima.h"
24
25/* flags definitions */
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020026#define IMA_FUNC 0x0001
27#define IMA_MASK 0x0002
Mimi Zohar3323eec2009-02-04 09:06:58 -050028#define IMA_FSMAGIC 0x0004
29#define IMA_UID 0x0008
Mimi Zohar07f6a792011-03-09 22:25:48 -050030#define IMA_FOWNER 0x0010
Dmitry Kasatkin85865c12012-09-03 23:23:13 +030031#define IMA_FSUUID 0x0020
Mimi Zohar4351c292014-11-05 07:53:55 -050032#define IMA_INMASK 0x0040
Mimi Zohar139069e2014-11-05 07:48:36 -050033#define IMA_EUID 0x0080
Eric Richter02606432016-06-01 13:14:01 -050034#define IMA_PCR 0x0100
Mimi Zoharf1b08bb2018-01-15 11:20:36 -050035#define IMA_FSNAME 0x0200
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -080036#define IMA_KEYRINGS 0x0400
Tushar Sugandhi47d76a42021-01-07 20:07:05 -080037#define IMA_LABEL 0x0800
THOBY Simon1624dc02021-08-16 08:11:00 +000038#define IMA_VALIDATE_ALGOS 0x1000
Curtis Veit40224c42021-10-07 14:03:02 -060039#define IMA_GID 0x2000
40#define IMA_EGID 0x4000
41#define IMA_FGROUP 0x8000
Mimi Zohar3323eec2009-02-04 09:06:58 -050042
Dmitry Kasatkin45e24722012-09-12 20:51:32 +030043#define UNKNOWN 0
44#define MEASURE 0x0001 /* same as IMA_MEASURE */
45#define DONT_MEASURE 0x0002
46#define APPRAISE 0x0004 /* same as IMA_APPRAISE */
47#define DONT_APPRAISE 0x0008
Peter Moodye7c568e2012-06-14 10:04:36 -070048#define AUDIT 0x0040
Mimi Zoharda1b0022016-09-29 10:04:52 -040049#define HASH 0x0100
50#define DONT_HASH 0x0200
Mimi Zohar4af46622009-02-04 09:07:00 -050051
Eric Richter02606432016-06-01 13:14:01 -050052#define INVALID_PCR(a) (((a) < 0) || \
Pankaj Bharadiyac5936422019-12-09 10:31:43 -080053 (a) >= (sizeof_field(struct integrity_iint_cache, measured_pcrs) * 8))
Eric Richter02606432016-06-01 13:14:01 -050054
Roberto Sassua7560242014-09-12 19:35:54 +020055int ima_policy_flag;
Mimi Zohar6ad6afa2015-12-07 14:35:47 -050056static int temp_ima_appraise;
Mimi Zoharef968372018-07-13 14:06:01 -040057static int build_ima_appraise __ro_after_init;
Roberto Sassua7560242014-09-12 19:35:54 +020058
THOBY Simon4f2946a2021-08-16 08:11:01 +000059atomic_t ima_setxattr_allowed_hash_algorithms;
60
Mimi Zohar4af46622009-02-04 09:07:00 -050061#define MAX_LSM_RULES 6
62enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
63 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
64};
Mimi Zohar3323eec2009-02-04 09:06:58 -050065
Mimi Zohar24fd03c2015-06-11 20:48:33 -040066enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB };
67
Nayna Jainc52657d2018-10-09 23:00:35 +053068enum policy_rule_list { IMA_DEFAULT_POLICY = 1, IMA_CUSTOM_POLICY };
69
Tyler Hicks176377d2020-08-11 14:26:20 -050070struct ima_rule_opt_list {
71 size_t count;
72 char *items[];
73};
74
Mimi Zohar07f6a792011-03-09 22:25:48 -050075struct ima_rule_entry {
Mimi Zohar3323eec2009-02-04 09:06:58 -050076 struct list_head list;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050077 int action;
Mimi Zohar3323eec2009-02-04 09:06:58 -050078 unsigned int flags;
79 enum ima_hooks func;
80 int mask;
81 unsigned long fsmagic;
Christoph Hellwig787d8c52017-06-01 07:00:26 +020082 uuid_t fsuuid;
Eric W. Biederman8b94eea2012-05-25 18:24:12 -060083 kuid_t uid;
Curtis Veit40224c42021-10-07 14:03:02 -060084 kgid_t gid;
Linus Torvalds882653222012-10-02 21:38:48 -070085 kuid_t fowner;
Curtis Veit40224c42021-10-07 14:03:02 -060086 kgid_t fgroup;
Alex Henrie30d87642021-10-07 14:03:01 -060087 bool (*uid_op)(kuid_t cred_uid, kuid_t rule_uid); /* Handlers for operators */
Curtis Veit40224c42021-10-07 14:03:02 -060088 bool (*gid_op)(kgid_t cred_gid, kgid_t rule_gid);
Alex Henrie30d87642021-10-07 14:03:01 -060089 bool (*fowner_op)(kuid_t cred_uid, kuid_t rule_uid); /* uid_eq(), uid_gt(), uid_lt() */
Curtis Veit40224c42021-10-07 14:03:02 -060090 bool (*fgroup_op)(kgid_t cred_gid, kgid_t rule_gid); /* gid_eq(), gid_gt(), gid_lt() */
Eric Richter02606432016-06-01 13:14:01 -050091 int pcr;
THOBY Simon1624dc02021-08-16 08:11:00 +000092 unsigned int allowed_algos; /* bitfield of allowed hash algorithms */
Mimi Zohar4af46622009-02-04 09:07:00 -050093 struct {
94 void *rule; /* LSM file metadata specific */
Tyler Hicksaa0c0222020-07-09 01:19:08 -050095 char *args_p; /* audit value */
Mimi Zohar4af46622009-02-04 09:07:00 -050096 int type; /* audit type */
97 } lsm[MAX_LSM_RULES];
Mimi Zoharf1b08bb2018-01-15 11:20:36 -050098 char *fsname;
Tyler Hicks176377d2020-08-11 14:26:20 -050099 struct ima_rule_opt_list *keyrings; /* Measure keys added to these keyrings */
Tushar Sugandhi47d76a42021-01-07 20:07:05 -0800100 struct ima_rule_opt_list *label; /* Measure data grouped under this label */
Matthew Garrett19453ce02019-06-19 15:46:11 -0700101 struct ima_template_desc *template;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500102};
103
Eric Paris5789ba32009-05-21 15:47:06 -0400104/*
THOBY Simon1624dc02021-08-16 08:11:00 +0000105 * sanity check in case the kernels gains more hash algorithms that can
106 * fit in an unsigned int
107 */
108static_assert(
109 8 * sizeof(unsigned int) >= HASH_ALGO__LAST,
110 "The bitfield allowed_algos in ima_rule_entry is too small to contain all the supported hash algorithms, consider using a bigger type");
111
112/*
Eric Paris5789ba32009-05-21 15:47:06 -0400113 * Without LSM specific knowledge, the default policy can only be
Curtis Veit40224c42021-10-07 14:03:02 -0600114 * written in terms of .action, .func, .mask, .fsmagic, .uid, .gid,
115 * .fowner, and .fgroup
Mimi Zohar4af46622009-02-04 09:07:00 -0500116 */
Eric Paris5789ba32009-05-21 15:47:06 -0400117
118/*
119 * The minimum rule set to allow for full TCB coverage. Measures all files
120 * opened or mmap for exec and everything read by root. Dangerous because
121 * normal users can easily run the machine out of memory simply building
122 * and running executables.
123 */
James Morrisbad44172017-02-13 16:34:35 +1100124static struct ima_rule_entry dont_measure_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200125 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
126 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
127 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
128 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
129 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
130 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
131 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
132 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
Martin Townsend1c070b12018-03-22 13:46:01 +0000133 {.action = DONT_MEASURE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
Roberto Sassu6438de92015-04-11 17:13:06 +0200134 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
135 .flags = IMA_FSMAGIC},
Laura Abbott82e3bb42017-05-09 11:25:27 -0700136 {.action = DONT_MEASURE, .fsmagic = CGROUP2_SUPER_MAGIC,
137 .flags = IMA_FSMAGIC},
Mimi Zohar060190f2018-11-14 17:24:13 -0500138 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
139 {.action = DONT_MEASURE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC}
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400140};
141
James Morrisbad44172017-02-13 16:34:35 +1100142static struct ima_rule_entry original_measurement_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200143 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500144 .flags = IMA_FUNC | IMA_MASK},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200145 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500146 .flags = IMA_FUNC | IMA_MASK},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400147 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300148 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
149 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400150 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
151 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
152};
153
James Morrisbad44172017-02-13 16:34:35 +1100154static struct ima_rule_entry default_measurement_rules[] __ro_after_init = {
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400155 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
156 .flags = IMA_FUNC | IMA_MASK},
157 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
158 .flags = IMA_FUNC | IMA_MASK},
159 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300160 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
161 .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400162 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300163 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
164 .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200165 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400166 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
Mimi Zohar19f8a842016-01-15 10:17:12 -0500167 {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
Mimi Zohar3323eec2009-02-04 09:06:58 -0500168};
169
James Morrisbad44172017-02-13 16:34:35 +1100170static struct ima_rule_entry default_appraise_rules[] __ro_after_init = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200171 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
172 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
173 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
174 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
175 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
176 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
177 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
178 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
179 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
Martin Townsend1c070b12018-03-22 13:46:01 +0000180 {.action = DONT_APPRAISE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zoharcd025f72015-04-21 16:54:24 -0400181 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zohar060190f2018-11-14 17:24:13 -0500182 {.action = DONT_APPRAISE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200183 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
Laura Abbott82e3bb42017-05-09 11:25:27 -0700184 {.action = DONT_APPRAISE, .fsmagic = CGROUP2_SUPER_MAGIC, .flags = IMA_FSMAGIC},
Mimi Zohar95ee08f2015-12-07 15:08:01 -0500185#ifdef CONFIG_IMA_WRITE_POLICY
186 {.action = APPRAISE, .func = POLICY_CHECK,
187 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
188#endif
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200189#ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300190 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
191 .flags = IMA_FOWNER},
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200192#else
193 /* force signature */
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300194 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
Dmitry Kasatkinc57782c2014-11-05 17:01:16 +0200195 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
196#endif
Mimi Zohar07f6a792011-03-09 22:25:48 -0500197};
Mimi Zohar3323eec2009-02-04 09:06:58 -0500198
Mimi Zoharef968372018-07-13 14:06:01 -0400199static struct ima_rule_entry build_appraise_rules[] __ro_after_init = {
200#ifdef CONFIG_IMA_APPRAISE_REQUIRE_MODULE_SIGS
201 {.action = APPRAISE, .func = MODULE_CHECK,
202 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
203#endif
204#ifdef CONFIG_IMA_APPRAISE_REQUIRE_FIRMWARE_SIGS
205 {.action = APPRAISE, .func = FIRMWARE_CHECK,
206 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
207#endif
208#ifdef CONFIG_IMA_APPRAISE_REQUIRE_KEXEC_SIGS
209 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
210 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
211#endif
212#ifdef CONFIG_IMA_APPRAISE_REQUIRE_POLICY_SIGS
213 {.action = APPRAISE, .func = POLICY_CHECK,
214 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
215#endif
216};
217
Mimi Zohar503ceae2017-04-21 18:58:27 -0400218static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
219 {.action = APPRAISE, .func = MODULE_CHECK,
220 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
221 {.action = APPRAISE, .func = FIRMWARE_CHECK,
222 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
223 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
224 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
225 {.action = APPRAISE, .func = POLICY_CHECK,
226 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
227};
228
Lakshmi Ramasubramanian03cee162021-01-07 20:07:07 -0800229static struct ima_rule_entry critical_data_rules[] __ro_after_init = {
230 {.action = MEASURE, .func = CRITICAL_DATA, .flags = IMA_FUNC},
231};
232
Nayna Jain61917062018-10-09 23:00:36 +0530233/* An array of architecture specific rules */
YueHaibing68f25292019-06-11 21:40:32 +0800234static struct ima_rule_entry *arch_policy_entry __ro_after_init;
Nayna Jain61917062018-10-09 23:00:36 +0530235
Mimi Zohar07f6a792011-03-09 22:25:48 -0500236static LIST_HEAD(ima_default_rules);
237static LIST_HEAD(ima_policy_rules);
Petko Manolov38d859f2015-12-02 17:47:54 +0200238static LIST_HEAD(ima_temp_rules);
liqiongeb0782b2021-10-09 18:38:21 +0800239static struct list_head __rcu *ima_rules = (struct list_head __rcu *)(&ima_default_rules);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500240
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400241static int ima_policy __initdata;
Petko Manolov38d859f2015-12-02 17:47:54 +0200242
Mimi Zohar07f6a792011-03-09 22:25:48 -0500243static int __init default_measure_policy_setup(char *str)
Eric Paris5789ba32009-05-21 15:47:06 -0400244{
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400245 if (ima_policy)
246 return 1;
247
248 ima_policy = ORIGINAL_TCB;
Eric Paris5789ba32009-05-21 15:47:06 -0400249 return 1;
250}
Mimi Zohar07f6a792011-03-09 22:25:48 -0500251__setup("ima_tcb", default_measure_policy_setup);
252
Mimi Zohar33ce9542017-04-24 12:04:09 -0400253static bool ima_use_appraise_tcb __initdata;
Mimi Zohar503ceae2017-04-21 18:58:27 -0400254static bool ima_use_secure_boot __initdata;
Lakshmi Ramasubramanian03cee162021-01-07 20:07:07 -0800255static bool ima_use_critical_data __initdata;
Mimi Zohar9e670282018-02-21 11:36:32 -0500256static bool ima_fail_unverifiable_sigs __ro_after_init;
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400257static int __init policy_setup(char *str)
258{
Mimi Zohar33ce9542017-04-24 12:04:09 -0400259 char *p;
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400260
Mimi Zohar33ce9542017-04-24 12:04:09 -0400261 while ((p = strsep(&str, " |\n")) != NULL) {
262 if (*p == ' ')
263 continue;
264 if ((strcmp(p, "tcb") == 0) && !ima_policy)
265 ima_policy = DEFAULT_TCB;
266 else if (strcmp(p, "appraise_tcb") == 0)
Thomas Meyer39adb922017-10-07 16:02:21 +0200267 ima_use_appraise_tcb = true;
Mimi Zohar503ceae2017-04-21 18:58:27 -0400268 else if (strcmp(p, "secure_boot") == 0)
Thomas Meyer39adb922017-10-07 16:02:21 +0200269 ima_use_secure_boot = true;
Lakshmi Ramasubramanian03cee162021-01-07 20:07:07 -0800270 else if (strcmp(p, "critical_data") == 0)
271 ima_use_critical_data = true;
Mimi Zohar9e670282018-02-21 11:36:32 -0500272 else if (strcmp(p, "fail_securely") == 0)
273 ima_fail_unverifiable_sigs = true;
Bruno Meneguele7fe2bb72020-09-04 16:40:58 -0300274 else
275 pr_err("policy \"%s\" not found", p);
Mimi Zohar33ce9542017-04-24 12:04:09 -0400276 }
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400277
278 return 1;
279}
280__setup("ima_policy=", policy_setup);
281
Mimi Zohar07f6a792011-03-09 22:25:48 -0500282static int __init default_appraise_policy_setup(char *str)
283{
Thomas Meyer39adb922017-10-07 16:02:21 +0200284 ima_use_appraise_tcb = true;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500285 return 1;
286}
287__setup("ima_appraise_tcb", default_appraise_policy_setup);
Eric Paris5789ba32009-05-21 15:47:06 -0400288
Tyler Hicks176377d2020-08-11 14:26:20 -0500289static struct ima_rule_opt_list *ima_alloc_rule_opt_list(const substring_t *src)
290{
291 struct ima_rule_opt_list *opt_list;
292 size_t count = 0;
293 char *src_copy;
294 char *cur, *next;
295 size_t i;
296
297 src_copy = match_strdup(src);
298 if (!src_copy)
299 return ERR_PTR(-ENOMEM);
300
301 next = src_copy;
302 while ((cur = strsep(&next, "|"))) {
303 /* Don't accept an empty list item */
304 if (!(*cur)) {
305 kfree(src_copy);
306 return ERR_PTR(-EINVAL);
307 }
308 count++;
309 }
310
311 /* Don't accept an empty list */
312 if (!count) {
313 kfree(src_copy);
314 return ERR_PTR(-EINVAL);
315 }
316
317 opt_list = kzalloc(struct_size(opt_list, items, count), GFP_KERNEL);
318 if (!opt_list) {
319 kfree(src_copy);
320 return ERR_PTR(-ENOMEM);
321 }
322
323 /*
324 * strsep() has already replaced all instances of '|' with '\0',
325 * leaving a byte sequence of NUL-terminated strings. Reference each
326 * string with the array of items.
327 *
328 * IMPORTANT: Ownership of the allocated buffer is transferred from
329 * src_copy to the first element in the items array. To free the
330 * buffer, kfree() must only be called on the first element of the
331 * array.
332 */
333 for (i = 0, cur = src_copy; i < count; i++) {
334 opt_list->items[i] = cur;
335 cur = strchr(cur, '\0') + 1;
336 }
337 opt_list->count = count;
338
339 return opt_list;
340}
341
342static void ima_free_rule_opt_list(struct ima_rule_opt_list *opt_list)
343{
344 if (!opt_list)
345 return;
346
347 if (opt_list->count) {
348 kfree(opt_list->items[0]);
349 opt_list->count = 0;
350 }
351
352 kfree(opt_list);
353}
354
Janne Karhunenb1694242019-06-14 15:20:15 +0300355static void ima_lsm_free_rule(struct ima_rule_entry *entry)
356{
357 int i;
358
359 for (i = 0; i < MAX_LSM_RULES; i++) {
Tyler Hicksb8867ee2020-07-10 15:37:50 -0500360 ima_filter_rule_free(entry->lsm[i].rule);
Janne Karhunenb1694242019-06-14 15:20:15 +0300361 kfree(entry->lsm[i].args_p);
362 }
Tyler Hicks465aee72020-07-09 01:19:01 -0500363}
364
365static void ima_free_rule(struct ima_rule_entry *entry)
366{
367 if (!entry)
368 return;
369
370 /*
371 * entry->template->fields may be allocated in ima_parse_rule() but that
372 * reference is owned by the corresponding ima_template_desc element in
373 * the defined_templates list and cannot be freed here
374 */
375 kfree(entry->fsname);
Tyler Hicks176377d2020-08-11 14:26:20 -0500376 ima_free_rule_opt_list(entry->keyrings);
Tyler Hicks465aee72020-07-09 01:19:01 -0500377 ima_lsm_free_rule(entry);
Janne Karhunenb1694242019-06-14 15:20:15 +0300378 kfree(entry);
379}
380
381static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
382{
383 struct ima_rule_entry *nentry;
Janne Karhunen483ec262020-01-15 17:42:30 +0200384 int i;
Janne Karhunenb1694242019-06-14 15:20:15 +0300385
Janne Karhunenb1694242019-06-14 15:20:15 +0300386 /*
387 * Immutable elements are copied over as pointers and data; only
388 * lsm rules can change
389 */
Alex Dewarf60c8262020-09-09 20:09:06 +0100390 nentry = kmemdup(entry, sizeof(*nentry), GFP_KERNEL);
391 if (!nentry)
392 return NULL;
393
Pankaj Bharadiyac5936422019-12-09 10:31:43 -0800394 memset(nentry->lsm, 0, sizeof_field(struct ima_rule_entry, lsm));
Janne Karhunenb1694242019-06-14 15:20:15 +0300395
396 for (i = 0; i < MAX_LSM_RULES; i++) {
Janne Karhunen483ec262020-01-15 17:42:30 +0200397 if (!entry->lsm[i].args_p)
Janne Karhunenb1694242019-06-14 15:20:15 +0300398 continue;
399
400 nentry->lsm[i].type = entry->lsm[i].type;
Tyler Hicks39e59932020-07-09 01:19:07 -0500401 nentry->lsm[i].args_p = entry->lsm[i].args_p;
402 /*
403 * Remove the reference from entry so that the associated
404 * memory will not be freed during a later call to
405 * ima_lsm_free_rule(entry).
406 */
407 entry->lsm[i].args_p = NULL;
Janne Karhunenb1694242019-06-14 15:20:15 +0300408
Tyler Hicksb8867ee2020-07-10 15:37:50 -0500409 ima_filter_rule_init(nentry->lsm[i].type, Audit_equal,
410 nentry->lsm[i].args_p,
411 &nentry->lsm[i].rule);
Janne Karhunen483ec262020-01-15 17:42:30 +0200412 if (!nentry->lsm[i].rule)
413 pr_warn("rule for LSM \'%s\' is undefined\n",
Tyler Hicksaa0c0222020-07-09 01:19:08 -0500414 nentry->lsm[i].args_p);
Janne Karhunenb1694242019-06-14 15:20:15 +0300415 }
416 return nentry;
Janne Karhunenb1694242019-06-14 15:20:15 +0300417}
418
419static int ima_lsm_update_rule(struct ima_rule_entry *entry)
420{
421 struct ima_rule_entry *nentry;
422
423 nentry = ima_lsm_copy_rule(entry);
424 if (!nentry)
425 return -ENOMEM;
426
427 list_replace_rcu(&entry->list, &nentry->list);
428 synchronize_rcu();
Tyler Hicks465aee72020-07-09 01:19:01 -0500429 /*
430 * ima_lsm_copy_rule() shallow copied all references, except for the
431 * LSM references, from entry to nentry so we only want to free the LSM
432 * references and the entry itself. All other memory refrences will now
433 * be owned by nentry.
434 */
Janne Karhunenb1694242019-06-14 15:20:15 +0300435 ima_lsm_free_rule(entry);
Tyler Hicks465aee72020-07-09 01:19:01 -0500436 kfree(entry);
Janne Karhunenb1694242019-06-14 15:20:15 +0300437
438 return 0;
439}
440
Tyler Hicksdb2045f2020-07-09 01:19:04 -0500441static bool ima_rule_contains_lsm_cond(struct ima_rule_entry *entry)
442{
443 int i;
444
445 for (i = 0; i < MAX_LSM_RULES; i++)
446 if (entry->lsm[i].args_p)
447 return true;
448
449 return false;
450}
451
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200452/*
Petko Manolov38d859f2015-12-02 17:47:54 +0200453 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
454 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
Janne Karhunenb1694242019-06-14 15:20:15 +0300455 * the reloaded LSM policy.
Mimi Zohar7163a992013-01-03 14:19:09 -0500456 */
457static void ima_lsm_update_rules(void)
458{
Janne Karhunenb1694242019-06-14 15:20:15 +0300459 struct ima_rule_entry *entry, *e;
Tyler Hicks592b24c2020-07-09 01:19:10 -0500460 int result;
Mimi Zohar7163a992013-01-03 14:19:09 -0500461
Janne Karhunenb1694242019-06-14 15:20:15 +0300462 list_for_each_entry_safe(entry, e, &ima_policy_rules, list) {
Tyler Hicks592b24c2020-07-09 01:19:10 -0500463 if (!ima_rule_contains_lsm_cond(entry))
Janne Karhunenb1694242019-06-14 15:20:15 +0300464 continue;
465
466 result = ima_lsm_update_rule(entry);
467 if (result) {
Janne Karhunen483ec262020-01-15 17:42:30 +0200468 pr_err("lsm rule update error %d\n", result);
Janne Karhunenb1694242019-06-14 15:20:15 +0300469 return;
Mimi Zohar7163a992013-01-03 14:19:09 -0500470 }
471 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500472}
473
Janne Karhunenb1694242019-06-14 15:20:15 +0300474int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
475 void *lsm_data)
476{
477 if (event != LSM_POLICY_CHANGE)
478 return NOTIFY_DONE;
479
480 ima_lsm_update_rules();
481 return NOTIFY_OK;
482}
483
Mimi Zohar3323eec2009-02-04 09:06:58 -0500484/**
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800485 * ima_match_rule_data - determine whether func_data matches the policy rule
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800486 * @rule: a pointer to a rule
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800487 * @func_data: data to match against the measure rule data
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800488 * @cred: a pointer to a credentials structure for user validation
489 *
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800490 * Returns true if func_data matches one in the rule, false otherwise.
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800491 */
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800492static bool ima_match_rule_data(struct ima_rule_entry *rule,
493 const char *func_data,
494 const struct cred *cred)
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800495{
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800496 const struct ima_rule_opt_list *opt_list = NULL;
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800497 bool matched = false;
Tyler Hicks176377d2020-08-11 14:26:20 -0500498 size_t i;
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800499
500 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
501 return false;
502
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800503 switch (rule->func) {
504 case KEY_CHECK:
505 if (!rule->keyrings)
506 return true;
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800507
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800508 opt_list = rule->keyrings;
509 break;
Tushar Sugandhic4e43aa2021-01-07 20:07:04 -0800510 case CRITICAL_DATA:
Tushar Sugandhi47d76a42021-01-07 20:07:05 -0800511 if (!rule->label)
512 return true;
513
514 opt_list = rule->label;
515 break;
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800516 default:
517 return false;
518 }
519
520 if (!func_data)
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800521 return false;
522
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800523 for (i = 0; i < opt_list->count; i++) {
524 if (!strcmp(opt_list->items[i], func_data)) {
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800525 matched = true;
526 break;
527 }
528 }
529
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800530 return matched;
531}
532
533/**
Janne Karhunen483ec262020-01-15 17:42:30 +0200534 * ima_match_rules - determine whether an inode matches the policy rule.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500535 * @rule: a pointer to a rule
Christian Braunera2d23292021-01-21 14:19:45 +0100536 * @mnt_userns: user namespace of the mount the inode was found from
Mimi Zohar3323eec2009-02-04 09:06:58 -0500537 * @inode: a pointer to an inode
Matthew Garrettd906c102018-01-08 13:36:20 -0800538 * @cred: a pointer to a credentials structure for user validation
539 * @secid: the secid of the task to be validated
Mimi Zohar3323eec2009-02-04 09:06:58 -0500540 * @func: LIM hook identifier
541 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800542 * @func_data: func specific data, may be NULL
Mimi Zohar3323eec2009-02-04 09:06:58 -0500543 *
544 * Returns true on rule match, false on failure.
545 */
Christian Braunera2d23292021-01-21 14:19:45 +0100546static bool ima_match_rules(struct ima_rule_entry *rule,
547 struct user_namespace *mnt_userns,
548 struct inode *inode, const struct cred *cred,
549 u32 secid, enum ima_hooks func, int mask,
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800550 const char *func_data)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500551{
Mimi Zohar4af46622009-02-04 09:07:00 -0500552 int i;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500553
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200554 if ((rule->flags & IMA_FUNC) &&
555 (rule->func != func && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500556 return false;
Tushar Sugandhic4e43aa2021-01-07 20:07:04 -0800557
558 switch (func) {
559 case KEY_CHECK:
560 case CRITICAL_DATA:
561 return ((rule->func == func) &&
562 ima_match_rule_data(rule, func_data, cred));
563 default:
564 break;
565 }
566
Dmitry Kasatkin09b11482013-11-13 23:42:39 +0200567 if ((rule->flags & IMA_MASK) &&
568 (rule->mask != mask && func != POST_SETATTR))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500569 return false;
Mimi Zohar4351c292014-11-05 07:53:55 -0500570 if ((rule->flags & IMA_INMASK) &&
571 (!(rule->mask & mask) && func != POST_SETATTR))
572 return false;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500573 if ((rule->flags & IMA_FSMAGIC)
574 && rule->fsmagic != inode->i_sb->s_magic)
575 return false;
Mimi Zoharf1b08bb2018-01-15 11:20:36 -0500576 if ((rule->flags & IMA_FSNAME)
577 && strcmp(rule->fsname, inode->i_sb->s_type->name))
578 return false;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300579 if ((rule->flags & IMA_FSUUID) &&
Christoph Hellwig85787092017-05-10 15:06:33 +0200580 !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid))
Dmitry Kasatkin85865c12012-09-03 23:23:13 +0300581 return false;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300582 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500583 return false;
Mimi Zohar139069e2014-11-05 07:48:36 -0500584 if (rule->flags & IMA_EUID) {
585 if (has_capability_noaudit(current, CAP_SETUID)) {
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300586 if (!rule->uid_op(cred->euid, rule->uid)
587 && !rule->uid_op(cred->suid, rule->uid)
588 && !rule->uid_op(cred->uid, rule->uid))
Mimi Zohar139069e2014-11-05 07:48:36 -0500589 return false;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300590 } else if (!rule->uid_op(cred->euid, rule->uid))
Mimi Zohar139069e2014-11-05 07:48:36 -0500591 return false;
592 }
Curtis Veit40224c42021-10-07 14:03:02 -0600593 if ((rule->flags & IMA_GID) && !rule->gid_op(cred->gid, rule->gid))
594 return false;
595 if (rule->flags & IMA_EGID) {
596 if (has_capability_noaudit(current, CAP_SETGID)) {
597 if (!rule->gid_op(cred->egid, rule->gid)
598 && !rule->gid_op(cred->sgid, rule->gid)
599 && !rule->gid_op(cred->gid, rule->gid))
600 return false;
601 } else if (!rule->gid_op(cred->egid, rule->gid))
602 return false;
603 }
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +0300604 if ((rule->flags & IMA_FOWNER) &&
Christian Braunera2d23292021-01-21 14:19:45 +0100605 !rule->fowner_op(i_uid_into_mnt(mnt_userns, inode), rule->fowner))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500606 return false;
Curtis Veit40224c42021-10-07 14:03:02 -0600607 if ((rule->flags & IMA_FGROUP) &&
608 !rule->fgroup_op(i_gid_into_mnt(mnt_userns, inode), rule->fgroup))
609 return false;
Mimi Zohar4af46622009-02-04 09:07:00 -0500610 for (i = 0; i < MAX_LSM_RULES; i++) {
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400611 int rc = 0;
Matthew Garrettd906c102018-01-08 13:36:20 -0800612 u32 osid;
Mimi Zohar4af46622009-02-04 09:07:00 -0500613
Janne Karhunen483ec262020-01-15 17:42:30 +0200614 if (!rule->lsm[i].rule) {
615 if (!rule->lsm[i].args_p)
616 continue;
617 else
618 return false;
619 }
Mimi Zohar4af46622009-02-04 09:07:00 -0500620 switch (i) {
621 case LSM_OBJ_USER:
622 case LSM_OBJ_ROLE:
623 case LSM_OBJ_TYPE:
624 security_inode_getsecid(inode, &osid);
Tyler Hicksb8867ee2020-07-10 15:37:50 -0500625 rc = ima_filter_rule_match(osid, rule->lsm[i].type,
626 Audit_equal,
627 rule->lsm[i].rule);
Mimi Zohar4af46622009-02-04 09:07:00 -0500628 break;
629 case LSM_SUBJ_USER:
630 case LSM_SUBJ_ROLE:
631 case LSM_SUBJ_TYPE:
Tyler Hicksb8867ee2020-07-10 15:37:50 -0500632 rc = ima_filter_rule_match(secid, rule->lsm[i].type,
633 Audit_equal,
634 rule->lsm[i].rule);
Gustavo A. R. Silva28073eb2020-11-20 12:25:46 -0600635 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500636 default:
637 break;
638 }
639 if (!rc)
640 return false;
641 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500642 return true;
643}
644
Mimi Zohard79d72e2012-12-03 17:08:11 -0500645/*
646 * In addition to knowing that we need to appraise the file in general,
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500647 * we need to differentiate between calling hooks, for hook specific rules.
Mimi Zohard79d72e2012-12-03 17:08:11 -0500648 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500649static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500650{
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500651 if (!(rule->flags & IMA_FUNC))
652 return IMA_FILE_APPRAISE;
653
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200654 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500655 case MMAP_CHECK:
656 return IMA_MMAP_APPRAISE;
657 case BPRM_CHECK:
658 return IMA_BPRM_APPRAISE;
Matthew Garrettd906c102018-01-08 13:36:20 -0800659 case CREDS_CHECK:
660 return IMA_CREDS_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500661 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500662 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -0500663 return IMA_FILE_APPRAISE;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500664 case MODULE_CHECK ... MAX_CHECK - 1:
665 default:
666 return IMA_READ_APPRAISE;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500667 }
668}
669
Mimi Zohar3323eec2009-02-04 09:06:58 -0500670/**
671 * ima_match_policy - decision based on LSM and other conditions
Christian Braunera2d23292021-01-21 14:19:45 +0100672 * @mnt_userns: user namespace of the mount the inode was found from
Mimi Zohar3323eec2009-02-04 09:06:58 -0500673 * @inode: pointer to an inode for which the policy decision is being made
Matthew Garrettd906c102018-01-08 13:36:20 -0800674 * @cred: pointer to a credentials structure for which the policy decision is
675 * being made
676 * @secid: LSM secid of the task to be validated
Mimi Zohar3323eec2009-02-04 09:06:58 -0500677 * @func: IMA hook identifier
678 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
Eric Richter725de7f2016-06-01 13:14:02 -0500679 * @pcr: set the pcr to extend
Matthew Garrett19453ce02019-06-19 15:46:11 -0700680 * @template_desc: the template that should be used for this rule
Tushar Sugandhi2b4a2472021-01-07 20:07:01 -0800681 * @func_data: func specific data, may be NULL
THOBY Simon1624dc02021-08-16 08:11:00 +0000682 * @allowed_algos: allowlist of hash algorithms for the IMA xattr
Mimi Zohar3323eec2009-02-04 09:06:58 -0500683 *
684 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
685 * conditions.
686 *
Petko Manolov38d859f2015-12-02 17:47:54 +0200687 * Since the IMA policy may be updated multiple times we need to lock the
688 * list when walking it. Reads are many orders of magnitude more numerous
689 * than writes so ima_match_policy() is classical RCU candidate.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500690 */
Christian Braunera2d23292021-01-21 14:19:45 +0100691int ima_match_policy(struct user_namespace *mnt_userns, struct inode *inode,
692 const struct cred *cred, u32 secid, enum ima_hooks func,
693 int mask, int flags, int *pcr,
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800694 struct ima_template_desc **template_desc,
THOBY Simon1624dc02021-08-16 08:11:00 +0000695 const char *func_data, unsigned int *allowed_algos)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500696{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500697 struct ima_rule_entry *entry;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500698 int action = 0, actmask = flags | (flags << 1);
liqiongeb0782b2021-10-09 18:38:21 +0800699 struct list_head *ima_rules_tmp;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500700
Lakshmi Ramasubramaniandea87d02020-11-12 12:39:59 -0800701 if (template_desc && !*template_desc)
Mimi Zoharb36f2812019-07-19 07:16:57 -0400702 *template_desc = ima_template_desc_current();
703
Petko Manolov38d859f2015-12-02 17:47:54 +0200704 rcu_read_lock();
liqiongeb0782b2021-10-09 18:38:21 +0800705 ima_rules_tmp = rcu_dereference(ima_rules);
706 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500707
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500708 if (!(entry->action & actmask))
709 continue;
710
Christian Braunera2d23292021-01-21 14:19:45 +0100711 if (!ima_match_rules(entry, mnt_userns, inode, cred, secid,
Linus Torvalds7d6beb72021-02-23 13:39:45 -0800712 func, mask, func_data))
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500713 continue;
714
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300715 action |= entry->flags & IMA_ACTION_FLAGS;
716
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300717 action |= entry->action & IMA_DO_MASK;
Mimi Zoharda1b0022016-09-29 10:04:52 -0400718 if (entry->action & IMA_APPRAISE) {
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500719 action |= get_subaction(entry, func);
Mimi Zohara9a49352018-03-10 23:07:34 -0500720 action &= ~IMA_HASH;
Mimi Zohar9e670282018-02-21 11:36:32 -0500721 if (ima_fail_unverifiable_sigs)
722 action |= IMA_FAIL_UNVERIFIABLE_SIGS;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500723
THOBY Simon1624dc02021-08-16 08:11:00 +0000724 if (allowed_algos &&
725 entry->flags & IMA_VALIDATE_ALGOS)
726 *allowed_algos = entry->allowed_algos;
727 }
Mimi Zoharb36f2812019-07-19 07:16:57 -0400728
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300729 if (entry->action & IMA_DO_MASK)
730 actmask &= ~(entry->action | entry->action << 1);
731 else
732 actmask &= ~(entry->action | entry->action >> 1);
733
Eric Richter725de7f2016-06-01 13:14:02 -0500734 if ((pcr) && (entry->flags & IMA_PCR))
735 *pcr = entry->pcr;
736
Matthew Garrett19453ce02019-06-19 15:46:11 -0700737 if (template_desc && entry->template)
738 *template_desc = entry->template;
Matthew Garrett19453ce02019-06-19 15:46:11 -0700739
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500740 if (!actmask)
741 break;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500742 }
Petko Manolov38d859f2015-12-02 17:47:54 +0200743 rcu_read_unlock();
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500744
745 return action;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500746}
747
THOBY Simon4f2946a2021-08-16 08:11:01 +0000748/**
749 * ima_update_policy_flags() - Update global IMA variables
750 *
751 * Update ima_policy_flag and ima_setxattr_allowed_hash_algorithms
752 * based on the currently loaded policy.
753 *
754 * With ima_policy_flag, the decision to short circuit out of a function
755 * or not call the function in the first place can be made earlier.
756 *
757 * With ima_setxattr_allowed_hash_algorithms, the policy can restrict the
758 * set of hash algorithms accepted when updating the security.ima xattr of
759 * a file.
760 *
761 * Context: called after a policy update and at system initialization.
Roberto Sassua7560242014-09-12 19:35:54 +0200762 */
THOBY Simon4f2946a2021-08-16 08:11:01 +0000763void ima_update_policy_flags(void)
Roberto Sassua7560242014-09-12 19:35:54 +0200764{
765 struct ima_rule_entry *entry;
THOBY Simon4f2946a2021-08-16 08:11:01 +0000766 int new_policy_flag = 0;
liqiongeb0782b2021-10-09 18:38:21 +0800767 struct list_head *ima_rules_tmp;
Roberto Sassua7560242014-09-12 19:35:54 +0200768
THOBY Simon4f2946a2021-08-16 08:11:01 +0000769 rcu_read_lock();
liqiongeb0782b2021-10-09 18:38:21 +0800770 ima_rules_tmp = rcu_dereference(ima_rules);
771 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
THOBY Simon4f2946a2021-08-16 08:11:01 +0000772 /*
773 * SETXATTR_CHECK rules do not implement a full policy check
774 * because rule checking would probably have an important
775 * performance impact on setxattr(). As a consequence, only one
776 * SETXATTR_CHECK can be active at a given time.
777 * Because we want to preserve that property, we set out to use
778 * atomic_cmpxchg. Either:
779 * - the atomic was non-zero: a setxattr hash policy is
780 * already enforced, we do nothing
781 * - the atomic was zero: no setxattr policy was set, enable
782 * the setxattr hash policy
783 */
784 if (entry->func == SETXATTR_CHECK) {
785 atomic_cmpxchg(&ima_setxattr_allowed_hash_algorithms,
786 0, entry->allowed_algos);
787 /* SETXATTR_CHECK doesn't impact ima_policy_flag */
788 continue;
789 }
790
Roberto Sassua7560242014-09-12 19:35:54 +0200791 if (entry->action & IMA_DO_MASK)
THOBY Simon4f2946a2021-08-16 08:11:01 +0000792 new_policy_flag |= entry->action;
Roberto Sassua7560242014-09-12 19:35:54 +0200793 }
THOBY Simon4f2946a2021-08-16 08:11:01 +0000794 rcu_read_unlock();
Roberto Sassua7560242014-09-12 19:35:54 +0200795
Mimi Zoharef968372018-07-13 14:06:01 -0400796 ima_appraise |= (build_ima_appraise | temp_ima_appraise);
Roberto Sassua7560242014-09-12 19:35:54 +0200797 if (!ima_appraise)
THOBY Simon4f2946a2021-08-16 08:11:01 +0000798 new_policy_flag &= ~IMA_APPRAISE;
799
800 ima_policy_flag = new_policy_flag;
Roberto Sassua7560242014-09-12 19:35:54 +0200801}
802
Mimi Zohar6f0911a2018-04-12 00:15:22 -0400803static int ima_appraise_flag(enum ima_hooks func)
804{
805 if (func == MODULE_CHECK)
806 return IMA_APPRAISE_MODULES;
807 else if (func == FIRMWARE_CHECK)
808 return IMA_APPRAISE_FIRMWARE;
809 else if (func == POLICY_CHECK)
810 return IMA_APPRAISE_POLICY;
Mimi Zohar16c267a2018-07-13 14:05:58 -0400811 else if (func == KEXEC_KERNEL_CHECK)
812 return IMA_APPRAISE_KEXEC;
Mimi Zohar6f0911a2018-04-12 00:15:22 -0400813 return 0;
814}
815
Nayna Jainc52657d2018-10-09 23:00:35 +0530816static void add_rules(struct ima_rule_entry *entries, int count,
817 enum policy_rule_list policy_rule)
818{
819 int i = 0;
820
821 for (i = 0; i < count; i++) {
822 struct ima_rule_entry *entry;
823
824 if (policy_rule & IMA_DEFAULT_POLICY)
825 list_add_tail(&entries[i].list, &ima_default_rules);
826
827 if (policy_rule & IMA_CUSTOM_POLICY) {
828 entry = kmemdup(&entries[i], sizeof(*entry),
829 GFP_KERNEL);
830 if (!entry)
831 continue;
832
833 list_add_tail(&entry->list, &ima_policy_rules);
834 }
Krzysztof Struczynskib59fda42020-04-27 12:28:59 +0200835 if (entries[i].action == APPRAISE) {
836 if (entries != build_appraise_rules)
837 temp_ima_appraise |=
838 ima_appraise_flag(entries[i].func);
839 else
840 build_ima_appraise |=
841 ima_appraise_flag(entries[i].func);
842 }
Nayna Jainc52657d2018-10-09 23:00:35 +0530843 }
844}
845
Nayna Jain61917062018-10-09 23:00:36 +0530846static int ima_parse_rule(char *rule, struct ima_rule_entry *entry);
847
848static int __init ima_init_arch_policy(void)
849{
850 const char * const *arch_rules;
851 const char * const *rules;
852 int arch_entries = 0;
853 int i = 0;
854
855 arch_rules = arch_get_ima_policy();
856 if (!arch_rules)
857 return arch_entries;
858
859 /* Get number of rules */
860 for (rules = arch_rules; *rules != NULL; rules++)
861 arch_entries++;
862
863 arch_policy_entry = kcalloc(arch_entries + 1,
864 sizeof(*arch_policy_entry), GFP_KERNEL);
865 if (!arch_policy_entry)
866 return 0;
867
868 /* Convert each policy string rules to struct ima_rule_entry format */
869 for (rules = arch_rules, i = 0; *rules != NULL; rules++) {
870 char rule[255];
871 int result;
872
Petr Vorelcc4299e2021-10-08 11:14:30 +0200873 result = strscpy(rule, *rules, sizeof(rule));
Nayna Jain61917062018-10-09 23:00:36 +0530874
875 INIT_LIST_HEAD(&arch_policy_entry[i].list);
876 result = ima_parse_rule(rule, &arch_policy_entry[i]);
877 if (result) {
878 pr_warn("Skipping unknown architecture policy rule: %s\n",
879 rule);
880 memset(&arch_policy_entry[i], 0,
881 sizeof(*arch_policy_entry));
882 continue;
883 }
884 i++;
885 }
886 return i;
887}
888
Mimi Zohar3323eec2009-02-04 09:06:58 -0500889/**
890 * ima_init_policy - initialize the default measure rules.
891 *
Petr Vorel61868ac2021-10-08 11:14:29 +0200892 * ima_rules points to either the ima_default_rules or the new ima_policy_rules.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500893 */
Eric Paris932995f2009-05-21 15:43:32 -0400894void __init ima_init_policy(void)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500895{
Nayna Jain61917062018-10-09 23:00:36 +0530896 int build_appraise_entries, arch_entries;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500897
Nayna Jainc52657d2018-10-09 23:00:35 +0530898 /* if !ima_policy, we load NO default rules */
899 if (ima_policy)
900 add_rules(dont_measure_rules, ARRAY_SIZE(dont_measure_rules),
901 IMA_DEFAULT_POLICY);
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400902
903 switch (ima_policy) {
904 case ORIGINAL_TCB:
Nayna Jainc52657d2018-10-09 23:00:35 +0530905 add_rules(original_measurement_rules,
906 ARRAY_SIZE(original_measurement_rules),
907 IMA_DEFAULT_POLICY);
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400908 break;
909 case DEFAULT_TCB:
Nayna Jainc52657d2018-10-09 23:00:35 +0530910 add_rules(default_measurement_rules,
911 ARRAY_SIZE(default_measurement_rules),
912 IMA_DEFAULT_POLICY);
Gustavo A. R. Silva28073eb2020-11-20 12:25:46 -0600913 break;
Mimi Zohar24fd03c2015-06-11 20:48:33 -0400914 default:
915 break;
916 }
Eric Paris5789ba32009-05-21 15:47:06 -0400917
Mimi Zohar503ceae2017-04-21 18:58:27 -0400918 /*
Nayna Jain61917062018-10-09 23:00:36 +0530919 * Based on runtime secure boot flags, insert arch specific measurement
920 * and appraise rules requiring file signatures for both the initial
921 * and custom policies, prior to other appraise rules.
922 * (Highest priority)
Mimi Zohar503ceae2017-04-21 18:58:27 -0400923 */
Nayna Jain61917062018-10-09 23:00:36 +0530924 arch_entries = ima_init_arch_policy();
925 if (!arch_entries)
926 pr_info("No architecture policies found\n");
927 else
928 add_rules(arch_policy_entry, arch_entries,
929 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
930
931 /*
Mimi Zohar503ceae2017-04-21 18:58:27 -0400932 * Insert the builtin "secure_boot" policy rules requiring file
Nayna Jain61917062018-10-09 23:00:36 +0530933 * signatures, prior to other appraise rules.
Mimi Zohar503ceae2017-04-21 18:58:27 -0400934 */
Nayna Jainc52657d2018-10-09 23:00:35 +0530935 if (ima_use_secure_boot)
936 add_rules(secure_boot_rules, ARRAY_SIZE(secure_boot_rules),
937 IMA_DEFAULT_POLICY);
Mimi Zohar503ceae2017-04-21 18:58:27 -0400938
Mimi Zoharef968372018-07-13 14:06:01 -0400939 /*
940 * Insert the build time appraise rules requiring file signatures
941 * for both the initial and custom policies, prior to other appraise
Nayna Jainc52657d2018-10-09 23:00:35 +0530942 * rules. As the secure boot rules includes all of the build time
943 * rules, include either one or the other set of rules, but not both.
Mimi Zoharef968372018-07-13 14:06:01 -0400944 */
Nayna Jainc52657d2018-10-09 23:00:35 +0530945 build_appraise_entries = ARRAY_SIZE(build_appraise_rules);
946 if (build_appraise_entries) {
947 if (ima_use_secure_boot)
948 add_rules(build_appraise_rules, build_appraise_entries,
949 IMA_CUSTOM_POLICY);
950 else
951 add_rules(build_appraise_rules, build_appraise_entries,
952 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
Mimi Zoharef968372018-07-13 14:06:01 -0400953 }
954
Nayna Jainc52657d2018-10-09 23:00:35 +0530955 if (ima_use_appraise_tcb)
956 add_rules(default_appraise_rules,
957 ARRAY_SIZE(default_appraise_rules),
958 IMA_DEFAULT_POLICY);
Mimi Zohar07f6a792011-03-09 22:25:48 -0500959
Lakshmi Ramasubramanian03cee162021-01-07 20:07:07 -0800960 if (ima_use_critical_data)
961 add_rules(critical_data_rules,
962 ARRAY_SIZE(critical_data_rules),
963 IMA_DEFAULT_POLICY);
964
THOBY Simon4f2946a2021-08-16 08:11:01 +0000965 atomic_set(&ima_setxattr_allowed_hash_algorithms, 0);
966
967 ima_update_policy_flags();
Mimi Zohar3323eec2009-02-04 09:06:58 -0500968}
Mimi Zohar4af46622009-02-04 09:07:00 -0500969
Sasha Levin01127212015-12-22 08:51:23 -0500970/* Make sure we have a valid policy, at least containing some rules. */
Colin Ian Kingc75d8e92016-01-20 11:13:46 +0000971int ima_check_policy(void)
Sasha Levin01127212015-12-22 08:51:23 -0500972{
973 if (list_empty(&ima_temp_rules))
974 return -EINVAL;
975 return 0;
976}
977
Mimi Zohar4af46622009-02-04 09:07:00 -0500978/**
979 * ima_update_policy - update default_rules with new measure rules
980 *
981 * Called on file .release to update the default rules with a complete new
Petko Manolov38d859f2015-12-02 17:47:54 +0200982 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
983 * they make a queue. The policy may be updated multiple times and this is the
984 * RCU updater.
985 *
986 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
987 * we switch from the default policy to user defined.
Mimi Zohar4af46622009-02-04 09:07:00 -0500988 */
989void ima_update_policy(void)
990{
Petko Manolov53b626f2018-05-22 17:06:55 +0300991 struct list_head *policy = &ima_policy_rules;
Petko Manolov38d859f2015-12-02 17:47:54 +0200992
Petko Manolov53b626f2018-05-22 17:06:55 +0300993 list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu);
Petko Manolov38d859f2015-12-02 17:47:54 +0200994
liqiongeb0782b2021-10-09 18:38:21 +0800995 if (ima_rules != (struct list_head __rcu *)policy) {
Petko Manolov38d859f2015-12-02 17:47:54 +0200996 ima_policy_flag = 0;
Nayna Jain61917062018-10-09 23:00:36 +0530997
liqiongeb0782b2021-10-09 18:38:21 +0800998 rcu_assign_pointer(ima_rules, policy);
Nayna Jain61917062018-10-09 23:00:36 +0530999 /*
1000 * IMA architecture specific policy rules are specified
1001 * as strings and converted to an array of ima_entry_rules
1002 * on boot. After loading a custom policy, free the
1003 * architecture specific rules stored as an array.
1004 */
1005 kfree(arch_policy_entry);
Petko Manolov38d859f2015-12-02 17:47:54 +02001006 }
THOBY Simon4f2946a2021-08-16 08:11:01 +00001007 ima_update_policy_flags();
Lakshmi Ramasubramanian450d0fd2020-01-22 17:32:05 -08001008
1009 /* Custom IMA policy has been loaded */
1010 ima_process_queued_keys();
Mimi Zohar4af46622009-02-04 09:07:00 -05001011}
1012
Mimi Zohar1a9430d2018-12-17 19:14:49 -05001013/* Keep the enumeration in sync with the policy_tokens! */
Curtis Veit40224c42021-10-07 14:03:02 -06001014enum policy_opt {
Mimi Zohar1a9430d2018-12-17 19:14:49 -05001015 Opt_measure, Opt_dont_measure,
Mimi Zohar07f6a792011-03-09 22:25:48 -05001016 Opt_appraise, Opt_dont_appraise,
Mimi Zoharda1b0022016-09-29 10:04:52 -04001017 Opt_audit, Opt_hash, Opt_dont_hash,
Mimi Zohar4af46622009-02-04 09:07:00 -05001018 Opt_obj_user, Opt_obj_role, Opt_obj_type,
1019 Opt_subj_user, Opt_subj_role, Opt_subj_type,
Curtis Veit40224c42021-10-07 14:03:02 -06001020 Opt_func, Opt_mask, Opt_fsmagic, Opt_fsname, Opt_fsuuid,
1021 Opt_uid_eq, Opt_euid_eq, Opt_gid_eq, Opt_egid_eq,
1022 Opt_fowner_eq, Opt_fgroup_eq,
1023 Opt_uid_gt, Opt_euid_gt, Opt_gid_gt, Opt_egid_gt,
1024 Opt_fowner_gt, Opt_fgroup_gt,
1025 Opt_uid_lt, Opt_euid_lt, Opt_gid_lt, Opt_egid_lt,
1026 Opt_fowner_lt, Opt_fgroup_lt,
THOBY Simon583a80a2021-08-16 08:11:00 +00001027 Opt_appraise_type, Opt_appraise_flag, Opt_appraise_algos,
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08001028 Opt_permit_directio, Opt_pcr, Opt_template, Opt_keyrings,
Tushar Sugandhi47d76a42021-01-07 20:07:05 -08001029 Opt_label, Opt_err
Mimi Zohar4af46622009-02-04 09:07:00 -05001030};
1031
Mimi Zohar1a9430d2018-12-17 19:14:49 -05001032static const match_table_t policy_tokens = {
Mimi Zohar4af46622009-02-04 09:07:00 -05001033 {Opt_measure, "measure"},
1034 {Opt_dont_measure, "dont_measure"},
Mimi Zohar07f6a792011-03-09 22:25:48 -05001035 {Opt_appraise, "appraise"},
1036 {Opt_dont_appraise, "dont_appraise"},
Peter Moodye7c568e2012-06-14 10:04:36 -07001037 {Opt_audit, "audit"},
Mimi Zoharda1b0022016-09-29 10:04:52 -04001038 {Opt_hash, "hash"},
1039 {Opt_dont_hash, "dont_hash"},
Mimi Zohar4af46622009-02-04 09:07:00 -05001040 {Opt_obj_user, "obj_user=%s"},
1041 {Opt_obj_role, "obj_role=%s"},
1042 {Opt_obj_type, "obj_type=%s"},
1043 {Opt_subj_user, "subj_user=%s"},
1044 {Opt_subj_role, "subj_role=%s"},
1045 {Opt_subj_type, "subj_type=%s"},
1046 {Opt_func, "func=%s"},
1047 {Opt_mask, "mask=%s"},
1048 {Opt_fsmagic, "fsmagic=%s"},
Mimi Zoharf1b08bb2018-01-15 11:20:36 -05001049 {Opt_fsname, "fsname=%s"},
Dmitry Kasatkin85865c12012-09-03 23:23:13 +03001050 {Opt_fsuuid, "fsuuid=%s"},
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001051 {Opt_uid_eq, "uid=%s"},
1052 {Opt_euid_eq, "euid=%s"},
Curtis Veit40224c42021-10-07 14:03:02 -06001053 {Opt_gid_eq, "gid=%s"},
1054 {Opt_egid_eq, "egid=%s"},
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001055 {Opt_fowner_eq, "fowner=%s"},
Curtis Veit40224c42021-10-07 14:03:02 -06001056 {Opt_fgroup_eq, "fgroup=%s"},
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001057 {Opt_uid_gt, "uid>%s"},
1058 {Opt_euid_gt, "euid>%s"},
Curtis Veit40224c42021-10-07 14:03:02 -06001059 {Opt_gid_gt, "gid>%s"},
1060 {Opt_egid_gt, "egid>%s"},
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001061 {Opt_fowner_gt, "fowner>%s"},
Curtis Veit40224c42021-10-07 14:03:02 -06001062 {Opt_fgroup_gt, "fgroup>%s"},
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001063 {Opt_uid_lt, "uid<%s"},
1064 {Opt_euid_lt, "euid<%s"},
Curtis Veit40224c42021-10-07 14:03:02 -06001065 {Opt_gid_lt, "gid<%s"},
1066 {Opt_egid_lt, "egid<%s"},
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001067 {Opt_fowner_lt, "fowner<%s"},
Curtis Veit40224c42021-10-07 14:03:02 -06001068 {Opt_fgroup_lt, "fgroup<%s"},
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +03001069 {Opt_appraise_type, "appraise_type=%s"},
Nayna Jain273df862019-10-30 23:31:32 -04001070 {Opt_appraise_flag, "appraise_flag=%s"},
THOBY Simon583a80a2021-08-16 08:11:00 +00001071 {Opt_appraise_algos, "appraise_algos=%s"},
Mimi Zoharf9b2a732014-05-12 09:28:11 -04001072 {Opt_permit_directio, "permit_directio"},
Eric Richter02606432016-06-01 13:14:01 -05001073 {Opt_pcr, "pcr=%s"},
Matthew Garrett19453ce02019-06-19 15:46:11 -07001074 {Opt_template, "template=%s"},
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08001075 {Opt_keyrings, "keyrings=%s"},
Tushar Sugandhi47d76a42021-01-07 20:07:05 -08001076 {Opt_label, "label=%s"},
Mimi Zohar4af46622009-02-04 09:07:00 -05001077 {Opt_err, NULL}
1078};
1079
Mimi Zohar07f6a792011-03-09 22:25:48 -05001080static int ima_lsm_rule_init(struct ima_rule_entry *entry,
Mimi Zohar7163a992013-01-03 14:19:09 -05001081 substring_t *args, int lsm_rule, int audit_type)
Mimi Zohar4af46622009-02-04 09:07:00 -05001082{
1083 int result;
1084
Eric Paris7b62e162010-04-20 10:21:01 -04001085 if (entry->lsm[lsm_rule].rule)
1086 return -EINVAL;
1087
Mimi Zohar7163a992013-01-03 14:19:09 -05001088 entry->lsm[lsm_rule].args_p = match_strdup(args);
1089 if (!entry->lsm[lsm_rule].args_p)
1090 return -ENOMEM;
1091
Mimi Zohar4af46622009-02-04 09:07:00 -05001092 entry->lsm[lsm_rule].type = audit_type;
Tyler Hicksb8867ee2020-07-10 15:37:50 -05001093 result = ima_filter_rule_init(entry->lsm[lsm_rule].type, Audit_equal,
1094 entry->lsm[lsm_rule].args_p,
1095 &entry->lsm[lsm_rule].rule);
Mimi Zohar7163a992013-01-03 14:19:09 -05001096 if (!entry->lsm[lsm_rule].rule) {
Janne Karhunen483ec262020-01-15 17:42:30 +02001097 pr_warn("rule for LSM \'%s\' is undefined\n",
Tyler Hicksaa0c0222020-07-09 01:19:08 -05001098 entry->lsm[lsm_rule].args_p);
Janne Karhunen483ec262020-01-15 17:42:30 +02001099
liqiongeb0782b2021-10-09 18:38:21 +08001100 if (ima_rules == (struct list_head __rcu *)(&ima_default_rules)) {
Janne Karhunen483ec262020-01-15 17:42:30 +02001101 kfree(entry->lsm[lsm_rule].args_p);
Tyler Hicks2bdd7372020-07-09 01:19:02 -05001102 entry->lsm[lsm_rule].args_p = NULL;
Janne Karhunen483ec262020-01-15 17:42:30 +02001103 result = -EINVAL;
1104 } else
1105 result = 0;
Mimi Zohar7163a992013-01-03 14:19:09 -05001106 }
1107
Mimi Zohar4af46622009-02-04 09:07:00 -05001108 return result;
1109}
1110
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001111static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
Curtis Veit40224c42021-10-07 14:03:02 -06001112 enum policy_opt rule_operator)
Eric Paris2f1506c2010-04-20 10:21:30 -04001113{
Stefan Berger2afd0202018-06-04 16:54:54 -04001114 if (!ab)
1115 return;
1116
Curtis Veit40224c42021-10-07 14:03:02 -06001117 switch (rule_operator) {
1118 case Opt_uid_gt:
1119 case Opt_euid_gt:
1120 case Opt_gid_gt:
1121 case Opt_egid_gt:
1122 case Opt_fowner_gt:
1123 case Opt_fgroup_gt:
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001124 audit_log_format(ab, "%s>", key);
Curtis Veit40224c42021-10-07 14:03:02 -06001125 break;
1126 case Opt_uid_lt:
1127 case Opt_euid_lt:
1128 case Opt_gid_lt:
1129 case Opt_egid_lt:
1130 case Opt_fowner_lt:
1131 case Opt_fgroup_lt:
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001132 audit_log_format(ab, "%s<", key);
Curtis Veit40224c42021-10-07 14:03:02 -06001133 break;
1134 default:
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001135 audit_log_format(ab, "%s=", key);
Curtis Veit40224c42021-10-07 14:03:02 -06001136 }
Stefan Berger3d2859d2018-06-04 16:54:53 -04001137 audit_log_format(ab, "%s ", value);
Eric Paris2f1506c2010-04-20 10:21:30 -04001138}
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001139static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
1140{
Curtis Veit40224c42021-10-07 14:03:02 -06001141 ima_log_string_op(ab, key, value, Opt_err);
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001142}
Eric Paris2f1506c2010-04-20 10:21:30 -04001143
Thiago Jung Bauermann3878d502019-06-27 23:19:32 -03001144/*
1145 * Validating the appended signature included in the measurement list requires
1146 * the file hash calculated without the appended signature (i.e., the 'd-modsig'
1147 * field). Therefore, notify the user if they have the 'modsig' field but not
1148 * the 'd-modsig' field in the template.
1149 */
1150static void check_template_modsig(const struct ima_template_desc *template)
1151{
1152#define MSG "template with 'modsig' field also needs 'd-modsig' field\n"
1153 bool has_modsig, has_dmodsig;
1154 static bool checked;
1155 int i;
1156
1157 /* We only need to notify the user once. */
1158 if (checked)
1159 return;
1160
1161 has_modsig = has_dmodsig = false;
1162 for (i = 0; i < template->num_fields; i++) {
1163 if (!strcmp(template->fields[i]->field_id, "modsig"))
1164 has_modsig = true;
1165 else if (!strcmp(template->fields[i]->field_id, "d-modsig"))
1166 has_dmodsig = true;
1167 }
1168
1169 if (has_modsig && !has_dmodsig)
1170 pr_notice(MSG);
1171
1172 checked = true;
1173#undef MSG
1174}
1175
Tyler Hicks712183432020-07-09 01:19:03 -05001176static bool ima_validate_rule(struct ima_rule_entry *entry)
1177{
Tyler Hicks30031b02020-07-09 01:19:09 -05001178 /* Ensure that the action is set and is compatible with the flags */
Tyler Hicks712183432020-07-09 01:19:03 -05001179 if (entry->action == UNKNOWN)
1180 return false;
1181
Tyler Hicks30031b02020-07-09 01:19:09 -05001182 if (entry->action != MEASURE && entry->flags & IMA_PCR)
1183 return false;
1184
1185 if (entry->action != APPRAISE &&
THOBY Simon583a80a2021-08-16 08:11:00 +00001186 entry->flags & (IMA_DIGSIG_REQUIRED | IMA_MODSIG_ALLOWED |
1187 IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS))
Tyler Hicks30031b02020-07-09 01:19:09 -05001188 return false;
1189
1190 /*
1191 * The IMA_FUNC bit must be set if and only if there's a valid hook
1192 * function specified, and vice versa. Enforcing this property allows
1193 * for the NONE case below to validate a rule without an explicit hook
1194 * function.
1195 */
1196 if (((entry->flags & IMA_FUNC) && entry->func == NONE) ||
1197 (!(entry->flags & IMA_FUNC) && entry->func != NONE))
1198 return false;
1199
Tyler Hicks712183432020-07-09 01:19:03 -05001200 /*
1201 * Ensure that the hook function is compatible with the other
1202 * components of the rule
1203 */
1204 switch (entry->func) {
1205 case NONE:
1206 case FILE_CHECK:
1207 case MMAP_CHECK:
1208 case BPRM_CHECK:
1209 case CREDS_CHECK:
1210 case POST_SETATTR:
Tyler Hicks712183432020-07-09 01:19:03 -05001211 case FIRMWARE_CHECK:
Tyler Hicks30031b02020-07-09 01:19:09 -05001212 case POLICY_CHECK:
1213 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1214 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1215 IMA_INMASK | IMA_EUID | IMA_PCR |
Curtis Veit40224c42021-10-07 14:03:02 -06001216 IMA_FSNAME | IMA_GID | IMA_EGID |
1217 IMA_FGROUP | IMA_DIGSIG_REQUIRED |
THOBY Simon583a80a2021-08-16 08:11:00 +00001218 IMA_PERMIT_DIRECTIO | IMA_VALIDATE_ALGOS))
Tyler Hicks30031b02020-07-09 01:19:09 -05001219 return false;
1220
1221 break;
1222 case MODULE_CHECK:
Tyler Hicks712183432020-07-09 01:19:03 -05001223 case KEXEC_KERNEL_CHECK:
1224 case KEXEC_INITRAMFS_CHECK:
Tyler Hicks30031b02020-07-09 01:19:09 -05001225 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1226 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1227 IMA_INMASK | IMA_EUID | IMA_PCR |
Curtis Veit40224c42021-10-07 14:03:02 -06001228 IMA_FSNAME | IMA_GID | IMA_EGID |
1229 IMA_FGROUP | IMA_DIGSIG_REQUIRED |
Tyler Hicks30031b02020-07-09 01:19:09 -05001230 IMA_PERMIT_DIRECTIO | IMA_MODSIG_ALLOWED |
THOBY Simon583a80a2021-08-16 08:11:00 +00001231 IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS))
Tyler Hicks30031b02020-07-09 01:19:09 -05001232 return false;
1233
Tyler Hicks712183432020-07-09 01:19:03 -05001234 break;
1235 case KEXEC_CMDLINE:
Tyler Hicksdb2045f2020-07-09 01:19:04 -05001236 if (entry->action & ~(MEASURE | DONT_MEASURE))
1237 return false;
1238
Tyler Hicks48341772020-07-09 01:19:11 -05001239 if (entry->flags & ~(IMA_FUNC | IMA_FSMAGIC | IMA_UID |
1240 IMA_FOWNER | IMA_FSUUID | IMA_EUID |
Curtis Veit40224c42021-10-07 14:03:02 -06001241 IMA_PCR | IMA_FSNAME | IMA_GID | IMA_EGID |
1242 IMA_FGROUP))
Tyler Hicksdb2045f2020-07-09 01:19:04 -05001243 return false;
1244
1245 break;
Tyler Hicks712183432020-07-09 01:19:03 -05001246 case KEY_CHECK:
1247 if (entry->action & ~(MEASURE | DONT_MEASURE))
1248 return false;
1249
Curtis Veit40224c42021-10-07 14:03:02 -06001250 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_GID | IMA_PCR |
Tyler Hickseb624fe2020-07-09 01:19:05 -05001251 IMA_KEYRINGS))
1252 return false;
1253
1254 if (ima_rule_contains_lsm_cond(entry))
1255 return false;
1256
Tyler Hicks712183432020-07-09 01:19:03 -05001257 break;
Tushar Sugandhic4e43aa2021-01-07 20:07:04 -08001258 case CRITICAL_DATA:
1259 if (entry->action & ~(MEASURE | DONT_MEASURE))
1260 return false;
1261
Curtis Veit40224c42021-10-07 14:03:02 -06001262 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_GID | IMA_PCR |
Tushar Sugandhi47d76a42021-01-07 20:07:05 -08001263 IMA_LABEL))
Tushar Sugandhic4e43aa2021-01-07 20:07:04 -08001264 return false;
1265
1266 if (ima_rule_contains_lsm_cond(entry))
1267 return false;
1268
1269 break;
THOBY Simon4f2946a2021-08-16 08:11:01 +00001270 case SETXATTR_CHECK:
1271 /* any action other than APPRAISE is unsupported */
1272 if (entry->action != APPRAISE)
1273 return false;
1274
1275 /* SETXATTR_CHECK requires an appraise_algos parameter */
1276 if (!(entry->flags & IMA_VALIDATE_ALGOS))
1277 return false;
1278
1279 /*
1280 * full policies are not supported, they would have too
1281 * much of a performance impact
1282 */
1283 if (entry->flags & ~(IMA_FUNC | IMA_VALIDATE_ALGOS))
1284 return false;
1285
1286 break;
Tyler Hicks712183432020-07-09 01:19:03 -05001287 default:
1288 return false;
1289 }
1290
Tyler Hicks5f3e9262020-07-09 01:19:06 -05001291 /* Ensure that combinations of flags are compatible with each other */
1292 if (entry->flags & IMA_CHECK_BLACKLIST &&
1293 !(entry->flags & IMA_MODSIG_ALLOWED))
1294 return false;
1295
Tyler Hicks712183432020-07-09 01:19:03 -05001296 return true;
1297}
1298
THOBY Simon583a80a2021-08-16 08:11:00 +00001299static unsigned int ima_parse_appraise_algos(char *arg)
1300{
1301 unsigned int res = 0;
1302 int idx;
1303 char *token;
1304
1305 while ((token = strsep(&arg, ",")) != NULL) {
1306 idx = match_string(hash_algo_name, HASH_ALGO__LAST, token);
1307
1308 if (idx < 0) {
1309 pr_err("unknown hash algorithm \"%s\"",
1310 token);
1311 return 0;
1312 }
1313
THOBY Simon8ecd39c2021-08-16 08:11:01 +00001314 if (!crypto_has_alg(hash_algo_name[idx], 0, 0)) {
1315 pr_err("unavailable hash algorithm \"%s\", check your kernel configuration",
1316 token);
1317 return 0;
1318 }
1319
THOBY Simon583a80a2021-08-16 08:11:00 +00001320 /* Add the hash algorithm to the 'allowed' bitfield */
1321 res |= (1U << idx);
1322 }
1323
1324 return res;
1325}
1326
Mimi Zohar07f6a792011-03-09 22:25:48 -05001327static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
Mimi Zohar4af46622009-02-04 09:07:00 -05001328{
1329 struct audit_buffer *ab;
Mimi Zohar4351c292014-11-05 07:53:55 -05001330 char *from;
Mimi Zohar4af46622009-02-04 09:07:00 -05001331 char *p;
Curtis Veit40224c42021-10-07 14:03:02 -06001332 bool eid_token; /* either euid or egid */
Matthew Garrett19453ce02019-06-19 15:46:11 -07001333 struct ima_template_desc *template_desc;
Mimi Zohar4af46622009-02-04 09:07:00 -05001334 int result = 0;
1335
Stefan Bergerdba31ee2018-06-04 16:54:55 -04001336 ab = integrity_audit_log_start(audit_context(), GFP_KERNEL,
1337 AUDIT_INTEGRITY_POLICY_RULE);
Mimi Zohar4af46622009-02-04 09:07:00 -05001338
Eric W. Biederman8b94eea2012-05-25 18:24:12 -06001339 entry->uid = INVALID_UID;
Curtis Veit40224c42021-10-07 14:03:02 -06001340 entry->gid = INVALID_GID;
Linus Torvalds882653222012-10-02 21:38:48 -07001341 entry->fowner = INVALID_UID;
Curtis Veit40224c42021-10-07 14:03:02 -06001342 entry->fgroup = INVALID_GID;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001343 entry->uid_op = &uid_eq;
Curtis Veit40224c42021-10-07 14:03:02 -06001344 entry->gid_op = &gid_eq;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001345 entry->fowner_op = &uid_eq;
Curtis Veit40224c42021-10-07 14:03:02 -06001346 entry->fgroup_op = &gid_eq;
Eric Parisb9035b12010-04-20 10:21:07 -04001347 entry->action = UNKNOWN;
Eric Paris28ef4002010-04-20 10:21:18 -04001348 while ((p = strsep(&rule, " \t")) != NULL) {
Mimi Zohar4af46622009-02-04 09:07:00 -05001349 substring_t args[MAX_OPT_ARGS];
1350 int token;
1351 unsigned long lnum;
1352
1353 if (result < 0)
1354 break;
Eric Paris28ef4002010-04-20 10:21:18 -04001355 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
1356 continue;
Mimi Zohar4af46622009-02-04 09:07:00 -05001357 token = match_token(p, policy_tokens, args);
1358 switch (token) {
1359 case Opt_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -04001360 ima_log_string(ab, "action", "measure");
Eric Paris7b62e162010-04-20 10:21:01 -04001361
1362 if (entry->action != UNKNOWN)
1363 result = -EINVAL;
1364
Mimi Zohar4af46622009-02-04 09:07:00 -05001365 entry->action = MEASURE;
1366 break;
1367 case Opt_dont_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -04001368 ima_log_string(ab, "action", "dont_measure");
Eric Paris7b62e162010-04-20 10:21:01 -04001369
1370 if (entry->action != UNKNOWN)
1371 result = -EINVAL;
1372
Mimi Zohar4af46622009-02-04 09:07:00 -05001373 entry->action = DONT_MEASURE;
1374 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -05001375 case Opt_appraise:
1376 ima_log_string(ab, "action", "appraise");
1377
1378 if (entry->action != UNKNOWN)
1379 result = -EINVAL;
1380
1381 entry->action = APPRAISE;
1382 break;
1383 case Opt_dont_appraise:
1384 ima_log_string(ab, "action", "dont_appraise");
1385
1386 if (entry->action != UNKNOWN)
1387 result = -EINVAL;
1388
1389 entry->action = DONT_APPRAISE;
1390 break;
Peter Moodye7c568e2012-06-14 10:04:36 -07001391 case Opt_audit:
1392 ima_log_string(ab, "action", "audit");
1393
1394 if (entry->action != UNKNOWN)
1395 result = -EINVAL;
1396
1397 entry->action = AUDIT;
1398 break;
Mimi Zoharda1b0022016-09-29 10:04:52 -04001399 case Opt_hash:
1400 ima_log_string(ab, "action", "hash");
1401
1402 if (entry->action != UNKNOWN)
1403 result = -EINVAL;
1404
1405 entry->action = HASH;
1406 break;
1407 case Opt_dont_hash:
1408 ima_log_string(ab, "action", "dont_hash");
1409
1410 if (entry->action != UNKNOWN)
1411 result = -EINVAL;
1412
1413 entry->action = DONT_HASH;
1414 break;
Mimi Zohar4af46622009-02-04 09:07:00 -05001415 case Opt_func:
Eric Paris2f1506c2010-04-20 10:21:30 -04001416 ima_log_string(ab, "func", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -04001417
1418 if (entry->func)
Mimi Zohar07f6a792011-03-09 22:25:48 -05001419 result = -EINVAL;
Eric Paris7b62e162010-04-20 10:21:01 -04001420
Mimi Zohar1e93d002010-01-26 17:02:41 -05001421 if (strcmp(args[0].from, "FILE_CHECK") == 0)
1422 entry->func = FILE_CHECK;
1423 /* PATH_CHECK is for backwards compat */
1424 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
1425 entry->func = FILE_CHECK;
Mimi Zoharfdf90722012-10-16 12:40:08 +10301426 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
1427 entry->func = MODULE_CHECK;
Mimi Zohar5a9196d2014-07-22 10:39:48 -04001428 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
1429 entry->func = FIRMWARE_CHECK;
Mimi Zohar16cac492012-12-13 11:15:04 -05001430 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
1431 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
1432 entry->func = MMAP_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -05001433 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
1434 entry->func = BPRM_CHECK;
Matthew Garrettd906c102018-01-08 13:36:20 -08001435 else if (strcmp(args[0].from, "CREDS_CHECK") == 0)
1436 entry->func = CREDS_CHECK;
Mimi Zohard9ddf072016-01-14 20:59:14 -05001437 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
1438 0)
1439 entry->func = KEXEC_KERNEL_CHECK;
1440 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
1441 == 0)
1442 entry->func = KEXEC_INITRAMFS_CHECK;
Mimi Zohar19f8a842016-01-15 10:17:12 -05001443 else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
1444 entry->func = POLICY_CHECK;
Prakhar Srivastavab0935122019-06-23 23:23:29 -07001445 else if (strcmp(args[0].from, "KEXEC_CMDLINE") == 0)
1446 entry->func = KEXEC_CMDLINE;
Tyler Hicks48ce1dd2020-08-11 14:26:21 -05001447 else if (IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) &&
1448 strcmp(args[0].from, "KEY_CHECK") == 0)
Lakshmi Ramasubramanian58086112019-12-11 08:47:03 -08001449 entry->func = KEY_CHECK;
Tushar Sugandhic4e43aa2021-01-07 20:07:04 -08001450 else if (strcmp(args[0].from, "CRITICAL_DATA") == 0)
1451 entry->func = CRITICAL_DATA;
THOBY Simon4f2946a2021-08-16 08:11:01 +00001452 else if (strcmp(args[0].from, "SETXATTR_CHECK") == 0)
1453 entry->func = SETXATTR_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -05001454 else
1455 result = -EINVAL;
1456 if (!result)
1457 entry->flags |= IMA_FUNC;
1458 break;
1459 case Opt_mask:
Eric Paris2f1506c2010-04-20 10:21:30 -04001460 ima_log_string(ab, "mask", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -04001461
1462 if (entry->mask)
1463 result = -EINVAL;
1464
Mimi Zohar4351c292014-11-05 07:53:55 -05001465 from = args[0].from;
1466 if (*from == '^')
1467 from++;
1468
1469 if ((strcmp(from, "MAY_EXEC")) == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -05001470 entry->mask = MAY_EXEC;
Mimi Zohar4351c292014-11-05 07:53:55 -05001471 else if (strcmp(from, "MAY_WRITE") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -05001472 entry->mask = MAY_WRITE;
Mimi Zohar4351c292014-11-05 07:53:55 -05001473 else if (strcmp(from, "MAY_READ") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -05001474 entry->mask = MAY_READ;
Mimi Zohar4351c292014-11-05 07:53:55 -05001475 else if (strcmp(from, "MAY_APPEND") == 0)
Mimi Zohar4af46622009-02-04 09:07:00 -05001476 entry->mask = MAY_APPEND;
1477 else
1478 result = -EINVAL;
1479 if (!result)
Mimi Zohar4351c292014-11-05 07:53:55 -05001480 entry->flags |= (*args[0].from == '^')
1481 ? IMA_INMASK : IMA_MASK;
Mimi Zohar4af46622009-02-04 09:07:00 -05001482 break;
1483 case Opt_fsmagic:
Eric Paris2f1506c2010-04-20 10:21:30 -04001484 ima_log_string(ab, "fsmagic", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -04001485
1486 if (entry->fsmagic) {
1487 result = -EINVAL;
1488 break;
1489 }
1490
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +02001491 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
Mimi Zohar4af46622009-02-04 09:07:00 -05001492 if (!result)
1493 entry->flags |= IMA_FSMAGIC;
1494 break;
Mimi Zoharf1b08bb2018-01-15 11:20:36 -05001495 case Opt_fsname:
1496 ima_log_string(ab, "fsname", args[0].from);
1497
1498 entry->fsname = kstrdup(args[0].from, GFP_KERNEL);
1499 if (!entry->fsname) {
1500 result = -ENOMEM;
1501 break;
1502 }
1503 result = 0;
1504 entry->flags |= IMA_FSNAME;
1505 break;
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08001506 case Opt_keyrings:
1507 ima_log_string(ab, "keyrings", args[0].from);
1508
Tyler Hicks48ce1dd2020-08-11 14:26:21 -05001509 if (!IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) ||
1510 entry->keyrings) {
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08001511 result = -EINVAL;
1512 break;
1513 }
Lakshmi Ramasubramanian5c7bac92020-01-16 18:18:21 -08001514
Tyler Hicks176377d2020-08-11 14:26:20 -05001515 entry->keyrings = ima_alloc_rule_opt_list(args);
1516 if (IS_ERR(entry->keyrings)) {
1517 result = PTR_ERR(entry->keyrings);
1518 entry->keyrings = NULL;
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08001519 break;
1520 }
Tyler Hicks176377d2020-08-11 14:26:20 -05001521
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08001522 entry->flags |= IMA_KEYRINGS;
1523 break;
Tushar Sugandhi47d76a42021-01-07 20:07:05 -08001524 case Opt_label:
1525 ima_log_string(ab, "label", args[0].from);
1526
1527 if (entry->label) {
1528 result = -EINVAL;
1529 break;
1530 }
1531
1532 entry->label = ima_alloc_rule_opt_list(args);
1533 if (IS_ERR(entry->label)) {
1534 result = PTR_ERR(entry->label);
1535 entry->label = NULL;
1536 break;
1537 }
1538
1539 entry->flags |= IMA_LABEL;
1540 break;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +03001541 case Opt_fsuuid:
1542 ima_log_string(ab, "fsuuid", args[0].from);
1543
Mike Rapoport36447452018-01-17 20:27:11 +02001544 if (!uuid_is_null(&entry->fsuuid)) {
Dmitry Kasatkin85865c12012-09-03 23:23:13 +03001545 result = -EINVAL;
1546 break;
1547 }
1548
Christoph Hellwig787d8c52017-06-01 07:00:26 +02001549 result = uuid_parse(args[0].from, &entry->fsuuid);
Mimi Zohar446d64e2013-02-24 23:42:37 -05001550 if (!result)
1551 entry->flags |= IMA_FSUUID;
Dmitry Kasatkin85865c12012-09-03 23:23:13 +03001552 break;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001553 case Opt_uid_gt:
1554 case Opt_euid_gt:
1555 entry->uid_op = &uid_gt;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001556 fallthrough;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001557 case Opt_uid_lt:
1558 case Opt_euid_lt:
1559 if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
1560 entry->uid_op = &uid_lt;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001561 fallthrough;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001562 case Opt_uid_eq:
1563 case Opt_euid_eq:
Curtis Veit40224c42021-10-07 14:03:02 -06001564 eid_token = (token == Opt_euid_eq) ||
1565 (token == Opt_euid_gt) ||
1566 (token == Opt_euid_lt);
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001567
Curtis Veit40224c42021-10-07 14:03:02 -06001568 ima_log_string_op(ab, eid_token ? "euid" : "uid",
1569 args[0].from, token);
Eric Paris7b62e162010-04-20 10:21:01 -04001570
Eric W. Biederman8b94eea2012-05-25 18:24:12 -06001571 if (uid_valid(entry->uid)) {
Eric Paris7b62e162010-04-20 10:21:01 -04001572 result = -EINVAL;
1573 break;
1574 }
1575
Jingoo Han29707b22014-02-05 15:13:14 +09001576 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar4af46622009-02-04 09:07:00 -05001577 if (!result) {
Mimi Zohar139069e2014-11-05 07:48:36 -05001578 entry->uid = make_kuid(current_user_ns(),
1579 (uid_t) lnum);
1580 if (!uid_valid(entry->uid) ||
1581 (uid_t)lnum != lnum)
Mimi Zohar4af46622009-02-04 09:07:00 -05001582 result = -EINVAL;
1583 else
Curtis Veit40224c42021-10-07 14:03:02 -06001584 entry->flags |= eid_token
1585 ? IMA_EUID : IMA_UID;
1586 }
1587 break;
1588 case Opt_gid_gt:
1589 case Opt_egid_gt:
1590 entry->gid_op = &gid_gt;
1591 fallthrough;
1592 case Opt_gid_lt:
1593 case Opt_egid_lt:
1594 if ((token == Opt_gid_lt) || (token == Opt_egid_lt))
1595 entry->gid_op = &gid_lt;
1596 fallthrough;
1597 case Opt_gid_eq:
1598 case Opt_egid_eq:
1599 eid_token = (token == Opt_egid_eq) ||
1600 (token == Opt_egid_gt) ||
1601 (token == Opt_egid_lt);
1602
1603 ima_log_string_op(ab, eid_token ? "egid" : "gid",
1604 args[0].from, token);
1605
1606 if (gid_valid(entry->gid)) {
1607 result = -EINVAL;
1608 break;
1609 }
1610
1611 result = kstrtoul(args[0].from, 10, &lnum);
1612 if (!result) {
1613 entry->gid = make_kgid(current_user_ns(),
1614 (gid_t)lnum);
1615 if (!gid_valid(entry->gid) ||
1616 (((gid_t)lnum) != lnum))
1617 result = -EINVAL;
1618 else
1619 entry->flags |= eid_token
1620 ? IMA_EGID : IMA_GID;
Mimi Zohar4af46622009-02-04 09:07:00 -05001621 }
1622 break;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001623 case Opt_fowner_gt:
1624 entry->fowner_op = &uid_gt;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001625 fallthrough;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001626 case Opt_fowner_lt:
1627 if (token == Opt_fowner_lt)
1628 entry->fowner_op = &uid_lt;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001629 fallthrough;
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03001630 case Opt_fowner_eq:
Curtis Veit40224c42021-10-07 14:03:02 -06001631 ima_log_string_op(ab, "fowner", args[0].from, token);
Mimi Zohar07f6a792011-03-09 22:25:48 -05001632
Linus Torvalds882653222012-10-02 21:38:48 -07001633 if (uid_valid(entry->fowner)) {
Mimi Zohar07f6a792011-03-09 22:25:48 -05001634 result = -EINVAL;
1635 break;
1636 }
1637
Jingoo Han29707b22014-02-05 15:13:14 +09001638 result = kstrtoul(args[0].from, 10, &lnum);
Mimi Zohar07f6a792011-03-09 22:25:48 -05001639 if (!result) {
Alex Henrie30d87642021-10-07 14:03:01 -06001640 entry->fowner = make_kuid(current_user_ns(),
1641 (uid_t)lnum);
1642 if (!uid_valid(entry->fowner) ||
1643 (((uid_t)lnum) != lnum))
Mimi Zohar07f6a792011-03-09 22:25:48 -05001644 result = -EINVAL;
1645 else
1646 entry->flags |= IMA_FOWNER;
1647 }
1648 break;
Curtis Veit40224c42021-10-07 14:03:02 -06001649 case Opt_fgroup_gt:
1650 entry->fgroup_op = &gid_gt;
1651 fallthrough;
1652 case Opt_fgroup_lt:
1653 if (token == Opt_fgroup_lt)
1654 entry->fgroup_op = &gid_lt;
1655 fallthrough;
1656 case Opt_fgroup_eq:
1657 ima_log_string_op(ab, "fgroup", args[0].from, token);
1658
1659 if (gid_valid(entry->fgroup)) {
1660 result = -EINVAL;
1661 break;
1662 }
1663
1664 result = kstrtoul(args[0].from, 10, &lnum);
1665 if (!result) {
1666 entry->fgroup = make_kgid(current_user_ns(),
1667 (gid_t)lnum);
1668 if (!gid_valid(entry->fgroup) ||
1669 (((gid_t)lnum) != lnum))
1670 result = -EINVAL;
1671 else
1672 entry->flags |= IMA_FGROUP;
1673 }
1674 break;
Mimi Zohar4af46622009-02-04 09:07:00 -05001675 case Opt_obj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -04001676 ima_log_string(ab, "obj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001677 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001678 LSM_OBJ_USER,
1679 AUDIT_OBJ_USER);
1680 break;
1681 case Opt_obj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -04001682 ima_log_string(ab, "obj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001683 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001684 LSM_OBJ_ROLE,
1685 AUDIT_OBJ_ROLE);
1686 break;
1687 case Opt_obj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -04001688 ima_log_string(ab, "obj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001689 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001690 LSM_OBJ_TYPE,
1691 AUDIT_OBJ_TYPE);
1692 break;
1693 case Opt_subj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -04001694 ima_log_string(ab, "subj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001695 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001696 LSM_SUBJ_USER,
1697 AUDIT_SUBJ_USER);
1698 break;
1699 case Opt_subj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -04001700 ima_log_string(ab, "subj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001701 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001702 LSM_SUBJ_ROLE,
1703 AUDIT_SUBJ_ROLE);
1704 break;
1705 case Opt_subj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -04001706 ima_log_string(ab, "subj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -05001707 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -05001708 LSM_SUBJ_TYPE,
1709 AUDIT_SUBJ_TYPE);
1710 break;
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +03001711 case Opt_appraise_type:
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +03001712 ima_log_string(ab, "appraise_type", args[0].from);
1713 if ((strcmp(args[0].from, "imasig")) == 0)
1714 entry->flags |= IMA_DIGSIG_REQUIRED;
Tyler Hicks30031b02020-07-09 01:19:09 -05001715 else if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
Thiago Jung Bauermann9044d622019-06-27 23:19:28 -03001716 strcmp(args[0].from, "imasig|modsig") == 0)
1717 entry->flags |= IMA_DIGSIG_REQUIRED |
1718 IMA_MODSIG_ALLOWED;
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +03001719 else
1720 result = -EINVAL;
1721 break;
Nayna Jain273df862019-10-30 23:31:32 -04001722 case Opt_appraise_flag:
1723 ima_log_string(ab, "appraise_flag", args[0].from);
Tyler Hicks5f3e9262020-07-09 01:19:06 -05001724 if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
1725 strstr(args[0].from, "blacklist"))
Nayna Jain273df862019-10-30 23:31:32 -04001726 entry->flags |= IMA_CHECK_BLACKLIST;
Tyler Hicks5f3e9262020-07-09 01:19:06 -05001727 else
1728 result = -EINVAL;
Nayna Jain273df862019-10-30 23:31:32 -04001729 break;
THOBY Simon583a80a2021-08-16 08:11:00 +00001730 case Opt_appraise_algos:
1731 ima_log_string(ab, "appraise_algos", args[0].from);
1732
1733 if (entry->allowed_algos) {
1734 result = -EINVAL;
1735 break;
1736 }
1737
1738 entry->allowed_algos =
1739 ima_parse_appraise_algos(args[0].from);
1740 /* invalid or empty list of algorithms */
1741 if (!entry->allowed_algos) {
1742 result = -EINVAL;
1743 break;
1744 }
1745
1746 entry->flags |= IMA_VALIDATE_ALGOS;
1747
1748 break;
Mimi Zoharf9b2a732014-05-12 09:28:11 -04001749 case Opt_permit_directio:
1750 entry->flags |= IMA_PERMIT_DIRECTIO;
1751 break;
Eric Richter02606432016-06-01 13:14:01 -05001752 case Opt_pcr:
Eric Richter02606432016-06-01 13:14:01 -05001753 ima_log_string(ab, "pcr", args[0].from);
1754
1755 result = kstrtoint(args[0].from, 10, &entry->pcr);
1756 if (result || INVALID_PCR(entry->pcr))
1757 result = -EINVAL;
1758 else
1759 entry->flags |= IMA_PCR;
1760
1761 break;
Matthew Garrett19453ce02019-06-19 15:46:11 -07001762 case Opt_template:
1763 ima_log_string(ab, "template", args[0].from);
1764 if (entry->action != MEASURE) {
1765 result = -EINVAL;
1766 break;
1767 }
1768 template_desc = lookup_template_desc(args[0].from);
1769 if (!template_desc || entry->template) {
1770 result = -EINVAL;
1771 break;
1772 }
1773
1774 /*
1775 * template_desc_init_fields() does nothing if
1776 * the template is already initialised, so
1777 * it's safe to do this unconditionally
1778 */
1779 template_desc_init_fields(template_desc->fmt,
1780 &(template_desc->fields),
1781 &(template_desc->num_fields));
1782 entry->template = template_desc;
1783 break;
Mimi Zohar4af46622009-02-04 09:07:00 -05001784 case Opt_err:
Eric Paris2f1506c2010-04-20 10:21:30 -04001785 ima_log_string(ab, "UNKNOWN", p);
Eric Parise9d393b2010-04-20 10:21:13 -04001786 result = -EINVAL;
Mimi Zohar4af46622009-02-04 09:07:00 -05001787 break;
1788 }
1789 }
Tyler Hicks712183432020-07-09 01:19:03 -05001790 if (!result && !ima_validate_rule(entry))
Mimi Zohar4af46622009-02-04 09:07:00 -05001791 result = -EINVAL;
Mimi Zohar6f0911a2018-04-12 00:15:22 -04001792 else if (entry->action == APPRAISE)
1793 temp_ima_appraise |= ima_appraise_flag(entry->func);
1794
Thiago Jung Bauermann3878d502019-06-27 23:19:32 -03001795 if (!result && entry->flags & IMA_MODSIG_ALLOWED) {
1796 template_desc = entry->template ? entry->template :
1797 ima_template_desc_current();
1798 check_template_modsig(template_desc);
1799 }
1800
Eric Parisb0d5de42012-02-14 17:11:07 -05001801 audit_log_format(ab, "res=%d", !result);
Mimi Zohar4af46622009-02-04 09:07:00 -05001802 audit_log_end(ab);
1803 return result;
1804}
1805
1806/**
Mimi Zohar07f6a792011-03-09 22:25:48 -05001807 * ima_parse_add_rule - add a rule to ima_policy_rules
Mimi Zohar4af46622009-02-04 09:07:00 -05001808 * @rule - ima measurement policy rule
1809 *
Petko Manolov38d859f2015-12-02 17:47:54 +02001810 * Avoid locking by allowing just one writer at a time in ima_write_policy()
Eric Paris6ccd0452010-04-20 10:20:54 -04001811 * Returns the length of the rule parsed, an error code on failure
Mimi Zohar4af46622009-02-04 09:07:00 -05001812 */
Eric Paris6ccd0452010-04-20 10:20:54 -04001813ssize_t ima_parse_add_rule(char *rule)
Mimi Zohar4af46622009-02-04 09:07:00 -05001814{
Mimi Zohar52a13282013-12-11 14:44:04 -05001815 static const char op[] = "update_policy";
Eric Paris6ccd0452010-04-20 10:20:54 -04001816 char *p;
Mimi Zohar07f6a792011-03-09 22:25:48 -05001817 struct ima_rule_entry *entry;
Eric Paris6ccd0452010-04-20 10:20:54 -04001818 ssize_t result, len;
Mimi Zohar4af46622009-02-04 09:07:00 -05001819 int audit_info = 0;
1820
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +03001821 p = strsep(&rule, "\n");
1822 len = strlen(p) + 1;
Dmitry Kasatkin71787842014-10-03 14:40:20 +03001823 p += strspn(p, " \t");
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +03001824
Dmitry Kasatkin71787842014-10-03 14:40:20 +03001825 if (*p == '#' || *p == '\0')
Dmitry Kasatkin272a6e92014-10-03 14:40:19 +03001826 return len;
1827
Mimi Zohar4af46622009-02-04 09:07:00 -05001828 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1829 if (!entry) {
1830 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1831 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
1832 return -ENOMEM;
1833 }
1834
1835 INIT_LIST_HEAD(&entry->list);
1836
Eric Paris6ccd0452010-04-20 10:20:54 -04001837 result = ima_parse_rule(p, entry);
Eric Paris7233e3e2010-04-20 10:21:24 -04001838 if (result) {
Tyler Hicks2bdd7372020-07-09 01:19:02 -05001839 ima_free_rule(entry);
Mimi Zohar523979a2009-02-11 11:12:28 -05001840 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
Richard Guy Briggs7e9001f2014-06-16 15:52:07 -04001841 NULL, op, "invalid-policy", result,
Mimi Zohar523979a2009-02-11 11:12:28 -05001842 audit_info);
Eric Paris7233e3e2010-04-20 10:21:24 -04001843 return result;
Mimi Zohar523979a2009-02-11 11:12:28 -05001844 }
Eric Paris7233e3e2010-04-20 10:21:24 -04001845
Petko Manolov38d859f2015-12-02 17:47:54 +02001846 list_add_tail(&entry->list, &ima_temp_rules);
Eric Paris7233e3e2010-04-20 10:21:24 -04001847
1848 return len;
Mimi Zohar4af46622009-02-04 09:07:00 -05001849}
1850
Petko Manolov38d859f2015-12-02 17:47:54 +02001851/**
1852 * ima_delete_rules() called to cleanup invalid in-flight policy.
1853 * We don't need locking as we operate on the temp list, which is
1854 * different from the active one. There is also only one user of
1855 * ima_delete_rules() at a time.
1856 */
James Morris64c61d82009-02-05 09:28:26 +11001857void ima_delete_rules(void)
Mimi Zohar4af46622009-02-04 09:07:00 -05001858{
Mimi Zohar07f6a792011-03-09 22:25:48 -05001859 struct ima_rule_entry *entry, *tmp;
Mimi Zohar4af46622009-02-04 09:07:00 -05001860
Mimi Zohar6ad6afa2015-12-07 14:35:47 -05001861 temp_ima_appraise = 0;
Petko Manolov38d859f2015-12-02 17:47:54 +02001862 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
Mimi Zohar4af46622009-02-04 09:07:00 -05001863 list_del(&entry->list);
Tyler Hicks465aee72020-07-09 01:19:01 -05001864 ima_free_rule(entry);
Mimi Zohar4af46622009-02-04 09:07:00 -05001865 }
Mimi Zohar4af46622009-02-04 09:07:00 -05001866}
Petko Manolov80eae202015-12-02 17:47:56 +02001867
Lakshmi Ramasubramanian34e980b2020-06-18 14:10:12 -07001868#define __ima_hook_stringify(func, str) (#func),
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -03001869
1870const char *const func_tokens[] = {
1871 __ima_hooks(__ima_hook_stringify)
1872};
1873
Petko Manolov80eae202015-12-02 17:47:56 +02001874#ifdef CONFIG_IMA_READ_POLICY
1875enum {
1876 mask_exec = 0, mask_write, mask_read, mask_append
1877};
1878
Thiago Jung Bauermannbb543e32017-06-07 22:49:10 -03001879static const char *const mask_tokens[] = {
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001880 "^MAY_EXEC",
1881 "^MAY_WRITE",
1882 "^MAY_READ",
1883 "^MAY_APPEND"
Petko Manolov80eae202015-12-02 17:47:56 +02001884};
1885
Petko Manolov80eae202015-12-02 17:47:56 +02001886void *ima_policy_start(struct seq_file *m, loff_t *pos)
1887{
1888 loff_t l = *pos;
1889 struct ima_rule_entry *entry;
liqiongeb0782b2021-10-09 18:38:21 +08001890 struct list_head *ima_rules_tmp;
Petko Manolov80eae202015-12-02 17:47:56 +02001891
1892 rcu_read_lock();
liqiongeb0782b2021-10-09 18:38:21 +08001893 ima_rules_tmp = rcu_dereference(ima_rules);
1894 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
Petko Manolov80eae202015-12-02 17:47:56 +02001895 if (!l--) {
1896 rcu_read_unlock();
1897 return entry;
1898 }
1899 }
1900 rcu_read_unlock();
1901 return NULL;
1902}
1903
1904void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
1905{
1906 struct ima_rule_entry *entry = v;
1907
1908 rcu_read_lock();
1909 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
1910 rcu_read_unlock();
1911 (*pos)++;
1912
liqiongeb0782b2021-10-09 18:38:21 +08001913 return (&entry->list == &ima_default_rules ||
1914 &entry->list == &ima_policy_rules) ? NULL : entry;
Petko Manolov80eae202015-12-02 17:47:56 +02001915}
1916
1917void ima_policy_stop(struct seq_file *m, void *v)
1918{
1919}
1920
Mimi Zohar1a9430d2018-12-17 19:14:49 -05001921#define pt(token) policy_tokens[token].pattern
Petko Manolov80eae202015-12-02 17:47:56 +02001922#define mt(token) mask_tokens[token]
Petko Manolov80eae202015-12-02 17:47:56 +02001923
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001924/*
1925 * policy_func_show - display the ima_hooks policy rule
1926 */
1927static void policy_func_show(struct seq_file *m, enum ima_hooks func)
1928{
Thiago Jung Bauermann26632182017-06-07 22:49:11 -03001929 if (func > 0 && func < MAX_CHECK)
1930 seq_printf(m, "func=%s ", func_tokens[func]);
1931 else
1932 seq_printf(m, "func=%d ", func);
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001933}
1934
Tyler Hicks176377d2020-08-11 14:26:20 -05001935static void ima_show_rule_opt_list(struct seq_file *m,
1936 const struct ima_rule_opt_list *opt_list)
1937{
1938 size_t i;
1939
1940 for (i = 0; i < opt_list->count; i++)
1941 seq_printf(m, "%s%s", i ? "|" : "", opt_list->items[i]);
1942}
1943
THOBY Simon583a80a2021-08-16 08:11:00 +00001944static void ima_policy_show_appraise_algos(struct seq_file *m,
1945 unsigned int allowed_hashes)
1946{
1947 int idx, list_size = 0;
1948
1949 for (idx = 0; idx < HASH_ALGO__LAST; idx++) {
1950 if (!(allowed_hashes & (1U << idx)))
1951 continue;
1952
1953 /* only add commas if the list contains multiple entries */
1954 if (list_size++)
1955 seq_puts(m, ",");
1956
1957 seq_puts(m, hash_algo_name[idx]);
1958 }
1959}
1960
Petko Manolov80eae202015-12-02 17:47:56 +02001961int ima_policy_show(struct seq_file *m, void *v)
1962{
1963 struct ima_rule_entry *entry = v;
Andy Shevchenkob8b57272016-05-20 17:00:57 -07001964 int i;
Petko Manolov80eae202015-12-02 17:47:56 +02001965 char tbuf[64] = {0,};
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001966 int offset = 0;
Petko Manolov80eae202015-12-02 17:47:56 +02001967
1968 rcu_read_lock();
1969
Stefan Berger89677192022-02-01 15:37:10 -05001970 /* Do not print rules with inactive LSM labels */
1971 for (i = 0; i < MAX_LSM_RULES; i++) {
1972 if (entry->lsm[i].args_p && !entry->lsm[i].rule) {
1973 rcu_read_unlock();
1974 return 0;
1975 }
1976 }
1977
Petko Manolov80eae202015-12-02 17:47:56 +02001978 if (entry->action & MEASURE)
1979 seq_puts(m, pt(Opt_measure));
1980 if (entry->action & DONT_MEASURE)
1981 seq_puts(m, pt(Opt_dont_measure));
1982 if (entry->action & APPRAISE)
1983 seq_puts(m, pt(Opt_appraise));
1984 if (entry->action & DONT_APPRAISE)
1985 seq_puts(m, pt(Opt_dont_appraise));
1986 if (entry->action & AUDIT)
1987 seq_puts(m, pt(Opt_audit));
Mimi Zoharda1b0022016-09-29 10:04:52 -04001988 if (entry->action & HASH)
1989 seq_puts(m, pt(Opt_hash));
1990 if (entry->action & DONT_HASH)
1991 seq_puts(m, pt(Opt_dont_hash));
Petko Manolov80eae202015-12-02 17:47:56 +02001992
1993 seq_puts(m, " ");
1994
Mimi Zoharb5269ab2016-01-28 13:10:36 -05001995 if (entry->flags & IMA_FUNC)
1996 policy_func_show(m, entry->func);
Petko Manolov80eae202015-12-02 17:47:56 +02001997
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02001998 if ((entry->flags & IMA_MASK) || (entry->flags & IMA_INMASK)) {
1999 if (entry->flags & IMA_MASK)
2000 offset = 1;
Petko Manolov80eae202015-12-02 17:47:56 +02002001 if (entry->mask & MAY_EXEC)
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02002002 seq_printf(m, pt(Opt_mask), mt(mask_exec) + offset);
Petko Manolov80eae202015-12-02 17:47:56 +02002003 if (entry->mask & MAY_WRITE)
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02002004 seq_printf(m, pt(Opt_mask), mt(mask_write) + offset);
Petko Manolov80eae202015-12-02 17:47:56 +02002005 if (entry->mask & MAY_READ)
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02002006 seq_printf(m, pt(Opt_mask), mt(mask_read) + offset);
Petko Manolov80eae202015-12-02 17:47:56 +02002007 if (entry->mask & MAY_APPEND)
Roberto Sassu8cdc23a2019-05-29 15:30:35 +02002008 seq_printf(m, pt(Opt_mask), mt(mask_append) + offset);
Petko Manolov80eae202015-12-02 17:47:56 +02002009 seq_puts(m, " ");
2010 }
2011
2012 if (entry->flags & IMA_FSMAGIC) {
2013 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
2014 seq_printf(m, pt(Opt_fsmagic), tbuf);
2015 seq_puts(m, " ");
2016 }
2017
Mimi Zoharf1b08bb2018-01-15 11:20:36 -05002018 if (entry->flags & IMA_FSNAME) {
2019 snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname);
2020 seq_printf(m, pt(Opt_fsname), tbuf);
2021 seq_puts(m, " ");
2022 }
2023
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08002024 if (entry->flags & IMA_KEYRINGS) {
Tyler Hicks176377d2020-08-11 14:26:20 -05002025 seq_puts(m, "keyrings=");
2026 ima_show_rule_opt_list(m, entry->keyrings);
Lakshmi Ramasubramanian2b60c0e2019-12-11 08:47:07 -08002027 seq_puts(m, " ");
2028 }
2029
Tushar Sugandhi47d76a42021-01-07 20:07:05 -08002030 if (entry->flags & IMA_LABEL) {
2031 seq_puts(m, "label=");
2032 ima_show_rule_opt_list(m, entry->label);
2033 seq_puts(m, " ");
2034 }
2035
Eric Richter02606432016-06-01 13:14:01 -05002036 if (entry->flags & IMA_PCR) {
2037 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
2038 seq_printf(m, pt(Opt_pcr), tbuf);
2039 seq_puts(m, " ");
2040 }
2041
Petko Manolov80eae202015-12-02 17:47:56 +02002042 if (entry->flags & IMA_FSUUID) {
Christoph Hellwig787d8c52017-06-01 07:00:26 +02002043 seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
Petko Manolov80eae202015-12-02 17:47:56 +02002044 seq_puts(m, " ");
2045 }
2046
2047 if (entry->flags & IMA_UID) {
2048 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03002049 if (entry->uid_op == &uid_gt)
2050 seq_printf(m, pt(Opt_uid_gt), tbuf);
2051 else if (entry->uid_op == &uid_lt)
2052 seq_printf(m, pt(Opt_uid_lt), tbuf);
2053 else
2054 seq_printf(m, pt(Opt_uid_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02002055 seq_puts(m, " ");
2056 }
2057
2058 if (entry->flags & IMA_EUID) {
2059 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03002060 if (entry->uid_op == &uid_gt)
2061 seq_printf(m, pt(Opt_euid_gt), tbuf);
2062 else if (entry->uid_op == &uid_lt)
2063 seq_printf(m, pt(Opt_euid_lt), tbuf);
2064 else
2065 seq_printf(m, pt(Opt_euid_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02002066 seq_puts(m, " ");
2067 }
2068
Curtis Veit40224c42021-10-07 14:03:02 -06002069 if (entry->flags & IMA_GID) {
2070 snprintf(tbuf, sizeof(tbuf), "%d", __kgid_val(entry->gid));
2071 if (entry->gid_op == &gid_gt)
2072 seq_printf(m, pt(Opt_gid_gt), tbuf);
2073 else if (entry->gid_op == &gid_lt)
2074 seq_printf(m, pt(Opt_gid_lt), tbuf);
2075 else
2076 seq_printf(m, pt(Opt_gid_eq), tbuf);
2077 seq_puts(m, " ");
2078 }
2079
2080 if (entry->flags & IMA_EGID) {
2081 snprintf(tbuf, sizeof(tbuf), "%d", __kgid_val(entry->gid));
2082 if (entry->gid_op == &gid_gt)
2083 seq_printf(m, pt(Opt_egid_gt), tbuf);
2084 else if (entry->gid_op == &gid_lt)
2085 seq_printf(m, pt(Opt_egid_lt), tbuf);
2086 else
2087 seq_printf(m, pt(Opt_egid_eq), tbuf);
2088 seq_puts(m, " ");
2089 }
2090
Petko Manolov80eae202015-12-02 17:47:56 +02002091 if (entry->flags & IMA_FOWNER) {
2092 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
Mikhail Kurinnoi3dd0c8d2017-01-27 19:23:01 +03002093 if (entry->fowner_op == &uid_gt)
2094 seq_printf(m, pt(Opt_fowner_gt), tbuf);
2095 else if (entry->fowner_op == &uid_lt)
2096 seq_printf(m, pt(Opt_fowner_lt), tbuf);
2097 else
2098 seq_printf(m, pt(Opt_fowner_eq), tbuf);
Petko Manolov80eae202015-12-02 17:47:56 +02002099 seq_puts(m, " ");
2100 }
2101
Curtis Veit40224c42021-10-07 14:03:02 -06002102 if (entry->flags & IMA_FGROUP) {
2103 snprintf(tbuf, sizeof(tbuf), "%d", __kgid_val(entry->fgroup));
2104 if (entry->fgroup_op == &gid_gt)
2105 seq_printf(m, pt(Opt_fgroup_gt), tbuf);
2106 else if (entry->fgroup_op == &gid_lt)
2107 seq_printf(m, pt(Opt_fgroup_lt), tbuf);
2108 else
2109 seq_printf(m, pt(Opt_fgroup_eq), tbuf);
2110 seq_puts(m, " ");
2111 }
2112
THOBY Simon583a80a2021-08-16 08:11:00 +00002113 if (entry->flags & IMA_VALIDATE_ALGOS) {
2114 seq_puts(m, "appraise_algos=");
2115 ima_policy_show_appraise_algos(m, entry->allowed_algos);
2116 seq_puts(m, " ");
2117 }
2118
Petko Manolov80eae202015-12-02 17:47:56 +02002119 for (i = 0; i < MAX_LSM_RULES; i++) {
2120 if (entry->lsm[i].rule) {
2121 switch (i) {
2122 case LSM_OBJ_USER:
2123 seq_printf(m, pt(Opt_obj_user),
Tyler Hicksaa0c0222020-07-09 01:19:08 -05002124 entry->lsm[i].args_p);
Petko Manolov80eae202015-12-02 17:47:56 +02002125 break;
2126 case LSM_OBJ_ROLE:
2127 seq_printf(m, pt(Opt_obj_role),
Tyler Hicksaa0c0222020-07-09 01:19:08 -05002128 entry->lsm[i].args_p);
Petko Manolov80eae202015-12-02 17:47:56 +02002129 break;
2130 case LSM_OBJ_TYPE:
2131 seq_printf(m, pt(Opt_obj_type),
Tyler Hicksaa0c0222020-07-09 01:19:08 -05002132 entry->lsm[i].args_p);
Petko Manolov80eae202015-12-02 17:47:56 +02002133 break;
2134 case LSM_SUBJ_USER:
2135 seq_printf(m, pt(Opt_subj_user),
Tyler Hicksaa0c0222020-07-09 01:19:08 -05002136 entry->lsm[i].args_p);
Petko Manolov80eae202015-12-02 17:47:56 +02002137 break;
2138 case LSM_SUBJ_ROLE:
2139 seq_printf(m, pt(Opt_subj_role),
Tyler Hicksaa0c0222020-07-09 01:19:08 -05002140 entry->lsm[i].args_p);
Petko Manolov80eae202015-12-02 17:47:56 +02002141 break;
2142 case LSM_SUBJ_TYPE:
2143 seq_printf(m, pt(Opt_subj_type),
Tyler Hicksaa0c0222020-07-09 01:19:08 -05002144 entry->lsm[i].args_p);
Petko Manolov80eae202015-12-02 17:47:56 +02002145 break;
2146 }
Clay Chang5350ceb2020-01-05 09:18:13 +08002147 seq_puts(m, " ");
Petko Manolov80eae202015-12-02 17:47:56 +02002148 }
2149 }
Matthew Garrett19453ce02019-06-19 15:46:11 -07002150 if (entry->template)
2151 seq_printf(m, "template=%s ", entry->template->name);
Thiago Jung Bauermann9044d622019-06-27 23:19:28 -03002152 if (entry->flags & IMA_DIGSIG_REQUIRED) {
2153 if (entry->flags & IMA_MODSIG_ALLOWED)
2154 seq_puts(m, "appraise_type=imasig|modsig ");
2155 else
2156 seq_puts(m, "appraise_type=imasig ");
2157 }
Nayna Jain273df862019-10-30 23:31:32 -04002158 if (entry->flags & IMA_CHECK_BLACKLIST)
2159 seq_puts(m, "appraise_flag=check_blacklist ");
Petko Manolov80eae202015-12-02 17:47:56 +02002160 if (entry->flags & IMA_PERMIT_DIRECTIO)
2161 seq_puts(m, "permit_directio ");
2162 rcu_read_unlock();
2163 seq_puts(m, "\n");
2164 return 0;
2165}
2166#endif /* CONFIG_IMA_READ_POLICY */
Matthew Garrett29d3c1c2019-08-19 17:18:01 -07002167
2168#if defined(CONFIG_IMA_APPRAISE) && defined(CONFIG_INTEGRITY_TRUSTED_KEYRING)
2169/*
2170 * ima_appraise_signature: whether IMA will appraise a given function using
2171 * an IMA digital signature. This is restricted to cases where the kernel
2172 * has a set of built-in trusted keys in order to avoid an attacker simply
2173 * loading additional keys.
2174 */
2175bool ima_appraise_signature(enum kernel_read_file_id id)
2176{
2177 struct ima_rule_entry *entry;
2178 bool found = false;
2179 enum ima_hooks func;
liqiongeb0782b2021-10-09 18:38:21 +08002180 struct list_head *ima_rules_tmp;
Matthew Garrett29d3c1c2019-08-19 17:18:01 -07002181
2182 if (id >= READING_MAX_ID)
2183 return false;
2184
2185 func = read_idmap[id] ?: FILE_CHECK;
2186
2187 rcu_read_lock();
liqiongeb0782b2021-10-09 18:38:21 +08002188 ima_rules_tmp = rcu_dereference(ima_rules);
2189 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
Matthew Garrett29d3c1c2019-08-19 17:18:01 -07002190 if (entry->action != APPRAISE)
2191 continue;
2192
2193 /*
2194 * A generic entry will match, but otherwise require that it
2195 * match the func we're looking for
2196 */
2197 if (entry->func && entry->func != func)
2198 continue;
2199
2200 /*
2201 * We require this to be a digital signature, not a raw IMA
2202 * hash.
2203 */
2204 if (entry->flags & IMA_DIGSIG_REQUIRED)
2205 found = true;
2206
2207 /*
2208 * We've found a rule that matches, so break now even if it
2209 * didn't require a digital signature - a later rule that does
2210 * won't override it, so would be a false positive.
2211 */
2212 break;
2213 }
2214
2215 rcu_read_unlock();
2216 return found;
2217}
2218#endif /* CONFIG_IMA_APPRAISE && CONFIG_INTEGRITY_TRUSTED_KEYRING */