Re: [PATCH v3 09/12] ima: add ima_inode_setxattr/removexattr functionand calls

From: Kasatkin, Dmitry
Date: Thu Mar 22 2012 - 10:22:20 EST


On Wed, Mar 21, 2012 at 8:54 PM, Mimi Zohar <zohar@xxxxxxxxxxxxxxxxxx> wrote:
> Based on xattr_permission comments, the restriction to modify 'security'
> xattr is left up to the underlying fs or lsm. Ensure that not just anyone
> can modify or remove 'security.ima'.
>
> Changelog v1:
> - Unless IMA-APPRAISE is configured, use stub ima_inode_removexattr()/setxattr()
> Âfunctions. Â(Moved ima_inode_removexattr()/setxattr() to ima_appraise.c)
>
> Changelog:
> Â- take i_mutex to fix locking (Dmitry Kasatkin)
> Â- ima_reset_appraise_flags should only be called when modifying or
> Â Âremoving the 'security.ima' xattr. Requires CAP_SYS_ADMIN privilege.
> Â Â(Incorporated fix from Roberto Sassu)
> Â- Even if allowed to update security.ima, reset the appraisal flags,
> Â Âforcing re-appraisal.
> Â- Replace CAP_MAC_ADMIN with CAP_SYS_ADMIN
> Â- static inline ima_inode_setxattr()/ima_inode_removexattr() stubs
> Â- ima_protect_xattr should be static
>
> Signed-off-by: Mimi Zohar <zohar@xxxxxxxxxx>

Signed-off-by: Dmitry Kasatkin <dmitry.kasatkin@xxxxxxxxx>

> ---
> Âinclude/linux/ima.h          |  17 ++++++++++
> Âsecurity/integrity/ima/ima_appraise.c | Â 57 +++++++++++++++++++++++++++++++++
> Âsecurity/security.c          |  Â6 +++
> Â3 files changed, 80 insertions(+), 0 deletions(-)
>
> diff --git a/include/linux/ima.h b/include/linux/ima.h
> index e2bfbb1..2c7223d 100644
> --- a/include/linux/ima.h
> +++ b/include/linux/ima.h
> @@ -44,10 +44,27 @@ static inline int ima_file_mmap(struct file *file, unsigned long prot)
>
> Â#ifdef CONFIG_IMA_APPRAISE
> Â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);
> Â#else
> Âstatic inline void ima_inode_post_setattr(struct dentry *dentry)
> Â{
> Â Â Â Âreturn;
> Â}
> +
> +static inline int ima_inode_setxattr(struct dentry *dentry,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âconst char *xattr_name,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âconst void *xattr_value,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âsize_t xattr_value_len)
> +{
> + Â Â Â return 0;
> +}
> +
> +static inline int ima_inode_removexattr(struct dentry *dentry,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â const char *xattr_name)
> +{
> + Â Â Â return 0;
> +}
> Â#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 681cb6e..becc7e0 100644
> --- a/security/integrity/ima/ima_appraise.c
> +++ b/security/integrity/ima/ima_appraise.c
> @@ -169,3 +169,60 @@ void ima_inode_post_setattr(struct dentry *dentry)
> Â Â Â Â Â Â Â Ârc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA);
> Â Â Â Âreturn;
> Â}
> +
> +/*
> + * ima_protect_xattr - protect 'security.ima'
> + *
> + * Ensure that not just anyone can modify or remove 'security.ima'.
> + */
> +static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Âconst void *xattr_value, size_t xattr_value_len)
> +{
> + Â Â Â if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
> + Â Â Â Â Â Â Â if (!capable(CAP_SYS_ADMIN))
> + Â Â Â Â Â Â Â Â Â Â Â return -EPERM;
> + Â Â Â Â Â Â Â return 1;
> + Â Â Â }
> + Â Â Â return 0;
> +}
> +
> +static void ima_reset_appraise_flags(struct inode *inode)
> +{
> + Â Â Â struct integrity_iint_cache *iint;
> +
> + Â Â Â if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode))
> + Â Â Â Â Â Â Â return;
> +
> + Â Â Â iint = integrity_iint_find(inode);
> + Â Â Â if (!iint)
> + Â Â Â Â Â Â Â return;
> +
> + Â Â Â iint->flags &= ~(IMA_COLLECTED | IMA_APPRAISED | IMA_MEASURED);
> + Â Â Â return;
> +}
> +
> +int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
> + Â Â Â Â Â Â Â Â Â Â Âconst void *xattr_value, size_t xattr_value_len)
> +{
> + Â Â Â int result;
> +
> + Â Â Â result = ima_protect_xattr(dentry, xattr_name, xattr_value,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âxattr_value_len);
> + Â Â Â if (result == 1) {
> + Â Â Â Â Â Â Â ima_reset_appraise_flags(dentry->d_inode);
> + Â Â Â Â Â Â Â result = 0;
> + Â Â Â }
> + Â Â Â return result;
> +}
> +
> +int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
> +{
> + Â Â Â int result;
> +
> + Â Â Â result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
> + Â Â Â if (result == 1) {
> + Â Â Â Â Â Â Â ima_reset_appraise_flags(dentry->d_inode);
> + Â Â Â Â Â Â Â result = 0;
> + Â Â Â }
> + Â Â Â return result;
> +}
> diff --git a/security/security.c b/security/security.c
> index e50bbf4..7bb127e 100644
> --- a/security/security.c
> +++ b/security/security.c
> @@ -557,6 +557,9 @@ int security_inode_setxattr(struct dentry *dentry, const char *name,
> Â Â Â Âret = security_ops->inode_setxattr(dentry, name, value, size, flags);
> Â Â Â Âif (ret)
> Â Â Â Â Â Â Â Âreturn ret;
> + Â Â Â ret = ima_inode_setxattr(dentry, name, value, size);
> + Â Â Â if (ret)
> + Â Â Â Â Â Â Â return ret;
> Â Â Â Âreturn evm_inode_setxattr(dentry, name, value, size);
> Â}
>
> @@ -592,6 +595,9 @@ int security_inode_removexattr(struct dentry *dentry, const char *name)
> Â Â Â Âret = security_ops->inode_removexattr(dentry, name);
> Â Â Â Âif (ret)
> Â Â Â Â Â Â Â Âreturn ret;
> + Â Â Â ret = ima_inode_removexattr(dentry, name);
> + Â Â Â if (ret)
> + Â Â Â Â Â Â Â return ret;
> Â Â Â Âreturn evm_inode_removexattr(dentry, name);
> Â}
>
> --
> 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/