Skip to content

Commit 105ff53

Browse files
Jeff Xuakpm00
authored andcommitted
mm/memfd: add MFD_NOEXEC_SEAL and MFD_EXEC
The new MFD_NOEXEC_SEAL and MFD_EXEC flags allows application to set executable bit at creation time (memfd_create). When MFD_NOEXEC_SEAL is set, memfd is created without executable bit (mode:0666), and sealed with F_SEAL_EXEC, so it can't be chmod to be executable (mode: 0777) after creation. when MFD_EXEC flag is set, memfd is created with executable bit (mode:0777), this is the same as the old behavior of memfd_create. The new pid namespaced sysctl vm.memfd_noexec has 3 values: 0: memfd_create() without MFD_EXEC nor MFD_NOEXEC_SEAL acts like MFD_EXEC was set. 1: memfd_create() without MFD_EXEC nor MFD_NOEXEC_SEAL acts like MFD_NOEXEC_SEAL was set. 2: memfd_create() without MFD_NOEXEC_SEAL will be rejected. The sysctl allows finer control of memfd_create for old-software that doesn't set the executable bit, for example, a container with vm.memfd_noexec=1 means the old-software will create non-executable memfd by default. Also, the value of memfd_noexec is passed to child namespace at creation time. For example, if the init namespace has vm.memfd_noexec=2, all its children namespaces will be created with 2. [[email protected]: add stub functions to fix build] [[email protected]: remove unneeded register_pid_ns_ctl_table_vm() stub, per Jeff] [[email protected]: s/pr_warn_ratelimited/pr_warn_once/, per review] [[email protected]: fix CONFIG_SYSCTL=n warning] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Jeff Xu <[email protected]> Co-developed-by: Daniel Verkamp <[email protected]> Signed-off-by: Daniel Verkamp <[email protected]> Reported-by: kernel test robot <[email protected]> Reviewed-by: Kees Cook <[email protected]> Cc: David Herrmann <[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]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 32d118a commit 105ff53

File tree

5 files changed

+134
-2
lines changed

5 files changed

+134
-2
lines changed

include/linux/pid_namespace.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@
1616

1717
struct fs_pin;
1818

19+
#if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE)
20+
/*
21+
* sysctl for vm.memfd_noexec
22+
* 0: memfd_create() without MFD_EXEC nor MFD_NOEXEC_SEAL
23+
* acts like MFD_EXEC was set.
24+
* 1: memfd_create() without MFD_EXEC nor MFD_NOEXEC_SEAL
25+
* acts like MFD_NOEXEC_SEAL was set.
26+
* 2: memfd_create() without MFD_NOEXEC_SEAL will be
27+
* rejected.
28+
*/
29+
#define MEMFD_NOEXEC_SCOPE_EXEC 0
30+
#define MEMFD_NOEXEC_SCOPE_NOEXEC_SEAL 1
31+
#define MEMFD_NOEXEC_SCOPE_NOEXEC_ENFORCED 2
32+
#endif
33+
1934
struct pid_namespace {
2035
struct idr idr;
2136
struct rcu_head rcu;
@@ -31,6 +46,10 @@ struct pid_namespace {
3146
struct ucounts *ucounts;
3247
int reboot; /* group exit code if this pidns was rebooted */
3348
struct ns_common ns;
49+
#if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE)
50+
/* sysctl for vm.memfd_noexec */
51+
int memfd_noexec_scope;
52+
#endif
3453
} __randomize_layout;
3554

3655
extern struct pid_namespace init_pid_ns;

include/uapi/linux/memfd.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#define MFD_CLOEXEC 0x0001U
99
#define MFD_ALLOW_SEALING 0x0002U
1010
#define MFD_HUGETLB 0x0004U
11+
/* not executable and sealed to prevent changing to executable. */
12+
#define MFD_NOEXEC_SEAL 0x0008U
13+
/* executable */
14+
#define MFD_EXEC 0x0010U
1115

1216
/*
1317
* Huge page size encoding when MFD_HUGETLB is specified, and a huge page

kernel/pid_namespace.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/sched/task.h>
2424
#include <linux/sched/signal.h>
2525
#include <linux/idr.h>
26+
#include "pid_sysctl.h"
2627

2728
static DEFINE_MUTEX(pid_caches_mutex);
2829
static struct kmem_cache *pid_ns_cachep;
@@ -110,6 +111,8 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns
110111
ns->ucounts = ucounts;
111112
ns->pid_allocated = PIDNS_ADDING;
112113

114+
initialize_memfd_noexec_scope(ns);
115+
113116
return ns;
114117

115118
out_free_idr:
@@ -455,6 +458,8 @@ static __init int pid_namespaces_init(void)
455458
#ifdef CONFIG_CHECKPOINT_RESTORE
456459
register_sysctl_paths(kern_path, pid_ns_ctl_table);
457460
#endif
461+
462+
register_pid_ns_sysctl_table_vm();
458463
return 0;
459464
}
460465

kernel/pid_sysctl.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef LINUX_PID_SYSCTL_H
3+
#define LINUX_PID_SYSCTL_H
4+
5+
#include <linux/pid_namespace.h>
6+
7+
#if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE)
8+
static inline void initialize_memfd_noexec_scope(struct pid_namespace *ns)
9+
{
10+
ns->memfd_noexec_scope =
11+
task_active_pid_ns(current)->memfd_noexec_scope;
12+
}
13+
14+
static int pid_mfd_noexec_dointvec_minmax(struct ctl_table *table,
15+
int write, void *buf, size_t *lenp, loff_t *ppos)
16+
{
17+
struct pid_namespace *ns = task_active_pid_ns(current);
18+
struct ctl_table table_copy;
19+
20+
if (write && !ns_capable(ns->user_ns, CAP_SYS_ADMIN))
21+
return -EPERM;
22+
23+
table_copy = *table;
24+
if (ns != &init_pid_ns)
25+
table_copy.data = &ns->memfd_noexec_scope;
26+
27+
/*
28+
* set minimum to current value, the effect is only bigger
29+
* value is accepted.
30+
*/
31+
if (*(int *)table_copy.data > *(int *)table_copy.extra1)
32+
table_copy.extra1 = table_copy.data;
33+
34+
return proc_dointvec_minmax(&table_copy, write, buf, lenp, ppos);
35+
}
36+
37+
static struct ctl_table pid_ns_ctl_table_vm[] = {
38+
{
39+
.procname = "memfd_noexec",
40+
.data = &init_pid_ns.memfd_noexec_scope,
41+
.maxlen = sizeof(init_pid_ns.memfd_noexec_scope),
42+
.mode = 0644,
43+
.proc_handler = pid_mfd_noexec_dointvec_minmax,
44+
.extra1 = SYSCTL_ZERO,
45+
.extra2 = SYSCTL_TWO,
46+
},
47+
{ }
48+
};
49+
static struct ctl_path vm_path[] = { { .procname = "vm", }, { } };
50+
static inline void register_pid_ns_sysctl_table_vm(void)
51+
{
52+
register_sysctl_paths(vm_path, pid_ns_ctl_table_vm);
53+
}
54+
#else
55+
static inline void initialize_memfd_noexec_scope(struct pid_namespace *ns) {}
56+
static inline void set_memfd_noexec_scope(struct pid_namespace *ns) {}
57+
static inline void register_pid_ns_sysctl_table_vm(void) {}
58+
#endif
59+
60+
#endif /* LINUX_PID_SYSCTL_H */

mm/memfd.c

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/hugetlb.h>
1919
#include <linux/shmem_fs.h>
2020
#include <linux/memfd.h>
21+
#include <linux/pid_namespace.h>
2122
#include <uapi/linux/memfd.h>
2223

2324
/*
@@ -263,12 +264,13 @@ long memfd_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
263264
#define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
264265
#define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
265266

266-
#define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB)
267+
#define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB | MFD_NOEXEC_SEAL | MFD_EXEC)
267268

268269
SYSCALL_DEFINE2(memfd_create,
269270
const char __user *, uname,
270271
unsigned int, flags)
271272
{
273+
char comm[TASK_COMM_LEN];
272274
unsigned int *file_seals;
273275
struct file *file;
274276
int fd, error;
@@ -285,6 +287,40 @@ SYSCALL_DEFINE2(memfd_create,
285287
return -EINVAL;
286288
}
287289

290+
/* Invalid if both EXEC and NOEXEC_SEAL are set.*/
291+
if ((flags & MFD_EXEC) && (flags & MFD_NOEXEC_SEAL))
292+
return -EINVAL;
293+
294+
if (!(flags & (MFD_EXEC | MFD_NOEXEC_SEAL))) {
295+
#ifdef CONFIG_SYSCTL
296+
int sysctl = MEMFD_NOEXEC_SCOPE_EXEC;
297+
struct pid_namespace *ns;
298+
299+
ns = task_active_pid_ns(current);
300+
if (ns)
301+
sysctl = ns->memfd_noexec_scope;
302+
303+
switch (sysctl) {
304+
case MEMFD_NOEXEC_SCOPE_EXEC:
305+
flags |= MFD_EXEC;
306+
break;
307+
case MEMFD_NOEXEC_SCOPE_NOEXEC_SEAL:
308+
flags |= MFD_NOEXEC_SEAL;
309+
break;
310+
default:
311+
pr_warn_once(
312+
"memfd_create(): MFD_NOEXEC_SEAL is enforced, pid=%d '%s'\n",
313+
task_pid_nr(current), get_task_comm(comm, current));
314+
return -EINVAL;
315+
}
316+
#else
317+
flags |= MFD_EXEC;
318+
#endif
319+
pr_warn_once(
320+
"memfd_create() without MFD_EXEC nor MFD_NOEXEC_SEAL, pid=%d '%s'\n",
321+
task_pid_nr(current), get_task_comm(comm, current));
322+
}
323+
288324
/* length includes terminating zero */
289325
len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1);
290326
if (len <= 0)
@@ -328,7 +364,15 @@ SYSCALL_DEFINE2(memfd_create,
328364
file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
329365
file->f_flags |= O_LARGEFILE;
330366

331-
if (flags & MFD_ALLOW_SEALING) {
367+
if (flags & MFD_NOEXEC_SEAL) {
368+
struct inode *inode = file_inode(file);
369+
370+
inode->i_mode &= ~0111;
371+
file_seals = memfd_file_seals_ptr(file);
372+
*file_seals &= ~F_SEAL_SEAL;
373+
*file_seals |= F_SEAL_EXEC;
374+
} else if (flags & MFD_ALLOW_SEALING) {
375+
/* MFD_EXEC and MFD_ALLOW_SEALING are set */
332376
file_seals = memfd_file_seals_ptr(file);
333377
*file_seals &= ~F_SEAL_SEAL;
334378
}

0 commit comments

Comments
 (0)