Skip to contentSkip to conversation

selftests/landlock: Fix snprintf truncation checks in test files

PATCH In review Wang Yan 2 participants 3 messages Jun 26 – Aug 26

Subsystems (3)
lore
Share & tools
lore

Wang Yan

author
#0 lore raw

PATCH 1/1 selftests/landlock: Fix snprintf truncation checks in test files

Commit b566f7a4f0e4 ("selftests/landlock: Fix snprintf truncation checks in audit helpers") fixed the truncation detection in audit.h by changing the comparison from ">" to ">=" to correctly handle the edge case where snprintf returns a value equal to the buffer size.

However, the same pattern exists in ptrace_test.c, audit_test.c, and net_test.c and was not fixed. snprintf() returns the number of characters that would have been written, excluding the terminating NUL byte. When the output is truncated, this return value equals or exceeds the buffer size. The existing ">" check therefore fails to detect truncation when the return value equals the buffer size.

Fix these remaining instances to use ">=" for truncation detection, matching the fix in audit.h.

Fixes 6a500b22971c ("selftests/landlock: Add tests for audit flags and domain IDs")
---
 tools/testing/selftests/landlock/audit_test.c  | 2 +-
 tools/testing/selftests/landlock/net_test.c    | 4 ++--
 tools/testing/selftests/landlock/ptrace_test.c | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/landlock/audit_test.c +1 -1
index 72b5612375dd..4bea8c880a4d 100644
@@ -31,7 +31,7 @@ static int matches_log_signal(struct __test_metadata *const _metadata,
 
 	log_match_len =
 		snprintf(log_match, sizeof(log_match), log_template, opid);
-	if (log_match_len > sizeof(log_match))
+	if (log_match_len >= sizeof(log_match))
 		return -E2BIG;
 
 	return audit_match_record(audit_fd, AUDIT_LANDLOCK_ACCESS, log_match,
tools/testing/selftests/landlock/net_test.c +2 -2
index 2ed1f76b7a8b..aebeafd80466 100644
@@ -2777,7 +2777,7 @@ static int matches_auditlog(const int audit_fd, const char *const blockers,
 		log_match_len = snprintf(log_match, sizeof(log_match),
 					 log_with_addrport_tmpl, blockers,
 					 dir_addr, addr, dir_port, port);
-	if (log_match_len > sizeof(log_match))
+	if (log_match_len >= sizeof(log_match))
 		return -E2BIG;
 
 	return audit_match_record(audit_fd, AUDIT_LANDLOCK_ACCESS, log_match,
@@ -3072,7 +3072,7 @@ static int matches_log_connect_bound(int audit_fd, const char *const blockers,
 
 	log_match_len = snprintf(log_match, sizeof(log_match), log_template,
 				 blockers, addr, lport, addr, dport);
-	if (log_match_len > sizeof(log_match))
+	if (log_match_len >= sizeof(log_match))
 		return -E2BIG;
 
 	return audit_match_record(audit_fd, AUDIT_LANDLOCK_ACCESS, log_match,
tools/testing/selftests/landlock/ptrace_test.c +1 -1
index 4f64c90583cd..65cf2d82f721 100644
@@ -302,7 +302,7 @@ static int matches_log_ptrace(struct __test_metadata *const _metadata,
 
 	log_match_len =
 		snprintf(log_match, sizeof(log_match), log_template, opid);
-	if (log_match_len > sizeof(log_match))
+	if (log_match_len >= sizeof(log_match))
 		return -E2BIG;
 
 	return audit_match_record(audit_fd, AUDIT_LANDLOCK_ACCESS, log_match,
Signature · 1 line
2.25.1
author
#1 lore raw

Hi maintainers and reviewers,

I hope this email finds you well.

I submitted this patch on June 26, 2026, and it has not received any review comments yet. I understand you are very busy, but I would greatly appreciate it if you could take a look when you have a moment.

The patch fixes the remaining snprintf truncation checks in selftests/landlock test files (audit_test.c, net_test.c, ptrace_test.c) to match the already-merged fix in audit.h. It is a small and straightforward change.

Please let me know if any adjustments are needed. I'm happy to update the patch based on your feedback.

Thank you for your time and work!

Best regards,
Wang Yan

reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Wang Yan
Quoted 75 lines · Wang Yan

Commit b566f7a4f0e4 ("selftests/landlock: Fix snprintf truncation checks in audit helpers") fixed the truncation detection in audit.h by changing the comparison from ">" to ">=" to correctly handle the edge case where snprintf returns a value equal to the buffer size.

However, the same pattern exists in ptrace_test.c, audit_test.c, and net_test.c and was not fixed. snprintf() returns the number of characters that would have been written, excluding the terminating NUL byte. When the output is truncated, this return value equals or exceeds the buffer size. The existing ">" check therefore fails to detect truncation when the return value equals the buffer size.

Fix these remaining instances to use ">=" for truncation detection, matching the fix in audit.h.

Fixes 6a500b22971c ("selftests/landlock: Add tests for audit flags and domain IDs")
---
 tools/testing/selftests/landlock/audit_test.c  | 2 +-
 tools/testing/selftests/landlock/net_test.c    | 4 ++--
 tools/testing/selftests/landlock/ptrace_test.c | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/landlock/audit_test.c +1 -1
index 72b5612375dd..4bea8c880a4d 100644
@@ -31,7 +31,7 @@ static int matches_log_signal(struct __test_metadata *const _metadata,
 
 	log_match_len =
 		snprintf(log_match, sizeof(log_match), log_template, opid);
-	if (log_match_len > sizeof(log_match))
+	if (log_match_len >= sizeof(log_match))
 		return -E2BIG;
 
 	return audit_match_record(audit_fd, AUDIT_LANDLOCK_ACCESS, log_match,
tools/testing/selftests/landlock/net_test.c +2 -2
index 2ed1f76b7a8b..aebeafd80466 100644
@@ -2777,7 +2777,7 @@ static int matches_auditlog(const int audit_fd, const char *const blockers,
 		log_match_len = snprintf(log_match, sizeof(log_match),
 					 log_with_addrport_tmpl, blockers,
 					 dir_addr, addr, dir_port, port);
-	if (log_match_len > sizeof(log_match))
+	if (log_match_len >= sizeof(log_match))
 		return -E2BIG;
 
 	return audit_match_record(audit_fd, AUDIT_LANDLOCK_ACCESS, log_match,
@@ -3072,7 +3072,7 @@ static int matches_log_connect_bound(int audit_fd, const char *const blockers,
 
 	log_match_len = snprintf(log_match, sizeof(log_match), log_template,
 				 blockers, addr, lport, addr, dport);
-	if (log_match_len > sizeof(log_match))
+	if (log_match_len >= sizeof(log_match))
 		return -E2BIG;
 
 	return audit_match_record(audit_fd, AUDIT_LANDLOCK_ACCESS, log_match,
tools/testing/selftests/landlock/ptrace_test.c +1 -1
index 4f64c90583cd..65cf2d82f721 100644
@@ -302,7 +302,7 @@ static int matches_log_ptrace(struct __test_metadata *const _metadata,
 
 	log_match_len =
 		snprintf(log_match, sizeof(log_match), log_template, opid);
-	if (log_match_len > sizeof(log_match))
+	if (log_match_len >= sizeof(log_match))
 		return -E2BIG;
 
 	return audit_match_record(audit_fd, AUDIT_LANDLOCK_ACCESS, log_match,
Signature · 1 line
2.25.1

Thanks for the fix and the reminder! The fix is correct, because snprintf() returns the number of bytes that would have been written *excluding* the trailing NUL byte, whereas the buffer size passed to it *includes* the space for the trailing NUL byte.

—Günther