blob: 2272c3255c7d8bb0a80895c5074056f3b838d6d6 [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/*
Paul Gortmaker4f83d5e2018-12-09 15:36:33 -05003 * Integrity Measurement Architecture
4 *
Mimi Zohar3323eec2009-02-04 09:06:58 -05005 * Copyright (C) 2005,2006,2007,2008 IBM Corporation
6 *
7 * Authors:
8 * Reiner Sailer <sailer@watson.ibm.com>
9 * Serge Hallyn <serue@us.ibm.com>
10 * Kylene Hall <kylene@us.ibm.com>
11 * Mimi Zohar <zohar@us.ibm.com>
12 *
Mimi Zohar3323eec2009-02-04 09:06:58 -050013 * File: ima_main.c
Eric Parise0d5bd22009-12-04 15:48:00 -050014 * implements the IMA hooks: ima_bprm_check, ima_file_mmap,
Mimi Zohar9bbb6ca2010-01-26 17:02:40 -050015 * and ima_file_check.
Mimi Zohar3323eec2009-02-04 09:06:58 -050016 */
Petr Vorelab603682018-03-23 14:41:08 +010017
18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Paul Gortmakerb49d5642018-12-14 16:48:07 -050020#include <linux/module.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050021#include <linux/file.h>
22#include <linux/binfmts.h>
23#include <linux/mount.h>
24#include <linux/mman.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050026#include <linux/xattr.h>
James Morrisd5813a52011-08-30 10:19:50 +100027#include <linux/ima.h>
Jeff Layton3b370b22017-12-11 06:35:21 -050028#include <linux/iversion.h>
Mimi Zohard77ccdc2018-02-21 11:35:20 -050029#include <linux/fs.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050030
31#include "ima.h"
32
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050033#ifdef CONFIG_IMA_APPRAISE
34int ima_appraise = IMA_APPRAISE_ENFORCE;
35#else
36int ima_appraise;
37#endif
38
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +030039int ima_hash_algo = HASH_ALGO_SHA1;
Mimi Zohare7a2ad72013-06-07 12:16:37 +020040static int hash_setup_done;
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +030041
Janne Karhunenb1694242019-06-14 15:20:15 +030042static struct notifier_block ima_lsm_policy_notifier = {
43 .notifier_call = ima_lsm_policy_change,
44};
45
Mimi Zohar3323eec2009-02-04 09:06:58 -050046static int __init hash_setup(char *str)
47{
Mimi Zohare7a2ad72013-06-07 12:16:37 +020048 struct ima_template_desc *template_desc = ima_template_desc_current();
49 int i;
50
51 if (hash_setup_done)
52 return 1;
53
54 if (strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) == 0) {
55 if (strncmp(str, "sha1", 4) == 0)
56 ima_hash_algo = HASH_ALGO_SHA1;
57 else if (strncmp(str, "md5", 3) == 0)
58 ima_hash_algo = HASH_ALGO_MD5;
Boshi Wangebe7c0a2017-10-20 16:01:03 +080059 else
60 return 1;
Mimi Zohare7a2ad72013-06-07 12:16:37 +020061 goto out;
62 }
63
Yisheng Xieb4df8602018-05-21 19:58:02 +080064 i = match_string(hash_algo_name, HASH_ALGO__LAST, str);
65 if (i < 0)
Boshi Wangebe7c0a2017-10-20 16:01:03 +080066 return 1;
Yisheng Xieb4df8602018-05-21 19:58:02 +080067
68 ima_hash_algo = i;
Mimi Zohare7a2ad72013-06-07 12:16:37 +020069out:
70 hash_setup_done = 1;
Mimi Zohar3323eec2009-02-04 09:06:58 -050071 return 1;
72}
73__setup("ima_hash=", hash_setup);
74
Mimi Zohar2cd47372019-04-30 08:34:44 -040075/* Prevent mmap'ing a file execute that is already mmap'ed write */
76static int mmap_violation_check(enum ima_hooks func, struct file *file,
77 char **pathbuf, const char **pathname,
78 char *filename)
79{
80 struct inode *inode;
81 int rc = 0;
82
83 if ((func == MMAP_CHECK) && mapping_writably_mapped(file->f_mapping)) {
84 rc = -ETXTBSY;
85 inode = file_inode(file);
86
87 if (!*pathbuf) /* ima_rdwr_violation possibly pre-fetched */
88 *pathname = ima_d_path(&file->f_path, pathbuf,
89 filename);
90 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, *pathname,
91 "mmap_file", "mmapped_writers", rc, 0);
92 }
93 return rc;
94}
95
Eric Parise0d5bd22009-12-04 15:48:00 -050096/*
Mimi Zohar890275b52010-11-02 10:13:07 -040097 * ima_rdwr_violation_check
Mimi Zohar8eb988c2010-01-20 15:35:41 -050098 *
Mimi Zohar890275b52010-11-02 10:13:07 -040099 * Only invalidate the PCR for measured files:
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200100 * - Opening a file for write when already open for read,
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500101 * results in a time of measure, time of use (ToMToU) error.
102 * - Opening a file for read when already open for write,
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200103 * could result in a file measurement error.
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500104 *
105 */
Roberto Sassuf7a859f2014-09-12 19:35:55 +0200106static void ima_rdwr_violation_check(struct file *file,
107 struct integrity_iint_cache *iint,
Roberto Sassu1b68bdf2014-09-12 19:35:56 +0200108 int must_measure,
Roberto Sassuf7a859f2014-09-12 19:35:55 +0200109 char **pathbuf,
Roberto Sassu4e8581e2017-11-30 11:56:02 +0100110 const char **pathname,
111 char *filename)
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500112{
David Howellsc77cece2013-06-13 23:37:49 +0100113 struct inode *inode = file_inode(file);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500114 fmode_t mode = file->f_mode;
Eric Parisad16ad02010-10-25 14:41:45 -0400115 bool send_tomtou = false, send_writers = false;
Eric Parisa178d202010-10-25 14:41:59 -0400116
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500117 if (mode & FMODE_WRITE) {
Dmitry Kasatkin14503eb2014-03-27 10:29:28 +0200118 if (atomic_read(&inode->i_readcount) && IS_IMA(inode)) {
Roberto Sassuf7a859f2014-09-12 19:35:55 +0200119 if (!iint)
120 iint = integrity_iint_find(inode);
Dmitry Kasatkin14503eb2014-03-27 10:29:28 +0200121 /* IMA_MEASURE is set from reader side */
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200122 if (iint && test_bit(IMA_MUST_MEASURE,
123 &iint->atomic_flags))
Dmitry Kasatkin14503eb2014-03-27 10:29:28 +0200124 send_tomtou = true;
125 }
Dmitry Kasatkinb882fae2014-03-27 10:54:11 +0200126 } else {
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200127 if (must_measure)
128 set_bit(IMA_MUST_MEASURE, &iint->atomic_flags);
Nikolay Borisoveed9de32018-12-11 10:31:40 +0200129 if (inode_is_open_for_write(inode) && must_measure)
Dmitry Kasatkinb882fae2014-03-27 10:54:11 +0200130 send_writers = true;
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500131 }
Eric Parisad16ad02010-10-25 14:41:45 -0400132
Mimi Zohar08e1b762012-06-20 09:32:55 -0400133 if (!send_tomtou && !send_writers)
134 return;
135
Mimi Zoharbc15ed62017-01-17 06:45:41 -0500136 *pathname = ima_d_path(&file->f_path, pathbuf, filename);
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300137
Eric Parisad16ad02010-10-25 14:41:45 -0400138 if (send_tomtou)
Roberto Sassu8d94eb92015-04-11 17:12:39 +0200139 ima_add_violation(file, *pathname, iint,
140 "invalid_pcr", "ToMToU");
Eric Parisad16ad02010-10-25 14:41:45 -0400141 if (send_writers)
Roberto Sassu8d94eb92015-04-11 17:12:39 +0200142 ima_add_violation(file, *pathname, iint,
Mimi Zohar08e1b762012-06-20 09:32:55 -0400143 "invalid_pcr", "open_writers");
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500144}
145
Mimi Zoharf381c272011-03-09 14:13:22 -0500146static void ima_check_last_writer(struct integrity_iint_cache *iint,
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500147 struct inode *inode, struct file *file)
Eric Parisbc7d2a32010-10-25 14:42:05 -0400148{
Al Viro4b2a2c62011-07-26 04:30:35 -0400149 fmode_t mode = file->f_mode;
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200150 bool update;
Eric Paris497f3232010-10-25 14:41:32 -0400151
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500152 if (!(mode & FMODE_WRITE))
153 return;
154
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200155 mutex_lock(&iint->mutex);
Dmitry Kasatkinb151d6b2014-06-27 18:04:27 +0300156 if (atomic_read(&inode->i_writecount) == 1) {
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200157 update = test_and_clear_bit(IMA_UPDATE_XATTR,
158 &iint->atomic_flags);
Sascha Hauerac0bf022017-12-11 06:35:20 -0500159 if (!IS_I_VERSION(inode) ||
Goffredo Baroncellic472c072018-02-01 08:15:25 -0500160 !inode_eq_iversion(inode, iint->version) ||
Dmitry Kasatkinb151d6b2014-06-27 18:04:27 +0300161 (iint->flags & IMA_NEW_FILE)) {
162 iint->flags &= ~(IMA_DONE_MASK | IMA_NEW_FILE);
Eric Richtera4226382016-06-01 13:14:06 -0500163 iint->measured_pcrs = 0;
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200164 if (update)
Dmitry Kasatkinb151d6b2014-06-27 18:04:27 +0300165 ima_update_xattr(iint, file);
166 }
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500167 }
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200168 mutex_unlock(&iint->mutex);
Eric Parisbc7d2a32010-10-25 14:42:05 -0400169}
170
Mimi Zohar3323eec2009-02-04 09:06:58 -0500171/**
172 * ima_file_free - called on __fput()
173 * @file: pointer to file structure being freed
174 *
Mimi Zohar890275b52010-11-02 10:13:07 -0400175 * Flag files that changed, based on i_version
Mimi Zohar3323eec2009-02-04 09:06:58 -0500176 */
177void ima_file_free(struct file *file)
178{
Al Viro496ad9a2013-01-23 17:07:38 -0500179 struct inode *inode = file_inode(file);
Mimi Zoharf381c272011-03-09 14:13:22 -0500180 struct integrity_iint_cache *iint;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500181
Dmitry Kasatkin0f34a002014-09-24 11:05:10 +0300182 if (!ima_policy_flag || !S_ISREG(inode->i_mode))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500183 return;
Eric Paris196f5182010-10-25 14:42:19 -0400184
Mimi Zoharf381c272011-03-09 14:13:22 -0500185 iint = integrity_iint_find(inode);
Mimi Zohar854fdd52010-11-02 10:14:22 -0400186 if (!iint)
187 return;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500188
Mimi Zohar854fdd52010-11-02 10:14:22 -0400189 ima_check_last_writer(iint, inode, file);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500190}
191
Matthew Garrettd906c102018-01-08 13:36:20 -0800192static int process_measurement(struct file *file, const struct cred *cred,
193 u32 secid, char *buf, loff_t size, int mask,
Al Viro6035a272018-06-08 13:40:10 -0400194 enum ima_hooks func)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500195{
Al Viro496ad9a2013-01-23 17:07:38 -0500196 struct inode *inode = file_inode(file);
Roberto Sassuf7a859f2014-09-12 19:35:55 +0200197 struct integrity_iint_cache *iint = NULL;
Matthew Garrett19453ce02019-06-19 15:46:11 -0700198 struct ima_template_desc *template_desc = NULL;
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300199 char *pathbuf = NULL;
Mimi Zoharbc15ed62017-01-17 06:45:41 -0500200 char filename[NAME_MAX];
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300201 const char *pathname = NULL;
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200202 int rc = 0, action, must_appraise = 0;
Eric Richter725de7f2016-06-01 13:14:02 -0500203 int pcr = CONFIG_IMA_MEASURE_PCR_IDX;
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200204 struct evm_ima_xattr_data *xattr_value = NULL;
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -0300205 struct modsig *modsig = NULL;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300206 int xattr_len = 0;
Roberto Sassuf7a859f2014-09-12 19:35:55 +0200207 bool violation_check;
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200208 enum hash_algo hash_algo;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500209
Roberto Sassua7560242014-09-12 19:35:54 +0200210 if (!ima_policy_flag || !S_ISREG(inode->i_mode))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500211 return 0;
Eric Parisbc7d2a32010-10-25 14:42:05 -0400212
Mimi Zohard79d72e2012-12-03 17:08:11 -0500213 /* Return an IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT action
214 * bitmask based on the appraise/audit/measurement policy.
215 * Included is the appraise submask.
216 */
Matthew Garrett19453ce02019-06-19 15:46:11 -0700217 action = ima_get_action(inode, cred, secid, mask, func, &pcr,
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800218 &template_desc, NULL);
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500219 violation_check = ((func == FILE_CHECK || func == MMAP_CHECK) &&
Roberto Sassuf7a859f2014-09-12 19:35:55 +0200220 (ima_policy_flag & IMA_MEASURE));
221 if (!action && !violation_check)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500222 return 0;
223
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500224 must_appraise = action & IMA_APPRAISE;
Eric Parisbc7d2a32010-10-25 14:42:05 -0400225
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500226 /* Is the appraise rule hook specific? */
Dmitry Kasatkin3a8a2ea2014-09-03 10:19:57 +0300227 if (action & IMA_FILE_APPRAISE)
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500228 func = FILE_CHECK;
Mimi Zohar5a73fcf2012-12-05 15:14:38 -0500229
Al Viro59551022016-01-22 15:40:57 -0500230 inode_lock(inode);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500231
Roberto Sassuf7a859f2014-09-12 19:35:55 +0200232 if (action) {
233 iint = integrity_inode_get(inode);
234 if (!iint)
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200235 rc = -ENOMEM;
Roberto Sassuf7a859f2014-09-12 19:35:55 +0200236 }
237
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200238 if (!rc && violation_check)
Roberto Sassu1b68bdf2014-09-12 19:35:56 +0200239 ima_rdwr_violation_check(file, iint, action & IMA_MEASURE,
Roberto Sassu4e8581e2017-11-30 11:56:02 +0100240 &pathbuf, &pathname, filename);
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200241
242 inode_unlock(inode);
243
244 if (rc)
245 goto out;
246 if (!action)
247 goto out;
248
249 mutex_lock(&iint->mutex);
250
251 if (test_and_clear_bit(IMA_CHANGE_ATTR, &iint->atomic_flags))
252 /* reset appraisal flags if ima_inode_post_setattr was called */
253 iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED |
254 IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK |
255 IMA_ACTION_FLAGS);
256
Mimi Zohard77ccdc2018-02-21 11:35:20 -0500257 /*
258 * Re-evaulate the file if either the xattr has changed or the
259 * kernel has no way of detecting file change on the filesystem.
260 * (Limited to privileged mounted filesystems.)
261 */
262 if (test_and_clear_bit(IMA_CHANGE_XATTR, &iint->atomic_flags) ||
263 ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
Mimi Zohar9e670282018-02-21 11:36:32 -0500264 !(inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) &&
265 !(action & IMA_FAIL_UNVERIFIABLE_SIGS))) {
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200266 iint->flags &= ~IMA_DONE_MASK;
Mimi Zohard77ccdc2018-02-21 11:35:20 -0500267 iint->measured_pcrs = 0;
268 }
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300269
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500270 /* Determine if already appraised/measured based on bitmask
Mimi Zohard79d72e2012-12-03 17:08:11 -0500271 * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED,
272 * IMA_AUDIT, IMA_AUDITED)
273 */
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500274 iint->flags |= action;
Dmitry Kasatkin0e5a2472012-06-08 13:58:49 +0300275 action &= IMA_DO_MASK;
Eric Richtera4226382016-06-01 13:14:06 -0500276 action &= ~((iint->flags & (IMA_DONE_MASK ^ IMA_MEASURED)) >> 1);
277
278 /* If target pcr is already measured, unset IMA_MEASURE action */
279 if ((action & IMA_MEASURE) && (iint->measured_pcrs & (0x1 << pcr)))
280 action ^= IMA_MEASURE;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500281
Mimi Zoharda1b0022016-09-29 10:04:52 -0400282 /* HASH sets the digital signature and update flags, nothing else */
283 if ((action & IMA_HASH) &&
284 !(test_bit(IMA_DIGSIG, &iint->atomic_flags))) {
285 xattr_len = ima_read_xattr(file_dentry(file), &xattr_value);
286 if ((xattr_value && xattr_len > 2) &&
287 (xattr_value->type == EVM_IMA_XATTR_DIGSIG))
288 set_bit(IMA_DIGSIG, &iint->atomic_flags);
289 iint->flags |= IMA_HASHED;
290 action ^= IMA_HASH;
291 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
292 }
293
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500294 /* Nothing to do, just return existing appraised status */
295 if (!action) {
Mimi Zohar2cd47372019-04-30 08:34:44 -0400296 if (must_appraise) {
297 rc = mmap_violation_check(func, file, &pathbuf,
298 &pathname, filename);
299 if (!rc)
300 rc = ima_get_cache_status(iint, func);
301 }
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200302 goto out_locked;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500303 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500304
Dmitry Kasatkinf68c05f2014-08-22 09:43:55 +0300305 if ((action & IMA_APPRAISE_SUBMASK) ||
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -0300306 strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) != 0) {
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200307 /* read 'security.ima' */
Miklos Szeredie71b9df2016-09-16 12:44:20 +0200308 xattr_len = ima_read_xattr(file_dentry(file), &xattr_value);
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300309
Thiago Jung Bauermanne5092252019-06-27 23:19:33 -0300310 /*
311 * Read the appended modsig if allowed by the policy, and allow
312 * an additional measurement list entry, if needed, based on the
313 * template format and whether the file was already measured.
314 */
315 if (iint->flags & IMA_MODSIG_ALLOWED) {
316 rc = ima_read_modsig(func, buf, size, &modsig);
317
318 if (!rc && ima_template_has_modsig(template_desc) &&
319 iint->flags & IMA_MEASURED)
320 action |= IMA_MEASURE;
321 }
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -0300322 }
323
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200324 hash_algo = ima_get_hash_algo(xattr_value, xattr_len);
325
Thiago Jung Bauermann15588222019-06-27 23:19:31 -0300326 rc = ima_collect_measurement(iint, file, buf, size, hash_algo, modsig);
Mimi Zoharf3cc6b22017-06-17 23:56:23 -0400327 if (rc != 0 && rc != -EBADF && rc != -EINVAL)
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200328 goto out_locked;
Mimi Zohar08e1b762012-06-20 09:32:55 -0400329
Mimi Zoharbc15ed62017-01-17 06:45:41 -0500330 if (!pathbuf) /* ima_rdwr_violation possibly pre-fetched */
331 pathname = ima_d_path(&file->f_path, &pathbuf, filename);
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300332
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500333 if (action & IMA_MEASURE)
Mimi Zoharbcbc9b0c2013-07-23 11:15:00 -0400334 ima_store_measurement(iint, file, pathname,
Thiago Jung Bauermann3878d502019-06-27 23:19:32 -0300335 xattr_value, xattr_len, modsig, pcr,
Matthew Garrett19453ce02019-06-19 15:46:11 -0700336 template_desc);
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200337 if (rc == 0 && (action & IMA_APPRAISE_SUBMASK)) {
Nayna Jain273df862019-10-30 23:31:32 -0400338 rc = ima_check_blacklist(iint, modsig, pcr);
339 if (rc != -EPERM) {
340 inode_lock(inode);
341 rc = ima_appraise_measurement(func, iint, file,
342 pathname, xattr_value,
343 xattr_len, modsig);
344 inode_unlock(inode);
345 }
Mimi Zohar2cd47372019-04-30 08:34:44 -0400346 if (!rc)
347 rc = mmap_violation_check(func, file, &pathbuf,
348 &pathname, filename);
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200349 }
Peter Moodye7c568e2012-06-14 10:04:36 -0700350 if (action & IMA_AUDIT)
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300351 ima_audit_measurement(iint, pathname);
Roberto Sassuf7a859f2014-09-12 19:35:55 +0200352
Mimi Zoharf3cc6b22017-06-17 23:56:23 -0400353 if ((file->f_flags & O_DIRECT) && (iint->flags & IMA_PERMIT_DIRECTIO))
354 rc = 0;
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200355out_locked:
356 if ((mask & MAY_WRITE) && test_bit(IMA_DIGSIG, &iint->atomic_flags) &&
Mimi Zohar05d1a712016-02-29 19:52:05 -0500357 !(iint->flags & IMA_NEW_FILE))
Dmitry Kasatkina175b8b2012-09-27 15:06:28 +0300358 rc = -EACCES;
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200359 mutex_unlock(&iint->mutex);
Roberto Sassuf7a859f2014-09-12 19:35:55 +0200360 kfree(xattr_value);
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -0300361 ima_free_modsig(modsig);
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200362out:
Dmitry Kasatkin456f5fd2014-10-01 21:43:10 +0300363 if (pathbuf)
364 __putname(pathbuf);
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200365 if (must_appraise) {
366 if (rc && (ima_appraise & IMA_APPRAISE_ENFORCE))
367 return -EACCES;
368 if (file->f_mode & FMODE_WRITE)
369 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
370 }
Dmitry Kasatkin750943a2012-09-27 15:57:10 +0300371 return 0;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500372}
373
374/**
375 * ima_file_mmap - based on policy, collect/store measurement.
376 * @file: pointer to the file to be measured (May be NULL)
377 * @prot: contains the protection that will be applied by the kernel.
378 *
379 * Measure files being mmapped executable based on the ima_must_measure()
380 * policy decision.
381 *
Dmitry Kasatkin750943a2012-09-27 15:57:10 +0300382 * On success return 0. On integrity appraisal error, assuming the file
383 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500384 */
385int ima_file_mmap(struct file *file, unsigned long prot)
386{
Matthew Garrettd906c102018-01-08 13:36:20 -0800387 u32 secid;
388
389 if (file && (prot & PROT_EXEC)) {
390 security_task_getsecid(current, &secid);
391 return process_measurement(file, current_cred(), secid, NULL,
Al Viro6035a272018-06-08 13:40:10 -0400392 0, MAY_EXEC, MMAP_CHECK);
Matthew Garrettd906c102018-01-08 13:36:20 -0800393 }
394
Dmitry Kasatkin750943a2012-09-27 15:57:10 +0300395 return 0;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500396}
397
398/**
399 * ima_bprm_check - based on policy, collect/store measurement.
400 * @bprm: contains the linux_binprm structure
401 *
402 * The OS protects against an executable file, already open for write,
403 * from being executed in deny_write_access() and an executable file,
404 * already open for execute, from being modified in get_write_access().
405 * So we can be certain that what we verify and measure here is actually
406 * what is being executed.
407 *
Dmitry Kasatkin750943a2012-09-27 15:57:10 +0300408 * On success return 0. On integrity appraisal error, assuming the file
409 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500410 */
411int ima_bprm_check(struct linux_binprm *bprm)
412{
Matthew Garrettd906c102018-01-08 13:36:20 -0800413 int ret;
414 u32 secid;
415
416 security_task_getsecid(current, &secid);
417 ret = process_measurement(bprm->file, current_cred(), secid, NULL, 0,
Al Viro6035a272018-06-08 13:40:10 -0400418 MAY_EXEC, BPRM_CHECK);
Matthew Garrettd906c102018-01-08 13:36:20 -0800419 if (ret)
420 return ret;
421
422 security_cred_getsecid(bprm->cred, &secid);
423 return process_measurement(bprm->file, bprm->cred, secid, NULL, 0,
Al Viro6035a272018-06-08 13:40:10 -0400424 MAY_EXEC, CREDS_CHECK);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500425}
426
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500427/**
428 * ima_path_check - based on policy, collect/store measurement.
429 * @file: pointer to the file to be measured
Lans Zhang20f482a2017-01-06 12:38:11 +0800430 * @mask: contains MAY_READ, MAY_WRITE, MAY_EXEC or MAY_APPEND
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500431 *
432 * Measure files based on the ima_must_measure() policy decision.
433 *
Dmitry Kasatkin750943a2012-09-27 15:57:10 +0300434 * On success return 0. On integrity appraisal error, assuming the file
435 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500436 */
Al Viro6035a272018-06-08 13:40:10 -0400437int ima_file_check(struct file *file, int mask)
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500438{
Matthew Garrettd906c102018-01-08 13:36:20 -0800439 u32 secid;
440
441 security_task_getsecid(current, &secid);
442 return process_measurement(file, current_cred(), secid, NULL, 0,
Lans Zhang20f482a2017-01-06 12:38:11 +0800443 mask & (MAY_READ | MAY_WRITE | MAY_EXEC |
Al Viro6035a272018-06-08 13:40:10 -0400444 MAY_APPEND), FILE_CHECK);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500445}
Mimi Zohar9bbb6ca2010-01-26 17:02:40 -0500446EXPORT_SYMBOL_GPL(ima_file_check);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500447
Mimi Zoharfdf90722012-10-16 12:40:08 +1030448/**
Mimi Zoharfdb24102019-01-22 14:06:49 -0600449 * ima_post_create_tmpfile - mark newly created tmpfile as new
450 * @file : newly created tmpfile
451 *
452 * No measuring, appraising or auditing of newly created tmpfiles is needed.
453 * Skip calling process_measurement(), but indicate which newly, created
454 * tmpfiles are in policy.
455 */
456void ima_post_create_tmpfile(struct inode *inode)
457{
458 struct integrity_iint_cache *iint;
459 int must_appraise;
460
461 must_appraise = ima_must_appraise(inode, MAY_ACCESS, FILE_CHECK);
462 if (!must_appraise)
463 return;
464
465 /* Nothing to do if we can't allocate memory */
466 iint = integrity_inode_get(inode);
467 if (!iint)
468 return;
469
470 /* needed for writing the security xattrs */
471 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
472 iint->ima_file_status = INTEGRITY_PASS;
473}
474
475/**
Mimi Zohar05d1a712016-02-29 19:52:05 -0500476 * ima_post_path_mknod - mark as a new inode
477 * @dentry: newly created dentry
478 *
479 * Mark files created via the mknodat syscall as new, so that the
480 * file data can be written later.
481 */
482void ima_post_path_mknod(struct dentry *dentry)
483{
484 struct integrity_iint_cache *iint;
485 struct inode *inode = dentry->d_inode;
486 int must_appraise;
487
488 must_appraise = ima_must_appraise(inode, MAY_ACCESS, FILE_CHECK);
489 if (!must_appraise)
490 return;
491
Mimi Zoharfdb24102019-01-22 14:06:49 -0600492 /* Nothing to do if we can't allocate memory */
Mimi Zohar05d1a712016-02-29 19:52:05 -0500493 iint = integrity_inode_get(inode);
Mimi Zoharfdb24102019-01-22 14:06:49 -0600494 if (!iint)
495 return;
496
497 /* needed for re-opening empty files */
498 iint->flags |= IMA_NEW_FILE;
Mimi Zohar05d1a712016-02-29 19:52:05 -0500499}
500
501/**
Mimi Zohar39eeb4f2016-01-30 22:23:26 -0500502 * ima_read_file - pre-measure/appraise hook decision based on policy
503 * @file: pointer to the file to be measured/appraised/audit
504 * @read_id: caller identifier
505 *
506 * Permit reading a file based on policy. The policy rules are written
507 * in terms of the policy identifier. Appraising the integrity of
508 * a file requires a file descriptor.
509 *
510 * For permission return 0, otherwise return -EACCES.
511 */
512int ima_read_file(struct file *file, enum kernel_read_file_id read_id)
513{
Mimi Zohar4f0496d2018-07-13 14:06:03 -0400514 /*
515 * READING_FIRMWARE_PREALLOC_BUFFER
516 *
517 * Do devices using pre-allocated memory run the risk of the
518 * firmware being accessible to the device prior to the completion
519 * of IMA's signature verification any more than when using two
520 * buffers?
521 */
Mimi Zohar39eeb4f2016-01-30 22:23:26 -0500522 return 0;
523}
524
Matthew Garrett29d3c1c2019-08-19 17:18:01 -0700525const int read_idmap[READING_MAX_ID] = {
Mimi Zohard9ddf072016-01-14 20:59:14 -0500526 [READING_FIRMWARE] = FIRMWARE_CHECK,
Mimi Zoharfd90bc52018-04-27 14:31:40 -0400527 [READING_FIRMWARE_PREALLOC_BUFFER] = FIRMWARE_CHECK,
Mimi Zohard9ddf072016-01-14 20:59:14 -0500528 [READING_MODULE] = MODULE_CHECK,
529 [READING_KEXEC_IMAGE] = KEXEC_KERNEL_CHECK,
530 [READING_KEXEC_INITRAMFS] = KEXEC_INITRAMFS_CHECK,
Mimi Zohar19f8a842016-01-15 10:17:12 -0500531 [READING_POLICY] = POLICY_CHECK
Mimi Zohard9ddf072016-01-14 20:59:14 -0500532};
533
Mimi Zohar39eeb4f2016-01-30 22:23:26 -0500534/**
Mimi Zoharcf222212016-01-14 17:57:47 -0500535 * ima_post_read_file - in memory collect/appraise/audit measurement
536 * @file: pointer to the file to be measured/appraised/audit
537 * @buf: pointer to in memory file contents
538 * @size: size of in memory file contents
539 * @read_id: caller identifier
540 *
541 * Measure/appraise/audit in memory file based on policy. Policy rules
542 * are written in terms of a policy identifier.
543 *
544 * On success return 0. On integrity appraisal error, assuming the file
545 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
546 */
547int ima_post_read_file(struct file *file, void *buf, loff_t size,
548 enum kernel_read_file_id read_id)
549{
Mimi Zohard9ddf072016-01-14 20:59:14 -0500550 enum ima_hooks func;
Matthew Garrettd906c102018-01-08 13:36:20 -0800551 u32 secid;
Mimi Zoharcf222212016-01-14 17:57:47 -0500552
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500553 if (!file && read_id == READING_FIRMWARE) {
554 if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
Mimi Zoharfed25122018-07-13 14:06:00 -0400555 (ima_appraise & IMA_APPRAISE_ENFORCE)) {
556 pr_err("Prevent firmware loading_store.\n");
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500557 return -EACCES; /* INTEGRITY_UNKNOWN */
Mimi Zoharfed25122018-07-13 14:06:00 -0400558 }
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500559 return 0;
560 }
561
Christoph Hellwiga7d3d032017-09-10 09:49:45 +0200562 /* permit signed certs */
563 if (!file && read_id == READING_X509_CERTIFICATE)
564 return 0;
565
Mimi Zoharcf222212016-01-14 17:57:47 -0500566 if (!file || !buf || size == 0) { /* should never happen */
567 if (ima_appraise & IMA_APPRAISE_ENFORCE)
568 return -EACCES;
569 return 0;
570 }
571
Mimi Zohard9ddf072016-01-14 20:59:14 -0500572 func = read_idmap[read_id] ?: FILE_CHECK;
Matthew Garrettd906c102018-01-08 13:36:20 -0800573 security_task_getsecid(current, &secid);
574 return process_measurement(file, current_cred(), secid, buf, size,
Al Viro6035a272018-06-08 13:40:10 -0400575 MAY_READ, func);
Mimi Zohar5a9196d2014-07-22 10:39:48 -0400576}
577
Mimi Zohar16c267a2018-07-13 14:05:58 -0400578/**
579 * ima_load_data - appraise decision based on policy
580 * @id: kernel load data caller identifier
581 *
582 * Callers of this LSM hook can not measure, appraise, or audit the
583 * data provided by userspace. Enforce policy rules requring a file
584 * signature (eg. kexec'ed kernel image).
585 *
586 * For permission return 0, otherwise return -EACCES.
587 */
588int ima_load_data(enum kernel_load_data_id id)
589{
Nayna Jainb5ca1172018-10-09 23:00:34 +0530590 bool ima_enforce, sig_enforce;
Mimi Zoharc77b8cd2018-07-13 14:06:02 -0400591
Nayna Jainb5ca1172018-10-09 23:00:34 +0530592 ima_enforce =
593 (ima_appraise & IMA_APPRAISE_ENFORCE) == IMA_APPRAISE_ENFORCE;
Mimi Zohar16c267a2018-07-13 14:05:58 -0400594
595 switch (id) {
596 case LOADING_KEXEC_IMAGE:
Jiri Bohac99d5cadf2019-08-19 17:17:44 -0700597 if (IS_ENABLED(CONFIG_KEXEC_SIG)
Nayna Jainb5ca1172018-10-09 23:00:34 +0530598 && arch_ima_get_secureboot()) {
599 pr_err("impossible to appraise a kernel image without a file descriptor; try using kexec_file_load syscall.\n");
600 return -EACCES;
601 }
602
603 if (ima_enforce && (ima_appraise & IMA_APPRAISE_KEXEC)) {
Mimi Zohar16c267a2018-07-13 14:05:58 -0400604 pr_err("impossible to appraise a kernel image without a file descriptor; try using kexec_file_load syscall.\n");
605 return -EACCES; /* INTEGRITY_UNKNOWN */
606 }
Mimi Zoharfed25122018-07-13 14:06:00 -0400607 break;
608 case LOADING_FIRMWARE:
Nayna Jainb5ca1172018-10-09 23:00:34 +0530609 if (ima_enforce && (ima_appraise & IMA_APPRAISE_FIRMWARE)) {
Mimi Zoharfed25122018-07-13 14:06:00 -0400610 pr_err("Prevent firmware sysfs fallback loading.\n");
611 return -EACCES; /* INTEGRITY_UNKNOWN */
612 }
Mimi Zoharc77b8cd2018-07-13 14:06:02 -0400613 break;
614 case LOADING_MODULE:
615 sig_enforce = is_module_sig_enforced();
616
Nayna Jainb5ca1172018-10-09 23:00:34 +0530617 if (ima_enforce && (!sig_enforce
618 && (ima_appraise & IMA_APPRAISE_MODULES))) {
Mimi Zoharc77b8cd2018-07-13 14:06:02 -0400619 pr_err("impossible to appraise a module without a file descriptor. sig_enforce kernel parameter might help\n");
620 return -EACCES; /* INTEGRITY_UNKNOWN */
621 }
Mimi Zohar16c267a2018-07-13 14:05:58 -0400622 default:
623 break;
624 }
625 return 0;
626}
627
Prakhar Srivastavab0935122019-06-23 23:23:29 -0700628/*
629 * process_buffer_measurement - Measure the buffer to ima log.
630 * @buf: pointer to the buffer that needs to be added to the log.
631 * @size: size of buffer(in bytes).
632 * @eventname: event name to be used for the buffer entry.
Nayna Jaine14555e2019-10-30 23:31:30 -0400633 * @func: IMA hook
634 * @pcr: pcr to extend the measurement
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800635 * @keyring: keyring name to determine the action to be performed
Prakhar Srivastavab0935122019-06-23 23:23:29 -0700636 *
637 * Based on policy, the buffer is measured into the ima log.
638 */
Nayna Jaine14555e2019-10-30 23:31:30 -0400639void process_buffer_measurement(const void *buf, int size,
640 const char *eventname, enum ima_hooks func,
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800641 int pcr, const char *keyring)
Prakhar Srivastavab0935122019-06-23 23:23:29 -0700642{
643 int ret = 0;
644 struct ima_template_entry *entry = NULL;
645 struct integrity_iint_cache iint = {};
646 struct ima_event_data event_data = {.iint = &iint,
Prakhar Srivastava86b4da82019-06-23 23:23:30 -0700647 .filename = eventname,
648 .buf = buf,
649 .buf_len = size};
Nayna Jaine14555e2019-10-30 23:31:30 -0400650 struct ima_template_desc *template = NULL;
Prakhar Srivastavab0935122019-06-23 23:23:29 -0700651 struct {
652 struct ima_digest_data hdr;
653 char digest[IMA_MAX_DIGEST_SIZE];
654 } hash = {};
655 int violation = 0;
Prakhar Srivastavab0935122019-06-23 23:23:29 -0700656 int action = 0;
Nayna Jaine14555e2019-10-30 23:31:30 -0400657 u32 secid;
Prakhar Srivastavab0935122019-06-23 23:23:29 -0700658
Lakshmi Ramasubramanianc5563ba2019-12-11 08:47:02 -0800659 if (!ima_policy_flag)
660 return;
661
Nayna Jaine14555e2019-10-30 23:31:30 -0400662 /*
663 * Both LSM hooks and auxilary based buffer measurements are
664 * based on policy. To avoid code duplication, differentiate
665 * between the LSM hooks and auxilary buffer measurements,
666 * retrieving the policy rule information only for the LSM hook
667 * buffer measurements.
668 */
669 if (func) {
670 security_task_getsecid(current, &secid);
671 action = ima_get_action(NULL, current_cred(), secid, 0, func,
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800672 &pcr, &template, keyring);
Nayna Jaine14555e2019-10-30 23:31:30 -0400673 if (!(action & IMA_MEASURE))
674 return;
675 }
676
677 if (!pcr)
678 pcr = CONFIG_IMA_MEASURE_PCR_IDX;
679
680 if (!template) {
681 template = lookup_template_desc("ima-buf");
682 ret = template_desc_init_fields(template->fmt,
683 &(template->fields),
684 &(template->num_fields));
685 if (ret < 0) {
686 pr_err("template %s init failed, result: %d\n",
687 (strlen(template->name) ?
688 template->name : template->fmt), ret);
689 return;
690 }
691 }
Prakhar Srivastavab0935122019-06-23 23:23:29 -0700692
693 iint.ima_hash = &hash.hdr;
694 iint.ima_hash->algo = ima_hash_algo;
695 iint.ima_hash->length = hash_digest_size[ima_hash_algo];
696
697 ret = ima_calc_buffer_hash(buf, size, iint.ima_hash);
698 if (ret < 0)
699 goto out;
700
Nayna Jaine14555e2019-10-30 23:31:30 -0400701 ret = ima_alloc_init_template(&event_data, &entry, template);
Prakhar Srivastavab0935122019-06-23 23:23:29 -0700702 if (ret < 0)
703 goto out;
704
705 ret = ima_store_template(entry, violation, NULL, buf, pcr);
706
707 if (ret < 0)
708 ima_free_template_entry(entry);
709
710out:
711 return;
712}
713
714/**
715 * ima_kexec_cmdline - measure kexec cmdline boot args
716 * @buf: pointer to buffer
717 * @size: size of buffer
718 *
719 * Buffers can only be measured, not appraised.
720 */
721void ima_kexec_cmdline(const void *buf, int size)
722{
Nayna Jaine14555e2019-10-30 23:31:30 -0400723 if (buf && size != 0)
Prakhar Srivastavab0935122019-06-23 23:23:29 -0700724 process_buffer_measurement(buf, size, "kexec-cmdline",
Lakshmi Ramasubramaniane9085e02019-12-11 08:47:06 -0800725 KEXEC_CMDLINE, 0, NULL);
Prakhar Srivastavab0935122019-06-23 23:23:29 -0700726}
727
Mimi Zohar3323eec2009-02-04 09:06:58 -0500728static int __init init_ima(void)
729{
730 int error;
731
Mimi Zohar3f23d622016-12-19 16:22:51 -0800732 ima_init_template_list();
Mimi Zohare7a2ad72013-06-07 12:16:37 +0200733 hash_setup(CONFIG_IMA_DEFAULT_HASH);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500734 error = ima_init();
Petr Vorelab603682018-03-23 14:41:08 +0100735
736 if (error && strcmp(hash_algo_name[ima_hash_algo],
737 CONFIG_IMA_DEFAULT_HASH) != 0) {
738 pr_info("Allocating %s failed, going to use default hash algorithm %s\n",
739 hash_algo_name[ima_hash_algo], CONFIG_IMA_DEFAULT_HASH);
740 hash_setup_done = 0;
741 hash_setup(CONFIG_IMA_DEFAULT_HASH);
742 error = ima_init();
743 }
744
Janne Karhunenb1694242019-06-14 15:20:15 +0300745 error = register_blocking_lsm_notifier(&ima_lsm_policy_notifier);
746 if (error)
747 pr_warn("Couldn't register LSM notifier, error %d\n", error);
748
Petr Vorel4ecd9932018-05-10 17:15:48 +0200749 if (!error)
Roberto Sassua7560242014-09-12 19:35:54 +0200750 ima_update_policy_flag();
Petr Vorel4ecd9932018-05-10 17:15:48 +0200751
Mimi Zohar3323eec2009-02-04 09:06:58 -0500752 return error;
753}
754
Mimi Zohar3323eec2009-02-04 09:06:58 -0500755late_initcall(init_ima); /* Start IMA after the TPM is available */