]>
Commit | Line | Data |
---|---|---|
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 |
23 | static struct rb_root ima_iint_tree = RB_ROOT; |
24 | static DEFINE_SPINLOCK(ima_iint_lock); | |
3323eec9 MZ |
25 | static struct kmem_cache *iint_cache __read_mostly; |
26 | ||
e950598d MZ |
27 | int iint_initialized = 0; |
28 | ||
85491641 EP |
29 | /* |
30 | * __ima_iint_find - return the iint associated with an inode | |
31 | */ | |
32 | static 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 | /* | |
56 | * ima_iint_find_get - return the iint associated with an inode | |
3323eec9 MZ |
57 | * |
58 | * ima_iint_find_get gets a reference to the iint. Caller must | |
59 | * remember to put the iint reference. | |
60 | */ | |
61 | struct ima_iint_cache *ima_iint_find_get(struct inode *inode) | |
62 | { | |
63 | struct ima_iint_cache *iint; | |
64 | ||
85491641 EP |
65 | spin_lock(&ima_iint_lock); |
66 | iint = __ima_iint_find(inode); | |
67 | if (iint) | |
68 | kref_get(&iint->refcount); | |
69 | spin_unlock(&ima_iint_lock); | |
70 | ||
3323eec9 MZ |
71 | return iint; |
72 | } | |
73 | ||
9353384e EP |
74 | /** |
75 | * ima_inode_alloc - allocate an iint associated with an inode | |
76 | * @inode: pointer to the inode | |
3323eec9 | 77 | */ |
9353384e | 78 | int ima_inode_alloc(struct inode *inode) |
3323eec9 | 79 | { |
85491641 EP |
80 | struct rb_node **p; |
81 | struct rb_node *new_node, *parent = NULL; | |
82 | struct ima_iint_cache *new_iint, *test_iint; | |
83 | int rc; | |
3323eec9 | 84 | |
85491641 EP |
85 | new_iint = kmem_cache_alloc(iint_cache, GFP_NOFS); |
86 | if (!new_iint) | |
9353384e | 87 | return -ENOMEM; |
3323eec9 | 88 | |
85491641 EP |
89 | new_iint->inode = inode; |
90 | new_node = &new_iint->rb_node; | |
3323eec9 MZ |
91 | |
92 | spin_lock(&ima_iint_lock); | |
85491641 EP |
93 | |
94 | p = &ima_iint_tree.rb_node; | |
95 | while (*p) { | |
96 | parent = *p; | |
97 | test_iint = rb_entry(parent, struct ima_iint_cache, rb_node); | |
98 | ||
99 | rc = -EEXIST; | |
100 | if (inode < test_iint->inode) | |
101 | p = &(*p)->rb_left; | |
102 | else if (inode > test_iint->inode) | |
103 | p = &(*p)->rb_right; | |
104 | else | |
105 | goto out_err; | |
106 | } | |
107 | ||
108 | rb_link_node(new_node, parent, p); | |
109 | rb_insert_color(new_node, &ima_iint_tree); | |
110 | ||
3323eec9 | 111 | spin_unlock(&ima_iint_lock); |
3323eec9 | 112 | |
85491641 EP |
113 | return 0; |
114 | out_err: | |
115 | spin_unlock(&ima_iint_lock); | |
116 | kref_put(&new_iint->refcount, iint_free); | |
9353384e | 117 | return rc; |
3323eec9 MZ |
118 | } |
119 | ||
120 | /* iint_free - called when the iint refcount goes to zero */ | |
121 | void iint_free(struct kref *kref) | |
122 | { | |
123 | struct ima_iint_cache *iint = container_of(kref, struct ima_iint_cache, | |
124 | refcount); | |
125 | iint->version = 0; | |
126 | iint->flags = 0UL; | |
db1afffa | 127 | kref_init(&iint->refcount); |
3323eec9 MZ |
128 | kmem_cache_free(iint_cache, iint); |
129 | } | |
130 | ||
3323eec9 | 131 | /** |
85a17f55 | 132 | * ima_inode_free - called on security_inode_free |
3323eec9 MZ |
133 | * @inode: pointer to the inode |
134 | * | |
135 | * Free the integrity information(iint) associated with an inode. | |
136 | */ | |
85a17f55 | 137 | void ima_inode_free(struct inode *inode) |
3323eec9 MZ |
138 | { |
139 | struct ima_iint_cache *iint; | |
140 | ||
a178d202 EP |
141 | if (inode->i_readcount) |
142 | printk(KERN_INFO "%s: readcount: %u\n", __func__, inode->i_readcount); | |
143 | ||
144 | inode->i_readcount = 0; | |
145 | ||
3323eec9 | 146 | spin_lock(&ima_iint_lock); |
85491641 EP |
147 | iint = __ima_iint_find(inode); |
148 | if (iint) | |
149 | rb_erase(&iint->rb_node, &ima_iint_tree); | |
3323eec9 MZ |
150 | spin_unlock(&ima_iint_lock); |
151 | if (iint) | |
85491641 | 152 | kref_put(&iint->refcount, iint_free); |
3323eec9 MZ |
153 | } |
154 | ||
155 | static void init_once(void *foo) | |
156 | { | |
157 | struct ima_iint_cache *iint = foo; | |
158 | ||
159 | memset(iint, 0, sizeof *iint); | |
160 | iint->version = 0; | |
161 | iint->flags = 0UL; | |
162 | mutex_init(&iint->mutex); | |
db1afffa | 163 | kref_init(&iint->refcount); |
3323eec9 MZ |
164 | } |
165 | ||
54bb6552 | 166 | static 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 | 174 | security_initcall(ima_iintcache_init); |