Skip to content

Commit

Permalink
Merge pull request #9700 from JiYou/fix-monitor-crush
Browse files Browse the repository at this point in the history
mon: Monitor: validate prefix on handle_command()

Reviewed-by: Sage Weil <sage@redhat.com>
Reviewed-by: Joao Eduardo Luis <joao@suse.de>
  • Loading branch information
liewegas committed Jun 30, 2016
2 parents 13c13c7 + 7cb3434 commit 957ece7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/mon/Monitor.cc
Expand Up @@ -2631,7 +2631,19 @@ void Monitor::handle_command(MonOpRequestRef op)
return;
}

cmd_getval(g_ceph_context, cmdmap, "prefix", prefix);
// check return value. If no prefix parameter provided,
// return value will be false, then return error info.
if(!cmd_getval(g_ceph_context, cmdmap, "prefix", prefix)) {
reply_command(op, -EINVAL, "command prefix not found", 0);
return;
}

// check prefix is empty
if (prefix.empty()) {
reply_command(op, -EINVAL, "command prefix must not be empty", 0);
return;
}

if (prefix == "get_command_descriptions") {
bufferlist rdata;
Formatter *f = Formatter::create("json");
Expand All @@ -2652,6 +2664,15 @@ void Monitor::handle_command(MonOpRequestRef op)
boost::scoped_ptr<Formatter> f(Formatter::create(format));

get_str_vec(prefix, fullcmd);

// make sure fullcmd is not empty.
// invalid prefix will cause empty vector fullcmd.
// such as, prefix=";,,;"
if (fullcmd.empty()) {
reply_command(op, -EINVAL, "command requires a prefix to be valid", 0);
return;
}

module = fullcmd[0];

// validate command is in leader map
Expand Down
35 changes: 35 additions & 0 deletions src/test/librados/cmd.cc
Expand Up @@ -48,6 +48,41 @@ TEST(LibRadosCmd, MonDescribe) {
rados_buffer_free(buf);
rados_buffer_free(st);

cmd[0] = (char *)"";
ASSERT_EQ(-EINVAL, rados_mon_command(cluster, (const char **)cmd, 1, "{}", 2, &buf, &buflen, &st, &stlen));
rados_buffer_free(buf);
rados_buffer_free(st);

cmd[0] = (char *)"{}";
ASSERT_EQ(-EINVAL, rados_mon_command(cluster, (const char **)cmd, 1, "", 0, &buf, &buflen, &st, &stlen));
rados_buffer_free(buf);
rados_buffer_free(st);

cmd[0] = (char *)"{\"abc\":\"something\"}";
ASSERT_EQ(-EINVAL, rados_mon_command(cluster, (const char **)cmd, 1, "", 0, &buf, &buflen, &st, &stlen));
rados_buffer_free(buf);
rados_buffer_free(st);

cmd[0] = (char *)"{\"prefix\":\"\"}";
ASSERT_EQ(-EINVAL, rados_mon_command(cluster, (const char **)cmd, 1, "", 0, &buf, &buflen, &st, &stlen));
rados_buffer_free(buf);
rados_buffer_free(st);

cmd[0] = (char *)"{\"prefix\":\" \"}";
ASSERT_EQ(-EINVAL, rados_mon_command(cluster, (const char **)cmd, 1, "", 0, &buf, &buflen, &st, &stlen));
rados_buffer_free(buf);
rados_buffer_free(st);

cmd[0] = (char *)"{\"prefix\":\";;;,,,;;,,\"}";
ASSERT_EQ(-EINVAL, rados_mon_command(cluster, (const char **)cmd, 1, "", 0, &buf, &buflen, &st, &stlen));
rados_buffer_free(buf);
rados_buffer_free(st);

cmd[0] = (char *)"{\"prefix\":\"extra command\"}";
ASSERT_EQ(-EINVAL, rados_mon_command(cluster, (const char **)cmd, 1, "", 0, &buf, &buflen, &st, &stlen));
rados_buffer_free(buf);
rados_buffer_free(st);

cmd[0] = (char *)"{\"prefix\":\"mon_status\"}";
ASSERT_EQ(0, rados_mon_command(cluster, (const char **)cmd, 1, "", 0, &buf, &buflen, &st, &stlen));
ASSERT_LT(0u, buflen);
Expand Down

0 comments on commit 957ece7

Please sign in to comment.