blob: 969a1c1cb333ebf156767a9aed8f2cae3413fef4 [file] [log] [blame]
Mimi Zohar3323eec92009-02-04 09:06:58 -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: ima_iint.c
13 * - implements the IMA hooks: ima_inode_alloc, ima_inode_free
14 * - cache integrity information associated with an inode
Eric Paris85491642010-10-25 14:41:18 -040015 * using a rbtree tree.
Mimi Zohar3323eec92009-02-04 09:06:58 -050016 */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Mimi Zohar3323eec92009-02-04 09:06:58 -050018#include <linux/module.h>
19#include <linux/spinlock.h>
Eric Paris85491642010-10-25 14:41:18 -040020#include <linux/rbtree.h>
Mimi Zohar3323eec92009-02-04 09:06:58 -050021#include "ima.h"
22
Eric Paris85491642010-10-25 14:41:18 -040023static struct rb_root ima_iint_tree = RB_ROOT;
24static DEFINE_SPINLOCK(ima_iint_lock);
Mimi Zohar3323eec92009-02-04 09:06:58 -050025static struct kmem_cache *iint_cache __read_mostly;
26
Mimi Zohare9505982010-08-31 09:38:51 -040027int iint_initialized = 0;
28
Eric Paris85491642010-10-25 14:41:18 -040029/*
30 * __ima_iint_find - return the iint associated with an inode
31 */
32static struct ima_iint_cache *__ima_iint_find(struct inode *inode)
33{
34 struct ima_iint_cache *iint;
35 struct rb_node *n = ima_iint_tree.rb_node;
36
37 assert_spin_locked(&ima_iint_lock);
38
39 while (n) {
40 iint = rb_entry(n, struct ima_iint_cache, rb_node);
41
42 if (inode < iint->inode)
43 n = n->rb_left;
44 else if (inode > iint->inode)
45 n = n->rb_right;
46 else
47 break;
48 }
49 if (!n)
50 return NULL;
51
52 return iint;
53}
54
55/*
Eric Paris64c62f02010-10-25 14:42:12 -040056 * ima_iint_find - return the iint associated with an inode
Mimi Zohar3323eec92009-02-04 09:06:58 -050057 */
Eric Paris64c62f02010-10-25 14:42:12 -040058struct ima_iint_cache *ima_iint_find(struct inode *inode)
Mimi Zohar3323eec92009-02-04 09:06:58 -050059{
60 struct ima_iint_cache *iint;
61
Eric Paris85491642010-10-25 14:41:18 -040062 spin_lock(&ima_iint_lock);
63 iint = __ima_iint_find(inode);
Eric Paris85491642010-10-25 14:41:18 -040064 spin_unlock(&ima_iint_lock);
65
Mimi Zohar3323eec92009-02-04 09:06:58 -050066 return iint;
67}
68
Eric Paris64c62f02010-10-25 14:42:12 -040069static void iint_free(struct ima_iint_cache *iint)
70{
71 iint->version = 0;
72 iint->flags = 0UL;
73 kmem_cache_free(iint_cache, iint);
74}
75
Eric Paris93533842009-12-04 15:47:52 -050076/**
77 * ima_inode_alloc - allocate an iint associated with an inode
78 * @inode: pointer to the inode
Mimi Zohar3323eec92009-02-04 09:06:58 -050079 */
Eric Paris93533842009-12-04 15:47:52 -050080int ima_inode_alloc(struct inode *inode)
Mimi Zohar3323eec92009-02-04 09:06:58 -050081{
Eric Paris85491642010-10-25 14:41:18 -040082 struct rb_node **p;
83 struct rb_node *new_node, *parent = NULL;
84 struct ima_iint_cache *new_iint, *test_iint;
85 int rc;
Mimi Zohar3323eec92009-02-04 09:06:58 -050086
Eric Paris85491642010-10-25 14:41:18 -040087 new_iint = kmem_cache_alloc(iint_cache, GFP_NOFS);
88 if (!new_iint)
Eric Paris93533842009-12-04 15:47:52 -050089 return -ENOMEM;
Mimi Zohar3323eec92009-02-04 09:06:58 -050090
Eric Paris85491642010-10-25 14:41:18 -040091 new_iint->inode = inode;
92 new_node = &new_iint->rb_node;
Mimi Zohar3323eec92009-02-04 09:06:58 -050093
94 spin_lock(&ima_iint_lock);
Eric Paris93533842009-12-04 15:47:52 -050095
Eric Paris85491642010-10-25 14:41:18 -040096 p = &ima_iint_tree.rb_node;
97 while (*p) {
98 parent = *p;
99 test_iint = rb_entry(parent, struct ima_iint_cache, rb_node);
100
101 rc = -EEXIST;
102 if (inode < test_iint->inode)
103 p = &(*p)->rb_left;
104 else if (inode > test_iint->inode)
105 p = &(*p)->rb_right;
106 else
107 goto out_err;
108 }
109
110 rb_link_node(new_node, parent, p);
111 rb_insert_color(new_node, &ima_iint_tree);
112
113 spin_unlock(&ima_iint_lock);
114
115 return 0;
116out_err:
117 spin_unlock(&ima_iint_lock);
Eric Paris64c62f02010-10-25 14:42:12 -0400118 iint_free(new_iint);
Mimi Zohar3323eec92009-02-04 09:06:58 -0500119
Eric Paris64c62f02010-10-25 14:42:12 -0400120 return rc;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500121}
122
Mimi Zohar3323eec92009-02-04 09:06:58 -0500123/**
Eric Paris85a17f52009-12-04 15:48:08 -0500124 * ima_inode_free - called on security_inode_free
Mimi Zohar3323eec92009-02-04 09:06:58 -0500125 * @inode: pointer to the inode
126 *
127 * Free the integrity information(iint) associated with an inode.
128 */
Eric Paris85a17f52009-12-04 15:48:08 -0500129void ima_inode_free(struct inode *inode)
Mimi Zohar3323eec92009-02-04 09:06:58 -0500130{
131 struct ima_iint_cache *iint;
132
Eric Parisa178d202010-10-25 14:41:59 -0400133 if (inode->i_readcount)
134 printk(KERN_INFO "%s: readcount: %u\n", __func__, inode->i_readcount);
135
136 inode->i_readcount = 0;
137
Mimi Zohar3323eec92009-02-04 09:06:58 -0500138 spin_lock(&ima_iint_lock);
Eric Paris85491642010-10-25 14:41:18 -0400139 iint = __ima_iint_find(inode);
140 if (iint)
141 rb_erase(&iint->rb_node, &ima_iint_tree);
Mimi Zohar3323eec92009-02-04 09:06:58 -0500142 spin_unlock(&ima_iint_lock);
Eric Paris64c62f02010-10-25 14:42:12 -0400143
144 if (!iint)
145 return;
146
147 iint_free(iint);
Mimi Zohar3323eec92009-02-04 09:06:58 -0500148}
149
150static void init_once(void *foo)
151{
152 struct ima_iint_cache *iint = foo;
153
154 memset(iint, 0, sizeof *iint);
155 iint->version = 0;
156 iint->flags = 0UL;
157 mutex_init(&iint->mutex);
Mimi Zohar3323eec92009-02-04 09:06:58 -0500158}
159
Eric Paris54bb6552009-12-09 15:29:01 -0500160static int __init ima_iintcache_init(void)
Mimi Zohar3323eec92009-02-04 09:06:58 -0500161{
162 iint_cache =
163 kmem_cache_create("iint_cache", sizeof(struct ima_iint_cache), 0,
164 SLAB_PANIC, init_once);
Mimi Zohare9505982010-08-31 09:38:51 -0400165 iint_initialized = 1;
Eric Paris54bb6552009-12-09 15:29:01 -0500166 return 0;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500167}
Eric Paris54bb6552009-12-09 15:29:01 -0500168security_initcall(ima_iintcache_init);