1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <dirent.h>
30 
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <malloc.h>
34 #include <string.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <unistd.h>
38 
39 #include <android/fdsan.h>
40 
41 #include "private/bionic_fortify.h"
42 #include "private/ErrnoRestorer.h"
43 #include "private/ScopedPthreadMutexLocker.h"
44 
45 extern "C" int __getdents64(unsigned int, dirent*, unsigned int);
46 
47 // Apportable decided to copy the data structure from this file
48 // and use it in their own code, but they also call into readdir.
49 // In order to avoid a lockup, the structure must be maintained in
50 // the exact same order as in L and below. New structure members
51 // need to be added to the end of this structure.
52 // See b/21037208 for more details.
53 struct DIR {
54   int fd_;
55   size_t available_bytes_;
56   dirent* next_;
57   pthread_mutex_t mutex_;
58   dirent buff_[15];
59   long current_pos_;
60 };
61 
62 #define CHECK_DIR(d) if (d == nullptr) __fortify_fatal("%s: null DIR*", __FUNCTION__)
63 
__get_dir_tag(DIR * dir)64 static uint64_t __get_dir_tag(DIR* dir) {
65   return android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_DIR,
66                                         reinterpret_cast<uint64_t>(dir));
67 }
68 
__allocate_DIR(int fd)69 static DIR* __allocate_DIR(int fd) {
70   DIR* d = reinterpret_cast<DIR*>(malloc(sizeof(DIR)));
71   if (d == nullptr) {
72     return nullptr;
73   }
74   d->fd_ = fd;
75   android_fdsan_exchange_owner_tag(fd, 0, __get_dir_tag(d));
76   d->available_bytes_ = 0;
77   d->next_ = nullptr;
78   d->current_pos_ = 0L;
79   pthread_mutex_init(&d->mutex_, nullptr);
80   return d;
81 }
82 
dirfd(DIR * d)83 int dirfd(DIR* d) {
84   CHECK_DIR(d);
85   return d->fd_;
86 }
87 
fdopendir(int fd)88 DIR* fdopendir(int fd) {
89   // Is 'fd' actually a directory?
90   struct stat sb;
91   if (fstat(fd, &sb) == -1) {
92     return nullptr;
93   }
94   if (!S_ISDIR(sb.st_mode)) {
95     errno = ENOTDIR;
96     return nullptr;
97   }
98 
99   return __allocate_DIR(fd);
100 }
101 
opendir(const char * path)102 DIR* opendir(const char* path) {
103   int fd = open(path, O_CLOEXEC | O_DIRECTORY | O_RDONLY);
104   return (fd != -1) ? __allocate_DIR(fd) : nullptr;
105 }
106 
__fill_DIR(DIR * d)107 static bool __fill_DIR(DIR* d) {
108   CHECK_DIR(d);
109   int rc = TEMP_FAILURE_RETRY(__getdents64(d->fd_, d->buff_, sizeof(d->buff_)));
110   if (rc <= 0) {
111     return false;
112   }
113   d->available_bytes_ = rc;
114   d->next_ = d->buff_;
115   return true;
116 }
117 
__readdir_locked(DIR * d)118 static dirent* __readdir_locked(DIR* d) {
119   if (d->available_bytes_ == 0 && !__fill_DIR(d)) {
120     return nullptr;
121   }
122 
123   dirent* entry = d->next_;
124   d->next_ = reinterpret_cast<dirent*>(reinterpret_cast<char*>(entry) + entry->d_reclen);
125   d->available_bytes_ -= entry->d_reclen;
126   // The directory entry offset uses 0, 1, 2 instead of real file offset,
127   // so the value range of long type is enough.
128   d->current_pos_ = static_cast<long>(entry->d_off);
129   return entry;
130 }
131 
readdir(DIR * d)132 dirent* readdir(DIR* d) {
133   CHECK_DIR(d);
134   ScopedPthreadMutexLocker locker(&d->mutex_);
135   return __readdir_locked(d);
136 }
137 __strong_alias(readdir64, readdir);
138 
readdir_r(DIR * d,dirent * entry,dirent ** result)139 int readdir_r(DIR* d, dirent* entry, dirent** result) {
140   CHECK_DIR(d);
141 
142   ErrnoRestorer errno_restorer;
143 
144   *result = nullptr;
145   errno = 0;
146 
147   ScopedPthreadMutexLocker locker(&d->mutex_);
148 
149   dirent* next = __readdir_locked(d);
150   if (errno != 0 && next == nullptr) {
151     return errno;
152   }
153 
154   if (next != nullptr) {
155     memcpy(entry, next, next->d_reclen);
156     *result = entry;
157   }
158   return 0;
159 }
160 __strong_alias(readdir64_r, readdir_r);
161 
closedir(DIR * d)162 int closedir(DIR* d) {
163   if (d == nullptr) {
164     errno = EINVAL;
165     return -1;
166   }
167 
168   int fd = d->fd_;
169   pthread_mutex_destroy(&d->mutex_);
170   int rc = android_fdsan_close_with_tag(fd, __get_dir_tag(d));
171   free(d);
172   return rc;
173 }
174 
rewinddir(DIR * d)175 void rewinddir(DIR* d) {
176   CHECK_DIR(d);
177 
178   ScopedPthreadMutexLocker locker(&d->mutex_);
179   lseek(d->fd_, 0, SEEK_SET);
180   d->available_bytes_ = 0;
181   d->current_pos_ = 0L;
182 }
183 
seekdir(DIR * d,long offset)184 void seekdir(DIR* d, long offset) {
185   CHECK_DIR(d);
186 
187   ScopedPthreadMutexLocker locker(&d->mutex_);
188   off_t ret = lseek(d->fd_, offset, SEEK_SET);
189   if (ret != -1L) {
190     d->available_bytes_ = 0;
191     d->current_pos_ = ret;
192   }
193 }
194 
telldir(DIR * d)195 long telldir(DIR* d) {
196   CHECK_DIR(d);
197 
198   return d->current_pos_;
199 }
200 
alphasort(const dirent ** a,const dirent ** b)201 int alphasort(const dirent** a, const dirent** b) {
202   return strcoll((*a)->d_name, (*b)->d_name);
203 }
204 __strong_alias(alphasort64, alphasort);
205