The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG SYNC |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sysdeps.h" |
| 20 | #include "file_sync_service.h" |
| 21 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 22 | #include <dirent.h> |
| 23 | #include <errno.h> |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 24 | #include <linux/xattr.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | #include <stdio.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 26 | #include <stdlib.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | #include <string.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | #include <sys/stat.h> |
| 29 | #include <sys/types.h> |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 30 | #include <sys/xattr.h> |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 31 | #include <unistd.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 32 | #include <utime.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 33 | |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 34 | #include <android-base/stringprintf.h> |
| 35 | #include <android-base/strings.h> |
| 36 | #include <private/android_filesystem_config.h> |
Mark Salyzyn | 6debf98 | 2016-10-05 08:13:56 -0700 | [diff] [blame] | 37 | #include <private/android_logger.h> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 38 | #include <selinux/android.h> |
| 39 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | #include "adb.h" |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 41 | #include "adb_io.h" |
Josh Gao | 45b6fc8 | 2015-11-04 14:51:23 -0800 | [diff] [blame] | 42 | #include "adb_utils.h" |
Rubin Xu | d61a25c | 2016-01-11 10:23:47 +0000 | [diff] [blame] | 43 | #include "security_log_tags.h" |
Josh Gao | 5a1e3fd | 2016-12-05 17:11:34 -0800 | [diff] [blame^] | 44 | #include "sysdeps/errno.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 45 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 46 | static bool should_use_fs_config(const std::string& path) { |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 47 | // TODO: use fs_config to configure permissions on /data. |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 48 | return android::base::StartsWith(path, "/system/") || |
| 49 | android::base::StartsWith(path, "/vendor/") || |
| 50 | android::base::StartsWith(path, "/oem/"); |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 53 | static bool update_capabilities(const char* path, uint64_t capabilities) { |
| 54 | if (capabilities == 0) { |
| 55 | // Ensure we clean up in case the capabilities weren't 0 in the past. |
| 56 | removexattr(path, XATTR_NAME_CAPS); |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | vfs_cap_data cap_data = {}; |
| 61 | cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE; |
| 62 | cap_data.data[0].permitted = (capabilities & 0xffffffff); |
| 63 | cap_data.data[0].inheritable = 0; |
| 64 | cap_data.data[1].permitted = (capabilities >> 32); |
| 65 | cap_data.data[1].inheritable = 0; |
| 66 | return setxattr(path, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) != -1; |
| 67 | } |
| 68 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 69 | static bool secure_mkdirs(const std::string& path) { |
Nick Kralevich | 7291783 | 2014-01-17 16:16:42 -0800 | [diff] [blame] | 70 | uid_t uid = -1; |
| 71 | gid_t gid = -1; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 72 | unsigned int mode = 0775; |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 73 | uint64_t capabilities = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 74 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 75 | if (path[0] != '/') return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 76 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 77 | std::vector<std::string> path_components = android::base::Split(path, "/"); |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 78 | std::string partial_path; |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 79 | for (const auto& path_component : path_components) { |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 80 | if (partial_path.back() != OS_PATH_SEPARATOR) partial_path += OS_PATH_SEPARATOR; |
| 81 | partial_path += path_component; |
| 82 | |
| 83 | if (should_use_fs_config(partial_path)) { |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 84 | fs_config(partial_path.c_str(), 1, nullptr, &uid, &gid, &mode, &capabilities); |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 85 | } |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 86 | if (adb_mkdir(partial_path.c_str(), mode) == -1) { |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 87 | if (errno != EEXIST) { |
| 88 | return false; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 89 | } |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 90 | } else { |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 91 | if (chown(partial_path.c_str(), uid, gid) == -1) return false; |
| 92 | |
Elliott Hughes | 3834440 | 2015-08-25 16:33:50 -0700 | [diff] [blame] | 93 | // Not all filesystems support setting SELinux labels. http://b/23530370. |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 94 | selinux_android_restorecon(partial_path.c_str(), 0); |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 95 | |
| 96 | if (!update_capabilities(partial_path.c_str(), capabilities)) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 97 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 98 | } |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 99 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Josh Gao | 5a1e3fd | 2016-12-05 17:11:34 -0800 | [diff] [blame^] | 102 | static bool do_lstat_v1(int s, const char* path) { |
| 103 | syncmsg msg = {}; |
| 104 | msg.stat_v1.id = ID_LSTAT_V1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 105 | |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 106 | struct stat st = {}; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 107 | lstat(path, &st); |
Josh Gao | 5a1e3fd | 2016-12-05 17:11:34 -0800 | [diff] [blame^] | 108 | msg.stat_v1.mode = st.st_mode; |
| 109 | msg.stat_v1.size = st.st_size; |
| 110 | msg.stat_v1.time = st.st_mtime; |
| 111 | return WriteFdExactly(s, &msg.stat_v1, sizeof(msg.stat_v1)); |
| 112 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 113 | |
Josh Gao | 5a1e3fd | 2016-12-05 17:11:34 -0800 | [diff] [blame^] | 114 | static bool do_stat_v2(int s, uint32_t id, const char* path) { |
| 115 | syncmsg msg = {}; |
| 116 | msg.stat_v2.id = id; |
| 117 | |
| 118 | decltype(&stat) stat_fn; |
| 119 | if (id == ID_STAT_V2) { |
| 120 | stat_fn = stat; |
| 121 | } else { |
| 122 | stat_fn = lstat; |
| 123 | } |
| 124 | |
| 125 | struct stat st = {}; |
| 126 | int rc = stat_fn(path, &st); |
| 127 | if (rc == -1) { |
| 128 | msg.stat_v2.error = errno_to_wire(errno); |
| 129 | } else { |
| 130 | msg.stat_v2.dev = st.st_dev; |
| 131 | msg.stat_v2.ino = st.st_ino; |
| 132 | msg.stat_v2.mode = st.st_mode; |
| 133 | msg.stat_v2.nlink = st.st_nlink; |
| 134 | msg.stat_v2.uid = st.st_uid; |
| 135 | msg.stat_v2.gid = st.st_gid; |
| 136 | msg.stat_v2.size = st.st_size; |
| 137 | msg.stat_v2.atime = st.st_atime; |
| 138 | msg.stat_v2.mtime = st.st_mtime; |
| 139 | msg.stat_v2.ctime = st.st_ctime; |
| 140 | } |
| 141 | |
| 142 | return WriteFdExactly(s, &msg.stat_v2, sizeof(msg.stat_v2)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 145 | static bool do_list(int s, const char* path) { |
| 146 | dirent* de; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 147 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 148 | syncmsg msg; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 149 | msg.dent.id = ID_DENT; |
| 150 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 151 | std::unique_ptr<DIR, int(*)(DIR*)> d(opendir(path), closedir); |
| 152 | if (!d) goto done; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 153 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 154 | while ((de = readdir(d.get()))) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 155 | std::string filename(android::base::StringPrintf("%s/%s", path, de->d_name)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 156 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 157 | struct stat st; |
| 158 | if (lstat(filename.c_str(), &st) == 0) { |
| 159 | size_t d_name_length = strlen(de->d_name); |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 160 | msg.dent.mode = st.st_mode; |
| 161 | msg.dent.size = st.st_size; |
| 162 | msg.dent.time = st.st_mtime; |
| 163 | msg.dent.namelen = d_name_length; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 164 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 165 | if (!WriteFdExactly(s, &msg.dent, sizeof(msg.dent)) || |
| 166 | !WriteFdExactly(s, de->d_name, d_name_length)) { |
| 167 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 172 | done: |
| 173 | msg.dent.id = ID_DONE; |
| 174 | msg.dent.mode = 0; |
| 175 | msg.dent.size = 0; |
| 176 | msg.dent.time = 0; |
| 177 | msg.dent.namelen = 0; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 178 | return WriteFdExactly(s, &msg.dent, sizeof(msg.dent)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Josh Gao | 6ad06b9 | 2016-01-28 16:21:12 -0800 | [diff] [blame] | 181 | // Make sure that SendFail from adb_io.cpp isn't accidentally used in this file. |
| 182 | #pragma GCC poison SendFail |
| 183 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 184 | static bool SendSyncFail(int fd, const std::string& reason) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 185 | D("sync: failure: %s", reason.c_str()); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 186 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 187 | syncmsg msg; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 188 | msg.data.id = ID_FAIL; |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 189 | msg.data.size = reason.size(); |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 190 | return WriteFdExactly(fd, &msg.data, sizeof(msg.data)) && WriteFdExactly(fd, reason); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 193 | static bool SendSyncFailErrno(int fd, const std::string& reason) { |
| 194 | return SendSyncFail(fd, android::base::StringPrintf("%s: %s", reason.c_str(), strerror(errno))); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 197 | static bool handle_send_file(int s, const char* path, uid_t uid, gid_t gid, uint64_t capabilities, |
| 198 | mode_t mode, std::vector<char>& buffer, bool do_unlink) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 199 | syncmsg msg; |
| 200 | unsigned int timestamp = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 201 | |
Rubin Xu | d61a25c | 2016-01-11 10:23:47 +0000 | [diff] [blame] | 202 | __android_log_security_bswrite(SEC_TAG_ADB_SEND_FILE, path); |
| 203 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 204 | int fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode); |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 205 | if (fd < 0 && errno == ENOENT) { |
Josh Gao | 45b6fc8 | 2015-11-04 14:51:23 -0800 | [diff] [blame] | 206 | if (!secure_mkdirs(adb_dirname(path))) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 207 | SendSyncFailErrno(s, "secure_mkdirs failed"); |
| 208 | goto fail; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 209 | } |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 210 | fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 211 | } |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 212 | if (fd < 0 && errno == EEXIST) { |
Nick Kralevich | fe8d7f4 | 2014-07-18 20:57:35 -0700 | [diff] [blame] | 213 | fd = adb_open_mode(path, O_WRONLY | O_CLOEXEC, mode); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 214 | } |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 215 | if (fd < 0) { |
| 216 | SendSyncFailErrno(s, "couldn't create file"); |
| 217 | goto fail; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 218 | } else { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 219 | if (fchown(fd, uid, gid) == -1) { |
| 220 | SendSyncFailErrno(s, "fchown failed"); |
| 221 | goto fail; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 222 | } |
Nick Kralevich | 7291783 | 2014-01-17 16:16:42 -0800 | [diff] [blame] | 223 | |
Elliott Hughes | 3834440 | 2015-08-25 16:33:50 -0700 | [diff] [blame] | 224 | // Not all filesystems support setting SELinux labels. http://b/23530370. |
| 225 | selinux_android_restorecon(path, 0); |
Elliott Hughes | 0adc097 | 2015-08-25 13:14:07 -0700 | [diff] [blame] | 226 | |
| 227 | // fchown clears the setuid bit - restore it if present. |
| 228 | // Ignore the result of calling fchmod. It's not supported |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 229 | // by all filesystems, so we don't check for success. b/12441485 |
Nick Kralevich | 7291783 | 2014-01-17 16:16:42 -0800 | [diff] [blame] | 230 | fchmod(fd, mode); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 233 | while (true) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 234 | if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) goto fail; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 235 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 236 | if (msg.data.id != ID_DATA) { |
| 237 | if (msg.data.id == ID_DONE) { |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 238 | timestamp = msg.data.size; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 239 | break; |
| 240 | } |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 241 | SendSyncFail(s, "invalid data message"); |
Josh Gao | 20a96c7 | 2016-02-19 14:33:14 -0800 | [diff] [blame] | 242 | goto abort; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 243 | } |
Josh Gao | 20a96c7 | 2016-02-19 14:33:14 -0800 | [diff] [blame] | 244 | |
| 245 | if (msg.data.size > buffer.size()) { // TODO: resize buffer? |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 246 | SendSyncFail(s, "oversize data message"); |
Josh Gao | 20a96c7 | 2016-02-19 14:33:14 -0800 | [diff] [blame] | 247 | goto abort; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 248 | } |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 249 | |
Josh Gao | 20a96c7 | 2016-02-19 14:33:14 -0800 | [diff] [blame] | 250 | if (!ReadFdExactly(s, &buffer[0], msg.data.size)) goto abort; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 251 | |
Josh Gao | 20a96c7 | 2016-02-19 14:33:14 -0800 | [diff] [blame] | 252 | if (!WriteFdExactly(fd, &buffer[0], msg.data.size)) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 253 | SendSyncFailErrno(s, "write failed"); |
| 254 | goto fail; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 258 | adb_close(fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 259 | |
Jin Qian | d0515e7 | 2016-11-02 16:27:17 -0700 | [diff] [blame] | 260 | if (!update_capabilities(path, capabilities)) { |
| 261 | SendSyncFailErrno(s, "update_capabilities failed"); |
| 262 | goto fail; |
| 263 | } |
| 264 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 265 | utimbuf u; |
| 266 | u.actime = timestamp; |
| 267 | u.modtime = timestamp; |
| 268 | utime(path, &u); |
| 269 | |
| 270 | msg.status.id = ID_OKAY; |
| 271 | msg.status.msglen = 0; |
| 272 | return WriteFdExactly(s, &msg.status, sizeof(msg.status)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 273 | |
| 274 | fail: |
Josh Gao | 20a96c7 | 2016-02-19 14:33:14 -0800 | [diff] [blame] | 275 | // If there's a problem on the device, we'll send an ID_FAIL message and |
| 276 | // close the socket. Unfortunately the kernel will sometimes throw that |
| 277 | // data away if the other end keeps writing without reading (which is |
| 278 | // the case with old versions of adb). To maintain compatibility, keep |
| 279 | // reading and throwing away ID_DATA packets until the other side notices |
| 280 | // that we've reported an error. |
| 281 | while (true) { |
| 282 | if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) goto fail; |
| 283 | |
| 284 | if (msg.data.id == ID_DONE) { |
| 285 | goto abort; |
| 286 | } else if (msg.data.id != ID_DATA) { |
| 287 | char id[5]; |
| 288 | memcpy(id, &msg.data.id, sizeof(msg.data.id)); |
| 289 | id[4] = '\0'; |
| 290 | D("handle_send_fail received unexpected id '%s' during failure", id); |
| 291 | goto abort; |
| 292 | } |
| 293 | |
| 294 | if (msg.data.size > buffer.size()) { |
| 295 | D("handle_send_fail received oversized packet of length '%u' during failure", |
| 296 | msg.data.size); |
| 297 | goto abort; |
| 298 | } |
| 299 | |
| 300 | if (!ReadFdExactly(s, &buffer[0], msg.data.size)) goto abort; |
| 301 | } |
| 302 | |
| 303 | abort: |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 304 | if (fd >= 0) adb_close(fd); |
JP Abgrall | 55b6c84 | 2014-03-07 17:31:25 -0800 | [diff] [blame] | 305 | if (do_unlink) adb_unlink(path); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 306 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 309 | #if defined(_WIN32) |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 310 | extern bool handle_send_link(int s, const std::string& path, std::vector<char>& buffer) __attribute__((error("no symlinks on Windows"))); |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 311 | #else |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 312 | static bool handle_send_link(int s, const std::string& path, std::vector<char>& buffer) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 313 | syncmsg msg; |
| 314 | unsigned int len; |
| 315 | int ret; |
| 316 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 317 | if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 318 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 319 | if (msg.data.id != ID_DATA) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 320 | SendSyncFail(s, "invalid data message: expected ID_DATA"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 321 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 324 | len = msg.data.size; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 325 | if (len > buffer.size()) { // TODO: resize buffer? |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 326 | SendSyncFail(s, "oversize data message"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 327 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 328 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 329 | if (!ReadFdExactly(s, &buffer[0], len)) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 330 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 331 | ret = symlink(&buffer[0], path.c_str()); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 332 | if (ret && errno == ENOENT) { |
Josh Gao | 45b6fc8 | 2015-11-04 14:51:23 -0800 | [diff] [blame] | 333 | if (!secure_mkdirs(adb_dirname(path))) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 334 | SendSyncFailErrno(s, "secure_mkdirs failed"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 335 | return false; |
Liang Cheng | 20d33f4 | 2014-01-02 18:27:51 -0800 | [diff] [blame] | 336 | } |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 337 | ret = symlink(&buffer[0], path.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 338 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 339 | if (ret) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 340 | SendSyncFailErrno(s, "symlink failed"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 341 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 344 | if (!ReadFdExactly(s, &msg.data, sizeof(msg.data))) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 345 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 346 | if (msg.data.id == ID_DONE) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 347 | msg.status.id = ID_OKAY; |
| 348 | msg.status.msglen = 0; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 349 | if (!WriteFdExactly(s, &msg.status, sizeof(msg.status))) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 350 | } else { |
Josh Gao | 6ad06b9 | 2016-01-28 16:21:12 -0800 | [diff] [blame] | 351 | SendSyncFail(s, "invalid data message: expected ID_DONE"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 352 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 355 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 356 | } |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 357 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 358 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 359 | static bool do_send(int s, const std::string& spec, std::vector<char>& buffer) { |
| 360 | // 'spec' is of the form "/some/path,0755". Break it up. |
| 361 | size_t comma = spec.find_last_of(','); |
| 362 | if (comma == std::string::npos) { |
Josh Gao | 6ad06b9 | 2016-01-28 16:21:12 -0800 | [diff] [blame] | 363 | SendSyncFail(s, "missing , in ID_SEND"); |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 364 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 367 | std::string path = spec.substr(0, comma); |
| 368 | |
| 369 | errno = 0; |
| 370 | mode_t mode = strtoul(spec.substr(comma + 1).c_str(), nullptr, 0); |
| 371 | if (errno != 0) { |
Josh Gao | 6ad06b9 | 2016-01-28 16:21:12 -0800 | [diff] [blame] | 372 | SendSyncFail(s, "bad mode"); |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 373 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 376 | // Don't delete files before copying if they are not "regular" or symlinks. |
| 377 | struct stat st; |
| 378 | bool do_unlink = (lstat(path.c_str(), &st) == -1) || S_ISREG(st.st_mode) || S_ISLNK(st.st_mode); |
| 379 | if (do_unlink) { |
| 380 | adb_unlink(path.c_str()); |
| 381 | } |
| 382 | |
| 383 | if (S_ISLNK(mode)) { |
| 384 | return handle_send_link(s, path.c_str(), buffer); |
| 385 | } |
| 386 | |
| 387 | // Copy user permission bits to "group" and "other" permissions. |
| 388 | mode &= 0777; |
| 389 | mode |= ((mode >> 3) & 0070); |
| 390 | mode |= ((mode >> 3) & 0007); |
| 391 | |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 392 | uid_t uid = -1; |
| 393 | gid_t gid = -1; |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 394 | uint64_t capabilities = 0; |
Elliott Hughes | ec7a667 | 2015-03-16 21:58:32 +0000 | [diff] [blame] | 395 | if (should_use_fs_config(path)) { |
Elliott Hughes | 7baecbe | 2015-08-25 11:09:04 -0700 | [diff] [blame] | 396 | unsigned int broken_api_hack = mode; |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 397 | fs_config(path.c_str(), 0, nullptr, &uid, &gid, &broken_api_hack, &capabilities); |
Elliott Hughes | 56bf309 | 2015-08-25 11:01:39 -0700 | [diff] [blame] | 398 | mode = broken_api_hack; |
Elliott Hughes | 0a049b1 | 2015-01-12 14:26:36 -0800 | [diff] [blame] | 399 | } |
Elliott Hughes | 32c60b4 | 2016-06-07 17:18:25 -0700 | [diff] [blame] | 400 | return handle_send_file(s, path.c_str(), uid, gid, capabilities, mode, buffer, do_unlink); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 401 | } |
| 402 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 403 | static bool do_recv(int s, const char* path, std::vector<char>& buffer) { |
Rubin Xu | d61a25c | 2016-01-11 10:23:47 +0000 | [diff] [blame] | 404 | __android_log_security_bswrite(SEC_TAG_ADB_RECV_FILE, path); |
| 405 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 406 | int fd = adb_open(path, O_RDONLY | O_CLOEXEC); |
| 407 | if (fd < 0) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 408 | SendSyncFailErrno(s, "open failed"); |
| 409 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 412 | syncmsg msg; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 413 | msg.data.id = ID_DATA; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 414 | while (true) { |
| 415 | int r = adb_read(fd, &buffer[0], buffer.size()); |
| 416 | if (r <= 0) { |
| 417 | if (r == 0) break; |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 418 | SendSyncFailErrno(s, "read failed"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 419 | adb_close(fd); |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 420 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 421 | } |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 422 | msg.data.size = r; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 423 | if (!WriteFdExactly(s, &msg.data, sizeof(msg.data)) || !WriteFdExactly(s, &buffer[0], r)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 424 | adb_close(fd); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 425 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
| 429 | adb_close(fd); |
| 430 | |
| 431 | msg.data.id = ID_DONE; |
| 432 | msg.data.size = 0; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 433 | return WriteFdExactly(s, &msg.data, sizeof(msg.data)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 436 | static bool handle_sync_command(int fd, std::vector<char>& buffer) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 437 | D("sync: waiting for request"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 438 | |
| 439 | SyncRequest request; |
| 440 | if (!ReadFdExactly(fd, &request, sizeof(request))) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 441 | SendSyncFail(fd, "command read failure"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 442 | return false; |
| 443 | } |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 444 | size_t path_length = request.path_length; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 445 | if (path_length > 1024) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 446 | SendSyncFail(fd, "path too long"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 447 | return false; |
| 448 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 449 | char name[1025]; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 450 | if (!ReadFdExactly(fd, name, path_length)) { |
Elliott Hughes | fdd4e58 | 2015-08-24 19:30:46 -0700 | [diff] [blame] | 451 | SendSyncFail(fd, "filename read failure"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 452 | return false; |
| 453 | } |
| 454 | name[path_length] = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 455 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 456 | const char* id = reinterpret_cast<const char*>(&request.id); |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 457 | D("sync: '%.4s' '%s'", id, name); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 458 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 459 | switch (request.id) { |
Josh Gao | 5a1e3fd | 2016-12-05 17:11:34 -0800 | [diff] [blame^] | 460 | case ID_LSTAT_V1: |
| 461 | if (!do_lstat_v1(fd, name)) return false; |
| 462 | break; |
| 463 | case ID_LSTAT_V2: |
| 464 | case ID_STAT_V2: |
| 465 | if (!do_stat_v2(fd, request.id, name)) return false; |
| 466 | break; |
| 467 | case ID_LIST: |
| 468 | if (!do_list(fd, name)) return false; |
| 469 | break; |
| 470 | case ID_SEND: |
| 471 | if (!do_send(fd, name, buffer)) return false; |
| 472 | break; |
| 473 | case ID_RECV: |
| 474 | if (!do_recv(fd, name, buffer)) return false; |
| 475 | break; |
| 476 | case ID_QUIT: |
| 477 | return false; |
| 478 | default: |
| 479 | SendSyncFail( |
| 480 | fd, android::base::StringPrintf("unknown command '%.4s' (%08x)", id, request.id)); |
| 481 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 482 | } |
| 483 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 484 | return true; |
| 485 | } |
| 486 | |
Josh Gao | 5a1e3fd | 2016-12-05 17:11:34 -0800 | [diff] [blame^] | 487 | void file_sync_service(int fd, void*) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 488 | std::vector<char> buffer(SYNC_DATA_MAX); |
| 489 | |
| 490 | while (handle_sync_command(fd, buffer)) { |
| 491 | } |
| 492 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 493 | D("sync: done"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 494 | adb_close(fd); |
| 495 | } |