2 * Copyright (C) 2008 IBM Corporation
5 * Mimi Zohar <zohar@us.ibm.com>
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
13 * - implements the IMA hooks: ima_inode_alloc, ima_inode_free
14 * - cache integrity information associated with an inode
15 * using a rbtree tree.
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/spinlock.h>
20 #include <linux/rbtree.h>
23 static struct rb_root ima_iint_tree
= RB_ROOT
;
24 static DEFINE_SPINLOCK(ima_iint_lock
);
25 static struct kmem_cache
*iint_cache __read_mostly
;
27 int iint_initialized
= 0;
30 * __ima_iint_find - return the iint associated with an inode
32 static struct ima_iint_cache
*__ima_iint_find(struct inode
*inode
)
34 struct ima_iint_cache
*iint
;
35 struct rb_node
*n
= ima_iint_tree
.rb_node
;
37 assert_spin_locked(&ima_iint_lock
);
40 iint
= rb_entry(n
, struct ima_iint_cache
, rb_node
);
42 if (inode
< iint
->inode
)
44 else if (inode
> iint
->inode
)
56 * ima_iint_find_get - return the iint associated with an inode
58 * ima_iint_find_get gets a reference to the iint. Caller must
59 * remember to put the iint reference.
61 struct ima_iint_cache
*ima_iint_find_get(struct inode
*inode
)
63 struct ima_iint_cache
*iint
;
65 spin_lock(&ima_iint_lock
);
66 iint
= __ima_iint_find(inode
);
68 kref_get(&iint
->refcount
);
69 spin_unlock(&ima_iint_lock
);
75 * ima_inode_alloc - allocate an iint associated with an inode
76 * @inode: pointer to the inode
78 int ima_inode_alloc(struct inode
*inode
)
81 struct rb_node
*new_node
, *parent
= NULL
;
82 struct ima_iint_cache
*new_iint
, *test_iint
;
85 new_iint
= kmem_cache_alloc(iint_cache
, GFP_NOFS
);
89 new_iint
->inode
= inode
;
90 new_node
= &new_iint
->rb_node
;
92 spin_lock(&ima_iint_lock
);
94 p
= &ima_iint_tree
.rb_node
;
97 test_iint
= rb_entry(parent
, struct ima_iint_cache
, rb_node
);
100 if (inode
< test_iint
->inode
)
102 else if (inode
> test_iint
->inode
)
108 rb_link_node(new_node
, parent
, p
);
109 rb_insert_color(new_node
, &ima_iint_tree
);
111 spin_unlock(&ima_iint_lock
);
115 spin_unlock(&ima_iint_lock
);
116 kref_put(&new_iint
->refcount
, iint_free
);
120 /* iint_free - called when the iint refcount goes to zero */
121 void iint_free(struct kref
*kref
)
123 struct ima_iint_cache
*iint
= container_of(kref
, struct ima_iint_cache
,
127 kref_init(&iint
->refcount
);
128 kmem_cache_free(iint_cache
, iint
);
132 * ima_inode_free - called on security_inode_free
133 * @inode: pointer to the inode
135 * Free the integrity information(iint) associated with an inode.
137 void ima_inode_free(struct inode
*inode
)
139 struct ima_iint_cache
*iint
;
141 if (inode
->i_readcount
)
142 printk(KERN_INFO
"%s: readcount: %u\n", __func__
, inode
->i_readcount
);
144 inode
->i_readcount
= 0;
146 spin_lock(&ima_iint_lock
);
147 iint
= __ima_iint_find(inode
);
149 rb_erase(&iint
->rb_node
, &ima_iint_tree
);
150 spin_unlock(&ima_iint_lock
);
152 kref_put(&iint
->refcount
, iint_free
);
155 static void init_once(void *foo
)
157 struct ima_iint_cache
*iint
= foo
;
159 memset(iint
, 0, sizeof *iint
);
162 mutex_init(&iint
->mutex
);
163 kref_init(&iint
->refcount
);
166 static int __init
ima_iintcache_init(void)
169 kmem_cache_create("iint_cache", sizeof(struct ima_iint_cache
), 0,
170 SLAB_PANIC
, init_once
);
171 iint_initialized
= 1;
174 security_initcall(ima_iintcache_init
);