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