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