2017-03-02 08:02:18 +00:00
|
|
|
#ifndef _PUPY_MEMFD_H
|
|
|
|
#define _PUPY_MEMFD_H
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <sys/syscall.h>
|
2017-03-06 16:36:29 +00:00
|
|
|
#include <string.h>
|
2017-03-07 21:22:25 +00:00
|
|
|
#include <stdbool.h>
|
2018-10-08 21:02:34 +00:00
|
|
|
#include <errno.h>
|
2019-05-07 12:15:10 +00:00
|
|
|
#include <stdio.h>
|
2019-07-11 15:58:33 +00:00
|
|
|
#include <unistd.h>
|
2017-03-02 08:02:18 +00:00
|
|
|
|
2019-07-11 15:58:33 +00:00
|
|
|
#ifndef MFD_CLOEXEC
|
|
|
|
#define MFD_CLOEXEC 0x0001U
|
|
|
|
#define MFD_ALLOW_SEALING 0x0002U
|
|
|
|
#endif
|
2017-03-02 08:02:18 +00:00
|
|
|
|
|
|
|
#ifndef __NR_memfd_create
|
|
|
|
#ifdef __x86_64__
|
|
|
|
#define __NR_memfd_create 319
|
|
|
|
#elif __i386__
|
|
|
|
#define __NR_memfd_create 356
|
2020-01-05 17:04:30 +00:00
|
|
|
#elif __arm__
|
|
|
|
#define __NR_memfd_create 385
|
2017-03-02 08:02:18 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2017-03-06 05:43:24 +00:00
|
|
|
#ifndef F_ADD_SEALS
|
2019-07-11 15:58:33 +00:00
|
|
|
#define F_ADD_SEALS (1024 + 9)
|
|
|
|
#define F_SEAL_SEAL 0x0001
|
|
|
|
#define F_SEAL_SHRINK 0x0002
|
|
|
|
#define F_SEAL_GROW 0x0004
|
|
|
|
#define F_SEAL_WRITE 0x0008
|
2017-03-06 05:43:24 +00:00
|
|
|
#endif
|
|
|
|
|
2018-02-03 18:49:54 +00:00
|
|
|
#define PROCFS_PATH "/proc/"
|
|
|
|
#define MEMFD_FILE_PATH PROCFS_PATH "%d/fd/"
|
2017-03-06 05:43:24 +00:00
|
|
|
|
2019-12-31 12:18:34 +00:00
|
|
|
static bool memfd_checked = false;
|
|
|
|
static bool memfd_works = true;
|
|
|
|
|
|
|
|
inline static bool pupy_memfd_supported()
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (memfd_checked)
|
|
|
|
return memfd_works;
|
|
|
|
|
|
|
|
fd = syscall(__NR_memfd_create, "check", MFD_CLOEXEC | MFD_ALLOW_SEALING);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return fd != -1;
|
|
|
|
}
|
|
|
|
|
2017-03-02 08:02:18 +00:00
|
|
|
inline static int pupy_memfd_create(char *path, unsigned int path_size)
|
|
|
|
{
|
2018-10-08 21:02:34 +00:00
|
|
|
#ifdef Linux
|
|
|
|
|
2019-12-31 12:18:34 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
#define _path path
|
|
|
|
#else
|
|
|
|
char _path[PATH_MAX] = "libc.so.6";
|
2017-04-05 05:49:51 +00:00
|
|
|
#endif
|
|
|
|
|
2018-10-08 21:02:34 +00:00
|
|
|
/* Do not make syscall billion times */
|
2019-12-31 12:18:34 +00:00
|
|
|
if (memfd_checked && !memfd_works) {
|
|
|
|
errno = ENOSYS;
|
2018-10-08 21:02:34 +00:00
|
|
|
return -1;
|
2019-12-31 12:18:34 +00:00
|
|
|
}
|
2018-10-08 21:02:34 +00:00
|
|
|
|
2019-12-31 12:18:34 +00:00
|
|
|
int fd = syscall(__NR_memfd_create, _path, MFD_CLOEXEC | MFD_ALLOW_SEALING);
|
2017-04-03 20:35:33 +00:00
|
|
|
|
2017-03-16 21:01:13 +00:00
|
|
|
if (fd == -1) {
|
2019-12-31 12:18:34 +00:00
|
|
|
if (errno == ENOSYS)
|
|
|
|
memfd_works = false;
|
2018-10-08 21:02:34 +00:00
|
|
|
|
2019-12-31 12:18:34 +00:00
|
|
|
return -1;
|
2017-03-16 21:01:13 +00:00
|
|
|
}
|
2017-03-02 08:02:18 +00:00
|
|
|
|
2018-02-03 18:49:54 +00:00
|
|
|
snprintf(path, path_size, MEMFD_FILE_PATH "%d", getpid(), fd);
|
2017-03-16 21:01:13 +00:00
|
|
|
return fd;
|
2018-10-08 21:02:34 +00:00
|
|
|
#else
|
2019-07-11 15:58:33 +00:00
|
|
|
return -1;
|
2018-10-08 21:02:34 +00:00
|
|
|
#endif
|
2017-03-02 08:02:18 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 05:43:24 +00:00
|
|
|
inline static bool is_memfd_path(const char *path)
|
|
|
|
{
|
2019-05-07 12:15:10 +00:00
|
|
|
return !strncmp(path, PROCFS_PATH, strlen(PROCFS_PATH)) && \
|
|
|
|
strstr(path, "/fd/");
|
2017-03-06 05:43:24 +00:00
|
|
|
}
|
2017-03-02 08:02:18 +00:00
|
|
|
#endif
|