Skip to contentSkip to conversation

xattr: enforce the name and size limits for in-kernel callers

PATCH Superseded Amir Goldstein 2 participants 4 messages Nov 23

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xattr: enforce the name and size limits for in-kernel callers

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.

---
 fs/xattr.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
fs/xattr.c +30 -1
index 0705801a62cc5..824e8349e8c10 100644
@@ -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);
 
Signature · 1 line
2.56.1
#1 lore raw
Quoted 51 lines · Amir Goldstein

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.

---
 fs/xattr.c | 31 ++++++++++++++++++++++++++++++-

 1 file changed, 30 insertions(+), 1 deletion(-)
fs/xattr.c +0 -0
index 0705801a62cc5..824e8349e8c10 100644
@@ -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.
+ */

fs/xattr.c +7 -0
+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. */

fs/xattr.c +1 -0
@@ -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;

fs/xattr.c __vfs_setxattr(const struct mnt_idmap *idmap, struct dentry *dentry, · line 233

Quoted 5 lines
fs/xattr.c +5 -0
+	error = xattr_check_name(name);
+	if (error)
+		return error;
+	if (size > XATTR_SIZE_MAX)
+		return -E2BIG;

Hi Amir,

fs/xattr.c vfs_getxattr_alloc(const struct mnt_idmap *idmap, · line 435

Quoted 6 lines

+
  handler = xattr_resolve_name(inode, &name); if (IS_ERR(handler))
  return PTR_ERR(handler);

fs/xattr.c +0 -0
@@ -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

Quoted 37 lines
fs/xattr.c +2 -0
  	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);

fs/xattr.c +1 -0
@@ -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;

fs/xattr.c +4 -0
+	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;

fs/xattr.c +0 -1
-	return handler->get(handler, dentry, inode, name, value,

size);

fs/xattr.c +1 -0
+	ret = handler->get(handler, dentry, inode, name, value,

size);

fs/xattr.c +3 -0
+	if (ret > XATTR_SIZE_MAX)
+		return -E2BIG;
+	return ret;

 }
 EXPORT_SYMBOL(__vfs_getxattr);

#2 lore raw

PATCH v2 1/1 xattr: enforce the name and size limits for in-kernel callers

In reply to Amir Goldstein

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.

---

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

v1: https://lore.kernel.org/r/[email protected]/

 fs/xattr.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)
fs/xattr.c +34 -1
index 0705801a62cc5..fb5e43d4ff356 100644
@@ -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);
 
Signature · 1 line
2.56.1
Reviewed-by on v2 1/1
#3 lore raw
Quoted 10 lines · Amir Goldstein

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.

Dear Amir,

Please, feel free to add:

Kind regards,
Alex

Quoted 108 lines

---

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

v1:
https://lore.kernel.org/r/[email protected]/

 fs/xattr.c | 35 ++++++++++++++++++++++++++++++++++-

 1 file changed, 34 insertions(+), 1 deletion(-)
fs/xattr.c +0 -0
index 0705801a62cc5..fb5e43d4ff356 100644
@@ -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.
+ */

fs/xattr.c +7 -0
+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. */

fs/xattr.c +1 -0
@@ -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;

fs/xattr.c +6 -0
+	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);

fs/xattr.c +0 -0
@@ -408,6 +427,10 @@ vfs_getxattr_alloc(const struct mnt_idmap
 *idmap, struct dentry *dentry,
  	if (error)
  		return error;

fs/xattr.c +4 -0
+	error = xattr_check_name(name);
+	if (error)
+		return error;
+

  handler = xattr_resolve_name(inode, &name); if (IS_ERR(handler))
  return PTR_ERR(handler);

fs/xattr.c +2 -0
@@ -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);

fs/xattr.c +1 -0
@@ -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;

fs/xattr.c +4 -0
+	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;

fs/xattr.c +0 -1
-	return handler->get(handler, dentry, inode, name, value,

size);

fs/xattr.c +1 -0
+	ret = handler->get(handler, dentry, inode, name, value,

size);

fs/xattr.c +3 -0
+	if (ret > XATTR_SIZE_MAX)
+		return -E2BIG;
+	return ret;

 }
 EXPORT_SYMBOL(__vfs_getxattr);