Re: [PATCH v4 10/12] ima: defer calling __fput()

From: Kasatkin, Dmitry
Date: Wed Apr 04 2012 - 04:53:12 EST


On Thu, Mar 29, 2012 at 5:41 PM, Mimi Zohar <zohar@xxxxxxxxxxxxxxxxxx> wrote:
> ima_file_free(), which is called on __fput(), updates the file data
> hash stored as an extended attribute to reflect file changes. Â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 the __fput() being called until after the mmap_sem is released.
>
> The number of __fput() calls needing to be deferred is minimal. ÂOnly
> those files in policy, that were closed prior to the munmap and were
> mmapped write, need to defer the __fput().
>
> With this patch, on a clean F16 install, from boot to login, only
> 5 out of ~100,000 mmap_sem held fput() calls were deferred.
>

Hello Al,

Could you please provide your comments?


Thanks,
Dmitry

> Changelog v4:
> - ima_defer_fput() performance improvements
> - move ima_defer_fput() call from remove_vma() to __fput()
> - only defer mmapped files opened for write
> - remove unnecessary ima_fput_mempool_destroy()
>
> 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@xxxxxxxxxx>
> Signed-off-by: Dmitry Kasatkin <dmitry.kasatkin@xxxxxxxxx>
> ---
> Âfs/file_table.c            |  Â5 ++
> Âinclude/linux/ima.h          |  Â5 ++
> Âsecurity/integrity/ima/ima_appraise.c | Â 81 +++++++++++++++++++++++++++++++++
> Â3 files changed, 91 insertions(+), 0 deletions(-)
>
> diff --git a/fs/file_table.c b/fs/file_table.c
> index 554161a..b91f039 100644
> --- a/fs/file_table.c
> +++ b/fs/file_table.c
> @@ -229,6 +229,11 @@ static void __fput(struct file *file)
> Â Â Â Âstruct vfsmount *mnt = file->f_path.mnt;
> Â Â Â Âstruct inode *inode = dentry->d_inode;
>
> + Â Â Â if (current->mm && rwsem_is_locked(&current->mm->mmap_sem)) {
> + Â Â Â Â Â Â Â if (ima_defer_fput(file) == 0)
> + Â Â Â Â Â Â Â Â Â Â Â return;
> + Â Â Â }
> +
> Â Â Â Âmight_sleep();
>
> Â Â Â Âfsnotify_close(file);
> diff --git a/include/linux/ima.h b/include/linux/ima.h
> index 2c7223d..bbfac16 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 int 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 int ima_defer_fput(struct file *file)
> +{
> + Â Â Â return 1;
> +}
> Â#endif /* CONFIG_IMA_APPRAISE_H */
> Â#endif /* _LINUX_IMA_H */
> diff --git a/security/integrity/ima/ima_appraise.c b/security/integrity/ima/ima_appraise.c
> index becc7e0..220b20e 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,83 @@ 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.
> + *
> + * Return 0 on success, 1 when unnecessary, or errno on failure.
> + */
> +int 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 result;
> +
> + Â Â Â if (!(file->f_mode & FMODE_WRITE) || !IS_IMA(inode))
> + Â Â Â Â Â Â Â return 1;
> +
> + Â Â Â defer = mempool_alloc(ima_fput_mempool, GFP_NOIO);
> + Â Â Â if (!defer) Â Â /* should never happen */
> + Â Â Â Â Â Â Â return -ENOMEM;
> +
> + Â Â Â get_file(file);
> +
> + Â Â Â defer->file = file;
> + Â Â Â INIT_WORK(&defer->work, ima_deferred_fput);
> + Â Â Â result = schedule_work(&defer->work);
> + Â Â Â if (result == 0) {
> + Â Â Â Â Â Â Â pr_info("ima: fput already scheduled\n");
> + Â Â Â Â Â Â Â mempool_free(defer, ima_fput_mempool);
> + Â Â Â Â Â Â Â fput_atomic(file);
> + Â Â Â }
> + Â Â Â return 0;
> +}
> +
> +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;
> +}
> +security_initcall(ima_fput_mempool_create);
> --
> 1.7.6.5
>
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/