blob: 5a6810041e5c0aa957da3519c6bc227987f24a16 [file] [log] [blame]
Mimi Zoharf381c272011-03-09 14:13:22 -05001/*
2 * Copyright (C) 2008 IBM Corporation
3 *
4 * Authors:
5 * Mimi Zohar <zohar@us.ibm.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, version 2 of the
10 * License.
11 *
12 * File: integrity_iint.c
13 * - implements the integrity hooks: integrity_inode_alloc,
14 * integrity_inode_free
15 * - cache integrity information associated with an inode
16 * using a rbtree tree.
17 */
18#include <linux/slab.h>
19#include <linux/module.h>
20#include <linux/spinlock.h>
21#include <linux/rbtree.h>
Dmitry Kasatkine3c4abb2014-11-05 17:01:12 +020022#include <linux/file.h>
23#include <linux/uaccess.h>
Matthew Garrett0c343af2018-05-11 16:12:34 -070024#include <linux/security.h>
Mimi Zoharf381c272011-03-09 14:13:22 -050025#include "integrity.h"
26
27static struct rb_root integrity_iint_tree = RB_ROOT;
Dmitry Kasatkina10bf262012-02-08 14:15:42 -050028static DEFINE_RWLOCK(integrity_iint_lock);
Mimi Zoharf381c272011-03-09 14:13:22 -050029static struct kmem_cache *iint_cache __read_mostly;
30
Matthew Garrett0c343af2018-05-11 16:12:34 -070031struct dentry *integrity_dir;
32
Mimi Zoharf381c272011-03-09 14:13:22 -050033/*
34 * __integrity_iint_find - return the iint associated with an inode
35 */
36static struct integrity_iint_cache *__integrity_iint_find(struct inode *inode)
37{
38 struct integrity_iint_cache *iint;
39 struct rb_node *n = integrity_iint_tree.rb_node;
40
Mimi Zoharf381c272011-03-09 14:13:22 -050041 while (n) {
42 iint = rb_entry(n, struct integrity_iint_cache, rb_node);
43
44 if (inode < iint->inode)
45 n = n->rb_left;
46 else if (inode > iint->inode)
47 n = n->rb_right;
48 else
49 break;
50 }
51 if (!n)
52 return NULL;
53
54 return iint;
55}
56
57/*
58 * integrity_iint_find - return the iint associated with an inode
59 */
60struct integrity_iint_cache *integrity_iint_find(struct inode *inode)
61{
62 struct integrity_iint_cache *iint;
63
64 if (!IS_IMA(inode))
65 return NULL;
66
Dmitry Kasatkina10bf262012-02-08 14:15:42 -050067 read_lock(&integrity_iint_lock);
Mimi Zoharf381c272011-03-09 14:13:22 -050068 iint = __integrity_iint_find(inode);
Dmitry Kasatkina10bf262012-02-08 14:15:42 -050069 read_unlock(&integrity_iint_lock);
Mimi Zoharf381c272011-03-09 14:13:22 -050070
71 return iint;
72}
73
74static void iint_free(struct integrity_iint_cache *iint)
75{
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +030076 kfree(iint->ima_hash);
77 iint->ima_hash = NULL;
Mimi Zoharf381c272011-03-09 14:13:22 -050078 iint->version = 0;
79 iint->flags = 0UL;
Mimi Zohare2598072018-01-23 10:00:41 -050080 iint->atomic_flags = 0UL;
Mimi Zohard79d72e2012-12-03 17:08:11 -050081 iint->ima_file_status = INTEGRITY_UNKNOWN;
82 iint->ima_mmap_status = INTEGRITY_UNKNOWN;
83 iint->ima_bprm_status = INTEGRITY_UNKNOWN;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -050084 iint->ima_read_status = INTEGRITY_UNKNOWN;
Matthew Garrettd906c102018-01-08 13:36:20 -080085 iint->ima_creds_status = INTEGRITY_UNKNOWN;
Dmitry Kasatkinfb788d82011-08-15 15:30:11 +030086 iint->evm_status = INTEGRITY_UNKNOWN;
Eric Richter96d450b2016-06-01 13:14:00 -050087 iint->measured_pcrs = 0;
Mimi Zoharf381c272011-03-09 14:13:22 -050088 kmem_cache_free(iint_cache, iint);
89}
90
91/**
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +030092 * integrity_inode_get - find or allocate an iint associated with an inode
Mimi Zoharf381c272011-03-09 14:13:22 -050093 * @inode: pointer to the inode
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +030094 * @return: allocated iint
95 *
96 * Caller must lock i_mutex
Mimi Zoharf381c272011-03-09 14:13:22 -050097 */
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +030098struct integrity_iint_cache *integrity_inode_get(struct inode *inode)
Mimi Zoharf381c272011-03-09 14:13:22 -050099{
100 struct rb_node **p;
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300101 struct rb_node *node, *parent = NULL;
102 struct integrity_iint_cache *iint, *test_iint;
Mimi Zoharf381c272011-03-09 14:13:22 -0500103
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300104 iint = integrity_iint_find(inode);
105 if (iint)
106 return iint;
Mimi Zoharf381c272011-03-09 14:13:22 -0500107
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300108 iint = kmem_cache_alloc(iint_cache, GFP_NOFS);
109 if (!iint)
110 return NULL;
Mimi Zoharf381c272011-03-09 14:13:22 -0500111
Dmitry Kasatkina10bf262012-02-08 14:15:42 -0500112 write_lock(&integrity_iint_lock);
Mimi Zoharf381c272011-03-09 14:13:22 -0500113
114 p = &integrity_iint_tree.rb_node;
115 while (*p) {
116 parent = *p;
117 test_iint = rb_entry(parent, struct integrity_iint_cache,
118 rb_node);
Mimi Zoharf381c272011-03-09 14:13:22 -0500119 if (inode < test_iint->inode)
120 p = &(*p)->rb_left;
Mimi Zoharf381c272011-03-09 14:13:22 -0500121 else
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300122 p = &(*p)->rb_right;
Mimi Zoharf381c272011-03-09 14:13:22 -0500123 }
124
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300125 iint->inode = inode;
126 node = &iint->rb_node;
Mimi Zoharf381c272011-03-09 14:13:22 -0500127 inode->i_flags |= S_IMA;
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300128 rb_link_node(node, parent, p);
129 rb_insert_color(node, &integrity_iint_tree);
Mimi Zoharf381c272011-03-09 14:13:22 -0500130
Dmitry Kasatkina10bf262012-02-08 14:15:42 -0500131 write_unlock(&integrity_iint_lock);
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300132 return iint;
Mimi Zoharf381c272011-03-09 14:13:22 -0500133}
134
135/**
136 * integrity_inode_free - called on security_inode_free
137 * @inode: pointer to the inode
138 *
139 * Free the integrity information(iint) associated with an inode.
140 */
141void integrity_inode_free(struct inode *inode)
142{
143 struct integrity_iint_cache *iint;
144
145 if (!IS_IMA(inode))
146 return;
147
Dmitry Kasatkina10bf262012-02-08 14:15:42 -0500148 write_lock(&integrity_iint_lock);
Mimi Zoharf381c272011-03-09 14:13:22 -0500149 iint = __integrity_iint_find(inode);
150 rb_erase(&iint->rb_node, &integrity_iint_tree);
Dmitry Kasatkina10bf262012-02-08 14:15:42 -0500151 write_unlock(&integrity_iint_lock);
Mimi Zoharf381c272011-03-09 14:13:22 -0500152
153 iint_free(iint);
154}
155
156static void init_once(void *foo)
157{
158 struct integrity_iint_cache *iint = foo;
159
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200160 memset(iint, 0, sizeof(*iint));
Mimi Zohard79d72e2012-12-03 17:08:11 -0500161 iint->ima_file_status = INTEGRITY_UNKNOWN;
162 iint->ima_mmap_status = INTEGRITY_UNKNOWN;
163 iint->ima_bprm_status = INTEGRITY_UNKNOWN;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500164 iint->ima_read_status = INTEGRITY_UNKNOWN;
Matthew Garrettd906c102018-01-08 13:36:20 -0800165 iint->ima_creds_status = INTEGRITY_UNKNOWN;
Dmitry Kasatkin24e01982011-05-06 11:34:17 +0300166 iint->evm_status = INTEGRITY_UNKNOWN;
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200167 mutex_init(&iint->mutex);
Mimi Zoharf381c272011-03-09 14:13:22 -0500168}
169
170static int __init integrity_iintcache_init(void)
171{
172 iint_cache =
173 kmem_cache_create("iint_cache", sizeof(struct integrity_iint_cache),
174 0, SLAB_PANIC, init_once);
Mimi Zoharf381c272011-03-09 14:13:22 -0500175 return 0;
176}
177security_initcall(integrity_iintcache_init);
Dmitry Kasatkine3c4abb2014-11-05 17:01:12 +0200178
179
180/*
181 * integrity_kernel_read - read data from the file
182 *
183 * This is a function for reading file content instead of kernel_read().
184 * It does not perform locking checks to ensure it cannot be blocked.
185 * It does not perform security checks because it is irrelevant for IMA.
186 *
187 */
188int integrity_kernel_read(struct file *file, loff_t offset,
Thiago Jung Bauermannbb543e32017-06-07 22:49:10 -0300189 void *addr, unsigned long count)
Dmitry Kasatkine3c4abb2014-11-05 17:01:12 +0200190{
191 mm_segment_t old_fs;
192 char __user *buf = (char __user *)addr;
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200193 ssize_t ret;
Dmitry Kasatkine3c4abb2014-11-05 17:01:12 +0200194
195 if (!(file->f_mode & FMODE_READ))
196 return -EBADF;
197
198 old_fs = get_fs();
199 set_fs(get_ds());
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200200 ret = __vfs_read(file, buf, count, &offset);
Dmitry Kasatkine3c4abb2014-11-05 17:01:12 +0200201 set_fs(old_fs);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200202
Dmitry Kasatkine3c4abb2014-11-05 17:01:12 +0200203 return ret;
204}
205
206/*
Dmitry Kasatkinc9cd2ce2014-11-05 17:01:15 +0200207 * integrity_load_keys - load integrity keys hook
208 *
209 * Hooks is called from init/main.c:kernel_init_freeable()
210 * when rootfs is ready
211 */
212void __init integrity_load_keys(void)
213{
214 ima_load_x509();
Dmitry Kasatkin2ce523e2015-10-22 21:26:21 +0300215 evm_load_x509();
Dmitry Kasatkinc9cd2ce2014-11-05 17:01:15 +0200216}
Matthew Garrett0c343af2018-05-11 16:12:34 -0700217
218static int __init integrity_fs_init(void)
219{
220 integrity_dir = securityfs_create_dir("integrity", NULL);
221 if (IS_ERR(integrity_dir)) {
Sudeep Hollaac2409a2018-06-05 11:25:45 +0100222 int ret = PTR_ERR(integrity_dir);
223
224 if (ret != -ENODEV)
225 pr_err("Unable to create integrity sysfs dir: %d\n",
226 ret);
Matthew Garrett0c343af2018-05-11 16:12:34 -0700227 integrity_dir = NULL;
Sudeep Hollaac2409a2018-06-05 11:25:45 +0100228 return ret;
Matthew Garrett0c343af2018-05-11 16:12:34 -0700229 }
230
231 return 0;
232}
233
234late_initcall(integrity_fs_init)