1 .\" Copyright (C) 2014 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (C) 2014 David Herrmann <dh.herrmann@gmail.com>
4 .\" %%%LICENSE_START(GPLv2+)
6 .\" This program is free software; you can redistribute it and/or modify
7 .\" it under the terms of the GNU General Public License as published by
8 .\" the Free Software Foundation; either version 2 of the License, or
9 .\" (at your option) any later version.
11 .\" This program is distributed in the hope that it will be useful,
12 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
13 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 .\" GNU General Public License for more details.
16 .\" You should have received a copy of the GNU General Public
17 .\" License along with this manual; if not, see
18 .\" <http://www.gnu.org/licenses/>.
21 .TH MEMFD_CREATE 2 2016-10-08 Linux "Linux Programmer's Manual"
23 memfd_create \- create an anonymous file
25 .B #include <sys/memfd.h>
27 .BI "int memfd_create(const char *" name ", unsigned int " flags ");"
30 There is no glibc wrapper for this system call; see NOTES.
33 creates an anonymous file and returns a file descriptor that refers to it.
34 The file behaves like a regular file, and so can be modified,
35 truncated, memory-mapped, and so on.
36 However, unlike a regular file,
37 it lives in RAM and has a volatile backing storage.
38 Once all references to the file are dropped, it is automatically released.
39 Anonymous memory is used for all backing pages of the file.
40 Therefore, files created by
42 have the same semantics as other anonymous
44 .\" memfd uses VM_NORESERVE so each page is accounted on first access.
45 .\" This means, the overcommit-limits (see __vm_enough_memory()) and the
46 .\" memory-cgroup limits (mem_cgroup_try_charge()) are applied. Note that
47 .\" those are accounted on "current" and "current->mm", that is, the
48 .\" process doing the first page access.
49 memory allocations such as those allocated using
55 The initial size of the file is set to 0.
56 Following the call, the file size should be set using
58 (Alternatively, the file may be populated by calls to
64 is used as a filename and will be displayed
65 as the target of the corresponding symbolic link in the directory
67 The displayed name is always prefixed with
69 and serves only for debugging purposes.
70 Names do not affect the behavior of the file descriptor,
71 and as such multiple files can have the same name without any side effects.
73 The following values may be bitwise ORed in
75 to change the behavior of
81 flag on the new file descriptor.
82 See the description of the
86 for reasons why this may be useful.
89 Allow sealing operations on this file.
90 See the discussion of the
96 and also NOTES, below.
97 The initial set of seals is empty.
98 If this flag is not set, the initial set of seals will be
100 meaning that no other seals can be set on the file.
101 .\" FIXME Why is the MFD_ALLOW_SEALING behavior not simply the default?
102 .\" Is it worth adding some text explaining this?
110 returns a new file descriptor that can be used to refer to the file.
111 This file descriptor is opened for both reading and writing
115 is set for the file descriptor.
121 the usual semantics apply for the file descriptor created by
123 A copy of the file descriptor is inherited by the child produced by
125 and refers to the same file.
126 The file descriptor is preserved across
128 unless the close-on-exec flag has been set.
132 returns a new file descriptor.
133 On error, \-1 is returned and
135 is set to indicate the error.
141 points to invalid memory.
144 An unsupported value was specified in one of the arguments:
146 included unknown bits, or
151 The per-process limit on the number of open file descriptors has been reached.
154 The system-wide limit on the total number of open files has been reached.
157 There was insufficient memory to create a new anonymous file.
161 system call first appeared in Linux 3.17.
165 system call is Linux-specific.
167 Glibc does not provide a wrapper for this system call; call it using
170 .\" See also http://lwn.net/Articles/593918/
171 .\" and http://lwn.net/Articles/594919/ and http://lwn.net/Articles/591108/
174 system call provides a simple alternative to manually mounting a
176 filesystem and creating and opening a file in that filesystem.
177 The primary purpose of
179 is to create files and associated file descriptors that are
180 used with the file-sealing APIs provided by
185 system call also has uses without file sealing
186 (which is why file-sealing is disabled, unless explicitly requested with the
187 .BR MFD_ALLOW_SEALING
189 In particular, it can be used as an alternative to creating files in
191 or as an alternative to using the
194 in cases where there is no intention to actually link the
195 resulting file into the filesystem.
197 In the absence of file sealing,
198 processes that communicate via shared memory must either trust each other,
199 or take measures to deal with the possibility that an untrusted peer
200 may manipulate the shared memory region in problematic ways.
201 For example, an untrusted peer might modify the contents of the
202 shared memory at any time, or shrink the shared memory region.
203 The former possibility leaves the local process vulnerable to
204 time-of-check-to-time-of-use race conditions
205 (typically dealt with by copying data from
206 the shared memory region before checking and using it).
207 The latter possibility leaves the local process vulnerable to
209 signals when an attempt is made to access a now-nonexistent
210 location in the shared memory region.
211 (Dealing with this possibility necessitates the use of a handler for the
215 Dealing with untrusted peers imposes extra complexity on
216 code that employs shared memory.
217 Memory sealing enables that extra complexity to be eliminated,
218 by allowing a process to operate secure in the knowledge that
219 its peer can't modify the shared memory in an undesired fashion.
221 An example of the usage of the sealing mechanism is as follows:
223 The first process creates a
227 The call yields a file descriptor used in subsequent steps.
230 sizes the file created in the previous step using
234 and populates the shared memory with the desired data.
236 The first process uses the
239 operation to place one or more seals on the file,
240 in order to restrict further modifications on the file.
243 then it will be necessary to first unmap the shared writable mapping
244 created in the previous step.)
246 A second process obtains a file descriptor for the
249 Among the possible ways in which this could happen are the following:
252 The process that called
254 could transfer the resulting file descriptor to the second process
255 via a UNIX domain socket (see
259 The second process then maps the file using
262 The second process is created via
264 and thus automatically inherits the file descriptor and mapping.
265 (Note that in this case and the next,
266 there is a natural trust relationship between the two processes,
267 since they are running under the same user ID.
268 Therefore, file sealing would not normally be necessary.)
270 The second process opens the file
271 .IR /proc/<pid>/fd/<fd> ,
274 is the PID of the first process (the one that called
275 .BR memfd_create ()),
278 is the number of the file descriptor returned by the call to
281 The second process then maps the file using
285 The second process uses the
288 operation to retrieve the bit mask of seals
289 that has been applied to the file.
290 This bit mask can be inspected in order to determine
291 what kinds of restrictions have been placed on file modifications.
292 If desired, the second process can apply further seals
293 to impose additional restrictions (so long as the
295 seal has not yet been applied).
297 Below are shown two example programs that demonstrate the use of
299 and the file sealing API.
302 .IR t_memfd_create.c ,
307 sets a size for the file, maps it into memory,
308 and optionally places some seals on the file.
309 The program accepts up to three command-line arguments,
310 of which the first two are required.
311 The first argument is the name to associate with the file,
312 the second argument is the size to be set for the file,
313 and the optional third argument is a string of characters that specify
314 seals to be set on file.
318 can be used to open an existing file that was created via
320 and inspect the set of seals that have been applied to that file.
322 The following shell session demonstrates the use of these programs.
325 file and set some seals on it:
329 $ \fB./t_memfd_create my_memfd_file 4096 sw &\fP
331 PID: 11775; fd: 3; /proc/11775/fd/3
337 program continues to run in the background.
338 From another program, we can obtain a file descriptor for the
343 file that corresponds to the file descriptor opened by
345 Using that pathname, we inspect the content of the
347 symbolic link, and use our
349 program to view the seals that have been placed on the file:
353 $ \fBreadlink /proc/11775/fd/3\fP
354 /memfd:my_memfd_file (deleted)
355 $ \fB./t_get_seals /proc/11775/fd/3\fP
356 Existing seals: WRITE SHRINK
359 .SS Program source: t_memfd_create.c
362 #include <sys/memfd.h>
369 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
373 main(int argc, char *argv[])
378 char *name, *seals_arg;
382 fprintf(stderr, "%s name size [seals]\\n", argv[0]);
383 fprintf(stderr, "\\t\(aqseals\(aq can contain any of the "
384 "following characters:\\n");
385 fprintf(stderr, "\\t\\tg \- F_SEAL_GROW\\n");
386 fprintf(stderr, "\\t\\ts \- F_SEAL_SHRINK\\n");
387 fprintf(stderr, "\\t\\tw \- F_SEAL_WRITE\\n");
388 fprintf(stderr, "\\t\\tS \- F_SEAL_SEAL\\n");
396 /* Create an anonymous file in tmpfs; allow seals to be
397 placed on the file */
399 fd = memfd_create(name, MFD_ALLOW_SEALING);
401 errExit("memfd_create");
403 /* Size the file as specified on the command line */
405 if (ftruncate(fd, len) == \-1)
408 printf("PID: %ld; fd: %d; /proc/%ld/fd/%d\\n",
409 (long) getpid(), fd, (long) getpid(), fd);
411 /* Code to map the file and populate the mapping with data
414 /* If a \(aqseals\(aq command\-line argument was supplied, set some
417 if (seals_arg != NULL) {
420 if (strchr(seals_arg, \(aqg\(aq) != NULL)
421 seals |= F_SEAL_GROW;
422 if (strchr(seals_arg, \(aqs\(aq) != NULL)
423 seals |= F_SEAL_SHRINK;
424 if (strchr(seals_arg, \(aqw\(aq) != NULL)
425 seals |= F_SEAL_WRITE;
426 if (strchr(seals_arg, \(aqS\(aq) != NULL)
427 seals |= F_SEAL_SEAL;
429 if (fcntl(fd, F_ADD_SEALS, seals) == \-1)
433 /* Keep running, so that the file created by memfd_create()
434 continues to exist */
441 .SS Program source: t_get_seals.c
444 #include <sys/memfd.h>
451 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
455 main(int argc, char *argv[])
461 fprintf(stderr, "%s /proc/PID/fd/FD\\n", argv[0]);
465 fd = open(argv[1], O_RDWR);
469 seals = fcntl(fd, F_GET_SEALS);
473 printf("Existing seals:");
474 if (seals & F_SEAL_SEAL)
476 if (seals & F_SEAL_GROW)
478 if (seals & F_SEAL_WRITE)
480 if (seals & F_SEAL_SHRINK)
484 /* Code to map the file and access the contents of the
485 resulting mapping omitted */