| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2012 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 |  | 
| Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 17 | #include <cutils/fs.h> | 
 | 18 |  | 
| Jeff Sharkey | ddb1733 | 2012-09-13 14:47:23 -0700 | [diff] [blame] | 19 | #define LOG_TAG "cutils" | 
 | 20 |  | 
| Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 21 | /* These defines are only needed because prebuilt headers are out of date */ | 
 | 22 | #define __USE_XOPEN2K8 1 | 
 | 23 | #define _ATFILE_SOURCE 1 | 
 | 24 | #define _GNU_SOURCE 1 | 
 | 25 |  | 
| Mark Salyzyn | 23ed4c2 | 2016-09-28 13:33:27 -0700 | [diff] [blame] | 26 | #include <dirent.h> | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 27 | #include <errno.h> | 
| Mark Salyzyn | 23ed4c2 | 2016-09-28 13:33:27 -0700 | [diff] [blame] | 28 | #include <fcntl.h> | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 29 | #include <limits.h> | 
| Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 30 | #include <stdio.h> | 
| Nick Kralevich | 69ce489 | 2012-10-15 15:51:33 -0700 | [diff] [blame] | 31 | #include <stdlib.h> | 
| Mark Salyzyn | 23ed4c2 | 2016-09-28 13:33:27 -0700 | [diff] [blame] | 32 | #include <string.h> | 
 | 33 | #include <sys/stat.h> | 
 | 34 | #include <sys/types.h> | 
 | 35 | #include <unistd.h> | 
 | 36 |  | 
| Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 37 | #include <log/log.h> | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 38 |  | 
 | 39 | #define ALL_PERMS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) | 
 | 40 | #define BUF_SIZE 64 | 
 | 41 |  | 
| Calin Juravle | 9812105 | 2016-05-24 16:11:04 +0100 | [diff] [blame] | 42 | static int fs_prepare_path_impl(const char* path, mode_t mode, uid_t uid, gid_t gid, | 
 | 43 |         int allow_fixup, int prepare_as_dir) { | 
| Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 44 |     // TODO: fix the goto hell below. | 
 | 45 |     int type_ok; | 
 | 46 |     int owner_match; | 
 | 47 |     int mode_match; | 
 | 48 |  | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 49 |     // Check if path needs to be created | 
 | 50 |     struct stat sb; | 
| Calin Juravle | 9812105 | 2016-05-24 16:11:04 +0100 | [diff] [blame] | 51 |     int create_result = -1; | 
| Jeff Sharkey | ddb1733 | 2012-09-13 14:47:23 -0700 | [diff] [blame] | 52 |     if (TEMP_FAILURE_RETRY(lstat(path, &sb)) == -1) { | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 53 |         if (errno == ENOENT) { | 
 | 54 |             goto create; | 
 | 55 |         } else { | 
| Jeff Sharkey | ddb1733 | 2012-09-13 14:47:23 -0700 | [diff] [blame] | 56 |             ALOGE("Failed to lstat(%s): %s", path, strerror(errno)); | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 57 |             return -1; | 
 | 58 |         } | 
 | 59 |     } | 
 | 60 |  | 
 | 61 |     // Exists, verify status | 
| Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 62 |     type_ok = prepare_as_dir ? S_ISDIR(sb.st_mode) : S_ISREG(sb.st_mode); | 
| Calin Juravle | 9812105 | 2016-05-24 16:11:04 +0100 | [diff] [blame] | 63 |     if (!type_ok) { | 
 | 64 |         ALOGE("Not a %s: %s", (prepare_as_dir ? "directory" : "regular file"), path); | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 65 |         return -1; | 
 | 66 |     } | 
| Calin Juravle | 9812105 | 2016-05-24 16:11:04 +0100 | [diff] [blame] | 67 |  | 
| Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 68 |     owner_match = ((sb.st_uid == uid) && (sb.st_gid == gid)); | 
 | 69 |     mode_match = ((sb.st_mode & ALL_PERMS) == mode); | 
| Jeff Sharkey | 8146403 | 2016-01-14 11:53:42 -0700 | [diff] [blame] | 70 |     if (owner_match && mode_match) { | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 71 |         return 0; | 
| Jeff Sharkey | cf94fe1 | 2016-01-12 12:42:32 -0700 | [diff] [blame] | 72 |     } else if (allow_fixup) { | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 73 |         goto fixup; | 
| Jeff Sharkey | cf94fe1 | 2016-01-12 12:42:32 -0700 | [diff] [blame] | 74 |     } else { | 
| Jeff Sharkey | 8146403 | 2016-01-14 11:53:42 -0700 | [diff] [blame] | 75 |         if (!owner_match) { | 
 | 76 |             ALOGE("Expected path %s with owner %d:%d but found %d:%d", | 
 | 77 |                     path, uid, gid, sb.st_uid, sb.st_gid); | 
 | 78 |             return -1; | 
 | 79 |         } else { | 
 | 80 |             ALOGW("Expected path %s with mode %o but found %o", | 
 | 81 |                     path, mode, (sb.st_mode & ALL_PERMS)); | 
 | 82 |             return 0; | 
 | 83 |         } | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 84 |     } | 
 | 85 |  | 
 | 86 | create: | 
| Calin Juravle | 9812105 | 2016-05-24 16:11:04 +0100 | [diff] [blame] | 87 |     create_result = prepare_as_dir | 
 | 88 |         ? TEMP_FAILURE_RETRY(mkdir(path, mode)) | 
| George Burgess IV | 09175a0 | 2017-01-25 17:25:57 -0800 | [diff] [blame] | 89 |         : TEMP_FAILURE_RETRY(open(path, O_CREAT | O_CLOEXEC | O_NOFOLLOW | O_RDONLY, 0644)); | 
| Calin Juravle | 9812105 | 2016-05-24 16:11:04 +0100 | [diff] [blame] | 90 |     if (create_result == -1) { | 
| Jeff Sharkey | 489609b | 2012-09-25 11:10:16 -0700 | [diff] [blame] | 91 |         if (errno != EEXIST) { | 
| Calin Juravle | 9812105 | 2016-05-24 16:11:04 +0100 | [diff] [blame] | 92 |             ALOGE("Failed to %s(%s): %s", | 
 | 93 |                     (prepare_as_dir ? "mkdir" : "open"), path, strerror(errno)); | 
| Jeff Sharkey | 489609b | 2012-09-25 11:10:16 -0700 | [diff] [blame] | 94 |             return -1; | 
 | 95 |         } | 
| Calin Juravle | 9812105 | 2016-05-24 16:11:04 +0100 | [diff] [blame] | 96 |     } else if (!prepare_as_dir) { | 
 | 97 |         // For regular files we need to make sure we close the descriptor | 
 | 98 |         if (close(create_result) == -1) { | 
 | 99 |             ALOGW("Failed to close file after create %s: %s", path, strerror(errno)); | 
 | 100 |         } | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 101 |     } | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 102 | fixup: | 
| Jeff Sharkey | ddb1733 | 2012-09-13 14:47:23 -0700 | [diff] [blame] | 103 |     if (TEMP_FAILURE_RETRY(chmod(path, mode)) == -1) { | 
 | 104 |         ALOGE("Failed to chmod(%s, %d): %s", path, mode, strerror(errno)); | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 105 |         return -1; | 
 | 106 |     } | 
| Jeff Sharkey | ddb1733 | 2012-09-13 14:47:23 -0700 | [diff] [blame] | 107 |     if (TEMP_FAILURE_RETRY(chown(path, uid, gid)) == -1) { | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 108 |         ALOGE("Failed to chown(%s, %d, %d): %s", path, uid, gid, strerror(errno)); | 
 | 109 |         return -1; | 
 | 110 |     } | 
 | 111 |  | 
 | 112 |     return 0; | 
 | 113 | } | 
 | 114 |  | 
| Jeff Sharkey | cf94fe1 | 2016-01-12 12:42:32 -0700 | [diff] [blame] | 115 | int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid) { | 
| Calin Juravle | 9812105 | 2016-05-24 16:11:04 +0100 | [diff] [blame] | 116 |     return fs_prepare_path_impl(path, mode, uid, gid, /*allow_fixup*/ 1, /*prepare_as_dir*/ 1); | 
| Jeff Sharkey | cf94fe1 | 2016-01-12 12:42:32 -0700 | [diff] [blame] | 117 | } | 
 | 118 |  | 
 | 119 | int fs_prepare_dir_strict(const char* path, mode_t mode, uid_t uid, gid_t gid) { | 
| Calin Juravle | 9812105 | 2016-05-24 16:11:04 +0100 | [diff] [blame] | 120 |     return fs_prepare_path_impl(path, mode, uid, gid, /*allow_fixup*/ 0, /*prepare_as_dir*/ 1); | 
 | 121 | } | 
 | 122 |  | 
 | 123 | int fs_prepare_file_strict(const char* path, mode_t mode, uid_t uid, gid_t gid) { | 
 | 124 |     return fs_prepare_path_impl(path, mode, uid, gid, /*allow_fixup*/ 0, /*prepare_as_dir*/ 0); | 
| Jeff Sharkey | cf94fe1 | 2016-01-12 12:42:32 -0700 | [diff] [blame] | 125 | } | 
 | 126 |  | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 127 | int fs_read_atomic_int(const char* path, int* out_value) { | 
| Jeff Sharkey | 6de7026 | 2012-09-13 15:11:42 -0700 | [diff] [blame] | 128 |     int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY)); | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 129 |     if (fd == -1) { | 
 | 130 |         ALOGE("Failed to read %s: %s", path, strerror(errno)); | 
 | 131 |         return -1; | 
 | 132 |     } | 
 | 133 |  | 
 | 134 |     char buf[BUF_SIZE]; | 
| Jeff Sharkey | 6de7026 | 2012-09-13 15:11:42 -0700 | [diff] [blame] | 135 |     if (TEMP_FAILURE_RETRY(read(fd, buf, BUF_SIZE)) == -1) { | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 136 |         ALOGE("Failed to read %s: %s", path, strerror(errno)); | 
 | 137 |         goto fail; | 
 | 138 |     } | 
 | 139 |     if (sscanf(buf, "%d", out_value) != 1) { | 
 | 140 |         ALOGE("Failed to parse %s: %s", path, strerror(errno)); | 
 | 141 |         goto fail; | 
 | 142 |     } | 
 | 143 |     close(fd); | 
 | 144 |     return 0; | 
 | 145 |  | 
 | 146 | fail: | 
 | 147 |     close(fd); | 
 | 148 |     *out_value = -1; | 
 | 149 |     return -1; | 
 | 150 | } | 
 | 151 |  | 
 | 152 | int fs_write_atomic_int(const char* path, int value) { | 
 | 153 |     char temp[PATH_MAX]; | 
 | 154 |     if (snprintf(temp, PATH_MAX, "%s.XXXXXX", path) >= PATH_MAX) { | 
 | 155 |         ALOGE("Path too long"); | 
 | 156 |         return -1; | 
 | 157 |     } | 
 | 158 |  | 
| Jeff Sharkey | 6de7026 | 2012-09-13 15:11:42 -0700 | [diff] [blame] | 159 |     int fd = TEMP_FAILURE_RETRY(mkstemp(temp)); | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 160 |     if (fd == -1) { | 
 | 161 |         ALOGE("Failed to open %s: %s", temp, strerror(errno)); | 
 | 162 |         return -1; | 
 | 163 |     } | 
 | 164 |  | 
 | 165 |     char buf[BUF_SIZE]; | 
 | 166 |     int len = snprintf(buf, BUF_SIZE, "%d", value) + 1; | 
 | 167 |     if (len > BUF_SIZE) { | 
 | 168 |         ALOGE("Value %d too large: %s", value, strerror(errno)); | 
 | 169 |         goto fail; | 
 | 170 |     } | 
| Jeff Sharkey | 6de7026 | 2012-09-13 15:11:42 -0700 | [diff] [blame] | 171 |     if (TEMP_FAILURE_RETRY(write(fd, buf, len)) < len) { | 
| Jeff Sharkey | 9685194 | 2012-08-27 15:03:37 -0700 | [diff] [blame] | 172 |         ALOGE("Failed to write %s: %s", temp, strerror(errno)); | 
 | 173 |         goto fail; | 
 | 174 |     } | 
 | 175 |     if (close(fd) == -1) { | 
 | 176 |         ALOGE("Failed to close %s: %s", temp, strerror(errno)); | 
 | 177 |         goto fail_closed; | 
 | 178 |     } | 
 | 179 |  | 
 | 180 |     if (rename(temp, path) == -1) { | 
 | 181 |         ALOGE("Failed to rename %s to %s: %s", temp, path, strerror(errno)); | 
 | 182 |         goto fail_closed; | 
 | 183 |     } | 
 | 184 |  | 
 | 185 |     return 0; | 
 | 186 |  | 
 | 187 | fail: | 
 | 188 |     close(fd); | 
 | 189 | fail_closed: | 
 | 190 |     unlink(temp); | 
 | 191 |     return -1; | 
 | 192 | } | 
| Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 193 |  | 
| Jeff Sharkey | 0ee7d8c | 2013-09-20 17:58:54 -0700 | [diff] [blame] | 194 | #ifndef __APPLE__ | 
 | 195 |  | 
| Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 196 | int fs_mkdirs(const char* path, mode_t mode) { | 
| Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 197 |     if (*path != '/') { | 
 | 198 |         ALOGE("Relative paths are not allowed: %s", path); | 
 | 199 |         return -EINVAL; | 
| Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 200 |     } | 
 | 201 |  | 
| Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 202 |     int fd = open("/", 0); | 
 | 203 |     if (fd == -1) { | 
| Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 204 |         ALOGE("Failed to open(/): %s", strerror(errno)); | 
| Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 205 |         return -errno; | 
| Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 206 |     } | 
 | 207 |  | 
| Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 208 |     struct stat sb; | 
 | 209 |     int res = 0; | 
 | 210 |     char* buf = strdup(path); | 
| Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 211 |     char* segment = buf + 1; | 
 | 212 |     char* p = segment; | 
 | 213 |     while (*p != '\0') { | 
 | 214 |         if (*p == '/') { | 
 | 215 |             *p = '\0'; | 
 | 216 |  | 
 | 217 |             if (!strcmp(segment, "..") || !strcmp(segment, ".") || !strcmp(segment, "")) { | 
 | 218 |                 ALOGE("Invalid path: %s", buf); | 
 | 219 |                 res = -EINVAL; | 
 | 220 |                 goto done_close; | 
 | 221 |             } | 
 | 222 |  | 
 | 223 |             if (fstatat(fd, segment, &sb, AT_SYMLINK_NOFOLLOW) != 0) { | 
 | 224 |                 if (errno == ENOENT) { | 
 | 225 |                     /* Nothing there yet; let's create it! */ | 
 | 226 |                     if (mkdirat(fd, segment, mode) != 0) { | 
 | 227 |                         if (errno == EEXIST) { | 
 | 228 |                             /* We raced with someone; ignore */ | 
 | 229 |                         } else { | 
 | 230 |                             ALOGE("Failed to mkdirat(%s): %s", buf, strerror(errno)); | 
 | 231 |                             res = -errno; | 
 | 232 |                             goto done_close; | 
 | 233 |                         } | 
 | 234 |                     } | 
 | 235 |                 } else { | 
 | 236 |                     ALOGE("Failed to fstatat(%s): %s", buf, strerror(errno)); | 
 | 237 |                     res = -errno; | 
 | 238 |                     goto done_close; | 
 | 239 |                 } | 
 | 240 |             } else { | 
 | 241 |                 if (S_ISLNK(sb.st_mode)) { | 
 | 242 |                     ALOGE("Symbolic links are not allowed: %s", buf); | 
 | 243 |                     res = -ELOOP; | 
 | 244 |                     goto done_close; | 
 | 245 |                 } | 
 | 246 |                 if (!S_ISDIR(sb.st_mode)) { | 
 | 247 |                     ALOGE("Existing segment not a directory: %s", buf); | 
 | 248 |                     res = -ENOTDIR; | 
 | 249 |                     goto done_close; | 
 | 250 |                 } | 
 | 251 |             } | 
 | 252 |  | 
 | 253 |             /* Yay, segment is ready for us to step into */ | 
 | 254 |             int next_fd; | 
| Nick Kralevich | 30a86eb | 2014-06-16 15:32:49 -0700 | [diff] [blame] | 255 |             if ((next_fd = openat(fd, segment, O_NOFOLLOW | O_CLOEXEC)) == -1) { | 
| Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 256 |                 ALOGE("Failed to openat(%s): %s", buf, strerror(errno)); | 
 | 257 |                 res = -errno; | 
 | 258 |                 goto done_close; | 
 | 259 |             } | 
 | 260 |  | 
 | 261 |             close(fd); | 
 | 262 |             fd = next_fd; | 
 | 263 |  | 
 | 264 |             *p = '/'; | 
 | 265 |             segment = p + 1; | 
 | 266 |         } | 
 | 267 |         p++; | 
 | 268 |     } | 
 | 269 |  | 
 | 270 | done_close: | 
 | 271 |     close(fd); | 
| Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 272 |     free(buf); | 
 | 273 |     return res; | 
 | 274 | } | 
| Jeff Sharkey | 0ee7d8c | 2013-09-20 17:58:54 -0700 | [diff] [blame] | 275 |  | 
 | 276 | #endif |