From: "flichtenheld (Code Review)" Date: Fri, 22 Aug 2025 13:45:22 +0000 References: Reply-To: frank@...2641..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net Message-ID: <2716e3fbca4c8de71b2e2f017af907eae8a61d59-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="tZf8cqN2dJ0="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: options: Introduce atoi_constrained and review usages of atoi_warn List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos Cc: openvpn-devel --tZf8cqN2dJ0= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: plaisthos. Hello plaisthos, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/1150?usp=3Demail to review the following change. Change subject: options: Introduce atoi_constrained and review usages of at= oi_warn ...................................................................... options: Introduce atoi_constrained and review usages of atoi_warn This is a more powerful version of atoi_warn that can - check minimum and maximum values - report error seperately from parsed value This can be used to simplify a lot of option parsing. Change-Id: Ibc7526d59c1de17a0f9d8ed88f75c6f070ab11e7 Signed-off-by: Frank Lichtenheld --- M src/openvpn/options.c M src/openvpn/options_util.c M src/openvpn/options_util.h M tests/unit_tests/openvpn/test_misc.c 4 files changed, 143 insertions(+), 107 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/50/1150/1 diff --git a/src/openvpn/options.c b/src/openvpn/options.c index e9584a8..b62e22c 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -6405,16 +6405,12 @@ } else if (streq(p[0], "management-log-cache") && p[1] && !p[2]) { - int cache; - VERIFY_PERMISSION(OPT_P_GENERAL); - cache =3D atoi_warn(p[1], msglevel); - if (cache < 1) + if (!atoi_constrained(p[1], &options->management_log_history_cache, + "management-log-cache", 1, 0, msglevel)) { - msg(msglevel, "--management-log-cache parameter is out of rang= e"); goto err; } - options->management_log_history_cache =3D cache; } #endif /* ifdef ENABLE_MANAGEMENT */ #ifdef ENABLE_PLUGIN @@ -6963,16 +6959,11 @@ } else if (streq(p[0], "status-version") && p[1] && !p[2]) { - int version; - VERIFY_PERMISSION(OPT_P_GENERAL); - version =3D atoi_warn(p[1], msglevel); - if (version < 1 || version > 3) + if (!atoi_constrained(p[1], &options->status_file_version, "status= -version", 1, 3, msglevel)) { - msg(msglevel, "--status-version must be 1 to 3"); goto err; } - options->status_file_version =3D version; } else if (streq(p[0], "remap-usr1") && p[1] && !p[2]) { @@ -7145,16 +7136,11 @@ } else if (streq(p[0], "shaper") && p[1] && !p[2]) { - int shaper; - VERIFY_PERMISSION(OPT_P_SHAPER); - shaper =3D atoi_warn(p[1], msglevel); - if (shaper < SHAPER_MIN || shaper > SHAPER_MAX) + if (!atoi_constrained(p[1], &options->shaper, "shaper", SHAPER_MIN= , SHAPER_MAX, msglevel)) { - msg(msglevel, "Bad shaper value, must be between %d and %d", S= HAPER_MIN, SHAPER_MAX); goto err; } - options->shaper =3D shaper; } else if (streq(p[0], "port") && p[1] && !p[2]) { @@ -7723,7 +7709,12 @@ else if (streq(p[0], "script-security") && p[1] && !p[2]) { VERIFY_PERMISSION(OPT_P_GENERAL); - script_security_set(atoi_warn(p[1], msglevel)); + int security; + if (atoi_constrained(p[1], &security, + "script-security", SSEC_NONE, SSEC_PW_ENV, ms= glevel)) + { + script_security_set(security); + } } else if (streq(p[0], "mssfix") && !p[3]) { @@ -7943,11 +7934,9 @@ int real, virtual; VERIFY_PERMISSION(OPT_P_GENERAL); - real =3D atoi_warn(p[1], msglevel); - virtual =3D atoi_warn(p[2], msglevel); - if (real < 1 || virtual < 1) + if (!atoi_constrained(p[1], &real, "hash-size real", 1, 0, msgleve= l) + || !atoi_constrained(p[2], &virtual, "hash-size virtual", 1, 0= , msglevel)) { - msg(msglevel, "--hash-size sizes must be >=3D 1 (preferably a = power of 2)"); goto err; } options->real_hash_size =3D real; @@ -7958,11 +7947,9 @@ int cf_max, cf_per; VERIFY_PERMISSION(OPT_P_GENERAL); - cf_max =3D atoi_warn(p[1], msglevel); - cf_per =3D atoi_warn(p[2], msglevel); - if (cf_max < 0 || cf_per < 0) + if (!atoi_constrained(p[1], &cf_max, "connect-freq n", 1, 0, msgle= vel) + || !atoi_constrained(p[2], &cf_per, "connect-freq seconds", 1,= 0, msglevel)) { - msg(msglevel, "--connect-freq parms must be > 0"); goto err; } options->cf_max =3D cf_max; @@ -7970,15 +7957,12 @@ } else if (streq(p[0], "connect-freq-initial") && p[1] && p[2] && !p[3]) { - long cf_max, cf_per; + int cf_max, cf_per; VERIFY_PERMISSION(OPT_P_GENERAL); - char *e1, *e2; - cf_max =3D strtol(p[1], &e1, 10); - cf_per =3D strtol(p[2], &e2, 10); - if (cf_max < 0 || cf_per < 0 || *e1 !=3D '\0' || *e2 !=3D '\0') + if (!atoi_constrained(p[1], &cf_max, "connect-freq-initial n", 1, = 0, msglevel) + || !atoi_constrained(p[2], &cf_per, "connect-freq-initial seco= nds", 1, 0, msglevel)) { - msg(msglevel, "--connect-freq-initial parameters must be integ= ers and >=3D 0"); goto err; } options->cf_initial_max =3D cf_max; @@ -7986,21 +7970,11 @@ } else if (streq(p[0], "max-clients") && p[1] && !p[2]) { - int max_clients; - VERIFY_PERMISSION(OPT_P_GENERAL); - max_clients =3D atoi_warn(p[1], msglevel); - if (max_clients < 0) + if (!atoi_constrained(p[1], &options->max_clients, "max-clients", = 1, MAX_PEER_ID, msglevel)) { - msg(msglevel, "--max-clients must be at least 1"); goto err; } - if (max_clients >=3D MAX_PEER_ID) /* max peer-id value */ - { - msg(msglevel, "--max-clients must be less than %d", MAX_PEER_I= D); - goto err; - } - options->max_clients =3D max_clients; } else if (streq(p[0], "max-routes-per-client") && p[1] && !p[2]) { @@ -8172,27 +8146,13 @@ } else if (streq(p[0], "bcast-buffers") && p[1] && !p[2]) { - int n_bcast_buf; - VERIFY_PERMISSION(OPT_P_GENERAL); - n_bcast_buf =3D atoi_warn(p[1], msglevel); - if (n_bcast_buf < 1) - { - msg(msglevel, "--bcast-buffers parameter must be > 0"); - } - options->n_bcast_buf =3D n_bcast_buf; + atoi_constrained(p[1], &options->n_bcast_buf, "bcast-buffers", 1, = 0, msglevel); } else if (streq(p[0], "tcp-queue-limit") && p[1] && !p[2]) { - int tcp_queue_limit; - VERIFY_PERMISSION(OPT_P_GENERAL); - tcp_queue_limit =3D atoi_warn(p[1], msglevel); - if (tcp_queue_limit < 1) - { - msg(msglevel, "--tcp-queue-limit parameter must be > 0"); - } - options->tcp_queue_limit =3D tcp_queue_limit; + atoi_constrained(p[1], &options->tcp_queue_limit, "tcp-queue-limit= ", 1, 0, msglevel); } #if PORT_SHARE else if (streq(p[0], "port-share") && p[1] && p[2] && !p[4]) @@ -8338,21 +8298,24 @@ int ageing_time, check_interval; VERIFY_PERMISSION(OPT_P_GENERAL); - ageing_time =3D atoi_warn(p[1], msglevel); + if (!atoi_constrained(p[1], &ageing_time, "stale-routes-check age"= , 1, 0, msglevel)) + { + goto err; + } + if (p[2]) { - check_interval =3D atoi_warn(p[2], msglevel); + if (!atoi_constrained(p[1], &check_interval, + "stale-routes-check interval", 1, 0, msg= level)) + { + goto err; + } } else { check_interval =3D ageing_time; } - if (ageing_time < 1 || check_interval < 1) - { - msg(msglevel, "--stale-routes-check aging time and check inter= val must be >=3D 1"); - goto err; - } options->stale_routes_ageing_time =3D ageing_time; options->stale_routes_check_interval =3D check_interval; } @@ -8370,7 +8333,7 @@ else if (streq(p[0], "push-continuation") && p[1] && !p[2]) { VERIFY_PERMISSION(OPT_P_PULL_MODE); - options->push_continuation =3D atoi_warn(p[1], msglevel); + atoi_constrained(p[1], &options->push_continuation, "push-continua= tion", 0, 2, msglevel); } else if (streq(p[0], "auth-user-pass") && !p[2]) { @@ -8489,33 +8452,22 @@ { if (!streq(p[2], "default")) { - int offset =3D atoi_warn(p[2], msglevel); + int offset; - if (!(offset > -256 && offset < 256)) + if (!atoi_constrained(p[1], &offset, "ip-win32 offset"= , -256, 256, msglevel)) { - msg(msglevel, - "--ip-win32 dynamic [offset] [lease-time]: off= set (%d) must be > -256 and < 256", - offset); goto err; } - to->dhcp_masq_custom_offset =3D true; to->dhcp_masq_offset =3D offset; } if (p[3]) { - const int min_lease =3D 30; - int lease_time; - lease_time =3D atoi_warn(p[3], msglevel); - if (lease_time < min_lease) + if (!atoi_constrained(p[1], &to->dhcp_lease_time, "ip-= win32 lease time", 30, 0, msglevel)) { - msg(msglevel, - "--ip-win32 dynamic [offset] [lease-time]: lea= se time parameter (%d) must be at least %d seconds", - lease_time, min_lease); goto err; } - to->dhcp_lease_time =3D lease_time; } } } @@ -8613,8 +8565,7 @@ } else if (streq(p[1], "NBT") && p[2] && !p[3]) { - int t; - t =3D atoi_warn(p[2], msglevel); + int t =3D atoi_warn(p[2], msglevel); if (!(t =3D=3D 1 || t =3D=3D 2 || t =3D=3D 4 || t =3D=3D 8)) { msg(msglevel, "--dhcp-option NBT: parameter (%d) must be 1= , 2, 4, or 8", t); @@ -8688,15 +8639,12 @@ } else if (streq(p[0], "tap-sleep") && p[1] && !p[2]) { - int s; VERIFY_PERMISSION(OPT_P_DHCPDNS); - s =3D atoi_warn(p[1], msglevel); - if (s < 0 || s >=3D 256) + if (!atoi_constrained(p[1], &options->tuntap_options.tap_sleep, + "tap-sleep", 0, 255, msglevel)) { - msg(msglevel, "--tap-sleep parameter must be between 0 and 255= "); goto err; } - options->tuntap_options.tap_sleep =3D s; } else if (streq(p[0], "dhcp-renew") && !p[1]) { @@ -9136,30 +9084,19 @@ VERIFY_PERMISSION(OPT_P_GENERAL); if (p[1]) { - int replay_window; - - replay_window =3D atoi_warn(p[1], msglevel); - if (!(MIN_SEQ_BACKTRACK <=3D replay_window && replay_window <= =3D MAX_SEQ_BACKTRACK)) + if (!atoi_constrained(p[1], &options->replay_window, "replay-w= indow windows size", + MIN_SEQ_BACKTRACK, MAX_SEQ_BACKTRACK, ms= glevel)) { - msg(msglevel, "replay-window window size parameter (%d) mu= st be between %d and %d", - replay_window, MIN_SEQ_BACKTRACK, MAX_SEQ_BACKTRACK); goto err; } - options->replay_window =3D replay_window; if (p[2]) { - int replay_time; - - replay_time =3D atoi_warn(p[2], msglevel); - if (!(MIN_TIME_BACKTRACK <=3D replay_time && replay_time <= =3D MAX_TIME_BACKTRACK)) + if (!atoi_constrained(p[2], &options->replay_time, "replay= -window time window", + MIN_TIME_BACKTRACK, MAX_TIME_BACKTRA= CK, msglevel)) { - msg(msglevel, - "replay-window time window parameter (%d) must be = between %d and %d", - replay_time, MIN_TIME_BACKTRACK, MAX_TIME_BACKTRAC= K); goto err; } - options->replay_time =3D replay_time; } } else @@ -9755,7 +9692,7 @@ else if (!p[2]) { char *endp =3D NULL; - int i =3D strtol(provider, &endp, 10); + long i =3D strtol(provider, &endp, 10); if (*endp =3D=3D 0) { @@ -9826,7 +9763,7 @@ else if (streq(p[0], "pkcs11-pin-cache") && p[1] && !p[2]) { VERIFY_PERMISSION(OPT_P_GENERAL); - options->pkcs11_pin_cache_period =3D atoi_warn(p[1], msglevel); + options->pkcs11_pin_cache_period =3D positive_atoi(p[1], msglevel); } else if (streq(p[0], "pkcs11-id") && p[1] && !p[2]) { diff --git a/src/openvpn/options_util.c b/src/openvpn/options_util.c index c3938a7..c1f7c74 100644 --- a/src/openvpn/options_util.c +++ b/src/openvpn/options_util.c @@ -146,6 +146,41 @@ return (int)i; } +bool +atoi_constrained(const char *str, int *value, const char *name, int min, i= nt max, int msglevel) +{ + if (!max) + { + max =3D INT_MAX; + } + ASSERT(min < max); + + char *endptr; + long long i =3D strtoll(str, &endptr, 10); + if (i < INT_MIN || *endptr !=3D '\0' || i > INT_MAX) + { + msg(msglevel, "%s: Cannot parse '%s' as integer", name, str); + return false; + } + if (i < min || i > max) + { + if (max =3D=3D INT_MAX) /* nicer message for common case */ + { + msg(msglevel, "%s: Must be an integer >=3D %d, not %lld", + name, min, i); + } + else + { + msg(msglevel, "%s: Must be an integer between %d and %d, not %= lld", + name, min, max, i); + } + return false; + } + + *value =3D i; + return true; +} + static const char *updatable_options[] =3D { "block-ipv6", "block-outside-= dns", "dhcp-option", "dns", "ifconfig", "ifconfig-ipv6", diff --git a/src/openvpn/options_util.h b/src/openvpn/options_util.h index 6f81b1e..d5ae8b9 100644 --- a/src/openvpn/options_util.h +++ b/src/openvpn/options_util.h @@ -41,11 +41,24 @@ /** * Converts a str to an integer if the string can be represented as an - * integer number. Otherwise print a warning with msglevel and return 0 + * integer number. Otherwise print a warning with \p msglevel and return 0 */ int atoi_warn(const char *str, int msglevel); /** + * Converts a str to an integer if the string can be represented as an + * integer number and is between \p min and \p max. + * If \p max is 0, \p INT_MAX is used instead. + * The integer is stored in \p value. + * On error, print a warning with \p msglevel using \p name. \p value is + * not changed on error. + * + * @return \c true if the integer has been parsed and stored in value, \c = false otherwise + */ +bool atoi_constrained(const char *str, int *value, const char *name, int m= in, int max, + int msglevel); + +/** * Filter an option line by all pull filters. * * If a match is found, the line is modified depending on diff --git a/tests/unit_tests/openvpn/test_misc.c b/tests/unit_tests/openvp= n/test_misc.c index 1857922..80a5dee 100644 --- a/tests/unit_tests/openvpn/test_misc.c +++ b/tests/unit_tests/openvpn/test_misc.c @@ -351,6 +351,14 @@ assert_int_equal(atoi_warn("0", msglevel), 0); assert_int_equal(atoi_warn("-1194", msglevel), -1194); + int parameter =3D 0; + assert_true(atoi_constrained("1234", ¶meter, "test", 0, 0, msgleve= l)); + assert_int_equal(parameter, 1234); + assert_true(atoi_constrained("0", ¶meter, "test", -1, 0, msglevel)= ); + assert_int_equal(parameter, 0); + assert_true(atoi_constrained("-1194", ¶meter, "test", INT_MIN, INT= _MAX, msglevel)); + assert_int_equal(parameter, -1194); + CLEAR(mock_msg_buf); assert_int_equal(positive_atoi("-1234", msglevel), 0); assert_string_equal(mock_msg_buf, "Cannot parse argument '-1234' as no= n-negative integer"); @@ -365,6 +373,12 @@ assert_string_equal(mock_msg_buf, "Cannot parse argument '2147483653' = as integer"); CLEAR(mock_msg_buf); + parameter =3D -42; + assert_false(atoi_constrained("2147483653", ¶meter, "test", 0, 0, = msglevel)); + assert_string_equal(mock_msg_buf, "test: Cannot parse '2147483653' as = integer"); + assert_int_equal(parameter, -42); + + CLEAR(mock_msg_buf); assert_int_equal(positive_atoi("foo77", msglevel), 0); assert_string_equal(mock_msg_buf, "Cannot parse argument 'foo77' as no= n-negative integer"); @@ -373,6 +387,18 @@ assert_string_equal(mock_msg_buf, "Cannot parse argument '77foo' as no= n-negative integer"); CLEAR(mock_msg_buf); + parameter =3D -42; + assert_false(atoi_constrained("foo77", ¶meter, "test", 0, 0, msgle= vel)); + assert_string_equal(mock_msg_buf, "test: Cannot parse 'foo77' as integ= er"); + assert_int_equal(parameter, -42); + + CLEAR(mock_msg_buf); + parameter =3D -42; + assert_false(atoi_constrained("77foo", ¶meter, "test", 0, 0, msgle= vel)); + assert_string_equal(mock_msg_buf, "test: Cannot parse '77foo' as integ= er"); + assert_int_equal(parameter, -42); + + CLEAR(mock_msg_buf); assert_int_equal(atoi_warn("foo77", msglevel), 0); assert_string_equal(mock_msg_buf, "Cannot parse argument 'foo77' as in= teger"); @@ -380,6 +406,31 @@ assert_int_equal(atoi_warn("77foo", msglevel), 0); assert_string_equal(mock_msg_buf, "Cannot parse argument '77foo' as in= teger"); + /* special tests for _constrained */ + CLEAR(mock_msg_buf); + parameter =3D -42; + assert_false(atoi_constrained("77", ¶meter, "test", 0, 76, msgleve= l)); + assert_string_equal(mock_msg_buf, "test: Must be an integer between 0 = and 76, not 77"); + assert_int_equal(parameter, -42); + + CLEAR(mock_msg_buf); + parameter =3D -42; + assert_false(atoi_constrained("-77", ¶meter, "test", -76, 76, msgl= evel)); + assert_string_equal(mock_msg_buf, "test: Must be an integer between -7= 6 and 76, not -77"); + assert_int_equal(parameter, -42); + + CLEAR(mock_msg_buf); + parameter =3D -42; + assert_false(atoi_constrained("-77", ¶meter, "test", 0, 0, msgleve= l)); + assert_string_equal(mock_msg_buf, "test: Must be an integer >=3D 0, no= t -77"); + assert_int_equal(parameter, -42); + + CLEAR(mock_msg_buf); + parameter =3D -42; + assert_false(atoi_constrained("0", ¶meter, "test", 1, 0, msglevel)= ); + assert_string_equal(mock_msg_buf, "test: Must be an integer >=3D 1, no= t 0"); + assert_int_equal(parameter, -42); + mock_set_debug_level(saved_log_level); } -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1150?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Ibc7526d59c1de17a0f9d8ed88f75c6f070ab11e7 Gerrit-Change-Number: 1150 Gerrit-PatchSet: 1 Gerrit-Owner: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-MessageType: newchange --tZf8cqN2dJ0= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: plaisthos.

flichtenheld would like plaisthos to review this change.

V= iew Change

options: Introduce atoi_constrained and review usages of ato=
i_warn

This is a more powerful version of atoi_warn that can
- ch= eck minimum and maximum values
- report error seperately from parsed val= ue

This can be used to simplify a lot of option parsing.

Chan= ge-Id: Ibc7526d59c1de17a0f9d8ed88f75c6f070ab11e7
Signed-off-by: Frank Li= chtenheld <frank@...2641...>
---
M src/openvpn/options.c
M s= rc/openvpn/options_util.c
M src/openvpn/options_util.h
M tests/unit_t= ests/openvpn/test_misc.c
4 files changed, 143 insertions(+), 107 deletio= ns(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/change=
s/50/1150/1
diff --git a/src/openvpn/options.c b/src/openvpn/option=
s.c
index e9584a8..b62e22c 100644
--- a/src= /openvpn/options.c
+++ b/src/openvpn/options.c
@@ -6405,16 +6405,12 @@
}
els= e if (streq(p[0], "management-log-cache") && p[1] &&a= mp; !p[2])
{
- int cache;
-
VERIFY_PERMISSION(OPT_P_GENERAL);=
- cache =3D atoi_warn(= p[1], msglevel);
- = if (cache < 1)
= + if (!atoi_constrained(p[1], &options->management_log_histor= y_cache,
+ = "management-log-cache", 1, 0, msglevel))
{
-= msg(msglevel, "--management-log-cache parameter is out of = range");
goto err;
= }
- options-&= gt;management_log_history_cache =3D cache;
} #endif /* ifdef ENABLE_MANAGEMENT */
#ifdef ENABLE= _PLUGIN
@@ -6963,16 +6959,11 @@
}
else if (streq(p[0], "status-version") &&= ; p[1] && !p[2])
{
- int version;
-
VERIFY_PERMISSION(OPT_P_= GENERAL);
- vers= ion =3D atoi_warn(p[1], msglevel);
- if (version < 1 || version > 3)
+ if (!atoi_constrained(p[1], = &options->status_file_version, "status-version", 1, 3, msg= level))
{
- msg(msglevel, "--status-version must be 1 to = 3");
goto err;
}=
- options->s= tatus_file_version =3D version;
}
= else if (streq(p[0], "remap-usr1") && p[1] && !p= [2])
{
@@ -7145,16 +7136,11 @@<= br> }
else if (streq(p[0], "shaper"= ;) && p[1] && !p[2])
{
- int shaper;

-
VERIFY_PERMIS= SION(OPT_P_SHAPER);
- = shaper =3D atoi_warn(p[1], msglevel);
- if (shaper < SHAPER_MIN || shaper > SHA= PER_MAX)
+ if = (!atoi_constrained(p[1], &options->shaper, "shaper", SHAPE= R_MIN, SHAPER_MAX, msglevel))
{
- msg(msglevel, "Bad shap= er value, must be between %d and %d", SHAPER_MIN, SHAPER_MAX);<= br> goto err;
}
- options->shaper =3D shaper;=
}
else if (streq(p[0], "por= t") && p[1] && !p[2])
{@@ -7723,7 +7709,12 @@
else if (streq(p[0], &qu= ot;script-security") && p[1] && !p[2])
{

VERIFY_PERMISSION(OPT_P_GENERAL);- script_security_set(at= oi_warn(p[1], msglevel));
+ int security;
+ if (atoi_constrained(p[1], &security,
+ "sc= ript-security", SSEC_NONE, SSEC_PW_ENV, msglevel))
+ {
+ script_security_set(security);=
+ }
= }
else if (streq(p[0], "mssfix") &&= amp; !p[3])
{
@@ -7943,11 +7934,9 @@
int real, virtual;

VERIFY_PERMISSION(OPT_P_GENERAL);

- real =3D atoi_warn(p[1], msglevel);- virtual =3D atoi_warn(= p[2], msglevel);
- = if (real < 1 || virtual < 1)
+ if (!atoi_constrained(p[1], &real, "hash= -size real", 1, 0, msglevel)
+ || !atoi_constrained(p[2], &virtual, "ha= sh-size virtual", 1, 0, msglevel))
{- msg(msglevel, &quo= t;--hash-size sizes must be >=3D 1 (preferably a power of 2)");
goto err;
}
options->real_hash_size =3D real;

@@ -7958,= 11 +7947,9 @@
int cf_max, cf_per;
=
VERIFY_PERMISSION(OPT_P_GENERAL);
- cf_max =3D atoi_warn(p[1], m= sglevel);
- cf_p= er =3D atoi_warn(p[2], msglevel);
- if (cf_max < 0 || cf_per < 0)
+ if (!atoi_constrained(p[1], &am= p;cf_max, "connect-freq n", 1, 0, msglevel)
+ || !atoi_constrained(p[2], &a= mp;cf_per, "connect-freq seconds", 1, 0, msglevel))
{
- = msg(msglevel, "--connect-freq parms must be > 0");<= br> goto err;
}
= options->cf_max =3D cf_max;
@@ -7970,15 +7957,1= 2 @@
}
else if (streq(p[0], "= ;connect-freq-initial") && p[1] && p[2] && !p[= 3])
{
- long cf_max, cf_per;
+ int cf_max, cf_per;

VERIFY_PERMISSION(OPT_P_GENERAL);
- char *e1, *e2;
- cf_max =3D strtol(p[1], &e1, 10);=
- cf_per =3D strtol(p[= 2], &e2, 10);
- = if (cf_max < 0 || cf_per < 0 || *e1 !=3D '\0' || *e2 !=3D= '\0')
+ = if (!atoi_constrained(p[1], &cf_max, "connect-freq-initial n&qu= ot;, 1, 0, msglevel)
= + || !atoi_constrained(p[2], &cf_per, "connect-freq-ini= tial seconds", 1, 0, msglevel))
{
<= span style=3D"color: hsl(0, 100%, 40%);">- msg(msglevel, "-= -connect-freq-initial parameters must be integers and >=3D 0");

goto err;
}
options->cf_initial_max =3D cf_max;
@@ -798= 6,21 +7970,11 @@
}
else if (streq= (p[0], "max-clients") && p[1] && !p[2]) {
- = int max_clients;
-

VERIFY_PERMISSION(OPT_P_GENERAL);
- max_clients =3D atoi_warn(p[1], ms= glevel);
- if (m= ax_clients < 0)
+ = if (!atoi_constrained(p[1], &options->max_clients, "max-= clients", 1, MAX_PEER_ID, msglevel))
{=
- msg(msglevel, &q= uot;--max-clients must be at least 1");
g= oto err;
}
- if (max_clients >=3D MAX_PEER_ID) /* max peer-id v= alue */
- {
- msg(msglevel, = "--max-clients must be less than %d", MAX_PEER_ID);
- goto err;

- }
- options->max_clients =3D max_clients= ;
}
else if (streq(p[0], "ma= x-routes-per-client") && p[1] && !p[2])
{
@@ -8172,27 +8146,13 @@
}

else if (streq(p[0], "bcast-buffers") &&= ; p[1] && !p[2])
{
- int n_bcast_buf;
-
VERIFY_PERMISSION(OP= T_P_GENERAL);
- = n_bcast_buf =3D atoi_warn(p[1], msglevel);
- if (n_bcast_buf < 1)
- {
- msg(msglevel, "--bcast-buffers paramet= er must be > 0");
- }
- = options->n_bcast_buf =3D n_bcast_buf;
+ atoi_constrained(p[1], &options->n_bcas= t_buf, "bcast-buffers", 1, 0, msglevel);
}
else if (streq(p[0], "tcp-queue-limit") &= & p[1] && !p[2])
{
- int tcp_queue_limit;
-

VERIFY_PER= MISSION(OPT_P_GENERAL);
- tcp_queue_limit =3D atoi_warn(p[1], msglevel);
- if (tcp_queue_limit < 1)
- {
- msg(msglevel, "--tcp-= queue-limit parameter must be > 0");
- }
- options->tcp_queue_limit =3D tcp_queue_limit;<= br>+ atoi_constrained(p[= 1], &options->tcp_queue_limit, "tcp-queue-limit", 1, 0, ms= glevel);
}
#if PORT_SHARE
<= span> else if (streq(p[0], "port-share") && p[1] &= ;& p[2] && !p[4])

@@ -8338,21 +8298,24 @@
int ageing_time, check_interval;

VERIFY_PERMISSION(OPT_P_GENERAL);
- ageing_time =3D atoi_warn(p[1], ms= glevel);
+ if = (!atoi_constrained(p[1], &ageing_time, "stale-routes-check age&quo= t;, 1, 0, msglevel))
= + {
+ = goto err;
+ = }
+
= if (p[2])
{
- check_interval =3D atoi_warn(p[2], msgl= evel);
+ i= f (!atoi_constrained(p[1], &check_interval,
+ "stale-r= outes-check interval", 1, 0, msglevel))
+ {
+ goto err;
+ }
} else
{
= check_interval =3D ageing_time;
}

- if (ageing= _time < 1 || check_interval < 1)
- {
- msg(msglevel, "--stale-routes-check aging time and ch= eck interval must be >=3D 1");
- goto err;
- }
options->stale_route= s_ageing_time =3D ageing_time;
options->stale_r= outes_check_interval =3D check_interval;
}
<= span>@@ -8370,7 +8333,7 @@

else if (streq(p[0], "= push-continuation") && p[1] && !p[2])
= {
VERIFY_PERMISSION(OPT_P_PULL_MODE);<= br>- options->push_cont= inuation =3D atoi_warn(p[1], msglevel);
+ atoi_constrained(p[1], &options->push_co= ntinuation, "push-continuation", 0, 2, msglevel);
}
else if (streq(p[0], "auth-user-pass"= ;) && !p[2])
{
@@ -8489,33 +84= 52,22 @@
{
if= (!streq(p[2], "default"))
{
- int o= ffset =3D atoi_warn(p[2], msglevel);
+ int offset;
- if (!(offs= et > -256 && offset < 256))
+ if (!atoi_constrained(p[1], &= offset, "ip-win32 offset", -256, 256, msglevel))
= {
= - msg(msglevel,
- "--ip-win32 dynamic [offs= et] [lease-time]: offset (%d) must be > -256 and < 256",<= br>- o= ffset);
goto err;
= }
= -
to->dhcp_masq_custom_offset =3D t= rue;
to->dhcp_masq_offset =3D offse= t;
}

= if (p[3])
{
- const int min_leas= e =3D 30;
- = int lease_time;
- lease_time =3D atoi_warn(p[3], msglevel);
<= span style=3D"color: hsl(0, 100%, 40%);">- if (lease_tim= e < min_lease)

+ = if (!atoi_constrained(p[1], &to->dhcp_lease_time, = "ip-win32 lease time", 30, 0, msglevel))
= {
- = msg(msglevel,
- "--ip-win32 dynamic [offset] [lea= se-time]: lease time parameter (%d) must be at least %d seconds",
- = lease_time, min_lease);
goto err= ;
}
- to->dhcp_lease_time =3D lease_tim= e;
}
}=
}
@@ -8613,8 +8565,7 @@
= }
else if (streq(p[1], "NBT") &= amp;& p[2] && !p[3])
{
- int t;

- t =3D atoi_warn(p[2], msglev= el);
+ int= t =3D atoi_warn(p[2], msglevel);
if (!(t =3D= =3D 1 || t =3D=3D 2 || t =3D=3D 4 || t =3D=3D 8))
= {
msg(msglevel, "--dhcp-option NB= T: parameter (%d) must be 1, 2, 4, or 8", t);
@@ -8688= ,15 +8639,12 @@
}
else if (streq(= p[0], "tap-sleep") && p[1] && !p[2])
{
- int= s;
VERIFY_PERMISSION(OPT_P_DHCPDNS);
- s =3D atoi_warn(p[1], msgl= evel);
- if (s &= lt; 0 || s >=3D 256)
+ if (!atoi_constrained(p[1], &options->tuntap_options.tap= _sleep,
+ = "tap-sleep", 0, 255, msglevel))
{
- = msg(msglevel, "--tap-sleep parameter must be between 0 and 255"= );
goto err;
}=
- options->tuntap_o= ptions.tap_sleep =3D s;
}
else if= (streq(p[0], "dhcp-renew") && !p[1])
= {
@@ -9136,30 +9084,19 @@
VERIFY= _PERMISSION(OPT_P_GENERAL);
if (p[1])
{
- = int replay_window;
-
- replay_= window =3D atoi_warn(p[1], msglevel);
- if (!(MIN_SEQ_BACKTRACK <=3D replay_window &= amp;& replay_window <=3D MAX_SEQ_BACKTRACK))
+ if (!atoi_constrained(p[1], &= amp;options->replay_window, "replay-window windows size",
+ = MIN_SEQ_BACKTRACK, MAX_SEQ_BACKTRACK, msglevel))
= {
- = msg(msglevel, "replay-window window size parameter (%d) must = be between %d and %d",
- replay_window, MIN_SEQ_BACKTRACK, MAX_SEQ_BACKTRA= CK);
goto err;
= }
- option= s->replay_window =3D replay_window;

= if (p[2])
{
- int replay_time;
= -
- replay_time =3D atoi_warn(p[2], msgle= vel);
- = if (!(MIN_TIME_BACKTRACK <=3D replay_time && replay_time <=3D= MAX_TIME_BACKTRACK))
+ if (!atoi_constrained(p[2], &options->replay_time,= "replay-window time window",
+ MIN_TIME_BACKTRAC= K, MAX_TIME_BACKTRACK, msglevel))
{=
- msg(msgl= evel,
- = "replay-window time window parameter (%d) must be between %d a= nd %d",
- = replay_time, MIN_TIME_BACKTRACK, MAX_TIME_BACKTRACK);=
goto err;
}=
- optio= ns->replay_time =3D replay_time;
} }
else
@@ -9755,7= +9692,7 @@
else if (!p[2])
= {
char *endp =3D NULL;
- int i =3D strtol(provider, &am= p;endp, 10);
+ = long i =3D strtol(provider, &endp, 10);
if (*endp =3D=3D 0)
{
@@ -9826,7 +9763,7 @@
else if (streq(p[0]= , "pkcs11-pin-cache") && p[1] && !p[2]) {
VERIFY_PERMISSION(OPT_P_GENERAL);

- options->pkcs= 11_pin_cache_period =3D atoi_warn(p[1], msglevel);
+ options->pkcs11_pin_cache_period = =3D positive_atoi(p[1], msglevel);
}
= else if (streq(p[0], "pkcs11-id") && p[1] && = !p[2])
{
diff --git a/src/openvpn/opti= ons_util.c b/src/openvpn/options_util.c
index c3938a7..c1f7= c74 100644
--- a/src/openvpn/options_util.c
+++ b/src/openvpn/options_util.c
@@ -146,6 +146,41 @@
return (int)i;
}
=
+bool
+atoi_constrained(const char *str, int *va= lue, const char *name, int min, int max, int msglevel)
+{
+ if (!max)
+ {
+ = max =3D INT_MAX;
+ }
+ ASSERT(m= in < max);
+

+ char *endptr;<= br>+ long long i =3D strtoll= (str, &endptr, 10);
+ if (i < INT_MIN || *endptr !=3D '\0' || i > INT_MAX)<= /span>
+ {
+ msg(msglevel, "%s: Ca= nnot parse '%s' as integer", name, str);

+ return false;
+ }
+ if (i < min || i > max)
+ {
+ if (max =3D=3D INT_MAX) /* nicer message for = common case */
+ = {
+ msg= (msglevel, "%s: Must be an integer >=3D %d, not %lld",<= br>+ name, min, = i);
+ }=
+ else
+ {
+ msg(msglevel, "%s: Must = be an integer between %d and %d, not %lld",
+ name, min, max, i);
= + }
+ return false;
+ }
+
+ *value =3D i;
+ return true;
+}=
+
s= tatic const char *updatable_options[] =3D { "block-ipv6", "b= lock-outside-dns",
= "dhcp-option", "dns",
= "ifconfig", "ifconfig-ip= v6",
diff --git a/src/openvpn/options_util.h b/src/ope= nvpn/options_util.h
index 6f81b1e..d5ae8b9 100644--- a/src/openvpn/options_util.h
+++ b/src/openvpn/o= ptions_util.h
@@ -41,11 +41,24 @@
<= br> /**
* Converts a str to an integer if the strin= g can be represented as an
- * integer number. Otherwise print a warning with msglevel and return = 0
+ * integer number.= Otherwise print a warning with \p msglevel and return 0
= */
int atoi_warn(const char *str, int msglevel);
/**
+ * Converts a str to an integer if the string can be represented = as an
+ * integer num= ber and is between \p min and \p max.
+ * If \p max is 0, \p INT_MAX is used instead.
= + * The integer is stored in \p= value.
+ * On error,= print a warning with \p msglevel using \p name. \p value is
+ * not changed on error.+ *
+ * @return \c true if the integer has been par= sed and stored in value, \c false otherwise
+ */
+bool atoi_constrained(const char *str, int *value, const char *name= , int min, int max,
+= int msglevel);
+
+/*= *
* Filter an option line by all pull filters.
= *
* If a match is found, the line is modified dep= ending on
diff --git a/tests/unit_tests/openvpn/test_misc.c= b/tests/unit_tests/openvpn/test_misc.c
index 1857922..80a5= dee 100644
--- a/tests/unit_tests/openvpn/test_misc.c

+++ b/tests/unit_tests/openvpn/test_misc.c
@@ -3= 51,6 +351,14 @@
assert_int_equal(atoi_warn("0&quo= t;, msglevel), 0);
assert_int_equal(atoi_warn("-1= 194", msglevel), -1194);

+ int parameter =3D 0;
+ assert_true(atoi_constrained("12= 34", &parameter, "test", 0, 0, msglevel));
+ assert_int_equal(parameter, 1= 234);
+ assert_tru= e(atoi_constrained("0", &parameter, "test", -1, 0, = msglevel));
+ asse= rt_int_equal(parameter, 0);
+ assert_true(atoi_constrained("-1194", &parameter,= "test", INT_MIN, INT_MAX, msglevel));
+ assert_int_equal(parameter, -1194);=
+
CLEA= R(mock_msg_buf);
assert_int_equal(positive_atoi("= -1234", msglevel), 0);
assert_string_equal(mock_m= sg_buf, "Cannot parse argument '-1234' as non-negative integer= ");
@@ -365,6 +373,12 @@
assert_s= tring_equal(mock_msg_buf, "Cannot parse argument '2147483653' = as integer");

CLEAR(mock_msg_bu= f);
+ parameter = =3D -42;
+ assert_= false(atoi_constrained("2147483653", &parameter, "test&q= uot;, 0, 0, msglevel));
+ assert_string_equal(mock_msg_buf, "test: Cannot parse '214= 7483653' as integer");
+ assert_int_equal(parameter, -42);
+
+ CLEAR(mock_msg_buf);
assert_int_equal(p= ositive_atoi("foo77", msglevel), 0);
assert_= string_equal(mock_msg_buf, "Cannot parse argument 'foo77' as n= on-negative integer");

@@ -373,6 +38= 7,18 @@
assert_string_equal(mock_msg_buf, "Cannot= parse argument '77foo' as non-negative integer");
<= span>
CLEAR(mock_msg_buf);
+ parameter =3D -42;
+ assert_false(atoi_constrained("f= oo77", &parameter, "test", 0, 0, msglevel));
<= span style=3D"color: hsl(120, 100%, 40%);">+ assert_string_equal(mock_ms= g_buf, "test: Cannot parse 'foo77' as integer");+ assert_int_equal(paramet= er, -42);
++ CLEAR(mock_msg_buf);
+ parameter =3D -42;
+ assert_false(atoi= _constrained("77foo", &parameter, "test", 0, 0, msg= level));
+ assert_= string_equal(mock_msg_buf, "test: Cannot parse '77foo' as inte= ger");
+ asse= rt_int_equal(parameter, -42);
+
+ CLEAR= (mock_msg_buf);
assert_int_equal(atoi_warn("foo77= ", msglevel), 0);
assert_string_equal(mock_msg_bu= f, "Cannot parse argument 'foo77' as integer");
@@ -380,6 +406,31 @@
assert_i= nt_equal(atoi_warn("77foo", msglevel), 0);
a= ssert_string_equal(mock_msg_buf, "Cannot parse argument '77foo'= ; as integer");

+ /* special tests for _constrained */
+ CLEAR(mock_msg_buf);

= + parameter =3D -42;<= br>+ assert_false(atoi_const= rained("77", &parameter, "test", 0, 76, msglevel));=
+ assert_string_e= qual(mock_msg_buf, "test: Must be an integer between 0 and 76, not 77&= quot;);
+ assert_i= nt_equal(parameter, -42);
+
+ CLEAR(moc= k_msg_buf);
+ para= meter =3D -42;
+ a= ssert_false(atoi_constrained("-77", &parameter, "test&qu= ot;, -76, 76, msglevel));
+ assert_string_equal(mock_msg_buf, "test: Must be an integer = between -76 and 76, not -77");
+ assert_int_equal(parameter, -42);
+
+ CLEAR(mock_msg_buf);
+ parameter =3D -42;
+ assert_false(atoi_constrained("-77", &= amp;parameter, "test", 0, 0, msglevel));
+ assert_string_equal(mock_msg_buf, "= test: Must be an integer >=3D 0, not -77");
+ assert_int_equal(parameter, -42);

+
+ CLEAR(mock_msg_buf);
+ parameter =3D -42;
+ assert_false(atoi_constrained= ("0", &parameter, "test", 1, 0, msglevel));<= br>+ assert_string_equal(moc= k_msg_buf, "test: Must be an integer >=3D 1, not 0");+ assert_int_equal(paramet= er, -42);
+ mock_set_debug_level(saved_log_level);
}



To view, visit change 1150. To= unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ibc7526d59c1de17a0f9d8ed88f7= 5c6f070ab11e7
Gerrit-Change-Number: 1150
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-MessageType: newchange
--tZf8cqN2dJ0=--