The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: "Günther Noack" <gnoack@google.com>
To: Wang Yan <wangyan01@kylinos.cn>
Cc: mic@digikod.net, shuah@kernel.org,
	linux-security-module@vger.kernel.org,
	linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
	Haofeng Li <lihaofeng@kylinos.cn>
Subject: Re: [PATCH] selftests/landlock: Fix snprintf truncation checks in test files
Date: Wed, 26 Aug 2026 12:44:14 +0200	[thread overview]
Message-ID: <ao7DflLdx7-lFyvk@google.com> (raw)
In-Reply-To: <20260626070016.61049-1-wangyan01@kylinos.cn>

On Fri, Jun 26, 2026 at 03:00:16PM +0800, Wang Yan wrote:
> 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")
> Signed-off-by: Haofeng Li <lihaofeng@kylinos.cn>
> Signed-off-by: Wang Yan <wangyan01@kylinos.cn>
> ---
>  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(-)
> 
> diff --git a/tools/testing/selftests/landlock/audit_test.c b/tools/testing/selftests/landlock/audit_test.c
> index 72b5612375dd..4bea8c880a4d 100644
> --- a/tools/testing/selftests/landlock/audit_test.c
> +++ b/tools/testing/selftests/landlock/audit_test.c
> @@ -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,
> diff --git a/tools/testing/selftests/landlock/net_test.c b/tools/testing/selftests/landlock/net_test.c
> index 2ed1f76b7a8b..aebeafd80466 100644
> --- a/tools/testing/selftests/landlock/net_test.c
> +++ b/tools/testing/selftests/landlock/net_test.c
> @@ -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,
> diff --git a/tools/testing/selftests/landlock/ptrace_test.c b/tools/testing/selftests/landlock/ptrace_test.c
> index 4f64c90583cd..65cf2d82f721 100644
> --- a/tools/testing/selftests/landlock/ptrace_test.c
> +++ b/tools/testing/selftests/landlock/ptrace_test.c
> @@ -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,
> -- 
> 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.

Reviewed-by: Günther Noack <gnoack@google.com>

—Günther

      parent reply	other threads:[~2026-08-26 10:44 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-26  7:00 [PATCH] selftests/landlock: Fix snprintf truncation checks in test files Wang Yan
2026-08-24  6:40 ` Wang Yan
2026-08-26 10:44 ` Günther Noack [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ao7DflLdx7-lFyvk@google.com \
    --to=gnoack@google.com \
    --cc=lihaofeng@kylinos.cn \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=mic@digikod.net \
    --cc=shuah@kernel.org \
    --cc=wangyan01@kylinos.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox