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
17 #include <linux/module.h>
18 #include <linux/spinlock.h>
19 #include <linux/radix-tree.h>
22 #define ima_iint_delete ima_inode_free
24 RADIX_TREE(ima_iint_store
, GFP_ATOMIC
);
25 DEFINE_SPINLOCK(ima_iint_lock
);
27 static struct kmem_cache
*iint_cache __read_mostly
;
29 /* ima_iint_find_get - return the iint associated with an inode
31 * ima_iint_find_get gets a reference to the iint. Caller must
32 * remember to put the iint reference.
34 struct ima_iint_cache
*ima_iint_find_get(struct inode
*inode
)
36 struct ima_iint_cache
*iint
;
39 iint
= radix_tree_lookup(&ima_iint_store
, (unsigned long)inode
);
42 kref_get(&iint
->refcount
);
48 /* Allocate memory for the iint associated with the inode
49 * from the iint_cache slab, initialize the iint, and
50 * insert it into the radix tree.
52 * On success return a pointer to the iint; on failure return NULL.
54 struct ima_iint_cache
*ima_iint_insert(struct inode
*inode
)
56 struct ima_iint_cache
*iint
= NULL
;
61 iint
= kmem_cache_alloc(iint_cache
, GFP_KERNEL
);
65 rc
= radix_tree_preload(GFP_KERNEL
);
69 spin_lock(&ima_iint_lock
);
70 rc
= radix_tree_insert(&ima_iint_store
, (unsigned long)inode
, iint
);
71 spin_unlock(&ima_iint_lock
);
74 kmem_cache_free(iint_cache
, iint
);
76 iint
= radix_tree_lookup(&ima_iint_store
,
77 (unsigned long)inode
);
81 radix_tree_preload_end();
86 * ima_inode_alloc - allocate an iint associated with an inode
87 * @inode: pointer to the inode
89 * Return 0 on success, 1 on failure.
91 int ima_inode_alloc(struct inode
*inode
)
93 struct ima_iint_cache
*iint
;
98 iint
= ima_iint_insert(inode
);
104 /* ima_iint_find_insert_get - get the iint associated with an inode
106 * Most insertions are done at inode_alloc, except those allocated
107 * before late_initcall. When the iint does not exist, allocate it,
108 * initialize and insert it, and increment the iint refcount.
110 * (Can't initialize at security_initcall before any inodes are
111 * allocated, got to wait at least until proc_init.)
115 struct ima_iint_cache
*ima_iint_find_insert_get(struct inode
*inode
)
117 struct ima_iint_cache
*iint
= NULL
;
119 iint
= ima_iint_find_get(inode
);
123 iint
= ima_iint_insert(inode
);
125 kref_get(&iint
->refcount
);
129 EXPORT_SYMBOL_GPL(ima_iint_find_insert_get
);
131 /* iint_free - called when the iint refcount goes to zero */
132 void iint_free(struct kref
*kref
)
134 struct ima_iint_cache
*iint
= container_of(kref
, struct ima_iint_cache
,
138 if (iint
->readcount
!= 0) {
139 printk(KERN_INFO
"%s: readcount: %ld\n", __FUNCTION__
,
143 if (iint
->writecount
!= 0) {
144 printk(KERN_INFO
"%s: writecount: %ld\n", __FUNCTION__
,
146 iint
->writecount
= 0;
148 if (iint
->opencount
!= 0) {
149 printk(KERN_INFO
"%s: opencount: %ld\n", __FUNCTION__
,
153 kref_set(&iint
->refcount
, 1);
154 kmem_cache_free(iint_cache
, iint
);
157 void iint_rcu_free(struct rcu_head
*rcu_head
)
159 struct ima_iint_cache
*iint
= container_of(rcu_head
,
160 struct ima_iint_cache
, rcu
);
161 kref_put(&iint
->refcount
, iint_free
);
165 * ima_iint_delete - called on integrity_inode_free
166 * @inode: pointer to the inode
168 * Free the integrity information(iint) associated with an inode.
170 void ima_iint_delete(struct inode
*inode
)
172 struct ima_iint_cache
*iint
;
174 if (!ima_initialized
)
176 spin_lock(&ima_iint_lock
);
177 iint
= radix_tree_delete(&ima_iint_store
, (unsigned long)inode
);
178 spin_unlock(&ima_iint_lock
);
180 call_rcu(&iint
->rcu
, iint_rcu_free
);
183 static void init_once(void *foo
)
185 struct ima_iint_cache
*iint
= foo
;
187 memset(iint
, 0, sizeof *iint
);
190 mutex_init(&iint
->mutex
);
192 iint
->writecount
= 0;
194 kref_set(&iint
->refcount
, 1);
197 void ima_iintcache_init(void)
200 kmem_cache_create("iint_cache", sizeof(struct ima_iint_cache
), 0,
201 SLAB_PANIC
, init_once
);