public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next v2] libbpf: Set MFD_NOEXEC_SEAL when creating memfd
@ 2024-12-30 21:31 Daniel Xu
  2024-12-30 23:00 ` patchwork-bot+netdevbpf
  0 siblings, 1 reply; 2+ messages in thread
From: Daniel Xu @ 2024-12-30 21:31 UTC (permalink / raw)
  To: ast, daniel, eddyz87, andrii
  Cc: martin.lau, song, yonghong.song, john.fastabend, kpsingh, sdf,
	haoluo, jolsa, bpf, linux-kernel

Starting from 105ff5339f49 ("mm/memfd: add MFD_NOEXEC_SEAL and
MFD_EXEC") and until 1717449b4417 ("memfd: drop warning for missing
exec-related flags"), the kernel would print a warning if neither
MFD_NOEXEC_SEAL nor MFD_EXEC is set in memfd_create().

If libbpf runs on on a kernel between these two commits (eg. on an
improperly backported system), it'll trigger this warning.

To avoid this warning (and also be more secure), explicitly set
MFD_NOEXEC_SEAL. But since libbpf can be run on potentially very old
kernels, leave a fallback for kernels without MFD_NOEXEC_SEAL support.

Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
---
Changes since v1:
* Update commit message with pointers to recent memfd warning changes

 tools/lib/bpf/libbpf.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 66173ddb5a2d..46492cc0927d 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -1731,12 +1731,24 @@ static int sys_memfd_create(const char *name, unsigned flags)
 #ifndef MFD_CLOEXEC
 #define MFD_CLOEXEC 0x0001U
 #endif
+#ifndef MFD_NOEXEC_SEAL
+#define MFD_NOEXEC_SEAL 0x0008U
+#endif
 
 static int create_placeholder_fd(void)
 {
+	unsigned int flags = MFD_CLOEXEC | MFD_NOEXEC_SEAL;
+	const char *name = "libbpf-placeholder-fd";
 	int fd;
 
-	fd = ensure_good_fd(sys_memfd_create("libbpf-placeholder-fd", MFD_CLOEXEC));
+	fd = ensure_good_fd(sys_memfd_create(name, flags));
+	if (fd >= 0)
+		return fd;
+	else if (errno != EINVAL)
+		return -errno;
+
+	/* Possibly running on kernel without MFD_NOEXEC_SEAL */
+	fd = ensure_good_fd(sys_memfd_create(name, flags & ~MFD_NOEXEC_SEAL));
 	if (fd < 0)
 		return -errno;
 	return fd;
-- 
2.47.1


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

* Re: [PATCH bpf-next v2] libbpf: Set MFD_NOEXEC_SEAL when creating memfd
  2024-12-30 21:31 [PATCH bpf-next v2] libbpf: Set MFD_NOEXEC_SEAL when creating memfd Daniel Xu
@ 2024-12-30 23:00 ` patchwork-bot+netdevbpf
  0 siblings, 0 replies; 2+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-12-30 23:00 UTC (permalink / raw)
  To: Daniel Xu
  Cc: ast, daniel, eddyz87, andrii, martin.lau, song, yonghong.song,
	john.fastabend, kpsingh, sdf, haoluo, jolsa, bpf, linux-kernel

Hello:

This patch was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <ast@kernel.org>:

On Mon, 30 Dec 2024 14:31:22 -0700 you wrote:
> Starting from 105ff5339f49 ("mm/memfd: add MFD_NOEXEC_SEAL and
> MFD_EXEC") and until 1717449b4417 ("memfd: drop warning for missing
> exec-related flags"), the kernel would print a warning if neither
> MFD_NOEXEC_SEAL nor MFD_EXEC is set in memfd_create().
> 
> If libbpf runs on on a kernel between these two commits (eg. on an
> improperly backported system), it'll trigger this warning.
> 
> [...]

Here is the summary with links:
  - [bpf-next,v2] libbpf: Set MFD_NOEXEC_SEAL when creating memfd
    https://git.kernel.org/bpf/bpf-next/c/1846dd8e3a3e

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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

end of thread, other threads:[~2024-12-30 23:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-30 21:31 [PATCH bpf-next v2] libbpf: Set MFD_NOEXEC_SEAL when creating memfd Daniel Xu
2024-12-30 23:00 ` patchwork-bot+netdevbpf

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