Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 1 | // Copyright 2019 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/smbfs_helper.h" |
| 6 | |
François Degros | a28315e | 2020-07-13 00:24:48 +1000 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 9 | #include <base/check.h> |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 10 | #include <base/files/file_path.h> |
| 11 | #include <base/logging.h> |
| 12 | #include <base/strings/string_number_conversions.h> |
| 13 | #include <base/strings/string_util.h> |
| 14 | |
| 15 | #include "cros-disks/fuse_mounter.h" |
| 16 | #include "cros-disks/mount_options.h" |
Anand K Mistry | 25a5f85 | 2020-01-14 17:08:39 +1100 | [diff] [blame] | 17 | #include "cros-disks/mount_point.h" |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 18 | #include "cros-disks/platform.h" |
Sergei Datsenko | 44612ac | 2020-11-30 09:35:56 +1100 | [diff] [blame] | 19 | #include "cros-disks/quote.h" |
Sergei Datsenko | 88035aa | 2020-11-15 00:24:01 +1100 | [diff] [blame] | 20 | #include "cros-disks/sandboxed_process.h" |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 21 | #include "cros-disks/uri.h" |
| 22 | |
| 23 | namespace cros_disks { |
| 24 | namespace { |
| 25 | |
| 26 | const char kUserName[] = "fuse-smbfs"; |
| 27 | const char kHelperTool[] = "/usr/sbin/smbfs"; |
| 28 | const char kType[] = "smbfs"; |
Anand K Mistry | 48515af | 2020-01-10 16:59:41 +1100 | [diff] [blame] | 29 | const char kSeccompPolicyFile[] = "/usr/share/policy/smbfs-seccomp.policy"; |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 30 | |
| 31 | const char kMojoIdOptionPrefix[] = "mojo_id="; |
| 32 | const char kDbusSocketPath[] = "/run/dbus"; |
Anand K Mistry | dc599ee | 2020-04-22 16:55:06 +1000 | [diff] [blame] | 33 | const char kDaemonStorePath[] = "/run/daemon-store/smbfs"; |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 34 | |
Sergei Datsenko | 44612ac | 2020-11-30 09:35:56 +1100 | [diff] [blame] | 35 | OwnerUser ResolveSmbFsUser(const Platform* platform) { |
| 36 | OwnerUser user; |
| 37 | PCHECK(platform->GetUserAndGroupId(kUserName, &user.uid, &user.gid)); |
| 38 | return user; |
| 39 | } |
Anand K Mistry | 25a5f85 | 2020-01-14 17:08:39 +1100 | [diff] [blame] | 40 | |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 41 | } // namespace |
| 42 | |
| 43 | SmbfsHelper::SmbfsHelper(const Platform* platform, |
| 44 | brillo::ProcessReaper* process_reaper) |
Sergei Datsenko | 44612ac | 2020-11-30 09:35:56 +1100 | [diff] [blame] | 45 | : FUSEMounterHelper(platform, |
| 46 | process_reaper, |
| 47 | kType, |
| 48 | /* nosymfollow= */ true, |
| 49 | &sandbox_factory_), |
| 50 | sandbox_factory_(platform, |
| 51 | SandboxedExecutable{base::FilePath(kHelperTool), |
| 52 | base::FilePath(kSeccompPolicyFile)}, |
| 53 | ResolveSmbFsUser(platform), |
| 54 | /* has_network_access= */ true) {} |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 55 | |
| 56 | SmbfsHelper::~SmbfsHelper() = default; |
| 57 | |
Sergei Datsenko | 44612ac | 2020-11-30 09:35:56 +1100 | [diff] [blame] | 58 | bool SmbfsHelper::CanMount(const std::string& source, |
| 59 | const std::vector<std::string>& params, |
| 60 | base::FilePath* suggested_name) const { |
| 61 | const Uri uri = Uri::Parse(source); |
| 62 | if (!uri.valid() || uri.scheme() != kType) |
| 63 | return false; |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 64 | |
Sergei Datsenko | 44612ac | 2020-11-30 09:35:56 +1100 | [diff] [blame] | 65 | if (uri.path().empty()) { |
| 66 | *suggested_name = base::FilePath(kType); |
| 67 | } else { |
| 68 | *suggested_name = base::FilePath(uri.path()); |
| 69 | } |
| 70 | return true; |
| 71 | } |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 72 | |
Sergei Datsenko | 44612ac | 2020-11-30 09:35:56 +1100 | [diff] [blame] | 73 | MountErrorType SmbfsHelper::ConfigureSandbox( |
| 74 | const std::string& source, |
| 75 | const base::FilePath& /*target_path*/, |
| 76 | std::vector<std::string> /*params*/, |
| 77 | SandboxedProcess* sandbox) const { |
| 78 | const Uri uri = Uri::Parse(source); |
| 79 | if (!uri.valid() || uri.scheme() != kType || uri.path().empty()) { |
François Degros | c309d93 | 2021-02-04 15:12:30 +1100 | [diff] [blame] | 80 | LOG(ERROR) << "Invalid source " << quote(source); |
Sergei Datsenko | 44612ac | 2020-11-30 09:35:56 +1100 | [diff] [blame] | 81 | return MOUNT_ERROR_INVALID_DEVICE_PATH; |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 82 | } |
| 83 | |
Anand K Mistry | dc599ee | 2020-04-22 16:55:06 +1000 | [diff] [blame] | 84 | // Bind DBus communication socket and daemon-store into the sandbox. |
Sergei Datsenko | 44612ac | 2020-11-30 09:35:56 +1100 | [diff] [blame] | 85 | if (!sandbox->BindMount(kDbusSocketPath, kDbusSocketPath, |
| 86 | /* writable= */ true, /* recursive= */ false)) { |
| 87 | LOG(ERROR) << "Cannot bind " << quote(kDbusSocketPath); |
| 88 | return MOUNT_ERROR_INTERNAL; |
| 89 | } |
| 90 | // Need to use recursive binding because the daemon-store directory in |
| 91 | // their cryptohome is bind mounted inside |kDaemonStorePath|. |
| 92 | // TODO(crbug.com/1054705): Pass the user account hash as a mount option |
| 93 | // and restrict binding to that specific directory. |
| 94 | if (!sandbox->BindMount(kDaemonStorePath, kDaemonStorePath, |
| 95 | /* writable= */ true, /* recursive= */ true)) { |
| 96 | LOG(ERROR) << "Cannot bind " << quote(kDaemonStorePath); |
| 97 | return MOUNT_ERROR_INTERNAL; |
| 98 | } |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 99 | |
Sergei Datsenko | 1bb0bdc | 2021-02-17 11:26:43 +1100 | [diff] [blame] | 100 | std::string options; |
| 101 | if (!JoinParamsIntoOptions( |
| 102 | {"uid=1000", "gid=1001", kMojoIdOptionPrefix + uri.path()}, |
| 103 | &options)) { |
| 104 | return MOUNT_ERROR_INVALID_MOUNT_OPTIONS; |
| 105 | } |
Sergei Datsenko | 44612ac | 2020-11-30 09:35:56 +1100 | [diff] [blame] | 106 | sandbox->AddArgument("-o"); |
Sergei Datsenko | 1bb0bdc | 2021-02-17 11:26:43 +1100 | [diff] [blame] | 107 | sandbox->AddArgument(options); |
Sergei Datsenko | 44612ac | 2020-11-30 09:35:56 +1100 | [diff] [blame] | 108 | |
| 109 | return MOUNT_ERROR_NONE; |
Anand K Mistry | 110478e | 2019-10-29 15:24:11 +1100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | } // namespace cros_disks |