From 4af450451599bebac28808fdbc8df1add5335067 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Fri, 10 Apr 2015 18:29:06 +0200 Subject: [PATCH] Validate the smartctl string before accepting it --- cmdline/state.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/cmdline/state.c b/cmdline/state.c index 66f96426..6b45517b 100644 --- a/cmdline/state.c +++ b/cmdline/state.c @@ -415,6 +415,36 @@ static void state_config_check(struct snapraid_state* state, const char* path, t } } +/** + * Validate the smartctl command. + * + * It must contains only one %s string, and not other % chars. + */ +static int validate_smartctl(const char* custom) +{ + const char* s = custom; + int arg = 0; + + while (*s) { + if (s[0] == '%' && s[1] == 's') { + if (arg) { + /* LCOV_EXCL_START */ + return -1; + /* LCOV_EXCL_STOP */ + } + arg = 1; + } else if (s[0] == '%') { + /* LCOV_EXCL_START */ + return -1; + /* LCOV_EXCL_STOP */ + } + + ++s; + } + + return 0; +} + void state_config(struct snapraid_state* state, const char* path, const char* command, struct snapraid_option* opt, tommy_list* filterlist_disk) { STREAM* f; @@ -792,6 +822,13 @@ void state_config(struct snapraid_state* state, const char* path, const char* co /* LCOV_EXCL_STOP */ } + if (validate_smartctl(custom) != 0) { + /* LCOV_EXCL_START */ + msg_error("Invalid 'smartctl' option specification in '%s' at line %u\n", path, line); + exit(EXIT_FAILURE); + /* LCOV_EXCL_STOP */ + } + /* search for parity */ if (lev_config_scan(buffer, &level, 0) == 0) { if (state->parity[level].smartctl[0] != 0) {