Skip to contentSkip to conversation

f2fs-tools: improve xattr value printing

PATCH In review Eric Biggers 3 participants 4 messages Apr 24, 2019 – May 20, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 f2fs-tools: improve xattr value printing

From: Eric Biggers <[email protected]>

- Print the values of xattrs that have an unknown ->e_name_index, rather than ignoring them.

- Replace char with u8. Otherwise xattr values containing bytes >= 0x80 are printed incorrectly on platforms where char is signed.

- Only parse the encryption xattr if it has a known format number and size. Otherwise print it as hex.

- Remove incorrect le16_to_cpu() on ->e_name_len which has type u8.

- Consolidate the code that prints the xattr value as hex.

- Constify pointers to the xattr entry.

---
 fsck/mount.c | 56 ++++++++++++++++++++++------------------------------
 fsck/xattr.h |  1 +
 2 files changed, 25 insertions(+), 32 deletions(-)
fsck/mount.c +24 -32
index 843742e..c69226d 100644
@@ -55,11 +55,11 @@ void update_free_segments(struct f2fs_sb_info *sbi)
 }
 
 #if defined(HAVE_LINUX_POSIX_ACL_H) || defined(HAVE_SYS_ACL_H)
-void print_acl(char *value, int size)
+static void print_acl(const u8 *value, int size)
 {
-	struct f2fs_acl_header *hdr = (struct f2fs_acl_header *)value;
-	struct f2fs_acl_entry *entry = (struct f2fs_acl_entry *)(hdr + 1);
-	const char *end = value + size;
+	const struct f2fs_acl_header *hdr = (struct f2fs_acl_header *)value;
+	const struct f2fs_acl_entry *entry = (struct f2fs_acl_entry *)(hdr + 1);
+	const u8 *end = value + size;
 	int i, count;
 
 	if (hdr->a_version != cpu_to_le32(F2FS_ACL_VERSION)) {
@@ -75,7 +75,7 @@ void print_acl(char *value, int size)
 	}
 
 	for (i = 0; i < count; i++) {
-		if ((char *)entry > end) {
+		if ((u8 *)entry > end) {
 			MSG(0, "Invalid ACL entries count %d\n", count);
 			return;
 		}
@@ -114,42 +114,33 @@ void print_acl(char *value, int size)
 		}
 	}
 }
-#else
-#define print_acl(value, size) do {		\
-	int i;					\
-	for (i = 0; i < size; i++)		\
-		MSG(0, "%02X", value[i]);	\
-	MSG(0, "\n");				\
-} while (0)
-#endif
+#endif /* HAVE_LINUX_POSIX_ACL_H || HAVE_SYS_ACL_H */
 
-void print_xattr_entry(struct f2fs_xattr_entry *ent)
+static void print_xattr_entry(const struct f2fs_xattr_entry *ent)
 {
-	char *value = (char *)(ent->e_name + le16_to_cpu(ent->e_name_len));
-	struct fscrypt_context *ctx;
+	const u8 *value = (const u8 *)&ent->e_name[ent->e_name_len];
+	const int size = le16_to_cpu(ent->e_value_size);
+	const struct fscrypt_context *ctx;
 	int i;
 
 	MSG(0, "\nxattr: e_name_index:%d e_name:", ent->e_name_index);
-	for (i = 0; i < le16_to_cpu(ent->e_name_len); i++)
+	for (i = 0; i < ent->e_name_len; i++)
 		MSG(0, "%c", ent->e_name[i]);
 	MSG(0, " e_name_len:%d e_value_size:%d e_value:\n",
-			ent->e_name_len, le16_to_cpu(ent->e_value_size));
+			ent->e_name_len, size);
 
 	switch (ent->e_name_index) {
+#if defined(HAVE_LINUX_POSIX_ACL_H) || defined(HAVE_SYS_ACL_H)
 	case F2FS_XATTR_INDEX_POSIX_ACL_ACCESS:
 	case F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT:
-		print_acl(value, le16_to_cpu(ent->e_value_size));
-		break;
-	case F2FS_XATTR_INDEX_USER:
-	case F2FS_XATTR_INDEX_SECURITY:
-	case F2FS_XATTR_INDEX_TRUSTED:
-	case F2FS_XATTR_INDEX_LUSTRE:
-		for (i = 0; i < le16_to_cpu(ent->e_value_size); i++)
-			MSG(0, "%02X", value[i]);
-		MSG(0, "\n");
-		break;
+		print_acl(value, size);
+		return;
+#endif
 	case F2FS_XATTR_INDEX_ENCRYPTION:
-		ctx = (struct fscrypt_context *)value;
+		ctx = (const struct fscrypt_context *)value;
+		if (size != sizeof(*ctx) ||
+		    ctx->format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
+			break;
 		MSG(0, "format: %d\n", ctx->format);
 		MSG(0, "contents_encryption_mode: 0x%x\n", ctx->contents_encryption_mode);
 		MSG(0, "filenames_encryption_mode: 0x%x\n", ctx->filenames_encryption_mode);
@@ -161,10 +152,11 @@ void print_xattr_entry(struct f2fs_xattr_entry *ent)
 		for (i = 0; i < FS_KEY_DERIVATION_NONCE_SIZE; i++)
 			MSG(0, "%02X", ctx->nonce[i]);
 		MSG(0, "\n");
-		break;
-	default:
-		break;
+		return;
 	}
+	for (i = 0; i < size; i++)
+		MSG(0, "%02X", value[i]);
+	MSG(0, "\n");
 }
 
 void print_inode_info(struct f2fs_sb_info *sbi,
fsck/xattr.h +1 -0
index dc80faf..579ab6c 100644
@@ -34,6 +34,7 @@ struct f2fs_xattr_entry {
 	char e_name[0];		/* attribute name */
 };
 
+#define FS_ENCRYPTION_CONTEXT_FORMAT_V1 1
 #define FS_KEY_DESCRIPTOR_SIZE 8
 #define FS_KEY_DERIVATION_NONCE_SIZE 16
 
Signature · 1 line
2.21.0.593.g511ec345e18-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Eric Biggers

From: Eric Biggers <[email protected]>

- Print the values of xattrs that have an unknown ->e_name_index, rather than ignoring them.

- Replace char with u8. Otherwise xattr values containing bytes >= 0x80 are printed incorrectly on platforms where char is signed.

- Only parse the encryption xattr if it has a known format number and size. Otherwise print it as hex.

Quoted 7 lines

- Remove incorrect le16_to_cpu() on ->e_name_len which has type u8.

- Consolidate the code that prints the xattr value as hex.

- Constify pointers to the xattr entry.

Thanks,

#2 lore raw
Quoted 16 lines · Chao Yu
Quoted 15 lines · Eric Biggers

From: Eric Biggers <[email protected]>

- Print the values of xattrs that have an unknown ->e_name_index, rather than ignoring them.

- Replace char with u8. Otherwise xattr values containing bytes >= 0x80 are printed incorrectly on platforms where char is signed.

- Only parse the encryption xattr if it has a known format number and size. Otherwise print it as hex.

- Remove incorrect le16_to_cpu() on ->e_name_len which has type u8.

- Consolidate the code that prints the xattr value as hex.

Quoted 8 lines
Quoted 3 lines

- Constify pointers to the xattr entry.

Thanks,

Hi Jaegeuk, are you planning to apply this patch?

Thanks,

- Eric

#3 lore raw
Quoted 19 lines · Eric Biggers
Quoted 18 lines · Chao Yu
Quoted 17 lines · Eric Biggers

From: Eric Biggers <[email protected]>

- Print the values of xattrs that have an unknown ->e_name_index, rather than ignoring them.

- Replace char with u8. Otherwise xattr values containing bytes >= 0x80 are printed incorrectly on platforms where char is signed.

- Only parse the encryption xattr if it has a known format number and size. Otherwise print it as hex.

- Remove incorrect le16_to_cpu() on ->e_name_len which has type u8.

- Consolidate the code that prints the xattr value as hex.

- Constify pointers to the xattr entry.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Thanks,

Hi Jaegeuk, are you planning to apply this patch?

Yup, it's in dev branch. Just too busy to add them into master. Thank you for the heads up. Will do soon.

Quoted 4 lines

Thanks,

- Eric