Skip to content

Commit

Permalink
Merge pull request #480 from CanonicalLtd/fix-long-uid-gid-failure
Browse files Browse the repository at this point in the history
client/mount: Better uid/gid validation to account for larger values
  • Loading branch information
townsend2010 committed Nov 7, 2018
2 parents 2c89d67 + 973369e commit ba67cda
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
39 changes: 36 additions & 3 deletions src/client/cmd/mount.cpp
Expand Up @@ -37,6 +37,17 @@ using RpcMethod = mp::Rpc::Stub;
namespace
{
constexpr auto category = "mount cmd";

auto convert_id_for(const QString& id_string)
{
bool ok;

auto id = id_string.toUInt(&ok);
if (!ok)
throw std::runtime_error(id_string.toStdString() + " is an invalid id");

return id;
}
} // namespace

mp::ReturnCode cmd::Mount::run(mp::ArgParser* parser)
Expand Down Expand Up @@ -161,7 +172,7 @@ mp::ParseCode cmd::Mount::parse_args(mp::ArgParser* parser)
}
}

QRegExp map_matcher("^([0-9]{1,5}[:][0-9]{1,5})$");
QRegExp map_matcher("^([0-9]+[:][0-9]+)$");

if (parser->isSet(uid_map))
{
Expand All @@ -177,7 +188,18 @@ mp::ParseCode cmd::Mount::parse_args(mp::ArgParser* parser)

auto parsed_map = map.split(":");

(*request.mutable_mount_maps()->mutable_uid_map())[parsed_map.at(0).toInt()] = parsed_map.at(1).toInt();
try
{
auto host_uid = convert_id_for(parsed_map.at(0));
auto instance_uid = convert_id_for(parsed_map.at(1));

(*request.mutable_mount_maps()->mutable_uid_map())[host_uid] = instance_uid;
}
catch (const std::exception& e)
{
cerr << e.what() << "\n";
return ParseCode::CommandLineError;
}
}
}

Expand All @@ -195,7 +217,18 @@ mp::ParseCode cmd::Mount::parse_args(mp::ArgParser* parser)

auto parsed_map = map.split(":");

(*request.mutable_mount_maps()->mutable_gid_map())[parsed_map.at(0).toInt()] = parsed_map.at(1).toInt();
try
{
auto host_gid = convert_id_for(parsed_map.at(0));
auto instance_gid = convert_id_for(parsed_map.at(1));

(*request.mutable_mount_maps()->mutable_gid_map())[host_gid] = instance_gid;
}
catch (const std::exception& e)
{
cerr << e.what() << "\n";
return ParseCode::CommandLineError;
}
}
}

Expand Down
50 changes: 49 additions & 1 deletion tests/test_cli_client.cpp
Expand Up @@ -331,7 +331,7 @@ TEST_F(Client, mount_cmd_good_absolute_source_path)
EXPECT_THAT(send_command({"mount", mpt::test_data_path().toStdString(), "test-vm:test"}), Eq(mp::ReturnCode::Ok));
}

TEST_F(Client, mount_cmd_good_relative_soure_path)
TEST_F(Client, mount_cmd_good_relative_source_path)
{
EXPECT_THAT(send_command({"mount", "..", "test-vm:test"}), Eq(mp::ReturnCode::Ok));
}
Expand All @@ -342,6 +342,54 @@ TEST_F(Client, mount_cmd_fails_invalid_source_path)
Eq(mp::ReturnCode::CommandLineError));
}

TEST_F(Client, mount_cmd_good_valid_uid_map)
{
EXPECT_THAT(send_command({"mount", mpt::test_data_path().toStdString(), "-u", "1000:501", "test-vm:test"}),
Eq(mp::ReturnCode::Ok));
}

TEST_F(Client, mount_cmd_good_valid_large_uid_map)
{
EXPECT_THAT(send_command({"mount", mpt::test_data_path().toStdString(), "-u", "218038053:0", "test-vm:test"}),
Eq(mp::ReturnCode::Ok));
}

TEST_F(Client, mount_cmd_fails_invalid_string_uid_map)
{
EXPECT_THAT(send_command({"mount", mpt::test_data_path().toStdString(), "-u", "foo:bar", "test-vm:test"}),
Eq(mp::ReturnCode::CommandLineError));
}

TEST_F(Client, mount_cmd_fails_invalid_host_int_uid_map)
{
EXPECT_THAT(send_command({"mount", mpt::test_data_path().toStdString(), "-u", "5000000000:0", "test-vm:test"}),
Eq(mp::ReturnCode::CommandLineError));
}

TEST_F(Client, mount_cmd_good_valid_gid_map)
{
EXPECT_THAT(send_command({"mount", mpt::test_data_path().toStdString(), "-g", "1000:501", "test-vm:test"}),
Eq(mp::ReturnCode::Ok));
}

TEST_F(Client, mount_cmd_good_valid_large_gid_map)
{
EXPECT_THAT(send_command({"mount", mpt::test_data_path().toStdString(), "-g", "218038053:0", "test-vm:test"}),
Eq(mp::ReturnCode::Ok));
}

TEST_F(Client, mount_cmd_fails_invalid_string_gid_map)
{
EXPECT_THAT(send_command({"mount", mpt::test_data_path().toStdString(), "-g", "foo:bar", "test-vm:test"}),
Eq(mp::ReturnCode::CommandLineError));
}

TEST_F(Client, mount_cmd_fails_invalid_host_int_gid_map)
{
EXPECT_THAT(send_command({"mount", mpt::test_data_path().toStdString(), "-g", "5000000000:0", "test-vm:test"}),
Eq(mp::ReturnCode::CommandLineError));
}

// recover cli tests
TEST_F(Client, recover_cmd_fails_no_args)
{
Expand Down

0 comments on commit ba67cda

Please sign in to comment.