Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix symlink resolution when mounting #3673

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/multipass/vm_mount.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class VMMount
explicit VMMount(const QJsonObject& json);
VMMount(const std::string& sourcePath, id_mappings gidMappings, id_mappings uidMappings, MountType mountType);

void resolve_source_path();

QJsonObject serialize() const;

const std::string& get_source_path() const noexcept;
Expand Down
8 changes: 5 additions & 3 deletions src/daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3270,11 +3270,13 @@ bool mp::Daemon::create_missing_mounts(std::unordered_map<std::string, VMMount>&
return mount_specs.size() != initial_mount_count;
}

mp::MountHandler::UPtr mp::Daemon::make_mount(VirtualMachine* vm, const std::string& target, const VMMount& mount)
mp::MountHandler::UPtr mp::Daemon::make_mount(VirtualMachine* vm, const std::string& target, VMMount mount)
{
mount.resolve_source_path();

return mount.get_mount_type() == VMMount::MountType::Classic
? std::make_unique<SSHFSMountHandler>(vm, config->ssh_key_provider.get(), target, mount)
: vm->make_native_mount_handler(target, mount);
? std::make_unique<SSHFSMountHandler>(vm, config->ssh_key_provider.get(), target, std::move(mount))
: vm->make_native_mount_handler(target, std::move(mount));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one doesn't help much unless you change make_native_mount_handler to take by value and move too. Otherwise you're asking for one extra temporary.

}

QFutureWatcher<mp::Daemon::AsyncOperationStatus>*
Expand Down
2 changes: 1 addition & 1 deletion src/daemon/daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public slots:
std::unordered_map<std::string, MountHandler::UPtr>& vm_mounts,
VirtualMachine* vm);

MountHandler::UPtr make_mount(VirtualMachine* vm, const std::string& target, const VMMount& mount);
MountHandler::UPtr make_mount(VirtualMachine* vm, const std::string& target, VMMount mount);

struct AsyncOperationStatus
{
Expand Down
32 changes: 32 additions & 0 deletions src/utils/vm_mount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include <multipass/vm_mount.h>

#include <multipass/file_ops.h>

#include <QJsonArray>

namespace mp = multipass;
Expand Down Expand Up @@ -111,6 +113,36 @@
{
}

void mp::VMMount::resolve_source_path()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call making this a method on the mount. Thinking about it a little further now, I wonder if we should just call this in the constructor and avoid the intermediate state at all. But I haven't looked deep for implications. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about the same thing. This seems to be a private function to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think doing it in the constructor would be good, I'll look into it and any implications it has.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that doing it in the constructor makes it so that the source path is persisted as the place the symlink points to. This changes the behavior a bit so that symlinks are only ever resolved once instead of when the daemon restarts. It's still a fix for the issue but acts a bit different, what do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I wouldn't mind just replacing the original path with the target of the symlink, but doing so completely. IOW, making sure that the source path is discarded and has no further effect. I would like to avoid recording something for a while (either persisted or just in memory) and then something else later.

Also, keep in mind that this will need to deal with "legacy" mounts, i.e., mounts that people established before this PR. You may find that they would already be transformed when the daemon restarts, but it is something to check.

{
std::error_code err;
fs::path path{source_path};

auto status = MP_FILEOPS.symlink_status(path, err);

if (status.type() == fs::file_type::symlink)
{
auto symlink_path = MP_FILEOPS.read_symlink(path, err);

if (err)
throw std::runtime_error(
fmt::format("Mount symlink source path \"{}\" could not be read: {}.", source_path, err.message()));

Check warning on line 129 in src/utils/vm_mount.cpp

View check run for this annotation

Codecov / codecov/patch

src/utils/vm_mount.cpp#L128-L129

Added lines #L128 - L129 were not covered by tests

if (symlink_path.is_relative())
symlink_path = path.parent_path() / symlink_path;

path = fs::weakly_canonical(symlink_path, err);

if (err)
throw std::runtime_error(
fmt::format("Mount symlink source path \"{}\" could not be made weakly canonical: {}.",
source_path,
err.message()));

Check warning on line 140 in src/utils/vm_mount.cpp

View check run for this annotation

Codecov / codecov/patch

src/utils/vm_mount.cpp#L137-L140

Added lines #L137 - L140 were not covered by tests

source_path = path.string();
}
}

QJsonObject mp::VMMount::serialize() const
{
QJsonObject ret;
Expand Down
42 changes: 42 additions & 0 deletions tests/test_daemon_mount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "common.h"
#include "daemon_test_fixture.h"
#include "mock_file_ops.h"
#include "mock_logger.h"
#include "mock_mount_handler.h"
#include "mock_platform.h"
Expand Down Expand Up @@ -267,3 +268,44 @@ TEST_F(TestDaemonMount, performanceMountsNotImplementedHasErrorFails)
EXPECT_EQ(status.error_code(), grpc::StatusCode::FAILED_PRECONDITION);
EXPECT_THAT(status.error_message(), StrEq("The native mounts feature is not implemented on this backend."));
}

TEST_F(TestDaemonMount, symlinkSourceGetsResolved)
{
const auto [temp_dir, filename] = plant_instance_json(fake_json_contents(mac_addr, extra_interfaces));
config_builder.data_directory = temp_dir->path();

const auto [mock_file_ops, _] = mpt::MockFileOps::inject();
EXPECT_CALL(*mock_file_ops, symlink_status).WillOnce(Return(mp::fs::file_status{mp::fs::file_type::symlink}));
EXPECT_CALL(*mock_file_ops, read_symlink)
.WillOnce(Return(mp::fs::path{config_builder.data_directory.toStdString()}));

auto original_implementation_of_mkpath = [](const QDir& dir, const QString& dirName) -> bool {
return MP_FILEOPS.FileOps::mkpath(dir, dirName);
};
EXPECT_CALL(*mock_file_ops, mkpath).WillRepeatedly(original_implementation_of_mkpath);

auto original_implementation_of_open = [](QFileDevice& dev, QIODevice::OpenMode mode) -> bool {
return MP_FILEOPS.FileOps::open(dev, mode);
};
EXPECT_CALL(*mock_file_ops, open(A<QFileDevice&>(), A<QIODevice::OpenMode>()))
.WillRepeatedly(original_implementation_of_open);

auto original_implementation_of_commit = [](QSaveFile& file) -> bool { return MP_FILEOPS.FileOps::commit(file); };
EXPECT_CALL(*mock_file_ops, commit).WillRepeatedly(original_implementation_of_commit);

mp::Daemon daemon{config_builder.build()};

mp::MountRequest request;
request.set_source_path(mount_dir.path().toStdString());
request.set_mount_type(mp::MountRequest::NATIVE);
auto entry = request.add_target_paths();
entry->set_instance_name(mock_instance_name);
entry->set_target_path(fake_target_path);

auto status = call_daemon_slot(daemon,
&mp::Daemon::mount,
request,
StrictMock<mpt::MockServerReaderWriter<mp::MountReply, mp::MountRequest>>{});

EXPECT_TRUE(status.ok());
}
46 changes: 46 additions & 0 deletions tests/test_vm_mount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

#include "common.h"
#include "mock_file_ops.h"

#include <multipass/vm_mount.h>

Expand Down Expand Up @@ -139,4 +140,49 @@ TEST_F(TestVMMount, duplicateGidsThrowsWithDuplicateTargetID)
HasSubstr("1002:1001"),
HasSubstr("1000:1001"))));
}

TEST_F(TestVMMount, notSymlinkSourcePathUnchanged)
{
const auto [mock_file_ops, _] = mpt::MockFileOps::inject();
EXPECT_CALL(*mock_file_ops, symlink_status).WillOnce(Return(mp::fs::file_status{mp::fs::file_type::regular}));
EXPECT_CALL(*mock_file_ops, read_symlink).Times(0);

mp::VMMount mount{"src", {}, {}, mp::VMMount::MountType::Classic};

mount.resolve_source_path();

EXPECT_EQ(mount.get_source_path(), "src");
}

TEST_F(TestVMMount, absoluteSymlinkSourcePathResolved)
{
auto source_path = mp::fs::weakly_canonical("/tmp/src");
auto symlink_path = mp::fs::weakly_canonical("/home/dest");

const auto [mock_file_ops, _] = mpt::MockFileOps::inject();
EXPECT_CALL(*mock_file_ops, symlink_status).WillOnce(Return(mp::fs::file_status{mp::fs::file_type::symlink}));
EXPECT_CALL(*mock_file_ops, read_symlink).WillOnce(Return(symlink_path));

mp::VMMount mount{source_path.string(), {}, {}, mp::VMMount::MountType::Classic};

mount.resolve_source_path();

EXPECT_EQ(mount.get_source_path(), symlink_path.string());
}

TEST_F(TestVMMount, relativeSymlinkSourcePathResolved)
{
auto source_path = mp::fs::weakly_canonical("/tmp/src");

const auto [mock_file_ops, _] = mpt::MockFileOps::inject();
EXPECT_CALL(*mock_file_ops, symlink_status).WillOnce(Return(mp::fs::file_status{mp::fs::file_type::symlink}));
EXPECT_CALL(*mock_file_ops, read_symlink).WillOnce(Return(mp::fs::path{"./dest"}));

mp::VMMount mount{source_path.string(), {}, {}, mp::VMMount::MountType::Classic};

mount.resolve_source_path();

EXPECT_EQ(mount.get_source_path(), source_path.replace_filename("dest").string());
}

} // namespace
Loading