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/slab.h>
18 #include <linux/module.h>
19 #include <linux/spinlock.h>
20 #include <linux/radix-tree.h>
23 RADIX_TREE(ima_iint_store, GFP_ATOMIC);
24 DEFINE_SPINLOCK(ima_iint_lock);
26 static struct kmem_cache *iint_cache __read_mostly;
28 /* ima_iint_find_get - return the iint associated with an inode
30 * ima_iint_find_get gets a reference to the iint. Caller must
31 * remember to put the iint reference.
33 struct ima_iint_cache *ima_iint_find_get(struct inode *inode)
35 struct ima_iint_cache *iint;
38 iint = radix_tree_lookup(&ima_iint_store, (unsigned long)inode);
41 kref_get(&iint->refcount);
48 * ima_inode_alloc - allocate an iint associated with an inode
49 * @inode: pointer to the inode
51 int ima_inode_alloc(struct inode *inode)
53 struct ima_iint_cache *iint = NULL;
56 iint = kmem_cache_alloc(iint_cache, GFP_NOFS);
60 rc = radix_tree_preload(GFP_NOFS);
64 spin_lock(&ima_iint_lock);
65 rc = radix_tree_insert(&ima_iint_store, (unsigned long)inode, iint);
66 spin_unlock(&ima_iint_lock);
67 radix_tree_preload_end();
70 kmem_cache_free(iint_cache, iint);
75 /* iint_free - called when the iint refcount goes to zero */
76 void iint_free(struct kref *kref)
78 struct ima_iint_cache *iint = container_of(kref, struct ima_iint_cache,
82 if (iint->readcount != 0) {
83 printk(KERN_INFO "%s: readcount: %ld\n", __func__,
87 if (iint->writecount != 0) {
88 printk(KERN_INFO "%s: writecount: %ld\n", __func__,
92 if (iint->opencount != 0) {
93 printk(KERN_INFO "%s: opencount: %ld\n", __func__,
97 kref_init(&iint->refcount);
98 kmem_cache_free(iint_cache, iint);
101 void iint_rcu_free(struct rcu_head *rcu_head)
103 struct ima_iint_cache *iint = container_of(rcu_head,
104 struct ima_iint_cache, rcu);
105 kref_put(&iint->refcount, iint_free);
109 * ima_inode_free - called on security_inode_free
110 * @inode: pointer to the inode
112 * Free the integrity information(iint) associated with an inode.
114 void ima_inode_free(struct inode *inode)
116 struct ima_iint_cache *iint;
118 spin_lock(&ima_iint_lock);
119 iint = radix_tree_delete(&ima_iint_store, (unsigned long)inode);
120 spin_unlock(&ima_iint_lock);
122 call_rcu(&iint->rcu, iint_rcu_free);
125 static void init_once(void *foo)
127 struct ima_iint_cache *iint = foo;
129 memset(iint, 0, sizeof *iint);
132 mutex_init(&iint->mutex);
134 iint->writecount = 0;
136 kref_init(&iint->refcount);
139 static int __init ima_iintcache_init(void)
142 kmem_cache_create("iint_cache", sizeof(struct ima_iint_cache), 0,
143 SLAB_PANIC, init_once);
146 security_initcall(ima_iintcache_init);