Re: [RFC PATCH 0/3] memfd: cleanups for vm.memfd_noexec

From: Jeff Xu
Date: Mon Jul 17 2023 - 20:47:35 EST


Hello Aleksa,

Thanks for your email and patches for discussion.

On Thu, Jul 13, 2023 at 7:34 AM Aleksa Sarai <cyphar@xxxxxxxxxx> wrote:
>
> It seems that the most critical issue with vm.memfd_noexec=2 (the fact
> that passing MFD_EXEC would bypass it entirely[1]) has been fixed in
> Andrew's tree[2], but there are still some outstanding issues that need
> to be addressed:
>
> * The dmesg warnings are pr_warn_once, which on most systems means that
> they will be used up by systemd or some other boot process and
> userspace developers will never see it. The original patch posted to
> the ML used pr_warn_ratelimited but the merged patch had it changed
> (with a comment about it being "per review"), but given that the
> current warnings are useless, pr_warn_ratelimited makes far more
> sense.
>
Ya, This was discussed in [1]
Replacing pr_warn_once with pr_warn_ratelimited won't address Peter
Xu's observation that "ratelimited" will fill syslog [2], I'm not
sure it is acceptable to ones who is not interested in memfd, I will
defer this to maintainers.

[1] https://lore.kernel.org/lkml/202212161233.85C9783FB@keescook/
[2] https://lwn.net/ml/linux-kernel/Y5yS8wCnuYGLHMj4@x1n/

> * vm.memfd_noexec=2 shouldn't reject old-style memfd_create(2) syscalls
> because it will make it far to difficult to ever migrate. Instead it
> should imply MFD_EXEC.
>
Though the purpose of memfd_noexec=2 is not to help with migration -
but to disable creation of executable memfd for the current system/pid
namespace.
During the migration, vm.memfd_noexe = 1 helps overwriting for
unmigrated user code as a temporary measure.
Additional functionality/features should be implemented through
security hook and LSM, not sysctl, I think.

> * The ratcheting mechanism for vm.memfd_noexec doesn't make sense as a
> security mechanism because a CAP_SYS_ADMIN capable user can create
> executable binaries in a hidden tmpfs very easily, not to mention the
> many other things they can do.
>
By further limiting CAP_SYS_ADMIN, an attacker can't modify this
sysctl even after compromising some system service with high
privilege, YAMA has the same approach for ptrace_scope=3

In addition, this sysctl is pid_name spaced, this means child
pid_namespace will alway have the same or stricter security setting
than its parent, this allows admin to maintain a tree like view. If we
allow the child pid namespace to elevate its setting, then the
system-wide setting is no longer meaningful.

The code sample shared in this patch set indicates that the attacker
already has the ability of creating tmpfs and executing complex steps,
at that point, it doesn't matter if the code execution is from memfd
or not. For a safe by default system such as ChromeOS, attackers won't
easily run arbitrary code, memfd is one of the open doors for that, so
we are disabling executable memfd in ChromeOS. In other words: if an
attacker can already execute the arbitrary code as sample given in
ChromeOS, without using executable memfd, then memfd is no longer the
thing we need to worry about, the arbitrary code execution is already
achieved by the attacker. Even though I use ChromeOS as an example, I
think the same type of threat model applies to any system that wants
to disable executable memfd entirely.

> * The memfd selftests would not exit with a non-zero error code when
> certain tests that ran in a forked process (specifically the ones
> related to MFD_EXEC and MFD_NOEXEC_SEAL) failed.
>
I will test this code and follow up.


Thanks!




-Jeff Xu

> (This patchset is based on top of Jeff Xu's patches[2] fixing the
> MFD_EXEC bug in vm.memfd_noexec=2.)
>
> [1]: https://lore.kernel.org/all/ZJwcsU0vI-nzgOB_@xxxxxxxxxxxxx/
> [2]: https://lore.kernel.org/all/20230705063315.3680666-1-jeffxu@xxxxxxxxxx/
>
> Aleksa Sarai (3):
> memfd: cleanups for vm.memfd_noexec handling
> memfd: remove racheting feature from vm.memfd_noexec
> selftests: memfd: error out test process when child test fails
>
> include/linux/pid_namespace.h | 16 +++------
> kernel/pid_sysctl.h | 7 ----
> mm/memfd.c | 32 +++++++----------
> tools/testing/selftests/memfd/memfd_test.c | 41 ++++++++++++++++++----
> 4 files changed, 51 insertions(+), 45 deletions(-)
>
> --
> 2.41.0
>
>