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