blob: fa5f931470b03e86faf13799e532269f170431bc [file] [log] [blame]
Robert Sesek8225b7c2016-12-16 14:02:31 -05001/*
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 "fd_utils.h"
18
19#include <algorithm>
20
21#include <fcntl.h>
22#include <grp.h>
23#include <stdlib.h>
24#include <sys/socket.h>
25#include <sys/types.h>
26#include <sys/un.h>
27#include <unistd.h>
28
Narayan Kamath3879ecc2017-03-02 17:30:03 +000029#include <android-base/file.h>
Narayan Kamatha352d242017-03-02 14:44:45 +000030#include <android-base/logging.h>
Narayan Kamath3879ecc2017-03-02 17:30:03 +000031#include <android-base/stringprintf.h>
Robert Sesek8225b7c2016-12-16 14:02:31 -050032#include <android-base/strings.h>
Robert Sesek8225b7c2016-12-16 14:02:31 -050033
Robert Sesek54e387d2016-12-02 17:27:50 -050034// Static whitelist of open paths that the zygote is allowed to keep open.
Robert Sesek8225b7c2016-12-16 14:02:31 -050035static const char* kPathWhitelist[] = {
Adam Vartanian25095752019-01-08 11:00:40 +000036 "/apex/com.android.conscrypt/javalib/conscrypt.jar",
Dongwon Kang0d035532019-01-17 13:35:03 -080037 "/apex/com.android.media/javalib/updatable-media.jar",
Robert Sesek8225b7c2016-12-16 14:02:31 -050038 "/dev/null",
39 "/dev/socket/zygote",
40 "/dev/socket/zygote_secondary",
Chris Wailes7e797b62019-02-22 18:29:22 -080041 "/dev/socket/usap_pool_primary",
42 "/dev/socket/usap_pool_secondary",
Robert Sesek8225b7c2016-12-16 14:02:31 -050043 "/dev/socket/webview_zygote",
Florian Mayer6ce2d992018-10-16 14:30:02 +010044 "/dev/socket/heapprofd",
Robert Sesek8225b7c2016-12-16 14:02:31 -050045 "/sys/kernel/debug/tracing/trace_marker",
46 "/system/framework/framework-res.apk",
47 "/dev/urandom",
48 "/dev/ion",
49 "/dev/dri/renderD129", // Fixes b/31172436
50};
51
52static const char kFdPath[] = "/proc/self/fd";
53
54// static
Robert Sesek54e387d2016-12-02 17:27:50 -050055FileDescriptorWhitelist* FileDescriptorWhitelist::Get() {
56 if (instance_ == nullptr) {
57 instance_ = new FileDescriptorWhitelist();
58 }
59 return instance_;
60}
61
62bool FileDescriptorWhitelist::IsAllowed(const std::string& path) const {
63 // Check the static whitelist path.
64 for (const auto& whitelist_path : kPathWhitelist) {
65 if (path == whitelist_path)
66 return true;
67 }
68
69 // Check any paths added to the dynamic whitelist.
70 for (const auto& whitelist_path : whitelist_) {
71 if (path == whitelist_path)
72 return true;
73 }
74
Nicolas Geoffrayfca69e92019-01-22 20:56:44 +000075 // Framework jars are allowed.
Narayan Kamath3879ecc2017-03-02 17:30:03 +000076 static const char* kFrameworksPrefix = "/system/framework/";
77 static const char* kJarSuffix = ".jar";
78 if (android::base::StartsWith(path, kFrameworksPrefix)
79 && android::base::EndsWith(path, kJarSuffix)) {
Robert Sesek54e387d2016-12-02 17:27:50 -050080 return true;
81 }
82
Nicolas Geoffrayfca69e92019-01-22 20:56:44 +000083 // Jars from the runtime apex are allowed.
84 static const char* kRuntimeApexPrefix = "/apex/com.android.runtime/javalib/";
85 if (android::base::StartsWith(path, kRuntimeApexPrefix)
86 && android::base::EndsWith(path, kJarSuffix)) {
87 return true;
88 }
89
Robert Sesek54e387d2016-12-02 17:27:50 -050090 // Whitelist files needed for Runtime Resource Overlay, like these:
91 // /system/vendor/overlay/framework-res.apk
92 // /system/vendor/overlay-subdir/pg/framework-res.apk
93 // /vendor/overlay/framework-res.apk
94 // /vendor/overlay/PG/android-framework-runtime-resource-overlay.apk
95 // /data/resource-cache/system@vendor@overlay@framework-res.apk@idmap
96 // /data/resource-cache/system@vendor@overlay-subdir@pg@framework-res.apk@idmap
97 // See AssetManager.cpp for more details on overlay-subdir.
Narayan Kamath3879ecc2017-03-02 17:30:03 +000098 static const char* kOverlayDir = "/system/vendor/overlay/";
99 static const char* kVendorOverlayDir = "/vendor/overlay";
Mårten Kongstad06a1ac82018-09-20 13:09:47 +0200100 static const char* kVendorOverlaySubdir = "/system/vendor/overlay-subdir/";
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900101 static const char* kSystemProductOverlayDir = "/system/product/overlay/";
102 static const char* kProductOverlayDir = "/product/overlay";
Dario Freni4ce46792018-06-01 14:02:08 +0100103 static const char* kSystemProductServicesOverlayDir = "/system/product_services/overlay/";
104 static const char* kProductServicesOverlayDir = "/product_services/overlay";
Mårten Kongstad48c24cf2019-02-25 10:54:09 +0100105 static const char* kSystemOdmOverlayDir = "/system/odm/overlay";
106 static const char* kOdmOverlayDir = "/odm/overlay";
Mårten Kongstadeb8a5c02019-02-25 14:18:17 +0100107 static const char* kSystemOemOverlayDir = "/system/oem/overlay";
108 static const char* kOemOverlayDir = "/oem/overlay";
Narayan Kamath3879ecc2017-03-02 17:30:03 +0000109 static const char* kApkSuffix = ".apk";
Robert Sesek54e387d2016-12-02 17:27:50 -0500110
Narayan Kamath3879ecc2017-03-02 17:30:03 +0000111 if ((android::base::StartsWith(path, kOverlayDir)
Mårten Kongstad06a1ac82018-09-20 13:09:47 +0200112 || android::base::StartsWith(path, kVendorOverlaySubdir)
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900113 || android::base::StartsWith(path, kVendorOverlayDir)
114 || android::base::StartsWith(path, kSystemProductOverlayDir)
Dario Freni4ce46792018-06-01 14:02:08 +0100115 || android::base::StartsWith(path, kProductOverlayDir)
116 || android::base::StartsWith(path, kSystemProductServicesOverlayDir)
Mårten Kongstad48c24cf2019-02-25 10:54:09 +0100117 || android::base::StartsWith(path, kProductServicesOverlayDir)
118 || android::base::StartsWith(path, kSystemOdmOverlayDir)
Mårten Kongstadeb8a5c02019-02-25 14:18:17 +0100119 || android::base::StartsWith(path, kOdmOverlayDir)
120 || android::base::StartsWith(path, kSystemOemOverlayDir)
121 || android::base::StartsWith(path, kOemOverlayDir))
Narayan Kamath3879ecc2017-03-02 17:30:03 +0000122 && android::base::EndsWith(path, kApkSuffix)
Robert Sesek54e387d2016-12-02 17:27:50 -0500123 && path.find("/../") == std::string::npos) {
124 return true;
125 }
126
Narayan Kamath3879ecc2017-03-02 17:30:03 +0000127 static const char* kOverlayIdmapPrefix = "/data/resource-cache/";
128 static const char* kOverlayIdmapSuffix = ".apk@idmap";
129 if (android::base::StartsWith(path, kOverlayIdmapPrefix)
130 && android::base::EndsWith(path, kOverlayIdmapSuffix)
Robert Sesek54e387d2016-12-02 17:27:50 -0500131 && path.find("/../") == std::string::npos) {
132 return true;
133 }
134
135 // All regular files that are placed under this path are whitelisted automatically.
Narayan Kamath3879ecc2017-03-02 17:30:03 +0000136 static const char* kZygoteWhitelistPath = "/vendor/zygote_whitelist/";
137 if (android::base::StartsWith(path, kZygoteWhitelistPath)
138 && path.find("/../") == std::string::npos) {
Robert Sesek54e387d2016-12-02 17:27:50 -0500139 return true;
140 }
141
142 return false;
143}
144
145FileDescriptorWhitelist::FileDescriptorWhitelist()
146 : whitelist_() {
147}
148
Robert Sesek54e387d2016-12-02 17:27:50 -0500149FileDescriptorWhitelist* FileDescriptorWhitelist::instance_ = nullptr;
150
Andreas Gampe183a5d32018-03-12 14:53:34 -0700151// Keeps track of all relevant information (flags, offset etc.) of an
152// open zygote file descriptor.
153class FileDescriptorInfo {
154 public:
Chris Wailesaa1c9622019-01-10 16:55:32 -0800155 // Create a FileDescriptorInfo for a given file descriptor.
156 static FileDescriptorInfo* CreateFromFd(int fd, fail_fn_t fail_fn);
Andreas Gampe183a5d32018-03-12 14:53:34 -0700157
158 // Checks whether the file descriptor associated with this object
159 // refers to the same description.
Chris Wailesaa1c9622019-01-10 16:55:32 -0800160 bool RefersToSameFile() const;
Andreas Gampe183a5d32018-03-12 14:53:34 -0700161
Chris Wailesaa1c9622019-01-10 16:55:32 -0800162 void ReopenOrDetach(fail_fn_t fail_fn) const;
Andreas Gampe183a5d32018-03-12 14:53:34 -0700163
164 const int fd;
165 const struct stat stat;
166 const std::string file_path;
167 const int open_flags;
168 const int fd_flags;
169 const int fs_flags;
170 const off_t offset;
171 const bool is_sock;
172
173 private:
Chih-Hung Hsieh0727be12018-12-20 13:43:46 -0800174 explicit FileDescriptorInfo(int fd);
Andreas Gampe183a5d32018-03-12 14:53:34 -0700175
176 FileDescriptorInfo(struct stat stat, const std::string& file_path, int fd, int open_flags,
177 int fd_flags, int fs_flags, off_t offset);
178
179 // Returns the locally-bound name of the socket |fd|. Returns true
180 // iff. all of the following hold :
181 //
182 // - the socket's sa_family is AF_UNIX.
183 // - the length of the path is greater than zero (i.e, not an unnamed socket).
184 // - the first byte of the path isn't zero (i.e, not a socket with an abstract
185 // address).
186 static bool GetSocketName(const int fd, std::string* result);
187
Chris Wailesaa1c9622019-01-10 16:55:32 -0800188 void DetachSocket(fail_fn_t fail_fn) const;
Andreas Gampe183a5d32018-03-12 14:53:34 -0700189
190 DISALLOW_COPY_AND_ASSIGN(FileDescriptorInfo);
191};
192
Robert Sesek54e387d2016-12-02 17:27:50 -0500193// static
Chris Wailesaa1c9622019-01-10 16:55:32 -0800194FileDescriptorInfo* FileDescriptorInfo::CreateFromFd(int fd, fail_fn_t fail_fn) {
Robert Sesek8225b7c2016-12-16 14:02:31 -0500195 struct stat f_stat;
196 // This should never happen; the zygote should always have the right set
197 // of permissions required to stat all its open files.
198 if (TEMP_FAILURE_RETRY(fstat(fd, &f_stat)) == -1) {
Chris Wailesaa1c9622019-01-10 16:55:32 -0800199 fail_fn(android::base::StringPrintf("Unable to stat %d", fd));
Robert Sesek8225b7c2016-12-16 14:02:31 -0500200 }
201
Robert Sesek54e387d2016-12-02 17:27:50 -0500202 const FileDescriptorWhitelist* whitelist = FileDescriptorWhitelist::Get();
203
Robert Sesek8225b7c2016-12-16 14:02:31 -0500204 if (S_ISSOCK(f_stat.st_mode)) {
205 std::string socket_name;
206 if (!GetSocketName(fd, &socket_name)) {
Chris Wailesaa1c9622019-01-10 16:55:32 -0800207 fail_fn("Unable to get socket name");
Robert Sesek8225b7c2016-12-16 14:02:31 -0500208 }
209
Robert Sesek54e387d2016-12-02 17:27:50 -0500210 if (!whitelist->IsAllowed(socket_name)) {
Chris Wailesaa1c9622019-01-10 16:55:32 -0800211 fail_fn(android::base::StringPrintf("Socket name not whitelisted : %s (fd=%d)",
212 socket_name.c_str(),
213 fd));
Robert Sesek8225b7c2016-12-16 14:02:31 -0500214 }
215
216 return new FileDescriptorInfo(fd);
217 }
218
219 // We only handle whitelisted regular files and character devices. Whitelisted
220 // character devices must provide a guarantee of sensible behaviour when
221 // reopened.
222 //
223 // S_ISDIR : Not supported. (We could if we wanted to, but it's unused).
224 // S_ISLINK : Not supported.
225 // S_ISBLK : Not supported.
Chris Wailes7e797b62019-02-22 18:29:22 -0800226 // S_ISFIFO : Not supported. Note that the Zygote and USAPs use pipes to
Chris Wailesaa1c9622019-01-10 16:55:32 -0800227 // communicate with the child processes across forks but those should have been
228 // added to the redirection exemption list.
Robert Sesek8225b7c2016-12-16 14:02:31 -0500229 if (!S_ISCHR(f_stat.st_mode) && !S_ISREG(f_stat.st_mode)) {
Chris Wailesaa1c9622019-01-10 16:55:32 -0800230 std::string mode = "Unknown";
231
232 if (S_ISDIR(f_stat.st_mode)) {
233 mode = "DIR";
234 } else if (S_ISLNK(f_stat.st_mode)) {
235 mode = "LINK";
236 } else if (S_ISBLK(f_stat.st_mode)) {
237 mode = "BLOCK";
238 } else if (S_ISFIFO(f_stat.st_mode)) {
239 mode = "FIFO";
240 }
241
242 fail_fn(android::base::StringPrintf("Unsupported st_mode for FD %d: %s", fd, mode.c_str()));
Robert Sesek8225b7c2016-12-16 14:02:31 -0500243 }
244
245 std::string file_path;
Narayan Kamath3879ecc2017-03-02 17:30:03 +0000246 const std::string fd_path = android::base::StringPrintf("/proc/self/fd/%d", fd);
247 if (!android::base::Readlink(fd_path, &file_path)) {
Chris Wailesaa1c9622019-01-10 16:55:32 -0800248 fail_fn(android::base::StringPrintf("Could not read fd link %s: %s",
249 fd_path.c_str(),
250 strerror(errno)));
Robert Sesek8225b7c2016-12-16 14:02:31 -0500251 }
252
Robert Sesek54e387d2016-12-02 17:27:50 -0500253 if (!whitelist->IsAllowed(file_path)) {
Chris Wailesaa1c9622019-01-10 16:55:32 -0800254 fail_fn(std::string("Not whitelisted : ").append(file_path));
Robert Sesek8225b7c2016-12-16 14:02:31 -0500255 }
256
257 // File descriptor flags : currently on FD_CLOEXEC. We can set these
258 // using F_SETFD - we're single threaded at this point of execution so
259 // there won't be any races.
260 const int fd_flags = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD));
261 if (fd_flags == -1) {
Chris Wailesaa1c9622019-01-10 16:55:32 -0800262 fail_fn(android::base::StringPrintf("Failed fcntl(%d, F_GETFD) (%s): %s",
263 fd,
264 file_path.c_str(),
265 strerror(errno)));
Robert Sesek8225b7c2016-12-16 14:02:31 -0500266 }
267
268 // File status flags :
269 // - File access mode : (O_RDONLY, O_WRONLY...) we'll pass these through
270 // to the open() call.
271 //
272 // - File creation flags : (O_CREAT, O_EXCL...) - there's not much we can
273 // do about these, since the file has already been created. We shall ignore
274 // them here.
275 //
276 // - Other flags : We'll have to set these via F_SETFL. On linux, F_SETFL
277 // can only set O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK.
278 // In particular, it can't set O_SYNC and O_DSYNC. We'll have to test for
279 // their presence and pass them in to open().
280 int fs_flags = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFL));
281 if (fs_flags == -1) {
Chris Wailesaa1c9622019-01-10 16:55:32 -0800282 fail_fn(android::base::StringPrintf("Failed fcntl(%d, F_GETFL) (%s): %s",
283 fd,
284 file_path.c_str(),
285 strerror(errno)));
Robert Sesek8225b7c2016-12-16 14:02:31 -0500286 }
287
288 // File offset : Ignore the offset for non seekable files.
289 const off_t offset = TEMP_FAILURE_RETRY(lseek64(fd, 0, SEEK_CUR));
290
291 // We pass the flags that open accepts to open, and use F_SETFL for
292 // the rest of them.
293 static const int kOpenFlags = (O_RDONLY | O_WRONLY | O_RDWR | O_DSYNC | O_SYNC);
294 int open_flags = fs_flags & (kOpenFlags);
295 fs_flags = fs_flags & (~(kOpenFlags));
296
297 return new FileDescriptorInfo(f_stat, file_path, fd, open_flags, fd_flags, fs_flags, offset);
298}
299
Chris Wailesaa1c9622019-01-10 16:55:32 -0800300bool FileDescriptorInfo::RefersToSameFile() const {
Robert Sesek8225b7c2016-12-16 14:02:31 -0500301 struct stat f_stat;
302 if (TEMP_FAILURE_RETRY(fstat(fd, &f_stat)) == -1) {
Narayan Kamatha352d242017-03-02 14:44:45 +0000303 PLOG(ERROR) << "Unable to restat fd " << fd;
Robert Sesek8225b7c2016-12-16 14:02:31 -0500304 return false;
305 }
306
307 return f_stat.st_ino == stat.st_ino && f_stat.st_dev == stat.st_dev;
308}
309
Chris Wailesaa1c9622019-01-10 16:55:32 -0800310void FileDescriptorInfo::ReopenOrDetach(fail_fn_t fail_fn) const {
Robert Sesek8225b7c2016-12-16 14:02:31 -0500311 if (is_sock) {
Chris Wailesaa1c9622019-01-10 16:55:32 -0800312 return DetachSocket(fail_fn);
Robert Sesek8225b7c2016-12-16 14:02:31 -0500313 }
314
315 // NOTE: This might happen if the file was unlinked after being opened.
316 // It's a common pattern in the case of temporary files and the like but
317 // we should not allow such usage from the zygote.
318 const int new_fd = TEMP_FAILURE_RETRY(open(file_path.c_str(), open_flags));
319
320 if (new_fd == -1) {
Chris Wailesaa1c9622019-01-10 16:55:32 -0800321 fail_fn(android::base::StringPrintf("Failed open(%s, %i): %s",
322 file_path.c_str(),
323 open_flags,
324 strerror(errno)));
Robert Sesek8225b7c2016-12-16 14:02:31 -0500325 }
326
327 if (TEMP_FAILURE_RETRY(fcntl(new_fd, F_SETFD, fd_flags)) == -1) {
328 close(new_fd);
Chris Wailesaa1c9622019-01-10 16:55:32 -0800329 fail_fn(android::base::StringPrintf("Failed fcntl(%d, F_SETFD, %d) (%s): %s",
330 new_fd,
331 fd_flags,
332 file_path.c_str(),
333 strerror(errno)));
Robert Sesek8225b7c2016-12-16 14:02:31 -0500334 }
335
336 if (TEMP_FAILURE_RETRY(fcntl(new_fd, F_SETFL, fs_flags)) == -1) {
337 close(new_fd);
Chris Wailesaa1c9622019-01-10 16:55:32 -0800338 fail_fn(android::base::StringPrintf("Failed fcntl(%d, F_SETFL, %d) (%s): %s",
339 new_fd,
340 fs_flags,
341 file_path.c_str(),
342 strerror(errno)));
Robert Sesek8225b7c2016-12-16 14:02:31 -0500343 }
344
345 if (offset != -1 && TEMP_FAILURE_RETRY(lseek64(new_fd, offset, SEEK_SET)) == -1) {
346 close(new_fd);
Chris Wailesaa1c9622019-01-10 16:55:32 -0800347 fail_fn(android::base::StringPrintf("Failed lseek64(%d, SEEK_SET) (%s): %s",
348 new_fd,
349 file_path.c_str(),
350 strerror(errno)));
Robert Sesek8225b7c2016-12-16 14:02:31 -0500351 }
352
Chris Wailesaa1c9622019-01-10 16:55:32 -0800353 int dup_flags = (fd_flags & FD_CLOEXEC) ? O_CLOEXEC : 0;
354 if (TEMP_FAILURE_RETRY(dup3(new_fd, fd, dup_flags)) == -1) {
Robert Sesek8225b7c2016-12-16 14:02:31 -0500355 close(new_fd);
Chris Wailesaa1c9622019-01-10 16:55:32 -0800356 fail_fn(android::base::StringPrintf("Failed dup3(%d, %d, %d) (%s): %s",
357 fd,
358 new_fd,
359 dup_flags,
360 file_path.c_str(),
361 strerror(errno)));
Robert Sesek8225b7c2016-12-16 14:02:31 -0500362 }
363
364 close(new_fd);
Robert Sesek8225b7c2016-12-16 14:02:31 -0500365}
366
367FileDescriptorInfo::FileDescriptorInfo(int fd) :
368 fd(fd),
369 stat(),
370 open_flags(0),
371 fd_flags(0),
372 fs_flags(0),
373 offset(0),
374 is_sock(true) {
375}
376
377FileDescriptorInfo::FileDescriptorInfo(struct stat stat, const std::string& file_path,
378 int fd, int open_flags, int fd_flags, int fs_flags,
379 off_t offset) :
380 fd(fd),
381 stat(stat),
382 file_path(file_path),
383 open_flags(open_flags),
384 fd_flags(fd_flags),
385 fs_flags(fs_flags),
386 offset(offset),
387 is_sock(false) {
388}
389
Robert Sesek8225b7c2016-12-16 14:02:31 -0500390bool FileDescriptorInfo::GetSocketName(const int fd, std::string* result) {
391 sockaddr_storage ss;
392 sockaddr* addr = reinterpret_cast<sockaddr*>(&ss);
393 socklen_t addr_len = sizeof(ss);
394
395 if (TEMP_FAILURE_RETRY(getsockname(fd, addr, &addr_len)) == -1) {
Narayan Kamath84b55112017-03-02 17:04:08 +0000396 PLOG(ERROR) << "Failed getsockname(" << fd << ")";
Robert Sesek8225b7c2016-12-16 14:02:31 -0500397 return false;
398 }
399
400 if (addr->sa_family != AF_UNIX) {
Narayan Kamath84b55112017-03-02 17:04:08 +0000401 LOG(ERROR) << "Unsupported socket (fd=" << fd << ") with family " << addr->sa_family;
Robert Sesek8225b7c2016-12-16 14:02:31 -0500402 return false;
403 }
404
405 const sockaddr_un* unix_addr = reinterpret_cast<const sockaddr_un*>(&ss);
406
407 size_t path_len = addr_len - offsetof(struct sockaddr_un, sun_path);
408 // This is an unnamed local socket, we do not accept it.
409 if (path_len == 0) {
Narayan Kamath84b55112017-03-02 17:04:08 +0000410 LOG(ERROR) << "Unsupported AF_UNIX socket (fd=" << fd << ") with empty path.";
Robert Sesek8225b7c2016-12-16 14:02:31 -0500411 return false;
412 }
413
Robert Sesekd0a190df2018-02-12 18:46:01 -0500414 // This is a local socket with an abstract address. Remove the leading NUL byte and
415 // add a human-readable "ABSTRACT/" prefix.
Robert Sesek8225b7c2016-12-16 14:02:31 -0500416 if (unix_addr->sun_path[0] == '\0') {
Robert Sesekd0a190df2018-02-12 18:46:01 -0500417 *result = "ABSTRACT/";
418 result->append(&unix_addr->sun_path[1], path_len - 1);
419 return true;
Robert Sesek8225b7c2016-12-16 14:02:31 -0500420 }
421
422 // If we're here, sun_path must refer to a null terminated filesystem
423 // pathname (man 7 unix). Remove the terminator before assigning it to an
424 // std::string.
425 if (unix_addr->sun_path[path_len - 1] == '\0') {
426 --path_len;
427 }
428
429 result->assign(unix_addr->sun_path, path_len);
430 return true;
431}
432
Chris Wailesaa1c9622019-01-10 16:55:32 -0800433void FileDescriptorInfo::DetachSocket(fail_fn_t fail_fn) const {
Nick Kralevich0361ce12019-01-25 10:08:58 -0800434 const int dev_null_fd = open("/dev/null", O_RDWR | O_CLOEXEC);
Robert Sesek8225b7c2016-12-16 14:02:31 -0500435 if (dev_null_fd < 0) {
Chris Wailesaa1c9622019-01-10 16:55:32 -0800436 fail_fn(std::string("Failed to open /dev/null: ").append(strerror(errno)));
Robert Sesek8225b7c2016-12-16 14:02:31 -0500437 }
438
Nick Kralevich0361ce12019-01-25 10:08:58 -0800439 if (dup3(dev_null_fd, fd, O_CLOEXEC) == -1) {
440 fail_fn(android::base::StringPrintf("Failed dup3 on socket descriptor %d: %s",
Chris Wailesaa1c9622019-01-10 16:55:32 -0800441 fd,
442 strerror(errno)));
Robert Sesek8225b7c2016-12-16 14:02:31 -0500443 }
444
445 if (close(dev_null_fd) == -1) {
Chris Wailesaa1c9622019-01-10 16:55:32 -0800446 fail_fn(android::base::StringPrintf("Failed close(%d): %s", dev_null_fd, strerror(errno)));
Robert Sesek8225b7c2016-12-16 14:02:31 -0500447 }
Robert Sesek8225b7c2016-12-16 14:02:31 -0500448}
449
450// static
Andreas Gampe183a5d32018-03-12 14:53:34 -0700451FileDescriptorTable* FileDescriptorTable::Create(const std::vector<int>& fds_to_ignore,
Chris Wailesaa1c9622019-01-10 16:55:32 -0800452 fail_fn_t fail_fn) {
453 DIR* proc_fd_dir = opendir(kFdPath);
454 if (proc_fd_dir == nullptr) {
455 fail_fn(std::string("Unable to open directory ").append(kFdPath));
Robert Sesek8225b7c2016-12-16 14:02:31 -0500456 }
Chris Wailesaa1c9622019-01-10 16:55:32 -0800457
458 int dir_fd = dirfd(proc_fd_dir);
459 dirent* dir_entry;
Robert Sesek8225b7c2016-12-16 14:02:31 -0500460
461 std::unordered_map<int, FileDescriptorInfo*> open_fd_map;
Chris Wailesaa1c9622019-01-10 16:55:32 -0800462 while ((dir_entry = readdir(proc_fd_dir)) != nullptr) {
463 const int fd = ParseFd(dir_entry, dir_fd);
Robert Sesek8225b7c2016-12-16 14:02:31 -0500464 if (fd == -1) {
465 continue;
466 }
Chris Wailesaa1c9622019-01-10 16:55:32 -0800467
Andreas Gampe8dfa1782017-01-05 12:45:58 -0800468 if (std::find(fds_to_ignore.begin(), fds_to_ignore.end(), fd) != fds_to_ignore.end()) {
Andreas Gampe8dfa1782017-01-05 12:45:58 -0800469 continue;
470 }
Robert Sesek8225b7c2016-12-16 14:02:31 -0500471
Chris Wailesaa1c9622019-01-10 16:55:32 -0800472 open_fd_map[fd] = FileDescriptorInfo::CreateFromFd(fd, fail_fn);
Robert Sesek8225b7c2016-12-16 14:02:31 -0500473 }
474
Chris Wailesaa1c9622019-01-10 16:55:32 -0800475 if (closedir(proc_fd_dir) == -1) {
476 fail_fn("Unable to close directory");
Robert Sesek8225b7c2016-12-16 14:02:31 -0500477 }
Chris Wailesaa1c9622019-01-10 16:55:32 -0800478
Robert Sesek8225b7c2016-12-16 14:02:31 -0500479 return new FileDescriptorTable(open_fd_map);
480}
481
Chris Wailesaa1c9622019-01-10 16:55:32 -0800482void FileDescriptorTable::Restat(const std::vector<int>& fds_to_ignore, fail_fn_t fail_fn) {
Robert Sesek8225b7c2016-12-16 14:02:31 -0500483 std::set<int> open_fds;
484
485 // First get the list of open descriptors.
Chris Wailesaa1c9622019-01-10 16:55:32 -0800486 DIR* proc_fd_dir = opendir(kFdPath);
487 if (proc_fd_dir == nullptr) {
488 fail_fn(android::base::StringPrintf("Unable to open directory %s: %s",
489 kFdPath,
490 strerror(errno)));
Robert Sesek8225b7c2016-12-16 14:02:31 -0500491 }
492
Chris Wailesaa1c9622019-01-10 16:55:32 -0800493 int dir_fd = dirfd(proc_fd_dir);
494 dirent* dir_entry;
495 while ((dir_entry = readdir(proc_fd_dir)) != nullptr) {
496 const int fd = ParseFd(dir_entry, dir_fd);
Robert Sesek8225b7c2016-12-16 14:02:31 -0500497 if (fd == -1) {
498 continue;
499 }
Chris Wailesaa1c9622019-01-10 16:55:32 -0800500
Andreas Gampe8dfa1782017-01-05 12:45:58 -0800501 if (std::find(fds_to_ignore.begin(), fds_to_ignore.end(), fd) != fds_to_ignore.end()) {
Andreas Gampe8dfa1782017-01-05 12:45:58 -0800502 continue;
503 }
Robert Sesek8225b7c2016-12-16 14:02:31 -0500504
505 open_fds.insert(fd);
506 }
507
Chris Wailesaa1c9622019-01-10 16:55:32 -0800508 if (closedir(proc_fd_dir) == -1) {
509 fail_fn(android::base::StringPrintf("Unable to close directory: %s", strerror(errno)));
Robert Sesek8225b7c2016-12-16 14:02:31 -0500510 }
511
Chris Wailesaa1c9622019-01-10 16:55:32 -0800512 RestatInternal(open_fds, fail_fn);
Robert Sesek8225b7c2016-12-16 14:02:31 -0500513}
514
Chris Wailesaa1c9622019-01-10 16:55:32 -0800515// Reopens all file descriptors that are contained in the table.
516void FileDescriptorTable::ReopenOrDetach(fail_fn_t fail_fn) {
Robert Sesek8225b7c2016-12-16 14:02:31 -0500517 std::unordered_map<int, FileDescriptorInfo*>::const_iterator it;
518 for (it = open_fd_map_.begin(); it != open_fd_map_.end(); ++it) {
519 const FileDescriptorInfo* info = it->second;
Chris Wailesaa1c9622019-01-10 16:55:32 -0800520 if (info == nullptr) {
521 return;
522 } else {
523 info->ReopenOrDetach(fail_fn);
Robert Sesek8225b7c2016-12-16 14:02:31 -0500524 }
525 }
Robert Sesek8225b7c2016-12-16 14:02:31 -0500526}
527
528FileDescriptorTable::FileDescriptorTable(
529 const std::unordered_map<int, FileDescriptorInfo*>& map)
530 : open_fd_map_(map) {
531}
532
Chris Wailesaa1c9622019-01-10 16:55:32 -0800533void FileDescriptorTable::RestatInternal(std::set<int>& open_fds, fail_fn_t fail_fn) {
Robert Sesek8225b7c2016-12-16 14:02:31 -0500534 // Iterate through the list of file descriptors we've already recorded
535 // and check whether :
536 //
537 // (a) they continue to be open.
538 // (b) they refer to the same file.
Andreas Gampe183a5d32018-03-12 14:53:34 -0700539 //
540 // We'll only store the last error message.
Robert Sesek8225b7c2016-12-16 14:02:31 -0500541 std::unordered_map<int, FileDescriptorInfo*>::iterator it = open_fd_map_.begin();
542 while (it != open_fd_map_.end()) {
543 std::set<int>::const_iterator element = open_fds.find(it->first);
544 if (element == open_fds.end()) {
545 // The entry from the file descriptor table is no longer in the list
546 // of open files. We warn about this condition and remove it from
547 // the list of FDs under consideration.
548 //
549 // TODO(narayan): This will be an error in a future android release.
550 // error = true;
551 // ALOGW("Zygote closed file descriptor %d.", it->first);
552 it = open_fd_map_.erase(it);
553 } else {
554 // The entry from the file descriptor table is still open. Restat
555 // it and check whether it refers to the same file.
Chris Wailesaa1c9622019-01-10 16:55:32 -0800556 if (!it->second->RefersToSameFile()) {
Robert Sesek8225b7c2016-12-16 14:02:31 -0500557 // The file descriptor refers to a different description. We must
558 // update our entry in the table.
559 delete it->second;
Chris Wailesaa1c9622019-01-10 16:55:32 -0800560 it->second = FileDescriptorInfo::CreateFromFd(*element, fail_fn);
Robert Sesek8225b7c2016-12-16 14:02:31 -0500561 } else {
562 // It's the same file. Nothing to do here. Move on to the next open
563 // FD.
Robert Sesek8225b7c2016-12-16 14:02:31 -0500564 }
565
Chris Wailesaa1c9622019-01-10 16:55:32 -0800566 ++it;
567
Robert Sesek8225b7c2016-12-16 14:02:31 -0500568 // Finally, remove the FD from the set of open_fds. We do this last because
569 // |element| will not remain valid after a call to erase.
570 open_fds.erase(element);
571 }
572 }
573
574 if (open_fds.size() > 0) {
575 // The zygote has opened new file descriptors since our last inspection.
576 // We warn about this condition and add them to our table.
577 //
578 // TODO(narayan): This will be an error in a future android release.
579 // error = true;
580 // ALOGW("Zygote opened %zd new file descriptor(s).", open_fds.size());
581
582 // TODO(narayan): This code will be removed in a future android release.
583 std::set<int>::const_iterator it;
584 for (it = open_fds.begin(); it != open_fds.end(); ++it) {
585 const int fd = (*it);
Chris Wailesaa1c9622019-01-10 16:55:32 -0800586 open_fd_map_[fd] = FileDescriptorInfo::CreateFromFd(fd, fail_fn);
Robert Sesek8225b7c2016-12-16 14:02:31 -0500587 }
588 }
Robert Sesek8225b7c2016-12-16 14:02:31 -0500589}
590
591// static
Chris Wailesaa1c9622019-01-10 16:55:32 -0800592int FileDescriptorTable::ParseFd(dirent* dir_entry, int dir_fd) {
Robert Sesek8225b7c2016-12-16 14:02:31 -0500593 char* end;
Chris Wailesaa1c9622019-01-10 16:55:32 -0800594 const int fd = strtol(dir_entry->d_name, &end, 10);
Robert Sesek8225b7c2016-12-16 14:02:31 -0500595 if ((*end) != '\0') {
596 return -1;
597 }
598
599 // Don't bother with the standard input/output/error, they're handled
600 // specially post-fork anyway.
601 if (fd <= STDERR_FILENO || fd == dir_fd) {
602 return -1;
603 }
604
605 return fd;
606}