Skip to contentSkip to conversation

maccess: fix strncpy_from_user_nofault empty string handling

PATCH Applied Mykyta Yatsenko 4 participants 6 messages Apr 22, 2025 – Apr 23, 2025

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

PATCH v2 1/1 maccess: fix strncpy_from_user_nofault empty string handling

From: Mykyta Yatsenko <[email protected]>

strncpy_from_user_nofault should return the length of the copied string including the trailing NUL, but if the argument unsafe_addr points to an empty string ({'\0'}), the return value is 0.

This happens as strncpy_from_user copies terminal symbol into dst and returns 0 (as expected), but strncpy_from_user_nofault does not modify ret as it is not equal to count and not greater than 0, so 0 is returned, which contradicts the contract.

---
 mm/maccess.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/maccess.c +1 -1
index 8f0906180a94..831b4dd7296c 100644
@@ -196,7 +196,7 @@ long strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
 	if (ret >= count) {
 		ret = count;
 		dst[ret - 1] = '\0';
-	} else if (ret > 0) {
+	} else if (ret >= 0) {
 		ret++;
 	}
 
Signature · 1 line
2.49.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 12 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

strncpy_from_user_nofault should return the length of the copied string including the trailing NUL, but if the argument unsafe_addr points to an empty string ({'\0'}), the return value is 0.

This happens as strncpy_from_user copies terminal symbol into dst and returns 0 (as expected), but strncpy_from_user_nofault does not modify ret as it is not equal to count and not greater than 0, so 0 is returned, which contradicts the contract.

Quoted 5 lines
---
 mm/maccess.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 16 lines
mm/maccess.c +1 -2
index 8f0906180a94..831b4dd7296c 100644
@@ -196,7 +196,7 @@ long strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
        if (ret >= count) {
                ret = count;
                dst[ret - 1] = '\0';
-       } else if (ret > 0) {
+       } else if (ret >= 0) {
                ret++;
        }
 
--

2.49.0

maintainer
#2 lore raw
In reply to Mykyta Yatsenko
Quoted 6 lines · On Tue, 22 Apr 2025 14:14:49 +0100 Mykyta Yatsenko…

From: Mykyta Yatsenko <[email protected]>

strncpy_from_user_nofault should return the length of the copied string including the trailing NUL, but if the argument unsafe_addr points to an empty string ({'\0'}), the return value is 0.

Quoted 7 lines

This happens as strncpy_from_user copies terminal symbol into dst and returns 0 (as expected), but strncpy_from_user_nofault does not modify ret as it is not equal to count and not greater than 0, so 0 is returned, which contradicts the contract.

...

Thanks.

Does this fix any known runtime issue? If so, please fully describe this?

Quoted 11 lines
mm/maccess.c +1 -1
@@ -196,7 +196,7 @@ long strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
 	if (ret >= count) {
 		ret = count;
 		dst[ret - 1] = '\0';
-	} else if (ret > 0) {
+	} else if (ret >= 0) {
 		ret++;
 	}
 
#3 lore raw
Quoted 13 lines · Andrew Morton
Quoted 11 lines · On Tue, 22 Apr 2025 14:14:49 +0100 Mykyta Yatsenko…

From: Mykyta Yatsenko <[email protected]>

strncpy_from_user_nofault should return the length of the copied string including the trailing NUL, but if the argument unsafe_addr points to an empty string ({'\0'}), the return value is 0.

This happens as strncpy_from_user copies terminal symbol into dst and returns 0 (as expected), but strncpy_from_user_nofault does not modify ret as it is not equal to count and not greater than 0, so 0 is returned, which contradicts the contract.

Quoted 5 lines
Quoted 2 lines

...

Thanks.

Does this fix any known runtime issue? If so, please fully describe this?

Not that I'm aware of. The issue could be found when trying to copy empty user space string in BPF program (and relying on return value).There are some usage of strncpy_from_user_nofault in tracing subsystem, but I'm not sure how to hit those code paths.

Quoted 12 lines
Quoted 12 lines
mm/maccess.c +1 -1
@@ -196,7 +196,7 @@ long strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
  	if (ret >= count) {
  		ret = count;
  		dst[ret - 1] = '\0';
-	} else if (ret > 0) {
+	} else if (ret >= 0) {
  		ret++;
  	}
 
#4 lore raw
Quoted 7 lines · On Wed, 23 Apr 2025 12:37:46 +0100 Mykyta Yatsenko…
Quoted 1 line

Does this fix any known runtime issue? If so, please fully describe this?

Not that I'm aware of. The issue could be found when trying to copy empty user space string in BPF program (and relying on return value).There are some usage of
strncpy_from_user_nofault in tracing subsystem, but I'm not sure how to hit those code paths.

Quoted 1 line

Although your patch found a bug in the tracing subsystem, this wasn't the cause. It only cared if the read faulted or not. It was incorrectly checking for zero as non fault when in reality, it needed to check >= 0.

With that fixed, it should work the same with or without this patch.

-- Steve

#5 lore raw
Quoted 10 lines · Steven Rostedt
Quoted 7 lines · On Wed, 23 Apr 2025 12:37:46 +0100 Mykyta Yatsenko…
Quoted 1 line

Does this fix any known runtime issue? If so, please fully describe this?

Not that I'm aware of. The issue could be found when trying to copy empty user space string in BPF program (and relying on return value).There are some usage of
strncpy_from_user_nofault in tracing subsystem, but I'm not sure how to hit those code paths.

Quoted 1 line
Quoted 7 lines

Although your patch found a bug in the tracing subsystem, this wasn't the cause. It only cared if the read faulted or not. It was incorrectly checking for zero as non fault when in reality, it needed to check >= 0.

With that fixed, it should work the same with or without this patch.

-- Steve

Sure, I had in mind usages from trace_probe_kernel.h,
namely fetch_store_string_user, fetch_store_string, having a second look, it appears these only used in trace_events_synth.c, and we are good there.