blob: 6cc22430cb4466ca0749f72a51f0016a4659709d [file] [log] [blame]
Mimi Zohar3323eec92009-02-04 09:06:58 -05001/*
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
15 * using a radix tree.
16 */
17#include <linux/module.h>
18#include <linux/spinlock.h>
19#include <linux/radix-tree.h>
20#include "ima.h"
21
Mimi Zohar3323eec92009-02-04 09:06:58 -050022RADIX_TREE(ima_iint_store, GFP_ATOMIC);
23DEFINE_SPINLOCK(ima_iint_lock);
24
25static struct kmem_cache *iint_cache __read_mostly;
26
27/* ima_iint_find_get - return the iint associated with an inode
28 *
29 * ima_iint_find_get gets a reference to the iint. Caller must
30 * remember to put the iint reference.
31 */
32struct ima_iint_cache *ima_iint_find_get(struct inode *inode)
33{
34 struct ima_iint_cache *iint;
35
36 rcu_read_lock();
37 iint = radix_tree_lookup(&ima_iint_store, (unsigned long)inode);
38 if (!iint)
39 goto out;
40 kref_get(&iint->refcount);
41out:
42 rcu_read_unlock();
43 return iint;
44}
45
Eric Paris93533842009-12-04 15:47:52 -050046/**
47 * ima_inode_alloc - allocate an iint associated with an inode
48 * @inode: pointer to the inode
Mimi Zohar3323eec92009-02-04 09:06:58 -050049 */
Eric Paris93533842009-12-04 15:47:52 -050050int ima_inode_alloc(struct inode *inode)
Mimi Zohar3323eec92009-02-04 09:06:58 -050051{
52 struct ima_iint_cache *iint = NULL;
53 int rc = 0;
54
Mimi Zoharc09c59e2009-11-18 16:16:06 -050055 iint = kmem_cache_alloc(iint_cache, GFP_NOFS);
Mimi Zohar3323eec92009-02-04 09:06:58 -050056 if (!iint)
Eric Paris93533842009-12-04 15:47:52 -050057 return -ENOMEM;
Mimi Zohar3323eec92009-02-04 09:06:58 -050058
Mimi Zoharc09c59e2009-11-18 16:16:06 -050059 rc = radix_tree_preload(GFP_NOFS);
Mimi Zohar3323eec92009-02-04 09:06:58 -050060 if (rc < 0)
61 goto out;
62
63 spin_lock(&ima_iint_lock);
64 rc = radix_tree_insert(&ima_iint_store, (unsigned long)inode, iint);
65 spin_unlock(&ima_iint_lock);
Xiaotian Fengbaac35c2010-02-24 18:39:02 +080066 radix_tree_preload_end();
Mimi Zohar3323eec92009-02-04 09:06:58 -050067out:
Eric Paris93533842009-12-04 15:47:52 -050068 if (rc < 0)
Mimi Zohar3323eec92009-02-04 09:06:58 -050069 kmem_cache_free(iint_cache, iint);
Eric Paris93533842009-12-04 15:47:52 -050070
Eric Paris93533842009-12-04 15:47:52 -050071 return rc;
Mimi Zohar3323eec92009-02-04 09:06:58 -050072}
73
Mimi Zohar3323eec92009-02-04 09:06:58 -050074/* iint_free - called when the iint refcount goes to zero */
75void iint_free(struct kref *kref)
76{
77 struct ima_iint_cache *iint = container_of(kref, struct ima_iint_cache,
78 refcount);
79 iint->version = 0;
80 iint->flags = 0UL;
Mimi Zohar1df9f0a2009-02-04 09:07:02 -050081 if (iint->readcount != 0) {
H Hartley Sweetena19c5bb2010-03-09 17:59:59 -060082 printk(KERN_INFO "%s: readcount: %ld\n", __func__,
Mimi Zohar1df9f0a2009-02-04 09:07:02 -050083 iint->readcount);
84 iint->readcount = 0;
85 }
86 if (iint->writecount != 0) {
H Hartley Sweetena19c5bb2010-03-09 17:59:59 -060087 printk(KERN_INFO "%s: writecount: %ld\n", __func__,
Mimi Zohar1df9f0a2009-02-04 09:07:02 -050088 iint->writecount);
89 iint->writecount = 0;
90 }
91 if (iint->opencount != 0) {
H Hartley Sweetena19c5bb2010-03-09 17:59:59 -060092 printk(KERN_INFO "%s: opencount: %ld\n", __func__,
Mimi Zohar1df9f0a2009-02-04 09:07:02 -050093 iint->opencount);
94 iint->opencount = 0;
95 }
Mimi Zohar3323eec92009-02-04 09:06:58 -050096 kref_set(&iint->refcount, 1);
97 kmem_cache_free(iint_cache, iint);
98}
99
100void iint_rcu_free(struct rcu_head *rcu_head)
101{
102 struct ima_iint_cache *iint = container_of(rcu_head,
103 struct ima_iint_cache, rcu);
104 kref_put(&iint->refcount, iint_free);
105}
106
107/**
Eric Paris85a17f52009-12-04 15:48:08 -0500108 * ima_inode_free - called on security_inode_free
Mimi Zohar3323eec92009-02-04 09:06:58 -0500109 * @inode: pointer to the inode
110 *
111 * Free the integrity information(iint) associated with an inode.
112 */
Eric Paris85a17f52009-12-04 15:48:08 -0500113void ima_inode_free(struct inode *inode)
Mimi Zohar3323eec92009-02-04 09:06:58 -0500114{
115 struct ima_iint_cache *iint;
116
Mimi Zohar3323eec92009-02-04 09:06:58 -0500117 spin_lock(&ima_iint_lock);
118 iint = radix_tree_delete(&ima_iint_store, (unsigned long)inode);
119 spin_unlock(&ima_iint_lock);
120 if (iint)
121 call_rcu(&iint->rcu, iint_rcu_free);
122}
123
124static void init_once(void *foo)
125{
126 struct ima_iint_cache *iint = foo;
127
128 memset(iint, 0, sizeof *iint);
129 iint->version = 0;
130 iint->flags = 0UL;
131 mutex_init(&iint->mutex);
132 iint->readcount = 0;
133 iint->writecount = 0;
Mimi Zohar1df9f0a2009-02-04 09:07:02 -0500134 iint->opencount = 0;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500135 kref_set(&iint->refcount, 1);
136}
137
Eric Paris54bb6552009-12-09 15:29:01 -0500138static int __init ima_iintcache_init(void)
Mimi Zohar3323eec92009-02-04 09:06:58 -0500139{
140 iint_cache =
141 kmem_cache_create("iint_cache", sizeof(struct ima_iint_cache), 0,
142 SLAB_PANIC, init_once);
Eric Paris54bb6552009-12-09 15:29:01 -0500143 return 0;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500144}
Eric Paris54bb6552009-12-09 15:29:01 -0500145security_initcall(ima_iintcache_init);