Skip to content

Commit 8fe3262

Browse files
committed
Add fallback for systems without memfd_create()
Signed-off-by: Christopher Desiniotis <[email protected]>
1 parent ff83efb commit 8fe3262

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

src/nvc_ldcache.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static int limit_resources(struct error *);
4242
static int limit_syscalls(struct error *);
4343
static ssize_t sendfile_nointr(int, int, off_t *, size_t);
4444
static int open_as_memfd(struct error *, const char *);
45+
int memfd_create(const char *, unsigned int);
4546

4647

4748
static inline bool
@@ -294,7 +295,9 @@ limit_syscalls(struct error *err)
294295
SCMP_SYS(_llseek),
295296
SCMP_SYS(lseek),
296297
SCMP_SYS(lstat),
298+
#ifdef SYS_memfd_create
297299
SCMP_SYS(memfd_create),
300+
#endif
298301
SCMP_SYS(mkdir),
299302
SCMP_SYS(mmap),
300303
SCMP_SYS(mprotect),
@@ -361,6 +364,38 @@ limit_syscalls(struct error *err)
361364
}
362365
#endif /* WITH_SECCOMP */
363366

367+
/* memfd_create(2) flags -- copied from <linux/memfd.h>. */
368+
#ifndef MFD_CLOEXEC
369+
# define MFD_CLOEXEC 0x0001U
370+
# define MFD_ALLOW_SEALING 0x0002U
371+
#endif
372+
#ifndef MFD_EXEC
373+
# define MFD_EXEC 0x0010U
374+
#endif
375+
376+
/* This comes directly from <linux/fcntl.h>. */
377+
#ifndef F_LINUX_SPECIFIC_BASE
378+
# define F_LINUX_SPECIFIC_BASE 1024
379+
#endif
380+
#ifndef F_ADD_SEALS
381+
# define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
382+
#endif
383+
#ifndef F_SEAL_SEAL
384+
# define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
385+
# define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
386+
# define F_SEAL_GROW 0x0004 /* prevent file from growing */
387+
# define F_SEAL_WRITE 0x0008 /* prevent writes */
388+
#endif
389+
390+
int memfd_create(const char *name, unsigned int flags)
391+
{
392+
#ifdef SYS_memfd_create
393+
return syscall(SYS_memfd_create, name, flags);
394+
#else
395+
errno = ENOSYS;
396+
return -1;
397+
#endif
398+
}
364399

365400
static ssize_t
366401
sendfile_nointr(int out_fd, int in_fd, off_t *offset, size_t count)
@@ -444,7 +479,9 @@ nvc_ldcache_update(struct nvc_context *ctx, const struct nvc_container *cnt)
444479
*/
445480
++argv[0];
446481
if ((fd = open_as_memfd(&ctx->err, argv[0])) < 0)
447-
return (-1);
482+
log_warn("failed to create virtual copy of the ldconfig binary");
483+
if ((fd = xopen(&ctx->err, argv[0], O_RDONLY|O_CLOEXEC)) < 0)
484+
return (-1);
448485
host_ldconfig = true;
449486
log_infof("executing %s from host at %s", argv[0], cnt->cfg.rootfs);
450487
} else {

0 commit comments

Comments
 (0)