Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

libbpf: Set MFD_NOEXEC_SEAL when creating memfd

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>
Link: https://lore.kernel.org/r/6e62c2421ad7eb1da49cbf16da95aaaa7f94d394.1735594195.git.dxu@dxuuu.xyz
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Daniel Xu and committed by
Alexei Starovoitov
1846dd8e c5d2bac9

+13 -1
+13 -1
tools/lib/bpf/libbpf.c
··· 1731 1731 #ifndef MFD_CLOEXEC 1732 1732 #define MFD_CLOEXEC 0x0001U 1733 1733 #endif 1734 + #ifndef MFD_NOEXEC_SEAL 1735 + #define MFD_NOEXEC_SEAL 0x0008U 1736 + #endif 1734 1737 1735 1738 static int create_placeholder_fd(void) 1736 1739 { 1740 + unsigned int flags = MFD_CLOEXEC | MFD_NOEXEC_SEAL; 1741 + const char *name = "libbpf-placeholder-fd"; 1737 1742 int fd; 1738 1743 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)); 1740 1752 if (fd < 0) 1741 1753 return -errno; 1742 1754 return fd;