Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 22 | #define ima_iint_delete ima_inode_free |
| 23 | |
| 24 | RADIX_TREE(ima_iint_store, GFP_ATOMIC); |
| 25 | DEFINE_SPINLOCK(ima_iint_lock); |
| 26 | |
| 27 | static struct kmem_cache *iint_cache __read_mostly; |
| 28 | |
| 29 | /* ima_iint_find_get - return the iint associated with an inode |
| 30 | * |
| 31 | * ima_iint_find_get gets a reference to the iint. Caller must |
| 32 | * remember to put the iint reference. |
| 33 | */ |
| 34 | struct ima_iint_cache *ima_iint_find_get(struct inode *inode) |
| 35 | { |
| 36 | struct ima_iint_cache *iint; |
| 37 | |
| 38 | rcu_read_lock(); |
| 39 | iint = radix_tree_lookup(&ima_iint_store, (unsigned long)inode); |
| 40 | if (!iint) |
| 41 | goto out; |
| 42 | kref_get(&iint->refcount); |
| 43 | out: |
| 44 | rcu_read_unlock(); |
| 45 | return iint; |
| 46 | } |
| 47 | |
Eric Paris | 9353384 | 2009-12-04 15:47:52 -0500 | [diff] [blame^] | 48 | /** |
| 49 | * ima_inode_alloc - allocate an iint associated with an inode |
| 50 | * @inode: pointer to the inode |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 51 | */ |
Eric Paris | 9353384 | 2009-12-04 15:47:52 -0500 | [diff] [blame^] | 52 | int ima_inode_alloc(struct inode *inode) |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 53 | { |
| 54 | struct ima_iint_cache *iint = NULL; |
| 55 | int rc = 0; |
| 56 | |
| 57 | if (!ima_initialized) |
Eric Paris | 9353384 | 2009-12-04 15:47:52 -0500 | [diff] [blame^] | 58 | return 0; |
| 59 | |
Mimi Zohar | c09c59e | 2009-11-18 16:16:06 -0500 | [diff] [blame] | 60 | iint = kmem_cache_alloc(iint_cache, GFP_NOFS); |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 61 | if (!iint) |
Eric Paris | 9353384 | 2009-12-04 15:47:52 -0500 | [diff] [blame^] | 62 | return -ENOMEM; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 63 | |
Mimi Zohar | c09c59e | 2009-11-18 16:16:06 -0500 | [diff] [blame] | 64 | rc = radix_tree_preload(GFP_NOFS); |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 65 | if (rc < 0) |
| 66 | goto out; |
| 67 | |
| 68 | spin_lock(&ima_iint_lock); |
| 69 | rc = radix_tree_insert(&ima_iint_store, (unsigned long)inode, iint); |
| 70 | spin_unlock(&ima_iint_lock); |
| 71 | out: |
Eric Paris | 9353384 | 2009-12-04 15:47:52 -0500 | [diff] [blame^] | 72 | if (rc < 0) |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 73 | kmem_cache_free(iint_cache, iint); |
Eric Paris | 9353384 | 2009-12-04 15:47:52 -0500 | [diff] [blame^] | 74 | |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 75 | radix_tree_preload_end(); |
Eric Paris | 9353384 | 2009-12-04 15:47:52 -0500 | [diff] [blame^] | 76 | |
| 77 | return rc; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 78 | } |
| 79 | |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 80 | /* iint_free - called when the iint refcount goes to zero */ |
| 81 | void iint_free(struct kref *kref) |
| 82 | { |
| 83 | struct ima_iint_cache *iint = container_of(kref, struct ima_iint_cache, |
| 84 | refcount); |
| 85 | iint->version = 0; |
| 86 | iint->flags = 0UL; |
Mimi Zohar | 1df9f0a | 2009-02-04 09:07:02 -0500 | [diff] [blame] | 87 | if (iint->readcount != 0) { |
| 88 | printk(KERN_INFO "%s: readcount: %ld\n", __FUNCTION__, |
| 89 | iint->readcount); |
| 90 | iint->readcount = 0; |
| 91 | } |
| 92 | if (iint->writecount != 0) { |
| 93 | printk(KERN_INFO "%s: writecount: %ld\n", __FUNCTION__, |
| 94 | iint->writecount); |
| 95 | iint->writecount = 0; |
| 96 | } |
| 97 | if (iint->opencount != 0) { |
| 98 | printk(KERN_INFO "%s: opencount: %ld\n", __FUNCTION__, |
| 99 | iint->opencount); |
| 100 | iint->opencount = 0; |
| 101 | } |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 102 | kref_set(&iint->refcount, 1); |
| 103 | kmem_cache_free(iint_cache, iint); |
| 104 | } |
| 105 | |
| 106 | void iint_rcu_free(struct rcu_head *rcu_head) |
| 107 | { |
| 108 | struct ima_iint_cache *iint = container_of(rcu_head, |
| 109 | struct ima_iint_cache, rcu); |
| 110 | kref_put(&iint->refcount, iint_free); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * ima_iint_delete - called on integrity_inode_free |
| 115 | * @inode: pointer to the inode |
| 116 | * |
| 117 | * Free the integrity information(iint) associated with an inode. |
| 118 | */ |
| 119 | void ima_iint_delete(struct inode *inode) |
| 120 | { |
| 121 | struct ima_iint_cache *iint; |
| 122 | |
| 123 | if (!ima_initialized) |
| 124 | return; |
| 125 | spin_lock(&ima_iint_lock); |
| 126 | iint = radix_tree_delete(&ima_iint_store, (unsigned long)inode); |
| 127 | spin_unlock(&ima_iint_lock); |
| 128 | if (iint) |
| 129 | call_rcu(&iint->rcu, iint_rcu_free); |
| 130 | } |
| 131 | |
| 132 | static void init_once(void *foo) |
| 133 | { |
| 134 | struct ima_iint_cache *iint = foo; |
| 135 | |
| 136 | memset(iint, 0, sizeof *iint); |
| 137 | iint->version = 0; |
| 138 | iint->flags = 0UL; |
| 139 | mutex_init(&iint->mutex); |
| 140 | iint->readcount = 0; |
| 141 | iint->writecount = 0; |
Mimi Zohar | 1df9f0a | 2009-02-04 09:07:02 -0500 | [diff] [blame] | 142 | iint->opencount = 0; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 143 | kref_set(&iint->refcount, 1); |
| 144 | } |
| 145 | |
Eric Paris | 932995f | 2009-05-21 15:43:32 -0400 | [diff] [blame] | 146 | void __init ima_iintcache_init(void) |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 147 | { |
| 148 | iint_cache = |
| 149 | kmem_cache_create("iint_cache", sizeof(struct ima_iint_cache), 0, |
| 150 | SLAB_PANIC, init_once); |
| 151 | } |