Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx+linux-hardening@kernel.org>
To: Hang Zhengyang <hang.zhengyang1010@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Kees Cook <kees@kernel.org>,  Lorenzo Stoakes <ljs@kernel.org>,
	Chris Mason <mason@kernel.org>,
	 Catalin Marinas <catalin.marinas@arm.com>,
	Joey Gouly <joey.gouly@arm.com>,
	 Christian Brauner <brauner@kernel.org>,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	 Jan Kara <jack@suse.cz>, David Hildenbrand <david@kernel.org>,
	 "Liam R. Howlett" <liam@infradead.org>,
	Vlastimil Babka <vbabka@kernel.org>,
	 Mike Rapoport <rppt@kernel.org>, Michal Hocko <mhocko@suse.com>,
	 Suren Baghdasaryan <surenb@google.com>,
	Shuah Khan <shuah@kernel.org>, Gregory Price <gourry@gourry.net>,
	 Jann Horn <jannh@google.com>, Pedro Falcato <pfalcato@suse.de>,
	 Florent Revest <revest@chromium.org>,
	linux-mm@kvack.org, linux-fsdevel@vger.kernel.org,
	 linux-kselftest@vger.kernel.org,
	linux-hardening@vger.kernel.org, linux-api@vger.kernel.org,
	 linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/2] selftests/mm: test exec of a PT_GNU_STACK PF_X program under MDWE
Date: Sat, 5 Dec 2026 12:27:21 +0100	[thread overview]
Message-ID: <axPydJlTl_J6f7qS@devuan> (raw)
In-Reply-To: <20261205105547.1251214-3-hang.zhengyang1010@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 6081 bytes --]

Hi Hang,

> Date: 2026-12-05 10:55:47+0000
> From: Hang Zhengyang <hang.zhengyang1010@gmail.com>
>
> Add mdwe_execstack, a helper linked with -z execstack. It exits 1 if
> its stack is writable and executable, and 0 if not. Add an exec_stack
> test that runs it in each mdwe variant. Where MDWE is inherited across
> exec ("enabled" and "inherited"), the stack must not be writable and
> executable. In the other variants it must be, as the marking asks.
> 
> The helper checks that its own PT_GNU_STACK has PF_X, and the test is
> skipped if not. So a build that loses -z execstack can't pass the test.
> 
> Signed-off-by: Hang Zhengyang <hang.zhengyang1010@gmail.com>
> ---
>  tools/testing/selftests/mm/Makefile         |  3 ++
>  tools/testing/selftests/mm/mdwe_execstack.c | 44 +++++++++++++++++++++
>  tools/testing/selftests/mm/mdwe_test.c      | 43 ++++++++++++++++++++
>  3 files changed, 90 insertions(+)
>  create mode 100644 tools/testing/selftests/mm/mdwe_execstack.c
> 
> diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
> index 675ec57d47de..5e0de1666e2f 100644
> --- a/tools/testing/selftests/mm/Makefile
> +++ b/tools/testing/selftests/mm/Makefile
> @@ -98,6 +98,7 @@ TEST_GEN_FILES += split_huge_page_test
>  TEST_GEN_FILES += ksm_tests
>  TEST_GEN_FILES += ksm_functional_tests
>  TEST_GEN_FILES += mdwe_test
> +TEST_GEN_FILES += mdwe_execstack
>  TEST_GEN_FILES += hugetlb_fault_after_madv
>  TEST_GEN_FILES += hugetlb_madv_vs_map
>  TEST_GEN_FILES += hugetlb_dio
> @@ -267,6 +268,8 @@ $(OUTPUT)/migration: LDLIBS += -lnuma
>  
>  $(OUTPUT)/rmap: LDLIBS += -lnuma
>  
> +$(OUTPUT)/mdwe_execstack: LDFLAGS += -z execstack
> +
>  local_config.mk local_config.h_gen: check_config.sh
>  	@{ $(patsubst @%,%,$(call msg,CHK,config,$@)) :; } >&2
>  	$(Q)CC="$(CC)" CFLAGS="$(CFLAGS)" ./check_config.sh
> diff --git a/tools/testing/selftests/mm/mdwe_execstack.c b/tools/testing/selftests/mm/mdwe_execstack.c
> new file mode 100644
> index 000000000000..820d8821d650
> --- /dev/null
> +++ b/tools/testing/selftests/mm/mdwe_execstack.c
> @@ -0,0 +1,44 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Helper for mdwe_test, linked with -z execstack so that its PT_GNU_STACK
> + * asks for an executable stack. Exits 1 if its stack is writable and
> + * executable, 0 if not, 2 if it can't tell, and 4 (KSFT_SKIP) if it wasn't
> + * linked with -z execstack after all.
> + */
> +#include <link.h>
> +#include <stdio.h>
> +#include <string.h>
> +#include <sys/auxv.h>
> +
> +static int asks_for_exec_stack(void)
> +{
> +	const ElfW(Phdr) *phdr = (const void *)getauxval(AT_PHDR);
> +	unsigned long i, n = getauxval(AT_PHNUM);
> +
> +	for (i = 0; phdr && i < n; i++)
> +		if (phdr[i].p_type == PT_GNU_STACK)
> +			return !!(phdr[i].p_flags & PF_X);
> +	return 0;
> +}
> +
> +int main(void)
> +{
> +	char line[256], perms[5];
> +	FILE *f;
> +
> +	if (!asks_for_exec_stack())
> +		return 4;
> +	f = fopen("/proc/self/maps", "r");
> +	if (!f)
> +		return 2;
> +	while (fgets(line, sizeof(line), f)) {

Lines returned by fgets(3) should almost always be verified.  If you end
up with a truncated line, you might end up in a weird situation.  This
should do it:

		if (!strchr(line, '\n'))
			return 2;

> +		if (!strstr(line, "[stack]"))
> +			continue;
> +		fclose(f);
> +		if (sscanf(line, "%*s %4s", perms) != 1)
> +			return 2;

sscanf(3) is rather complex, and easy to misuse.  How about using simple
string functions?:

		const char  *p;

		p = strpbrk(line, " \t");
		if (p == NULL)
			return 2;
		p += strspn(line, " \t");

This should also result in faster tests, since sscanf(3) is slower than
<string.h> functions.

> +		return perms[1] == 'w' && perms[2] == 'x';

And how about this to avoid manual byte operations?:

		return strstr(p, "wx") == p+1;


Have a lovely day!
Alex

> +	}
> +	fclose(f);
> +	return 2;
> +}
> diff --git a/tools/testing/selftests/mm/mdwe_test.c b/tools/testing/selftests/mm/mdwe_test.c
> index 647779653da0..3d79d370d702 100644
> --- a/tools/testing/selftests/mm/mdwe_test.c
> +++ b/tools/testing/selftests/mm/mdwe_test.c
> @@ -7,8 +7,10 @@
>  #include <linux/mman.h>
>  #include <linux/prctl.h>
>  
> +#include <limits.h>
>  #include <stdio.h>
>  #include <stdlib.h>
> +#include <string.h>
>  #include <sys/auxv.h>
>  #include <sys/prctl.h>
>  #include <sys/wait.h>
> @@ -284,6 +286,47 @@ TEST_F(mdwe, mmap_FIXED)
>  	EXPECT_EQ(p, self->p);
>  }
>  
> +TEST_F(mdwe, exec_stack)
> +{
> +	/* Without MDWE after exec, the marking gives a writable executable stack */
> +	int expect_wx = !(variant->enabled && variant->inherit);
> +	char helper[PATH_MAX], *slash;
> +	ssize_t len;
> +	pid_t pid;
> +	int status;
> +
> +	len = readlink("/proc/self/exe", helper, sizeof(helper) - sizeof("mdwe_execstack"));
> +	if (len <= 0)
> +		SKIP(return, "can't read /proc/self/exe");
> +	helper[len] = '\0';
> +	slash = strrchr(helper, '/');
> +	ASSERT_NE(slash, NULL);
> +	strcpy(slash + 1, "mdwe_execstack");
> +	if (access(helper, X_OK))
> +		SKIP(return, "mdwe_execstack not found");
> +
> +	pid = fork();
> +	ASSERT_GE(pid, 0);
> +	if (pid == 0) {
> +		execl(helper, helper, (char *)NULL);
> +		perror("execl mdwe_execstack");
> +		_exit(126);
> +	}
> +	ASSERT_EQ(waitpid(pid, &status, 0), pid);
> +	ASSERT_TRUE(WIFEXITED(status)) {
> +		TH_LOG("mdwe_execstack killed by signal %d", WTERMSIG(status));
> +	}
> +	if (WEXITSTATUS(status) == KSFT_SKIP)
> +		SKIP(return, "mdwe_execstack wasn't linked with -z execstack");
> +	ASSERT_LT(WEXITSTATUS(status), 2) {
> +		TH_LOG("mdwe_execstack failed with %d", WEXITSTATUS(status));
> +	}
> +	EXPECT_EQ(WEXITSTATUS(status), expect_wx) {
> +		TH_LOG("the stack is %swritable and executable after exec",
> +		       WEXITSTATUS(status) ? "" : "not ");
> +	}
> +}
> +
>  TEST_F(mdwe, arm64_BTI)
>  {
>  	int ret;
> -- 
> 2.53.0
> 
> 

-- 
<https://www.alejandro-colomar.es>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

      reply	other threads:[~2026-12-05 11:27 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 10:55 [PATCH 0/2] exec: don't honor PT_GNU_STACK PF_X under MDWE Hang Zhengyang
2026-12-05 10:55 ` [PATCH 1/2] " Hang Zhengyang
2026-12-05 10:55 ` [PATCH 2/2] selftests/mm: test exec of a PT_GNU_STACK PF_X program " Hang Zhengyang
2026-12-05 11:27   ` Alejandro Colomar [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=axPydJlTl_J6f7qS@devuan \
    --to=alx+linux-hardening@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=brauner@kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=david@kernel.org \
    --cc=gourry@gourry.net \
    --cc=hang.zhengyang1010@gmail.com \
    --cc=jack@suse.cz \
    --cc=jannh@google.com \
    --cc=joey.gouly@arm.com \
    --cc=kees@kernel.org \
    --cc=liam@infradead.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mason@kernel.org \
    --cc=mhocko@suse.com \
    --cc=pfalcato@suse.de \
    --cc=revest@chromium.org \
    --cc=rppt@kernel.org \
    --cc=shuah@kernel.org \
    --cc=surenb@google.com \
    --cc=vbabka@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /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