Skip to contentSkip to conversation

mm/memfd: add documentation for MFD_NOEXEC_SEAL

PATCH Superseded [email protected] 3 participants 4 messages Jun 11, 2024

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover mm/memfd: add documentation for MFD_NOEXEC_SEAL

From: Jeff Xu <[email protected]>

When MFD_NOEXEC_SEAL was introduced, there was one big mistake: it didn't have proper documentation. This led to a lot of confusion, especially about whether or not memfd created with the MFD_NOEXEC_SEAL flag is sealable. Before MFD_NOEXEC_SEAL, memfd had to explicitly set MFD_ALLOW_SEALING to be sealable, so it's a fair question.

As one might have noticed, unlike other flags in memfd_create, MFD_NOEXEC_SEAL is actually a combination of multiple flags. The idea is to make it easier to use memfd in the most common way, which is NOEXEC + F_SEAL_EXEC + MFD_ALLOW_SEALING. This works with sysctl vm.noexec to help existing applications move to a more secure way of using memfd.

Proposals have been made to put MFD_NOEXEC_SEAL non-sealable, unless MFD_ALLOW_SEALING is set, to be consistent with other flags [1] [2], Those are based on the viewpoint that each flag is an atomic unit, which is a reasonable assumption. However, MFD_NOEXEC_SEAL was designed with the intent of promoting the most secure method of using memfd, therefore a combination of multiple functionalities into one bit.

Furthermore, the MFD_NOEXEC_SEAL has been added for more than one year, and multiple applications and distributions have backported and utilized it. Altering ABI now presents a degree of risk and may lead to disruption.

MFD_NOEXEC_SEAL is a new flag, and applications must change their code to use it. There is no backward compatibility problem.

When sysctl vm.noexec == 1 or 2, applications that don't set MFD_NOEXEC_SEAL or MFD_EXEC will get MFD_NOEXEC_SEAL memfd. And old-application might break, that is by-design, in such a system vm.noexec = 0 shall be used. Also no backward compatibility problem.

I propose to include this documentation patch to assist in clarifying the semantics of MFD_NOEXEC_SEAL, thereby preventing any potential future confusion.

This patch supersede previous patch which is trying different direction [3], and please remove [2] from mm-unstable branch when applying this patch.

Finally, I would like to express my gratitude to David Rheinsberg and Barnabás Pőcze for initiating the discussion on the topic of sealability.

[1]
https://lore.kernel.org/lkml/[email protected]/

[2]
https://lore.kernel.org/lkml/[email protected]/

[3]
https://lore.kernel.org/lkml/[email protected]/

v2:
Update according to Randy Dunlap' comments.

v1:
https://lore.kernel.org/linux-mm/[email protected]/

Jeff Xu (1):
mm/memfd: add documentation for MFD_NOEXEC_SEAL MFD_EXEC

 Documentation/userspace-api/index.rst      |  1 +
 Documentation/userspace-api/mfd_noexec.rst | 86 ++++++++++++++++++++++
 2 files changed, 87 insertions(+)
 create mode 100644 Documentation/userspace-api/mfd_noexec.rst
Signature · 1 line
2.45.2.505.gda0bf45e8d-goog
#1 lore raw

PATCH v2 1/1 mm/memfd: add documentation for MFD_NOEXEC_SEAL MFD_EXEC

From: Jeff Xu <[email protected]>

Add documentation for memfd_create flags: MFD_NOEXEC_SEAL and MFD_EXEC

---
 Documentation/userspace-api/index.rst      |  1 +
 Documentation/userspace-api/mfd_noexec.rst | 86 ++++++++++++++++++++++
 2 files changed, 87 insertions(+)
 create mode 100644 Documentation/userspace-api/mfd_noexec.rst
Documentation/userspace-api/index.rst +1 -0
index 5926115ec0ed..8a251d71fa6e 100644
@@ -32,6 +32,7 @@ Security-related interfaces
    seccomp_filter
    landlock
    lsm
+   mfd_noexec
    spec_ctrl
    tee
 
Documentation/userspace-api/mfd_noexec.rst new file +86 -0

86 lines not on this page · raw message

Signature · 1 line
2.45.2.505.gda0bf45e8d-goog
reviewer
#2 lore raw

Re: [PATCH v2 1/1] mm/memfd: add documentation for MFD_NOEXEC_SEAL MFD_EXEC

Quoted 32 lines · On 6/10/24 8:49 PM, [email protected] wrote:

From: Jeff Xu <[email protected]>

Add documentation for memfd_create flags: MFD_NOEXEC_SEAL and MFD_EXEC

---
 Documentation/userspace-api/index.rst      |  1 +
 Documentation/userspace-api/mfd_noexec.rst | 86 ++++++++++++++++++++++
 2 files changed, 87 insertions(+)
 create mode 100644 Documentation/userspace-api/mfd_noexec.rst
Documentation/userspace-api/index.rst +1 -0
index 5926115ec0ed..8a251d71fa6e 100644
@@ -32,6 +32,7 @@ Security-related interfaces
    seccomp_filter
    landlock
    lsm
+   mfd_noexec
    spec_ctrl
    tee
 
Documentation/userspace-api/mfd_noexec.rst new file +0 -0
new file mode 100644
index 000000000000..ec6e3560fbff

Documentation/userspace-api/mfd_noexec.rst line 4

Quoted 4 lines
Documentation/userspace-api/mfd_noexec.rst new file +4 -0
@@ -0,0 +1,86 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+==================================
+Introduction of non executable mfd

Missed:
non-executable

Quoted 19 lines
Documentation/userspace-api/mfd_noexec.rst new file +19 -0
+==================================
+:Author:
+    Daniel Verkamp <[email protected]>
+    Jeff Xu <[email protected]>
+
+:Contributor:
+	Aleksa Sarai <[email protected]>
+
+Since Linux introduced the memfd feature, memfds have always had their
+execute bit set, and the memfd_create() syscall doesn't allow setting
+it differently.
+
+However, in a secure-by-default system, such as ChromeOS, (where all
+executables should come from the rootfs, which is protected by verified
+boot), this executable nature of memfd opens a door for NoExec bypass
+and enables “confused deputy attack”.  E.g, in VRP bug [1]: cros_vm
+process created a memfd to share the content with an external process,
+however the memfd is overwritten and used for executing arbitrary code
+and root escalation. [2] lists more VRP of this kind.

Documentation/userspace-api/mfd_noexec.rst line 33

Quoted 10 lines
Documentation/userspace-api/mfd_noexec.rst new file +10 -0
+
+On the other hand, executable memfd has its legit use: runc uses memfd’s
+seal and executable feature to copy the contents of the binary then
+execute them. For such a system, we need a solution to differentiate runc's
+use of executable memfds and an attacker's [3].
+
+To address those above:
+ - Let memfd_create() set X bit at creation time.
+ - Let memfd be sealed for modifying X bit when NX is set.
+ - Add a new pid namespace sysctl: vm.memfd_noexec to help applications to

help applications in

Documentation/userspace-api/mfd_noexec.rst line 37

Quoted 4 lines
Documentation/userspace-api/mfd_noexec.rst new file +4 -0
+   migrating and enforcing non-executable MFD.
+
+User API
+========

The rest looks good. Thanks.

Signature · 1 line
~Randy
#3 lore raw

Re: [PATCH v2 1/1] mm/memfd: add documentation for MFD_NOEXEC_SEAL MFD_EXEC

Quoted 76 lines · Randy Dunlap
Quoted 32 lines · On 6/10/24 8:49 PM, [email protected] wrote:

From: Jeff Xu <[email protected]>

Add documentation for memfd_create flags: MFD_NOEXEC_SEAL and MFD_EXEC

---
 Documentation/userspace-api/index.rst      |  1 +
 Documentation/userspace-api/mfd_noexec.rst | 86 ++++++++++++++++++++++
 2 files changed, 87 insertions(+)
 create mode 100644 Documentation/userspace-api/mfd_noexec.rst
Documentation/userspace-api/index.rst +1 -0
index 5926115ec0ed..8a251d71fa6e 100644
@@ -32,6 +32,7 @@ Security-related interfaces
    seccomp_filter
    landlock
    lsm
+   mfd_noexec
    spec_ctrl
    tee
 
Documentation/userspace-api/mfd_noexec.rst new file +0 -0
new file mode 100644
index 000000000000..ec6e3560fbff

Documentation/userspace-api/mfd_noexec.rst line 4

Quoted 4 lines
Documentation/userspace-api/mfd_noexec.rst new file +4 -0
@@ -0,0 +1,86 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+==================================
+Introduction of non executable mfd

Missed:
non-executable

Quoted 19 lines
Documentation/userspace-api/mfd_noexec.rst new file +19 -0
+==================================
+:Author:
+    Daniel Verkamp <[email protected]>
+    Jeff Xu <[email protected]>
+
+:Contributor:
+     Aleksa Sarai <[email protected]>
+
+Since Linux introduced the memfd feature, memfds have always had their
+execute bit set, and the memfd_create() syscall doesn't allow setting
+it differently.
+
+However, in a secure-by-default system, such as ChromeOS, (where all
+executables should come from the rootfs, which is protected by verified
+boot), this executable nature of memfd opens a door for NoExec bypass
+and enables “confused deputy attack”.  E.g, in VRP bug [1]: cros_vm
+process created a memfd to share the content with an external process,
+however the memfd is overwritten and used for executing arbitrary code
+and root escalation. [2] lists more VRP of this kind.

Documentation/userspace-api/mfd_noexec.rst line 33

Quoted 10 lines
Documentation/userspace-api/mfd_noexec.rst new file +10 -0
+
+On the other hand, executable memfd has its legit use: runc uses memfd’s
+seal and executable feature to copy the contents of the binary then
+execute them. For such a system, we need a solution to differentiate runc's
+use of executable memfds and an attacker's [3].
+
+To address those above:
+ - Let memfd_create() set X bit at creation time.
+ - Let memfd be sealed for modifying X bit when NX is set.
+ - Add a new pid namespace sysctl: vm.memfd_noexec to help applications to

help applications in

Quoted 7 lines

Documentation/userspace-api/mfd_noexec.rst line 37

Quoted 4 lines
Documentation/userspace-api/mfd_noexec.rst new file +4 -0
+   migrating and enforcing non-executable MFD.
+
+User API
+========

The rest looks good. Thanks.

Thanks for your review!

Quoted 2 lines

--
~Randy