From: "cron2 (Code Review)" Date: Thu, 30 Oct 2025 21:03:55 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <69a689e5eaa31af2b4c772000597e7283d0a53f7-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="xiT+ASWRUbU="; charset=UTF-8 Subject: [Openvpn-devel] [S] Change in openvpn[master]: Ensure return value of snprintf is correctly checked List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos Cc: flichtenheld , openvpn-devel --xiT+ASWRUbU= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/13= 30?usp=3Demail ) Change subject: Ensure return value of snprintf is correctly checked ...................................................................... Ensure return value of snprintf is correctly checked Commit 130548fe4d 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 Found-by: ZeroPath (https://zeropath.com/) Change-Id: I830b6b27fc3efe707e103ba629c4bfe3796a9cbe Signed-off-by: Arne Schwabe Acked-by: Gert Doering Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1330 Message-Id: <20251030193638.1010-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg34= 063.html Signed-off-by: Gert Doering --- M src/openvpn/crypto_mbedtls.c M src/openvpn/dns.c M src/openvpn/env_set.c M src/openvpn/platform.c M src/openvpn/ssl_ncp.c M src/openvpn/ssl_verify.c M src/openvpn/ssl_verify_mbedtls.c M src/openvpn/win32.c 8 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/openvpn/crypto_mbedtls.c b/src/openvpn/crypto_mbedtls.c index 2e328c3..89f0ab9 100644 --- a/src/openvpn/crypto_mbedtls.c +++ b/src/openvpn/crypto_mbedtls.c @@ -127,7 +127,7 @@ { char prefix[256]; - if (!snprintf(prefix, sizeof(prefix), "%s:%d", func, line)) + if (snprintf(prefix, sizeof(prefix), "%s:%d", func, line) >=3D sizeof(= prefix)) { return mbed_log_err(flags, errval, func); } @@ -243,11 +243,11 @@ char header[1000 + 1] =3D { 0 }; char footer[1000 + 1] =3D { 0 }; - if (!snprintf(header, sizeof(header), "-----BEGIN %s-----\n", name)) + if (snprintf(header, sizeof(header), "-----BEGIN %s-----\n", name) >= =3D sizeof(header)) { return false; } - if (!snprintf(footer, sizeof(footer), "-----END %s-----\n", name)) + if (snprintf(footer, sizeof(footer), "-----END %s-----\n", name) >=3D = sizeof(footer)) { return false; } @@ -280,11 +280,11 @@ char header[1000 + 1] =3D { 0 }; char footer[1000 + 1] =3D { 0 }; - if (!snprintf(header, sizeof(header), "-----BEGIN %s-----", name)) + if (snprintf(header, sizeof(header), "-----BEGIN %s-----", name) >=3D = sizeof(header)) { return false; } - if (!snprintf(footer, sizeof(footer), "-----END %s-----", name)) + if (snprintf(footer, sizeof(footer), "-----END %s-----", name) >=3D si= zeof(footer)) { return false; } diff --git a/src/openvpn/dns.c b/src/openvpn/dns.c index d2ff670..539023f 100644 --- a/src/openvpn/dns.c +++ b/src/openvpn/dns.c @@ -485,11 +485,13 @@ if (j < 0) { - name_ok =3D snprintf(name, sizeof(name), format, i); + const int ret =3D snprintf(name, sizeof(name), format, i); + name_ok =3D (ret > 0 && ret < sizeof(name)); } else { - name_ok =3D snprintf(name, sizeof(name), format, i, j); + const int ret =3D snprintf(name, sizeof(name), format, i, j); + name_ok =3D (ret > 0 && ret < sizeof(name)); } if (!name_ok) diff --git a/src/openvpn/env_set.c b/src/openvpn/env_set.c index 2ae71ab..1311e6f 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)); + ASSERT(snprintf(tmpname, tmpname_len, "%s_%u", name, counter) < tm= pname_len); counter++; } if (counter < 1000) diff --git a/src/openvpn/platform.c b/src/openvpn/platform.c index a1ffdaf..a612237 100644 --- a/src/openvpn/platform.c +++ b/src/openvpn/platform.c @@ -550,8 +550,9 @@ { ++attempts; - if (!snprintf(fname, sizeof(fname), fname_fmt, max_prefix_len, pre= fix, - (unsigned long)get_random(), (unsigned long)get_rand= om())) + 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)) { msg(M_WARN, "ERROR: temporary filename too long"); return NULL; diff --git a/src/openvpn/ssl_ncp.c b/src/openvpn/ssl_ncp.c index 790e50f..d4519b0 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)); + ASSERT(snprintf(ncp_ciphers, newlen, "%s:%s", o->ncp_ciphers, cipherna= me) < newlen); o->ncp_ciphers =3D ncp_ciphers; } diff --git a/src/openvpn/ssl_verify.c b/src/openvpn/ssl_verify.c index a16f5fa..a11003c 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, seria= l)) + if (snprintf(fn, sizeof(fn), "%s%c%s", crl_dir, PATH_SEPARATOR, serial= ) >=3D sizeof(fn)) { 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 472eb49..9e2aa19 100644 --- a/src/openvpn/ssl_verify_mbedtls.c +++ b/src/openvpn/ssl_verify_mbedtls.c @@ -85,8 +85,9 @@ 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)) + && snprintf(errstr, sizeof(errstr), "Could not retrieve error = string, flags=3D%" PRIx32, + *flags) + >=3D sizeof(errstr)) { errstr[0] =3D '\0'; } diff --git a/src/openvpn/win32.c b/src/openvpn/win32.c index 3c11ec3..df29dd7 100644 --- a/src/openvpn/win32.c +++ b/src/openvpn/win32.c @@ -881,8 +881,9 @@ 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)) + if (snprintf(force_path, sizeof(force_path), "PATH=3D%s\\System32;%s;%= s\\System32\\Wbem", + sysroot, sysroot, sysroot) + >=3D sizeof(force_path)) { msg(M_WARN, "env_block: default path truncated to %s", force_path); } -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1330?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: merged Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I830b6b27fc3efe707e103ba629c4bfe3796a9cbe Gerrit-Change-Number: 1330 Gerrit-PatchSet: 2 Gerrit-Owner: plaisthos Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel --xiT+ASWRUbU= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 sub= mitted this change.

View Change

Ensure return value of snprintf is correctly che=
cked

Commit 130548fe4d change the usages of openvpn_snprintf to snpr= intf. When
doing that conversion I did not notice that, despite the func= tion name,
openvpn_snprintf had a different semantic for the return valu= e.

This commit goes through all the usages of snprintf and ensures t= hat
the return is correctly checked for overruns.

Reported-by: Jo= shua Rogers <contact@...2949...>
Found-by: ZeroPath (https://zerop= ath.com/)
Change-Id: I830b6b27fc3efe707e103ba629c4bfe3796a9cbe
Signed= -off-by: Arne Schwabe <arne@...1227...>
Acked-by: Gert Doering <= ;gert@...1296...>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/= 1330
Message-Id: <20251030193638.1010-1-gert@...1296...>
URL: h= ttps://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg34063.ht= ml
Signed-off-by: Gert Doering <gert@...1296...>
---
M src/o= penvpn/crypto_mbedtls.c
M src/openvpn/dns.c
M src/openvpn/env_set.cM src/openvpn/platform.c
M src/openvpn/ssl_ncp.c
M src/openvpn/ssl_= verify.c
M src/openvpn/ssl_verify_mbedtls.c
M src/openvpn/win32.c
= 8 files changed, 21 insertions(+), 16 deletions(-)

diff --git a/src/openvpn/crypto_mbedtls.c b/src/openvpn/crypto_mbedtls.c=

index 2e328c3..89f0ab9 100644
--- a/src/op= envpn/crypto_mbedtls.c
+++ b/src/openvpn/crypto_mbedtls.c
@@ -127,7 +127,7 @@
{
= char prefix[256];

- if (!snprintf(prefix, sizeof(prefix), "%s:%d", = func, line))
+ if = (snprintf(prefix, sizeof(prefix), "%s:%d", func, line) >=3D si= zeof(prefix))
{
return mbed_l= og_err(flags, errval, func);
}
@@ -243= ,11 +243,11 @@
char header[1000 + 1] =3D { 0 };=
char footer[1000 + 1] =3D { 0 };
- if (!snprintf(header, sizeo= f(header), "-----BEGIN %s-----\n", name))
+ if (snprintf(header, sizeof(header), = "-----BEGIN %s-----\n", name) >=3D sizeof(header))
<= span> {

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


- if = (!snprintf(header, sizeof(header), "-----BEGIN %s-----", name))
+ if (snprintf(head= er, sizeof(header), "-----BEGIN %s-----", name) >=3D sizeof(he= ader))
{
return false;=
}
- i= f (!snprintf(footer, sizeof(footer), "-----END %s-----", name))
+ if (snprintf(foot= er, sizeof(footer), "-----END %s-----", name) >=3D sizeof(foot= er))
{
return false; }
diff --git a/src/openvpn/dns.c b/src/openvpn= /dns.c
index d2ff670..539023f 100644
--- a/= src/openvpn/dns.c
+++ b/src/openvpn/dns.c
@= @ -485,11 +485,13 @@

if (j < 0)
{
-= name_ok =3D snprintf(name, sizeof(name), format, i);
+ const int ret =3D snprintf= (name, sizeof(name), format, i);

+ name_ok =3D (ret > 0 && ret < sizeof(nam= e));
}
else
= {
- name_ok =3D = snprintf(name, sizeof(name), format, i, j);
+ const int ret =3D snprintf(name, sizeof(nam= e), format, i, j);
+ = name_ok =3D (ret > 0 && ret < sizeof(name)); }

if (!name_ok)diff --git a/src/openvpn/env_set.c b/src/openvpn/env_set.cindex 2ae71ab..1311e6f 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, coun= ter));
+ ASSER= T(snprintf(tmpname, tmpname_len, "%s_%u", name, counter) < tmp= name_len);
counter++;
}
if (counter < 1000)
diff --git a/src/ope= nvpn/platform.c b/src/openvpn/platform.c
index a1ffdaf..a61= 2237 100644
--- a/src/openvpn/platform.c
++= + b/src/openvpn/platform.c
@@ -550,8 +550,9 @@
{

++attempts;
- if (!snprintf(fname, si= zeof(fname), fname_fmt, max_prefix_len, prefix,
- (unsigned long)get_random()= , (unsigned long)get_random()))
+ const int ret =3D snprintf(fname, sizeof(fname), fname_= fmt, max_prefix_len, prefix,
+ (unsigned long)get_random(), (uns= igned long)get_random());
+ if (ret < 0 || ret >=3D sizeof(fname))
= {
msg(M_WARN, "ERROR: temporary = filename too long");
return NULL;<= br>diff --git a/src/openvpn/ssl_ncp.c b/src/openvpn/ssl_ncp.c<= br>index 790e50f..d4519b0 100644
--- a/src/openvpn/ss= l_ncp.c
+++ b/src/openvpn/ssl_ncp.c
@@ -198= ,7 +198,7 @@
size_t newlen =3D strlen(o->ncp_cipher= s) + 1 + strlen(ciphername) + 1;
char *ncp_ciphers =3D= gc_malloc(newlen, false, &o->gc);

- ASSERT(snprintf(ncp_ciphers, newl= en, "%s:%s", o->ncp_ciphers, ciphername));

+ ASSERT(snprintf(ncp_ciphers, newlen= , "%s:%s", o->ncp_ciphers, ciphername) < newlen); o->ncp_ciphers =3D ncp_ciphers;
}
diff --git a/src/openvpn/ssl_verify.c b/src/openvp= n/ssl_verify.c
index a16f5fa..a11003c 100644
--- a/src/openvpn/ssl_verify.c

+++ b/src/openvpn/ssl_veri= fy.c
@@ -548,7 +548,7 @@
goto clea= nup;
}

- if (!snprintf(fn, sizeof(fn), "%s%c%s"= ;, crl_dir, PATH_SEPARATOR, serial))
+ if (snprintf(fn, sizeof(fn), "%s%c%s", crl_d= ir, PATH_SEPARATOR, serial) >=3D sizeof(fn))
{
msg(D_HANDSHAKE, "VERIFY CRL: filename overflow&q= uot;);
goto cleanup;
diff --git a/= src/openvpn/ssl_verify_mbedtls.c b/src/openvpn/ssl_verify_mbedtls.c<= br>index 472eb49..9e2aa19 100644
--- a/src/openvpn/ss= l_verify_mbedtls.c
+++ b/src/openvpn/ssl_verify_mbedtls.c
@@ -85,8 +85,9 @@

= ret =3D mbedtls_x509_crt_verify_info(errstr, sizeof(errstr) - 1, "&qu= ot;, *flags);
if (ret <=3D 0
- && !snprintf(errstr,= sizeof(errstr), "Could not retrieve error string, flags=3D%" PRI= x32,
- = *flags))
+ = && snprintf(errstr, sizeof(errstr), "Could not retrie= ve error string, flags=3D%" PRIx32,
+ *flags)
+ >=3D sizeof(errstr)= )
{
errstr[0] =3D = 9;\0';
}
diff --git a/src/open= vpn/win32.c b/src/openvpn/win32.c
index 3c11ec3..df29dd7 10= 0644
--- a/src/openvpn/win32.c
+++ b/src/op= envpn/win32.c
@@ -881,8 +881,9 @@
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))
+ if (snprintf(force_path, sizeof(force= _path), "PATH=3D%s\\System32;%s;%s\\System32\\Wbem",
+ sysroot, sysroo= t, sysroot)

+ = >=3D sizeof(force_path))
{
= msg(M_WARN, "env_block: default path truncated to %s", force_pat= h);
}

To view, vis= it chang= e 1330. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: merged
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I830b6b27fc3efe707e103ba629c= 4bfe3796a9cbe
Gerrit-Change-Number: 1330
Gerrit-PatchSet: 2
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
= --xiT+ASWRUbU=--