blob: ea019f458d388e874d1680479c76f1f33bb3e831 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 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
Dan Albert33134262015-03-19 15:21:08 -070017#define TRACE_TAG TRACE_SYNC
18
19#include "sysdeps.h"
20#include "file_sync_service.h"
21
Dan Albert76649012015-02-24 15:51:19 -080022#include <dirent.h>
23#include <errno.h>
24#include <selinux/android.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025#include <stdio.h>
Dan Albert76649012015-02-24 15:51:19 -080026#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028#include <sys/stat.h>
29#include <sys/types.h>
Liang Cheng20d33f42014-01-02 18:27:51 -080030#include <unistd.h>
Dan Albert76649012015-02-24 15:51:19 -080031#include <utime.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033#include "adb.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080034#include "adb_io.h"
Dan Albert76649012015-02-24 15:51:19 -080035#include "private/android_filesystem_config.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036
Elliott Hughes5c742702015-07-30 17:42:01 -070037#include <base/strings.h>
38
39static bool should_use_fs_config(const std::string& path) {
Elliott Hughesec7a6672015-03-16 21:58:32 +000040 // TODO: use fs_config to configure permissions on /data.
Elliott Hughes5c742702015-07-30 17:42:01 -070041 return android::base::StartsWith(path, "/system/") ||
42 android::base::StartsWith(path, "/vendor/") ||
43 android::base::StartsWith(path, "/oem/");
Daniel Rosenberg686bce62014-06-30 20:29:40 -070044}
45
Alex Vallée47d67c92015-05-06 16:26:00 -040046static bool secure_mkdirs(const std::string& path) {
Nick Kralevich72917832014-01-17 16:16:42 -080047 uid_t uid = -1;
48 gid_t gid = -1;
Liang Cheng20d33f42014-01-02 18:27:51 -080049 unsigned int mode = 0775;
50 uint64_t cap = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051
Alex Vallée47d67c92015-05-06 16:26:00 -040052 if (path[0] != '/') return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
Elliott Hughes5c742702015-07-30 17:42:01 -070054 std::vector<std::string> path_components = android::base::Split(path, "/");
55 path_components.pop_back(); // For "/system/bin/sh", only create "/system/bin".
56
57 std::string partial_path;
58 for (auto& path_component : path_components) {
59 if (partial_path.back() != OS_PATH_SEPARATOR) partial_path += OS_PATH_SEPARATOR;
60 partial_path += path_component;
61
62 if (should_use_fs_config(partial_path)) {
63 fs_config(partial_path.c_str(), 1, &uid, &gid, &mode, &cap);
Liang Cheng20d33f42014-01-02 18:27:51 -080064 }
Elliott Hughes5c742702015-07-30 17:42:01 -070065 if (adb_mkdir(partial_path.c_str(), mode) == -1) {
Alex Vallée47d67c92015-05-06 16:26:00 -040066 if (errno != EEXIST) {
67 return false;
Liang Cheng20d33f42014-01-02 18:27:51 -080068 }
Alex Vallée47d67c92015-05-06 16:26:00 -040069 } else {
Elliott Hughes5c742702015-07-30 17:42:01 -070070 if (chown(partial_path.c_str(), uid, gid) == -1) {
Alex Vallée47d67c92015-05-06 16:26:00 -040071 return false;
72 }
Elliott Hughes5c742702015-07-30 17:42:01 -070073 selinux_android_restorecon(partial_path.c_str(), 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075 }
Alex Vallée47d67c92015-05-06 16:26:00 -040076 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077}
78
79static int do_stat(int s, const char *path)
80{
81 syncmsg msg;
82 struct stat st;
83
84 msg.stat.id = ID_STAT;
85
86 if(lstat(path, &st)) {
87 msg.stat.mode = 0;
88 msg.stat.size = 0;
89 msg.stat.time = 0;
90 } else {
91 msg.stat.mode = htoll(st.st_mode);
92 msg.stat.size = htoll(st.st_size);
93 msg.stat.time = htoll(st.st_mtime);
94 }
95
Dan Albertcc731cc2015-02-24 21:26:58 -080096 return WriteFdExactly(s, &msg.stat, sizeof(msg.stat)) ? 0 : -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097}
98
99static int do_list(int s, const char *path)
100{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101 struct dirent *de;
102 struct stat st;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103
104 char tmp[1024 + 256 + 1];
105 char *fname;
106
Elliott Hughes5c742702015-07-30 17:42:01 -0700107 size_t len = strlen(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108 memcpy(tmp, path, len);
109 tmp[len] = '/';
110 fname = tmp + len + 1;
111
Elliott Hughes5c742702015-07-30 17:42:01 -0700112 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113 msg.dent.id = ID_DENT;
114
Elliott Hughes5c742702015-07-30 17:42:01 -0700115 std::unique_ptr<DIR, int(*)(DIR*)> d(opendir(path), closedir);
116 if (!d) goto done;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117
Elliott Hughes5c742702015-07-30 17:42:01 -0700118 while ((de = readdir(d.get()))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119 int len = strlen(de->d_name);
120
121 /* not supposed to be possible, but
122 if it does happen, let's not buffer overrun */
123 if(len > 256) continue;
124
125 strcpy(fname, de->d_name);
126 if(lstat(tmp, &st) == 0) {
127 msg.dent.mode = htoll(st.st_mode);
128 msg.dent.size = htoll(st.st_size);
129 msg.dent.time = htoll(st.st_mtime);
130 msg.dent.namelen = htoll(len);
131
Dan Albertcc731cc2015-02-24 21:26:58 -0800132 if(!WriteFdExactly(s, &msg.dent, sizeof(msg.dent)) ||
133 !WriteFdExactly(s, de->d_name, len)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134 return -1;
135 }
136 }
137 }
138
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139done:
140 msg.dent.id = ID_DONE;
141 msg.dent.mode = 0;
142 msg.dent.size = 0;
143 msg.dent.time = 0;
144 msg.dent.namelen = 0;
Dan Albertf3519a82015-03-09 13:22:02 -0700145 return WriteFdExactly(s, &msg.dent, sizeof(msg.dent)) ? 0 : -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146}
147
148static int fail_message(int s, const char *reason)
149{
150 syncmsg msg;
151 int len = strlen(reason);
152
153 D("sync: failure: %s\n", reason);
154
155 msg.data.id = ID_FAIL;
156 msg.data.size = htoll(len);
Dan Albertcc731cc2015-02-24 21:26:58 -0800157 if(!WriteFdExactly(s, &msg.data, sizeof(msg.data)) ||
158 !WriteFdExactly(s, reason, len)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159 return -1;
160 } else {
161 return 0;
162 }
163}
164
165static int fail_errno(int s)
166{
167 return fail_message(s, strerror(errno));
168}
169
Nick Kralevich72917832014-01-17 16:16:42 -0800170static int handle_send_file(int s, char *path, uid_t uid,
JP Abgrall55b6c842014-03-07 17:31:25 -0800171 gid_t gid, mode_t mode, char *buffer, bool do_unlink)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172{
173 syncmsg msg;
174 unsigned int timestamp = 0;
175 int fd;
176
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700177 fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178 if(fd < 0 && errno == ENOENT) {
Alex Vallée47d67c92015-05-06 16:26:00 -0400179 if (!secure_mkdirs(path)) {
Liang Cheng20d33f42014-01-02 18:27:51 -0800180 if(fail_errno(s))
181 return -1;
182 fd = -1;
183 } else {
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700184 fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
Liang Cheng20d33f42014-01-02 18:27:51 -0800185 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 }
187 if(fd < 0 && errno == EEXIST) {
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700188 fd = adb_open_mode(path, O_WRONLY | O_CLOEXEC, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189 }
190 if(fd < 0) {
191 if(fail_errno(s))
192 return -1;
193 fd = -1;
Liang Cheng20d33f42014-01-02 18:27:51 -0800194 } else {
195 if(fchown(fd, uid, gid) != 0) {
196 fail_errno(s);
197 errno = 0;
198 }
Nick Kralevich72917832014-01-17 16:16:42 -0800199
200 /*
201 * fchown clears the setuid bit - restore it if present.
202 * Ignore the result of calling fchmod. It's not supported
203 * by all filesystems. b/12441485
204 */
205 fchmod(fd, mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 }
207
208 for(;;) {
209 unsigned int len;
210
Dan Albertcc731cc2015-02-24 21:26:58 -0800211 if(!ReadFdExactly(s, &msg.data, sizeof(msg.data)))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212 goto fail;
213
214 if(msg.data.id != ID_DATA) {
215 if(msg.data.id == ID_DONE) {
216 timestamp = ltohl(msg.data.size);
217 break;
218 }
219 fail_message(s, "invalid data message");
220 goto fail;
221 }
222 len = ltohl(msg.data.size);
223 if(len > SYNC_DATA_MAX) {
224 fail_message(s, "oversize data message");
225 goto fail;
226 }
Dan Albertcc731cc2015-02-24 21:26:58 -0800227 if(!ReadFdExactly(s, buffer, len))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228 goto fail;
229
230 if(fd < 0)
231 continue;
Dan Albertcc731cc2015-02-24 21:26:58 -0800232 if(!WriteFdExactly(fd, buffer, len)) {
JP Abgrall408fa572011-03-16 15:57:42 -0700233 int saved_errno = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 adb_close(fd);
JP Abgrall55b6c842014-03-07 17:31:25 -0800235 if (do_unlink) adb_unlink(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236 fd = -1;
JP Abgrall408fa572011-03-16 15:57:42 -0700237 errno = saved_errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 if(fail_errno(s)) return -1;
239 }
240 }
241
242 if(fd >= 0) {
243 struct utimbuf u;
244 adb_close(fd);
Stephen Smalley27a93652014-02-07 09:14:13 -0500245 selinux_android_restorecon(path, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 u.actime = timestamp;
247 u.modtime = timestamp;
248 utime(path, &u);
249
250 msg.status.id = ID_OKAY;
251 msg.status.msglen = 0;
Dan Albertcc731cc2015-02-24 21:26:58 -0800252 if(!WriteFdExactly(s, &msg.status, sizeof(msg.status)))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253 return -1;
254 }
255 return 0;
256
257fail:
258 if(fd >= 0)
259 adb_close(fd);
JP Abgrall55b6c842014-03-07 17:31:25 -0800260 if (do_unlink) adb_unlink(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261 return -1;
262}
263
Elliott Hughes0a049b12015-01-12 14:26:36 -0800264#if defined(_WIN32)
265extern int handle_send_link(int s, char *path, char *buffer) __attribute__((error("no symlinks on Windows")));
266#else
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267static int handle_send_link(int s, char *path, char *buffer)
268{
269 syncmsg msg;
270 unsigned int len;
271 int ret;
272
Dan Albertcc731cc2015-02-24 21:26:58 -0800273 if(!ReadFdExactly(s, &msg.data, sizeof(msg.data)))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800274 return -1;
275
276 if(msg.data.id != ID_DATA) {
277 fail_message(s, "invalid data message: expected ID_DATA");
278 return -1;
279 }
280
281 len = ltohl(msg.data.size);
282 if(len > SYNC_DATA_MAX) {
283 fail_message(s, "oversize data message");
284 return -1;
285 }
Dan Albertcc731cc2015-02-24 21:26:58 -0800286 if(!ReadFdExactly(s, buffer, len))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800287 return -1;
288
289 ret = symlink(buffer, path);
290 if(ret && errno == ENOENT) {
Alex Vallée47d67c92015-05-06 16:26:00 -0400291 if (!secure_mkdirs(path)) {
Liang Cheng20d33f42014-01-02 18:27:51 -0800292 fail_errno(s);
293 return -1;
294 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295 ret = symlink(buffer, path);
296 }
297 if(ret) {
298 fail_errno(s);
299 return -1;
300 }
301
Dan Albertcc731cc2015-02-24 21:26:58 -0800302 if(!ReadFdExactly(s, &msg.data, sizeof(msg.data)))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303 return -1;
304
305 if(msg.data.id == ID_DONE) {
306 msg.status.id = ID_OKAY;
307 msg.status.msglen = 0;
Dan Albertcc731cc2015-02-24 21:26:58 -0800308 if(!WriteFdExactly(s, &msg.status, sizeof(msg.status)))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309 return -1;
310 } else {
311 fail_message(s, "invalid data message: expected ID_DONE");
312 return -1;
313 }
314
315 return 0;
316}
Elliott Hughes0a049b12015-01-12 14:26:36 -0800317#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800318
319static int do_send(int s, char *path, char *buffer)
320{
Liang Cheng20d33f42014-01-02 18:27:51 -0800321 unsigned int mode;
Elliott Hughes0a049b12015-01-12 14:26:36 -0800322 bool is_link = false;
JP Abgrall55b6c842014-03-07 17:31:25 -0800323 bool do_unlink;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324
Elliott Hughes0a049b12015-01-12 14:26:36 -0800325 char* tmp = strrchr(path,',');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326 if(tmp) {
327 *tmp = 0;
328 errno = 0;
329 mode = strtoul(tmp + 1, NULL, 0);
Liang Cheng20d33f42014-01-02 18:27:51 -0800330 is_link = S_ISLNK((mode_t) mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 mode &= 0777;
332 }
333 if(!tmp || errno) {
334 mode = 0644;
335 is_link = 0;
JP Abgrall55b6c842014-03-07 17:31:25 -0800336 do_unlink = true;
JP Abgrall4ce2f832013-12-03 14:52:39 -0800337 } else {
338 struct stat st;
339 /* Don't delete files before copying if they are not "regular" */
JP Abgrall55b6c842014-03-07 17:31:25 -0800340 do_unlink = lstat(path, &st) || S_ISREG(st.st_mode) || S_ISLNK(st.st_mode);
341 if (do_unlink) {
JP Abgrall4ce2f832013-12-03 14:52:39 -0800342 adb_unlink(path);
343 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344 }
345
Elliott Hughes0a049b12015-01-12 14:26:36 -0800346 if (is_link) {
347 return handle_send_link(s, path, buffer);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348 }
349
Elliott Hughes0a049b12015-01-12 14:26:36 -0800350 uid_t uid = -1;
351 gid_t gid = -1;
352 uint64_t cap = 0;
353
354 /* copy user permission bits to "group" and "other" permissions */
355 mode |= ((mode >> 3) & 0070);
356 mode |= ((mode >> 3) & 0007);
357
358 tmp = path;
359 if(*tmp == '/') {
360 tmp++;
361 }
Elliott Hughesec7a6672015-03-16 21:58:32 +0000362 if (should_use_fs_config(path)) {
Elliott Hughes0a049b12015-01-12 14:26:36 -0800363 fs_config(tmp, 0, &uid, &gid, &mode, &cap);
364 }
365 return handle_send_file(s, path, uid, gid, mode, buffer, do_unlink);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366}
367
368static int do_recv(int s, const char *path, char *buffer)
369{
370 syncmsg msg;
371 int fd, r;
372
Nick Kralevichfe8d7f42014-07-18 20:57:35 -0700373 fd = adb_open(path, O_RDONLY | O_CLOEXEC);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374 if(fd < 0) {
375 if(fail_errno(s)) return -1;
376 return 0;
377 }
378
379 msg.data.id = ID_DATA;
380 for(;;) {
381 r = adb_read(fd, buffer, SYNC_DATA_MAX);
382 if(r <= 0) {
383 if(r == 0) break;
384 if(errno == EINTR) continue;
385 r = fail_errno(s);
386 adb_close(fd);
387 return r;
388 }
389 msg.data.size = htoll(r);
Dan Albertcc731cc2015-02-24 21:26:58 -0800390 if(!WriteFdExactly(s, &msg.data, sizeof(msg.data)) ||
391 !WriteFdExactly(s, buffer, r)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392 adb_close(fd);
393 return -1;
394 }
395 }
396
397 adb_close(fd);
398
399 msg.data.id = ID_DONE;
400 msg.data.size = 0;
Dan Albertcc731cc2015-02-24 21:26:58 -0800401 if(!WriteFdExactly(s, &msg.data, sizeof(msg.data))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 return -1;
403 }
404
405 return 0;
406}
407
408void file_sync_service(int fd, void *cookie)
409{
410 syncmsg msg;
411 char name[1025];
412 unsigned namelen;
413
Dan Albertbac34742015-02-25 17:51:28 -0800414 char *buffer = reinterpret_cast<char*>(malloc(SYNC_DATA_MAX));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800415 if(buffer == 0) goto fail;
416
417 for(;;) {
418 D("sync: waiting for command\n");
419
Dan Albertcc731cc2015-02-24 21:26:58 -0800420 if(!ReadFdExactly(fd, &msg.req, sizeof(msg.req))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421 fail_message(fd, "command read failure");
422 break;
423 }
424 namelen = ltohl(msg.req.namelen);
425 if(namelen > 1024) {
426 fail_message(fd, "invalid namelen");
427 break;
428 }
Dan Albertcc731cc2015-02-24 21:26:58 -0800429 if(!ReadFdExactly(fd, name, namelen)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800430 fail_message(fd, "filename read failure");
431 break;
432 }
433 name[namelen] = 0;
434
435 msg.req.namelen = 0;
436 D("sync: '%s' '%s'\n", (char*) &msg.req, name);
437
438 switch(msg.req.id) {
439 case ID_STAT:
440 if(do_stat(fd, name)) goto fail;
441 break;
442 case ID_LIST:
443 if(do_list(fd, name)) goto fail;
444 break;
445 case ID_SEND:
446 if(do_send(fd, name, buffer)) goto fail;
447 break;
448 case ID_RECV:
449 if(do_recv(fd, name, buffer)) goto fail;
450 break;
451 case ID_QUIT:
452 goto fail;
453 default:
454 fail_message(fd, "unknown command");
455 goto fail;
456 }
457 }
458
459fail:
460 if(buffer != 0) free(buffer);
461 D("sync: done\n");
462 adb_close(fd);
463}