From: "cron2 (Code Review)" Date: Wed, 4 Mar 2026 14:24:45 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="i7BAI7C4OoE="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: buffer: Add checked_snprintf function and use it in the code List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld , plaisthos Cc: openvpn-devel --i7BAI7C4OoE= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#7) to the change originally created by= flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1489?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by cron2 Change subject: buffer: Add checked_snprintf function and use it in the code ...................................................................... buffer: Add checked_snprintf function and use it in the code This reintroduces a function that converts the result of snprintf to a boolean since the check is always the same but annoyingly verbose. And it gets worse when you add -Wsign-compare. So in preparation of introducing -Wsign-compare wrap this check in the function. This somewhat reverts the removal of openvpn_snprintf. But note that that was originally introduced to work around the broken snprintf of Windows. So this is not exactly the same. For this reason I also classified this as a buffer function and not a compat function. Change-Id: Ia3477b8ee7a637c15aad7f285144280595cda5d5 Signed-off-by: Frank Lichtenheld Acked-by: Gert Doering Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1489 Message-Id: <20260304110455.15859-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg35= 872.html Signed-off-by: Gert Doering --- M src/openvpn/buffer.c M src/openvpn/buffer.h M src/openvpn/crypto_mbedtls.c M src/openvpn/crypto_mbedtls_legacy.c M src/openvpn/dns.c M src/openvpn/env_set.c M src/openvpn/platform.c M src/openvpn/proxy.c M src/openvpn/socks.c M src/openvpn/ssl_ncp.c M src/openvpn/ssl_verify.c M src/openvpn/ssl_verify_mbedtls.c M src/openvpn/tun.c M src/openvpn/win32.c M tests/unit_tests/openvpn/test_buffer.c 15 files changed, 76 insertions(+), 42 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/89/1489/7 diff --git a/src/openvpn/buffer.c b/src/openvpn/buffer.c index 280cbc8..745b1c8 100644 --- a/src/openvpn/buffer.c +++ b/src/openvpn/buffer.c @@ -1134,6 +1134,17 @@ } } +bool +checked_snprintf(char *str, size_t size, const char *format, ...) +{ + va_list arglist; + va_start(arglist, format); + ASSERT(size < INT_MAX); + int len =3D vsnprintf(str, size, format, arglist); + va_end(arglist); + return (len >=3D 0 && len < (ssize_t)size); +} + #ifdef VERIFY_ALIGNMENT void valign4(const struct buffer *buf, const char *file, const int line) diff --git a/src/openvpn/buffer.h b/src/openvpn/buffer.h index 7bb491d..040f752 100644 --- a/src/openvpn/buffer.h +++ b/src/openvpn/buffer.h @@ -972,6 +972,29 @@ return 0 =3D=3D strncmp(str, prefix, strlen(prefix)); } +/** + * Like snprintf() but returns an boolean. + * + * To check the return value of snprintf() one needs to + * do multiple comparisons of the \p size parameter + * against the return value. Doesn't get prettier by + * them being different types with different signedness + * and size. + * + * So this function allows to wrap all of that into one + * boolean return value. + * + * @return true if snprintf() was successful and not truncated. + */ +bool checked_snprintf(char *str, size_t size, const char *format, ...) +#ifdef __GNUC__ +#if __USE_MINGW_ANSI_STDIO + __attribute__((format(gnu_printf, 3, 4))) +#else + __attribute__((format(__printf__, 3, 4))) +#endif +#endif + ; /* * Verify that a pointer is correctly aligned diff --git a/src/openvpn/crypto_mbedtls.c b/src/openvpn/crypto_mbedtls.c index e8931d7..7aaea3d 100644 --- a/src/openvpn/crypto_mbedtls.c +++ b/src/openvpn/crypto_mbedtls.c @@ -1014,7 +1014,7 @@ { char prefix[256]; - if (snprintf(prefix, sizeof(prefix), "%s:%d", func, line) >=3D sizeof(= prefix)) + if (!checked_snprintf(prefix, sizeof(prefix), "%s:%d", func, line)) { return mbed_log_err(flags, errval, func); } @@ -1104,11 +1104,11 @@ char header[1000 + 1] =3D { 0 }; char footer[1000 + 1] =3D { 0 }; - if (snprintf(header, sizeof(header), "-----BEGIN %s-----\n", name) >= =3D sizeof(header)) + if (!checked_snprintf(header, sizeof(header), "-----BEGIN %s-----\n", = name)) { return false; } - if (snprintf(footer, sizeof(footer), "-----END %s-----\n", name) >=3D = sizeof(footer)) + if (!checked_snprintf(footer, sizeof(footer), "-----END %s-----\n", na= me)) { return false; } @@ -1142,11 +1142,11 @@ char header[1000 + 1] =3D { 0 }; char footer[1000 + 1] =3D { 0 }; - if (snprintf(header, sizeof(header), "-----BEGIN %s-----", name) >=3D = sizeof(header)) + if (!checked_snprintf(header, sizeof(header), "-----BEGIN %s-----", na= me)) { return false; } - if (snprintf(footer, sizeof(footer), "-----END %s-----", name) >=3D si= zeof(footer)) + if (!checked_snprintf(footer, sizeof(footer), "-----END %s-----", name= )) { return false; } diff --git a/src/openvpn/crypto_mbedtls_legacy.c b/src/openvpn/crypto_mbedt= ls_legacy.c index 00fe542..237564c 100644 --- a/src/openvpn/crypto_mbedtls_legacy.c +++ b/src/openvpn/crypto_mbedtls_legacy.c @@ -130,7 +130,7 @@ { char prefix[256]; - if (snprintf(prefix, sizeof(prefix), "%s:%d", func, line) >=3D sizeof(= prefix)) + if (!checked_snprintf(prefix, sizeof(prefix), "%s:%d", func, line)) { return mbed_log_err(flags, errval, func); } @@ -246,11 +246,11 @@ char header[1000 + 1] =3D { 0 }; char footer[1000 + 1] =3D { 0 }; - if (snprintf(header, sizeof(header), "-----BEGIN %s-----\n", name) >= =3D sizeof(header)) + if (!checked_snprintf(header, sizeof(header), "-----BEGIN %s-----\n", = name)) { return false; } - if (snprintf(footer, sizeof(footer), "-----END %s-----\n", name) >=3D = sizeof(footer)) + if (!checked_snprintf(footer, sizeof(footer), "-----END %s-----\n", na= me)) { return false; } @@ -283,11 +283,11 @@ char header[1000 + 1] =3D { 0 }; char footer[1000 + 1] =3D { 0 }; - if (snprintf(header, sizeof(header), "-----BEGIN %s-----", name) >=3D = sizeof(header)) + if (!checked_snprintf(header, sizeof(header), "-----BEGIN %s-----", na= me)) { return false; } - if (snprintf(footer, sizeof(footer), "-----END %s-----", name) >=3D si= zeof(footer)) + if (!checked_snprintf(footer, sizeof(footer), "-----END %s-----", name= )) { return false; } diff --git a/src/openvpn/dns.c b/src/openvpn/dns.c index f4f7779..3a294ec 100644 --- a/src/openvpn/dns.c +++ b/src/openvpn/dns.c @@ -485,13 +485,11 @@ if (j < 0) { - const int ret =3D snprintf(name, sizeof(name), format, i); - name_ok =3D (ret > 0 && ret < sizeof(name)); + name_ok =3D checked_snprintf(name, sizeof(name), format, i); } else { - const int ret =3D snprintf(name, sizeof(name), format, i, j); - name_ok =3D (ret > 0 && ret < sizeof(name)); + name_ok =3D checked_snprintf(name, sizeof(name), format, i, j); } if (!name_ok) diff --git a/src/openvpn/env_set.c b/src/openvpn/env_set.c index 99ac45c..d992097 100644 --- a/src/openvpn/env_set.c +++ b/src/openvpn/env_set.c @@ -334,7 +334,7 @@ strcpy(tmpname, name); while (NULL !=3D env_set_get(es, tmpname) && counter < 1000) { - ASSERT(snprintf(tmpname, tmpname_len, "%s_%u", name, counter) < tm= pname_len); + ASSERT(checked_snprintf(tmpname, tmpname_len, "%s_%u", name, count= er)); counter++; } if (counter < 1000) diff --git a/src/openvpn/platform.c b/src/openvpn/platform.c index 4f0eddf..3a6b272 100644 --- a/src/openvpn/platform.c +++ b/src/openvpn/platform.c @@ -550,9 +550,8 @@ { ++attempts; - const int ret =3D snprintf(fname, sizeof(fname), fname_fmt, max_pr= efix_len, prefix, - (unsigned long)get_random(), (unsigned lo= ng)get_random()); - if (ret < 0 || ret >=3D sizeof(fname)) + if (!checked_snprintf(fname, sizeof(fname), fname_fmt, max_prefix_= len, prefix, + (unsigned long)get_random(), (unsigned long)= get_random())) { msg(M_WARN, "ERROR: temporary filename too long"); return NULL; diff --git a/src/openvpn/proxy.c b/src/openvpn/proxy.c index b355827..9f3ec93 100644 --- a/src/openvpn/proxy.c +++ b/src/openvpn/proxy.c @@ -771,11 +771,10 @@ } /* send digest response */ - int sret =3D snprintf( - buf, sizeof(buf), - "Proxy-Authorization: Digest username=3D\"%s\", realm= =3D\"%s\", nonce=3D\"%s\", uri=3D\"%s\", qop=3D%s, nc=3D%s, cnonce=3D\"%s\"= , response=3D\"%s\"%s", - username, realm, nonce, uri, qop, nonce_count, cnonce,= response, opaque_kv); - if (sret >=3D sizeof(buf)) + if (!checked_snprintf( + buf, sizeof(buf), + "Proxy-Authorization: Digest username=3D\"%s\", re= alm=3D\"%s\", nonce=3D\"%s\", uri=3D\"%s\", qop=3D%s, nc=3D%s, cnonce=3D\"%= s\", response=3D\"%s\"%s", + username, realm, nonce, uri, qop, nonce_count, cno= nce, response, opaque_kv)) { goto error; } diff --git a/src/openvpn/socks.c b/src/openvpn/socks.c index 5cb5912..19f3d54 100644 --- a/src/openvpn/socks.c +++ b/src/openvpn/socks.c @@ -119,10 +119,9 @@ goto cleanup; } - int sret =3D snprintf(to_send, sizeof(to_send), "\x01%c%s%c%s", (int)s= trlen(creds.username), - creds.username, (int)strlen(creds.password), creds= .password); - ASSERT(sret >=3D 0 && sret <=3D sizeof(to_send)); - + ASSERT(checked_snprintf(to_send, sizeof(to_send), "\x01%c%s%c%s", + (int)strlen(creds.username), creds.username, + (int)strlen(creds.password), creds.password)); if (!proxy_send(sd, to_send, strlen(to_send))) { goto cleanup; diff --git a/src/openvpn/ssl_ncp.c b/src/openvpn/ssl_ncp.c index 20fd2f0..686f823 100644 --- a/src/openvpn/ssl_ncp.c +++ b/src/openvpn/ssl_ncp.c @@ -198,7 +198,7 @@ size_t newlen =3D strlen(o->ncp_ciphers) + 1 + strlen(ciphername) + 1; char *ncp_ciphers =3D gc_malloc(newlen, false, &o->gc); - ASSERT(snprintf(ncp_ciphers, newlen, "%s:%s", o->ncp_ciphers, cipherna= me) < newlen); + ASSERT(checked_snprintf(ncp_ciphers, newlen, "%s:%s", o->ncp_ciphers, = ciphername)); o->ncp_ciphers =3D ncp_ciphers; } diff --git a/src/openvpn/ssl_verify.c b/src/openvpn/ssl_verify.c index 8bd8e96..d44f25f 100644 --- a/src/openvpn/ssl_verify.c +++ b/src/openvpn/ssl_verify.c @@ -548,7 +548,7 @@ goto cleanup; } - if (snprintf(fn, sizeof(fn), "%s%c%s", crl_dir, PATH_SEPARATOR, serial= ) >=3D sizeof(fn)) + if (!checked_snprintf(fn, sizeof(fn), "%s%c%s", crl_dir, PATH_SEPARATO= R, serial)) { msg(D_HANDSHAKE, "VERIFY CRL: filename overflow"); goto cleanup; diff --git a/src/openvpn/ssl_verify_mbedtls.c b/src/openvpn/ssl_verify_mbed= tls.c index adeaa13..ad5479c 100644 --- a/src/openvpn/ssl_verify_mbedtls.c +++ b/src/openvpn/ssl_verify_mbedtls.c @@ -93,9 +93,7 @@ ret =3D mbedtls_x509_crt_verify_info(errstr, sizeof(errstr) - 1, "= ", *flags); if (ret <=3D 0 - && snprintf(errstr, sizeof(errstr), "Could not retrieve error = string, flags=3D%" PRIx32, - *flags) - >=3D sizeof(errstr)) + && !checked_snprintf(errstr, sizeof(errstr), "Could not retrie= ve error string, flags=3D%" PRIx32, *flags)) { errstr[0] =3D '\0'; } diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c index 3f9ee5d..ecebff7 100644 --- a/src/openvpn/tun.c +++ b/src/openvpn/tun.c @@ -3558,9 +3558,7 @@ msg(M_FATAL, "Error enumerating registry subkeys of key: %s", = ADAPTER_KEY); } - int ret =3D snprintf(unit_string, sizeof(unit_string), "%s\\%s", A= DAPTER_KEY, enum_name); - - if (ret < 0 || ret >=3D sizeof(unit_string)) + if (!checked_snprintf(unit_string, sizeof(unit_string), "%s\\%s", = ADAPTER_KEY, enum_name)) { msg(M_WARN, "Error constructing unit string for %s", enum_name= ); continue; @@ -3673,10 +3671,9 @@ msg(M_FATAL, "Error enumerating registry subkeys of key: %s", = NETWORK_CONNECTIONS_KEY); } - int ret =3D snprintf(connection_string, sizeof(connection_string),= "%s\\%s\\Connection", - NETWORK_CONNECTIONS_KEY, enum_name); - - if (ret < 0 || ret >=3D sizeof(connection_string)) + if (!checked_snprintf(connection_string, sizeof(connection_string), + "%s\\%s\\Connection", + NETWORK_CONNECTIONS_KEY, enum_name)) { msg(M_WARN, "Error constructing connection string for %s", enu= m_name); continue; diff --git a/src/openvpn/win32.c b/src/openvpn/win32.c index b938d7b..ac449fd 100644 --- a/src/openvpn/win32.c +++ b/src/openvpn/win32.c @@ -881,9 +881,8 @@ char force_path[256]; char *sysroot =3D get_win_sys_path(); - if (snprintf(force_path, sizeof(force_path), "PATH=3D%s\\System32;%s;%= s\\System32\\Wbem", - sysroot, sysroot, sysroot) - >=3D sizeof(force_path)) + if (!checked_snprintf(force_path, sizeof(force_path), "PATH=3D%s\\Syst= em32;%s;%s\\System32\\Wbem", + sysroot, sysroot, sysroot)) { msg(M_WARN, "env_block: default path truncated to %s", force_path); } diff --git a/tests/unit_tests/openvpn/test_buffer.c b/tests/unit_tests/open= vpn/test_buffer.c index 1c78cf0..5f43e0d 100644 --- a/tests/unit_tests/openvpn/test_buffer.c +++ b/tests/unit_tests/openvpn/test_buffer.c @@ -424,6 +424,16 @@ #endif } +static void +test_checked_snprintf(void **state) +{ + char buf[10]; + assert_true(checked_snprintf(buf, sizeof(buf), "%s", "Hello")); + assert_true(checked_snprintf(buf, sizeof(buf), "%s", "Hello Foo")); + assert_false(checked_snprintf(buf, sizeof(buf), "%s", "Hello Foo!")); + assert_false(checked_snprintf(buf, sizeof(buf), "%s", "Hello World!")); +} + void test_buffer_chomp(void **state) { @@ -528,6 +538,7 @@ cmocka_unit_test(test_character_class), cmocka_unit_test(test_character_string_mod_buf), cmocka_unit_test(test_snprintf), + cmocka_unit_test(test_checked_snprintf), cmocka_unit_test(test_buffer_chomp), cmocka_unit_test(test_buffer_parse) }; -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1489?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: newpatchset Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Ia3477b8ee7a637c15aad7f285144280595cda5d5 Gerrit-Change-Number: 1489 Gerrit-PatchSet: 7 Gerrit-Owner: flichtenheld Gerrit-Reviewer: cron2 Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel --i7BAI7C4OoE= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 upl= oaded patch set #7 to the change originally created by flichtenhel= d.

View Change

The following approvals got outdated and were remove= d: Code-Review+2 by cron2

buffer: Add checked_snprintf functi=
on and use it in the code

This reintroduces a function that converts= the result
of snprintf to a boolean since the check is always the
sa= me but annoyingly verbose. And it gets worse when you add
-Wsign-compare= .

So in preparation of introducing -Wsign-compare wrap this
check= in the function.

This somewhat reverts the removal of openvpn_snpri= ntf.
But note that that was originally introduced to work
around the = broken snprintf of Windows. So this is not
exactly the same. For this re= ason I also classified this
as a buffer function and not a compat functi= on.

Change-Id: Ia3477b8ee7a637c15aad7f285144280595cda5d5
Signed-o= ff-by: Frank Lichtenheld <frank@...2641...>
Acked-by: Gert Doering= <gert@...1296...>
Gerrit URL: https://gerrit.openvpn.net/c/openvp= n/+/1489
Message-Id: <20260304110455.15859-1-gert@...1296...>
U= RL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg358= 72.html
Signed-off-by: Gert Doering <gert@...1296...>
---
M = src/openvpn/buffer.c
M src/openvpn/buffer.h
M src/openvpn/crypto_mbed= tls.c
M src/openvpn/crypto_mbedtls_legacy.c
M src/openvpn/dns.c
M = src/openvpn/env_set.c
M src/openvpn/platform.c
M src/openvpn/proxy.c<= br>M src/openvpn/socks.c
M src/openvpn/ssl_ncp.c
M src/openvpn/ssl_ve= rify.c
M src/openvpn/ssl_verify_mbedtls.c
M src/openvpn/tun.c
M sr= c/openvpn/win32.c
M tests/unit_tests/openvpn/test_buffer.c
15 files c= hanged, 76 insertions(+), 42 deletions(-)

git pu=
ll ssh://gerrit.openvpn.net:29418/openvpn refs/changes/89/1489/7
di=
ff --git a/src/openvpn/buffer.c b/src/openvpn/buffer.c
inde= x 280cbc8..745b1c8 100644
--- a/src/openvpn/buffer.c=
+++ b/src/openvpn/buffer.c
@@ -1134,6 +1134,17 @@=
}
}

+bool
+checked_snprintf(char *str, size_t size, const ch= ar *format, ...)
+{
+ va_list arglist;<= /span>
+ va_start(arglist= , format);
+ ASSER= T(size < INT_MAX);
+ int len =3D vsnprintf(str, size, format, arglist);
+ va_end(arglist);
+ return (len >=3D 0 && = len < (ssize_t)size);
+}
+
#ifdef VERIFY_ALIGNMENT

void
valign4= (const struct buffer *buf, const char *file, const int line)
diff --git a/src/openvpn/buffer.h b/src/openvpn/buffer.h

= index 7bb491d..040f752 100644
--- a/src/openvpn/buffer.h
+++ b/src/openvpn/buffer.h
@@ -972,6 +972,29 = @@
return 0 =3D=3D strncmp(str, prefix, strlen(prefix)= );
}

+/**
+ * Like snprintf() but returns an boolean.
+ *
+ * To check the return value of snprintf() one needs to+ * do multiple comparisons of= the \p size parameter
+ * them being different types with = different signedness
= + * and size.
+ *
+ * So this function all= ows to wrap all of that into one
+ * boolean return value.
+ *
= + * @return true if snprintf() was successful and not truncated.
= + */
+bool checked_snprintf(char *str, size_t size, = const char *format, ...)
+#ifdef __GNUC__
= +#if __USE_MINGW_ANSI_STDIO
+ __attribute__((format(gnu_printf, 3, 4)))
+#else
+ __attribute__((format(__printf__, 3, 4)))+#endif
+#endif
+ ;

/*
= * Verify that a pointer is correctly aligned
diff -= -git a/src/openvpn/crypto_mbedtls.c b/src/openvpn/crypto_mbedtls.cindex e8931d7..7aaea3d 100644
--- a/src/openvpn/cry= pto_mbedtls.c
+++ b/src/openvpn/crypto_mbedtls.c
= @@ -1014,7 +1014,7 @@
{
char pr= efix[256];

- if (snprintf(prefix, sizeof(prefix), "%s:%d", func, li= ne) >=3D sizeof(prefix))
+ if (!checked_snprintf(prefix, sizeof(prefix), "%s:%d"= , func, line))
{
return mbed_= log_err(flags, errval, func);
}
@@ -11= 04,11 +1104,11 @@
char header[1000 + 1] =3D { 0 };
char footer[1000 + 1] =3D { 0 };
=
- if (snprintf(header, siz= eof(header), "-----BEGIN %s-----\n", name) >=3D sizeof(header)= )
+ if (!checked_s= nprintf(header, sizeof(header), "-----BEGIN %s-----\n", name))
{
return false;
}

- if (snprin= tf(footer, sizeof(footer), "-----END %s-----\n", name) >=3D si= zeof(footer))
+ if= (!checked_snprintf(footer, sizeof(footer), "-----END %s-----\n",= name))
{
return false;
}
@@ -1142,11 +1142,11 @@
= char header[1000 + 1] =3D { 0 };
char footer[1000 = + 1] =3D { 0 };

- if (snprintf(header, sizeof(header), "-----BEGIN %s---= --", name) >=3D sizeof(header))
+ if (!checked_snprintf(header, sizeof(header), "= ;-----BEGIN %s-----", name))
{
= return false;
}
- if (snprintf(footer, sizeof(footer), "-----E= ND %s-----", name) >=3D sizeof(footer))
+ if (!checked_snprintf(footer, sizeof(footer= ), "-----END %s-----", name))
{
return false;

}
diff --gi= t a/src/openvpn/crypto_mbedtls_legacy.c b/src/openvpn/crypto_mbedtls_legacy= .c
index 00fe542..237564c 100644
--- a/src/= openvpn/crypto_mbedtls_legacy.c
+++ b/src/openvpn/crypto_mb= edtls_legacy.c
@@ -130,7 +130,7 @@
{
char prefix[256];

- if (snprintf(prefix, sizeof(prefix), &q= uot;%s:%d", func, line) >=3D sizeof(prefix))
+ if (!checked_snprintf(prefix, sizeof(= prefix), "%s:%d", func, line))
{
<= span> return mbed_log_err(flags, errval, func);

= }
@@ -246,11 +246,11 @@
char header[= 1000 + 1] =3D { 0 };
char footer[1000 + 1] =3D { 0 };<= /span>

- = if (snprintf(header, sizeof(header), "-----BEGIN %s-----\n", name= ) >=3D sizeof(header))
+ if (!checked_snprintf(header, sizeof(header), "-----BEGIN %s= -----\n", name))
{
retur= n false;
}
- if (snprintf(footer, sizeof(footer), "-----END %s-----\n= ", name) >=3D sizeof(footer))
+ if (!checked_snprintf(footer, sizeof(footer), "-= ----END %s-----\n", name))
{
= return false;
}
@@ -283,11 +283,1= 1 @@
char header[1000 + 1] =3D { 0 };
= char footer[1000 + 1] =3D { 0 };

- if (snprintf(header, sizeof(header), = "-----BEGIN %s-----", name) >=3D sizeof(header))
+ if (!checked_snprintf(header,= sizeof(header), "-----BEGIN %s-----", name))

= {
return false;
}- if (snprintf(footer, sizeof= (footer), "-----END %s-----", name) >=3D sizeof(footer))
+ if (!checked_snprintf= (footer, sizeof(footer), "-----END %s-----", name))
{

return false;
}
diff --git a/src/openvpn/dns.c b/src/openvpn/dns.c
<= span>index f4f7779..3a294ec 100644

--- a/src/openvpn/dns.c<= /span>
+++ b/src/openvpn/dns.c
@@ -485,13 +485,11 = @@

if (j < 0)
= {
- const int = ret =3D snprintf(name, sizeof(name), format, i);
- name_ok =3D (ret > 0 && ret &= lt; sizeof(name));
+ = name_ok =3D checked_snprintf(name, sizeof(name), format, i);<= br> }
else
{
= - const int ret =3D snprin= tf(name, sizeof(name), format, i, j);
- name_ok =3D (ret > 0 && ret < sizeof(= name));
+ name= _ok =3D checked_snprintf(name, sizeof(name), format, i, j);
}


if (!name_ok)
diff --git a/src/openvpn/env_set.c b/src/openvpn/env_set.c

index 99ac45c..d992097 100644

--- a/src/openvpn/env_set.c=
+++ b/src/openvpn/env_set.c
@@ -334,7 +334= ,7 @@
strcpy(tmpname, name);
whil= e (NULL !=3D env_set_get(es, tmpname) && counter < 1000)<= br> {
- = ASSERT(snprintf(tmpname, tmpname_len, "%s_%u", name, counter) &= lt; tmpname_len);
+ = ASSERT(checked_snprintf(tmpname, tmpname_len, "%s_%u", name= , counter));
counter++;
}
if (counter < 1000)
diff --git a/src/o= penvpn/platform.c b/src/openvpn/platform.c
index 4f0eddf..3= a6b272 100644
--- a/src/openvpn/platform.c
= +++ b/src/openvpn/platform.c
@@ -550,9 +550,8 @@
= {
++attempts;
<= br>- const int ret =3D snp= rintf(fname, sizeof(fname), fname_fmt, max_prefix_len, prefix,
- (= unsigned long)get_random(), (unsigned long)get_random());

- if (ret < 0 || ret >=3D s= izeof(fname))
+ = if (!checked_snprintf(fname, sizeof(fname), fname_fmt, max_prefix_len, pr= efix,
+ = (unsigned long)get_random(), (unsigned long)get_random()))<= /span>
{
msg(M_WARN, "E= RROR: temporary filename too long");
retu= rn NULL;
diff --git a/src/openvpn/proxy.c b/src/openvpn/pro= xy.c
index b355827..9f3ec93 100644
--- a/sr= c/openvpn/proxy.c
+++ b/src/openvpn/proxy.c
@@ -771,11 +771,10 @@

}
=
/* send digest response */
- int sret =3D snprint= f(

- = buf, sizeof(buf),
- = "Proxy-Authorization: Digest username=3D\"%s\&qu= ot;, realm=3D\"%s\", nonce=3D\"%s\", uri=3D\"%s\&q= uot;, qop=3D%s, nc=3D%s, cnonce=3D\"%s\", response=3D\"%s\&q= uot;%s",
- = username, realm, nonce, uri, qop, nonce_count, cnonce, response= , opaque_kv);
- = if (sret >=3D sizeof(buf))
+ if (!checked_snprintf(
+ buf, sizeof(bu= f),
+ = "Proxy-Authorization: Digest username=3D\"%s\", real= m=3D\"%s\", nonce=3D\"%s\", uri=3D\"%s\", qop= =3D%s, nc=3D%s, cnonce=3D\"%s\", response=3D\"%s\"%s&qu= ot;,
+ = username, realm, nonce, uri, qop, nonce_count, cnonce, response, o= paque_kv))
{
= goto error;
}
diff= --git a/src/openvpn/socks.c b/src/openvpn/socks.c
index 5c= b5912..19f3d54 100644
--- a/src/openvpn/socks.c
<= span>+++ b/src/openvpn/socks.c
@@ -119,10 +119,9 @@<= br> goto cleanup;
}

- int sret =3D snprint= f(to_send, sizeof(to_send), "\x01%c%s%c%s", (int)strlen(creds.use= rname),
- = creds.username, (int)strlen(creds.password), creds.password);
- ASSERT(sret >=3D 0= && sret <=3D sizeof(to_send));
-
+ ASSERT(checked_snprintf(to_send, sizeof(to_send), "\x01%c%s%c%s&= quot;,
+ = (int)strlen(creds.username), creds.username,
+ (int)st= rlen(creds.password), creds.password));

if (!proxy_sen= d(sd, to_send, strlen(to_send)))
{
= goto cleanup;
diff --git a/src/openvpn/ssl_ncp.c b/sr= c/openvpn/ssl_ncp.c
index 20fd2f0..686f823 100644--- a/src/openvpn/ssl_ncp.c
+++ b/src/openvpn/ssl_nc= p.c
@@ -198,7 +198,7 @@
size_t newlen = =3D strlen(o->ncp_ciphers) + 1 + strlen(ciphername) + 1;
char *ncp_ciphers =3D gc_malloc(newlen, false, &o->gc);
=

- ASSERT= (snprintf(ncp_ciphers, newlen, "%s:%s", o->ncp_ciphers, cipher= name) < newlen);
+= ASSERT(checked_snprintf(ncp_ciphers, newlen, "%s:%s", o->n= cp_ciphers, ciphername));
o->ncp_ciphers =3D ncp_ci= phers;
}

diff --git a/sr= c/openvpn/ssl_verify.c b/src/openvpn/ssl_verify.c
index 8bd= 8e96..d44f25f 100644
--- a/src/openvpn/ssl_verify.c<= br>+++ b/src/openvpn/ssl_verify.c
@@ -548,7 +548,7 @@=
goto cleanup;
}

- if (snprintf= (fn, sizeof(fn), "%s%c%s", crl_dir, PATH_SEPARATOR, serial) >= =3D sizeof(fn))
+ = if (!checked_snprintf(fn, sizeof(fn), "%s%c%s", crl_dir, PATH_SEP= ARATOR, serial))
{
msg(D_HAND= SHAKE, "VERIFY CRL: filename overflow");
= goto cleanup;
diff --git a/src/openvpn/ssl_verify_mbedtls.= c b/src/openvpn/ssl_verify_mbedtls.c
index adeaa13..ad5479c= 100644
--- a/src/openvpn/ssl_verify_mbedtls.c
+++ b/src/openvpn/ssl_verify_mbedtls.c
@@ -93,9 +93,7 @= @

ret =3D mbedtls_x509_crt_verif= y_info(errstr, sizeof(errstr) - 1, "", *flags);
= if (ret <=3D 0
- && snprintf(errstr, sizeof(errstr), "Could not= retrieve error string, flags=3D%" PRIx32,
- *flags)
- >=3D sizeof(errst= r))
+ &= ;& !checked_snprintf(errstr, sizeof(errstr), "Could not retrieve e= rror string, flags=3D%" PRIx32, *flags))
{

errstr[0] =3D '\0';
= }
diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c
index 3f9ee5d..ecebff7 100644
--- a/src/open= vpn/tun.c
+++ b/src/openvpn/tun.c
@@ -3558,= 9 +3558,7 @@
msg(M_FATAL, "Error enumerat= ing registry subkeys of key: %s", ADAPTER_KEY);
= }

= - int ret =3D snprintf(unit_string, sizeof(unit_string), "%s\\%= s", ADAPTER_KEY, enum_name);
-
- i= f (ret < 0 || ret >=3D sizeof(unit_string))
+ if (!checked_snprintf(unit_string, si= zeof(unit_string), "%s\\%s", ADAPTER_KEY, enum_name))
<= span> {

msg(M_WARN, "Error constr= ucting unit string for %s", enum_name);
c= ontinue;
@@ -3673,10 +3671,9 @@
= msg(M_FATAL, "Error enumerating registry subkeys of key: %s", N= ETWORK_CONNECTIONS_KEY);
}

- int ret =3D snprint= f(connection_string, sizeof(connection_string), "%s\\%s\\Connection&qu= ot;,
- = NETWORK_CONNECTIONS_KEY, enum_name);
-
- if (ret < 0 || ret >=3D sizeof(connection_string))=
+ if (!checked_snpri= ntf(connection_string, sizeof(connection_string),
+ "%s\\%s\\C= onnection",
+ = NETWORK_CONNECTIONS_KEY, enum_name))
<= span> {

msg(M_WARN, "Error constr= ucting connection string for %s", enum_name);
= continue;
diff --git a/src/openvpn/win32.c b/src/openv= pn/win32.c
index b938d7b..ac449fd 100644
--= - a/src/openvpn/win32.c
+++ b/src/openvpn/win32.c@@ -881,9 +881,8 @@
char force_path[256];
char *sysroot =3D get_win_sys_path();

- if (snprintf(force_pa= th, sizeof(force_path), "PATH=3D%s\\System32;%s;%s\\System32\\Wbem&quo= t;,
- s= ysroot, sysroot, sysroot)
- >=3D sizeof(force_path))
+ if (!checked_snprintf(force_path, sizeof(force_path= ), "PATH=3D%s\\System32;%s;%s\\System32\\Wbem",
+ sysroot, sy= sroot, sysroot))
{
msg(M_WARN= , "env_block: default path truncated to %s", force_path);<= br> }
diff --git a/tests/unit_tests/openvpn/test_= buffer.c b/tests/unit_tests/openvpn/test_buffer.c
index 1c7= 8cf0..5f43e0d 100644
--- a/tests/unit_tests/openvpn/test_bu= ffer.c
+++ b/tests/unit_tests/openvpn/test_buffer.c<= br>@@ -424,6 +424,16 @@
#endif
}


+stat= ic void
+test_checked= _snprintf(void **state)
+{
+ char buf[1= 0];
+ assert_true(= checked_snprintf(buf, sizeof(buf), "%s", "Hello"));
+ assert_true(checked= _snprintf(buf, sizeof(buf), "%s", "Hello Foo"));=
+ assert_false(checked_s= nprintf(buf, sizeof(buf), "%s", "Hello Foo!"));<= br>+ assert_false(checked_sn= printf(buf, sizeof(buf), "%s", "Hello World!"));=
+}
+
void
test= _buffer_chomp(void **state)
{
@@ -528,6 +5= 38,7 @@
cmocka_unit_test(test_character_class),
cmocka_unit_test(test_character_string_mod_buf),
cmocka_unit_test(test_snprintf),
+ cmocka_unit_test(test_checked_sn= printf),
cmocka_unit_test(test_buffer_chomp),

cmocka_unit_test(test_buffer_parse)
= };

To view, visit change 1489. To unsubs= cribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia3477b8ee7a637c15aad7f28514= 4280595cda5d5
Gerrit-Change-Number: 1489
Gerrit-PatchSet: 7
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
= --i7BAI7C4OoE=--