Linux Security Modules development
 help / color / mirror / Atom feed
* [PATCH] selftests/landlock: fix the regex_escape() error check
@ 2026-11-03 16:39 Sasha Levin
  2026-11-03 16:43 ` sashiko-bot
  2026-11-03 20:31 ` Günther Noack
  0 siblings, 2 replies; 4+ messages in thread
From: Sasha Levin @ 2026-11-03 16:39 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: Günther Noack, Shuah Khan, linux-security-module,
	linux-kselftest, linux-kernel

regex_escape() returns (char *)-ENOMEM when the buffer is too small, but
matches_log_fs_extra() checks the returned pointer with "< 0", which is
never true, and goes on with a bogus cursor. On 32-bit architectures gcc
also warns about the cast of the pointer in the error return:

  fs_test.c:7474:24: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]

Compare against (char *)-ENOMEM instead.

The size left after the escaped path also subtracts the prefix a second
time, which can make a long path fail with -E2BIG. Compute it from the
cursor instead.

Fixes: 316d06b01130 ("selftests/landlock: Add audit tests for filesystem")
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
Found with 32-bit builds of the selftests (gcc 16 and clang 21 with
-m32, with and without -D_TIME_BITS=64 -D_FILE_OFFSET_BITS=64);
KernelCI's 32-bit arm builds show the same warnings. The changed files
build without them now, and the x86_64 builds don't change.

 tools/testing/selftests/landlock/fs_test.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/landlock/fs_test.c b/tools/testing/selftests/landlock/fs_test.c
index f725882fa4a78..27a78434d1c21 100644
--- a/tools/testing/selftests/landlock/fs_test.c
+++ b/tools/testing/selftests/landlock/fs_test.c
@@ -7470,10 +7470,10 @@ static int matches_log_fs_extra(struct __test_metadata *const _metadata,
 	log_match_cursor = regex_escape(absolute_path, log_match_cursor,
 					log_match_remaining);
 	free(absolute_path);
-	if (log_match_cursor < 0)
-		return (long long)log_match_cursor;
+	if (log_match_cursor == (char *)-ENOMEM)
+		return -ENOMEM;
 
-	log_match_remaining -= log_match_cursor - log_match;
+	log_match_remaining = sizeof(log_match) - (log_match_cursor - log_match);
 	chunk_len = snprintf(log_match_cursor, log_match_remaining,
 			     "\" dev=\"[^\"]\\+\" ino=[0-9]\\+%s$",
 			     extra ?: "");
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] selftests/landlock: fix the regex_escape() error check
  2026-11-03 16:39 [PATCH] selftests/landlock: fix the regex_escape() error check Sasha Levin
@ 2026-11-03 16:43 ` sashiko-bot
  2026-11-03 20:31 ` Günther Noack
  1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-03 16:43 UTC (permalink / raw)
  Cc: linux-security-module

> regex_escape() returns (char *)-ENOMEM when the buffer is too small, but
> matches_log_fs_extra() checks the returned pointer with "< 0", which is
> never true, and goes on with a bogus cursor. On 32-bit architectures gcc
> also warns about the cast of the pointer in the error return:
> 
>   fs_test.c:7474:24: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
> 
> Compare against (char *)-ENOMEM instead.
> 
> The size left after the escaped path also subtracts the prefix a second
> time, which can make a long path fail with -E2BIG. Compute it from the
> cursor instead.
> 
> Fixes: 316d06b01130 ("selftests/landlock: Add audit tests for filesystem")
> Signed-off-by: Sasha Levin <sashal@kernel.org>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261103163917.641130-1-sashal@kernel.org?part=1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] selftests/landlock: fix the regex_escape() error check
  2026-11-03 16:39 [PATCH] selftests/landlock: fix the regex_escape() error check Sasha Levin
  2026-11-03 16:43 ` sashiko-bot
@ 2026-11-03 20:31 ` Günther Noack
  2026-11-03 21:49   ` Sasha Levin
  1 sibling, 1 reply; 4+ messages in thread
From: Günther Noack @ 2026-11-03 20:31 UTC (permalink / raw)
  To: Sasha Levin
  Cc: Mickaël Salaün, Günther Noack, Shuah Khan,
	linux-security-module, linux-kselftest, linux-kernel

Hello Sasha!

On Tue, Nov 03, 2026 at 11:39:17AM -0500, Sasha Levin wrote:
> regex_escape() returns (char *)-ENOMEM when the buffer is too small, but
> matches_log_fs_extra() checks the returned pointer with "< 0", which is
> never true, and goes on with a bogus cursor. On 32-bit architectures gcc
> also warns about the cast of the pointer in the error return:
> 
>   fs_test.c:7474:24: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
> 
> Compare against (char *)-ENOMEM instead.
> 
> The size left after the escaped path also subtracts the prefix a second
> time, which can make a long path fail with -E2BIG. Compute it from the
> cursor instead.
> 
> Fixes: 316d06b01130 ("selftests/landlock: Add audit tests for filesystem")
> Signed-off-by: Sasha Levin <sashal@kernel.org>
> ---
> Found with 32-bit builds of the selftests (gcc 16 and clang 21 with
> -m32, with and without -D_TIME_BITS=64 -D_FILE_OFFSET_BITS=64);
> KernelCI's 32-bit arm builds show the same warnings. The changed files
> build without them now, and the x86_64 builds don't change.
> 
>  tools/testing/selftests/landlock/fs_test.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/testing/selftests/landlock/fs_test.c b/tools/testing/selftests/landlock/fs_test.c
> index f725882fa4a78..27a78434d1c21 100644
> --- a/tools/testing/selftests/landlock/fs_test.c
> +++ b/tools/testing/selftests/landlock/fs_test.c
> @@ -7470,10 +7470,10 @@ static int matches_log_fs_extra(struct __test_metadata *const _metadata,
>  	log_match_cursor = regex_escape(absolute_path, log_match_cursor,
>  					log_match_remaining);
>  	free(absolute_path);
> -	if (log_match_cursor < 0)
> -		return (long long)log_match_cursor;
> +	if (log_match_cursor == (char *)-ENOMEM)
> +		return -ENOMEM;
>  
> -	log_match_remaining -= log_match_cursor - log_match;
> +	log_match_remaining = sizeof(log_match) - (log_match_cursor - log_match);
>  	chunk_len = snprintf(log_match_cursor, log_match_remaining,
>  			     "\" dev=\"[^\"]\\+\" ino=[0-9]\\+%s$",
>  			     extra ?: "");
> -- 
> 2.53.0
> 

Thanks for the contribution!  I also stumbled across this one when I
did a wider cleanup, and at the time I came to the conclusion that it
would be wiser to fix the weird way in which regex_escape() reports
errors.

https://lore.kernel.org/all/20261008142604.39107-20-gnoack3000@gmail.com/

Does that seem reasonable to you?

If you are looking into Landlock selftests, you might want to have a
look at that patch set, it already fixes a wide range of issues.

–Günther

P.S.: Mickaël, gentle nudge!  I would still appreciate a review on the
      selftest cleanup patch set, even though I know that I need to
      start rebasing it soon to make it apply cleanly.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] selftests/landlock: fix the regex_escape() error check
  2026-11-03 20:31 ` Günther Noack
@ 2026-11-03 21:49   ` Sasha Levin
  0 siblings, 0 replies; 4+ messages in thread
From: Sasha Levin @ 2026-11-03 21:49 UTC (permalink / raw)
  To: Günther Noack
  Cc: Mickaël Salaün, Günther Noack, Shuah Khan,
	linux-security-module, linux-kselftest, linux-kernel

On Tue, Nov 03, 2026 at 09:31:33PM +0100, Günther Noack wrote:
> Thanks for the contribution!  I also stumbled across this one when I
> did a wider cleanup, and at the time I came to the conclusion that it
> would be wiser to fix the weird way in which regex_escape() reports
> errors.
>
> https://lore.kernel.org/all/20261008142604.39107-20-gnoack3000@gmail.com/
>
> Does that seem reasonable to you?

Yes, that's better. Your 18/27 also fixes the size miscount after the
escaped path, so it covers everything this patch does. Let's drop this
one in favor of your series.

--
Thanks,
Sasha

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-03 21:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-03 16:39 [PATCH] selftests/landlock: fix the regex_escape() error check Sasha Levin
2026-11-03 16:43 ` sashiko-bot
2026-11-03 20:31 ` Günther Noack
2026-11-03 21:49   ` Sasha Levin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox