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 #include "memfd.h"
18 
19 #include <errno.h>
20 #include <stdio.h>
21 #if !defined(_WIN32)
22 #include <fcntl.h>
23 #include <sys/syscall.h>
24 #include <sys/utsname.h>
25 #include <unistd.h>
26 #endif
27 #if defined(__BIONIC__)
28 #include <linux/memfd.h>  // To access memfd flags.
29 #endif
30 
31 #include <android-base/logging.h>
32 #include <android-base/unique_fd.h>
33 
34 #include "macros.h"
35 
36 namespace art {
37 
38 #if defined(__NR_memfd_create)
39 
memfd_create(const char * name,unsigned int flags)40 int memfd_create(const char* name, unsigned int flags) {
41   // Check kernel version supports memfd_create(). Some older kernels segfault executing
42   // memfd_create() rather than returning ENOSYS (b/116769556).
43   static constexpr int kRequiredMajor = 3;
44   static constexpr int kRequiredMinor = 17;
45   struct utsname uts;
46   int major, minor;
47   if (uname(&uts) != 0 ||
48       strcmp(uts.sysname, "Linux") != 0 ||
49       sscanf(uts.release, "%d.%d", &major, &minor) != 2 ||
50       (major < kRequiredMajor || (major == kRequiredMajor && minor < kRequiredMinor))) {
51     errno = ENOSYS;
52     return -1;
53   }
54 
55   return syscall(__NR_memfd_create, name, flags);
56 }
57 
58 #else  // __NR_memfd_create
59 
60 int memfd_create([[maybe_unused]] const char* name, [[maybe_unused]] unsigned int flags) {
61   errno = ENOSYS;
62   return -1;
63 }
64 
65 #endif  // __NR_memfd_create
66 
67 // This is a wrapper that will attempt to simulate memfd_create if normal running fails.
memfd_create_compat(const char * name,unsigned int flags)68 int memfd_create_compat(const char* name, unsigned int flags) {
69   int res = memfd_create(name, flags);
70   if (res >= 0) {
71     return res;
72   }
73 #if !defined(_WIN32)
74   // Try to create an anonymous file with tmpfile that we can use instead.
75   if (flags == 0) {
76     FILE* file = tmpfile();
77     if (file != nullptr) {
78       // We want the normal 'dup' semantics since memfd_create without any flags isn't CLOEXEC.
79       // Unfortunately on some android targets we will compiler error if we use dup directly and so
80       // need to use fcntl.
81       int nfd = fcntl(fileno(file), F_DUPFD, /*lowest allowed fd*/ 0);
82       fclose(file);
83       return nfd;
84     }
85   }
86 #endif
87   return res;
88 }
89 
90 #if defined(__BIONIC__)
91 
IsSealFutureWriteSupportedInternal()92 static bool IsSealFutureWriteSupportedInternal() {
93   android::base::unique_fd fd(art::memfd_create("test_android_memfd", MFD_ALLOW_SEALING));
94   if (fd == -1) {
95     LOG(INFO) << "memfd_create failed: " << strerror(errno) << ", no memfd support.";
96     return false;
97   }
98 
99   if (fcntl(fd, F_ADD_SEALS, F_SEAL_FUTURE_WRITE) == -1) {
100     LOG(INFO) << "fcntl(F_ADD_SEALS) failed: " << strerror(errno) << ", no memfd support.";
101     return false;
102   }
103 
104   LOG(INFO) << "Using memfd for future sealing";
105   return true;
106 }
107 
IsSealFutureWriteSupported()108 bool IsSealFutureWriteSupported() {
109   static bool is_seal_future_write_supported = IsSealFutureWriteSupportedInternal();
110   return is_seal_future_write_supported;
111 }
112 
113 #else
114 
IsSealFutureWriteSupported()115 bool IsSealFutureWriteSupported() {
116   return false;
117 }
118 
119 #endif
120 
121 }  // namespace art
122