blob: 1ea05da2323d16275e87d2ee7bd4013dc0e7fdec [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>
Kees Cook5b89c1b2018-10-10 17:18:21 -070025#include <linux/lsm_hooks.h>
Mimi Zoharf381c272011-03-09 14:13:22 -050026#include "integrity.h"
27
28static struct rb_root integrity_iint_tree = RB_ROOT;
Dmitry Kasatkina10bf262012-02-08 14:15:42 -050029static DEFINE_RWLOCK(integrity_iint_lock);
Mimi Zoharf381c272011-03-09 14:13:22 -050030static struct kmem_cache *iint_cache __read_mostly;
31
Matthew Garrett0c343af2018-05-11 16:12:34 -070032struct dentry *integrity_dir;
33
Mimi Zoharf381c272011-03-09 14:13:22 -050034/*
35 * __integrity_iint_find - return the iint associated with an inode
36 */
37static struct integrity_iint_cache *__integrity_iint_find(struct inode *inode)
38{
39 struct integrity_iint_cache *iint;
40 struct rb_node *n = integrity_iint_tree.rb_node;
41
Mimi Zoharf381c272011-03-09 14:13:22 -050042 while (n) {
43 iint = rb_entry(n, struct integrity_iint_cache, rb_node);
44
45 if (inode < iint->inode)
46 n = n->rb_left;
47 else if (inode > iint->inode)
48 n = n->rb_right;
49 else
50 break;
51 }
52 if (!n)
53 return NULL;
54
55 return iint;
56}
57
58/*
59 * integrity_iint_find - return the iint associated with an inode
60 */
61struct integrity_iint_cache *integrity_iint_find(struct inode *inode)
62{
63 struct integrity_iint_cache *iint;
64
65 if (!IS_IMA(inode))
66 return NULL;
67
Dmitry Kasatkina10bf262012-02-08 14:15:42 -050068 read_lock(&integrity_iint_lock);
Mimi Zoharf381c272011-03-09 14:13:22 -050069 iint = __integrity_iint_find(inode);
Dmitry Kasatkina10bf262012-02-08 14:15:42 -050070 read_unlock(&integrity_iint_lock);
Mimi Zoharf381c272011-03-09 14:13:22 -050071
72 return iint;
73}
74
75static void iint_free(struct integrity_iint_cache *iint)
76{
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +030077 kfree(iint->ima_hash);
78 iint->ima_hash = NULL;
Mimi Zoharf381c272011-03-09 14:13:22 -050079 iint->version = 0;
80 iint->flags = 0UL;
Mimi Zohare2598072018-01-23 10:00:41 -050081 iint->atomic_flags = 0UL;
Mimi Zohard79d72e2012-12-03 17:08:11 -050082 iint->ima_file_status = INTEGRITY_UNKNOWN;
83 iint->ima_mmap_status = INTEGRITY_UNKNOWN;
84 iint->ima_bprm_status = INTEGRITY_UNKNOWN;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -050085 iint->ima_read_status = INTEGRITY_UNKNOWN;
Matthew Garrettd906c102018-01-08 13:36:20 -080086 iint->ima_creds_status = INTEGRITY_UNKNOWN;
Dmitry Kasatkinfb788d82011-08-15 15:30:11 +030087 iint->evm_status = INTEGRITY_UNKNOWN;
Eric Richter96d450b2016-06-01 13:14:00 -050088 iint->measured_pcrs = 0;
Mimi Zoharf381c272011-03-09 14:13:22 -050089 kmem_cache_free(iint_cache, iint);
90}
91
92/**
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +030093 * integrity_inode_get - find or allocate an iint associated with an inode
Mimi Zoharf381c272011-03-09 14:13:22 -050094 * @inode: pointer to the inode
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +030095 * @return: allocated iint
96 *
97 * Caller must lock i_mutex
Mimi Zoharf381c272011-03-09 14:13:22 -050098 */
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +030099struct integrity_iint_cache *integrity_inode_get(struct inode *inode)
Mimi Zoharf381c272011-03-09 14:13:22 -0500100{
101 struct rb_node **p;
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300102 struct rb_node *node, *parent = NULL;
103 struct integrity_iint_cache *iint, *test_iint;
Mimi Zoharf381c272011-03-09 14:13:22 -0500104
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300105 iint = integrity_iint_find(inode);
106 if (iint)
107 return iint;
Mimi Zoharf381c272011-03-09 14:13:22 -0500108
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300109 iint = kmem_cache_alloc(iint_cache, GFP_NOFS);
110 if (!iint)
111 return NULL;
Mimi Zoharf381c272011-03-09 14:13:22 -0500112
Dmitry Kasatkina10bf262012-02-08 14:15:42 -0500113 write_lock(&integrity_iint_lock);
Mimi Zoharf381c272011-03-09 14:13:22 -0500114
115 p = &integrity_iint_tree.rb_node;
116 while (*p) {
117 parent = *p;
118 test_iint = rb_entry(parent, struct integrity_iint_cache,
119 rb_node);
Mimi Zoharf381c272011-03-09 14:13:22 -0500120 if (inode < test_iint->inode)
121 p = &(*p)->rb_left;
Mimi Zoharf381c272011-03-09 14:13:22 -0500122 else
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300123 p = &(*p)->rb_right;
Mimi Zoharf381c272011-03-09 14:13:22 -0500124 }
125
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300126 iint->inode = inode;
127 node = &iint->rb_node;
Mimi Zoharf381c272011-03-09 14:13:22 -0500128 inode->i_flags |= S_IMA;
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300129 rb_link_node(node, parent, p);
130 rb_insert_color(node, &integrity_iint_tree);
Mimi Zoharf381c272011-03-09 14:13:22 -0500131
Dmitry Kasatkina10bf262012-02-08 14:15:42 -0500132 write_unlock(&integrity_iint_lock);
Dmitry Kasatkinbf2276d2011-10-19 12:04:40 +0300133 return iint;
Mimi Zoharf381c272011-03-09 14:13:22 -0500134}
135
136/**
137 * integrity_inode_free - called on security_inode_free
138 * @inode: pointer to the inode
139 *
140 * Free the integrity information(iint) associated with an inode.
141 */
142void integrity_inode_free(struct inode *inode)
143{
144 struct integrity_iint_cache *iint;
145
146 if (!IS_IMA(inode))
147 return;
148
Dmitry Kasatkina10bf262012-02-08 14:15:42 -0500149 write_lock(&integrity_iint_lock);
Mimi Zoharf381c272011-03-09 14:13:22 -0500150 iint = __integrity_iint_find(inode);
151 rb_erase(&iint->rb_node, &integrity_iint_tree);
Dmitry Kasatkina10bf262012-02-08 14:15:42 -0500152 write_unlock(&integrity_iint_lock);
Mimi Zoharf381c272011-03-09 14:13:22 -0500153
154 iint_free(iint);
155}
156
157static void init_once(void *foo)
158{
159 struct integrity_iint_cache *iint = foo;
160
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200161 memset(iint, 0, sizeof(*iint));
Mimi Zohard79d72e2012-12-03 17:08:11 -0500162 iint->ima_file_status = INTEGRITY_UNKNOWN;
163 iint->ima_mmap_status = INTEGRITY_UNKNOWN;
164 iint->ima_bprm_status = INTEGRITY_UNKNOWN;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500165 iint->ima_read_status = INTEGRITY_UNKNOWN;
Matthew Garrettd906c102018-01-08 13:36:20 -0800166 iint->ima_creds_status = INTEGRITY_UNKNOWN;
Dmitry Kasatkin24e01982011-05-06 11:34:17 +0300167 iint->evm_status = INTEGRITY_UNKNOWN;
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200168 mutex_init(&iint->mutex);
Mimi Zoharf381c272011-03-09 14:13:22 -0500169}
170
171static int __init integrity_iintcache_init(void)
172{
173 iint_cache =
174 kmem_cache_create("iint_cache", sizeof(struct integrity_iint_cache),
175 0, SLAB_PANIC, init_once);
Mimi Zoharf381c272011-03-09 14:13:22 -0500176 return 0;
177}
Kees Cook3d6e5f62018-10-10 17:18:23 -0700178DEFINE_LSM(integrity) = {
Kees Cook07aed2f2018-10-10 17:18:24 -0700179 .name = "integrity",
Kees Cook3d6e5f62018-10-10 17:18:23 -0700180 .init = integrity_iintcache_init,
181};
Dmitry Kasatkine3c4abb2014-11-05 17:01:12 +0200182
183
184/*
185 * integrity_kernel_read - read data from the file
186 *
187 * This is a function for reading file content instead of kernel_read().
188 * It does not perform locking checks to ensure it cannot be blocked.
189 * It does not perform security checks because it is irrelevant for IMA.
190 *
191 */
192int integrity_kernel_read(struct file *file, loff_t offset,
Thiago Jung Bauermannbb543e32017-06-07 22:49:10 -0300193 void *addr, unsigned long count)
Dmitry Kasatkine3c4abb2014-11-05 17:01:12 +0200194{
195 mm_segment_t old_fs;
196 char __user *buf = (char __user *)addr;
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200197 ssize_t ret;
Dmitry Kasatkine3c4abb2014-11-05 17:01:12 +0200198
199 if (!(file->f_mode & FMODE_READ))
200 return -EBADF;
201
202 old_fs = get_fs();
203 set_fs(get_ds());
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200204 ret = __vfs_read(file, buf, count, &offset);
Dmitry Kasatkine3c4abb2014-11-05 17:01:12 +0200205 set_fs(old_fs);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200206
Dmitry Kasatkine3c4abb2014-11-05 17:01:12 +0200207 return ret;
208}
209
210/*
Dmitry Kasatkinc9cd2ce2014-11-05 17:01:15 +0200211 * integrity_load_keys - load integrity keys hook
212 *
213 * Hooks is called from init/main.c:kernel_init_freeable()
214 * when rootfs is ready
215 */
216void __init integrity_load_keys(void)
217{
218 ima_load_x509();
Dmitry Kasatkin2ce523e2015-10-22 21:26:21 +0300219 evm_load_x509();
Dmitry Kasatkinc9cd2ce2014-11-05 17:01:15 +0200220}
Matthew Garrett0c343af2018-05-11 16:12:34 -0700221
222static int __init integrity_fs_init(void)
223{
224 integrity_dir = securityfs_create_dir("integrity", NULL);
225 if (IS_ERR(integrity_dir)) {
Sudeep Hollaac2409a2018-06-05 11:25:45 +0100226 int ret = PTR_ERR(integrity_dir);
227
228 if (ret != -ENODEV)
229 pr_err("Unable to create integrity sysfs dir: %d\n",
230 ret);
Matthew Garrett0c343af2018-05-11 16:12:34 -0700231 integrity_dir = NULL;
Sudeep Hollaac2409a2018-06-05 11:25:45 +0100232 return ret;
Matthew Garrett0c343af2018-05-11 16:12:34 -0700233 }
234
235 return 0;
236}
237
238late_initcall(integrity_fs_init)