Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 <string> |
| 18 | #include <unordered_map> |
| 19 | #include <set> |
| 20 | #include <vector> |
| 21 | #include <algorithm> |
| 22 | |
Jakub Adamek | 1c15c63 | 2016-09-23 09:07:11 +0100 | [diff] [blame] | 23 | #include <android-base/strings.h> |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 24 | #include <dirent.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <grp.h> |
| 27 | #include <inttypes.h> |
| 28 | #include <stdlib.h> |
Narayan Kamath | 3764a26 | 2016-08-30 15:36:19 +0100 | [diff] [blame] | 29 | #include <sys/socket.h> |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 30 | #include <sys/stat.h> |
| 31 | #include <sys/types.h> |
Narayan Kamath | 3764a26 | 2016-08-30 15:36:19 +0100 | [diff] [blame] | 32 | #include <sys/un.h> |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 33 | #include <unistd.h> |
| 34 | |
| 35 | #include <cutils/log.h> |
| 36 | #include "JNIHelp.h" |
| 37 | #include "ScopedPrimitiveArray.h" |
| 38 | |
Narayan Kamath | 3764a26 | 2016-08-30 15:36:19 +0100 | [diff] [blame] | 39 | // Whitelist of open paths that the zygote is allowed to keep open. |
| 40 | // |
| 41 | // In addition to the paths listed here, all files ending with |
| 42 | // ".jar" under /system/framework" are whitelisted. See |
| 43 | // FileDescriptorInfo::IsWhitelisted for the canonical definition. |
| 44 | // |
| 45 | // If the whitelisted path is associated with a regular file or a |
| 46 | // character device, the file is reopened after a fork with the same |
| 47 | // offset and mode. If the whilelisted path is associated with a |
| 48 | // AF_UNIX socket, the socket will refer to /dev/null after each |
| 49 | // fork, and all operations on it will fail. |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 50 | static const char* kPathWhitelist[] = { |
| 51 | "/dev/null", |
Narayan Kamath | 3764a26 | 2016-08-30 15:36:19 +0100 | [diff] [blame] | 52 | "/dev/socket/zygote", |
| 53 | "/dev/socket/zygote_secondary", |
Torne (Richard Coles) | 898fd69 | 2016-09-23 16:41:42 +0100 | [diff] [blame] | 54 | "/dev/socket/webview_zygote", |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 55 | "/sys/kernel/debug/tracing/trace_marker", |
| 56 | "/system/framework/framework-res.apk", |
| 57 | "/dev/urandom", |
Adrian Salido | 8977e42 | 2016-08-30 12:51:55 -0700 | [diff] [blame] | 58 | "/dev/ion", |
| 59 | "/dev/dri/renderD129", // Fixes b/31172436 |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | static const char* kFdPath = "/proc/self/fd"; |
| 63 | |
| 64 | // Keeps track of all relevant information (flags, offset etc.) of an |
| 65 | // open zygote file descriptor. |
| 66 | class FileDescriptorInfo { |
| 67 | public: |
| 68 | // Create a FileDescriptorInfo for a given file descriptor. Returns |
| 69 | // |NULL| if an error occurred. |
| 70 | static FileDescriptorInfo* createFromFd(int fd) { |
| 71 | struct stat f_stat; |
| 72 | // This should never happen; the zygote should always have the right set |
| 73 | // of permissions required to stat all its open files. |
| 74 | if (TEMP_FAILURE_RETRY(fstat(fd, &f_stat)) == -1) { |
| 75 | ALOGE("Unable to stat fd %d : %s", fd, strerror(errno)); |
| 76 | return NULL; |
| 77 | } |
| 78 | |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 79 | if (S_ISSOCK(f_stat.st_mode)) { |
Narayan Kamath | 3764a26 | 2016-08-30 15:36:19 +0100 | [diff] [blame] | 80 | std::string socket_name; |
| 81 | if (!GetSocketName(fd, &socket_name)) { |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | if (!IsWhitelisted(socket_name)) { |
| 86 | ALOGE("Socket name not whitelisted : %s (fd=%d)", socket_name.c_str(), fd); |
| 87 | return NULL; |
| 88 | } |
| 89 | |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 90 | return new FileDescriptorInfo(fd); |
| 91 | } |
| 92 | |
| 93 | // We only handle whitelisted regular files and character devices. Whitelisted |
| 94 | // character devices must provide a guarantee of sensible behaviour when |
| 95 | // reopened. |
| 96 | // |
| 97 | // S_ISDIR : Not supported. (We could if we wanted to, but it's unused). |
| 98 | // S_ISLINK : Not supported. |
| 99 | // S_ISBLK : Not supported. |
| 100 | // S_ISFIFO : Not supported. Note that the zygote uses pipes to communicate |
| 101 | // with the child process across forks but those should have been closed |
| 102 | // before we got to this point. |
| 103 | if (!S_ISCHR(f_stat.st_mode) && !S_ISREG(f_stat.st_mode)) { |
| 104 | ALOGE("Unsupported st_mode %d", f_stat.st_mode); |
| 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | std::string file_path; |
| 109 | if (!Readlink(fd, &file_path)) { |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | if (!IsWhitelisted(file_path)) { |
| 114 | ALOGE("Not whitelisted : %s", file_path.c_str()); |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | // File descriptor flags : currently on FD_CLOEXEC. We can set these |
| 119 | // using F_SETFD - we're single threaded at this point of execution so |
| 120 | // there won't be any races. |
| 121 | const int fd_flags = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD)); |
| 122 | if (fd_flags == -1) { |
| 123 | ALOGE("Failed fcntl(%d, F_GETFD) : %s", fd, strerror(errno)); |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | // File status flags : |
| 128 | // - File access mode : (O_RDONLY, O_WRONLY...) we'll pass these through |
| 129 | // to the open() call. |
| 130 | // |
| 131 | // - File creation flags : (O_CREAT, O_EXCL...) - there's not much we can |
| 132 | // do about these, since the file has already been created. We shall ignore |
| 133 | // them here. |
| 134 | // |
| 135 | // - Other flags : We'll have to set these via F_SETFL. On linux, F_SETFL |
| 136 | // can only set O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK. |
| 137 | // In particular, it can't set O_SYNC and O_DSYNC. We'll have to test for |
| 138 | // their presence and pass them in to open(). |
| 139 | int fs_flags = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFL)); |
| 140 | if (fs_flags == -1) { |
| 141 | ALOGE("Failed fcntl(%d, F_GETFL) : %s", fd, strerror(errno)); |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | // File offset : Ignore the offset for non seekable files. |
| 146 | const off_t offset = TEMP_FAILURE_RETRY(lseek64(fd, 0, SEEK_CUR)); |
| 147 | |
| 148 | // We pass the flags that open accepts to open, and use F_SETFL for |
| 149 | // the rest of them. |
| 150 | static const int kOpenFlags = (O_RDONLY | O_WRONLY | O_RDWR | O_DSYNC | O_SYNC); |
| 151 | int open_flags = fs_flags & (kOpenFlags); |
| 152 | fs_flags = fs_flags & (~(kOpenFlags)); |
| 153 | |
| 154 | return new FileDescriptorInfo(f_stat, file_path, fd, open_flags, fd_flags, fs_flags, offset); |
| 155 | } |
| 156 | |
| 157 | // Checks whether the file descriptor associated with this object |
| 158 | // refers to the same description. |
| 159 | bool Restat() const { |
| 160 | struct stat f_stat; |
| 161 | if (TEMP_FAILURE_RETRY(fstat(fd, &f_stat)) == -1) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | return f_stat.st_ino == stat.st_ino && f_stat.st_dev == stat.st_dev; |
| 166 | } |
| 167 | |
Narayan Kamath | 3764a26 | 2016-08-30 15:36:19 +0100 | [diff] [blame] | 168 | bool ReopenOrDetach() const { |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 169 | if (is_sock) { |
Narayan Kamath | 3764a26 | 2016-08-30 15:36:19 +0100 | [diff] [blame] | 170 | return DetachSocket(); |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // NOTE: This might happen if the file was unlinked after being opened. |
| 174 | // It's a common pattern in the case of temporary files and the like but |
| 175 | // we should not allow such usage from the zygote. |
| 176 | const int new_fd = TEMP_FAILURE_RETRY(open(file_path.c_str(), open_flags)); |
| 177 | |
| 178 | if (new_fd == -1) { |
| 179 | ALOGE("Failed open(%s, %d) : %s", file_path.c_str(), open_flags, strerror(errno)); |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | if (TEMP_FAILURE_RETRY(fcntl(new_fd, F_SETFD, fd_flags)) == -1) { |
| 184 | close(new_fd); |
| 185 | ALOGE("Failed fcntl(%d, F_SETFD, %x) : %s", new_fd, fd_flags, strerror(errno)); |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | if (TEMP_FAILURE_RETRY(fcntl(new_fd, F_SETFL, fs_flags)) == -1) { |
| 190 | close(new_fd); |
| 191 | ALOGE("Failed fcntl(%d, F_SETFL, %x) : %s", new_fd, fs_flags, strerror(errno)); |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | if (offset != -1 && TEMP_FAILURE_RETRY(lseek64(new_fd, offset, SEEK_SET)) == -1) { |
| 196 | close(new_fd); |
| 197 | ALOGE("Failed lseek64(%d, SEEK_SET) : %s", new_fd, strerror(errno)); |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | if (TEMP_FAILURE_RETRY(dup2(new_fd, fd)) == -1) { |
| 202 | close(new_fd); |
| 203 | ALOGE("Failed dup2(%d, %d) : %s", fd, new_fd, strerror(errno)); |
| 204 | return false; |
| 205 | } |
| 206 | |
Narayan Kamath | 3764a26 | 2016-08-30 15:36:19 +0100 | [diff] [blame] | 207 | close(new_fd); |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 208 | |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | const int fd; |
| 213 | const struct stat stat; |
| 214 | const std::string file_path; |
| 215 | const int open_flags; |
| 216 | const int fd_flags; |
| 217 | const int fs_flags; |
| 218 | const off_t offset; |
| 219 | const bool is_sock; |
| 220 | |
| 221 | private: |
| 222 | FileDescriptorInfo(int fd) : |
| 223 | fd(fd), |
| 224 | stat(), |
| 225 | open_flags(0), |
| 226 | fd_flags(0), |
| 227 | fs_flags(0), |
| 228 | offset(0), |
| 229 | is_sock(true) { |
| 230 | } |
| 231 | |
| 232 | FileDescriptorInfo(struct stat stat, const std::string& file_path, int fd, int open_flags, |
| 233 | int fd_flags, int fs_flags, off_t offset) : |
| 234 | fd(fd), |
| 235 | stat(stat), |
| 236 | file_path(file_path), |
| 237 | open_flags(open_flags), |
| 238 | fd_flags(fd_flags), |
| 239 | fs_flags(fs_flags), |
| 240 | offset(offset), |
| 241 | is_sock(false) { |
| 242 | } |
| 243 | |
| 244 | // Returns true iff. a given path is whitelisted. A path is whitelisted |
| 245 | // if it belongs to the whitelist (see kPathWhitelist) or if it's a path |
Jakub Adamek | 1c15c63 | 2016-09-23 09:07:11 +0100 | [diff] [blame] | 246 | // under /system/framework that ends with ".jar" or if it is a system |
| 247 | // framework overlay. |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 248 | static bool IsWhitelisted(const std::string& path) { |
| 249 | for (size_t i = 0; i < (sizeof(kPathWhitelist) / sizeof(kPathWhitelist[0])); ++i) { |
| 250 | if (kPathWhitelist[i] == path) { |
| 251 | return true; |
| 252 | } |
| 253 | } |
| 254 | |
Jakub Adamek | 1c15c63 | 2016-09-23 09:07:11 +0100 | [diff] [blame] | 255 | static const char* kFrameworksPrefix = "/system/framework/"; |
| 256 | static const char* kJarSuffix = ".jar"; |
| 257 | if (android::base::StartsWith(path, kFrameworksPrefix) |
| 258 | && android::base::EndsWith(path, kJarSuffix)) { |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 259 | return true; |
| 260 | } |
Jakub Adamek | 1c15c63 | 2016-09-23 09:07:11 +0100 | [diff] [blame] | 261 | |
| 262 | // Whitelist files needed for Runtime Resource Overlay, like these: |
| 263 | // /system/vendor/overlay/framework-res.apk |
Jakub Adamek | c03d948 | 2016-09-30 09:19:09 +0100 | [diff] [blame] | 264 | // /system/vendor/overlay/PG/android-framework-runtime-resource-overlay.apk |
Jakub Adamek | 1c15c63 | 2016-09-23 09:07:11 +0100 | [diff] [blame] | 265 | // /data/resource-cache/system@vendor@overlay@framework-res.apk@idmap |
Jakub Adamek | c03d948 | 2016-09-30 09:19:09 +0100 | [diff] [blame] | 266 | // /data/resource-cache/system@vendor@overlay@PG@framework-res.apk@idmap |
Jakub Adamek | 1c15c63 | 2016-09-23 09:07:11 +0100 | [diff] [blame] | 267 | static const char* kOverlayDir = "/system/vendor/overlay/"; |
Jakub Adamek | 1c15c63 | 2016-09-23 09:07:11 +0100 | [diff] [blame] | 268 | static const char* kApkSuffix = ".apk"; |
| 269 | |
Jakub Adamek | c03d948 | 2016-09-30 09:19:09 +0100 | [diff] [blame] | 270 | if (android::base::StartsWith(path, kOverlayDir) |
Jakub Adamek | 1c15c63 | 2016-09-23 09:07:11 +0100 | [diff] [blame] | 271 | && android::base::EndsWith(path, kApkSuffix) |
| 272 | && path.find("/../") == std::string::npos) { |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | static const char* kOverlayIdmapPrefix = "/data/resource-cache/"; |
| 277 | static const char* kOverlayIdmapSuffix = ".apk@idmap"; |
| 278 | if (android::base::StartsWith(path, kOverlayIdmapPrefix) |
| 279 | && android::base::EndsWith(path, kOverlayIdmapSuffix)) { |
| 280 | return true; |
| 281 | } |
| 282 | |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 283 | return false; |
| 284 | } |
| 285 | |
| 286 | // TODO: Call android::base::Readlink instead of copying the code here. |
| 287 | static bool Readlink(const int fd, std::string* result) { |
| 288 | char path[64]; |
| 289 | snprintf(path, sizeof(path), "/proc/self/fd/%d", fd); |
| 290 | |
| 291 | // Code copied from android::base::Readlink starts here : |
| 292 | |
| 293 | // Annoyingly, the readlink system call returns EINVAL for a zero-sized buffer, |
| 294 | // and truncates to whatever size you do supply, so it can't be used to query. |
| 295 | // We could call lstat first, but that would introduce a race condition that |
| 296 | // we couldn't detect. |
| 297 | // ext2 and ext4 both have PAGE_SIZE limitations, so we assume that here. |
| 298 | char buf[4096]; |
| 299 | ssize_t len = readlink(path, buf, sizeof(buf)); |
| 300 | if (len == -1) return false; |
| 301 | |
| 302 | result->assign(buf, len); |
| 303 | return true; |
| 304 | } |
| 305 | |
Narayan Kamath | 3764a26 | 2016-08-30 15:36:19 +0100 | [diff] [blame] | 306 | // Returns the locally-bound name of the socket |fd|. Returns true |
| 307 | // iff. all of the following hold : |
| 308 | // |
| 309 | // - the socket's sa_family is AF_UNIX. |
| 310 | // - the length of the path is greater than zero (i.e, not an unnamed socket). |
| 311 | // - the first byte of the path isn't zero (i.e, not a socket with an abstract |
| 312 | // address). |
| 313 | static bool GetSocketName(const int fd, std::string* result) { |
| 314 | sockaddr_storage ss; |
| 315 | sockaddr* addr = reinterpret_cast<sockaddr*>(&ss); |
| 316 | socklen_t addr_len = sizeof(ss); |
| 317 | |
| 318 | if (TEMP_FAILURE_RETRY(getsockname(fd, addr, &addr_len)) == -1) { |
| 319 | ALOGE("Failed getsockname(%d) : %s", fd, strerror(errno)); |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | if (addr->sa_family != AF_UNIX) { |
| 324 | ALOGE("Unsupported socket (fd=%d) with family %d", fd, addr->sa_family); |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | const sockaddr_un* unix_addr = reinterpret_cast<const sockaddr_un*>(&ss); |
| 329 | |
| 330 | size_t path_len = addr_len - offsetof(struct sockaddr_un, sun_path); |
| 331 | // This is an unnamed local socket, we do not accept it. |
| 332 | if (path_len == 0) { |
| 333 | ALOGE("Unsupported AF_UNIX socket (fd=%d) with empty path.", fd); |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | // This is a local socket with an abstract address, we do not accept it. |
| 338 | if (unix_addr->sun_path[0] == '\0') { |
| 339 | ALOGE("Unsupported AF_UNIX socket (fd=%d) with abstract address.", fd); |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | // If we're here, sun_path must refer to a null terminated filesystem |
| 344 | // pathname (man 7 unix). Remove the terminator before assigning it to an |
| 345 | // std::string. |
| 346 | if (unix_addr->sun_path[path_len - 1] == '\0') { |
| 347 | --path_len; |
| 348 | } |
| 349 | |
| 350 | result->assign(unix_addr->sun_path, path_len); |
| 351 | return true; |
| 352 | } |
| 353 | |
| 354 | bool DetachSocket() const { |
| 355 | const int dev_null_fd = open("/dev/null", O_RDWR); |
| 356 | if (dev_null_fd < 0) { |
| 357 | ALOGE("Failed to open /dev/null : %s", strerror(errno)); |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | if (dup2(dev_null_fd, fd) == -1) { |
| 362 | ALOGE("Failed dup2 on socket descriptor %d : %s", fd, strerror(errno)); |
| 363 | return false; |
| 364 | } |
| 365 | |
| 366 | if (close(dev_null_fd) == -1) { |
| 367 | ALOGE("Failed close(%d) : %s", dev_null_fd, strerror(errno)); |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | return true; |
| 372 | } |
| 373 | |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 374 | DISALLOW_COPY_AND_ASSIGN(FileDescriptorInfo); |
| 375 | }; |
| 376 | |
| 377 | // A FileDescriptorTable is a collection of FileDescriptorInfo objects |
| 378 | // keyed by their FDs. |
| 379 | class FileDescriptorTable { |
| 380 | public: |
| 381 | // Creates a new FileDescriptorTable. This function scans |
| 382 | // /proc/self/fd for the list of open file descriptors and collects |
| 383 | // information about them. Returns NULL if an error occurs. |
| 384 | static FileDescriptorTable* Create() { |
| 385 | DIR* d = opendir(kFdPath); |
| 386 | if (d == NULL) { |
| 387 | ALOGE("Unable to open directory %s: %s", kFdPath, strerror(errno)); |
| 388 | return NULL; |
| 389 | } |
| 390 | int dir_fd = dirfd(d); |
| 391 | dirent* e; |
| 392 | |
| 393 | std::unordered_map<int, FileDescriptorInfo*> open_fd_map; |
| 394 | while ((e = readdir(d)) != NULL) { |
| 395 | const int fd = ParseFd(e, dir_fd); |
| 396 | if (fd == -1) { |
| 397 | continue; |
| 398 | } |
| 399 | |
| 400 | FileDescriptorInfo* info = FileDescriptorInfo::createFromFd(fd); |
| 401 | if (info == NULL) { |
| 402 | if (closedir(d) == -1) { |
| 403 | ALOGE("Unable to close directory : %s", strerror(errno)); |
| 404 | } |
| 405 | return NULL; |
| 406 | } |
| 407 | open_fd_map[fd] = info; |
| 408 | } |
| 409 | |
| 410 | if (closedir(d) == -1) { |
| 411 | ALOGE("Unable to close directory : %s", strerror(errno)); |
| 412 | return NULL; |
| 413 | } |
| 414 | return new FileDescriptorTable(open_fd_map); |
| 415 | } |
| 416 | |
| 417 | bool Restat() { |
| 418 | std::set<int> open_fds; |
| 419 | |
| 420 | // First get the list of open descriptors. |
| 421 | DIR* d = opendir(kFdPath); |
| 422 | if (d == NULL) { |
| 423 | ALOGE("Unable to open directory %s: %s", kFdPath, strerror(errno)); |
| 424 | return false; |
| 425 | } |
| 426 | |
| 427 | int dir_fd = dirfd(d); |
| 428 | dirent* e; |
| 429 | while ((e = readdir(d)) != NULL) { |
| 430 | const int fd = ParseFd(e, dir_fd); |
| 431 | if (fd == -1) { |
| 432 | continue; |
| 433 | } |
| 434 | |
| 435 | open_fds.insert(fd); |
| 436 | } |
| 437 | |
| 438 | if (closedir(d) == -1) { |
| 439 | ALOGE("Unable to close directory : %s", strerror(errno)); |
| 440 | return false; |
| 441 | } |
| 442 | |
| 443 | return RestatInternal(open_fds); |
| 444 | } |
| 445 | |
| 446 | // Reopens all file descriptors that are contained in the table. Returns true |
Narayan Kamath | 3764a26 | 2016-08-30 15:36:19 +0100 | [diff] [blame] | 447 | // if all descriptors were successfully re-opened or detached, and false if an |
| 448 | // error occurred. |
| 449 | bool ReopenOrDetach() { |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 450 | std::unordered_map<int, FileDescriptorInfo*>::const_iterator it; |
| 451 | for (it = open_fd_map_.begin(); it != open_fd_map_.end(); ++it) { |
| 452 | const FileDescriptorInfo* info = it->second; |
Narayan Kamath | 3764a26 | 2016-08-30 15:36:19 +0100 | [diff] [blame] | 453 | if (info == NULL || !info->ReopenOrDetach()) { |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 454 | return false; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | return true; |
| 459 | } |
| 460 | |
| 461 | private: |
| 462 | FileDescriptorTable(const std::unordered_map<int, FileDescriptorInfo*>& map) |
| 463 | : open_fd_map_(map) { |
| 464 | } |
| 465 | |
| 466 | bool RestatInternal(std::set<int>& open_fds) { |
| 467 | bool error = false; |
| 468 | |
| 469 | // Iterate through the list of file descriptors we've already recorded |
| 470 | // and check whether : |
| 471 | // |
| 472 | // (a) they continue to be open. |
| 473 | // (b) they refer to the same file. |
Narayan Kamath | 0b76d6a | 2016-09-07 13:14:40 +0100 | [diff] [blame] | 474 | std::unordered_map<int, FileDescriptorInfo*>::iterator it = open_fd_map_.begin(); |
| 475 | while (it != open_fd_map_.end()) { |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 476 | std::set<int>::const_iterator element = open_fds.find(it->first); |
| 477 | if (element == open_fds.end()) { |
| 478 | // The entry from the file descriptor table is no longer in the list |
| 479 | // of open files. We warn about this condition and remove it from |
| 480 | // the list of FDs under consideration. |
| 481 | // |
| 482 | // TODO(narayan): This will be an error in a future android release. |
| 483 | // error = true; |
Narayan Kamath | 3764a26 | 2016-08-30 15:36:19 +0100 | [diff] [blame] | 484 | // ALOGW("Zygote closed file descriptor %d.", it->first); |
Narayan Kamath | 0b76d6a | 2016-09-07 13:14:40 +0100 | [diff] [blame] | 485 | it = open_fd_map_.erase(it); |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 486 | } else { |
| 487 | // The entry from the file descriptor table is still open. Restat |
| 488 | // it and check whether it refers to the same file. |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 489 | const bool same_file = it->second->Restat(); |
| 490 | if (!same_file) { |
| 491 | // The file descriptor refers to a different description. We must |
| 492 | // update our entry in the table. |
| 493 | delete it->second; |
| 494 | it->second = FileDescriptorInfo::createFromFd(*element); |
| 495 | if (it->second == NULL) { |
| 496 | // The descriptor no longer no longer refers to a whitelisted file. |
| 497 | // We flag an error and remove it from the list of files we're |
| 498 | // tracking. |
| 499 | error = true; |
Narayan Kamath | 0b76d6a | 2016-09-07 13:14:40 +0100 | [diff] [blame] | 500 | it = open_fd_map_.erase(it); |
| 501 | } else { |
| 502 | // Successfully restatted the file, move on to the next open FD. |
| 503 | ++it; |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 504 | } |
| 505 | } else { |
Narayan Kamath | 0b76d6a | 2016-09-07 13:14:40 +0100 | [diff] [blame] | 506 | // It's the same file. Nothing to do here. Move on to the next open |
| 507 | // FD. |
| 508 | ++it; |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 509 | } |
Narayan Kamath | 0b76d6a | 2016-09-07 13:14:40 +0100 | [diff] [blame] | 510 | |
| 511 | // Finally, remove the FD from the set of open_fds. We do this last because |
| 512 | // |element| will not remain valid after a call to erase. |
| 513 | open_fds.erase(element); |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
| 517 | if (open_fds.size() > 0) { |
| 518 | // The zygote has opened new file descriptors since our last inspection. |
| 519 | // We warn about this condition and add them to our table. |
| 520 | // |
| 521 | // TODO(narayan): This will be an error in a future android release. |
| 522 | // error = true; |
Narayan Kamath | 3764a26 | 2016-08-30 15:36:19 +0100 | [diff] [blame] | 523 | // ALOGW("Zygote opened %zd new file descriptor(s).", open_fds.size()); |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 524 | |
| 525 | // TODO(narayan): This code will be removed in a future android release. |
| 526 | std::set<int>::const_iterator it; |
| 527 | for (it = open_fds.begin(); it != open_fds.end(); ++it) { |
| 528 | const int fd = (*it); |
| 529 | FileDescriptorInfo* info = FileDescriptorInfo::createFromFd(fd); |
| 530 | if (info == NULL) { |
| 531 | // A newly opened file is not on the whitelist. Flag an error and |
| 532 | // continue. |
| 533 | error = true; |
| 534 | } else { |
| 535 | // Track the newly opened file. |
| 536 | open_fd_map_[fd] = info; |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | return !error; |
| 542 | } |
| 543 | |
| 544 | static int ParseFd(dirent* e, int dir_fd) { |
| 545 | char* end; |
| 546 | const int fd = strtol(e->d_name, &end, 10); |
| 547 | if ((*end) != '\0') { |
| 548 | return -1; |
| 549 | } |
| 550 | |
| 551 | // Don't bother with the standard input/output/error, they're handled |
| 552 | // specially post-fork anyway. |
| 553 | if (fd <= STDERR_FILENO || fd == dir_fd) { |
| 554 | return -1; |
| 555 | } |
| 556 | |
| 557 | return fd; |
| 558 | } |
| 559 | |
| 560 | // Invariant: All values in this unordered_map are non-NULL. |
| 561 | std::unordered_map<int, FileDescriptorInfo*> open_fd_map_; |
| 562 | |
| 563 | DISALLOW_COPY_AND_ASSIGN(FileDescriptorTable); |
| 564 | }; |