1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_LIBARTBASE_BASE_MEMFD_H_
18 #define ART_LIBARTBASE_BASE_MEMFD_H_
19 
20 #include <fcntl.h>
21 #include <unistd.h>
22 
23 #if defined(__BIONIC__)
24 #include <linux/memfd.h>  // To access memfd flags.
25 #else
26 
27 // If memfd flags don't exist in the current toolchain, define them ourselves.
28 #ifndef F_ADD_SEALS
29 # define F_ADD_SEALS          (1033)
30 #endif
31 
32 #ifndef F_GET_SEALS
33 # define F_GET_SEALS          (1034)
34 #endif
35 
36 #ifndef F_SEAL_SEAL
37 # define F_SEAL_SEAL          0x0001
38 #endif
39 
40 #ifndef F_SEAL_SHRINK
41 # define F_SEAL_SHRINK        0x0002
42 #endif
43 
44 #ifndef F_SEAL_GROW
45 # define F_SEAL_GROW          0x0004
46 #endif
47 
48 #ifndef F_SEAL_WRITE
49 # define F_SEAL_WRITE         0x0008
50 #endif
51 
52 #ifndef F_SEAL_FUTURE_WRITE
53 # define F_SEAL_FUTURE_WRITE  0x0010
54 #endif
55 
56 #ifndef MFD_ALLOW_SEALING
57 # define MFD_ALLOW_SEALING    0x0002U
58 #endif
59 
60 #endif
61 
62 namespace art {
63 
64 // Call memfd(2) if available on platform and return result. This call also makes a kernel version
65 // check for safety on older kernels (b/116769556)..
66 int memfd_create(const char* name, unsigned int flags);
67 
68 // Call memfd(2) if available on platform and return result. Try to give us an unlinked FD in some
69 // other way if memfd fails or isn't supported.
70 int memfd_create_compat(const char* name, unsigned int flags);
71 
72 // Return whether the kernel supports sealing future writes of a memfd.
73 bool IsSealFutureWriteSupported();
74 
75 }  // namespace art
76 
77 #endif  // ART_LIBARTBASE_BASE_MEMFD_H_
78