Skip to contentSkip to conversation

xattr: Additional maximum size check

PATCH In review Andreas Gruenbacher 2 participants 3 messages Feb 23, 2017 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xattr: Additional maximum size check

When querying for the buffer size required, a filesystem's getxattr xattr handler or listxattr iop may return a value bigger than the maximum size limit. However, the VFS will never return oversize buffers, so cast such values to -E2BIG.

---
 fs/xattr.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
fs/xattr.c +12 -8
index 7e3317c..c19a163 100644
@@ -537,16 +537,18 @@ getxattr(struct dentry *d, const char __user *name, void __user *value,
 	}
 
 	error = vfs_getxattr(d, kname, kvalue, size);
+	if (error > XATTR_SIZE_MAX ||
+	    (error == -ERANGE && size >= XATTR_SIZE_MAX)) {
+		/* The file system tried to returned a value bigger
+		   than XATTR_SIZE_MAX bytes. Not possible. */
+		error = -E2BIG;
+	}
 	if (error > 0) {
 		if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
 		    (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
 			posix_acl_fix_xattr_to_user(kvalue, size);
 		if (size && copy_to_user(value, kvalue, error))
 			error = -EFAULT;
-	} else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
-		/* The file system tried to returned a value bigger
-		   than XATTR_SIZE_MAX bytes. Not possible. */
-		error = -E2BIG;
 	}
 
 	kvfree(kvalue);
@@ -620,14 +622,16 @@ listxattr(struct dentry *d, char __user *list, size_t size)
 	}
 
 	error = vfs_listxattr(d, klist, size);
-	if (error > 0) {
-		if (size && copy_to_user(list, klist, error))
-			error = -EFAULT;
-	} else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
+	if (error > XATTR_LIST_MAX ||
+	    (error == -ERANGE && size >= XATTR_LIST_MAX)) {
 		/* The file system tried to returned a list bigger
 		   than XATTR_LIST_MAX bytes. Not possible. */
 		error = -E2BIG;
 	}
+	if (error > 0) {
+		if (size && copy_to_user(list, klist, error))
+			error = -EFAULT;
+	}
 
 	kvfree(klist);
 
Signature · 1 line
2.7.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Andreas Gruenbacher

When querying for the buffer size required, a filesystem's getxattr xattr handler or listxattr iop may return a value bigger than the maximum size limit. However, the VFS will never return oversize buffers, so cast such values to -E2BIG.

---
 fs/xattr.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
fs/xattr.c +0 -0
index 7e3317c..c19a163 100644

fs/xattr.c getxattr(struct dentry *d, const char __user *name, void __user *value, · line 539

Quoted 3 lines
fs/xattr.c +0 -0
@@ -537,16 +537,18 @@ getxattr(struct dentry *d, const char __user *name, void __user *value,
 	}
 
 	error = vfs_getxattr(d, kname, kvalue, size);

We have this just above:

	if (size) {
		if (size > XATTR_SIZE_MAX)
			size = XATTR_SIZE_MAX;

So this test:

fs/xattr.c getxattr(struct dentry *d, const char __user *name, void __user *value, · line 545

Quoted 6 lines
fs/xattr.c +6 -0
+	if (error > XATTR_SIZE_MAX ||
+	    (error == -ERANGE && size >= XATTR_SIZE_MAX)) {
+		/* The file system tried to returned a value bigger
+		   than XATTR_SIZE_MAX bytes. Not possible. */
+		error = -E2BIG;
+	}

is equivalent to:

	if (error > XATTR_SIZE_MAX ||
	    (error = -ERANGE && size == XATTR_SIZE_MAX))

That's kind of a subtle case. I guess the idea is that ERANGE means "the xattr value doesn't fit into the buffer you gave me", and EF2BIG means "the xattr value couldn't fit in any buffer you could possible give me". And if the filesystem isn't satisfied even with a maximum-sized buffer then clearly we're in the second case. OK, got it. Still, this is a little subtle. I don't see EF2BIG in my copy of getxattr(2).

Anyway, feel free to add

but I wouldn't say no to more documentation.

--b.

Quoted 37 lines
fs/xattr.c +6 -8
 	if (error > 0) {
 		if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
 		    (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
 			posix_acl_fix_xattr_to_user(kvalue, size);
 		if (size && copy_to_user(value, kvalue, error))
 			error = -EFAULT;
-	} else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
-		/* The file system tried to returned a value bigger
-		   than XATTR_SIZE_MAX bytes. Not possible. */
-		error = -E2BIG;
 	}
 
 	kvfree(kvalue);
@@ -620,14 +622,16 @@ listxattr(struct dentry *d, char __user *list, size_t size)
 	}
 
 	error = vfs_listxattr(d, klist, size);
-	if (error > 0) {
-		if (size && copy_to_user(list, klist, error))
-			error = -EFAULT;
-	} else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
+	if (error > XATTR_LIST_MAX ||
+	    (error == -ERANGE && size >= XATTR_LIST_MAX)) {
 		/* The file system tried to returned a list bigger
 		   than XATTR_LIST_MAX bytes. Not possible. */
 		error = -E2BIG;
 	}
+	if (error > 0) {
+		if (size && copy_to_user(list, klist, error))
+			error = -EFAULT;
+	}
 
 	kvfree(klist);
 
Signature · 1 line
2.7.4
#2 lore raw
Quoted 35 lines · J. Bruce Fields
Quoted 15 lines · Andreas Gruenbacher

When querying for the buffer size required, a filesystem's getxattr xattr handler or listxattr iop may return a value bigger than the maximum size limit. However, the VFS will never return oversize buffers, so cast such values to -E2BIG.

---
 fs/xattr.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
fs/xattr.c +0 -0
index 7e3317c..c19a163 100644

fs/xattr.c getxattr(struct dentry *d, const char __user *name, void __user *value, · line 539

Quoted 3 lines
fs/xattr.c +0 -0
@@ -537,16 +537,18 @@ getxattr(struct dentry *d, const char __user *name, void __user *value,
      }
 
      error = vfs_getxattr(d, kname, kvalue, size);

We have this just above:

        if (size) {
                if (size > XATTR_SIZE_MAX)
                        size = XATTR_SIZE_MAX;

So this test:

fs/xattr.c getxattr(struct dentry *d, const char __user *name, void __user *value, · line 545

Quoted 6 lines
fs/xattr.c +6 -0
+     if (error > XATTR_SIZE_MAX ||
+         (error == -ERANGE && size >= XATTR_SIZE_MAX)) {
+             /* The file system tried to returned a value bigger
+                than XATTR_SIZE_MAX bytes. Not possible. */
+             error = -E2BIG;
+     }
Quoted 6 lines

is equivalent to:

        if (error > XATTR_SIZE_MAX ||
            (error = -ERANGE && size == XATTR_SIZE_MAX))

That's kind of a subtle case.

True.

Quoted 7 lines

I guess the idea is that ERANGE means
"the xattr value doesn't fit into the buffer you gave me", and E2BIG means "the xattr value couldn't fit in any buffer you could possible give me". And if the filesystem isn't satisfied even with a maximum-sized buffer then clearly we're in the second case. OK, got it. Still, this is a little subtle. I don't see EF2BIG in my copy of getxattr(2).

It's not in any of my copies of getxattr(2) either yet, but it will be soon :)

Quoted 5 lines

Anyway, feel free to add

but I wouldn't say no to more documentation.

Thanks,
Andreas