Skip to contentSkip to conversation

fortify: Use __struct_size replace __member_size

PATCH In review [email protected] 2 participants 2 messages Dec 4, 2024 FORTIFY_SOURCE

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fortify: Use __struct_size replace __member_size

From: lihaojie <[email protected]>

Use __struct_size get destination size.

Size of destination less of the size to be written will make buffer overflow, the size of destination should be complete.

---
 include/linux/fortify-string.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/fortify-string.h +1 -1
index 0d99bf11d260..0504b2c8aab7 100644
@@ -277,7 +277,7 @@ extern ssize_t __real_strscpy(char *, const char *, size_t) __RENAME(sized_strsc
 __FORTIFY_INLINE ssize_t sized_strscpy(char * const POS p, const char * const POS q, size_t size)
 {
 	/* Use string size rather than possible enclosing struct size. */
-	const size_t p_size = __member_size(p);
+	const size_t p_size = __struct_size(p);
 	const size_t q_size = __member_size(q);
 	size_t len;
 
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 8 lines · On December 4, 2024 4:41:49 PM GMT+10:00, [email protected] wrote:

From: lihaojie <[email protected]>

Use __struct_size get destination size.

Size of destination less of the size to be written will make buffer overflow, the size of destination should be complete.

I cannot understand what you mean here. Have you encountered a problem where a destination size is incorrectly calculated?

Quoted 10 lines

---
include/linux/fortify-string.h | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/fortify-string.h +0 -0
index 0d99bf11d260..0504b2c8aab7 100644

include/linux/fortify-string.h extern ssize_t __real_strscpy(char *, const char *, size_t) __RENAME(sized_strsc · line 280

Quoted 5 lines
include/linux/fortify-string.h +1 -1
@@ -277,7 +277,7 @@ extern ssize_t __real_strscpy(char *, const char *, size_t) __RENAME(sized_strsc
 __FORTIFY_INLINE ssize_t sized_strscpy(char * const POS p, const char * const POS q, size_t size)
 {
 	/* Use string size rather than possible enclosing struct size. */
-	const size_t p_size = __member_size(p);
+	const size_t p_size = __struct_size(p);

No, this must not be done. (See the comment immediately above it.) We must limit the maximum write size to the length of the target buffe, not the enclosing structure that contains it.

-Kees

Signature · 1 line
Kees Cook