From 304c08efbe0d57f5a6783ffca0c28030f3ff7016 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Fri, 24 Aug 2012 16:02:02 -0700 Subject: [PATCH] mon: check for int parsing errors in osdmon Signed-off-by: Sage Weil --- src/mon/Monitor.cc | 18 ++++++++++++++++++ src/mon/Monitor.h | 2 ++ src/mon/OSDMonitor.cc | 38 ++++++++++++++++++++++++++++++-------- 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/mon/Monitor.cc b/src/mon/Monitor.cc index f08f5b659d256..f73de532ad8dd 100644 --- a/src/mon/Monitor.cc +++ b/src/mon/Monitor.cc @@ -90,6 +90,24 @@ CompatSet get_ceph_mon_feature_compat_set() ceph_mon_feature_incompat); } +long parse_pos_long(const char *s, ostream *pss) +{ + char *e = 0; + long r = strtol(s, &e, 10); + if (e == s || e == 0) { + if (pss) + *pss << "unable to parse positive integer '" << s << "'"; + return -1; + } + if (r < 0 || r == LONG_MAX || r == LONG_MIN) { + if (pss) + *pss << "unable to parse positive integer '" << s << "'"; + return -1; + } + return r; +} + + Monitor::Monitor(CephContext* cct_, string nm, MonitorStore *s, Messenger *m, MonMap *map) : Dispatcher(cct_), name(nm), diff --git a/src/mon/Monitor.h b/src/mon/Monitor.h index a87b726e4c2c0..9e92de6be7f06 100644 --- a/src/mon/Monitor.h +++ b/src/mon/Monitor.h @@ -443,5 +443,7 @@ private: #define CEPH_MON_FEATURE_INCOMPAT_BASE CompatSet::Feature (1, "initial feature set (~v.18)") +long parse_pos_long(const char *s, ostream *pss = NULL); + #endif diff --git a/src/mon/OSDMonitor.cc b/src/mon/OSDMonitor.cc index a94a159336ae2..88590c254a173 100644 --- a/src/mon/OSDMonitor.cc +++ b/src/mon/OSDMonitor.cc @@ -1536,9 +1536,14 @@ bool OSDMonitor::preprocess_command(MMonCommand *m) break; else if (ceph_argparse_witharg(args, i, &val, "-f", "--format", (char*)NULL)) format = val; - else if (!epoch) - epoch = atoi(*i++); - else + else if (!epoch) { + long l = parse_pos_long(*i++, &ss); + if (l < 0) { + r = -EINVAL; + goto out; + } + epoch = l; + } else i++; } @@ -1839,10 +1844,14 @@ bool OSDMonitor::prepare_command(MMonCommand *m) else if (m->cmd.size() >= 6 && m->cmd[1] == "crush" && m->cmd[2] == "set") { do { // osd crush set [ [ ...]] - int id = atoi(m->cmd[3].c_str()); + int id = parse_pos_long(m->cmd[3].c_str(), &ss); + if (id < 0) { + err = -EINVAL; + goto out; + } if (!osdmap.exists(id)) { err = -ENOENT; - ss << "osd." << id << " does not exist. create it before updating the crush map"; + ss << "osd." << m->cmd[3] << " does not exist. create it before updating the crush map"; goto out; } @@ -2004,7 +2013,11 @@ bool OSDMonitor::prepare_command(MMonCommand *m) } while (false); } else if (m->cmd[1] == "setmaxosd" && m->cmd.size() > 2) { - int newmax = atoi(m->cmd[2].c_str()); + int newmax = parse_pos_long(m->cmd[2].c_str(), &ss); + if (newmax < 0) { + err = -EINVAL; + goto out; + } if (newmax < osdmap.crush->get_max_devices()) { err = -ERANGE; ss << "cannot set max_osd to " << newmax << " which is < crush max_devices " @@ -2536,7 +2549,11 @@ bool OSDMonitor::prepare_command(MMonCommand *m) (m->cmd[1] == "reweight-by-utilization")) { int oload = 120; if (m->cmd.size() > 2) { - oload = atoi(m->cmd[2].c_str()); + oload = parse_pos_long(m->cmd[2].c_str(), &ss); + if (oload < 0) { + err = -EINVAL; + goto out; + } } string out_str; err = reweight_by_utilization(oload, out_str); @@ -2553,7 +2570,12 @@ bool OSDMonitor::prepare_command(MMonCommand *m) } } else if (m->cmd.size() == 3 && m->cmd[1] == "thrash") { - thrash_map = atoi(m->cmd[2].c_str()); + long l = parse_pos_long(m->cmd[2].c_str(), &ss); + if (l < 0) { + err = -EINVAL; + goto out; + } + thrash_map = l; ss << "will thrash map for " << thrash_map << " epochs"; ret = thrash(); err = 0; -- 2.39.5