1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2008 IBM Corporation
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Authors:
6*4882a593Smuzhiyun * Mimi Zohar <zohar@us.ibm.com>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * File: integrity_iint.c
9*4882a593Smuzhiyun * - implements the integrity hooks: integrity_inode_alloc,
10*4882a593Smuzhiyun * integrity_inode_free
11*4882a593Smuzhiyun * - cache integrity information associated with an inode
12*4882a593Smuzhiyun * using a rbtree tree.
13*4882a593Smuzhiyun */
14*4882a593Smuzhiyun #include <linux/slab.h>
15*4882a593Smuzhiyun #include <linux/init.h>
16*4882a593Smuzhiyun #include <linux/spinlock.h>
17*4882a593Smuzhiyun #include <linux/rbtree.h>
18*4882a593Smuzhiyun #include <linux/file.h>
19*4882a593Smuzhiyun #include <linux/uaccess.h>
20*4882a593Smuzhiyun #include <linux/security.h>
21*4882a593Smuzhiyun #include <linux/lsm_hooks.h>
22*4882a593Smuzhiyun #include "integrity.h"
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun static struct rb_root integrity_iint_tree = RB_ROOT;
25*4882a593Smuzhiyun static DEFINE_RWLOCK(integrity_iint_lock);
26*4882a593Smuzhiyun static struct kmem_cache *iint_cache __read_mostly;
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun struct dentry *integrity_dir;
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun /*
31*4882a593Smuzhiyun * __integrity_iint_find - return the iint associated with an inode
32*4882a593Smuzhiyun */
__integrity_iint_find(struct inode * inode)33*4882a593Smuzhiyun static struct integrity_iint_cache *__integrity_iint_find(struct inode *inode)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun struct integrity_iint_cache *iint;
36*4882a593Smuzhiyun struct rb_node *n = integrity_iint_tree.rb_node;
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun while (n) {
39*4882a593Smuzhiyun iint = rb_entry(n, struct integrity_iint_cache, rb_node);
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun if (inode < iint->inode)
42*4882a593Smuzhiyun n = n->rb_left;
43*4882a593Smuzhiyun else if (inode > iint->inode)
44*4882a593Smuzhiyun n = n->rb_right;
45*4882a593Smuzhiyun else
46*4882a593Smuzhiyun break;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun if (!n)
49*4882a593Smuzhiyun return NULL;
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun return iint;
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /*
55*4882a593Smuzhiyun * integrity_iint_find - return the iint associated with an inode
56*4882a593Smuzhiyun */
integrity_iint_find(struct inode * inode)57*4882a593Smuzhiyun struct integrity_iint_cache *integrity_iint_find(struct inode *inode)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun struct integrity_iint_cache *iint;
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun if (!IS_IMA(inode))
62*4882a593Smuzhiyun return NULL;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun read_lock(&integrity_iint_lock);
65*4882a593Smuzhiyun iint = __integrity_iint_find(inode);
66*4882a593Smuzhiyun read_unlock(&integrity_iint_lock);
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun return iint;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun
iint_free(struct integrity_iint_cache * iint)71*4882a593Smuzhiyun static void iint_free(struct integrity_iint_cache *iint)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun kfree(iint->ima_hash);
74*4882a593Smuzhiyun iint->ima_hash = NULL;
75*4882a593Smuzhiyun iint->version = 0;
76*4882a593Smuzhiyun iint->flags = 0UL;
77*4882a593Smuzhiyun iint->atomic_flags = 0UL;
78*4882a593Smuzhiyun iint->ima_file_status = INTEGRITY_UNKNOWN;
79*4882a593Smuzhiyun iint->ima_mmap_status = INTEGRITY_UNKNOWN;
80*4882a593Smuzhiyun iint->ima_bprm_status = INTEGRITY_UNKNOWN;
81*4882a593Smuzhiyun iint->ima_read_status = INTEGRITY_UNKNOWN;
82*4882a593Smuzhiyun iint->ima_creds_status = INTEGRITY_UNKNOWN;
83*4882a593Smuzhiyun iint->evm_status = INTEGRITY_UNKNOWN;
84*4882a593Smuzhiyun iint->measured_pcrs = 0;
85*4882a593Smuzhiyun kmem_cache_free(iint_cache, iint);
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun /**
89*4882a593Smuzhiyun * integrity_inode_get - find or allocate an iint associated with an inode
90*4882a593Smuzhiyun * @inode: pointer to the inode
91*4882a593Smuzhiyun * @return: allocated iint
92*4882a593Smuzhiyun *
93*4882a593Smuzhiyun * Caller must lock i_mutex
94*4882a593Smuzhiyun */
integrity_inode_get(struct inode * inode)95*4882a593Smuzhiyun struct integrity_iint_cache *integrity_inode_get(struct inode *inode)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun struct rb_node **p;
98*4882a593Smuzhiyun struct rb_node *node, *parent = NULL;
99*4882a593Smuzhiyun struct integrity_iint_cache *iint, *test_iint;
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /*
102*4882a593Smuzhiyun * The integrity's "iint_cache" is initialized at security_init(),
103*4882a593Smuzhiyun * unless it is not included in the ordered list of LSMs enabled
104*4882a593Smuzhiyun * on the boot command line.
105*4882a593Smuzhiyun */
106*4882a593Smuzhiyun if (!iint_cache)
107*4882a593Smuzhiyun panic("%s: lsm=integrity required.\n", __func__);
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun iint = integrity_iint_find(inode);
110*4882a593Smuzhiyun if (iint)
111*4882a593Smuzhiyun return iint;
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun iint = kmem_cache_alloc(iint_cache, GFP_NOFS);
114*4882a593Smuzhiyun if (!iint)
115*4882a593Smuzhiyun return NULL;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun write_lock(&integrity_iint_lock);
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun p = &integrity_iint_tree.rb_node;
120*4882a593Smuzhiyun while (*p) {
121*4882a593Smuzhiyun parent = *p;
122*4882a593Smuzhiyun test_iint = rb_entry(parent, struct integrity_iint_cache,
123*4882a593Smuzhiyun rb_node);
124*4882a593Smuzhiyun if (inode < test_iint->inode)
125*4882a593Smuzhiyun p = &(*p)->rb_left;
126*4882a593Smuzhiyun else
127*4882a593Smuzhiyun p = &(*p)->rb_right;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun iint->inode = inode;
131*4882a593Smuzhiyun node = &iint->rb_node;
132*4882a593Smuzhiyun inode->i_flags |= S_IMA;
133*4882a593Smuzhiyun rb_link_node(node, parent, p);
134*4882a593Smuzhiyun rb_insert_color(node, &integrity_iint_tree);
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun write_unlock(&integrity_iint_lock);
137*4882a593Smuzhiyun return iint;
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun /**
141*4882a593Smuzhiyun * integrity_inode_free - called on security_inode_free
142*4882a593Smuzhiyun * @inode: pointer to the inode
143*4882a593Smuzhiyun *
144*4882a593Smuzhiyun * Free the integrity information(iint) associated with an inode.
145*4882a593Smuzhiyun */
integrity_inode_free(struct inode * inode)146*4882a593Smuzhiyun void integrity_inode_free(struct inode *inode)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun struct integrity_iint_cache *iint;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun if (!IS_IMA(inode))
151*4882a593Smuzhiyun return;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun write_lock(&integrity_iint_lock);
154*4882a593Smuzhiyun iint = __integrity_iint_find(inode);
155*4882a593Smuzhiyun rb_erase(&iint->rb_node, &integrity_iint_tree);
156*4882a593Smuzhiyun write_unlock(&integrity_iint_lock);
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun iint_free(iint);
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
init_once(void * foo)161*4882a593Smuzhiyun static void init_once(void *foo)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun struct integrity_iint_cache *iint = foo;
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun memset(iint, 0, sizeof(*iint));
166*4882a593Smuzhiyun iint->ima_file_status = INTEGRITY_UNKNOWN;
167*4882a593Smuzhiyun iint->ima_mmap_status = INTEGRITY_UNKNOWN;
168*4882a593Smuzhiyun iint->ima_bprm_status = INTEGRITY_UNKNOWN;
169*4882a593Smuzhiyun iint->ima_read_status = INTEGRITY_UNKNOWN;
170*4882a593Smuzhiyun iint->ima_creds_status = INTEGRITY_UNKNOWN;
171*4882a593Smuzhiyun iint->evm_status = INTEGRITY_UNKNOWN;
172*4882a593Smuzhiyun mutex_init(&iint->mutex);
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun
integrity_iintcache_init(void)175*4882a593Smuzhiyun static int __init integrity_iintcache_init(void)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun iint_cache =
178*4882a593Smuzhiyun kmem_cache_create("iint_cache", sizeof(struct integrity_iint_cache),
179*4882a593Smuzhiyun 0, SLAB_PANIC, init_once);
180*4882a593Smuzhiyun return 0;
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun DEFINE_LSM(integrity) = {
183*4882a593Smuzhiyun .name = "integrity",
184*4882a593Smuzhiyun .init = integrity_iintcache_init,
185*4882a593Smuzhiyun };
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun /*
189*4882a593Smuzhiyun * integrity_kernel_read - read data from the file
190*4882a593Smuzhiyun *
191*4882a593Smuzhiyun * This is a function for reading file content instead of kernel_read().
192*4882a593Smuzhiyun * It does not perform locking checks to ensure it cannot be blocked.
193*4882a593Smuzhiyun * It does not perform security checks because it is irrelevant for IMA.
194*4882a593Smuzhiyun *
195*4882a593Smuzhiyun */
integrity_kernel_read(struct file * file,loff_t offset,void * addr,unsigned long count)196*4882a593Smuzhiyun int integrity_kernel_read(struct file *file, loff_t offset,
197*4882a593Smuzhiyun void *addr, unsigned long count)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun return __kernel_read(file, addr, count, &offset);
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun /*
203*4882a593Smuzhiyun * integrity_load_keys - load integrity keys hook
204*4882a593Smuzhiyun *
205*4882a593Smuzhiyun * Hooks is called from init/main.c:kernel_init_freeable()
206*4882a593Smuzhiyun * when rootfs is ready
207*4882a593Smuzhiyun */
integrity_load_keys(void)208*4882a593Smuzhiyun void __init integrity_load_keys(void)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun ima_load_x509();
211*4882a593Smuzhiyun evm_load_x509();
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
integrity_fs_init(void)214*4882a593Smuzhiyun static int __init integrity_fs_init(void)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun integrity_dir = securityfs_create_dir("integrity", NULL);
217*4882a593Smuzhiyun if (IS_ERR(integrity_dir)) {
218*4882a593Smuzhiyun int ret = PTR_ERR(integrity_dir);
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun if (ret != -ENODEV)
221*4882a593Smuzhiyun pr_err("Unable to create integrity sysfs dir: %d\n",
222*4882a593Smuzhiyun ret);
223*4882a593Smuzhiyun integrity_dir = NULL;
224*4882a593Smuzhiyun return ret;
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun return 0;
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun late_initcall(integrity_fs_init)
231