Skip to content

Commit

Permalink
src: remove multiple uses of atoi
Browse files Browse the repository at this point in the history
atoi() and related functions lack a mechanism for reporting errors for
invalid values. Replace them with calls to the appropriate
ByteExtractString* functions.

Partially closes redmine ticket #3053.
  • Loading branch information
inashivb authored and victorjulien committed Apr 25, 2020
1 parent 92bb52f commit e7c0f0a
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 49 deletions.
12 changes: 7 additions & 5 deletions src/app-layer-htp.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "util-pool.h"
#include "util-radix-tree.h"
#include "util-file.h"
#include "util-byte.h"

#include "stream-tcp-private.h"
#include "stream-tcp-reassemble.h"
Expand Down Expand Up @@ -2794,11 +2795,12 @@ static void HTPConfigParseParameters(HTPCfgRec *cfg_prec, ConfNode *s,
cfg_prec->randomize = ConfValIsTrue(p->val);
}
} else if (strcasecmp("randomize-inspection-range", p->name) == 0) {
uint32_t range = atoi(p->val);
if (range > 100) {
SCLogError(SC_ERR_SIZE_PARSE, "Invalid value for randomize"
" inspection range setting from conf file - %s."
" It should be inferior to 100."
uint32_t range;
if (StringParseU32RangeCheck(&range, 10, 0,
(const char *)p->val, 0, 100) < 0) {
SCLogError(SC_ERR_INVALID_VALUE, "Invalid value for randomize"
"-inspection-range setting from conf file - \"%s\"."
" It should be a valid integer less than or equal to 100."
" Killing engine",
p->val);
exit(EXIT_FAILURE);
Expand Down
8 changes: 7 additions & 1 deletion src/counters.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "util-time.h"
#include "util-unittest.h"
#include "util-debug.h"
#include "util-byte.h"
#include "util-privs.h"
#include "util-signal.h"
#include "unix-manager.h"
Expand Down Expand Up @@ -251,7 +252,12 @@ static void StatsInitCtxPreOutput(void)

const char *interval = ConfNodeLookupChildValue(stats, "interval");
if (interval != NULL)
stats_tts = (uint32_t) atoi(interval);
if (StringParseUint32(&stats_tts, 10, 0, interval) < 0) {
SCLogWarning(SC_ERR_INVALID_VALUE, "Invalid value for "
"interval: \"%s\". Resetting to %d.", interval,
STATS_MGMTT_TTS);
stats_tts = STATS_MGMTT_TTS;
}

int b;
int ret = ConfGetChildValueBool(stats, "decoder-events", &b);
Expand Down
12 changes: 6 additions & 6 deletions src/detect-engine-address.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "detect-engine-port.h"

#include "util-debug.h"
#include "util-byte.h"
#include "util-print.h"
#include "util-var.h"

Expand Down Expand Up @@ -483,10 +484,9 @@ static int DetectAddressParseString(DetectAddress *dd, const char *str)
goto error;
}

int cidr = atoi(mask);
if (cidr < 0 || cidr > 32)
int cidr;
if (StringParseI32RangeCheck(&cidr, 10, 0, (const char *)mask, 0, 32) < 0)
goto error;

netmask = CIDRGet(cidr);
} else {
/* 1.2.3.4/255.255.255.0 format */
Expand Down Expand Up @@ -543,9 +543,9 @@ static int DetectAddressParseString(DetectAddress *dd, const char *str)
ip[mask - ip] = '\0';
mask++;

int cidr = atoi(mask);
if (cidr < 0 || cidr > 128)
goto error;
int cidr;
if (StringParseI32RangeCheck(&cidr, 10, 0, (const char *)mask, 0, 128) < 0)
goto error;

r = inet_pton(AF_INET6, ip, &in6);
if (r <= 0)
Expand Down
10 changes: 7 additions & 3 deletions src/detect-engine-iponly.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "util-unittest.h"
#include "util-unittest-helper.h"
#include "util-print.h"
#include "util-byte.h"
#include "util-profiling.h"
#include "util-validate.h"

Expand Down Expand Up @@ -165,8 +166,8 @@ static int IPOnlyCIDRItemParseSingle(IPOnlyCIDRItem *dd, const char *str)
goto error;
}

int cidr = atoi(mask);
if (cidr < 0 || cidr > 32)
int cidr;
if (StringParseI32RangeCheck(&cidr, 10, 0, (const char *)mask, 0, 32) < 0)
goto error;

dd->netmask = cidr;
Expand Down Expand Up @@ -263,7 +264,10 @@ static int IPOnlyCIDRItemParseSingle(IPOnlyCIDRItem *dd, const char *str)
goto error;

/* Format is cidr val */
dd->netmask = atoi(mask);
if (StringParseU8RangeCheck(&dd->netmask, 10, 0,
(const char *)mask, 0, 128) < 0) {
goto error;
}

memcpy(dd->ip, &in6.s6_addr, sizeof(ip6addr));
} else {
Expand Down
12 changes: 10 additions & 2 deletions src/detect-engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -2307,7 +2307,15 @@ static int DetectEngineCtxLoadConf(DetectEngineCtx *de_ctx)
}

if (insp_recursion_limit != NULL) {
de_ctx->inspection_recursion_limit = atoi(insp_recursion_limit);
if (StringParseInt32(&de_ctx->inspection_recursion_limit, 10,
0, (const char *)insp_recursion_limit) < 0) {
SCLogWarning(SC_ERR_INVALID_VALUE, "Invalid value for "
"detect-engine.inspection-recursion-limit: %s "
"resetting to %d", insp_recursion_limit,
DETECT_ENGINE_DEFAULT_INSPECTION_RECURSION_LIMIT);
de_ctx->inspection_recursion_limit =
DETECT_ENGINE_DEFAULT_INSPECTION_RECURSION_LIMIT;
}
} else {
de_ctx->inspection_recursion_limit =
DETECT_ENGINE_DEFAULT_INSPECTION_RECURSION_LIMIT;
Expand Down Expand Up @@ -4299,7 +4307,7 @@ static int DetectEngineTest02(void)
if (de_ctx == NULL)
goto end;

result = (de_ctx->inspection_recursion_limit == -1);
result = (de_ctx->inspection_recursion_limit == DETECT_ENGINE_DEFAULT_INSPECTION_RECURSION_LIMIT);

end:
if (de_ctx != NULL)
Expand Down
10 changes: 4 additions & 6 deletions src/detect-id.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "flow-var.h"

#include "util-debug.h"
#include "util-byte.h"
#include "util-unittest.h"
#include "util-unittest-helper.h"

Expand Down Expand Up @@ -150,12 +151,9 @@ static DetectIdData *DetectIdParse (const char *idstr)
}

/* ok, fill the id data */
temp = atoi((char *)tmp_str);

if (temp > DETECT_IPID_MAX) {
SCLogError(SC_ERR_INVALID_VALUE, "invalid id option '%s'. The id option "
"value must be in the range %u - %u",
idstr, DETECT_IPID_MIN, DETECT_IPID_MAX);
if (StringParseU32RangeCheck(&temp, 10, 0, (const char *)tmp_str,
DETECT_IPID_MIN, DETECT_IPID_MAX) < 0) {
SCLogError(SC_ERR_INVALID_VALUE, "invalid id option '%s'", tmp_str);
return NULL;
}

Expand Down
10 changes: 5 additions & 5 deletions src/detect-xbits.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "detect-xbits.h"
#include "detect-hostbits.h"
#include "util-spm.h"
#include "util-byte.h"

#include "detect-engine-sigorder.h"

Expand Down Expand Up @@ -196,7 +197,7 @@ static int DetectXbitParse(DetectEngineCtx *de_ctx,
char fb_cmd_str[16] = "", fb_name[256] = "";
char hb_dir_str[16] = "";
enum VarTypes var_type = VAR_TYPE_NOT_SET;
int expire = DETECT_XBITS_EXPIRE_DEFAULT;
uint32_t expire = DETECT_XBITS_EXPIRE_DEFAULT;

ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0, ov, MAX_SUBSTRINGS);
if (ret != 2 && ret != 3 && ret != 4 && ret != 5) {
Expand Down Expand Up @@ -247,10 +248,9 @@ static int DetectXbitParse(DetectEngineCtx *de_ctx,
return -1;
}
SCLogDebug("expire_str %s", expire_str);
expire = atoi(expire_str);
if (expire < 0) {
SCLogError(SC_ERR_INVALID_VALUE, "expire must be positive. "
"Got %d (\"%s\")", expire, expire_str);
if (StringParseUint32(&expire, 10, 0, (const char *)expire_str) < 0) {
SCLogError(SC_ERR_INVALID_VALUE, "Invalid value for "
"expire: \"%s\"", expire_str);
return -1;
}
if (expire == 0) {
Expand Down
15 changes: 6 additions & 9 deletions src/reputation.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "suricata-common.h"
#include "util-error.h"
#include "util-debug.h"
#include "util-byte.h"
#include "util-ip.h"
#include "util-radix-tree.h"
#include "util-unittest.h"
Expand Down Expand Up @@ -251,10 +252,9 @@ static int SRepCatSplitLine(char *line, uint8_t *cat, char *shortname, size_t sh

SCLogDebug("%s, %s", ptrs[0], ptrs[1]);

int c = atoi(ptrs[0]);
if (c < 0 || c >= SREP_MAX_CATS) {
int c;
if (StringParseI32RangeCheck(&c, 10, 0, (const char *)ptrs[0], 0, SREP_MAX_CATS - 1) < 0)
return -1;
}

*cat = (uint8_t)c;
strlcpy(shortname, ptrs[1], shortname_len);
Expand Down Expand Up @@ -305,15 +305,12 @@ static int SRepSplitLine(SRepCIDRTree *cidr_ctx, char *line, Address *ip, uint8_
if (strcmp(ptrs[0], "ip") == 0)
return 1;

int c = atoi(ptrs[1]);
if (c < 0 || c >= SREP_MAX_CATS) {
int c, v;
if (StringParseI32RangeCheck(&c, 10, 0, (const char *)ptrs[1], 0, SREP_MAX_CATS - 1) < 0)
return -1;
}

int v = atoi(ptrs[2]);
if (v < 0 || v > SREP_MAX_VAL) {
if (StringParseI32RangeCheck(&v, 10, 0, (const char *)ptrs[2], 0, SREP_MAX_VAL) < 0)
return -1;
}

if (strchr(ptrs[0], '/') != NULL) {
SRepCIDRAddNetblock(cidr_ctx, ptrs[0], c, v);
Expand Down
12 changes: 10 additions & 2 deletions src/runmode-af-packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include "util-runmodes.h"
#include "util-ioctl.h"
#include "util-ebpf.h"
#include "util-byte.h"

#include "source-af-packet.h"

Expand Down Expand Up @@ -194,7 +195,11 @@ static void *ParseAFPConfig(const char *iface)
if (strcmp(threadsstr, "auto") == 0) {
aconf->threads = 0;
} else {
aconf->threads = atoi(threadsstr);
if (StringParseInt32(&aconf->threads, 10, 0, (const char *)threadsstr) < 0) {
SCLogWarning(SC_ERR_INVALID_VALUE, "Invalid number of "
"threads, resetting to default");
aconf->threads = 0;
}
}
}
}
Expand Down Expand Up @@ -289,7 +294,10 @@ static void *ParseAFPConfig(const char *iface)
if (ConfGetChildValueWithDefault(if_root, if_default, "cluster-id", &tmpclusterid) != 1) {
aconf->cluster_id = (uint16_t)(cluster_id_auto++);
} else {
aconf->cluster_id = (uint16_t)atoi(tmpclusterid);
if (StringParseInt32(&aconf->cluster_id, 10, 0, (const char *)tmpclusterid) < 0) {
SCLogWarning(SC_ERR_INVALID_VALUE, "Invalid cluster_id, resetting to 0");
aconf->cluster_id = 0;
}
SCLogDebug("Going to use cluster-id %" PRId32, aconf->cluster_id);
}

Expand Down
9 changes: 7 additions & 2 deletions src/runmode-napatech.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "util-debug.h"
#include "util-time.h"
#include "util-cpu.h"
#include "util-byte.h"
#include "util-affinity.h"
#include "util-runmodes.h"
#include "util-device.h"
Expand Down Expand Up @@ -195,8 +196,12 @@ static void *NapatechConfigParser(const char *device)
return NULL;
}

/* device+5 is a pointer to the beginning of the stream id after the constant nt portion */
conf->stream_id = atoi(device + 2);
/* device+2 is a pointer to the beginning of the stream id after the constant nt portion */
if (StringParseUint16(&conf->stream_id, 10, 0, device + 2) < 0) {
SCLogError(SC_ERR_INVALID_VALUE, "Invalid value for stream_id: %s", device + 2);
SCFree(conf);
return NULL;
}

/* Set the host buffer allowance for this stream
* Right now we just look at the global default - there is no per-stream hba configuration
Expand Down
7 changes: 6 additions & 1 deletion src/runmode-netmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "util-device.h"
#include "util-runmodes.h"
#include "util-ioctl.h"
#include "util-byte.h"

#include "source-netmap.h"

Expand Down Expand Up @@ -148,7 +149,11 @@ static int ParseNetmapSettings(NetmapIfaceSettings *ns, const char *iface,
ns->threads = 0;
ns->threads_auto = true;
} else {
ns->threads = atoi(threadsstr);
if (StringParseUint16(&ns->threads, 10, 0, threadsstr) < 0) {
SCLogWarning(SC_ERR_INVALID_VALUE, "Invalid config value for "
"threads: %s, resetting to 0", threadsstr);
ns->threads = 0;
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/runmode-pcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "util-runmodes.h"
#include "util-atomic.h"
#include "util-misc.h"
#include "util-byte.h"

const char *RunModeIdsGetDefaultMode(void)
{
Expand Down Expand Up @@ -144,7 +145,11 @@ static void *ParsePcapConfig(const char *iface)
aconf->threads = 1;
} else {
if (threadsstr != NULL) {
aconf->threads = atoi(threadsstr);
if (StringParseInt32(&aconf->threads, 10, 0, (const char *)threadsstr) < 0) {
SCLogWarning(SC_ERR_INVALID_VALUE, "Invalid value for "
"pcap.threads: %s, resetting to 1", threadsstr);
aconf->threads = 1;
}
}
}
if (aconf->threads == 0) {
Expand Down
Loading

0 comments on commit e7c0f0a

Please sign in to comment.