Skip to contentSkip to conversation

fs: fix xattr permission checking error

PATCH In review Nicolas Belouin 3 participants 4 messages Oct 21, 2017 – Oct 22, 2017

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

PATCH 1/1 fs: fix xattr permission checking error

Fix an issue making trusted xattr world readable and other cap_sys_admin only

---
 fs/hfsplus/xattr.c | 2 +-
 fs/jfs/xattr.c     | 5 ++---
 2 files changed, 3 insertions(+), 4 deletions(-)
fs/hfsplus/xattr.c +1 -1
index d37bb88dc746..ae03a19196ef 100644
@@ -604,7 +604,7 @@ static inline int can_list(const char *xattr_name)
 	if (!xattr_name)
 		return 0;
 
-	return strncmp(xattr_name, XATTR_TRUSTED_PREFIX,
+	return !strncmp(xattr_name, XATTR_TRUSTED_PREFIX,
 			XATTR_TRUSTED_PREFIX_LEN) ||
 				capable(CAP_SYS_ADMIN);
 }
fs/jfs/xattr.c +2 -3
index c60f3d32ee91..1c46573a96ed 100644
@@ -858,9 +858,8 @@ ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
  */
 static inline int can_list(struct jfs_ea *ea)
 {
-	return (strncmp(ea->name, XATTR_TRUSTED_PREFIX,
-			    XATTR_TRUSTED_PREFIX_LEN) ||
-		capable(CAP_SYS_ADMIN));
+	return (!strncmp(ea->name, XATTR_TRUSTED_PREFIX,
+			 XATTR_TRUSTED_PREFIX_LEN) || capable(CAP_SYS_ADMIN));
 }
 
 ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size)
Signature · 1 line
2.14.2
#1 lore raw
Quoted 15 lines · Nicolas Belouin

Fix an issue making trusted xattr world readable and other cap_sys_admin only

---
fs/hfsplus/xattr.c | 2 +-
fs/jfs/xattr.c | 5 ++---

2 files changed, 3 insertions(+), 4 deletions(-)
fs/hfsplus/xattr.c +0 -0
index d37bb88dc746..ae03a19196ef 100644

fs/hfsplus/xattr.c static inline int can_list(const char *xattr_name) · line 609

Quoted 7 lines
fs/hfsplus/xattr.c +1 -1
@@ -604,7 +604,7 @@ static inline int can_list(const char *xattr_name)
 	if (!xattr_name)
 		return 0;
 
-	return strncmp(xattr_name, XATTR_TRUSTED_PREFIX,
+	return !strncmp(xattr_name, XATTR_TRUSTED_PREFIX,
 			XATTR_TRUSTED_PREFIX_LEN) ||
 				capable(CAP_SYS_ADMIN);

I don't think this is correct. This means "you can list the xattr if it IS 'trusted.*', OR if you have sysadmin privilege", so non-trusted xattrs could not be listed by regular users.

As can be seen by this defect, the use of "strncmp()" with an explicit boolean return code is confusing and subject to errors, in particular "strncmp()" returning 0 (false) means the strings MATCH. My preference is to explicitly check "strncmp() == 0" for the match, as this is more clear to the reader that strncmp() has a non-standard return convention.

To my reading, the original logic is correct, which is "you can list the xattr if it is not 'trusted.*' OR if you have sysadmin privilege", but it could be improved like:

	return strncmp(xattr_name, XATTR_TRUSTED_PREFIX,
		       XATTR_TRUSTED_PREFIX_LEN) != 0 ||
		capable(CAP_SYS_ADMIN);
Quoted 5 lines
fs/jfs/xattr.c +0 -0
index c60f3d32ee91..1c46573a96ed 100644

fs/jfs/xattr.c ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data, · line 863

Quoted 9 lines
fs/jfs/xattr.c +2 -3
@@ -858,9 +858,8 @@ ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
 */
 static inline int can_list(struct jfs_ea *ea)
 {
-	return (strncmp(ea->name, XATTR_TRUSTED_PREFIX,
-			    XATTR_TRUSTED_PREFIX_LEN) ||
-		capable(CAP_SYS_ADMIN));
+	return (!strncmp(ea->name, XATTR_TRUSTED_PREFIX,
+			 XATTR_TRUSTED_PREFIX_LEN) || capable(CAP_SYS_ADMIN));
 }

I think the original code is also correct here, and your patch is adding a bug.

Cheers, Andreas

#2 lore raw

Indeed, I was mistaken here.
Thanks for the review.
I will do the "strncmp() ==0" in another patch as it is particularly unclear in this context.

Quoted 65 lines · Andreas Dilger

fs/hfsplus/xattr.c static inline int can_list(const char

Quoted 15 lines · Nicolas Belouin

Fix an issue making trusted xattr world readable and other cap_sys_admin only

---
fs/hfsplus/xattr.c | 2 +-
fs/jfs/xattr.c | 5 ++---

2 files changed, 3 insertions(+), 4 deletions(-)
fs/hfsplus/xattr.c +0 -0
index d37bb88dc746..ae03a19196ef 100644
@@ -604,7 +604,7 @@ static inline int can_list(const char

*xattr_name)

fs/hfsplus/xattr.c static inline int can_list(const char *xattr_name) · line 609

Quoted 7 lines
fs/hfsplus/xattr.c +1 -1
 	if (!xattr_name)
 		return 0;
 
-	return strncmp(xattr_name, XATTR_TRUSTED_PREFIX,
+	return !strncmp(xattr_name, XATTR_TRUSTED_PREFIX,
 			XATTR_TRUSTED_PREFIX_LEN) ||
 				capable(CAP_SYS_ADMIN);

I don't think this is correct. This means "you can list the xattr if it IS 'trusted.*', OR if you have sysadmin privilege", so non-trusted xattrs could not be listed by regular users.

As can be seen by this defect, the use of "strncmp()" with an explicit boolean return code is confusing and subject to errors, in particular "strncmp()" returning 0 (false) means the strings MATCH. My preference is to explicitly check "strncmp() == 0" for the match, as this is more clear to the reader that strncmp() has a non-standard return convention.

To my reading, the original logic is correct, which is "you can list the xattr if it is not 'trusted.*' OR if you have sysadmin privilege", but it could be improved like:

	return strncmp(xattr_name, XATTR_TRUSTED_PREFIX,
		       XATTR_TRUSTED_PREFIX_LEN) != 0 ||
		capable(CAP_SYS_ADMIN);

fs/jfs/xattr.c ssize_t __jfs_getxattr(struct inode *inode, const

Quoted 5 lines
fs/jfs/xattr.c +0 -0
index c60f3d32ee91..1c46573a96ed 100644
@@ -858,9 +858,8 @@ ssize_t __jfs_getxattr(struct inode *inode, const

char *name, void *data,

fs/jfs/xattr.c ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data, · line 863

Quoted 9 lines
fs/jfs/xattr.c +2 -3
 */
 static inline int can_list(struct jfs_ea *ea)
 {
-	return (strncmp(ea->name, XATTR_TRUSTED_PREFIX,
-			    XATTR_TRUSTED_PREFIX_LEN) ||
-		capable(CAP_SYS_ADMIN));
+	return (!strncmp(ea->name, XATTR_TRUSTED_PREFIX,
+			 XATTR_TRUSTED_PREFIX_LEN) || capable(CAP_SYS_ADMIN));
 }

I think the original code is also correct here, and your patch is adding
a bug.

Quoted 1 line

Cheers, Andreas

Nicolas

#3 lore raw
In reply to Nicolas Belouin
Quoted 3 lines · Nicolas Belouin

Fix an issue making trusted xattr world readable and other cap_sys_admin only

NACK. It is *documented* that trusted xattrs are only supposed to be readable by root.

- Ted