Skip to contentSkip to conversation

tools: usb: ffs-test: read the descriptor counts with unaligned accesses

PATCH Superseded Sasha Levin 3 participants 6 messages Oct 11 – Oct 13 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools: usb: ffs-test: read the descriptor counts with unaligned accesses

descs_to_legacy() reads the descriptor counts through a __le32 pointer taken from a member of a packed struct, which may be unaligned:

ffs-test.c:235:40: warning: taking address of packed member of 'struct <anonymous>' may result in an unaligned pointer value [-Waddress-of-packed-member]

Walk the counts with a byte pointer and get_unaligned_le32().

Fixes b9a4274699c69 ("tools: ffs-test: add compatibility code for old kernels")
Assisted-by LLM
---
 tools/usb/ffs-test.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/usb/ffs-test.c +4 -3
index 22b938fbdfb77..f08060459b3f1 100644
@@ -232,7 +232,8 @@ static size_t descs_to_legacy(void **legacy, const void *descriptors_v2)
 			const struct usb_functionfs_descs_head_v2 header;
 			const __le32 counts[];
 		} __attribute__((packed)) *const in = descriptors_v2;
-		const __le32 *counts = in->counts;
+		const unsigned char *counts =
+			(const unsigned char *)descriptors_v2 + sizeof(in->header);
 		__u32 flags;
 
 		if (le32_to_cpu(in->header.magic) !=
@@ -252,9 +253,9 @@ static size_t descs_to_legacy(void **legacy, const void *descriptors_v2)
 				break;			\
 			if (length < 4)			\
 				return 0;		\
-			ret = le32_to_cpu(*counts);	\
+			ret = get_unaligned_le32(counts); \
 			length -= 4;			\
-			++counts;			\
+			counts += 4;			\
 		} while (0)
 
 		GET_NEXT_COUNT_IF_FLAG(fs_count, FUNCTIONFS_HAS_FS_DESC);
Signature · 1 line
2.53.0
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

descs_to_legacy() reads the descriptor counts through a __le32 pointer taken from a member of a packed struct, which may be unaligned:

ffs-test.c:235:40: warning: taking address of packed member of 'struct <anonymous>' may result in an unaligned pointer value [-Waddress-of-packed-member]

Walk the counts with a byte pointer and get_unaligned_le32().

Fixes b9a4274699c69 ("tools: ffs-test: add compatibility code for old kernels")
Assisted-by LLM

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Sasha Levin
Quoted 19 lines · Sasha Levin

descs_to_legacy() reads the descriptor counts through a __le32 pointer taken from a member of a packed struct, which may be unaligned:

ffs-test.c:235:40: warning: taking address of packed member of 'struct <anonymous>' may result in an unaligned pointer value [-Waddress-of-packed-member]

Walk the counts with a byte pointer and get_unaligned_le32().

Fixes b9a4274699c69 ("tools: ffs-test: add compatibility code for old kernels")
Assisted-by LLM
---
 tools/usb/ffs-test.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/usb/ffs-test.c +0 -0
index 22b938fbdfb77..f08060459b3f1 100644

tools/usb/ffs-test.c static size_t descs_to_legacy(void **legacy, const void *descriptors_v2) · line 236

Quoted 6 lines
tools/usb/ffs-test.c +2 -1
@@ -232,7 +232,8 @@ static size_t descs_to_legacy(void **legacy, const void *descriptors_v2)
 			const struct usb_functionfs_descs_head_v2 header;
 			const __le32 counts[];
 		} __attribute__((packed)) *const in = descriptors_v2;
-		const __le32 *counts = in->counts;
+		const unsigned char *counts =
+			(const unsigned char *)descriptors_v2 + sizeof(in->header);

Why not:

+ const unsigned char *counts = (const void*)in->counts;

Or maybe change the structure to const unsigned char counts[4][] and the variable const unsigned char (*counts)[4]?

Quoted 15 lines
tools/usb/ffs-test.c +2 -2
 		__u32 flags;
 
 		if (le32_to_cpu(in->header.magic) !=
@@ -252,9 +253,9 @@ static size_t descs_to_legacy(void **legacy, const void *descriptors_v2)
 				break;			\
 			if (length < 4)			\
 				return 0;		\
-			ret = le32_to_cpu(*counts);	\
+			ret = get_unaligned_le32(counts); \
 			length -= 4;			\
-			++counts;			\
+			counts += 4;			\
 		} while (0)
 
 		GET_NEXT_COUNT_IF_FLAG(fs_count, FUNCTIONFS_HAS_FS_DESC);
Signature · 3 lines
Best regards
ミハウ “𝓶𝓲𝓷𝓪86” ナザレヴィツ
«If at first you don’t succeed, give up skydiving»
#3 lore raw

PATCH v2 1/1 tools: usb: ffs-test: read the descriptor counts with unaligned accesses

descs_to_legacy() reads the descriptor counts through a __le32 pointer taken from a member of a packed struct, which may be unaligned:

ffs-test.c:235:40: warning: taking address of packed member of 'struct <anonymous>' may result in an unaligned pointer value [-Waddress-of-packed-member]

Walk the counts with a byte pointer and get_unaligned_le32().

Fixes b9a4274699c69 ("tools: ffs-test: add compatibility code for old kernels")
Assisted-by LLM

---
Changes in v2:
- Take the byte pointer from in->counts instead of computing it from descriptors_v2 (Michał Nazarewicz)
- Link to v1: https://lore.kernel.org/r/[email protected]

 tools/usb/ffs-test.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/usb/ffs-test.c +3 -3
index 22b938fbdfb77..4453d810e9412 100644
@@ -232,7 +232,7 @@ static size_t descs_to_legacy(void **legacy, const void *descriptors_v2)
 			const struct usb_functionfs_descs_head_v2 header;
 			const __le32 counts[];
 		} __attribute__((packed)) *const in = descriptors_v2;
-		const __le32 *counts = in->counts;
+		const unsigned char *counts = (const void *)in->counts;
 		__u32 flags;
 
 		if (le32_to_cpu(in->header.magic) !=
@@ -252,9 +252,9 @@ static size_t descs_to_legacy(void **legacy, const void *descriptors_v2)
 				break;			\
 			if (length < 4)			\
 				return 0;		\
-			ret = le32_to_cpu(*counts);	\
+			ret = get_unaligned_le32(counts); \
 			length -= 4;			\
-			++counts;			\
+			counts += 4;			\
 		} while (0)
 
 		GET_NEXT_COUNT_IF_FLAG(fs_count, FUNCTIONFS_HAS_FS_DESC);
Signature · 1 line
2.53.0
#4 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

descs_to_legacy() reads the descriptor counts through a __le32 pointer taken from a member of a packed struct, which may be unaligned:

ffs-test.c:235:40: warning: taking address of packed member of 'struct <anonymous>' may result in an unaligned pointer value [-Waddress-of-packed-member]

Walk the counts with a byte pointer and get_unaligned_le32().

Fixes b9a4274699c69 ("tools: ffs-test: add compatibility code for old kernels")
Assisted-by LLM

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Acked-by on v2 1/1
#5 lore raw
In reply to Sasha Levin
Quoted 10 lines · Sasha Levin

descs_to_legacy() reads the descriptor counts through a __le32 pointer taken from a member of a packed struct, which may be unaligned:

ffs-test.c:235:40: warning: taking address of packed member of 'struct <anonymous>' may result in an unaligned pointer value [-Waddress-of-packed-member]

Walk the counts with a byte pointer and get_unaligned_le32().

Fixes b9a4274699c69 ("tools: ffs-test: add compatibility code for old kernels")
Assisted-by LLM
Quoted 34 lines

---
Changes in v2:
- Take the byte pointer from in->counts instead of computing it from descriptors_v2 (Michał Nazarewicz)
- Link to v1: https://lore.kernel.org/r/[email protected]

 tools/usb/ffs-test.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/usb/ffs-test.c +3 -3
index 22b938fbdfb77..4453d810e9412 100644
@@ -232,7 +232,7 @@ static size_t descs_to_legacy(void **legacy, const void *descriptors_v2)
 			const struct usb_functionfs_descs_head_v2 header;
 			const __le32 counts[];
 		} __attribute__((packed)) *const in = descriptors_v2;
-		const __le32 *counts = in->counts;
+		const unsigned char *counts = (const void *)in->counts;
 		__u32 flags;
 
 		if (le32_to_cpu(in->header.magic) !=
@@ -252,9 +252,9 @@ static size_t descs_to_legacy(void **legacy, const void *descriptors_v2)
 				break;			\
 			if (length < 4)			\
 				return 0;		\
-			ret = le32_to_cpu(*counts);	\
+			ret = get_unaligned_le32(counts); \
 			length -= 4;			\
-			++counts;			\
+			counts += 4;			\
 		} while (0)
 
 		GET_NEXT_COUNT_IF_FLAG(fs_count, FUNCTIONFS_HAS_FS_DESC);
Signature · 3 lines
Best regards
ミハウ “𝓶𝓲𝓷𝓪86” ナザレヴィツ
«If at first you don’t succeed, give up skydiving»