diff options
author | Holger Hans Peter Freyther <zecke@selfish.org> | 2011-04-04 19:19:26 +0200 |
---|---|---|
committer | Holger Hans Peter Freyther <zecke@selfish.org> | 2011-04-04 19:19:26 +0200 |
commit | 4c9557ef24f267ad0912f1bff6b0fdcb52c74e68 (patch) | |
tree | 339fcec4f5244553b740add467333a9300d023b7 /openbsc/src | |
parent | 13673749ffdd8947af5ff69e477f9144e8afc57f (diff) |
nat: Bail out if the regexp fails to compile and avoid a crash
If the regexp fails to compile the internal dfa is NULL and a
regexec will crash nicely. Fail and free the string if the regexp
fails to compile.
Diffstat (limited to 'openbsc/src')
-rw-r--r-- | openbsc/src/osmo-bsc_nat/bsc_nat_utils.c | 15 | ||||
-rw-r--r-- | openbsc/src/osmo-bsc_nat/bsc_nat_vty.c | 12 |
2 files changed, 21 insertions, 6 deletions
diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c b/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c index f83289b61..4258364fe 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c @@ -606,8 +606,11 @@ int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, } } -void bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv) +int bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv) { + int ret; + + ret = 0; if (*imsi) { talloc_free(*imsi); *imsi = NULL; @@ -616,8 +619,16 @@ void bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char ** if (argc > 0) { *imsi = talloc_strdup(ctx, argv[0]); - regcomp(reg, argv[0], 0); + ret = regcomp(reg, argv[0], 0); + + /* handle compilation failures */ + if (ret != 0) { + talloc_free(*imsi); + *imsi = NULL; + } } + + return ret; } static const char *con_types [] = { diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c b/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c index 3158f3472..3a5068e26 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c @@ -467,7 +467,8 @@ DEFUN(cfg_nat_ussd_query, "Set the USSD query to match with the ussd-list-name\n" "The query to match") { - bsc_parse_reg(_nat, &_nat->ussd_query_re, &_nat->ussd_query, argc, argv); + if (bsc_parse_reg(_nat, &_nat->ussd_query_re, &_nat->ussd_query, argc, argv) != 0) + return CMD_WARNING; return CMD_SUCCESS; } @@ -580,7 +581,8 @@ DEFUN(cfg_lst_imsi_allow, if (!entry) return CMD_WARNING; - bsc_parse_reg(acc, &entry->imsi_allow_re, &entry->imsi_allow, argc - 1, &argv[1]); + if (bsc_parse_reg(acc, &entry->imsi_allow_re, &entry->imsi_allow, argc - 1, &argv[1]) != 0) + return CMD_WARNING; return CMD_SUCCESS; } @@ -602,7 +604,8 @@ DEFUN(cfg_lst_imsi_deny, if (!entry) return CMD_WARNING; - bsc_parse_reg(acc, &entry->imsi_deny_re, &entry->imsi_deny, argc - 1, &argv[1]); + if (bsc_parse_reg(acc, &entry->imsi_deny_re, &entry->imsi_deny, argc - 1, &argv[1]) != 0) + return CMD_WARNING; return CMD_SUCCESS; } @@ -710,7 +713,8 @@ DEFUN(test_regex, test_regex_cmd, char *str = NULL; memset(®, 0, sizeof(reg)); - bsc_parse_reg(_nat, ®, &str, 1, argv); + if (bsc_parse_reg(_nat, ®, &str, 1, argv) != 0) + return CMD_WARNING; vty_out(vty, "String matches allow pattern: %d%s", regexec(®, argv[1], 0, NULL, 0) == 0, VTY_NEWLINE); |