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 RADIX_TREE(ima_iint_store, GFP_ATOMIC);
23 DEFINE_SPINLOCK(ima_iint_lock);
25 static struct kmem_cache *iint_cache __read_mostly;
27 /* ima_iint_find_get - return the iint associated with an inode
29 * ima_iint_find_get gets a reference to the iint. Caller must
30 * remember to put the iint reference.
32 struct ima_iint_cache *ima_iint_find_get(struct inode *inode)
34 struct ima_iint_cache *iint;
37 iint = radix_tree_lookup(&ima_iint_store, (unsigned long)inode);
40 kref_get(&iint->refcount);
47 * ima_inode_alloc - allocate an iint associated with an inode
48 * @inode: pointer to the inode
50 int ima_inode_alloc(struct inode *inode)
52 struct ima_iint_cache *iint = NULL;
55 iint = kmem_cache_alloc(iint_cache, GFP_NOFS);
59 rc = radix_tree_preload(GFP_NOFS);
63 spin_lock(&ima_iint_lock);
64 rc = radix_tree_insert(&ima_iint_store, (unsigned long)inode, iint);
65 spin_unlock(&ima_iint_lock);
66 radix_tree_preload_end();
69 kmem_cache_free(iint_cache, iint);
74 /* iint_free - called when the iint refcount goes to zero */
75 void iint_free(struct kref *kref)
77 struct ima_iint_cache *iint = container_of(kref, struct ima_iint_cache,
81 if (iint->readcount != 0) {
82 printk(KERN_INFO "%s: readcount: %ld\n", __FUNCTION__,
86 if (iint->writecount != 0) {
87 printk(KERN_INFO "%s: writecount: %ld\n", __FUNCTION__,
91 if (iint->opencount != 0) {
92 printk(KERN_INFO "%s: opencount: %ld\n", __FUNCTION__,
96 kref_set(&iint->refcount, 1);
97 kmem_cache_free(iint_cache, iint);
100 void iint_rcu_free(struct rcu_head *rcu_head)
102 struct ima_iint_cache *iint = container_of(rcu_head,
103 struct ima_iint_cache, rcu);
104 kref_put(&iint->refcount, iint_free);
108 * ima_inode_free - called on security_inode_free
109 * @inode: pointer to the inode
111 * Free the integrity information(iint) associated with an inode.
113 void ima_inode_free(struct inode *inode)
115 struct ima_iint_cache *iint;
117 spin_lock(&ima_iint_lock);
118 iint = radix_tree_delete(&ima_iint_store, (unsigned long)inode);
119 spin_unlock(&ima_iint_lock);
121 call_rcu(&iint->rcu, iint_rcu_free);
124 static void init_once(void *foo)
126 struct ima_iint_cache *iint = foo;
128 memset(iint, 0, sizeof *iint);
131 mutex_init(&iint->mutex);
133 iint->writecount = 0;
135 kref_set(&iint->refcount, 1);
138 static int __init ima_iintcache_init(void)
141 kmem_cache_create("iint_cache", sizeof(struct ima_iint_cache), 0,
142 SLAB_PANIC, init_once);
145 security_initcall(ima_iintcache_init);