Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 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 | |
Nicolas Geoffray | e3884e3 | 2019-10-28 17:04:49 +0000 | [diff] [blame] | 20 | #include <fcntl.h> |
| 21 | #include <unistd.h> |
| 22 | |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 23 | #if defined(__BIONIC__) |
| 24 | #include <linux/memfd.h> // To access memfd flags. |
Nicolas Geoffray | e3884e3 | 2019-10-28 17:04:49 +0000 | [diff] [blame] | 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 | |
Hu Wang | 3326fce | 2021-08-23 17:41:57 +0800 | [diff] [blame] | 56 | #ifndef MFD_CLOEXEC |
| 57 | # define MFD_CLOEXEC 0x0001U |
| 58 | #endif |
| 59 | |
Nicolas Geoffray | e3884e3 | 2019-10-28 17:04:49 +0000 | [diff] [blame] | 60 | #ifndef MFD_ALLOW_SEALING |
| 61 | # define MFD_ALLOW_SEALING 0x0002U |
| 62 | #endif |
| 63 | |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 64 | #endif |
| 65 | |
Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 66 | namespace art { |
| 67 | |
Orion Hodson | 51f89d9 | 2018-09-27 12:51:41 +0100 | [diff] [blame] | 68 | // Call memfd(2) if available on platform and return result. This call also makes a kernel version |
| 69 | // check for safety on older kernels (b/116769556).. |
Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 70 | int memfd_create(const char* name, unsigned int flags); |
| 71 | |
Alex Light | 639e73b | 2019-05-17 21:44:36 +0000 | [diff] [blame] | 72 | // Call memfd(2) if available on platform and return result. Try to give us an unlinked FD in some |
| 73 | // other way if memfd fails or isn't supported. |
| 74 | int memfd_create_compat(const char* name, unsigned int flags); |
| 75 | |
Nicolas Geoffray | 3a614ea | 2019-06-27 15:47:09 +0100 | [diff] [blame] | 76 | // Return whether the kernel supports sealing future writes of a memfd. |
| 77 | bool IsSealFutureWriteSupported(); |
| 78 | |
Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 79 | } // namespace art |
| 80 | |
| 81 | #endif // ART_LIBARTBASE_BASE_MEMFD_H_ |