All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] xattr: enforce the name and size limits for in-kernel callers
@ 2026-11-23 11:35 Amir Goldstein
  2026-11-23 14:08 ` Alexander Mikhalitsyn
  2026-11-23 16:58 ` [PATCH v2] " Amir Goldstein
  0 siblings, 2 replies; 11+ messages in thread
From: Amir Goldstein @ 2026-11-23 11:35 UTC (permalink / raw)
  To: Christian Brauner
  Cc: Miklos Szeredi, Chris Mason, Christoph Hellwig, Theodore Ts'o,
	Carlos Maiolino, linux-fsdevel, linux-unionfs, stable

The xattr system calls refuse a name longer than XATTR_NAME_MAX and a
value larger than XATTR_SIZE_MAX, but the VFS helpers do not. An
in-kernel caller may pass a name or a size that it got from another
filesystem, such as overlayfs copy up from a lower layer.

Check both limits before calling the xattr handler of the filesystem,
and fail with the errors that the system calls return for them.

Cc: stable@vger.kernel.org
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
---
 fs/xattr.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/fs/xattr.c b/fs/xattr.c
index 0705801a62cc5..824e8349e8c10 100644
--- a/fs/xattr.c
+++ b/fs/xattr.c
@@ -178,6 +178,18 @@ xattr_permission(const struct mnt_idmap *idmap, struct inode *inode,
 	return inode_permission(idmap, inode, mask);
 }
 
+/*
+ * The xattr system calls refuse a name above XATTR_NAME_MAX and a value above
+ * XATTR_SIZE_MAX, but an in-kernel caller may pass a name or a size that it
+ * got from another filesystem, such as the lower layer of overlayfs.
+ */
+static int xattr_check_name(const char *name)
+{
+	if (strnlen(name, XATTR_NAME_MAX + 1) > XATTR_NAME_MAX)
+		return -ERANGE;
+	return 0;
+}
+
 /*
  * Look for any handler that deals with the specified namespace.
  */
@@ -209,10 +221,17 @@ __vfs_setxattr(const struct mnt_idmap *idmap, struct dentry *dentry,
 	       size_t size, int flags)
 {
 	const struct xattr_handler *handler;
+	int error;
 
 	if (is_posix_acl_xattr(name))
 		return -EOPNOTSUPP;
 
+	error = xattr_check_name(name);
+	if (error)
+		return error;
+	if (size > XATTR_SIZE_MAX)
+		return -E2BIG;
+
 	handler = xattr_resolve_name(inode, &name);
 	if (IS_ERR(handler))
 		return PTR_ERR(handler);
@@ -416,6 +435,8 @@ vfs_getxattr_alloc(const struct mnt_idmap *idmap, struct dentry *dentry,
 	error = handler->get(handler, dentry, inode, name, NULL, 0);
 	if (error < 0)
 		return error;
+	if (error > XATTR_SIZE_MAX)
+		return -E2BIG;
 
 	if (!value || (error > xattr_size)) {
 		value = krealloc(*xattr_value, error + 1, flags);
@@ -434,16 +455,24 @@ __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
 	       void *value, size_t size)
 {
 	const struct xattr_handler *handler;
+	ssize_t ret;
 
 	if (is_posix_acl_xattr(name))
 		return -EOPNOTSUPP;
 
+	ret = xattr_check_name(name);
+	if (ret)
+		return ret;
+
 	handler = xattr_resolve_name(inode, &name);
 	if (IS_ERR(handler))
 		return PTR_ERR(handler);
 	if (!handler->get)
 		return -EOPNOTSUPP;
-	return handler->get(handler, dentry, inode, name, value, size);
+	ret = handler->get(handler, dentry, inode, name, value, size);
+	if (ret > XATTR_SIZE_MAX)
+		return -E2BIG;
+	return ret;
 }
 EXPORT_SYMBOL(__vfs_getxattr);
 
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] xattr: enforce the name and size limits for in-kernel callers
  2026-11-23 11:35 [PATCH] xattr: enforce the name and size limits for in-kernel callers Amir Goldstein
@ 2026-11-23 14:08 ` Alexander Mikhalitsyn
  2026-11-23 16:58 ` [PATCH v2] " Amir Goldstein
  1 sibling, 0 replies; 11+ messages in thread
From: Alexander Mikhalitsyn @ 2026-11-23 14:08 UTC (permalink / raw)
  To: Amir Goldstein, Christian Brauner
  Cc: Miklos Szeredi, Chris Mason, Christoph Hellwig, Theodore Ts'o,
	Carlos Maiolino, linux-fsdevel, linux-unionfs, stable

On Mon, 2026-11-23 at 12:35 +0100, Amir Goldstein wrote:
> The xattr system calls refuse a name longer than XATTR_NAME_MAX and a
> value larger than XATTR_SIZE_MAX, but the VFS helpers do not. An
> in-kernel caller may pass a name or a size that it got from another
> filesystem, such as overlayfs copy up from a lower layer.
> 
> Check both limits before calling the xattr handler of the filesystem,
> and fail with the errors that the system calls return for them.
> 
> Cc: stable@vger.kernel.org
> Signed-off-by: Amir Goldstein <amir73il@gmail.com>
> ---
>  fs/xattr.c | 31 ++++++++++++++++++++++++++++++-
>  1 file changed, 30 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/xattr.c b/fs/xattr.c
> index 0705801a62cc5..824e8349e8c10 100644
> --- a/fs/xattr.c
> +++ b/fs/xattr.c
> @@ -178,6 +178,18 @@ xattr_permission(const struct mnt_idmap *idmap,
> struct inode *inode,
>  	return inode_permission(idmap, inode, mask);
>  }
>  
> +/*
> + * The xattr system calls refuse a name above XATTR_NAME_MAX and a
> value above
> + * XATTR_SIZE_MAX, but an in-kernel caller may pass a name or a size
> that it
> + * got from another filesystem, such as the lower layer of
> overlayfs.
> + */
> +static int xattr_check_name(const char *name)
> +{
> +	if (strnlen(name, XATTR_NAME_MAX + 1) > XATTR_NAME_MAX)
> +		return -ERANGE;
> +	return 0;
> +}
> +
>  /*
>   * Look for any handler that deals with the specified namespace.
>   */
> @@ -209,10 +221,17 @@ __vfs_setxattr(const struct mnt_idmap *idmap,
> struct dentry *dentry,
>  	       size_t size, int flags)
>  {
>  	const struct xattr_handler *handler;
> +	int error;
>  
>  	if (is_posix_acl_xattr(name))
>  		return -EOPNOTSUPP;
>  
> +	error = xattr_check_name(name);
> +	if (error)
> +		return error;
> +	if (size > XATTR_SIZE_MAX)
> +		return -E2BIG;

Hi Amir,

> +
>  	handler = xattr_resolve_name(inode, &name);
>  	if (IS_ERR(handler))
>  		return PTR_ERR(handler);
> @@ -416,6 +435,8 @@ vfs_getxattr_alloc(const struct mnt_idmap *idmap,
> struct dentry *dentry,

Don't we want to have xattr_check_name() in vfs_getxattr_alloc() too?

Kind regards,
Alex

>  	error = handler->get(handler, dentry, inode, name, NULL, 0);
>  	if (error < 0)
>  		return error;
> +	if (error > XATTR_SIZE_MAX)
> +		return -E2BIG;
>  
>  	if (!value || (error > xattr_size)) {
>  		value = krealloc(*xattr_value, error + 1, flags);
> @@ -434,16 +455,24 @@ __vfs_getxattr(struct dentry *dentry, struct
> inode *inode, const char *name,
>  	       void *value, size_t size)
>  {
>  	const struct xattr_handler *handler;
> +	ssize_t ret;
>  
>  	if (is_posix_acl_xattr(name))
>  		return -EOPNOTSUPP;
>  
> +	ret = xattr_check_name(name);
> +	if (ret)
> +		return ret;
> +
>  	handler = xattr_resolve_name(inode, &name);
>  	if (IS_ERR(handler))
>  		return PTR_ERR(handler);
>  	if (!handler->get)
>  		return -EOPNOTSUPP;
> -	return handler->get(handler, dentry, inode, name, value,
> size);
> +	ret = handler->get(handler, dentry, inode, name, value,
> size);
> +	if (ret > XATTR_SIZE_MAX)
> +		return -E2BIG;
> +	return ret;
>  }
>  EXPORT_SYMBOL(__vfs_getxattr);
>  

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2] xattr: enforce the name and size limits for in-kernel callers
  2026-11-23 11:35 [PATCH] xattr: enforce the name and size limits for in-kernel callers Amir Goldstein
  2026-11-23 14:08 ` Alexander Mikhalitsyn
@ 2026-11-23 16:58 ` Amir Goldstein
  2026-11-23 19:09   ` Alexander Mikhalitsyn
                     ` (3 more replies)
  1 sibling, 4 replies; 11+ messages in thread
From: Amir Goldstein @ 2026-11-23 16:58 UTC (permalink / raw)
  To: Christian Brauner
  Cc: Alexander Viro, Jan Kara, Miklos Szeredi, Chris Mason,
	Christoph Hellwig, Theodore Ts'o, Carlos Maiolino,
	Alexander Mikhalitsyn, linux-fsdevel, linux-unionfs, stable

The xattr system calls refuse a name longer than XATTR_NAME_MAX and a
value larger than XATTR_SIZE_MAX, but the VFS helpers do not. An
in-kernel caller may pass a name or a size that it got from another
filesystem, such as overlayfs copy up from a lower layer.

Check both limits before calling the xattr handler of the filesystem,
and fail with the errors that the system calls return for them.

Cc: stable@vger.kernel.org
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
---

Changes since v1:
- Check the name in vfs_getxattr_alloc() too (Alex)

v1: https://lore.kernel.org/r/20261123113513.1910906-1-amir73il@gmail.com/

 fs/xattr.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/fs/xattr.c b/fs/xattr.c
index 0705801a62cc5..fb5e43d4ff356 100644
--- a/fs/xattr.c
+++ b/fs/xattr.c
@@ -178,6 +178,18 @@ xattr_permission(const struct mnt_idmap *idmap, struct inode *inode,
 	return inode_permission(idmap, inode, mask);
 }
 
+/*
+ * The xattr system calls refuse a name above XATTR_NAME_MAX and a value above
+ * XATTR_SIZE_MAX, but an in-kernel caller may pass a name or a size that it
+ * got from another filesystem, such as the lower layer of overlayfs.
+ */
+static int xattr_check_name(const char *name)
+{
+	if (strnlen(name, XATTR_NAME_MAX + 1) > XATTR_NAME_MAX)
+		return -ERANGE;
+	return 0;
+}
+
 /*
  * Look for any handler that deals with the specified namespace.
  */
@@ -209,10 +221,17 @@ __vfs_setxattr(const struct mnt_idmap *idmap, struct dentry *dentry,
 	       size_t size, int flags)
 {
 	const struct xattr_handler *handler;
+	int error;
 
 	if (is_posix_acl_xattr(name))
 		return -EOPNOTSUPP;
 
+	error = xattr_check_name(name);
+	if (error)
+		return error;
+	if (size > XATTR_SIZE_MAX)
+		return -E2BIG;
+
 	handler = xattr_resolve_name(inode, &name);
 	if (IS_ERR(handler))
 		return PTR_ERR(handler);
@@ -408,6 +427,10 @@ vfs_getxattr_alloc(const struct mnt_idmap *idmap, struct dentry *dentry,
 	if (error)
 		return error;
 
+	error = xattr_check_name(name);
+	if (error)
+		return error;
+
 	handler = xattr_resolve_name(inode, &name);
 	if (IS_ERR(handler))
 		return PTR_ERR(handler);
@@ -416,6 +439,8 @@ vfs_getxattr_alloc(const struct mnt_idmap *idmap, struct dentry *dentry,
 	error = handler->get(handler, dentry, inode, name, NULL, 0);
 	if (error < 0)
 		return error;
+	if (error > XATTR_SIZE_MAX)
+		return -E2BIG;
 
 	if (!value || (error > xattr_size)) {
 		value = krealloc(*xattr_value, error + 1, flags);
@@ -434,16 +459,24 @@ __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
 	       void *value, size_t size)
 {
 	const struct xattr_handler *handler;
+	ssize_t ret;
 
 	if (is_posix_acl_xattr(name))
 		return -EOPNOTSUPP;
 
+	ret = xattr_check_name(name);
+	if (ret)
+		return ret;
+
 	handler = xattr_resolve_name(inode, &name);
 	if (IS_ERR(handler))
 		return PTR_ERR(handler);
 	if (!handler->get)
 		return -EOPNOTSUPP;
-	return handler->get(handler, dentry, inode, name, value, size);
+	ret = handler->get(handler, dentry, inode, name, value, size);
+	if (ret > XATTR_SIZE_MAX)
+		return -E2BIG;
+	return ret;
 }
 EXPORT_SYMBOL(__vfs_getxattr);
 
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v2] xattr: enforce the name and size limits for in-kernel callers
  2026-11-23 16:58 ` [PATCH v2] " Amir Goldstein
@ 2026-11-23 19:09   ` Alexander Mikhalitsyn
  2026-11-24 12:29   ` Carlos Maiolino
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 11+ messages in thread
From: Alexander Mikhalitsyn @ 2026-11-23 19:09 UTC (permalink / raw)
  To: Amir Goldstein, Christian Brauner
  Cc: Alexander Viro, Jan Kara, Miklos Szeredi, Chris Mason,
	Christoph Hellwig, Theodore Ts'o, Carlos Maiolino,
	linux-fsdevel, linux-unionfs, stable

On Mon, 2026-11-23 at 17:58 +0100, Amir Goldstein wrote:
> The xattr system calls refuse a name longer than XATTR_NAME_MAX and a
> value larger than XATTR_SIZE_MAX, but the VFS helpers do not. An
> in-kernel caller may pass a name or a size that it got from another
> filesystem, such as overlayfs copy up from a lower layer.
> 
> Check both limits before calling the xattr handler of the filesystem,
> and fail with the errors that the system calls return for them.
> 
> Cc: stable@vger.kernel.org
> Signed-off-by: Amir Goldstein <amir73il@gmail.com>

Dear Amir,

Please, feel free to add:

Reviewed-by: Alexander Mikhalitsyn <alex@amutable.com>

Kind regards,
Alex

> ---
> 
> Changes since v1:
> - Check the name in vfs_getxattr_alloc() too (Alex)
> 
> v1:
> https://lore.kernel.org/r/20261123113513.1910906-1-amir73il@gmail.com/
> 
>  fs/xattr.c | 35 ++++++++++++++++++++++++++++++++++-
>  1 file changed, 34 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/xattr.c b/fs/xattr.c
> index 0705801a62cc5..fb5e43d4ff356 100644
> --- a/fs/xattr.c
> +++ b/fs/xattr.c
> @@ -178,6 +178,18 @@ xattr_permission(const struct mnt_idmap *idmap,
> struct inode *inode,
>  	return inode_permission(idmap, inode, mask);
>  }
>  
> +/*
> + * The xattr system calls refuse a name above XATTR_NAME_MAX and a
> value above
> + * XATTR_SIZE_MAX, but an in-kernel caller may pass a name or a size
> that it
> + * got from another filesystem, such as the lower layer of
> overlayfs.
> + */
> +static int xattr_check_name(const char *name)
> +{
> +	if (strnlen(name, XATTR_NAME_MAX + 1) > XATTR_NAME_MAX)
> +		return -ERANGE;
> +	return 0;
> +}
> +
>  /*
>   * Look for any handler that deals with the specified namespace.
>   */
> @@ -209,10 +221,17 @@ __vfs_setxattr(const struct mnt_idmap *idmap,
> struct dentry *dentry,
>  	       size_t size, int flags)
>  {
>  	const struct xattr_handler *handler;
> +	int error;
>  
>  	if (is_posix_acl_xattr(name))
>  		return -EOPNOTSUPP;
>  
> +	error = xattr_check_name(name);
> +	if (error)
> +		return error;
> +	if (size > XATTR_SIZE_MAX)
> +		return -E2BIG;
> +
>  	handler = xattr_resolve_name(inode, &name);
>  	if (IS_ERR(handler))
>  		return PTR_ERR(handler);
> @@ -408,6 +427,10 @@ vfs_getxattr_alloc(const struct mnt_idmap
> *idmap, struct dentry *dentry,
>  	if (error)
>  		return error;
>  
> +	error = xattr_check_name(name);
> +	if (error)
> +		return error;
> +
>  	handler = xattr_resolve_name(inode, &name);
>  	if (IS_ERR(handler))
>  		return PTR_ERR(handler);
> @@ -416,6 +439,8 @@ vfs_getxattr_alloc(const struct mnt_idmap *idmap,
> struct dentry *dentry,
>  	error = handler->get(handler, dentry, inode, name, NULL, 0);
>  	if (error < 0)
>  		return error;
> +	if (error > XATTR_SIZE_MAX)
> +		return -E2BIG;
>  
>  	if (!value || (error > xattr_size)) {
>  		value = krealloc(*xattr_value, error + 1, flags);
> @@ -434,16 +459,24 @@ __vfs_getxattr(struct dentry *dentry, struct
> inode *inode, const char *name,
>  	       void *value, size_t size)
>  {
>  	const struct xattr_handler *handler;
> +	ssize_t ret;
>  
>  	if (is_posix_acl_xattr(name))
>  		return -EOPNOTSUPP;
>  
> +	ret = xattr_check_name(name);
> +	if (ret)
> +		return ret;
> +
>  	handler = xattr_resolve_name(inode, &name);
>  	if (IS_ERR(handler))
>  		return PTR_ERR(handler);
>  	if (!handler->get)
>  		return -EOPNOTSUPP;
> -	return handler->get(handler, dentry, inode, name, value,
> size);
> +	ret = handler->get(handler, dentry, inode, name, value,
> size);
> +	if (ret > XATTR_SIZE_MAX)
> +		return -E2BIG;
> +	return ret;
>  }
>  EXPORT_SYMBOL(__vfs_getxattr);
>  

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2] xattr: enforce the name and size limits for in-kernel callers
  2026-11-23 16:58 ` [PATCH v2] " Amir Goldstein
  2026-11-23 19:09   ` Alexander Mikhalitsyn
@ 2026-11-24 12:29   ` Carlos Maiolino
  2026-11-25  5:29     ` Christoph Hellwig
  2026-11-24 17:06   ` Jan Kara
  2026-11-25  5:59   ` [PATCH v3] " Amir Goldstein
  3 siblings, 1 reply; 11+ messages in thread
From: Carlos Maiolino @ 2026-11-24 12:29 UTC (permalink / raw)
  To: Amir Goldstein
  Cc: Christian Brauner, Alexander Viro, Jan Kara, Miklos Szeredi,
	Chris Mason, Christoph Hellwig, Theodore Ts'o,
	Alexander Mikhalitsyn, linux-fsdevel, linux-unionfs, stable

On Mon, Nov 23, 2026 at 05:58:07PM +0100, Amir Goldstein wrote:
> The xattr system calls refuse a name longer than XATTR_NAME_MAX and a
> value larger than XATTR_SIZE_MAX, but the VFS helpers do not. An
> in-kernel caller may pass a name or a size that it got from another
> filesystem, such as overlayfs copy up from a lower layer.
> 
> Check both limits before calling the xattr handler of the filesystem,
> and fail with the errors that the system calls return for them.
> 
> Cc: stable@vger.kernel.org
> Signed-off-by: Amir Goldstein <amir73il@gmail.com>
> ---
> 
> Changes since v1:
> - Check the name in vfs_getxattr_alloc() too (Alex)
> 
> v1: https://lore.kernel.org/r/20261123113513.1910906-1-amir73il@gmail.com/
> 
>  fs/xattr.c | 35 ++++++++++++++++++++++++++++++++++-
>  1 file changed, 34 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/xattr.c b/fs/xattr.c
> index 0705801a62cc5..fb5e43d4ff356 100644
> --- a/fs/xattr.c
> +++ b/fs/xattr.c
> @@ -178,6 +178,18 @@ xattr_permission(const struct mnt_idmap *idmap, struct inode *inode,
>  	return inode_permission(idmap, inode, mask);
>  }
>  
> +/*
> + * The xattr system calls refuse a name above XATTR_NAME_MAX and a value above
> + * XATTR_SIZE_MAX, but an in-kernel caller may pass a name or a size that it
> + * got from another filesystem, such as the lower layer of overlayfs.
> + */
> +static int xattr_check_name(const char *name)
> +{
> +	if (strnlen(name, XATTR_NAME_MAX + 1) > XATTR_NAME_MAX)
> +		return -ERANGE;
> +	return 0;
> +}
> +

I'd use a more specific naming like xattr_check_name_len() to avoid
confusion, but not that I'm passionate about it :)

If we are trying to prevent invalid names from in-kernel callers,
shouldn't this also protect against a zero len?


>  /*
>   * Look for any handler that deals with the specified namespace.
>   */
> @@ -209,10 +221,17 @@ __vfs_setxattr(const struct mnt_idmap *idmap, struct dentry *dentry,
>  	       size_t size, int flags)
>  {
>  	const struct xattr_handler *handler;
> +	int error;
>  
>  	if (is_posix_acl_xattr(name))
>  		return -EOPNOTSUPP;
>  
> +	error = xattr_check_name(name);
> +	if (error)
> +		return error;
> +	if (size > XATTR_SIZE_MAX)
> +		return -E2BIG;
> +
>  	handler = xattr_resolve_name(inode, &name);
>  	if (IS_ERR(handler))
>  		return PTR_ERR(handler);
> @@ -408,6 +427,10 @@ vfs_getxattr_alloc(const struct mnt_idmap *idmap, struct dentry *dentry,
>  	if (error)
>  		return error;
>  
> +	error = xattr_check_name(name);
> +	if (error)
> +		return error;
> +
>  	handler = xattr_resolve_name(inode, &name);
>  	if (IS_ERR(handler))
>  		return PTR_ERR(handler);
> @@ -416,6 +439,8 @@ vfs_getxattr_alloc(const struct mnt_idmap *idmap, struct dentry *dentry,
>  	error = handler->get(handler, dentry, inode, name, NULL, 0);
>  	if (error < 0)
>  		return error;
> +	if (error > XATTR_SIZE_MAX)
> +		return -E2BIG;
>  
>  	if (!value || (error > xattr_size)) {
>  		value = krealloc(*xattr_value, error + 1, flags);
> @@ -434,16 +459,24 @@ __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
>  	       void *value, size_t size)
>  {
>  	const struct xattr_handler *handler;
> +	ssize_t ret;
>  
>  	if (is_posix_acl_xattr(name))
>  		return -EOPNOTSUPP;
>  
> +	ret = xattr_check_name(name);
> +	if (ret)
> +		return ret;
> +
>  	handler = xattr_resolve_name(inode, &name);
>  	if (IS_ERR(handler))
>  		return PTR_ERR(handler);
>  	if (!handler->get)
>  		return -EOPNOTSUPP;
> -	return handler->get(handler, dentry, inode, name, value, size);
> +	ret = handler->get(handler, dentry, inode, name, value, size);
> +	if (ret > XATTR_SIZE_MAX)
> +		return -E2BIG;
> +	return ret;
>  }
>  EXPORT_SYMBOL(__vfs_getxattr);
>  
> -- 
> 2.56.1
> 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2] xattr: enforce the name and size limits for in-kernel callers
  2026-11-23 16:58 ` [PATCH v2] " Amir Goldstein
  2026-11-23 19:09   ` Alexander Mikhalitsyn
  2026-11-24 12:29   ` Carlos Maiolino
@ 2026-11-24 17:06   ` Jan Kara
  2026-11-25  5:59   ` [PATCH v3] " Amir Goldstein
  3 siblings, 0 replies; 11+ messages in thread
From: Jan Kara @ 2026-11-24 17:06 UTC (permalink / raw)
  To: Amir Goldstein
  Cc: Christian Brauner, Alexander Viro, Jan Kara, Miklos Szeredi,
	Chris Mason, Christoph Hellwig, Theodore Ts'o,
	Carlos Maiolino, Alexander Mikhalitsyn, linux-fsdevel,
	linux-unionfs, stable

On Mon 23-11-26 17:58:07, Amir Goldstein wrote:
> The xattr system calls refuse a name longer than XATTR_NAME_MAX and a
> value larger than XATTR_SIZE_MAX, but the VFS helpers do not. An
> in-kernel caller may pass a name or a size that it got from another
> filesystem, such as overlayfs copy up from a lower layer.
> 
> Check both limits before calling the xattr handler of the filesystem,
> and fail with the errors that the system calls return for them.
> 
> Cc: stable@vger.kernel.org
> Signed-off-by: Amir Goldstein <amir73il@gmail.com>

Looks sensible. Feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza

> ---
> 
> Changes since v1:
> - Check the name in vfs_getxattr_alloc() too (Alex)
> 
> v1: https://lore.kernel.org/r/20261123113513.1910906-1-amir73il@gmail.com/
> 
>  fs/xattr.c | 35 ++++++++++++++++++++++++++++++++++-
>  1 file changed, 34 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/xattr.c b/fs/xattr.c
> index 0705801a62cc5..fb5e43d4ff356 100644
> --- a/fs/xattr.c
> +++ b/fs/xattr.c
> @@ -178,6 +178,18 @@ xattr_permission(const struct mnt_idmap *idmap, struct inode *inode,
>  	return inode_permission(idmap, inode, mask);
>  }
>  
> +/*
> + * The xattr system calls refuse a name above XATTR_NAME_MAX and a value above
> + * XATTR_SIZE_MAX, but an in-kernel caller may pass a name or a size that it
> + * got from another filesystem, such as the lower layer of overlayfs.
> + */
> +static int xattr_check_name(const char *name)
> +{
> +	if (strnlen(name, XATTR_NAME_MAX + 1) > XATTR_NAME_MAX)
> +		return -ERANGE;
> +	return 0;
> +}
> +
>  /*
>   * Look for any handler that deals with the specified namespace.
>   */
> @@ -209,10 +221,17 @@ __vfs_setxattr(const struct mnt_idmap *idmap, struct dentry *dentry,
>  	       size_t size, int flags)
>  {
>  	const struct xattr_handler *handler;
> +	int error;
>  
>  	if (is_posix_acl_xattr(name))
>  		return -EOPNOTSUPP;
>  
> +	error = xattr_check_name(name);
> +	if (error)
> +		return error;
> +	if (size > XATTR_SIZE_MAX)
> +		return -E2BIG;
> +
>  	handler = xattr_resolve_name(inode, &name);
>  	if (IS_ERR(handler))
>  		return PTR_ERR(handler);
> @@ -408,6 +427,10 @@ vfs_getxattr_alloc(const struct mnt_idmap *idmap, struct dentry *dentry,
>  	if (error)
>  		return error;
>  
> +	error = xattr_check_name(name);
> +	if (error)
> +		return error;
> +
>  	handler = xattr_resolve_name(inode, &name);
>  	if (IS_ERR(handler))
>  		return PTR_ERR(handler);
> @@ -416,6 +439,8 @@ vfs_getxattr_alloc(const struct mnt_idmap *idmap, struct dentry *dentry,
>  	error = handler->get(handler, dentry, inode, name, NULL, 0);
>  	if (error < 0)
>  		return error;
> +	if (error > XATTR_SIZE_MAX)
> +		return -E2BIG;
>  
>  	if (!value || (error > xattr_size)) {
>  		value = krealloc(*xattr_value, error + 1, flags);
> @@ -434,16 +459,24 @@ __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
>  	       void *value, size_t size)
>  {
>  	const struct xattr_handler *handler;
> +	ssize_t ret;
>  
>  	if (is_posix_acl_xattr(name))
>  		return -EOPNOTSUPP;
>  
> +	ret = xattr_check_name(name);
> +	if (ret)
> +		return ret;
> +
>  	handler = xattr_resolve_name(inode, &name);
>  	if (IS_ERR(handler))
>  		return PTR_ERR(handler);
>  	if (!handler->get)
>  		return -EOPNOTSUPP;
> -	return handler->get(handler, dentry, inode, name, value, size);
> +	ret = handler->get(handler, dentry, inode, name, value, size);
> +	if (ret > XATTR_SIZE_MAX)
> +		return -E2BIG;
> +	return ret;
>  }
>  EXPORT_SYMBOL(__vfs_getxattr);
>  
> -- 
> 2.56.1
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2] xattr: enforce the name and size limits for in-kernel callers
  2026-11-24 12:29   ` Carlos Maiolino
@ 2026-11-25  5:29     ` Christoph Hellwig
  0 siblings, 0 replies; 11+ messages in thread
From: Christoph Hellwig @ 2026-11-25  5:29 UTC (permalink / raw)
  To: Carlos Maiolino
  Cc: Amir Goldstein, Christian Brauner, Alexander Viro, Jan Kara,
	Miklos Szeredi, Chris Mason, Christoph Hellwig, Theodore Ts'o,
	Alexander Mikhalitsyn, linux-fsdevel, linux-unionfs, stable

On Tue, Nov 24, 2026 at 01:29:41PM +0100, Carlos Maiolino wrote:
> I'd use a more specific naming like xattr_check_name_len() to avoid
> confusion, but not that I'm passionate about it :)

Sounds reasonable.

> If we are trying to prevent invalid names from in-kernel callers,
> shouldn't this also protect against a zero len?

And this sounds valid too.

Otherwise the patch looks good to me.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v3] xattr: enforce the name and size limits for in-kernel callers
  2026-11-23 16:58 ` [PATCH v2] " Amir Goldstein
                     ` (2 preceding siblings ...)
  2026-11-24 17:06   ` Jan Kara
@ 2026-11-25  5:59   ` Amir Goldstein
  2026-11-25  6:06     ` Christoph Hellwig
                       ` (2 more replies)
  3 siblings, 3 replies; 11+ messages in thread
From: Amir Goldstein @ 2026-11-25  5:59 UTC (permalink / raw)
  To: Christian Brauner
  Cc: Alexander Viro, Jan Kara, Miklos Szeredi, Chris Mason,
	Christoph Hellwig, Theodore Ts'o, Carlos Maiolino,
	linux-fsdevel, linux-unionfs, stable, Alexander Mikhalitsyn

The xattr system calls refuse an empty name, a name longer than
XATTR_NAME_MAX and a value larger than XATTR_SIZE_MAX, but the VFS
helpers do not. An in-kernel caller may pass a name or a size that it
got from another filesystem, such as overlayfs copy up from a lower
layer.

Check these limits before calling the xattr handler of the filesystem,
and fail with the errors that the system calls return for them.

Cc: stable@vger.kernel.org
Reviewed-by: Alexander Mikhalitsyn <alex@amutable.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
---

Changes since v2:
- Rename the helper to xattr_check_name_len() (Carlos)
- Also refuse an empty name (Carlos)
- Add Reviewed-by tags

Changes since v1:
- Check the name in vfs_getxattr_alloc() too (Alex)

v2: https://lore.kernel.org/r/20261123165807.1939942-1-amir73il@gmail.com/
v1: https://lore.kernel.org/r/20261123113513.1910906-1-amir73il@gmail.com/

 fs/xattr.c | 38 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/fs/xattr.c b/fs/xattr.c
index 0705801a62cc5..64df10ed498c3 100644
--- a/fs/xattr.c
+++ b/fs/xattr.c
@@ -178,6 +178,21 @@ xattr_permission(const struct mnt_idmap *idmap, struct inode *inode,
 	return inode_permission(idmap, inode, mask);
 }
 
+/*
+ * The xattr system calls refuse an empty name, a name above XATTR_NAME_MAX
+ * and a value above XATTR_SIZE_MAX, but an in-kernel caller may pass a name or
+ * a size that it got from another filesystem, such as the lower layer of
+ * overlayfs.
+ */
+static int xattr_check_name_len(const char *name)
+{
+	size_t len = strnlen(name, XATTR_NAME_MAX + 1);
+
+	if (!len || len > XATTR_NAME_MAX)
+		return -ERANGE;
+	return 0;
+}
+
 /*
  * Look for any handler that deals with the specified namespace.
  */
@@ -209,10 +224,17 @@ __vfs_setxattr(const struct mnt_idmap *idmap, struct dentry *dentry,
 	       size_t size, int flags)
 {
 	const struct xattr_handler *handler;
+	int error;
 
 	if (is_posix_acl_xattr(name))
 		return -EOPNOTSUPP;
 
+	error = xattr_check_name_len(name);
+	if (error)
+		return error;
+	if (size > XATTR_SIZE_MAX)
+		return -E2BIG;
+
 	handler = xattr_resolve_name(inode, &name);
 	if (IS_ERR(handler))
 		return PTR_ERR(handler);
@@ -408,6 +430,10 @@ vfs_getxattr_alloc(const struct mnt_idmap *idmap, struct dentry *dentry,
 	if (error)
 		return error;
 
+	error = xattr_check_name_len(name);
+	if (error)
+		return error;
+
 	handler = xattr_resolve_name(inode, &name);
 	if (IS_ERR(handler))
 		return PTR_ERR(handler);
@@ -416,6 +442,8 @@ vfs_getxattr_alloc(const struct mnt_idmap *idmap, struct dentry *dentry,
 	error = handler->get(handler, dentry, inode, name, NULL, 0);
 	if (error < 0)
 		return error;
+	if (error > XATTR_SIZE_MAX)
+		return -E2BIG;
 
 	if (!value || (error > xattr_size)) {
 		value = krealloc(*xattr_value, error + 1, flags);
@@ -434,16 +462,24 @@ __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
 	       void *value, size_t size)
 {
 	const struct xattr_handler *handler;
+	ssize_t ret;
 
 	if (is_posix_acl_xattr(name))
 		return -EOPNOTSUPP;
 
+	ret = xattr_check_name_len(name);
+	if (ret)
+		return ret;
+
 	handler = xattr_resolve_name(inode, &name);
 	if (IS_ERR(handler))
 		return PTR_ERR(handler);
 	if (!handler->get)
 		return -EOPNOTSUPP;
-	return handler->get(handler, dentry, inode, name, value, size);
+	ret = handler->get(handler, dentry, inode, name, value, size);
+	if (ret > XATTR_SIZE_MAX)
+		return -E2BIG;
+	return ret;
 }
 EXPORT_SYMBOL(__vfs_getxattr);
 
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v3] xattr: enforce the name and size limits for in-kernel callers
  2026-11-25  5:59   ` [PATCH v3] " Amir Goldstein
@ 2026-11-25  6:06     ` Christoph Hellwig
  2026-11-25  8:35     ` Carlos Maiolino
  2026-12-01 10:37     ` Christian Brauner
  2 siblings, 0 replies; 11+ messages in thread
From: Christoph Hellwig @ 2026-11-25  6:06 UTC (permalink / raw)
  To: Amir Goldstein
  Cc: Christian Brauner, Alexander Viro, Jan Kara, Miklos Szeredi,
	Chris Mason, Christoph Hellwig, Theodore Ts'o,
	Carlos Maiolino, linux-fsdevel, linux-unionfs, stable,
	Alexander Mikhalitsyn

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v3] xattr: enforce the name and size limits for in-kernel callers
  2026-11-25  5:59   ` [PATCH v3] " Amir Goldstein
  2026-11-25  6:06     ` Christoph Hellwig
@ 2026-11-25  8:35     ` Carlos Maiolino
  2026-12-01 10:37     ` Christian Brauner
  2 siblings, 0 replies; 11+ messages in thread
From: Carlos Maiolino @ 2026-11-25  8:35 UTC (permalink / raw)
  To: Amir Goldstein
  Cc: Christian Brauner, Alexander Viro, Jan Kara, Miklos Szeredi,
	Chris Mason, Christoph Hellwig, Theodore Ts'o, linux-fsdevel,
	linux-unionfs, stable, Alexander Mikhalitsyn

On Wed, Nov 25, 2026 at 06:59:00AM +0100, Amir Goldstein wrote:
> The xattr system calls refuse an empty name, a name longer than
> XATTR_NAME_MAX and a value larger than XATTR_SIZE_MAX, but the VFS
> helpers do not. An in-kernel caller may pass a name or a size that it
> got from another filesystem, such as overlayfs copy up from a lower
> layer.
> 
> Check these limits before calling the xattr handler of the filesystem,
> and fail with the errors that the system calls return for them.
> 
> Cc: stable@vger.kernel.org
> Reviewed-by: Alexander Mikhalitsyn <alex@amutable.com>
> Reviewed-by: Jan Kara <jack@suse.cz>
> Signed-off-by: Amir Goldstein <amir73il@gmail.com>
> ---

Looks good to me:

Reviewed-by: Carlos Maiolino <cmaiolino@redhat.com>

> 
> Changes since v2:
> - Rename the helper to xattr_check_name_len() (Carlos)
> - Also refuse an empty name (Carlos)
> - Add Reviewed-by tags
> 
> Changes since v1:
> - Check the name in vfs_getxattr_alloc() too (Alex)
> 
> v2: https://lore.kernel.org/r/20261123165807.1939942-1-amir73il@gmail.com/
> v1: https://lore.kernel.org/r/20261123113513.1910906-1-amir73il@gmail.com/
> 
>  fs/xattr.c | 38 +++++++++++++++++++++++++++++++++++++-
>  1 file changed, 37 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/xattr.c b/fs/xattr.c
> index 0705801a62cc5..64df10ed498c3 100644
> --- a/fs/xattr.c
> +++ b/fs/xattr.c
> @@ -178,6 +178,21 @@ xattr_permission(const struct mnt_idmap *idmap, struct inode *inode,
>  	return inode_permission(idmap, inode, mask);
>  }
>  
> +/*
> + * The xattr system calls refuse an empty name, a name above XATTR_NAME_MAX
> + * and a value above XATTR_SIZE_MAX, but an in-kernel caller may pass a name or
> + * a size that it got from another filesystem, such as the lower layer of
> + * overlayfs.
> + */
> +static int xattr_check_name_len(const char *name)
> +{
> +	size_t len = strnlen(name, XATTR_NAME_MAX + 1);
> +
> +	if (!len || len > XATTR_NAME_MAX)
> +		return -ERANGE;
> +	return 0;
> +}
> +
>  /*
>   * Look for any handler that deals with the specified namespace.
>   */
> @@ -209,10 +224,17 @@ __vfs_setxattr(const struct mnt_idmap *idmap, struct dentry *dentry,
>  	       size_t size, int flags)
>  {
>  	const struct xattr_handler *handler;
> +	int error;
>  
>  	if (is_posix_acl_xattr(name))
>  		return -EOPNOTSUPP;
>  
> +	error = xattr_check_name_len(name);
> +	if (error)
> +		return error;
> +	if (size > XATTR_SIZE_MAX)
> +		return -E2BIG;
> +
>  	handler = xattr_resolve_name(inode, &name);
>  	if (IS_ERR(handler))
>  		return PTR_ERR(handler);
> @@ -408,6 +430,10 @@ vfs_getxattr_alloc(const struct mnt_idmap *idmap, struct dentry *dentry,
>  	if (error)
>  		return error;
>  
> +	error = xattr_check_name_len(name);
> +	if (error)
> +		return error;
> +
>  	handler = xattr_resolve_name(inode, &name);
>  	if (IS_ERR(handler))
>  		return PTR_ERR(handler);
> @@ -416,6 +442,8 @@ vfs_getxattr_alloc(const struct mnt_idmap *idmap, struct dentry *dentry,
>  	error = handler->get(handler, dentry, inode, name, NULL, 0);
>  	if (error < 0)
>  		return error;
> +	if (error > XATTR_SIZE_MAX)
> +		return -E2BIG;
>  
>  	if (!value || (error > xattr_size)) {
>  		value = krealloc(*xattr_value, error + 1, flags);
> @@ -434,16 +462,24 @@ __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
>  	       void *value, size_t size)
>  {
>  	const struct xattr_handler *handler;
> +	ssize_t ret;
>  
>  	if (is_posix_acl_xattr(name))
>  		return -EOPNOTSUPP;
>  
> +	ret = xattr_check_name_len(name);
> +	if (ret)
> +		return ret;
> +
>  	handler = xattr_resolve_name(inode, &name);
>  	if (IS_ERR(handler))
>  		return PTR_ERR(handler);
>  	if (!handler->get)
>  		return -EOPNOTSUPP;
> -	return handler->get(handler, dentry, inode, name, value, size);
> +	ret = handler->get(handler, dentry, inode, name, value, size);
> +	if (ret > XATTR_SIZE_MAX)
> +		return -E2BIG;
> +	return ret;
>  }
>  EXPORT_SYMBOL(__vfs_getxattr);
>  
> -- 
> 2.56.1
> 
> 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v3] xattr: enforce the name and size limits for in-kernel callers
  2026-11-25  5:59   ` [PATCH v3] " Amir Goldstein
  2026-11-25  6:06     ` Christoph Hellwig
  2026-11-25  8:35     ` Carlos Maiolino
@ 2026-12-01 10:37     ` Christian Brauner
  2 siblings, 0 replies; 11+ messages in thread
From: Christian Brauner @ 2026-12-01 10:37 UTC (permalink / raw)
  To: Amir Goldstein
  Cc: Alexander Viro, Jan Kara, Miklos Szeredi, Chris Mason,
	Christoph Hellwig, Theodore Ts'o, Carlos Maiolino,
	linux-fsdevel, linux-unionfs, stable, Alexander Mikhalitsyn

On Wed, 25 Nov 2026 06:59:00 +0100, Amir Goldstein wrote:
> The xattr system calls refuse an empty name, a name longer than
> XATTR_NAME_MAX and a value larger than XATTR_SIZE_MAX, but the VFS
> helpers do not. An in-kernel caller may pass a name or a size that it
> got from another filesystem, such as overlayfs copy up from a lower
> layer.
> 
> Check these limits before calling the xattr handler of the filesystem,
> and fail with the errors that the system calls return for them.
> 
> [...]

Applied to the vfs-7.5.misc branch of the vfs/vfs.git tree.
Patches in the vfs-7.5.misc branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a
new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the
patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase,
trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-7.5.misc

[1/1] xattr: enforce the name and size limits for in-kernel callers
      https://git.kernel.org/vfs/vfs/c/7fc0ca625a79


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-12-01 10:37 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 11:35 [PATCH] xattr: enforce the name and size limits for in-kernel callers Amir Goldstein
2026-11-23 14:08 ` Alexander Mikhalitsyn
2026-11-23 16:58 ` [PATCH v2] " Amir Goldstein
2026-11-23 19:09   ` Alexander Mikhalitsyn
2026-11-24 12:29   ` Carlos Maiolino
2026-11-25  5:29     ` Christoph Hellwig
2026-11-24 17:06   ` Jan Kara
2026-11-25  5:59   ` [PATCH v3] " Amir Goldstein
2026-11-25  6:06     ` Christoph Hellwig
2026-11-25  8:35     ` Carlos Maiolino
2026-12-01 10:37     ` Christian Brauner

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.