blob: ef85accce66fb0e0c2015d6b666e084134c20c45 [file] [log] [blame]
Jeff Sharkey96851942012-08-27 15:03:37 -07001/*
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 Hughes8e9aeb92017-11-10 10:22:07 -080017#include <cutils/fs.h>
18
Jeff Sharkeyddb17332012-09-13 14:47:23 -070019#define LOG_TAG "cutils"
20
Jeff Sharkey44d63422013-09-12 09:44:48 -070021/* 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 Salyzyn23ed4c22016-09-28 13:33:27 -070026#include <dirent.h>
Jeff Sharkey96851942012-08-27 15:03:37 -070027#include <errno.h>
Mark Salyzyn23ed4c22016-09-28 13:33:27 -070028#include <fcntl.h>
Jeff Sharkey96851942012-08-27 15:03:37 -070029#include <limits.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070030#include <stdio.h>
Nick Kralevich69ce4892012-10-15 15:51:33 -070031#include <stdlib.h>
Mark Salyzyn23ed4c22016-09-28 13:33:27 -070032#include <string.h>
33#include <sys/stat.h>
34#include <sys/types.h>
35#include <unistd.h>
36
Mark Salyzyn30f991f2017-01-10 13:19:54 -080037#include <log/log.h>
Jeff Sharkey96851942012-08-27 15:03:37 -070038
39#define ALL_PERMS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
40#define BUF_SIZE 64
41
Calin Juravle98121052016-05-24 16:11:04 +010042static 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 Hughes8e9aeb92017-11-10 10:22:07 -080044 // TODO: fix the goto hell below.
45 int type_ok;
46 int owner_match;
47 int mode_match;
48
Jeff Sharkey96851942012-08-27 15:03:37 -070049 // Check if path needs to be created
50 struct stat sb;
Calin Juravle98121052016-05-24 16:11:04 +010051 int create_result = -1;
Jeff Sharkeyddb17332012-09-13 14:47:23 -070052 if (TEMP_FAILURE_RETRY(lstat(path, &sb)) == -1) {
Jeff Sharkey96851942012-08-27 15:03:37 -070053 if (errno == ENOENT) {
54 goto create;
55 } else {
Jeff Sharkeyddb17332012-09-13 14:47:23 -070056 ALOGE("Failed to lstat(%s): %s", path, strerror(errno));
Jeff Sharkey96851942012-08-27 15:03:37 -070057 return -1;
58 }
59 }
60
61 // Exists, verify status
Elliott Hughes8e9aeb92017-11-10 10:22:07 -080062 type_ok = prepare_as_dir ? S_ISDIR(sb.st_mode) : S_ISREG(sb.st_mode);
Calin Juravle98121052016-05-24 16:11:04 +010063 if (!type_ok) {
64 ALOGE("Not a %s: %s", (prepare_as_dir ? "directory" : "regular file"), path);
Jeff Sharkey96851942012-08-27 15:03:37 -070065 return -1;
66 }
Calin Juravle98121052016-05-24 16:11:04 +010067
Elliott Hughes8e9aeb92017-11-10 10:22:07 -080068 owner_match = ((sb.st_uid == uid) && (sb.st_gid == gid));
69 mode_match = ((sb.st_mode & ALL_PERMS) == mode);
Jeff Sharkey81464032016-01-14 11:53:42 -070070 if (owner_match && mode_match) {
Jeff Sharkey96851942012-08-27 15:03:37 -070071 return 0;
Jeff Sharkeycf94fe12016-01-12 12:42:32 -070072 } else if (allow_fixup) {
Jeff Sharkey96851942012-08-27 15:03:37 -070073 goto fixup;
Jeff Sharkeycf94fe12016-01-12 12:42:32 -070074 } else {
Jeff Sharkey81464032016-01-14 11:53:42 -070075 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 Sharkey96851942012-08-27 15:03:37 -070084 }
85
86create:
Calin Juravle98121052016-05-24 16:11:04 +010087 create_result = prepare_as_dir
88 ? TEMP_FAILURE_RETRY(mkdir(path, mode))
George Burgess IV09175a02017-01-25 17:25:57 -080089 : TEMP_FAILURE_RETRY(open(path, O_CREAT | O_CLOEXEC | O_NOFOLLOW | O_RDONLY, 0644));
Calin Juravle98121052016-05-24 16:11:04 +010090 if (create_result == -1) {
Jeff Sharkey489609b2012-09-25 11:10:16 -070091 if (errno != EEXIST) {
Calin Juravle98121052016-05-24 16:11:04 +010092 ALOGE("Failed to %s(%s): %s",
93 (prepare_as_dir ? "mkdir" : "open"), path, strerror(errno));
Jeff Sharkey489609b2012-09-25 11:10:16 -070094 return -1;
95 }
Calin Juravle98121052016-05-24 16:11:04 +010096 } 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 Sharkey96851942012-08-27 15:03:37 -0700101 }
Jeff Sharkey96851942012-08-27 15:03:37 -0700102fixup:
Jeff Sharkeyddb17332012-09-13 14:47:23 -0700103 if (TEMP_FAILURE_RETRY(chmod(path, mode)) == -1) {
104 ALOGE("Failed to chmod(%s, %d): %s", path, mode, strerror(errno));
Jeff Sharkey96851942012-08-27 15:03:37 -0700105 return -1;
106 }
Jeff Sharkeyddb17332012-09-13 14:47:23 -0700107 if (TEMP_FAILURE_RETRY(chown(path, uid, gid)) == -1) {
Jeff Sharkey96851942012-08-27 15:03:37 -0700108 ALOGE("Failed to chown(%s, %d, %d): %s", path, uid, gid, strerror(errno));
109 return -1;
110 }
111
112 return 0;
113}
114
Jeff Sharkeycf94fe12016-01-12 12:42:32 -0700115int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid) {
Calin Juravle98121052016-05-24 16:11:04 +0100116 return fs_prepare_path_impl(path, mode, uid, gid, /*allow_fixup*/ 1, /*prepare_as_dir*/ 1);
Jeff Sharkeycf94fe12016-01-12 12:42:32 -0700117}
118
119int fs_prepare_dir_strict(const char* path, mode_t mode, uid_t uid, gid_t gid) {
Calin Juravle98121052016-05-24 16:11:04 +0100120 return fs_prepare_path_impl(path, mode, uid, gid, /*allow_fixup*/ 0, /*prepare_as_dir*/ 1);
121}
122
123int 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 Sharkeycf94fe12016-01-12 12:42:32 -0700125}
126
Jeff Sharkey96851942012-08-27 15:03:37 -0700127int fs_read_atomic_int(const char* path, int* out_value) {
Jeff Sharkey6de70262012-09-13 15:11:42 -0700128 int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY));
Jeff Sharkey96851942012-08-27 15:03:37 -0700129 if (fd == -1) {
130 ALOGE("Failed to read %s: %s", path, strerror(errno));
131 return -1;
132 }
133
134 char buf[BUF_SIZE];
Jeff Sharkey6de70262012-09-13 15:11:42 -0700135 if (TEMP_FAILURE_RETRY(read(fd, buf, BUF_SIZE)) == -1) {
Jeff Sharkey96851942012-08-27 15:03:37 -0700136 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
146fail:
147 close(fd);
148 *out_value = -1;
149 return -1;
150}
151
152int 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 Sharkey6de70262012-09-13 15:11:42 -0700159 int fd = TEMP_FAILURE_RETRY(mkstemp(temp));
Jeff Sharkey96851942012-08-27 15:03:37 -0700160 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 Sharkey6de70262012-09-13 15:11:42 -0700171 if (TEMP_FAILURE_RETRY(write(fd, buf, len)) < len) {
Jeff Sharkey96851942012-08-27 15:03:37 -0700172 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
187fail:
188 close(fd);
189fail_closed:
190 unlink(temp);
191 return -1;
192}
Jeff Sharkey44d63422013-09-12 09:44:48 -0700193
Jeff Sharkey0ee7d8c2013-09-20 17:58:54 -0700194#ifndef __APPLE__
195
Jeff Sharkey44d63422013-09-12 09:44:48 -0700196int fs_mkdirs(const char* path, mode_t mode) {
Elliott Hughes8e9aeb92017-11-10 10:22:07 -0800197 if (*path != '/') {
198 ALOGE("Relative paths are not allowed: %s", path);
199 return -EINVAL;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700200 }
201
Elliott Hughes8e9aeb92017-11-10 10:22:07 -0800202 int fd = open("/", 0);
203 if (fd == -1) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700204 ALOGE("Failed to open(/): %s", strerror(errno));
Elliott Hughes8e9aeb92017-11-10 10:22:07 -0800205 return -errno;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700206 }
207
Elliott Hughes8e9aeb92017-11-10 10:22:07 -0800208 struct stat sb;
209 int res = 0;
210 char* buf = strdup(path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700211 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 Kralevich30a86eb2014-06-16 15:32:49 -0700255 if ((next_fd = openat(fd, segment, O_NOFOLLOW | O_CLOEXEC)) == -1) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700256 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
270done_close:
271 close(fd);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700272 free(buf);
273 return res;
274}
Jeff Sharkey0ee7d8c2013-09-20 17:58:54 -0700275
276#endif