Skip to content

Commit 6fd7353

Browse files
danielverkampakpm00
authored andcommitted
mm/memfd: add F_SEAL_EXEC
Patch series "mm/memfd: introduce MFD_NOEXEC_SEAL and MFD_EXEC", v8. Since Linux introduced the memfd feature, memfd 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 in this kind. 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 system, we need a solution to differentiate runc's use of executable memfds and an attacker's [3]. To address those above, this set of patches add following: 1> Let memfd_create() set X bit at creation time. 2> Let memfd to be sealed for modifying X bit. 3> A new pid namespace sysctl: vm.memfd_noexec to control the behavior of X bit.For example, if a container has vm.memfd_noexec=2, then memfd_create() without MFD_NOEXEC_SEAL will be rejected. 4> A new security hook in memfd_create(). This make it possible to a new LSM, which rejects or allows executable memfd based on its security policy. This patch (of 5): The new F_SEAL_EXEC flag will prevent modification of the exec bits: written as traditional octal mask, 0111, or as named flags, S_IXUSR | S_IXGRP | S_IXOTH. Any chmod(2) or similar call that attempts to modify any of these bits after the seal is applied will fail with errno EPERM. This will preserve the execute bits as they are at the time of sealing, so the memfd will become either permanently executable or permanently un-executable. Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Daniel Verkamp <[email protected]> Co-developed-by: Jeff Xu <[email protected]> Signed-off-by: Jeff Xu <[email protected]> Reviewed-by: Kees Cook <[email protected]> Cc: Dmitry Torokhov <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Jann Horn <[email protected]> Cc: Jorge Lucangeli Obes <[email protected]> Cc: Shuah Khan <[email protected]> Cc: David Herrmann <[email protected]> Cc: kernel test robot <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent f1eb1ba commit 6fd7353

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

include/uapi/linux/fcntl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#define F_SEAL_GROW 0x0004 /* prevent file from growing */
4444
#define F_SEAL_WRITE 0x0008 /* prevent writes */
4545
#define F_SEAL_FUTURE_WRITE 0x0010 /* prevent future writes while mapped */
46+
#define F_SEAL_EXEC 0x0020 /* prevent chmod modifying exec bits */
4647
/* (1U << 31) is reserved for signed error codes */
4748

4849
/*

mm/memfd.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ static unsigned int *memfd_file_seals_ptr(struct file *file)
147147
}
148148

149149
#define F_ALL_SEALS (F_SEAL_SEAL | \
150+
F_SEAL_EXEC | \
150151
F_SEAL_SHRINK | \
151152
F_SEAL_GROW | \
152153
F_SEAL_WRITE | \
@@ -175,6 +176,7 @@ static int memfd_add_seals(struct file *file, unsigned int seals)
175176
* SEAL_SHRINK: Prevent the file from shrinking
176177
* SEAL_GROW: Prevent the file from growing
177178
* SEAL_WRITE: Prevent write access to the file
179+
* SEAL_EXEC: Prevent modification of the exec bits in the file mode
178180
*
179181
* As we don't require any trust relationship between two parties, we
180182
* must prevent seals from being removed. Therefore, sealing a file

mm/shmem.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,12 @@ static int shmem_setattr(struct user_namespace *mnt_userns,
10931093
if (error)
10941094
return error;
10951095

1096+
if ((info->seals & F_SEAL_EXEC) && (attr->ia_valid & ATTR_MODE)) {
1097+
if ((inode->i_mode ^ attr->ia_mode) & 0111) {
1098+
return -EPERM;
1099+
}
1100+
}
1101+
10961102
if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
10971103
loff_t oldsize = inode->i_size;
10981104
loff_t newsize = attr->ia_size;

0 commit comments

Comments
 (0)