Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 1 | // Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "cros-disks/sshfs_helper.h" |
| 6 | |
| 7 | #include <algorithm> |
François Degros | a28315e | 2020-07-13 00:24:48 +1000 | [diff] [blame] | 8 | #include <utility> |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 9 | |
Sergei Datsenko | ad2cb6a | 2018-05-15 17:34:26 +1000 | [diff] [blame] | 10 | #include <base/base64.h> |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 11 | #include <base/check.h> |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 12 | #include <base/files/file_path.h> |
| 13 | #include <base/files/file_util.h> |
Sergei Datsenko | 71ba50a | 2020-11-27 14:33:39 +1100 | [diff] [blame] | 14 | #include <base/files/scoped_temp_dir.h> |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 15 | #include <base/strings/string_number_conversions.h> |
| 16 | #include <base/strings/string_util.h> |
| 17 | |
| 18 | #include "cros-disks/fuse_mounter.h" |
| 19 | #include "cros-disks/mount_options.h" |
Anand K Mistry | 25a5f85 | 2020-01-14 17:08:39 +1100 | [diff] [blame] | 20 | #include "cros-disks/mount_point.h" |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 21 | #include "cros-disks/platform.h" |
François Degros | 8b4e31e | 2019-07-29 11:39:19 +1000 | [diff] [blame] | 22 | #include "cros-disks/quote.h" |
Sergei Datsenko | 88035aa | 2020-11-15 00:24:01 +1100 | [diff] [blame] | 23 | #include "cros-disks/sandboxed_process.h" |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 24 | #include "cros-disks/uri.h" |
| 25 | |
| 26 | namespace cros_disks { |
| 27 | |
| 28 | namespace { |
| 29 | |
Sergei Datsenko | 71ba50a | 2020-11-27 14:33:39 +1100 | [diff] [blame] | 30 | constexpr char kUserName[] = "fuse-sshfs"; |
| 31 | constexpr char kHelperTool[] = "/usr/bin/sshfs"; |
| 32 | constexpr char kType[] = "sshfs"; |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 33 | |
Sergei Datsenko | 71ba50a | 2020-11-27 14:33:39 +1100 | [diff] [blame] | 34 | constexpr char kOptionIdentityFile[] = "IdentityFile"; |
| 35 | constexpr char kOptionIdentityBase64[] = "IdentityBase64"; |
| 36 | constexpr char kOptionUserKnownHostsFile[] = "UserKnownHostsFile"; |
| 37 | constexpr char kOptionUserKnownHostsBase64[] = "UserKnownHostsBase64"; |
| 38 | constexpr char kOptionHostName[] = "HostName"; |
| 39 | constexpr char kOptionPort[] = "Port"; |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 40 | |
Sergei Datsenko | 71ba50a | 2020-11-27 14:33:39 +1100 | [diff] [blame] | 41 | constexpr char kIdentityFile[] = "id"; |
| 42 | constexpr char kUserKnownHostsFile[] = "known_hosts"; |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 43 | |
Sergei Datsenko | 71ba50a | 2020-11-27 14:33:39 +1100 | [diff] [blame] | 44 | OwnerUser ResolveSshfsUser(const Platform* platform) { |
| 45 | OwnerUser user; |
| 46 | PCHECK(platform->GetUserAndGroupId(kUserName, &user.uid, &user.gid)); |
| 47 | return user; |
| 48 | } |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 49 | |
Sergei Datsenko | 71ba50a | 2020-11-27 14:33:39 +1100 | [diff] [blame] | 50 | MountErrorType WriteConfigurationFile(const Platform* platform, |
| 51 | const OwnerUser& owner, |
| 52 | const base::FilePath& path, |
| 53 | const std::string& b64_data) { |
| 54 | std::string data; |
| 55 | if (!base::Base64Decode(b64_data, &data)) { |
| 56 | LOG(ERROR) << "Invalid base64 value for " << quote(path); |
| 57 | return MOUNT_ERROR_INVALID_MOUNT_OPTIONS; |
Anand K Mistry | 25a5f85 | 2020-01-14 17:08:39 +1100 | [diff] [blame] | 58 | } |
Sergei Datsenko | 71ba50a | 2020-11-27 14:33:39 +1100 | [diff] [blame] | 59 | |
| 60 | if (platform->WriteFile(path.value(), data.c_str(), data.size()) != |
| 61 | static_cast<int>(data.size())) { |
| 62 | PLOG(ERROR) << "Cannot write file " << quote(path); |
| 63 | return MOUNT_ERROR_INSUFFICIENT_PERMISSIONS; |
| 64 | } |
| 65 | |
| 66 | if (!platform->SetPermissions(path.value(), 0600) || |
| 67 | !platform->SetOwnership(path.value(), owner.uid, owner.gid)) { |
| 68 | PLOG(ERROR) << "Cannot change owner of file " << quote(path); |
| 69 | return MOUNT_ERROR_INSUFFICIENT_PERMISSIONS; |
| 70 | } |
| 71 | |
| 72 | return MOUNT_ERROR_NONE; |
| 73 | } |
Anand K Mistry | 25a5f85 | 2020-01-14 17:08:39 +1100 | [diff] [blame] | 74 | |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 75 | } // namespace |
| 76 | |
Sergei Datsenko | a910bba | 2019-06-18 13:31:59 +1000 | [diff] [blame] | 77 | SshfsHelper::SshfsHelper(const Platform* platform, |
Sergei Datsenko | 71ba50a | 2020-11-27 14:33:39 +1100 | [diff] [blame] | 78 | brillo::ProcessReaper* process_reaper, |
| 79 | base::FilePath working_dir) |
| 80 | : FUSEMounterHelper(platform, |
| 81 | process_reaper, |
| 82 | kType, |
| 83 | /* nosymfollow= */ true, |
| 84 | &sandbox_factory_), |
| 85 | sandbox_factory_(platform, |
| 86 | SandboxedExecutable{base::FilePath(kHelperTool)}, |
| 87 | ResolveSshfsUser(platform), |
| 88 | /* has_network_access= */ true), |
| 89 | working_dir_(std::move(working_dir)) {} |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 90 | |
| 91 | SshfsHelper::~SshfsHelper() = default; |
| 92 | |
Sergei Datsenko | 71ba50a | 2020-11-27 14:33:39 +1100 | [diff] [blame] | 93 | bool SshfsHelper::CanMount(const std::string& source, |
| 94 | const std::vector<std::string>& params, |
| 95 | base::FilePath* suggested_name) const { |
| 96 | const Uri uri = Uri::Parse(source); |
| 97 | if (!uri.valid() || uri.scheme() != kType) |
| 98 | return false; |
| 99 | |
| 100 | if (uri.path().empty()) { |
| 101 | *suggested_name = base::FilePath(kType); |
| 102 | } else { |
| 103 | std::string path = uri.path(); |
| 104 | std::replace(path.begin(), path.end(), '/', '$'); |
| 105 | std::replace(path.begin(), path.end(), '.', '_'); |
| 106 | *suggested_name = base::FilePath(path); |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 107 | } |
Sergei Datsenko | 71ba50a | 2020-11-27 14:33:39 +1100 | [diff] [blame] | 108 | return true; |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 109 | } |
| 110 | |
Sergei Datsenko | 71ba50a | 2020-11-27 14:33:39 +1100 | [diff] [blame] | 111 | MountErrorType SshfsHelper::ConfigureSandbox(const std::string& source, |
| 112 | const base::FilePath& target_path, |
| 113 | std::vector<std::string> params, |
| 114 | SandboxedProcess* sandbox) const { |
| 115 | const Uri uri = Uri::Parse(source); |
| 116 | if (!uri.valid() || uri.scheme() != kType || uri.path().empty()) { |
François Degros | c309d93 | 2021-02-04 15:12:30 +1100 | [diff] [blame] | 117 | LOG(ERROR) << "Invalid source " << quote(source); |
Sergei Datsenko | 71ba50a | 2020-11-27 14:33:39 +1100 | [diff] [blame] | 118 | return MOUNT_ERROR_INVALID_DEVICE_PATH; |
| 119 | } |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 120 | |
Sergei Datsenko | 71ba50a | 2020-11-27 14:33:39 +1100 | [diff] [blame] | 121 | std::string b64_identity; |
| 122 | if (!GetParamValue(params, kOptionIdentityBase64, &b64_identity) || |
| 123 | b64_identity.empty()) { |
| 124 | LOG(ERROR) << "Missing required parameter " << kOptionIdentityBase64; |
| 125 | return MOUNT_ERROR_INVALID_MOUNT_OPTIONS; |
| 126 | } |
| 127 | std::string b64_known_hosts; |
| 128 | if (!GetParamValue(params, kOptionUserKnownHostsBase64, &b64_known_hosts) || |
| 129 | b64_known_hosts.empty()) { |
| 130 | LOG(ERROR) << "Missing required parameter " << kOptionUserKnownHostsBase64; |
| 131 | return MOUNT_ERROR_INVALID_MOUNT_OPTIONS; |
| 132 | } |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 133 | |
Sergei Datsenko | 71ba50a | 2020-11-27 14:33:39 +1100 | [diff] [blame] | 134 | std::string path; |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 135 | |
Sergei Datsenko | 71ba50a | 2020-11-27 14:33:39 +1100 | [diff] [blame] | 136 | // TODO(dats): Consider plumbing hooks that would allow removing this |
| 137 | // directory after unmount. |
| 138 | if (!platform()->CreateTemporaryDirInDir(working_dir_.value(), "sshfs-", |
| 139 | &path)) { |
| 140 | PLOG(ERROR) << "Cannot create temporary directory inside " |
| 141 | << quote(working_dir_); |
| 142 | return MOUNT_ERROR_INSUFFICIENT_PERMISSIONS; |
| 143 | } |
| 144 | base::FilePath working_dir(path); |
| 145 | base::FilePath identity_file = working_dir.Append(kIdentityFile); |
| 146 | base::FilePath known_hosts_file = working_dir.Append(kUserKnownHostsFile); |
| 147 | |
| 148 | MountErrorType error = WriteConfigurationFile( |
| 149 | platform(), sandbox_factory_.run_as(), identity_file, b64_identity); |
| 150 | if (error != MOUNT_ERROR_NONE) { |
| 151 | return error; |
| 152 | } |
| 153 | error = WriteConfigurationFile(platform(), sandbox_factory_.run_as(), |
| 154 | known_hosts_file, b64_known_hosts); |
| 155 | if (error != MOUNT_ERROR_NONE) { |
| 156 | return error; |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // We retain group ownership on the directory to allow potential cleanup |
| 160 | // of its contents. |
| 161 | if (!platform()->SetPermissions(working_dir.value(), 0770) || |
Sergei Datsenko | 71ba50a | 2020-11-27 14:33:39 +1100 | [diff] [blame] | 162 | !platform()->SetOwnership(working_dir.value(), |
| 163 | sandbox_factory_.run_as().uid, getgid())) { |
François Degros | 8b4e31e | 2019-07-29 11:39:19 +1000 | [diff] [blame] | 164 | LOG(ERROR) << "Cannot set proper ownership of working directory " |
| 165 | << quote(working_dir); |
Sergei Datsenko | 71ba50a | 2020-11-27 14:33:39 +1100 | [diff] [blame] | 166 | return MOUNT_ERROR_INSUFFICIENT_PERMISSIONS; |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 167 | } |
Sergei Datsenko | 71ba50a | 2020-11-27 14:33:39 +1100 | [diff] [blame] | 168 | |
| 169 | if (!sandbox->BindMount(working_dir.value(), working_dir.value(), false, |
| 170 | false)) { |
| 171 | LOG(ERROR) << "Cannot bind working directory " << quote(working_dir); |
| 172 | return MOUNT_ERROR_INTERNAL; |
| 173 | } |
| 174 | |
| 175 | std::vector<std::string> options = { |
| 176 | "KbdInteractiveAuthentication=no", |
| 177 | "PasswordAuthentication=no", |
| 178 | "BatchMode=yes", |
| 179 | "follow_symlinks", |
| 180 | "cache=no", |
| 181 | }; |
| 182 | |
| 183 | SetParamValue(&options, "uid", base::NumberToString(kChronosUID)); |
| 184 | SetParamValue(&options, "gid", base::NumberToString(kChronosAccessGID)); |
| 185 | SetParamValue(&options, kOptionIdentityFile, identity_file.value()); |
| 186 | SetParamValue(&options, kOptionUserKnownHostsFile, known_hosts_file.value()); |
| 187 | |
| 188 | std::string value; |
| 189 | if (GetParamValue(params, kOptionHostName, &value)) { |
| 190 | SetParamValue(&options, kOptionHostName, value); |
| 191 | } |
| 192 | if (GetParamValue(params, kOptionPort, &value)) { |
| 193 | SetParamValue(&options, kOptionPort, value); |
| 194 | } |
| 195 | |
| 196 | sandbox->AddArgument(uri.path()); |
Sergei Datsenko | 1bb0bdc | 2021-02-17 11:26:43 +1100 | [diff] [blame] | 197 | |
| 198 | std::string option_string; |
| 199 | if (!JoinParamsIntoOptions(options, &option_string)) { |
| 200 | return MOUNT_ERROR_INVALID_MOUNT_OPTIONS; |
| 201 | } |
Sergei Datsenko | 71ba50a | 2020-11-27 14:33:39 +1100 | [diff] [blame] | 202 | sandbox->AddArgument("-o"); |
Sergei Datsenko | 1bb0bdc | 2021-02-17 11:26:43 +1100 | [diff] [blame] | 203 | sandbox->AddArgument(option_string); |
| 204 | |
Sergei Datsenko | 71ba50a | 2020-11-27 14:33:39 +1100 | [diff] [blame] | 205 | return MOUNT_ERROR_NONE; |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | } // namespace cros_disks |