1 .\" Copyright (C) 2014 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (C) 2014 David Herrmann <dh.herrmann@gmail.com>
4 .\" SPDX-License-Identifier: GPL-2.0-or-later
6 .TH MEMFD_CREATE 2 2022-10-08 "Linux man-pages (unreleased)"
8 memfd_create \- create an anonymous file
11 .RI ( libc ", " \-lc )
14 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
15 .B #include <sys/mman.h>
17 .BI "int memfd_create(const char *" name ", unsigned int " flags ");"
21 creates an anonymous file and returns a file descriptor that refers to it.
22 The file behaves like a regular file, and so can be modified,
23 truncated, memory-mapped, and so on.
24 However, unlike a regular file,
25 it lives in RAM and has a volatile backing storage.
26 Once all references to the file are dropped, it is automatically released.
27 Anonymous memory is used for all backing pages of the file.
28 Therefore, files created by
30 have the same semantics as other anonymous
32 .\" memfd uses VM_NORESERVE so each page is accounted on first access.
33 .\" This means, the overcommit-limits (see __vm_enough_memory()) and the
34 .\" memory-cgroup limits (mem_cgroup_try_charge()) are applied. Note that
35 .\" those are accounted on "current" and "current->mm", that is, the
36 .\" process doing the first page access.
37 memory allocations such as those allocated using
43 The initial size of the file is set to 0.
44 Following the call, the file size should be set using
46 (Alternatively, the file may be populated by calls to
52 is used as a filename and will be displayed
53 as the target of the corresponding symbolic link in the directory
55 The displayed name is always prefixed with
57 and serves only for debugging purposes.
58 Names do not affect the behavior of the file descriptor,
59 and as such multiple files can have the same name without any side effects.
61 The following values may be bitwise ORed in
63 to change the behavior of
69 flag on the new file descriptor.
70 See the description of the
74 for reasons why this may be useful.
77 Allow sealing operations on this file.
78 See the discussion of the
84 and also NOTES, below.
85 The initial set of seals is empty.
86 If this flag is not set, the initial set of seals will be
88 meaning that no other seals can be set on the file.
89 .\" FIXME Why is the MFD_ALLOW_SEALING behavior not simply the default?
90 .\" Is it worth adding some text explaining this?
92 .BR MFD_HUGETLB " (since Linux 4.14)"
93 .\" commit 749df87bd7bee5a79cef073f5d032ddb2b211de8
94 The anonymous file will be created in the hugetlbfs filesystem using
96 See the Linux kernel source file
97 .I Documentation/admin\-guide/mm/hugetlbpage.rst
98 for more information about hugetlbfs.
99 .\" commit 47b9012ecdc747f6936395265e677d41e11a31ff
106 is supported since Linux 4.16.
108 .BR MFD_HUGE_2MB ", " MFD_HUGE_1GB ", " "..."
109 Used in conjunction with
111 to select alternative hugetlb page sizes (respectively, 2\ MB, 1\ GB, ...)
112 on systems that support multiple hugetlb page sizes.
113 Definitions for known
114 huge page sizes are included in the header file
117 For details on encoding huge page sizes not included in the header file,
118 see the discussion of the similarly named constants in
127 returns a new file descriptor that can be used to refer to the file.
128 This file descriptor is opened for both reading and writing
132 is set for the file descriptor.
138 the usual semantics apply for the file descriptor created by
140 A copy of the file descriptor is inherited by the child produced by
142 and refers to the same file.
143 The file descriptor is preserved across
145 unless the close-on-exec flag has been set.
149 returns a new file descriptor.
150 On error, \-1 is returned and
152 is set to indicate the error.
158 points to invalid memory.
162 included unknown bits.
168 .\" NAME_MAX - strlen("memfd:")
169 249 bytes, excluding the terminating null byte.)
180 The per-process limit on the number of open file descriptors has been reached.
183 The system-wide limit on the total number of open files has been reached.
186 There was insufficient memory to create a new anonymous file.
190 system call first appeared in Linux 3.17;
191 glibc support was added in version 2.27.
196 flag was specified, but the caller was not privileged (did not have the
199 and is not a member of the
200 .I sysctl_hugetlb_shm_group
201 group; see the description of
202 .I /proc/sys/vm/sysctl_hugetlb_shm_group
208 system call is Linux-specific.
210 .\" See also http://lwn.net/Articles/593918/
211 .\" and http://lwn.net/Articles/594919/ and http://lwn.net/Articles/591108/
214 system call provides a simple alternative to manually mounting a
216 filesystem and creating and opening a file in that filesystem.
217 The primary purpose of
219 is to create files and associated file descriptors that are
220 used with the file-sealing APIs provided by
225 system call also has uses without file sealing
226 (which is why file-sealing is disabled, unless explicitly requested with the
229 In particular, it can be used as an alternative to creating files in
231 or as an alternative to using the
234 in cases where there is no intention to actually link the
235 resulting file into the filesystem.
237 In the absence of file sealing,
238 processes that communicate via shared memory must either trust each other,
239 or take measures to deal with the possibility that an untrusted peer
240 may manipulate the shared memory region in problematic ways.
241 For example, an untrusted peer might modify the contents of the
242 shared memory at any time, or shrink the shared memory region.
243 The former possibility leaves the local process vulnerable to
244 time-of-check-to-time-of-use race conditions
245 (typically dealt with by copying data from
246 the shared memory region before checking and using it).
247 The latter possibility leaves the local process vulnerable to
249 signals when an attempt is made to access a now-nonexistent
250 location in the shared memory region.
251 (Dealing with this possibility necessitates the use of a handler for the
255 Dealing with untrusted peers imposes extra complexity on
256 code that employs shared memory.
257 Memory sealing enables that extra complexity to be eliminated,
258 by allowing a process to operate secure in the knowledge that
259 its peer can't modify the shared memory in an undesired fashion.
261 An example of the usage of the sealing mechanism is as follows:
263 The first process creates a
267 The call yields a file descriptor used in subsequent steps.
270 sizes the file created in the previous step using
274 and populates the shared memory with the desired data.
276 The first process uses the
279 operation to place one or more seals on the file,
280 in order to restrict further modifications on the file.
283 then it will be necessary to first unmap the shared writable mapping
284 created in the previous step.
285 Otherwise, behavior similar to
287 can be achieved by using
288 .BR F_SEAL_FUTURE_WRITE ,
289 which will prevent future writes via
293 from succeeding while keeping existing shared writable mappings).
295 A second process obtains a file descriptor for the
298 Among the possible ways in which this could happen are the following:
301 The process that called
303 could transfer the resulting file descriptor to the second process
304 via a UNIX domain socket (see
308 The second process then maps the file using
311 The second process is created via
313 and thus automatically inherits the file descriptor and mapping.
314 (Note that in this case and the next,
315 there is a natural trust relationship between the two processes,
316 since they are running under the same user ID.
317 Therefore, file sealing would not normally be necessary.)
319 The second process opens the file
320 .IR /proc/<pid>/fd/<fd> ,
323 is the PID of the first process (the one that called
324 .BR memfd_create ()),
327 is the number of the file descriptor returned by the call to
330 The second process then maps the file using
334 The second process uses the
337 operation to retrieve the bit mask of seals
338 that has been applied to the file.
339 This bit mask can be inspected in order to determine
340 what kinds of restrictions have been placed on file modifications.
341 If desired, the second process can apply further seals
342 to impose additional restrictions (so long as the
344 seal has not yet been applied).
346 Below are shown two example programs that demonstrate the use of
348 and the file sealing API.
351 .IR t_memfd_create.c ,
356 sets a size for the file, maps it into memory,
357 and optionally places some seals on the file.
358 The program accepts up to three command-line arguments,
359 of which the first two are required.
360 The first argument is the name to associate with the file,
361 the second argument is the size to be set for the file,
362 and the optional third argument is a string of characters that specify
363 seals to be set on the file.
367 can be used to open an existing file that was created via
369 and inspect the set of seals that have been applied to that file.
371 The following shell session demonstrates the use of these programs.
374 file and set some seals on it:
378 $ \fB./t_memfd_create my_memfd_file 4096 sw &\fP
380 PID: 11775; fd: 3; /proc/11775/fd/3
386 program continues to run in the background.
387 From another program, we can obtain a file descriptor for the
392 file that corresponds to the file descriptor opened by
394 Using that pathname, we inspect the content of the
396 symbolic link, and use our
398 program to view the seals that have been placed on the file:
402 $ \fBreadlink /proc/11775/fd/3\fP
403 /memfd:my_memfd_file (deleted)
404 $ \fB./t_get_seals /proc/11775/fd/3\fP
405 Existing seals: WRITE SHRINK
408 .SS Program source: t_memfd_create.c
410 .\" SRC BEGIN (t_memfd_create.c)
419 #include <sys/mman.h>
423 main(int argc, char *argv[])
426 char *name, *seals_arg;
431 fprintf(stderr, "%s name size [seals]\en", argv[0]);
432 fprintf(stderr, "\et\(aqseals\(aq can contain any of the "
433 "following characters:\en");
434 fprintf(stderr, "\et\etg \- F_SEAL_GROW\en");
435 fprintf(stderr, "\et\ets \- F_SEAL_SHRINK\en");
436 fprintf(stderr, "\et\etw \- F_SEAL_WRITE\en");
437 fprintf(stderr, "\et\etW \- F_SEAL_FUTURE_WRITE\en");
438 fprintf(stderr, "\et\etS \- F_SEAL_SEAL\en");
446 /* Create an anonymous file in tmpfs; allow seals to be
447 placed on the file. */
449 fd = memfd_create(name, MFD_ALLOW_SEALING);
451 err(EXIT_FAILURE, "memfd_create");
453 /* Size the file as specified on the command line. */
455 if (ftruncate(fd, len) == \-1)
456 err(EXIT_FAILURE, "truncate");
458 printf("PID: %jd; fd: %d; /proc/%jd/fd/%d\en",
459 (intmax_t) getpid(), fd, (intmax_t) getpid(), fd);
461 /* Code to map the file and populate the mapping with data
464 /* If a \(aqseals\(aq command\-line argument was supplied, set some
465 seals on the file. */
467 if (seals_arg != NULL) {
470 if (strchr(seals_arg, \(aqg\(aq) != NULL)
471 seals |= F_SEAL_GROW;
472 if (strchr(seals_arg, \(aqs\(aq) != NULL)
473 seals |= F_SEAL_SHRINK;
474 if (strchr(seals_arg, \(aqw\(aq) != NULL)
475 seals |= F_SEAL_WRITE;
476 if (strchr(seals_arg, \(aqW\(aq) != NULL)
477 seals |= F_SEAL_FUTURE_WRITE;
478 if (strchr(seals_arg, \(aqS\(aq) != NULL)
479 seals |= F_SEAL_SEAL;
481 if (fcntl(fd, F_ADD_SEALS, seals) == \-1)
482 err(EXIT_FAILURE, "fcntl");
485 /* Keep running, so that the file created by memfd_create()
486 continues to exist. */
494 .SS Program source: t_get_seals.c
496 .\" SRC BEGIN (t_get_seals.c)
505 main(int argc, char *argv[])
511 fprintf(stderr, "%s /proc/PID/fd/FD\en", argv[0]);
515 fd = open(argv[1], O_RDWR);
517 err(EXIT_FAILURE, "open");
519 seals = fcntl(fd, F_GET_SEALS);
521 err(EXIT_FAILURE, "fcntl");
523 printf("Existing seals:");
524 if (seals & F_SEAL_SEAL)
526 if (seals & F_SEAL_GROW)
528 if (seals & F_SEAL_WRITE)
530 if (seals & F_SEAL_FUTURE_WRITE)
531 printf(" FUTURE_WRITE");
532 if (seals & F_SEAL_SHRINK)
536 /* Code to map the file and access the contents of the
537 resulting mapping omitted. */
546 .BR memfd_secret (2),