Skip to content

Commit 1846dd8

Browse files
danobiAlexei Starovoitov
authored andcommitted
libbpf: Set MFD_NOEXEC_SEAL when creating memfd
Starting from 105ff53 ("mm/memfd: add MFD_NOEXEC_SEAL and MFD_EXEC") and until 1717449 ("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 <[email protected]> Link: https://lore.kernel.org/r/6e62c2421ad7eb1da49cbf16da95aaaa7f94d394.1735594195.git.dxu@dxuuu.xyz Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent c5d2bac commit 1846dd8

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1731,12 +1731,24 @@ static int sys_memfd_create(const char *name, unsigned flags)
17311731
#ifndef MFD_CLOEXEC
17321732
#define MFD_CLOEXEC 0x0001U
17331733
#endif
1734+
#ifndef MFD_NOEXEC_SEAL
1735+
#define MFD_NOEXEC_SEAL 0x0008U
1736+
#endif
17341737

17351738
static int create_placeholder_fd(void)
17361739
{
1740+
unsigned int flags = MFD_CLOEXEC | MFD_NOEXEC_SEAL;
1741+
const char *name = "libbpf-placeholder-fd";
17371742
int fd;
17381743

1739-
fd = ensure_good_fd(sys_memfd_create("libbpf-placeholder-fd", MFD_CLOEXEC));
1744+
fd = ensure_good_fd(sys_memfd_create(name, flags));
1745+
if (fd >= 0)
1746+
return fd;
1747+
else if (errno != EINVAL)
1748+
return -errno;
1749+
1750+
/* Possibly running on kernel without MFD_NOEXEC_SEAL */
1751+
fd = ensure_good_fd(sys_memfd_create(name, flags & ~MFD_NOEXEC_SEAL));
17401752
if (fd < 0)
17411753
return -errno;
17421754
return fd;

0 commit comments

Comments
 (0)