Change in openvpn[master]: buffer: Add checked_snprintf function and use it in the code
A message that is not in the archive (sent off-list or not yet received): gerrit.1769443458000.Ia3477b8ee7a637c15aad7f285144280595cda5d5@...2715...
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/+/1489?usp=email
to review the following change.
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.
---
M src/openvpn/buffer.c
M src/openvpn/buffer.h
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
14 files changed, 71 insertions(+), 37 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/89/1489/1
src/openvpn/buffer.c +11 -0
@@ -1125,6 +1125,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 =vsnprintf (str, size, format, arglist); +va_end (arglist); +return (len >=0 && len < (ssize_t )size); +} +#ifdef VERIFY_ALIGNMENT void valign4 (const struct buffer *buf,const char *file,const int line)
src/openvpn/buffer.h +23 -0
@@ -971,6 +971,29 @@return 0 ==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
src/openvpn/crypto_mbedtls_legacy.c +5 -5
@@ -130,7 +130,7 @@ {char prefix[256 ]; -if (snprintf (prefix,sizeof (prefix),"%s:%d" , func, line) >=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 ] = {0 };char footer[1000 +1 ] = {0 }; -if (snprintf (header,sizeof (header),"-----BEGIN %s-----\n" , name) >=sizeof (header)) +if (!checked_snprintf (header,sizeof (header),"-----BEGIN %s-----\n" , name)) {return false; } -if (snprintf (footer,sizeof (footer),"-----END %s-----\n" , name) >=sizeof (footer)) +if (!checked_snprintf (footer,sizeof (footer),"-----END %s-----\n" , name)) {return false; }
@@ -283,11 +283,11 @@char header[1000 +1 ] = {0 };char footer[1000 +1 ] = {0 }; -if (snprintf (header,sizeof (header),"-----BEGIN %s-----" , name) >=sizeof (header)) +if (!checked_snprintf (header,sizeof (header),"-----BEGIN %s-----" , name)) {return false; } -if (snprintf (footer,sizeof (footer),"-----END %s-----" , name) >=sizeof (footer)) +if (!checked_snprintf (footer,sizeof (footer),"-----END %s-----" , name)) {return false; }
src/openvpn/dns.c +2 -4
@@ -485,13 +485,11 @@if (j <0 ) { -const int ret =snprintf (name,sizeof (name), format, i); - name_ok = (ret >0 && ret <sizeof (name)); + name_ok =checked_snprintf (name,sizeof (name), format, i); }else { -const int ret =snprintf (name,sizeof (name), format, i, j); - name_ok = (ret >0 && ret <sizeof (name)); + name_ok =checked_snprintf (name,sizeof (name), format, i, j); }if (!name_ok)
src/openvpn/env_set.c +1 -1
@@ -334,7 +334,7 @@strcpy (tmpname, name);while (NULL !=env_set_get (es, tmpname) && counter <1000 ) { -ASSERT (snprintf (tmpname, tmpname_len,"%s_%u" , name, counter) < tmpname_len); +ASSERT (checked_snprintf (tmpname, tmpname_len,"%s_%u" , name, counter)); counter++; }if (counter <1000 )
src/openvpn/platform.c +2 -3
@@ -550,9 +550,8 @@ { ++attempts; -const int ret =snprintf (fname,sizeof (fname), fname_fmt, max_prefix_len, prefix, - (unsigned long )get_random (), (unsigned long )get_random ()); -if (ret <0 || ret >=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 ;
src/openvpn/proxy.c +4 -5
@@ -926,11 +926,10 @@ }/* send digest response */ -int sret =snprintf ( - buf,sizeof (buf), -"Proxy-Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", qop=%s, nc=%s, cnonce=\"%s\", response=\"%s\"%s" , - username, realm, nonce, uri, qop, nonce_count, cnonce, response, opaque_kv); -if (sret >=sizeof (buf)) +if (!checked_snprintf ( + buf,sizeof (buf), +"Proxy-Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", qop=%s, nc=%s, cnonce=\"%s\", response=\"%s\"%s" , + username, realm, nonce, uri, qop, nonce_count, cnonce, response, opaque_kv)) { goto error; }
src/openvpn/socks.c +3 -4
@@ -119,10 +119,9 @@ goto cleanup; } -int sret =snprintf (to_send,sizeof (to_send),"\x01%c%s%c%s" , (int )strlen (creds.username), - creds.username, (int )strlen (creds.password), creds.password); -ASSERT (sret >=0 && sret <=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;
src/openvpn/ssl_ncp.c +1 -1
@@ -198,7 +198,7 @@size_t newlen =strlen (o->ncp_ciphers) +1 +strlen (ciphername) +1 ;char *ncp_ciphers =gc_malloc (newlen, false, &o->gc); -ASSERT (snprintf (ncp_ciphers, newlen,"%s:%s" , o->ncp_ciphers, ciphername) < newlen); +ASSERT (checked_snprintf (ncp_ciphers, newlen,"%s:%s" , o->ncp_ciphers, ciphername)); o->ncp_ciphers = ncp_ciphers; }
src/openvpn/ssl_verify.c +1 -1
@@ -548,7 +548,7 @@ goto cleanup; } -if (snprintf (fn,sizeof (fn),"%s%c%s" , crl_dir,PATH_SEPARATOR , serial) >=sizeof (fn)) +if (!checked_snprintf (fn,sizeof (fn),"%s%c%s" , crl_dir,PATH_SEPARATOR , serial)) {msg (D_HANDSHAKE ,"VERIFY CRL: filename overflow" ); goto cleanup;
src/openvpn/ssl_verify_mbedtls.c +1 -3
@@ -93,9 +93,7 @@ ret =mbedtls_x509_crt_verify_info (errstr,sizeof (errstr) -1 ,"" , *flags);if (ret <=0 - &&snprintf (errstr,sizeof (errstr),"Could not retrieve error string, flags=%" PRIx32, - *flags) - >=sizeof (errstr)) + && !checked_snprintf (errstr,sizeof (errstr),"Could not retrieve error string, flags=%" PRIx32, *flags)) { errstr[0 ] ='\0' ; }
src/openvpn/tun.c +4 -7
@@ -3558,9 +3558,7 @@msg (M_FATAL ,"Error enumerating registry subkeys of key: %s" ,ADAPTER_KEY ); } -int ret =snprintf (unit_string,sizeof (unit_string),"%s\\%s" ,ADAPTER_KEY , enum_name); - -if (ret <0 || ret >=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 =snprintf (connection_string,sizeof (connection_string),"%s\\%s\\Connection" , -NETWORK_CONNECTIONS_KEY , enum_name); - -if (ret <0 || ret >=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" , enum_name);continue ;
src/openvpn/win32.c +2 -3
@@ -881,9 +881,8 @@char force_path[256 ];char *sysroot =get_win_sys_path (); -if (snprintf (force_path,sizeof (force_path),"PATH=%s\\System32;%s;%s\\System32\\Wbem" , - sysroot, sysroot, sysroot) - >=sizeof (force_path)) +if (!checked_snprintf (force_path,sizeof (force_path),"PATH=%s\\System32;%s;%s\\System32\\Wbem" , + sysroot, sysroot, sysroot)) {msg (M_WARN ,"env_block: default path truncated to %s" , force_path); }
tests/unit_tests/openvpn/test_buffer.c +11 -0
@@ -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=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia3477b8ee7a637c15aad7f285144280595cda5d5
Gerrit-Change-Number: 1489
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Attention is currently required from: plaisthos.
Hello plaisthos,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/1489?usp=email
to look at the new patch set (#3).
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.
---
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/3
src/openvpn/buffer.c +11 -0
@@ -1125,6 +1125,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 =vsnprintf (str, size, format, arglist); +va_end (arglist); +return (len >=0 && len < (ssize_t )size); +} +#ifdef VERIFY_ALIGNMENT void valign4 (const struct buffer *buf,const char *file,const int line)
src/openvpn/buffer.h +23 -0
@@ -971,6 +971,29 @@return 0 ==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
src/openvpn/crypto_mbedtls.c +5 -5
@@ -988,7 +988,7 @@ {char prefix[256 ]; -if (snprintf (prefix,sizeof (prefix),"%s:%d" , func, line) >=sizeof (prefix)) +if (!checked_snprintf (prefix,sizeof (prefix),"%s:%d" , func, line)) {return mbed_log_err (flags, errval, func); }
@@ -1078,11 +1078,11 @@char header[1000 +1 ] = {0 };char footer[1000 +1 ] = {0 }; -if (snprintf (header,sizeof (header),"-----BEGIN %s-----\n" , name) >=sizeof (header)) +if (!checked_snprintf (header,sizeof (header),"-----BEGIN %s-----\n" , name)) {return false; } -if (snprintf (footer,sizeof (footer),"-----END %s-----\n" , name) >=sizeof (footer)) +if (!checked_snprintf (footer,sizeof (footer),"-----END %s-----\n" , name)) {return false; }
@@ -1116,11 +1116,11 @@char header[1000 +1 ] = {0 };char footer[1000 +1 ] = {0 }; -if (snprintf (header,sizeof (header),"-----BEGIN %s-----" , name) >=sizeof (header)) +if (!checked_snprintf (header,sizeof (header),"-----BEGIN %s-----" , name)) {return false; } -if (snprintf (footer,sizeof (footer),"-----END %s-----" , name) >=sizeof (footer)) +if (!checked_snprintf (footer,sizeof (footer),"-----END %s-----" , name)) {return false; }
src/openvpn/crypto_mbedtls_legacy.c +5 -5
@@ -130,7 +130,7 @@ {char prefix[256 ]; -if (snprintf (prefix,sizeof (prefix),"%s:%d" , func, line) >=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 ] = {0 };char footer[1000 +1 ] = {0 }; -if (snprintf (header,sizeof (header),"-----BEGIN %s-----\n" , name) >=sizeof (header)) +if (!checked_snprintf (header,sizeof (header),"-----BEGIN %s-----\n" , name)) {return false; } -if (snprintf (footer,sizeof (footer),"-----END %s-----\n" , name) >=sizeof (footer)) +if (!checked_snprintf (footer,sizeof (footer),"-----END %s-----\n" , name)) {return false; }
@@ -283,11 +283,11 @@char header[1000 +1 ] = {0 };char footer[1000 +1 ] = {0 }; -if (snprintf (header,sizeof (header),"-----BEGIN %s-----" , name) >=sizeof (header)) +if (!checked_snprintf (header,sizeof (header),"-----BEGIN %s-----" , name)) {return false; } -if (snprintf (footer,sizeof (footer),"-----END %s-----" , name) >=sizeof (footer)) +if (!checked_snprintf (footer,sizeof (footer),"-----END %s-----" , name)) {return false; }
src/openvpn/dns.c +2 -4
@@ -485,13 +485,11 @@if (j <0 ) { -const int ret =snprintf (name,sizeof (name), format, i); - name_ok = (ret >0 && ret <sizeof (name)); + name_ok =checked_snprintf (name,sizeof (name), format, i); }else { -const int ret =snprintf (name,sizeof (name), format, i, j); - name_ok = (ret >0 && ret <sizeof (name)); + name_ok =checked_snprintf (name,sizeof (name), format, i, j); }if (!name_ok)
src/openvpn/env_set.c +1 -1
@@ -334,7 +334,7 @@strcpy (tmpname, name);while (NULL !=env_set_get (es, tmpname) && counter <1000 ) { -ASSERT (snprintf (tmpname, tmpname_len,"%s_%u" , name, counter) < tmpname_len); +ASSERT (checked_snprintf (tmpname, tmpname_len,"%s_%u" , name, counter)); counter++; }if (counter <1000 )
src/openvpn/platform.c +2 -3
@@ -550,9 +550,8 @@ { ++attempts; -const int ret =snprintf (fname,sizeof (fname), fname_fmt, max_prefix_len, prefix, - (unsigned long )get_random (), (unsigned long )get_random ()); -if (ret <0 || ret >=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 ;
src/openvpn/proxy.c +4 -5
@@ -926,11 +926,10 @@ }/* send digest response */ -int sret =snprintf ( - buf,sizeof (buf), -"Proxy-Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", qop=%s, nc=%s, cnonce=\"%s\", response=\"%s\"%s" , - username, realm, nonce, uri, qop, nonce_count, cnonce, response, opaque_kv); -if (sret >=sizeof (buf)) +if (!checked_snprintf ( + buf,sizeof (buf), +"Proxy-Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", qop=%s, nc=%s, cnonce=\"%s\", response=\"%s\"%s" , + username, realm, nonce, uri, qop, nonce_count, cnonce, response, opaque_kv)) { goto error; }
src/openvpn/socks.c +3 -4
@@ -119,10 +119,9 @@ goto cleanup; } -int sret =snprintf (to_send,sizeof (to_send),"\x01%c%s%c%s" , (int )strlen (creds.username), - creds.username, (int )strlen (creds.password), creds.password); -ASSERT (sret >=0 && sret <=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;
src/openvpn/ssl_ncp.c +1 -1
@@ -198,7 +198,7 @@size_t newlen =strlen (o->ncp_ciphers) +1 +strlen (ciphername) +1 ;char *ncp_ciphers =gc_malloc (newlen, false, &o->gc); -ASSERT (snprintf (ncp_ciphers, newlen,"%s:%s" , o->ncp_ciphers, ciphername) < newlen); +ASSERT (checked_snprintf (ncp_ciphers, newlen,"%s:%s" , o->ncp_ciphers, ciphername)); o->ncp_ciphers = ncp_ciphers; }
src/openvpn/ssl_verify.c +1 -1
@@ -548,7 +548,7 @@ goto cleanup; } -if (snprintf (fn,sizeof (fn),"%s%c%s" , crl_dir,PATH_SEPARATOR , serial) >=sizeof (fn)) +if (!checked_snprintf (fn,sizeof (fn),"%s%c%s" , crl_dir,PATH_SEPARATOR , serial)) {msg (D_HANDSHAKE ,"VERIFY CRL: filename overflow" ); goto cleanup;
src/openvpn/ssl_verify_mbedtls.c +1 -3
@@ -93,9 +93,7 @@ ret =mbedtls_x509_crt_verify_info (errstr,sizeof (errstr) -1 ,"" , *flags);if (ret <=0 - &&snprintf (errstr,sizeof (errstr),"Could not retrieve error string, flags=%" PRIx32, - *flags) - >=sizeof (errstr)) + && !checked_snprintf (errstr,sizeof (errstr),"Could not retrieve error string, flags=%" PRIx32, *flags)) { errstr[0 ] ='\0' ; }
src/openvpn/tun.c +4 -7
@@ -3558,9 +3558,7 @@msg (M_FATAL ,"Error enumerating registry subkeys of key: %s" ,ADAPTER_KEY ); } -int ret =snprintf (unit_string,sizeof (unit_string),"%s\\%s" ,ADAPTER_KEY , enum_name); - -if (ret <0 || ret >=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 =snprintf (connection_string,sizeof (connection_string),"%s\\%s\\Connection" , -NETWORK_CONNECTIONS_KEY , enum_name); - -if (ret <0 || ret >=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" , enum_name);continue ;
src/openvpn/win32.c +2 -3
@@ -881,9 +881,8 @@char force_path[256 ];char *sysroot =get_win_sys_path (); -if (snprintf (force_path,sizeof (force_path),"PATH=%s\\System32;%s;%s\\System32\\Wbem" , - sysroot, sysroot, sysroot) - >=sizeof (force_path)) +if (!checked_snprintf (force_path,sizeof (force_path),"PATH=%s\\System32;%s;%s\\System32\\Wbem" , + sysroot, sysroot, sysroot)) {msg (M_WARN ,"env_block: default path truncated to %s" , force_path); }
tests/unit_tests/openvpn/test_buffer.c +11 -0
@@ -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=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia3477b8ee7a637c15aad7f285144280595cda5d5
Gerrit-Change-Number: 1489
Gerrit-PatchSet: 3
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Attention is currently required from: flichtenheld, plaisthos.
cron2 has posted comments on this change by flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1489?usp=email )
Change subject: buffer: Add checked_snprintf function and use it in the code
......................................................................
Patch Set 6: Code-Review+2
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1489?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia3477b8ee7a637c15aad7f285144280595cda5d5
Gerrit-Change-Number: 1489
Gerrit-PatchSet: 6
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Wed, 04 Mar 2026 11:04:23 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
PATCH v6 1/1 buffer: Add checked_snprintf function and use it in the code
In reply to a message not in the archiveFrom: Frank Lichtenheld <[email protected]...>
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.
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1489
---
This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master.
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1489
This mail reflects revision 6 of this Change.
Acked-by according to Gerrit (reflected above):
Gert Doering <[email protected]...>
src/openvpn/buffer.c +11 -0
@@ -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 =vsnprintf (str, size, format, arglist); +va_end (arglist); +return (len >=0 && len < (ssize_t )size); +} +#ifdef VERIFY_ALIGNMENT void valign4 (const struct buffer *buf,const char *file,const int line)
src/openvpn/buffer.h +23 -0
@@ -971,6 +971,29 @@return 0 ==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
src/openvpn/crypto_mbedtls.c +5 -5
@@ -1014,7 +1014,7 @@ {char prefix[256 ]; -if (snprintf (prefix,sizeof (prefix),"%s:%d" , func, line) >=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 ] = {0 };char footer[1000 +1 ] = {0 }; -if (snprintf (header,sizeof (header),"-----BEGIN %s-----\n" , name) >=sizeof (header)) +if (!checked_snprintf (header,sizeof (header),"-----BEGIN %s-----\n" , name)) {return false; } -if (snprintf (footer,sizeof (footer),"-----END %s-----\n" , name) >=sizeof (footer)) +if (!checked_snprintf (footer,sizeof (footer),"-----END %s-----\n" , name)) {return false; }
@@ -1142,11 +1142,11 @@char header[1000 +1 ] = {0 };char footer[1000 +1 ] = {0 }; -if (snprintf (header,sizeof (header),"-----BEGIN %s-----" , name) >=sizeof (header)) +if (!checked_snprintf (header,sizeof (header),"-----BEGIN %s-----" , name)) {return false; } -if (snprintf (footer,sizeof (footer),"-----END %s-----" , name) >=sizeof (footer)) +if (!checked_snprintf (footer,sizeof (footer),"-----END %s-----" , name)) {return false; }
src/openvpn/crypto_mbedtls_legacy.c +5 -5
@@ -130,7 +130,7 @@ {char prefix[256 ]; -if (snprintf (prefix,sizeof (prefix),"%s:%d" , func, line) >=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 ] = {0 };char footer[1000 +1 ] = {0 }; -if (snprintf (header,sizeof (header),"-----BEGIN %s-----\n" , name) >=sizeof (header)) +if (!checked_snprintf (header,sizeof (header),"-----BEGIN %s-----\n" , name)) {return false; } -if (snprintf (footer,sizeof (footer),"-----END %s-----\n" , name) >=sizeof (footer)) +if (!checked_snprintf (footer,sizeof (footer),"-----END %s-----\n" , name)) {return false; }
@@ -283,11 +283,11 @@char header[1000 +1 ] = {0 };char footer[1000 +1 ] = {0 }; -if (snprintf (header,sizeof (header),"-----BEGIN %s-----" , name) >=sizeof (header)) +if (!checked_snprintf (header,sizeof (header),"-----BEGIN %s-----" , name)) {return false; } -if (snprintf (footer,sizeof (footer),"-----END %s-----" , name) >=sizeof (footer)) +if (!checked_snprintf (footer,sizeof (footer),"-----END %s-----" , name)) {return false; }
src/openvpn/dns.c +2 -4
@@ -485,13 +485,11 @@if (j <0 ) { -const int ret =snprintf (name,sizeof (name), format, i); - name_ok = (ret >0 && ret <sizeof (name)); + name_ok =checked_snprintf (name,sizeof (name), format, i); }else { -const int ret =snprintf (name,sizeof (name), format, i, j); - name_ok = (ret >0 && ret <sizeof (name)); + name_ok =checked_snprintf (name,sizeof (name), format, i, j); }if (!name_ok)
src/openvpn/env_set.c +1 -1
@@ -334,7 +334,7 @@strcpy (tmpname, name);while (NULL !=env_set_get (es, tmpname) && counter <1000 ) { -ASSERT (snprintf (tmpname, tmpname_len,"%s_%u" , name, counter) < tmpname_len); +ASSERT (checked_snprintf (tmpname, tmpname_len,"%s_%u" , name, counter)); counter++; }if (counter <1000 )
src/openvpn/platform.c +2 -3
@@ -550,9 +550,8 @@ { ++attempts; -const int ret =snprintf (fname,sizeof (fname), fname_fmt, max_prefix_len, prefix, - (unsigned long )get_random (), (unsigned long )get_random ()); -if (ret <0 || ret >=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 ;
src/openvpn/proxy.c +4 -5
@@ -771,11 +771,10 @@ }/* send digest response */ -int sret =snprintf ( - buf,sizeof (buf), -"Proxy-Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", qop=%s, nc=%s, cnonce=\"%s\", response=\"%s\"%s" , - username, realm, nonce, uri, qop, nonce_count, cnonce, response, opaque_kv); -if (sret >=sizeof (buf)) +if (!checked_snprintf ( + buf,sizeof (buf), +"Proxy-Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", qop=%s, nc=%s, cnonce=\"%s\", response=\"%s\"%s" , + username, realm, nonce, uri, qop, nonce_count, cnonce, response, opaque_kv)) { goto error; }
src/openvpn/socks.c +3 -4
@@ -119,10 +119,9 @@ goto cleanup; } -int sret =snprintf (to_send,sizeof (to_send),"\x01%c%s%c%s" , (int )strlen (creds.username), - creds.username, (int )strlen (creds.password), creds.password); -ASSERT (sret >=0 && sret <=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;
src/openvpn/ssl_ncp.c +1 -1
@@ -198,7 +198,7 @@size_t newlen =strlen (o->ncp_ciphers) +1 +strlen (ciphername) +1 ;char *ncp_ciphers =gc_malloc (newlen, false, &o->gc); -ASSERT (snprintf (ncp_ciphers, newlen,"%s:%s" , o->ncp_ciphers, ciphername) < newlen); +ASSERT (checked_snprintf (ncp_ciphers, newlen,"%s:%s" , o->ncp_ciphers, ciphername)); o->ncp_ciphers = ncp_ciphers; }
src/openvpn/ssl_verify.c +1 -1
@@ -548,7 +548,7 @@ goto cleanup; } -if (snprintf (fn,sizeof (fn),"%s%c%s" , crl_dir,PATH_SEPARATOR , serial) >=sizeof (fn)) +if (!checked_snprintf (fn,sizeof (fn),"%s%c%s" , crl_dir,PATH_SEPARATOR , serial)) {msg (D_HANDSHAKE ,"VERIFY CRL: filename overflow" ); goto cleanup;
src/openvpn/ssl_verify_mbedtls.c +1 -3
@@ -93,9 +93,7 @@ ret =mbedtls_x509_crt_verify_info (errstr,sizeof (errstr) -1 ,"" , *flags);if (ret <=0 - &&snprintf (errstr,sizeof (errstr),"Could not retrieve error string, flags=%" PRIx32, - *flags) - >=sizeof (errstr)) + && !checked_snprintf (errstr,sizeof (errstr),"Could not retrieve error string, flags=%" PRIx32, *flags)) { errstr[0 ] ='\0' ; }
src/openvpn/tun.c +4 -7
@@ -3558,9 +3558,7 @@msg (M_FATAL ,"Error enumerating registry subkeys of key: %s" ,ADAPTER_KEY ); } -int ret =snprintf (unit_string,sizeof (unit_string),"%s\\%s" ,ADAPTER_KEY , enum_name); - -if (ret <0 || ret >=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 =snprintf (connection_string,sizeof (connection_string),"%s\\%s\\Connection" , -NETWORK_CONNECTIONS_KEY , enum_name); - -if (ret <0 || ret >=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" , enum_name);continue ;
src/openvpn/win32.c +2 -3
@@ -881,9 +881,8 @@char force_path[256 ];char *sysroot =get_win_sys_path (); -if (snprintf (force_path,sizeof (force_path),"PATH=%s\\System32;%s;%s\\System32\\Wbem" , - sysroot, sysroot, sysroot) - >=sizeof (force_path)) +if (!checked_snprintf (force_path,sizeof (force_path),"PATH=%s\\System32;%s;%s\\System32\\Wbem" , + sysroot, sysroot, sysroot)) {msg (M_WARN ,"env_block: default path truncated to %s" , force_path); }
tests/unit_tests/openvpn/test_buffer.c +11 -0
@@ -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) };
[Openvpn-devel] [PATCH applied] Re: buffer: Add checked_snprintf function and use it in the code
Thanks for this undertaking :-) - I admit that I was not too happy to have "the snprintf() wrapper come back", but on really looking into the patch, I like it a lot. All the redundant code in the snprintf() calls where we actually check the result gets much simpler this way - and also integer-correct without casts all over the place.
Given that this changes lots of places (= future conflicts), brings no actual functional difference, *and* makes the code easier to read and understand I took the liberty to apply it to 2.7 as well. Both branches have seen a full t_client/t_server test run "just to be sure".
Your patch has been applied to the master and release/2.7 branch.
commit 1ec71fe0c0cd3f593036197abdc9486ce3665c38 (master) commit c0e792112a7dfeff26039a03c0799b89cb86e071 (release/2.7)
Author: Frank Lichtenheld Date: Wed Mar 4 12:04:49 2026 +0100
buffer: Add checked_snprintf function and use it in the code
Signed-off-by: Frank Lichtenheld <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1489
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg35872.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
cron2 has uploaded a new patch set (#7) to the change originally created by flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1489?usp=email )
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.
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1489
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg35872.html
---
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
src/openvpn/buffer.c +11 -0
@@ -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 =vsnprintf (str, size, format, arglist); +va_end (arglist); +return (len >=0 && len < (ssize_t )size); +} +#ifdef VERIFY_ALIGNMENT void valign4 (const struct buffer *buf,const char *file,const int line)
src/openvpn/buffer.h +23 -0
@@ -972,6 +972,29 @@return 0 ==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
src/openvpn/crypto_mbedtls.c +5 -5
@@ -1014,7 +1014,7 @@ {char prefix[256 ]; -if (snprintf (prefix,sizeof (prefix),"%s:%d" , func, line) >=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 ] = {0 };char footer[1000 +1 ] = {0 }; -if (snprintf (header,sizeof (header),"-----BEGIN %s-----\n" , name) >=sizeof (header)) +if (!checked_snprintf (header,sizeof (header),"-----BEGIN %s-----\n" , name)) {return false; } -if (snprintf (footer,sizeof (footer),"-----END %s-----\n" , name) >=sizeof (footer)) +if (!checked_snprintf (footer,sizeof (footer),"-----END %s-----\n" , name)) {return false; }
@@ -1142,11 +1142,11 @@char header[1000 +1 ] = {0 };char footer[1000 +1 ] = {0 }; -if (snprintf (header,sizeof (header),"-----BEGIN %s-----" , name) >=sizeof (header)) +if (!checked_snprintf (header,sizeof (header),"-----BEGIN %s-----" , name)) {return false; } -if (snprintf (footer,sizeof (footer),"-----END %s-----" , name) >=sizeof (footer)) +if (!checked_snprintf (footer,sizeof (footer),"-----END %s-----" , name)) {return false; }
src/openvpn/crypto_mbedtls_legacy.c +5 -5
@@ -130,7 +130,7 @@ {char prefix[256 ]; -if (snprintf (prefix,sizeof (prefix),"%s:%d" , func, line) >=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 ] = {0 };char footer[1000 +1 ] = {0 }; -if (snprintf (header,sizeof (header),"-----BEGIN %s-----\n" , name) >=sizeof (header)) +if (!checked_snprintf (header,sizeof (header),"-----BEGIN %s-----\n" , name)) {return false; } -if (snprintf (footer,sizeof (footer),"-----END %s-----\n" , name) >=sizeof (footer)) +if (!checked_snprintf (footer,sizeof (footer),"-----END %s-----\n" , name)) {return false; }
@@ -283,11 +283,11 @@char header[1000 +1 ] = {0 };char footer[1000 +1 ] = {0 }; -if (snprintf (header,sizeof (header),"-----BEGIN %s-----" , name) >=sizeof (header)) +if (!checked_snprintf (header,sizeof (header),"-----BEGIN %s-----" , name)) {return false; } -if (snprintf (footer,sizeof (footer),"-----END %s-----" , name) >=sizeof (footer)) +if (!checked_snprintf (footer,sizeof (footer),"-----END %s-----" , name)) {return false; }
src/openvpn/dns.c +2 -4
@@ -485,13 +485,11 @@if (j <0 ) { -const int ret =snprintf (name,sizeof (name), format, i); - name_ok = (ret >0 && ret <sizeof (name)); + name_ok =checked_snprintf (name,sizeof (name), format, i); }else { -const int ret =snprintf (name,sizeof (name), format, i, j); - name_ok = (ret >0 && ret <sizeof (name)); + name_ok =checked_snprintf (name,sizeof (name), format, i, j); }if (!name_ok)
src/openvpn/env_set.c +1 -1
@@ -334,7 +334,7 @@strcpy (tmpname, name);while (NULL !=env_set_get (es, tmpname) && counter <1000 ) { -ASSERT (snprintf (tmpname, tmpname_len,"%s_%u" , name, counter) < tmpname_len); +ASSERT (checked_snprintf (tmpname, tmpname_len,"%s_%u" , name, counter)); counter++; }if (counter <1000 )
src/openvpn/platform.c +2 -3
@@ -550,9 +550,8 @@ { ++attempts; -const int ret =snprintf (fname,sizeof (fname), fname_fmt, max_prefix_len, prefix, - (unsigned long )get_random (), (unsigned long )get_random ()); -if (ret <0 || ret >=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 ;
src/openvpn/proxy.c +4 -5
@@ -771,11 +771,10 @@ }/* send digest response */ -int sret =snprintf ( - buf,sizeof (buf), -"Proxy-Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", qop=%s, nc=%s, cnonce=\"%s\", response=\"%s\"%s" , - username, realm, nonce, uri, qop, nonce_count, cnonce, response, opaque_kv); -if (sret >=sizeof (buf)) +if (!checked_snprintf ( + buf,sizeof (buf), +"Proxy-Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", qop=%s, nc=%s, cnonce=\"%s\", response=\"%s\"%s" , + username, realm, nonce, uri, qop, nonce_count, cnonce, response, opaque_kv)) { goto error; }
src/openvpn/socks.c +3 -4
@@ -119,10 +119,9 @@ goto cleanup; } -int sret =snprintf (to_send,sizeof (to_send),"\x01%c%s%c%s" , (int )strlen (creds.username), - creds.username, (int )strlen (creds.password), creds.password); -ASSERT (sret >=0 && sret <=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;
src/openvpn/ssl_ncp.c +1 -1
@@ -198,7 +198,7 @@size_t newlen =strlen (o->ncp_ciphers) +1 +strlen (ciphername) +1 ;char *ncp_ciphers =gc_malloc (newlen, false, &o->gc); -ASSERT (snprintf (ncp_ciphers, newlen,"%s:%s" , o->ncp_ciphers, ciphername) < newlen); +ASSERT (checked_snprintf (ncp_ciphers, newlen,"%s:%s" , o->ncp_ciphers, ciphername)); o->ncp_ciphers = ncp_ciphers; }
src/openvpn/ssl_verify.c +1 -1
@@ -548,7 +548,7 @@ goto cleanup; } -if (snprintf (fn,sizeof (fn),"%s%c%s" , crl_dir,PATH_SEPARATOR , serial) >=sizeof (fn)) +if (!checked_snprintf (fn,sizeof (fn),"%s%c%s" , crl_dir,PATH_SEPARATOR , serial)) {msg (D_HANDSHAKE ,"VERIFY CRL: filename overflow" ); goto cleanup;
src/openvpn/ssl_verify_mbedtls.c +1 -3
@@ -93,9 +93,7 @@ ret =mbedtls_x509_crt_verify_info (errstr,sizeof (errstr) -1 ,"" , *flags);if (ret <=0 - &&snprintf (errstr,sizeof (errstr),"Could not retrieve error string, flags=%" PRIx32, - *flags) - >=sizeof (errstr)) + && !checked_snprintf (errstr,sizeof (errstr),"Could not retrieve error string, flags=%" PRIx32, *flags)) { errstr[0 ] ='\0' ; }
src/openvpn/tun.c +4 -7
@@ -3558,9 +3558,7 @@msg (M_FATAL ,"Error enumerating registry subkeys of key: %s" ,ADAPTER_KEY ); } -int ret =snprintf (unit_string,sizeof (unit_string),"%s\\%s" ,ADAPTER_KEY , enum_name); - -if (ret <0 || ret >=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 =snprintf (connection_string,sizeof (connection_string),"%s\\%s\\Connection" , -NETWORK_CONNECTIONS_KEY , enum_name); - -if (ret <0 || ret >=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" , enum_name);continue ;
src/openvpn/win32.c +2 -3
@@ -881,9 +881,8 @@char force_path[256 ];char *sysroot =get_win_sys_path (); -if (snprintf (force_path,sizeof (force_path),"PATH=%s\\System32;%s;%s\\System32\\Wbem" , - sysroot, sysroot, sysroot) - >=sizeof (force_path)) +if (!checked_snprintf (force_path,sizeof (force_path),"PATH=%s\\System32;%s;%s\\System32\\Wbem" , + sysroot, sysroot, sysroot)) {msg (M_WARN ,"env_block: default path truncated to %s" , force_path); }
tests/unit_tests/openvpn/test_buffer.c +11 -0
@@ -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=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia3477b8ee7a637c15aad7f285144280595cda5d5
Gerrit-Change-Number: 1489
Gerrit-PatchSet: 7
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/1489?usp=email )
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.
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1489
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg35872.html
---
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(-)
src/openvpn/buffer.c +11 -0
@@ -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 =vsnprintf (str, size, format, arglist); +va_end (arglist); +return (len >=0 && len < (ssize_t )size); +} +#ifdef VERIFY_ALIGNMENT void valign4 (const struct buffer *buf,const char *file,const int line)
src/openvpn/buffer.h +23 -0
@@ -972,6 +972,29 @@return 0 ==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
src/openvpn/crypto_mbedtls.c +5 -5
@@ -1014,7 +1014,7 @@ {char prefix[256 ]; -if (snprintf (prefix,sizeof (prefix),"%s:%d" , func, line) >=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 ] = {0 };char footer[1000 +1 ] = {0 }; -if (snprintf (header,sizeof (header),"-----BEGIN %s-----\n" , name) >=sizeof (header)) +if (!checked_snprintf (header,sizeof (header),"-----BEGIN %s-----\n" , name)) {return false; } -if (snprintf (footer,sizeof (footer),"-----END %s-----\n" , name) >=sizeof (footer)) +if (!checked_snprintf (footer,sizeof (footer),"-----END %s-----\n" , name)) {return false; }
@@ -1142,11 +1142,11 @@char header[1000 +1 ] = {0 };char footer[1000 +1 ] = {0 }; -if (snprintf (header,sizeof (header),"-----BEGIN %s-----" , name) >=sizeof (header)) +if (!checked_snprintf (header,sizeof (header),"-----BEGIN %s-----" , name)) {return false; } -if (snprintf (footer,sizeof (footer),"-----END %s-----" , name) >=sizeof (footer)) +if (!checked_snprintf (footer,sizeof (footer),"-----END %s-----" , name)) {return false; }
src/openvpn/crypto_mbedtls_legacy.c +5 -5
@@ -130,7 +130,7 @@ {char prefix[256 ]; -if (snprintf (prefix,sizeof (prefix),"%s:%d" , func, line) >=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 ] = {0 };char footer[1000 +1 ] = {0 }; -if (snprintf (header,sizeof (header),"-----BEGIN %s-----\n" , name) >=sizeof (header)) +if (!checked_snprintf (header,sizeof (header),"-----BEGIN %s-----\n" , name)) {return false; } -if (snprintf (footer,sizeof (footer),"-----END %s-----\n" , name) >=sizeof (footer)) +if (!checked_snprintf (footer,sizeof (footer),"-----END %s-----\n" , name)) {return false; }
@@ -283,11 +283,11 @@char header[1000 +1 ] = {0 };char footer[1000 +1 ] = {0 }; -if (snprintf (header,sizeof (header),"-----BEGIN %s-----" , name) >=sizeof (header)) +if (!checked_snprintf (header,sizeof (header),"-----BEGIN %s-----" , name)) {return false; } -if (snprintf (footer,sizeof (footer),"-----END %s-----" , name) >=sizeof (footer)) +if (!checked_snprintf (footer,sizeof (footer),"-----END %s-----" , name)) {return false; }
src/openvpn/dns.c +2 -4
@@ -485,13 +485,11 @@if (j <0 ) { -const int ret =snprintf (name,sizeof (name), format, i); - name_ok = (ret >0 && ret <sizeof (name)); + name_ok =checked_snprintf (name,sizeof (name), format, i); }else { -const int ret =snprintf (name,sizeof (name), format, i, j); - name_ok = (ret >0 && ret <sizeof (name)); + name_ok =checked_snprintf (name,sizeof (name), format, i, j); }if (!name_ok)
src/openvpn/env_set.c +1 -1
@@ -334,7 +334,7 @@strcpy (tmpname, name);while (NULL !=env_set_get (es, tmpname) && counter <1000 ) { -ASSERT (snprintf (tmpname, tmpname_len,"%s_%u" , name, counter) < tmpname_len); +ASSERT (checked_snprintf (tmpname, tmpname_len,"%s_%u" , name, counter)); counter++; }if (counter <1000 )
src/openvpn/platform.c +2 -3
@@ -550,9 +550,8 @@ { ++attempts; -const int ret =snprintf (fname,sizeof (fname), fname_fmt, max_prefix_len, prefix, - (unsigned long )get_random (), (unsigned long )get_random ()); -if (ret <0 || ret >=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 ;
src/openvpn/proxy.c +4 -5
@@ -771,11 +771,10 @@ }/* send digest response */ -int sret =snprintf ( - buf,sizeof (buf), -"Proxy-Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", qop=%s, nc=%s, cnonce=\"%s\", response=\"%s\"%s" , - username, realm, nonce, uri, qop, nonce_count, cnonce, response, opaque_kv); -if (sret >=sizeof (buf)) +if (!checked_snprintf ( + buf,sizeof (buf), +"Proxy-Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", qop=%s, nc=%s, cnonce=\"%s\", response=\"%s\"%s" , + username, realm, nonce, uri, qop, nonce_count, cnonce, response, opaque_kv)) { goto error; }
src/openvpn/socks.c +3 -4
@@ -119,10 +119,9 @@ goto cleanup; } -int sret =snprintf (to_send,sizeof (to_send),"\x01%c%s%c%s" , (int )strlen (creds.username), - creds.username, (int )strlen (creds.password), creds.password); -ASSERT (sret >=0 && sret <=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;
src/openvpn/ssl_ncp.c +1 -1
@@ -198,7 +198,7 @@size_t newlen =strlen (o->ncp_ciphers) +1 +strlen (ciphername) +1 ;char *ncp_ciphers =gc_malloc (newlen, false, &o->gc); -ASSERT (snprintf (ncp_ciphers, newlen,"%s:%s" , o->ncp_ciphers, ciphername) < newlen); +ASSERT (checked_snprintf (ncp_ciphers, newlen,"%s:%s" , o->ncp_ciphers, ciphername)); o->ncp_ciphers = ncp_ciphers; }
src/openvpn/ssl_verify.c +1 -1
@@ -548,7 +548,7 @@ goto cleanup; } -if (snprintf (fn,sizeof (fn),"%s%c%s" , crl_dir,PATH_SEPARATOR , serial) >=sizeof (fn)) +if (!checked_snprintf (fn,sizeof (fn),"%s%c%s" , crl_dir,PATH_SEPARATOR , serial)) {msg (D_HANDSHAKE ,"VERIFY CRL: filename overflow" ); goto cleanup;
src/openvpn/ssl_verify_mbedtls.c +1 -3
@@ -93,9 +93,7 @@ ret =mbedtls_x509_crt_verify_info (errstr,sizeof (errstr) -1 ,"" , *flags);if (ret <=0 - &&snprintf (errstr,sizeof (errstr),"Could not retrieve error string, flags=%" PRIx32, - *flags) - >=sizeof (errstr)) + && !checked_snprintf (errstr,sizeof (errstr),"Could not retrieve error string, flags=%" PRIx32, *flags)) { errstr[0 ] ='\0' ; }
src/openvpn/tun.c +4 -7
@@ -3558,9 +3558,7 @@msg (M_FATAL ,"Error enumerating registry subkeys of key: %s" ,ADAPTER_KEY ); } -int ret =snprintf (unit_string,sizeof (unit_string),"%s\\%s" ,ADAPTER_KEY , enum_name); - -if (ret <0 || ret >=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 =snprintf (connection_string,sizeof (connection_string),"%s\\%s\\Connection" , -NETWORK_CONNECTIONS_KEY , enum_name); - -if (ret <0 || ret >=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" , enum_name);continue ;
src/openvpn/win32.c +2 -3
@@ -881,9 +881,8 @@char force_path[256 ];char *sysroot =get_win_sys_path (); -if (snprintf (force_path,sizeof (force_path),"PATH=%s\\System32;%s;%s\\System32\\Wbem" , - sysroot, sysroot, sysroot) - >=sizeof (force_path)) +if (!checked_snprintf (force_path,sizeof (force_path),"PATH=%s\\System32;%s;%s\\System32\\Wbem" , + sysroot, sysroot, sysroot)) {msg (M_WARN ,"env_block: default path truncated to %s" , force_path); }
tests/unit_tests/openvpn/test_buffer.c +11 -0
@@ -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=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia3477b8ee7a637c15aad7f285144280595cda5d5
Gerrit-Change-Number: 1489
Gerrit-PatchSet: 7
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>