]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/memfd_secret.2
termios.3: CIBAUD and IBSHIFT are implemented on Linux, just unsupported by glibc
[thirdparty/man-pages.git] / man2 / memfd_secret.2
CommitLineData
ac5edfeb
MR
1.\" Copyright (c) 2021, IBM Corporation.
2.\" Written by Mike Rapoport <rppt@linux.ibm.com>
3.\"
4.\" Based on memfd_create(2) man page
5.\" Copyright (C) 2014 Michael Kerrisk <mtk.manpages@gmail.com>
6.\" and Copyright (C) 2014 David Herrmann <dh.herrmann@gmail.com>
7.\"
8.\" %%%LICENSE_START(GPLv2+)
9.\"
10.\" This program is free software; you can redistribute it and/or modify
11.\" it under the terms of the GNU General Public License as published by
12.\" the Free Software Foundation; either version 2 of the License, or
13.\" (at your option) any later version.
14.\"
15.\" This program is distributed in the hope that it will be useful,
16.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
17.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18.\" GNU General Public License for more details.
19.\"
20.\" You should have received a copy of the GNU General Public
21.\" License along with this manual; if not, see
22.\" <http://www.gnu.org/licenses/>.
23.\" %%%LICENSE_END
24.\"
25.TH MEMFD_SECRET 2 2020-08-02 Linux "Linux Programmer's Manual"
26.SH NAME
27memfd_secret \- create an anonymous RAM-based file
28to access secret memory regions
29.SH SYNOPSIS
30.nf
31.PP
32.BR "#include <sys/syscall.h>" " /* Definition of " SYS_* " constants */"
33.B #include <unistd.h>
34.PP
35.BI "int syscall(SYS_memfd_secret, unsigned int " flags );
36.fi
37.PP
38.IR Note :
39glibc provides no wrapper for
40.BR memfd_secret (),
41necessitating the use of
42.BR syscall (2).
43.SH DESCRIPTION
44.BR memfd_secret ()
eabb03a4
MK
45creates an anonymous RAM-based file and returns a file descriptor
46that refers to it.
ac5edfeb
MR
47The file provides a way to create and access memory regions
48with stronger protection than usual RAM-based files and
49anonymous memory mappings.
50Once all open references to the file are closed,
51it is automatically released.
52The initial size of the file is set to 0.
53Following the call, the file size should be set using
54.BR ftruncate (2).
55.PP
56The memory areas backing the file created with
84a2ce0f 57.BR memfd_secret (2)
ac5edfeb
MR
58are visible only to the processes that have access to the file descriptor.
59The memory region is removed from the kernel page tables
60and only the page tables of the processes holding the file descriptor
61map the corresponding physical memory.
62(Thus, the pages in the region can't be accessed by the kernel itself,
63so that, for example, pointers to the region can't be passed to
64system calls.)
65.PP
66The following values may be bitwise ORed in
67.I flags
68to control the behavior of
84a2ce0f 69.BR memfd_secret ():
ac5edfeb
MR
70.TP
71.B FD_CLOEXEC
72Set the close-on-exec flag on the new file descriptor,
73which causes the region to be removed from the process on
74.BR execve (2).
75See the description of the
76.B O_CLOEXEC
77flag in
78.BR open (2)
79.PP
80As its return value,
81.BR memfd_secret ()
82returns a new file descriptor that refers to an anonymous file.
83This file descriptor is opened for both reading and writing
84.RB ( O_RDWR )
85and
86.B O_LARGEFILE
87is set for the file descriptor.
88.PP
89With respect to
90.BR fork (2)
91and
92.BR execve (2),
93the usual semantics apply for the file descriptor created by
94.BR memfd_secret ().
95A copy of the file descriptor is inherited by the child produced by
96.BR fork (2)
97and refers to the same file.
98The file descriptor is preserved across
99.BR execve (2),
100unless the close-on-exec flag has been set.
101.PP
102The memory region is locked into memory in the same way as with
103.BR mlock (2),
104so that it will never be written into swap.
105However the implementation of
84a2ce0f 106.BR memfd_secret ()
ac5edfeb
MR
107will not try to populate the whole range during the
108.BR mmap (2)
109call that attaches the region into the process's address space;
110instead, the pages are only actually allocated
111as they are faulted in.
112The amount of memory allowed for memory mappings
113of the file descriptor obeys the same rules as
114.BR mlock (2)
115and cannot exceed
116.BR RLIMIT_MEMLOCK .
117.SH RETURN VALUE
118On success,
84a2ce0f 119.BR memfd_secret ()
ac5edfeb
MR
120returns a new file descriptor.
121On error, \-1 is returned and
122.I errno
123is set to indicate the error.
124.SH ERRORS
125.TP
126.B EINVAL
127.I flags
128included unknown bits.
129.TP
130.B EMFILE
131The per-process limit on the number of open file descriptors has been reached.
132.TP
133.B EMFILE
134The system-wide limit on the total number of open files has been reached.
135.TP
136.B ENOMEM
137There was insufficient memory to create a new anonymous file.
138.TP
139.B ENOSYS
140.BR memfd_secret ()
141is not implemented on this architecture.
142.SH VERSIONS
143The
84a2ce0f 144.BR memfd_secret ()
ac5edfeb
MR
145system call first appeared in Linux 5.14.
146.SH CONFORMING TO
147The
84a2ce0f 148.BR memfd_secret ()
ac5edfeb
MR
149system call is Linux-specific.
150.SH SEE ALSO
151.BR fcntl (2),
152.BR ftruncate (2),
153.BR mlock (2),
d5ee9f93 154.BR memfd_create (2),
ac5edfeb
MR
155.BR mmap (2),
156.BR setrlimit (2)