#define _GNU_SOURCE
#define __EXPORTED_HEADERS__
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <linux/falloc.h>
#include <linux/fcntl.h>
#include <linux/memfd.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <unistd.h>
#define MFD_DEF_SIZE 8192
#define STACK_SIZE 65535
static int sys_memfd_create(const char *name,
unsigned int flags)
{
return syscall(__NR_memfd_create, name, flags);
}
static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags)
{
int r, fd;
fd = sys_memfd_create(name, flags);
if (fd < 0) {
printf("memfd_create(\"%s\", %u) failed: %m\n",
name, flags);
abort();
}
r = ftruncate(fd, sz);
if (r < 0) {
printf("ftruncate(%llu) failed: %m\n", (unsigned long long)sz);
abort();
}
return fd;
}
static void mfd_fail_new(const char *name, unsigned int flags)
{
int r;
r = sys_memfd_create(name, flags);
if (r >= 0) {
printf("memfd_create(\"%s\", %u) succeeded, but failure expected\n",
name, flags);
close(r);
abort();
}
}
static unsigned int mfd_assert_get_seals(int fd)
{
int r;
r = fcntl(fd, F_GET_SEALS);
if (r < 0) {
printf("GET_SEALS(%d) failed: %m\n", fd);
abort();
}
return (unsigned int)r;
}
static void mfd_assert_has_seals(int fd, unsigned int seals)
{
unsigned int s;
s = mfd_assert_get_seals(fd);
if (s != seals) {
printf("%u != %u = GET_SEALS(%d)\n", seals, s, fd);
abort();
}
}
static void mfd_assert_add_seals(int fd, unsigned int seals)
{
int r;
unsigned int s;
s = mfd_assert_get_seals(fd);
r = fcntl(fd, F_ADD_SEALS, seals);
if (r < 0) {
printf("ADD_SEALS(%d, %u -> %u) failed: %m\n", fd, s, seals);
abort();
}
}
static void mfd_fail_add_seals(int fd, unsigned int seals)
{
int r;
unsigned int s;
r = fcntl(fd, F_GET_SEALS);
if (r < 0)
s = 0;
else
s = (unsigned int)r;
r = fcntl(fd, F_ADD_SEALS, seals);
if (r >= 0) {
printf("ADD_SEALS(%d, %u -> %u) didn't fail as expected\n",
fd, s, seals);
abort();
}
}
static void mfd_assert_size(int fd, size_t size)
{
struct stat st;
int r;
r = fstat(fd, &st);
if (r < 0) {
printf("fstat(%d) failed: %m\n", fd);
abort();
} else if (st.st_size != size) {
printf("wrong file size %lld, but expected %lld\n",
(long long)st.st_size, (long long)size);
abort();
}
}
static int mfd_assert_dup(int fd)
{
int r;
r = dup(fd);
if (r < 0) {
printf("dup(%d) failed: %m\n", fd);
abort();
}
return r;
}
static void *mfd_assert_mmap_shared(int fd)
{
void *p;
p = mmap(NULL,
MFD_DEF_SIZE,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fd,
0);
if (p == MAP_FAILED) {
printf("mmap() failed: %m\n");
abort();
}
return p;
}
static void *mfd_assert_mmap_private(int fd)
{
void *p;
p = mmap(NULL,
MFD_DEF_SIZE,
PROT_READ,
MAP_PRIVATE,
fd,
0);
if (p == MAP_FAILED) {
printf("mmap() failed: %m\n");
abort();
}
return p;
}
static int mfd_assert_open(int fd, int flags, mode_t mode)
{
char buf[512];
int r;
sprintf(buf, "/proc/self/fd/%d", fd);
r = open(buf, flags, mode);
if (r < 0) {
printf("open(%s) failed: %m\n", buf);
abort();
}
return r;
}
static void mfd_fail_open(int fd, int flags, mode_t mode)
{
char buf[512];
int r;
sprintf(buf, "/proc/self/fd/%d", fd);
r = open(buf, flags, mode);
if (r >= 0) {
printf("open(%s) didn't fail as expected\n", buf);
abort();
}
}
static void mfd_assert_read(int fd)
{
char buf[16];
void *p;
ssize_t l;
l = read(fd, buf, sizeof(buf));
if (l != sizeof(buf)) {
printf("read() failed: %m\n");
abort();
}
/* verify PROT_READ *is* allowed */
p = mmap(NULL,
MFD_DEF_SIZE,
PROT_READ,
MAP_PRIVATE,
fd,
0);
if (p == MAP_FAILED) {
printf("mmap() failed: %m\n");
abort();
}
munmap(p, MFD_DEF_SIZE);
/* verify MAP_PRIVATE is *always* allowed (even writable) */
p = mmap(NULL,
MFD_DEF_SIZE,
PROT_READ | PROT_WRITE,
MAP_PRIVATE,
fd,
0);
if (p == MAP_FAILED) {
printf("mmap() failed: %m\n");
abort();
}
munmap(p, MFD_DEF_SIZE);
}
static void mfd_assert_write(
|