blob: 3d83eb794e17adc741ab8b8bff5e9fa0ce55ccf9 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +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
17#include <stdlib.h>
18#include <stdio.h>
19#include <string.h>
20
21#include <sys/stat.h>
22#include <sys/types.h>
23#include <dirent.h>
24#include <utime.h>
25#include <unistd.h>
26
27#include <errno.h>
28#include <private/android_filesystem_config.h>
29//#include <selinux/android.h> //changed by feilv
30#include "sysdeps.h"
31
32#define TRACE_TAG TRACE_SYNC
33#include "adb.h"
34#include "file_sync_service.h"
35
b.liub17525e2025-05-14 17:22:29 +080036#ifdef MBTK_ADB_SEC_SUPPORT
37static char* mbtk_sec_files[] = {"/etc/passwd", "/etc/shadow", "/bin/adb_shell", "/bin/busybox", "/usr/bin/adbd", NULL};
38
39static bool file_name_cmp(const char *name1, const char *name2) {
40 char tmp_name1[1025] = {0};
41 char tmp_name2[1025] = {0};
42 int i = 0;
43 int j = 0;
44 while(i < strlen(name1)) {
45 if(name1[i] != '/')
46 tmp_name1[j++] = name1[i];
47 i++;
48 }
49
50 i = 0;
51 j = 0;
52 while(i < strlen(name2)) {
53 if(name2[i] != '/')
54 tmp_name2[j++] = name2[i];
55 i++;
56 }
57
58 return strcmp(tmp_name1, tmp_name2);
59}
60
61static bool mbtk_sec_file_check(bool is_pull, const char *name) {
62 struct stat file_stat;
63 memset(&file_stat, 0, sizeof(struct stat));
64 if(lstat(name, &file_stat)) { // Fail.
65 D("MBTK : lstat fail : %d\n", errno);
66 if(errno == ENOENT) { // File not exist.
67 return true;
68 } else {
69 return false;
70 }
71 }
72
73 if(S_ISREG(file_stat.st_mode)) {
74 D("MBTK : FILE REG.\n");
75 char **ptr = mbtk_sec_files;
76 while(*ptr) {
77 if(file_name_cmp(*ptr, name) == 0)
78 return false;
79 ptr++;
80 }
81 return true;
82 } else if(S_ISCHR(file_stat.st_mode)
83 || S_ISBLK(file_stat.st_mode)) {
84 D("MBTK : FILE CHR/BLK.\n");
85 if(is_pull) {
86 return true;
87 } else {
88 return false;
89 }
90 } else {
91 return false;
92 }
93}
94#endif
95
b.liue9582032025-04-17 19:18:16 +080096/* TODO: use fs_config to configure permissions on /data */
97static bool is_on_system(const char *name) {
98 const char *SYSTEM = "/system/";
99 return (strncmp(SYSTEM, name, strlen(SYSTEM)) == 0);
100}
101
102static int mkdirs(char *name)
103{
104 int ret;
105 char *x = name + 1;
106 uid_t uid = -1;
107 gid_t gid = -1;
108 unsigned int mode = 0775;
109 uint64_t cap = 0;
110
111 if(name[0] != '/') return -1;
112
113 for(;;) {
114 x = adb_dirstart(x);
115 if(x == 0) return 0;
116 *x = 0;
117 if (is_on_system(name)) {
118 fs_config(name, 1, &uid, &gid, &mode, &cap);
119 }
120 ret = adb_mkdir(name, mode);
121 if((ret < 0) && (errno != EEXIST)) {
122 D("mkdir(\"%s\") -> %s\n", name, strerror(errno));
123 *x = '/';
124 return ret;
125 } else if(ret == 0) {
126 ret = chown(name, uid, gid);
127 if (ret < 0) {
128 *x = '/';
129 return ret;
130 }
131 //selinux_android_restorecon(name, 0);
132 }
133 *x++ = '/';
134 }
135 return 0;
136}
137
138static int do_stat(int s, const char *path)
139{
140 syncmsg msg;
141 struct stat st;
142
143 msg.stat.id = ID_STAT;
144
145 if(lstat(path, &st)) {
146 msg.stat.mode = 0;
147 msg.stat.size = 0;
148 msg.stat.time = 0;
149 } else {
150 msg.stat.mode = htoll(st.st_mode);
151 msg.stat.size = htoll(st.st_size);
152 msg.stat.time = htoll(st.st_mtime);
153 }
154
155 return writex(s, &msg.stat, sizeof(msg.stat));
156}
157
158static int do_list(int s, const char *path)
159{
160 DIR *d;
161 struct dirent *de;
162 struct stat st;
163 syncmsg msg;
164 int len;
165
166 char tmp[1024 + 256 + 1];
167 char *fname;
168
169 len = strlen(path);
170 memcpy(tmp, path, len);
171 tmp[len] = '/';
172 fname = tmp + len + 1;
173
174 msg.dent.id = ID_DENT;
175
176 d = opendir(path);
177 if(d == 0) goto done;
178
179 while((de = readdir(d))) {
180 int len = strlen(de->d_name);
181
182 /* not supposed to be possible, but
183 if it does happen, let's not buffer overrun */
184 if(len > 256) continue;
185
186 strcpy(fname, de->d_name);
187 if(lstat(tmp, &st) == 0) {
188 msg.dent.mode = htoll(st.st_mode);
189 msg.dent.size = htoll(st.st_size);
190 msg.dent.time = htoll(st.st_mtime);
191 msg.dent.namelen = htoll(len);
192
193 if(writex(s, &msg.dent, sizeof(msg.dent)) ||
194 writex(s, de->d_name, len)) {
195 closedir(d);
196 return -1;
197 }
198 }
199 }
200
201 closedir(d);
202
203done:
204 msg.dent.id = ID_DONE;
205 msg.dent.mode = 0;
206 msg.dent.size = 0;
207 msg.dent.time = 0;
208 msg.dent.namelen = 0;
209 return writex(s, &msg.dent, sizeof(msg.dent));
210}
211
212static int fail_message(int s, const char *reason)
213{
214 syncmsg msg;
215 int len = strlen(reason);
216
217 D("sync: failure: %s\n", reason);
218
219 msg.data.id = ID_FAIL;
220 msg.data.size = htoll(len);
221 if(writex(s, &msg.data, sizeof(msg.data)) ||
222 writex(s, reason, len)) {
223 return -1;
224 } else {
225 return 0;
226 }
227}
228
229static int fail_errno(int s)
230{
231 return fail_message(s, strerror(errno));
232}
233
234static int handle_send_file(int s, char *path, uid_t uid,
235 gid_t gid, mode_t mode, char *buffer, bool do_unlink)
236{
237 syncmsg msg;
238 unsigned int timestamp = 0;
239 int fd;
240
241 fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
242 if(fd < 0 && errno == ENOENT) {
243 if(mkdirs(path) != 0) {
244 if(fail_errno(s))
245 return -1;
246 fd = -1;
247 } else {
248 fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
249 }
250 }
251 if(fd < 0 && errno == EEXIST) {
252 fd = adb_open_mode(path, O_WRONLY, mode);
253 }
254 if(fd < 0) {
255 if(fail_errno(s))
256 return -1;
257 fd = -1;
258 } else {
259 if(fchown(fd, uid, gid) != 0) {
260 fail_errno(s);
261 errno = 0;
262 }
263
264 /*
265 * fchown clears the setuid bit - restore it if present.
266 * Ignore the result of calling fchmod. It's not supported
267 * by all filesystems. b/12441485
268 */
269 fchmod(fd, mode);
270 }
271
272 for(;;) {
273 unsigned int len;
274
275 if(readx(s, &msg.data, sizeof(msg.data)))
276 goto fail;
277
278 if(msg.data.id != ID_DATA) {
279 if(msg.data.id == ID_DONE) {
280 timestamp = ltohl(msg.data.size);
281 break;
282 }
283 fail_message(s, "invalid data message");
284 goto fail;
285 }
286 len = ltohl(msg.data.size);
287 if(len > SYNC_DATA_MAX) {
288 fail_message(s, "oversize data message");
289 goto fail;
290 }
291 if(readx(s, buffer, len))
292 goto fail;
293
294 if(fd < 0)
295 continue;
296 if(writex(fd, buffer, len)) {
297 int saved_errno = errno;
298 adb_close(fd);
299 if (do_unlink) adb_unlink(path);
300 fd = -1;
301 errno = saved_errno;
302 if(fail_errno(s)) return -1;
303 }
304 }
305
306 if(fd >= 0) {
307 struct utimbuf u;
308 adb_close(fd);
309 //selinux_android_restorecon(path, 0);
310 u.actime = timestamp;
311 u.modtime = timestamp;
312 utime(path, &u);
313
314 msg.status.id = ID_OKAY;
315 msg.status.msglen = 0;
316 if(writex(s, &msg.status, sizeof(msg.status)))
317 return -1;
318 }
319 return 0;
320
321fail:
322 if(fd >= 0)
323 adb_close(fd);
324 if (do_unlink) adb_unlink(path);
325 return -1;
326}
327
328#ifdef HAVE_SYMLINKS
329static int handle_send_link(int s, char *path, char *buffer)
330{
331 syncmsg msg;
332 unsigned int len;
333 int ret;
334
335 if(readx(s, &msg.data, sizeof(msg.data)))
336 return -1;
337
338 if(msg.data.id != ID_DATA) {
339 fail_message(s, "invalid data message: expected ID_DATA");
340 return -1;
341 }
342
343 len = ltohl(msg.data.size);
344 if(len > SYNC_DATA_MAX) {
345 fail_message(s, "oversize data message");
346 return -1;
347 }
348 if(readx(s, buffer, len))
349 return -1;
350
351 ret = symlink(buffer, path);
352 if(ret && errno == ENOENT) {
353 if(mkdirs(path) != 0) {
354 fail_errno(s);
355 return -1;
356 }
357 ret = symlink(buffer, path);
358 }
359 if(ret) {
360 fail_errno(s);
361 return -1;
362 }
363
364 if(readx(s, &msg.data, sizeof(msg.data)))
365 return -1;
366
367 if(msg.data.id == ID_DONE) {
368 msg.status.id = ID_OKAY;
369 msg.status.msglen = 0;
370 if(writex(s, &msg.status, sizeof(msg.status)))
371 return -1;
372 } else {
373 fail_message(s, "invalid data message: expected ID_DONE");
374 return -1;
375 }
376
377 return 0;
378}
379#endif /* HAVE_SYMLINKS */
380
381static int do_send(int s, char *path, char *buffer)
382{
383 char *tmp;
384 unsigned int mode;
385 int is_link, ret;
386 bool do_unlink;
387
388 tmp = strrchr(path,',');
389 if(tmp) {
390 *tmp = 0;
391 errno = 0;
392 mode = strtoul(tmp + 1, NULL, 0);
393#ifndef HAVE_SYMLINKS
394 is_link = 0;
395#else
396 is_link = S_ISLNK((mode_t) mode);
397#endif
398 mode &= 0777;
399 }
400 if(!tmp || errno) {
401 mode = 0644;
402 is_link = 0;
403 do_unlink = true;
404 } else {
405 struct stat st;
406 /* Don't delete files before copying if they are not "regular" */
407 do_unlink = lstat(path, &st) || S_ISREG(st.st_mode) || S_ISLNK(st.st_mode);
408 if (do_unlink) {
409 adb_unlink(path);
410 }
411 }
412
413#ifdef HAVE_SYMLINKS
414 if(is_link)
415 ret = handle_send_link(s, path, buffer);
416 else {
417#else
418 { (void)is_link;
419#endif
420 uid_t uid = -1;
421 gid_t gid = -1;
422 uint64_t cap = 0;
423
424 /* copy user permission bits to "group" and "other" permissions */
425 mode |= ((mode >> 3) & 0070);
426 mode |= ((mode >> 3) & 0007);
427
428 tmp = path;
429 if(*tmp == '/') {
430 tmp++;
431 }
432 if (is_on_system(path)) {
433 fs_config(tmp, 0, &uid, &gid, &mode, &cap);
434 }
435 ret = handle_send_file(s, path, uid, gid, mode, buffer, do_unlink);
436 }
437
438 return ret;
439}
440
441static int do_recv(int s, const char *path, char *buffer)
442{
443 syncmsg msg;
444 int fd, r;
445
446 fd = adb_open(path, O_RDONLY);
447 if(fd < 0) {
448 if(fail_errno(s)) return -1;
449 return 0;
450 }
451
452 msg.data.id = ID_DATA;
453 for(;;) {
454 r = adb_read(fd, buffer, SYNC_DATA_MAX);
455 if(r <= 0) {
456 if(r == 0) break;
457 if(errno == EINTR) continue;
458 r = fail_errno(s);
459 adb_close(fd);
460 return r;
461 }
462 msg.data.size = htoll(r);
463 if(writex(s, &msg.data, sizeof(msg.data)) ||
464 writex(s, buffer, r)) {
465 adb_close(fd);
466 return -1;
467 }
468 }
469
470 adb_close(fd);
471
472 msg.data.id = ID_DONE;
473 msg.data.size = 0;
474 if(writex(s, &msg.data, sizeof(msg.data))) {
475 return -1;
476 }
477
478 return 0;
479}
480
481void file_sync_service(int fd, void *cookie)
482{
483 syncmsg msg;
484 char name[1025];
485 unsigned namelen;
b.liub17525e2025-05-14 17:22:29 +0800486#ifdef MBTK_ADB_SEC_SUPPORT
487 mode_t mode = 0;
488 char file_name[1025] = {0};
489#endif
b.liue9582032025-04-17 19:18:16 +0800490
491 char *buffer = malloc(SYNC_DATA_MAX);
492 if(buffer == 0) goto fail;
493
494 for(;;) {
495 D("sync: waiting for command\n");
496
497 if(readx(fd, &msg.req, sizeof(msg.req))) {
498 fail_message(fd, "command read failure");
499 break;
500 }
501 namelen = ltohl(msg.req.namelen);
502 if(namelen > 1024) {
503 fail_message(fd, "invalid namelen");
504 break;
505 }
506 if(readx(fd, name, namelen)) {
507 fail_message(fd, "filename read failure");
508 break;
509 }
510 name[namelen] = 0;
511
512 msg.req.namelen = 0;
513 D("sync: '%s' '%s'\n", (char*) &msg.req, name);
b.liub17525e2025-05-14 17:22:29 +0800514#ifdef MBTK_ADB_SEC_SUPPORT
515 if(msg.req.id == ID_SEND) { // push
516 // '/usr/bin/adbd,33206'
517 char *ptr = strstr(name, ",");
518 if(ptr && ptr > name) {
519 struct stat file_stat;
520 mode = 0;
521 memset(file_name, 0, sizeof(file_name));
522 memcpy(file_name, name, ptr - name);
523
524 if(!mbtk_sec_file_check(false, file_name)) {
525 break;
526 }
527
528 memset(&file_stat, 0, sizeof(struct stat));
529 if(stat(file_name, &file_stat)) { // Fail.
530 mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH/* | S_IXOTH*/;
531 } else {
532 if(file_stat.st_mode & S_IRUSR) {
533 mode |= S_IRUSR;
534 }
535 if(file_stat.st_mode & S_IWUSR) {
536 mode |= S_IWUSR;
537 }
538 if(file_stat.st_mode & S_IXUSR) {
539 mode |= S_IXUSR;
540 }
541 if(file_stat.st_mode & S_IRGRP) {
542 mode |= S_IRGRP;
543 }
544 if(file_stat.st_mode & S_IWGRP) {
545 mode |= S_IWGRP;
546 }
547 if(file_stat.st_mode & S_IXGRP) {
548 mode |= S_IXGRP;
549 }
550 if(file_stat.st_mode & S_IROTH) {
551 mode |= S_IROTH;
552 }
553 if(file_stat.st_mode & S_IWOTH) {
554 mode |= S_IWOTH;
555 }
556 if(file_stat.st_mode & S_IXOTH) {
557 mode |= S_IXOTH;
558 }
559 }
560
561 D("MBTK : %s mode is %x\n", file_name, mode);
562 }
563 } else if(msg.req.id == ID_RECV) { // pull
564 if(!mbtk_sec_file_check(true, name)) {
565 break;
566 }
567 }
568#endif
b.liue9582032025-04-17 19:18:16 +0800569
570 switch(msg.req.id) {
571 case ID_STAT:
572 if(do_stat(fd, name)) goto fail;
573 break;
574 case ID_LIST:
575 if(do_list(fd, name)) goto fail;
576 break;
577 case ID_SEND:
578 if(do_send(fd, name, buffer)) goto fail;
b.liub17525e2025-05-14 17:22:29 +0800579#ifdef MBTK_ADB_SEC_SUPPORT
580 if(strlen(file_name) > 0) {
581 if(chmod(file_name, mode)) {
582 D("MBTK : chmod(%s, %x) fail.\n", file_name, mode);
583 } else {
584 D("MBTK : chmod(%s, %x) success.\n", file_name, mode);
585 system("sync");
586 }
587 }
588#endif
b.liue9582032025-04-17 19:18:16 +0800589 break;
590 case ID_RECV:
591 if(do_recv(fd, name, buffer)) goto fail;
592 break;
593 case ID_QUIT:
594 goto fail;
595 default:
596 fail_message(fd, "unknown command");
597 goto fail;
598 }
599 }
600
601fail:
602 if(buffer != 0) free(buffer);
603 D("sync: done\n");
604 adb_close(fd);
605}