]> bbs.cooldavid.org Git - net-next-2.6.git/blame - security/integrity/ima/ima_iint.c
Merge branch 'ima-memory-use-fixes'
[net-next-2.6.git] / security / integrity / ima / ima_iint.c
CommitLineData
3323eec9
MZ
1/*
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
85491641 15 * using a rbtree tree.
3323eec9 16 */
5a0e3ad6 17#include <linux/slab.h>
3323eec9
MZ
18#include <linux/module.h>
19#include <linux/spinlock.h>
85491641 20#include <linux/rbtree.h>
3323eec9
MZ
21#include "ima.h"
22
85491641
EP
23static struct rb_root ima_iint_tree = RB_ROOT;
24static DEFINE_SPINLOCK(ima_iint_lock);
3323eec9
MZ
25static struct kmem_cache *iint_cache __read_mostly;
26
e950598d
MZ
27int iint_initialized = 0;
28
85491641
EP
29/*
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/*
64c62f06 56 * ima_iint_find - return the iint associated with an inode
3323eec9 57 */
64c62f06 58struct ima_iint_cache *ima_iint_find(struct inode *inode)
3323eec9
MZ
59{
60 struct ima_iint_cache *iint;
61
196f5181
EP
62 if (!IS_IMA(inode))
63 return NULL;
64
85491641
EP
65 spin_lock(&ima_iint_lock);
66 iint = __ima_iint_find(inode);
85491641
EP
67 spin_unlock(&ima_iint_lock);
68
3323eec9
MZ
69 return iint;
70}
71
64c62f06
EP
72static void iint_free(struct ima_iint_cache *iint)
73{
74 iint->version = 0;
75 iint->flags = 0UL;
76 kmem_cache_free(iint_cache, iint);
77}
78
9353384e
EP
79/**
80 * ima_inode_alloc - allocate an iint associated with an inode
81 * @inode: pointer to the inode
3323eec9 82 */
9353384e 83int ima_inode_alloc(struct inode *inode)
3323eec9 84{
85491641
EP
85 struct rb_node **p;
86 struct rb_node *new_node, *parent = NULL;
87 struct ima_iint_cache *new_iint, *test_iint;
88 int rc;
3323eec9 89
85491641
EP
90 new_iint = kmem_cache_alloc(iint_cache, GFP_NOFS);
91 if (!new_iint)
9353384e 92 return -ENOMEM;
3323eec9 93
85491641
EP
94 new_iint->inode = inode;
95 new_node = &new_iint->rb_node;
3323eec9 96
196f5181 97 mutex_lock(&inode->i_mutex); /* i_flags */
3323eec9 98 spin_lock(&ima_iint_lock);
85491641
EP
99
100 p = &ima_iint_tree.rb_node;
101 while (*p) {
102 parent = *p;
103 test_iint = rb_entry(parent, struct ima_iint_cache, rb_node);
104
105 rc = -EEXIST;
106 if (inode < test_iint->inode)
107 p = &(*p)->rb_left;
108 else if (inode > test_iint->inode)
109 p = &(*p)->rb_right;
110 else
111 goto out_err;
112 }
113
196f5181 114 inode->i_flags |= S_IMA;
85491641
EP
115 rb_link_node(new_node, parent, p);
116 rb_insert_color(new_node, &ima_iint_tree);
117
3323eec9 118 spin_unlock(&ima_iint_lock);
196f5181 119 mutex_unlock(&inode->i_mutex); /* i_flags */
3323eec9 120
85491641
EP
121 return 0;
122out_err:
123 spin_unlock(&ima_iint_lock);
196f5181 124 mutex_unlock(&inode->i_mutex); /* i_flags */
64c62f06 125 iint_free(new_iint);
3323eec9 126
64c62f06 127 return rc;
3323eec9
MZ
128}
129
3323eec9 130/**
85a17f55 131 * ima_inode_free - called on security_inode_free
3323eec9
MZ
132 * @inode: pointer to the inode
133 *
134 * Free the integrity information(iint) associated with an inode.
135 */
85a17f55 136void ima_inode_free(struct inode *inode)
3323eec9
MZ
137{
138 struct ima_iint_cache *iint;
139
a178d202
EP
140 if (inode->i_readcount)
141 printk(KERN_INFO "%s: readcount: %u\n", __func__, inode->i_readcount);
142
143 inode->i_readcount = 0;
144
196f5181
EP
145 if (!IS_IMA(inode))
146 return;
147
3323eec9 148 spin_lock(&ima_iint_lock);
85491641 149 iint = __ima_iint_find(inode);
196f5181 150 rb_erase(&iint->rb_node, &ima_iint_tree);
3323eec9 151 spin_unlock(&ima_iint_lock);
64c62f06 152
64c62f06 153 iint_free(iint);
3323eec9
MZ
154}
155
156static void init_once(void *foo)
157{
158 struct ima_iint_cache *iint = foo;
159
160 memset(iint, 0, sizeof *iint);
161 iint->version = 0;
162 iint->flags = 0UL;
163 mutex_init(&iint->mutex);
3323eec9
MZ
164}
165
54bb6552 166static int __init ima_iintcache_init(void)
3323eec9
MZ
167{
168 iint_cache =
169 kmem_cache_create("iint_cache", sizeof(struct ima_iint_cache), 0,
170 SLAB_PANIC, init_once);
e950598d 171 iint_initialized = 1;
54bb6552 172 return 0;
3323eec9 173}
54bb6552 174security_initcall(ima_iintcache_init);