blob: 65418e0906c136bd2e4fb70320d67d8d252080ca [file] [log] [blame]
Thomas Gleixnerb886d832019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Mimi Zoharf381c272011-03-09 14:13:22 -05002/*
3 * Copyright (C) 2008 IBM Corporation
4 *
5 * Authors:
6 * Mimi Zohar <zohar@us.ibm.com>
7 *
Mimi Zoharf381c272011-03-09 14:13:22 -05008 * File: integrity_iint.c
9 * - implements the integrity hooks: integrity_inode_alloc,
10 * integrity_inode_free
11 * - cache integrity information associated with an inode
12 * using a rbtree tree.
13 */
14#include <linux/slab.h>
Paul Gortmaker876979c2018-12-09 15:36:29 -050015#include <linux/init.h>
Mimi Zoharf381c272011-03-09 14:13:22 -050016#include <linux/spinlock.h>
17#include <linux/rbtree.h>
Dmitry Kasatkine3c4abb2014-11-05 17:01:12 +020018#include <linux/file.h>
19#include <linux/uaccess.h>
Matthew Garrett0c343af2018-05-11 16:12:34 -070020#include <linux/security.h>
Kees Cook5b89c1b2018-10-10 17:18:21 -070021#include <linux/lsm_hooks.h>
Mimi Zoharf381c272011-03-09 14:13:22 -050022#include "integrity.h"
23
24static struct rb_root integrity_iint_tree = RB_ROOT;
Dmitry Kasatkina10bf262012-02-08 14:15:42 -050025static DEFINE_RWLOCK(integrity_iint_lock);
Mimi Zoharf381c272011-03-09 14:13:22 -050026static struct kmem_cache *iint_cache __read_mostly;
27
Matthew Garrett0c343af2018-05-11 16:12:34 -070028struct dentry *integrity_dir;
29
Mimi Zoharf381c272011-03-09 14:13:22 -050030/*
31 * __integrity_iint_find - return the iint associated with an inode
32 */
33static struct integrity_iint_cache *__integrity_iint_find(struct inode *inode)
34{
35 struct integrity_iint_cache *iint;
36 struct rb_node *n = integrity_iint_tree.rb_node;
37
Mimi Zoharf381c272011-03-09 14:13:22 -050038 while (n) {
39 iint = rb_entry(n, struct integrity_iint_cache, rb_node);
40
41 if (inode < iint->inode)
42 n = n->rb_left;
43 else if (inode > iint->inode)
44 n = n->rb_right;
45 else
Tianjia Zhange7acd182023-06-01 14:42:44 +080046 return iint;
Mimi Zoharf381c272011-03-09 14:13:22 -050047 }
Mimi Zoharf381c272011-03-09 14:13:22 -050048
Tianjia Zhange7acd182023-06-01 14:42:44 +080049 return NULL;
Mimi Zoharf381c272011-03-09 14:13:22 -050050}
51
52/*
53 * integrity_iint_find - return the iint associated with an inode
54 */
55struct integrity_iint_cache *integrity_iint_find(struct inode *inode)
56{
57 struct integrity_iint_cache *iint;
58
59 if (!IS_IMA(inode))
60 return NULL;
61
Dmitry Kasatkina10bf262012-02-08 14:15:42 -050062 read_lock(&integrity_iint_lock);
Mimi Zoharf381c272011-03-09 14:13:22 -050063 iint = __integrity_iint_find(inode);
Dmitry Kasatkina10bf262012-02-08 14:15:42 -050064 read_unlock(&integrity_iint_lock);
Mimi Zoharf381c272011-03-09 14:13:22 -050065
66 return iint;
67}
68
69static void iint_free(struct integrity_iint_cache *iint)
70{
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +030071 kfree(iint->ima_hash);
72 iint->ima_hash = NULL;
Mimi Zoharf381c272011-03-09 14:13:22 -050073 iint->version = 0;
74 iint->flags = 0UL;
Mimi Zohare2598072018-01-23 10:00:41 -050075 iint->atomic_flags = 0UL;
Mimi Zohard79d72e2012-12-03 17:08:11 -050076 iint->ima_file_status = INTEGRITY_UNKNOWN;
77 iint->ima_mmap_status = INTEGRITY_UNKNOWN;
78 iint->ima_bprm_status = INTEGRITY_UNKNOWN;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -050079 iint->ima_read_status = INTEGRITY_UNKNOWN;
Matthew Garrettd906c102018-01-08 13:36:20 -080080 iint->ima_creds_status = INTEGRITY_UNKNOWN;
Dmitry Kasatkinfb788d82011-08-15 15:30:11 +030081 iint->evm_status = INTEGRITY_UNKNOWN;
Eric Richter96d450b2016-06-01 13:14:00 -050082 iint->measured_pcrs = 0;
Mimi Zoharf381c272011-03-09 14:13:22 -050083 kmem_cache_free(iint_cache, iint);
84}
85
86/**
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +030087 * integrity_inode_get - find or allocate an iint associated with an inode
Mimi Zoharf381c272011-03-09 14:13:22 -050088 * @inode: pointer to the inode
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +030089 * @return: allocated iint
90 *
91 * Caller must lock i_mutex
Mimi Zoharf381c272011-03-09 14:13:22 -050092 */
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +030093struct integrity_iint_cache *integrity_inode_get(struct inode *inode)
Mimi Zoharf381c272011-03-09 14:13:22 -050094{
95 struct rb_node **p;
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +030096 struct rb_node *node, *parent = NULL;
97 struct integrity_iint_cache *iint, *test_iint;
Mimi Zoharf381c272011-03-09 14:13:22 -050098
Mimi Zohar92063f32021-03-19 11:17:23 -040099 /*
100 * The integrity's "iint_cache" is initialized at security_init(),
101 * unless it is not included in the ordered list of LSMs enabled
102 * on the boot command line.
103 */
104 if (!iint_cache)
105 panic("%s: lsm=integrity required.\n", __func__);
106
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300107 iint = integrity_iint_find(inode);
108 if (iint)
109 return iint;
Mimi Zoharf381c272011-03-09 14:13:22 -0500110
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300111 iint = kmem_cache_alloc(iint_cache, GFP_NOFS);
112 if (!iint)
113 return NULL;
Mimi Zoharf381c272011-03-09 14:13:22 -0500114
Dmitry Kasatkina10bf262012-02-08 14:15:42 -0500115 write_lock(&integrity_iint_lock);
Mimi Zoharf381c272011-03-09 14:13:22 -0500116
117 p = &integrity_iint_tree.rb_node;
118 while (*p) {
119 parent = *p;
120 test_iint = rb_entry(parent, struct integrity_iint_cache,
121 rb_node);
Tianjia Zhange7acd182023-06-01 14:42:44 +0800122 if (inode < test_iint->inode) {
Mimi Zoharf381c272011-03-09 14:13:22 -0500123 p = &(*p)->rb_left;
Tianjia Zhange7acd182023-06-01 14:42:44 +0800124 } else if (inode > test_iint->inode) {
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300125 p = &(*p)->rb_right;
Tianjia Zhange7acd182023-06-01 14:42:44 +0800126 } else {
127 write_unlock(&integrity_iint_lock);
128 kmem_cache_free(iint_cache, iint);
129 return test_iint;
130 }
Mimi Zoharf381c272011-03-09 14:13:22 -0500131 }
132
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300133 iint->inode = inode;
134 node = &iint->rb_node;
Mimi Zoharf381c272011-03-09 14:13:22 -0500135 inode->i_flags |= S_IMA;
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300136 rb_link_node(node, parent, p);
137 rb_insert_color(node, &integrity_iint_tree);
Mimi Zoharf381c272011-03-09 14:13:22 -0500138
Dmitry Kasatkina10bf262012-02-08 14:15:42 -0500139 write_unlock(&integrity_iint_lock);
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300140 return iint;
Mimi Zoharf381c272011-03-09 14:13:22 -0500141}
142
143/**
144 * integrity_inode_free - called on security_inode_free
145 * @inode: pointer to the inode
146 *
147 * Free the integrity information(iint) associated with an inode.
148 */
149void integrity_inode_free(struct inode *inode)
150{
151 struct integrity_iint_cache *iint;
152
153 if (!IS_IMA(inode))
154 return;
155
Dmitry Kasatkina10bf262012-02-08 14:15:42 -0500156 write_lock(&integrity_iint_lock);
Mimi Zoharf381c272011-03-09 14:13:22 -0500157 iint = __integrity_iint_find(inode);
158 rb_erase(&iint->rb_node, &integrity_iint_tree);
Dmitry Kasatkina10bf262012-02-08 14:15:42 -0500159 write_unlock(&integrity_iint_lock);
Mimi Zoharf381c272011-03-09 14:13:22 -0500160
161 iint_free(iint);
162}
163
164static void init_once(void *foo)
165{
Jiele Zhao282c0a42021-04-07 01:44:38 +0000166 struct integrity_iint_cache *iint = (struct integrity_iint_cache *) foo;
Mimi Zoharf381c272011-03-09 14:13:22 -0500167
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200168 memset(iint, 0, sizeof(*iint));
Mimi Zohard79d72e2012-12-03 17:08:11 -0500169 iint->ima_file_status = INTEGRITY_UNKNOWN;
170 iint->ima_mmap_status = INTEGRITY_UNKNOWN;
171 iint->ima_bprm_status = INTEGRITY_UNKNOWN;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500172 iint->ima_read_status = INTEGRITY_UNKNOWN;
Matthew Garrettd906c102018-01-08 13:36:20 -0800173 iint->ima_creds_status = INTEGRITY_UNKNOWN;
Dmitry Kasatkin24e01982011-05-06 11:34:17 +0300174 iint->evm_status = INTEGRITY_UNKNOWN;
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200175 mutex_init(&iint->mutex);
Mimi Zoharf381c272011-03-09 14:13:22 -0500176}
177
178static int __init integrity_iintcache_init(void)
179{
180 iint_cache =
181 kmem_cache_create("iint_cache", sizeof(struct integrity_iint_cache),
182 0, SLAB_PANIC, init_once);
Mimi Zoharf381c272011-03-09 14:13:22 -0500183 return 0;
184}
Kees Cook3d6e5f62018-10-10 17:18:23 -0700185DEFINE_LSM(integrity) = {
Kees Cook07aed2f2018-10-10 17:18:24 -0700186 .name = "integrity",
Kees Cook3d6e5f62018-10-10 17:18:23 -0700187 .init = integrity_iintcache_init,
188};
Dmitry Kasatkine3c4abb2014-11-05 17:01:12 +0200189
190
191/*
192 * integrity_kernel_read - read data from the file
193 *
194 * This is a function for reading file content instead of kernel_read().
195 * It does not perform locking checks to ensure it cannot be blocked.
196 * It does not perform security checks because it is irrelevant for IMA.
197 *
198 */
199int integrity_kernel_read(struct file *file, loff_t offset,
Thiago Jung Bauermannbb543e32017-06-07 22:49:10 -0300200 void *addr, unsigned long count)
Dmitry Kasatkine3c4abb2014-11-05 17:01:12 +0200201{
Christoph Hellwiga1f9b1c2020-05-08 08:54:27 +0200202 return __kernel_read(file, addr, count, &offset);
Dmitry Kasatkine3c4abb2014-11-05 17:01:12 +0200203}
204
205/*
Dmitry Kasatkinc9cd2ce2014-11-05 17:01:15 +0200206 * integrity_load_keys - load integrity keys hook
207 *
208 * Hooks is called from init/main.c:kernel_init_freeable()
209 * when rootfs is ready
210 */
211void __init integrity_load_keys(void)
212{
213 ima_load_x509();
Roberto Sassuaa2ead72021-05-14 17:27:43 +0200214
215 if (!IS_ENABLED(CONFIG_IMA_LOAD_X509))
216 evm_load_x509();
Dmitry Kasatkinc9cd2ce2014-11-05 17:01:15 +0200217}
Matthew Garrett0c343af2018-05-11 16:12:34 -0700218
219static int __init integrity_fs_init(void)
220{
221 integrity_dir = securityfs_create_dir("integrity", NULL);
222 if (IS_ERR(integrity_dir)) {
Sudeep Hollaac2409a2018-06-05 11:25:45 +0100223 int ret = PTR_ERR(integrity_dir);
224
225 if (ret != -ENODEV)
226 pr_err("Unable to create integrity sysfs dir: %d\n",
227 ret);
Matthew Garrett0c343af2018-05-11 16:12:34 -0700228 integrity_dir = NULL;
Sudeep Hollaac2409a2018-06-05 11:25:45 +0100229 return ret;
Matthew Garrett0c343af2018-05-11 16:12:34 -0700230 }
231
232 return 0;
233}
234
235late_initcall(integrity_fs_init)