]>
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 | /* | |
64c62f06 | 56 | * ima_iint_find - return the iint associated with an inode |
3323eec9 | 57 | */ |
64c62f06 | 58 | struct ima_iint_cache *ima_iint_find(struct inode *inode) |
3323eec9 MZ |
59 | { |
60 | struct ima_iint_cache *iint; | |
61 | ||
85491641 EP |
62 | spin_lock(&ima_iint_lock); |
63 | iint = __ima_iint_find(inode); | |
85491641 EP |
64 | spin_unlock(&ima_iint_lock); |
65 | ||
3323eec9 MZ |
66 | return iint; |
67 | } | |
68 | ||
64c62f06 EP |
69 | static 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 | ||
9353384e EP |
76 | /** |
77 | * ima_inode_alloc - allocate an iint associated with an inode | |
78 | * @inode: pointer to the inode | |
3323eec9 | 79 | */ |
9353384e | 80 | int ima_inode_alloc(struct inode *inode) |
3323eec9 | 81 | { |
85491641 EP |
82 | struct rb_node **p; |
83 | struct rb_node *new_node, *parent = NULL; | |
84 | struct ima_iint_cache *new_iint, *test_iint; | |
85 | int rc; | |
3323eec9 | 86 | |
85491641 EP |
87 | new_iint = kmem_cache_alloc(iint_cache, GFP_NOFS); |
88 | if (!new_iint) | |
9353384e | 89 | return -ENOMEM; |
3323eec9 | 90 | |
85491641 EP |
91 | new_iint->inode = inode; |
92 | new_node = &new_iint->rb_node; | |
3323eec9 MZ |
93 | |
94 | spin_lock(&ima_iint_lock); | |
85491641 EP |
95 | |
96 | 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 | ||
3323eec9 | 113 | spin_unlock(&ima_iint_lock); |
3323eec9 | 114 | |
85491641 EP |
115 | return 0; |
116 | out_err: | |
117 | spin_unlock(&ima_iint_lock); | |
64c62f06 | 118 | iint_free(new_iint); |
3323eec9 | 119 | |
64c62f06 | 120 | return rc; |
3323eec9 MZ |
121 | } |
122 | ||
3323eec9 | 123 | /** |
85a17f55 | 124 | * ima_inode_free - called on security_inode_free |
3323eec9 MZ |
125 | * @inode: pointer to the inode |
126 | * | |
127 | * Free the integrity information(iint) associated with an inode. | |
128 | */ | |
85a17f55 | 129 | void ima_inode_free(struct inode *inode) |
3323eec9 MZ |
130 | { |
131 | struct ima_iint_cache *iint; | |
132 | ||
a178d202 EP |
133 | if (inode->i_readcount) |
134 | printk(KERN_INFO "%s: readcount: %u\n", __func__, inode->i_readcount); | |
135 | ||
136 | inode->i_readcount = 0; | |
137 | ||
3323eec9 | 138 | spin_lock(&ima_iint_lock); |
85491641 EP |
139 | iint = __ima_iint_find(inode); |
140 | if (iint) | |
141 | rb_erase(&iint->rb_node, &ima_iint_tree); | |
3323eec9 | 142 | spin_unlock(&ima_iint_lock); |
64c62f06 EP |
143 | |
144 | if (!iint) | |
145 | return; | |
146 | ||
147 | iint_free(iint); | |
3323eec9 MZ |
148 | } |
149 | ||
150 | static 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); | |
3323eec9 MZ |
158 | } |
159 | ||
54bb6552 | 160 | static int __init ima_iintcache_init(void) |
3323eec9 MZ |
161 | { |
162 | iint_cache = | |
163 | kmem_cache_create("iint_cache", sizeof(struct ima_iint_cache), 0, | |
164 | SLAB_PANIC, init_once); | |
e950598d | 165 | iint_initialized = 1; |
54bb6552 | 166 | return 0; |
3323eec9 | 167 | } |
54bb6552 | 168 | security_initcall(ima_iintcache_init); |