From: Mimi Zohar <zohar@linux.vnet.ibm.com>
To: linux-security-module@vger.kernel.org
Cc: Mimi Zohar <zohar@linux.vnet.ibm.com>,
linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
Al Viro <viro@ZenIV.linux.org.uk>,
David Safford <safford@linux.vnet.ibm.com>,
Dmitry Kasatkin <dmitry.kasatkin@intel.com>,
Matt Helsley <matt.helsley@gmail.com>,
Mimi Zohar <zohar@us.ibm.com>
Subject: [PATCH v3 10/12] ima: defer calling __fput()
Date: Wed, 21 Mar 2012 14:54:15 -0400 [thread overview]
Message-ID: <1332356057-3356-11-git-send-email-zohar@linux.vnet.ibm.com> (raw)
In-Reply-To: <1332356057-3356-1-git-send-email-zohar@linux.vnet.ibm.com>
If a file is closed before it is munmapped, __fput() is called with
the mmap_sem taken. With IMA-appraisal enabled, this results in an
mmap_sem/i_mutex lockdep. ima_defer_fput() increments the f_count
to defer __fput() being called until after the mmap_sem is released.
Changelog v3:
- use slab mempool (suggested by Dmitry Kasatkin)
- renamed a few functions based on Matt Helsley's review
- define ima_fput_mempool as static (fix sparse error)
- fix ima_defer_fput() stub definition
Changelog v2:
- based on discussions with: Tyler Hicks (use workqueues),
Dave Hansen (defer only IMA related files), and
Dmitry Kasatkin (add new IMA hook)
- use slab memory for allocating work
Signed-off-by: Mimi Zohar <zohar@us.ibm.com>
---
include/linux/ima.h | 5 ++
mm/mmap.c | 1 +
security/integrity/ima/ima_appraise.c | 84 +++++++++++++++++++++++++++++++++
3 files changed, 90 insertions(+), 0 deletions(-)
diff --git a/include/linux/ima.h b/include/linux/ima.h
index 2c7223d..af8cb3b 100644
--- a/include/linux/ima.h
+++ b/include/linux/ima.h
@@ -47,6 +47,7 @@ extern void ima_inode_post_setattr(struct dentry *dentry);
extern int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
const void *xattr_value, size_t xattr_value_len);
extern int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name);
+extern void ima_defer_fput(struct file *file);
#else
static inline void ima_inode_post_setattr(struct dentry *dentry)
{
@@ -66,5 +67,9 @@ static inline int ima_inode_removexattr(struct dentry *dentry,
{
return 0;
}
+static inline void ima_defer_fput(struct file *file)
+{
+ return;
+}
#endif /* CONFIG_IMA_APPRAISE_H */
#endif /* _LINUX_IMA_H */
diff --git a/mm/mmap.c b/mm/mmap.c
index 4c16a0a..c1accd4 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -232,6 +232,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
if (vma->vm_ops && vma->vm_ops->close)
vma->vm_ops->close(vma);
if (vma->vm_file) {
+ ima_defer_fput(vma->vm_file); /* drop mmap_sem first */
fput(vma->vm_file);
if (vma->vm_flags & VM_EXECUTABLE)
removed_exe_file_vma(vma->vm_mm);
diff --git a/security/integrity/ima/ima_appraise.c b/security/integrity/ima/ima_appraise.c
index becc7e0..a100a3c 100644
--- a/security/integrity/ima/ima_appraise.c
+++ b/security/integrity/ima/ima_appraise.c
@@ -11,6 +11,7 @@
#include <linux/module.h>
#include <linux/file.h>
#include <linux/fs.h>
+#include <linux/mempool.h>
#include <linux/xattr.h>
#include <linux/magic.h>
#include <linux/ima.h>
@@ -226,3 +227,86 @@ int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
}
return result;
}
+
+struct ima_fput_req {
+ struct file *file;
+ struct work_struct work;
+};
+static struct kmem_cache *ima_fput_slab;
+static mempool_t *ima_fput_mempool;
+
+static void ima_deferred_fput(struct work_struct *work)
+{
+ struct ima_fput_req *deferred = container_of(work, struct ima_fput_req,
+ work);
+
+ fput(deferred->file);
+ mempool_free(deferred, ima_fput_mempool);
+}
+
+/**
+ * ima_defer_fput - defer calling __fput() for mmaped files
+ * @file: pointer to file structure to be freed
+ *
+ * The i_mutex is taken by ima_file_free(), called on __fput(), to
+ * update 'security.ima' to reflect the current file data hash. When
+ * a file is closed before it is munmapped, __fput() is called with
+ * the mmap_sem taken, resulting in an mmap_sem/i_mutex lockdep.
+ * This function defers calling __fput() until after the mmap_sem is
+ * released.
+ */
+void ima_defer_fput(struct file *file)
+{
+ struct dentry *dentry = file->f_path.dentry;
+ struct inode *inode = dentry->d_inode;
+ struct ima_fput_req *defer;
+ int rc;
+
+ if (!IS_IMA(inode))
+ return;
+
+ defer = mempool_alloc(ima_fput_mempool, GFP_NOIO);
+ if (!defer)
+ return;
+
+ get_file(file);
+
+ defer->file = file;
+ INIT_WORK(&defer->work, ima_deferred_fput);
+ rc = schedule_work(&defer->work);
+ if (rc == 0) {
+ pr_info("ima: schedule work failed\n");
+ mempool_free(defer, ima_fput_mempool);
+ fput_atomic(file);
+ }
+}
+
+static int __init ima_fput_mempool_create(void)
+{
+ ima_fput_slab = kmem_cache_create("ima_fput",
+ sizeof(struct ima_fput_req),
+ 0, 0, NULL);
+ if (!ima_fput_slab) {
+ pr_info("ima: failed kmem_cache_create\n");
+ goto err_out;
+ }
+
+ ima_fput_mempool = mempool_create_slab_pool(1, ima_fput_slab);
+ if (!ima_fput_mempool) {
+ pr_info("ima: failed mempool_create_slab_pool\n");
+ goto free_slab;
+ }
+ return 0;
+
+free_slab:
+ kmem_cache_destroy(ima_fput_slab);
+err_out:
+ return -ENOMEM;
+}
+
+static void __exit ima_fput_mempool_destroy(void)
+{
+ mempool_destroy(ima_fput_mempool);
+ kmem_cache_destroy(ima_fput_slab);
+}
+security_initcall(ima_fput_mempool_create);
--
1.7.6.5
next prev parent reply other threads:[~2012-03-21 18:58 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-03-21 18:54 [PATCH v3 00/12] ima: appraisal extension Mimi Zohar
2012-03-21 18:54 ` [PATCH v3 01/12] vfs: extend vfs_removexattr locking Mimi Zohar
2012-03-21 18:54 ` [PATCH v3 02/12] vfs: move ima_file_free before releasing the file Mimi Zohar
2012-03-22 14:23 ` Kasatkin, Dmitry
2012-03-21 18:54 ` [PATCH v3 03/12] ima: free securityfs violations file Mimi Zohar
2012-03-21 18:54 ` [PATCH v3 04/12] ima: integrity appraisal extension Mimi Zohar
2012-03-22 14:28 ` Kasatkin, Dmitry
2012-03-21 18:54 ` [PATCH v3 05/12] ima: add appraise action keywords and default rules Mimi Zohar
2012-03-22 14:27 ` Kasatkin, Dmitry
2012-03-21 18:54 ` [PATCH v3 06/12] ima: allocating iint improvements Mimi Zohar
2012-03-21 18:54 ` [PATCH v3 07/12] ima: replace iint spinlock with rwlock/read_lock Mimi Zohar
2012-03-21 18:54 ` [PATCH v3 08/12] ima: add inode_post_setattr call Mimi Zohar
2012-03-22 14:21 ` Kasatkin, Dmitry
2012-03-21 18:54 ` [PATCH v3 09/12] ima: add ima_inode_setxattr/removexattr function and calls Mimi Zohar
2012-03-22 14:22 ` Kasatkin, Dmitry
2012-03-21 18:54 ` Mimi Zohar [this message]
2012-03-22 14:07 ` [PATCH v3 10/12] ima: defer calling __fput() Kasatkin, Dmitry
2012-03-22 14:22 ` Al Viro
2012-03-22 14:53 ` Mimi Zohar
2012-03-22 14:58 ` Kasatkin, Dmitry
2012-03-22 15:09 ` Al Viro
2012-03-22 15:19 ` Kasatkin, Dmitry
2012-03-22 15:39 ` Al Viro
2012-03-23 14:55 ` Mimi Zohar
2012-03-21 18:54 ` [PATCH v3 11/12] ima: add support for different security.ima data types Mimi Zohar
2012-03-21 18:54 ` [PATCH v3 12/12] ima: digital signature verification support Mimi Zohar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1332356057-3356-11-git-send-email-zohar@linux.vnet.ibm.com \
--to=zohar@linux.vnet.ibm.com \
--cc=dmitry.kasatkin@intel.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=matt.helsley@gmail.com \
--cc=safford@linux.vnet.ibm.com \
--cc=viro@ZenIV.linux.org.uk \
--cc=zohar@us.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).