Re: [PATCH] tmpfs: security xattr setting on inode creation

From: Sakkinen, Jarkko
Date: Sat Feb 25 2012 - 02:04:17 EST


Hi Hugh,

On Sat, Feb 25, 2012 at 5:19 AM, Hugh Dickins <hughd@xxxxxxxxxx> wrote:
> Thanks a lot for doing this, Jarkko: I knew nothing about it.
> And thank you James for reviewing, I felt much happier seeing that.
>
> I did fiddle around with it slightly, to reduce the added textsize,
> and eliminate it when CONFIG_TMPFS_XATTR is off. ÂPlease, Jarkko
> and James, complain bitterly if I've messed up the good work.

Thank you for the nice feedback :) LGTM.

>
> [PATCH] tmpfs: security xattr setting on inode creation
>
> From: Jarkko Sakkinen <jarkko.sakkinen@xxxxxxxxx>
>
> Adds to generic xattr support introduced in Linux 3.0 by
> implementing initxattrs callback. This enables consulting
> of security attributes from LSM and EVM when inode is created.
>
> [hughd: moved under CONFIG_TMPFS_XATTR, with memcpy in shmem_xattr_alloc]
>
> Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@xxxxxxxxx>
> Reviewed-by: James Morris <james.l.morris@xxxxxxxxxx>
> Signed-off-by: Hugh Dickins <hughd@xxxxxxxxxx>
> ---
> Âmm/shmem.c | Â 88 +++++++++++++++++++++++++++++++++++++++++----------
> Â1 file changed, 72 insertions(+), 16 deletions(-)
>
> --- a/mm/shmem.c
> +++ b/mm/shmem.c
> @@ -1178,6 +1178,12 @@ static struct inode *shmem_get_inode(str
> Âstatic const struct inode_operations shmem_symlink_inode_operations;
> Âstatic const struct inode_operations shmem_short_symlink_operations;
>
> +#ifdef CONFIG_TMPFS_XATTR
> +static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
> +#else
> +#define shmem_initxattrs NULL
> +#endif
> +
> Âstatic int
> Âshmem_write_begin(struct file *file, struct address_space *mapping,
> Â Â Â Â Â Â Â Â Â Â Â Âloff_t pos, unsigned len, unsigned flags,
> @@ -1490,7 +1496,7 @@ shmem_mknod(struct inode *dir, struct de
> Â Â Â Âif (inode) {
> Â Â Â Â Â Â Â Âerror = security_inode_init_security(inode, dir,
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &dentry->d_name,
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ÂNULL, NULL);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âshmem_initxattrs, NULL);
> Â Â Â Â Â Â Â Âif (error) {
> Â Â Â Â Â Â Â Â Â Â Â Âif (error != -EOPNOTSUPP) {
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âiput(inode);
> @@ -1630,7 +1636,7 @@ static int shmem_symlink(struct inode *d
> Â Â Â Â Â Â Â Âreturn -ENOSPC;
>
> Â Â Â Âerror = security_inode_init_security(inode, dir, &dentry->d_name,
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ÂNULL, NULL);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âshmem_initxattrs, NULL);
> Â Â Â Âif (error) {
> Â Â Â Â Â Â Â Âif (error != -EOPNOTSUPP) {
> Â Â Â Â Â Â Â Â Â Â Â Âiput(inode);
> @@ -1704,6 +1710,66 @@ static void shmem_put_link(struct dentry
> Â* filesystem level, though.
> Â*/
>
> +/*
> + * Allocate new xattr and copy in the value; but leave the name to callers.
> + */
> +static struct shmem_xattr *shmem_xattr_alloc(const void *value, size_t size)
> +{
> + Â Â Â struct shmem_xattr *new_xattr;
> + Â Â Â size_t len;
> +
> + Â Â Â /* wrap around? */
> + Â Â Â len = sizeof(*new_xattr) + size;
> + Â Â Â if (len <= sizeof(*new_xattr))
> + Â Â Â Â Â Â Â return NULL;
> +
> + Â Â Â new_xattr = kmalloc(len, GFP_KERNEL);
> + Â Â Â if (!new_xattr)
> + Â Â Â Â Â Â Â return NULL;
> +
> + Â Â Â new_xattr->size = size;
> + Â Â Â memcpy(new_xattr->value, value, size);
> + Â Â Â return new_xattr;
> +}
> +
> +/*
> + * Callback for security_inode_init_security() for acquiring xattrs.
> + */
> +static int shmem_initxattrs(struct inode *inode,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â const struct xattr *xattr_array,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â void *fs_info)
> +{
> + Â Â Â struct shmem_inode_info *info = SHMEM_I(inode);
> + Â Â Â const struct xattr *xattr;
> + Â Â Â struct shmem_xattr *new_xattr;
> + Â Â Â size_t len;
> +
> + Â Â Â for (xattr = xattr_array; xattr->name != NULL; xattr++) {
> + Â Â Â Â Â Â Â new_xattr = shmem_xattr_alloc(xattr->value, xattr->value_len);
> + Â Â Â Â Â Â Â if (!new_xattr)
> + Â Â Â Â Â Â Â Â Â Â Â return -ENOMEM;
> +
> + Â Â Â Â Â Â Â len = strlen(xattr->name) + 1;
> + Â Â Â Â Â Â Â new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â GFP_KERNEL);
> + Â Â Â Â Â Â Â if (!new_xattr->name) {
> + Â Â Â Â Â Â Â Â Â Â Â kfree(new_xattr);
> + Â Â Â Â Â Â Â Â Â Â Â return -ENOMEM;
> + Â Â Â Â Â Â Â }
> +
> + Â Â Â Â Â Â Â memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
> + Â Â Â Â Â Â Â Â Â Â ÂXATTR_SECURITY_PREFIX_LEN);
> + Â Â Â Â Â Â Â memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
> + Â Â Â Â Â Â Â Â Â Â Âxattr->name, len);
> +
> + Â Â Â Â Â Â Â spin_lock(&info->lock);
> + Â Â Â Â Â Â Â list_add(&new_xattr->list, &info->xattr_list);
> + Â Â Â Â Â Â Â spin_unlock(&info->lock);
> + Â Â Â }
> +
> + Â Â Â return 0;
> +}
> +
> Âstatic int shmem_xattr_get(struct dentry *dentry, const char *name,
> Â Â Â Â Â Â Â Â Â Â Â Â Â void *buffer, size_t size)
> Â{
> @@ -1731,24 +1797,17 @@ static int shmem_xattr_get(struct dentry
> Â Â Â Âreturn ret;
> Â}
>
> -static int shmem_xattr_set(struct dentry *dentry, const char *name,
> +static int shmem_xattr_set(struct inode *inode, const char *name,
> Â Â Â Â Â Â Â Â Â Â Â Â Â const void *value, size_t size, int flags)
> Â{
> - Â Â Â struct inode *inode = dentry->d_inode;
> Â Â Â Âstruct shmem_inode_info *info = SHMEM_I(inode);
> Â Â Â Âstruct shmem_xattr *xattr;
> Â Â Â Âstruct shmem_xattr *new_xattr = NULL;
> - Â Â Â size_t len;
> Â Â Â Âint err = 0;
>
> Â Â Â Â/* value == NULL means remove */
> Â Â Â Âif (value) {
> - Â Â Â Â Â Â Â /* wrap around? */
> - Â Â Â Â Â Â Â len = sizeof(*new_xattr) + size;
> - Â Â Â Â Â Â Â if (len <= sizeof(*new_xattr))
> - Â Â Â Â Â Â Â Â Â Â Â return -ENOMEM;
> -
> - Â Â Â Â Â Â Â new_xattr = kmalloc(len, GFP_KERNEL);
> + Â Â Â Â Â Â Â new_xattr = shmem_xattr_alloc(value, size);
> Â Â Â Â Â Â Â Âif (!new_xattr)
> Â Â Â Â Â Â Â Â Â Â Â Âreturn -ENOMEM;
>
> @@ -1757,9 +1816,6 @@ static int shmem_xattr_set(struct dentry
> Â Â Â Â Â Â Â Â Â Â Â Âkfree(new_xattr);
> Â Â Â Â Â Â Â Â Â Â Â Âreturn -ENOMEM;
> Â Â Â Â Â Â Â Â}
> -
> - Â Â Â Â Â Â Â new_xattr->size = size;
> - Â Â Â Â Â Â Â memcpy(new_xattr->value, value, size);
> Â Â Â Â}
>
> Â Â Â Âspin_lock(&info->lock);
> @@ -1858,7 +1914,7 @@ static int shmem_setxattr(struct dentry
> Â Â Â Âif (size == 0)
> Â Â Â Â Â Â Â Âvalue = ""; Â/* empty EA, do not remove */
>
> - Â Â Â return shmem_xattr_set(dentry, name, value, size, flags);
> + Â Â Â return shmem_xattr_set(dentry->d_inode, name, value, size, flags);
>
> Â}
>
> @@ -1878,7 +1934,7 @@ static int shmem_removexattr(struct dent
> Â Â Â Âif (err)
> Â Â Â Â Â Â Â Âreturn err;
>
> - Â Â Â return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
> + Â Â Â return shmem_xattr_set(dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
> Â}
>
> Âstatic bool xattr_is_trusted(const char *name)

/Jarkko
N‹§²æìr¸›yúèšØb²X¬¶ÇvØ^–)Þ{.nÇ+‰·¥Š{±‘êçzX§¶›¡Ü}©ž²ÆzÚ&j:+v‰¨¾«‘êçzZ+€Ê+zf£¢·hšˆ§~†­†Ûiÿûàz¹®w¥¢¸?™¨è­Ú&¢)ßf”ù^jÇy§m…á@A«a¶Úÿ 0¶ìh®å’i