Skip to content

Commit ba67cda

Browse files
authored
Merge pull request #480 from CanonicalLtd/fix-long-uid-gid-failure
client/mount: Better uid/gid validation to account for larger values
2 parents 2c89d67 + 973369e commit ba67cda

File tree

2 files changed

+85
-4
lines changed

2 files changed

+85
-4
lines changed

src/client/cmd/mount.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ using RpcMethod = mp::Rpc::Stub;
3737
namespace
3838
{
3939
constexpr auto category = "mount cmd";
40+
41+
auto convert_id_for(const QString& id_string)
42+
{
43+
bool ok;
44+
45+
auto id = id_string.toUInt(&ok);
46+
if (!ok)
47+
throw std::runtime_error(id_string.toStdString() + " is an invalid id");
48+
49+
return id;
50+
}
4051
} // namespace
4152

4253
mp::ReturnCode cmd::Mount::run(mp::ArgParser* parser)
@@ -161,7 +172,7 @@ mp::ParseCode cmd::Mount::parse_args(mp::ArgParser* parser)
161172
}
162173
}
163174

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

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

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

180-
(*request.mutable_mount_maps()->mutable_uid_map())[parsed_map.at(0).toInt()] = parsed_map.at(1).toInt();
191+
try
192+
{
193+
auto host_uid = convert_id_for(parsed_map.at(0));
194+
auto instance_uid = convert_id_for(parsed_map.at(1));
195+
196+
(*request.mutable_mount_maps()->mutable_uid_map())[host_uid] = instance_uid;
197+
}
198+
catch (const std::exception& e)
199+
{
200+
cerr << e.what() << "\n";
201+
return ParseCode::CommandLineError;
202+
}
181203
}
182204
}
183205

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

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

198-
(*request.mutable_mount_maps()->mutable_gid_map())[parsed_map.at(0).toInt()] = parsed_map.at(1).toInt();
220+
try
221+
{
222+
auto host_gid = convert_id_for(parsed_map.at(0));
223+
auto instance_gid = convert_id_for(parsed_map.at(1));
224+
225+
(*request.mutable_mount_maps()->mutable_gid_map())[host_gid] = instance_gid;
226+
}
227+
catch (const std::exception& e)
228+
{
229+
cerr << e.what() << "\n";
230+
return ParseCode::CommandLineError;
231+
}
199232
}
200233
}
201234

tests/test_cli_client.cpp

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ TEST_F(Client, mount_cmd_good_absolute_source_path)
331331
EXPECT_THAT(send_command({"mount", mpt::test_data_path().toStdString(), "test-vm:test"}), Eq(mp::ReturnCode::Ok));
332332
}
333333

334-
TEST_F(Client, mount_cmd_good_relative_soure_path)
334+
TEST_F(Client, mount_cmd_good_relative_source_path)
335335
{
336336
EXPECT_THAT(send_command({"mount", "..", "test-vm:test"}), Eq(mp::ReturnCode::Ok));
337337
}
@@ -342,6 +342,54 @@ TEST_F(Client, mount_cmd_fails_invalid_source_path)
342342
Eq(mp::ReturnCode::CommandLineError));
343343
}
344344

345+
TEST_F(Client, mount_cmd_good_valid_uid_map)
346+
{
347+
EXPECT_THAT(send_command({"mount", mpt::test_data_path().toStdString(), "-u", "1000:501", "test-vm:test"}),
348+
Eq(mp::ReturnCode::Ok));
349+
}
350+
351+
TEST_F(Client, mount_cmd_good_valid_large_uid_map)
352+
{
353+
EXPECT_THAT(send_command({"mount", mpt::test_data_path().toStdString(), "-u", "218038053:0", "test-vm:test"}),
354+
Eq(mp::ReturnCode::Ok));
355+
}
356+
357+
TEST_F(Client, mount_cmd_fails_invalid_string_uid_map)
358+
{
359+
EXPECT_THAT(send_command({"mount", mpt::test_data_path().toStdString(), "-u", "foo:bar", "test-vm:test"}),
360+
Eq(mp::ReturnCode::CommandLineError));
361+
}
362+
363+
TEST_F(Client, mount_cmd_fails_invalid_host_int_uid_map)
364+
{
365+
EXPECT_THAT(send_command({"mount", mpt::test_data_path().toStdString(), "-u", "5000000000:0", "test-vm:test"}),
366+
Eq(mp::ReturnCode::CommandLineError));
367+
}
368+
369+
TEST_F(Client, mount_cmd_good_valid_gid_map)
370+
{
371+
EXPECT_THAT(send_command({"mount", mpt::test_data_path().toStdString(), "-g", "1000:501", "test-vm:test"}),
372+
Eq(mp::ReturnCode::Ok));
373+
}
374+
375+
TEST_F(Client, mount_cmd_good_valid_large_gid_map)
376+
{
377+
EXPECT_THAT(send_command({"mount", mpt::test_data_path().toStdString(), "-g", "218038053:0", "test-vm:test"}),
378+
Eq(mp::ReturnCode::Ok));
379+
}
380+
381+
TEST_F(Client, mount_cmd_fails_invalid_string_gid_map)
382+
{
383+
EXPECT_THAT(send_command({"mount", mpt::test_data_path().toStdString(), "-g", "foo:bar", "test-vm:test"}),
384+
Eq(mp::ReturnCode::CommandLineError));
385+
}
386+
387+
TEST_F(Client, mount_cmd_fails_invalid_host_int_gid_map)
388+
{
389+
EXPECT_THAT(send_command({"mount", mpt::test_data_path().toStdString(), "-g", "5000000000:0", "test-vm:test"}),
390+
Eq(mp::ReturnCode::CommandLineError));
391+
}
392+
345393
// recover cli tests
346394
TEST_F(Client, recover_cmd_fails_no_args)
347395
{

0 commit comments

Comments
 (0)