Man pages sections > man2 > memfd_create

memfd_create - create an anonymous file

MEMFD_CREATE(2) Linux Programmer's Manual MEMFD_CREATE(2)


memfd_create - create an anonymous file


#include <sys/memfd.h>


Allow sealing operations on this file. See the discussion of the F_ADD_SEALS and F_GET_SEALS operations in fcntl(2), and also NOTES, below. The initial set of seals is empty. If this flag is not set, the initial set of seals will be F_SEAL_SEAL, meaning that no other seals can be set on the file.
Unused bits in flags must be 0.




The address in name points to invalid memory.
An unsupported value was specified in one of the arguments: flags included unknown bits, or name was too long.
There was insufficient memory to create a new anonymous file.


The memfd_create() system call first appeared in Linux 3.17.


The memfd_create() system call is Linux-specific.



File sealing




$  ./t_memfd_create my_memfd_file 4096 sw &
[1] 11775


$  readlink /proc/11775/fd/3
/memfd:my_memfd_file (deleted)
$  ./t_get_seals /proc/11775/fd/3
Existing seals: WRITE SHRINK

Program source: t_memfd_create.c

#include <sys/memfd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
int main(int argc, char *argv[]) { int fd; unsigned int seals; char *addr; char *name, *seals_arg; ssize_t len;
if (argc < 3) { fprintf(stderr, "%s name size [seals]\n", argv[0]); fprintf(stderr, "\t'seals' can contain any of the " "following characters:\n"); fprintf(stderr, "\t\tg - F_SEAL_GROW\n"); fprintf(stderr, "\t\ts - F_SEAL_SHRINK\n"); fprintf(stderr, "\t\tw - F_SEAL_WRITE\n"); fprintf(stderr, "\t\tS - F_SEAL_SEAL\n"); exit(EXIT_FAILURE); }
name = argv[1]; len = atoi(argv[2]); seals_arg = argv[3];
/* Create an anonymous file in tmpfs; allow seals to be placed on the file */
fd = memfd_create(name, MFD_ALLOW_SEALING); if (fd == -1) errExit("memfd_create");
if (ftruncate(fd, len) == -1) errExit("truncate");
printf("PID: %ld; fd: %d; /proc/%ld/fd/%d\n", (long) getpid(), fd, (long) getpid(), fd);
/* Code to map the file and populate the mapping with data omitted */
/* If a 'seals' command-line argument was supplied, set some seals on the file */
if (seals_arg != NULL) { seals = 0;
if (strchr(seals_arg, 'g') != NULL) seals |= F_SEAL_GROW; if (strchr(seals_arg, 's') != NULL) seals |= F_SEAL_SHRINK; if (strchr(seals_arg, 'w') != NULL) seals |= F_SEAL_WRITE; if (strchr(seals_arg, 'S') != NULL) seals |= F_SEAL_SEAL;
if (fcntl(fd, F_ADD_SEALS, seals) == -1) errExit("fcntl"); }
/* Keep running, so that the file created by memfd_create() continues to exist */

Program source: t_get_seals.c

#include <sys/memfd.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
if (argc != 2) { fprintf(stderr, "%s /proc/PID/fd/FD\n", argv[0]); exit(EXIT_FAILURE); }
fd = open(argv[1], O_RDWR); if (fd == -1) errExit("open");
seals = fcntl(fd, F_GET_SEALS); if (seals == -1) errExit("fcntl");
printf("Existing seals:"); if (seals & F_SEAL_SEAL) printf(" SEAL"); if (seals & F_SEAL_GROW) printf(" GROW"); if (seals & F_SEAL_WRITE) printf(" WRITE"); if (seals & F_SEAL_SHRINK) printf(" SHRINK"); printf("\n");
/* Code to map the file and access the contents of the resulting mapping omitted */




2017-09-15 Linux