Skip to content

Commit 11291df

Browse files
committed
memfd_secret: add memfd_secret file support
See "man 2 memfd_secret". Fixes: checkpoint-restore#2188 Signed-off-by: Dhanuka Warusadura <[email protected]>
1 parent 4ca4628 commit 11291df

22 files changed

+628
-6
lines changed

criu/Makefile.crtools

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ obj-y += log.o
4040
obj-y += lsm.o
4141
obj-y += mem.o
4242
obj-y += memfd.o
43+
obj-y += memfd-secret.o
4344
obj-y += mount.o
4445
obj-y += mount-v2.o
4546
obj-y += filesystems.o

criu/cr-check.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,14 @@ static int check_memfd_hugetlb(void)
13401340
return 0;
13411341
}
13421342

1343+
static int check_memfd_secret(void)
1344+
{
1345+
if (!kdat.has_memfd_secret)
1346+
return -1;
1347+
1348+
return 0;
1349+
}
1350+
13431351
static int check_network_lock_nftables(void)
13441352
{
13451353
if (!kdat.has_nftables_concat) {
@@ -1502,6 +1510,7 @@ int cr_check(void)
15021510
ret |= check_openat2();
15031511
ret |= check_ptrace_get_rseq_conf();
15041512
ret |= check_ipv6_freebind();
1513+
ret |= check_memfd_secret();
15051514

15061515
if (kdat.lsm == LSMTYPE__APPARMOR)
15071516
ret |= check_apparmor_stacking();
@@ -1623,6 +1632,7 @@ static struct feature_list feature_list[] = {
16231632
{ "openat2", check_openat2 },
16241633
{ "get_rseq_conf", check_ptrace_get_rseq_conf },
16251634
{ "ipv6_freebind", check_ipv6_freebind },
1635+
{ "memfd_secret", check_memfd_secret },
16261636
{ NULL, NULL },
16271637
};
16281638

criu/cr-restore.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
#include "timens.h"
8080
#include "bpfmap.h"
8181
#include "apparmor.h"
82+
#include "memfd-secret.h"
8283

8384
#include "parasite-syscall.h"
8485
#include "files-reg.h"
@@ -279,7 +280,7 @@ static struct collect_image_info *cinfos_files[] = {
279280
&unix_sk_cinfo, &fifo_cinfo, &pipe_cinfo, &nsfile_cinfo, &packet_sk_cinfo,
280281
&netlink_sk_cinfo, &eventfd_cinfo, &epoll_cinfo, &epoll_tfd_cinfo, &signalfd_cinfo,
281282
&tunfile_cinfo, &timerfd_cinfo, &inotify_cinfo, &inotify_mark_cinfo, &fanotify_cinfo,
282-
&fanotify_mark_cinfo, &ext_file_cinfo, &memfd_cinfo,
283+
&fanotify_mark_cinfo, &ext_file_cinfo, &memfd_cinfo, &memfd_secret_cinfo,
283284
};
284285

285286
/* These images are required to restore namespaces */

criu/files.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include "kerndat.h"
5050
#include "fdstore.h"
5151
#include "bpfmap.h"
52+
#include "memfd-secret.h"
5253

5354
#include "protobuf.h"
5455
#include "util.h"
@@ -563,6 +564,9 @@ static int dump_one_file(struct pid *pid, int fd, int lfd, struct fd_opts *opts,
563564
/* TODO: Dump for hugetlb fd when memfd hugetlb is not supported */
564565
if (is_memfd(p.stat.st_dev) || (kdat.has_memfd_hugetlb && is_hugetlb_dev(p.stat.st_dev, NULL)))
565566
ops = &memfd_dump_ops;
567+
/* memfd_secret */
568+
else if (is_memfd_secret(p.stat.st_dev) && kdat.has_memfd_secret)
569+
ops = &memfd_secret_dump_ops;
566570
else if (link.name[1] == '/')
567571
ops = &regfile_dump_ops;
568572
else if (check_ns_proc(&link))
@@ -1778,6 +1782,9 @@ static int collect_one_file(void *o, ProtobufCMessage *base, struct cr_img *i)
17781782
case FD_TYPES__MEMFD:
17791783
ret = collect_one_file_entry(fe, fe->memfd->id, &fe->memfd->base, &memfd_cinfo);
17801784
break;
1785+
case FD_TYPES__MEMFD_SECRET:
1786+
ret = collect_one_file_entry(fe, fe->memfd_secret->id, &fe->memfd_secret->base, &memfd_secret_cinfo);
1787+
break;
17811788
#ifdef CONFIG_HAS_LIBBPF
17821789
case FD_TYPES__BPFMAP:
17831790
ret = collect_one_file_entry(fe, fe->bpf->id, &fe->bpf->base, &bpfmap_cinfo);

criu/image-desc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct cr_fd_desc_tmpl imgset_template[CR_FD_MAX] = {
2929
FD_ENTRY(FDINFO, "fdinfo-%u"),
3030
FD_ENTRY(PAGEMAP, "pagemap-%lu"),
3131
FD_ENTRY(SHMEM_PAGEMAP, "pagemap-shmem-%lu"),
32+
FD_ENTRY(SECRETMEM_PAGEMAP, "pagemap-secretmem-%lu"),
3233
FD_ENTRY(REG_FILES, "reg-files"),
3334
FD_ENTRY(EXT_FILES, "ext-files"),
3435
FD_ENTRY(NS_FILES, "ns-files"),
@@ -67,6 +68,7 @@ struct cr_fd_desc_tmpl imgset_template[CR_FD_MAX] = {
6768
FD_ENTRY(REMAP_FPATH, "remap-fpath"),
6869
FD_ENTRY_F(GHOST_FILE, "ghost-file-%x", O_NOBUF),
6970
FD_ENTRY_F(MEMFD_INODE, "memfd", O_NOBUF),
71+
FD_ENTRY_F(MEMFD_SECRET_INODE, "memfd-secret", O_NOBUF),
7072
FD_ENTRY(TCP_STREAM, "tcp-stream-%x"),
7173
FD_ENTRY(MNTS, "mountpoints-%u"),
7274
FD_ENTRY(NETDEV, "netdev-%u"),

criu/include/image-desc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ enum {
5252

5353
CR_FD_PSTREE,
5454
CR_FD_SHMEM_PAGEMAP,
55+
CR_FD_SECRETMEM_PAGEMAP,
5556
CR_FD_GHOST_FILE,
5657
CR_FD_TCP_STREAM,
5758
CR_FD_FDINFO,
@@ -69,6 +70,7 @@ enum {
6970
CR_FD_SECCOMP,
7071
CR_FD_APPARMOR,
7172
CR_FD_MEMFD_INODE,
73+
CR_FD_MEMFD_SECRET_INODE,
7274
CR_FD_BPFMAP_FILE,
7375
CR_FD_BPFMAP_DATA,
7476
_CR_FD_GLOB_TO,
@@ -113,6 +115,7 @@ enum {
113115
CR_FD_PIPES,
114116
CR_FD_TTY_FILES,
115117
CR_FD_MEMFD_FILE,
118+
CR_FD_MEMFD_SECRET_FILE,
116119

117120
CR_FD_AUTOFS,
118121

criu/include/image.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
#define VMA_AREA_VVAR (1 << 12)
8585
#define VMA_AREA_AIORING (1 << 13)
8686
#define VMA_AREA_MEMFD (1 << 14)
87+
#define VMA_AREA_MEMFD_SECRET (1 << 15)
8788

8889
#define VMA_EXT_PLUGIN (1 << 27)
8990
#define VMA_CLOSE (1 << 28)

criu/include/kerndat.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ enum loginuid_func {
3434
struct kerndat_s {
3535
u32 magic1, magic2;
3636
dev_t shmem_dev;
37+
dev_t secretmem_dev;
3738
int last_cap;
3839
u64 zero_page_pfn;
3940
bool has_dirty_track;
4041
bool has_memfd;
4142
bool has_memfd_hugetlb;
43+
bool has_memfd_secret;
4244
bool has_fdinfo_lock;
4345
unsigned long task_size;
4446
bool ipv6;

criu/include/magic.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#define FDINFO_MAGIC 0x56213732 /* Dmitrov */
3838
#define PAGEMAP_MAGIC 0x56084025 /* Vladimir */
3939
#define SHMEM_PAGEMAP_MAGIC PAGEMAP_MAGIC
40+
#define SECRETMEM_PAGEMAP_MAGIC PAGEMAP_MAGIC
4041
#define PAGES_MAGIC RAW_IMAGE_MAGIC
4142
#define CORE_MAGIC 0x55053847 /* Kolomna */
4243
#define IDS_MAGIC 0x54432030 /* Konigsberg */
@@ -95,6 +96,7 @@
9596
#define AUTOFS_MAGIC 0x49353943 /* Sochi */
9697
#define FILES_MAGIC 0x56303138 /* Toropets */
9798
#define MEMFD_INODE_MAGIC 0x48453499 /* Dnipro */
99+
#define MEMFD_SECRET_INODE_MAGIC 0x44573468 /* Simferopol */
98100
#define TIMENS_MAGIC 0x43114433 /* Beslan */
99101
#define PIDNS_MAGIC 0x61157326 /* Surgut */
100102
#define BPFMAP_FILE_MAGIC 0x57506142 /* Alapayevsk */

criu/include/memfd-secret.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef __CR_MEMFD_SECRET_H__
2+
#define __CR_MEMFD_SECRET_H__
3+
4+
#include <sys/types.h>
5+
#include <sys/syscall.h>
6+
#include <unistd.h>
7+
#include <errno.h>
8+
9+
#include "common/config.h"
10+
11+
extern int is_memfd_secret(dev_t dev);
12+
extern const struct fdtype_ops memfd_secret_dump_ops;
13+
extern struct collect_image_info memfd_secret_cinfo;
14+
15+
static inline int memfd_secret(unsigned int flags)
16+
{
17+
#ifdef __NR_memfd_secret
18+
return syscall(__NR_memfd_secret, flags);
19+
#else
20+
errno = ENOSYS;
21+
return -1;
22+
#endif /* __NR_memfd_secret */
23+
}
24+
25+
#endif /* __CR_MEMFD_SECRET_H__ */

0 commit comments

Comments
 (0)