Skip to content

Commit d7f86dd

Browse files
schwabecron2
authored andcommitted
Ensure return value of snprintf is correctly checked
Commit 130548f change the usages of openvpn_snprintf to snprintf. When doing that conversion I did not notice that, despite the function name, openvpn_snprintf had a different semantic for the return value. This commit goes through all the usages of snprintf and ensures that the return is correctly checked for overruns. Reported-by: Joshua Rogers <[email protected]> Found-by: ZeroPath (https://zeropath.com/) Change-Id: I830b6b27fc3efe707e103ba629c4bfe3796a9cbe Signed-off-by: Arne Schwabe <[email protected]> Acked-by: Gert Doering <[email protected]> Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1330 Message-Id: <[email protected]> URL: https://www.mail-archive.com/[email protected]/msg34063.html Signed-off-by: Gert Doering <[email protected]>
1 parent e7df832 commit d7f86dd

File tree

8 files changed

+21
-16
lines changed

8 files changed

+21
-16
lines changed

src/openvpn/crypto_mbedtls.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ mbed_log_func_line(unsigned int flags, int errval, const char *func, int line)
127127
{
128128
char prefix[256];
129129

130-
if (!snprintf(prefix, sizeof(prefix), "%s:%d", func, line))
130+
if (snprintf(prefix, sizeof(prefix), "%s:%d", func, line) >= sizeof(prefix))
131131
{
132132
return mbed_log_err(flags, errval, func);
133133
}
@@ -243,11 +243,11 @@ crypto_pem_encode(const char *name, struct buffer *dst, const struct buffer *src
243243
char header[1000 + 1] = { 0 };
244244
char footer[1000 + 1] = { 0 };
245245

246-
if (!snprintf(header, sizeof(header), "-----BEGIN %s-----\n", name))
246+
if (snprintf(header, sizeof(header), "-----BEGIN %s-----\n", name) >= sizeof(header))
247247
{
248248
return false;
249249
}
250-
if (!snprintf(footer, sizeof(footer), "-----END %s-----\n", name))
250+
if (snprintf(footer, sizeof(footer), "-----END %s-----\n", name) >= sizeof(footer))
251251
{
252252
return false;
253253
}
@@ -280,11 +280,11 @@ crypto_pem_decode(const char *name, struct buffer *dst, const struct buffer *src
280280
char header[1000 + 1] = { 0 };
281281
char footer[1000 + 1] = { 0 };
282282

283-
if (!snprintf(header, sizeof(header), "-----BEGIN %s-----", name))
283+
if (snprintf(header, sizeof(header), "-----BEGIN %s-----", name) >= sizeof(header))
284284
{
285285
return false;
286286
}
287-
if (!snprintf(footer, sizeof(footer), "-----END %s-----", name))
287+
if (snprintf(footer, sizeof(footer), "-----END %s-----", name) >= sizeof(footer))
288288
{
289289
return false;
290290
}

src/openvpn/dns.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,11 +485,13 @@ setenv_dns_option(struct env_set *es, const char *format, int i, int j, const ch
485485

486486
if (j < 0)
487487
{
488-
name_ok = snprintf(name, sizeof(name), format, i);
488+
const int ret = snprintf(name, sizeof(name), format, i);
489+
name_ok = (ret > 0 && ret < sizeof(name));
489490
}
490491
else
491492
{
492-
name_ok = snprintf(name, sizeof(name), format, i, j);
493+
const int ret = snprintf(name, sizeof(name), format, i, j);
494+
name_ok = (ret > 0 && ret < sizeof(name));
493495
}
494496

495497
if (!name_ok)

src/openvpn/env_set.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ setenv_str_incr(struct env_set *es, const char *name, const char *value)
334334
strcpy(tmpname, name);
335335
while (NULL != env_set_get(es, tmpname) && counter < 1000)
336336
{
337-
ASSERT(snprintf(tmpname, tmpname_len, "%s_%u", name, counter));
337+
ASSERT(snprintf(tmpname, tmpname_len, "%s_%u", name, counter) < tmpname_len);
338338
counter++;
339339
}
340340
if (counter < 1000)

src/openvpn/platform.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,9 @@ platform_create_temp_file(const char *directory, const char *prefix, struct gc_a
550550
{
551551
++attempts;
552552

553-
if (!snprintf(fname, sizeof(fname), fname_fmt, max_prefix_len, prefix,
554-
(unsigned long)get_random(), (unsigned long)get_random()))
553+
const int ret = snprintf(fname, sizeof(fname), fname_fmt, max_prefix_len, prefix,
554+
(unsigned long)get_random(), (unsigned long)get_random());
555+
if (ret < 0 || ret >= sizeof(fname))
555556
{
556557
msg(M_WARN, "ERROR: temporary filename too long");
557558
return NULL;

src/openvpn/ssl_ncp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ append_cipher_to_ncp_list(struct options *o, const char *ciphername)
198198
size_t newlen = strlen(o->ncp_ciphers) + 1 + strlen(ciphername) + 1;
199199
char *ncp_ciphers = gc_malloc(newlen, false, &o->gc);
200200

201-
ASSERT(snprintf(ncp_ciphers, newlen, "%s:%s", o->ncp_ciphers, ciphername));
201+
ASSERT(snprintf(ncp_ciphers, newlen, "%s:%s", o->ncp_ciphers, ciphername) < newlen);
202202
o->ncp_ciphers = ncp_ciphers;
203203
}
204204

src/openvpn/ssl_verify.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ verify_check_crl_dir(const char *crl_dir, openvpn_x509_cert_t *cert, const char
548548
goto cleanup;
549549
}
550550

551-
if (!snprintf(fn, sizeof(fn), "%s%c%s", crl_dir, PATH_SEPARATOR, serial))
551+
if (snprintf(fn, sizeof(fn), "%s%c%s", crl_dir, PATH_SEPARATOR, serial) >= sizeof(fn))
552552
{
553553
msg(D_HANDSHAKE, "VERIFY CRL: filename overflow");
554554
goto cleanup;

src/openvpn/ssl_verify_mbedtls.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ verify_callback(void *session_obj, mbedtls_x509_crt *cert, int cert_depth, uint3
8585

8686
ret = mbedtls_x509_crt_verify_info(errstr, sizeof(errstr) - 1, "", *flags);
8787
if (ret <= 0
88-
&& !snprintf(errstr, sizeof(errstr), "Could not retrieve error string, flags=%" PRIx32,
89-
*flags))
88+
&& snprintf(errstr, sizeof(errstr), "Could not retrieve error string, flags=%" PRIx32,
89+
*flags)
90+
>= sizeof(errstr))
9091
{
9192
errstr[0] = '\0';
9293
}

src/openvpn/win32.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -881,8 +881,9 @@ env_block(const struct env_set *es)
881881
char force_path[256];
882882
char *sysroot = get_win_sys_path();
883883

884-
if (!snprintf(force_path, sizeof(force_path), "PATH=%s\\System32;%s;%s\\System32\\Wbem",
885-
sysroot, sysroot, sysroot))
884+
if (snprintf(force_path, sizeof(force_path), "PATH=%s\\System32;%s;%s\\System32\\Wbem",
885+
sysroot, sysroot, sysroot)
886+
>= sizeof(force_path))
886887
{
887888
msg(M_WARN, "env_block: default path truncated to %s", force_path);
888889
}

0 commit comments

Comments
 (0)