[v2,3/4] examples/mp_server: fix snprintf overflow
Checks
Commit Message
When producing a printable mac address the buffer was appropriately sized
for holding the mac address exactly, but the actual snprintf included a
'\n' character at the end, which means that the snprintf technically is
getting truncated i.e. the \n would not be added due to lack of space.
This gets flagged as a problem by modern versions of gcc, e.g. on Ubuntu
20.04.
main.c:77:37: warning: ‘__builtin___snprintf_chk’ output truncated before the last format character [-Wformat-truncation=]
77 | "%02x:%02x:%02x:%02x:%02x:%02x\n",
| ^
Since the \n is getting stripped anyway, we can fix the issue by just
removing it. In the process we can switch to using the standard ethernet
address formating function from rte_ether.h.
Fixes: af75078fece3 ("first public release")
Cc: stable@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>
Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
V2: switched code to use standard formatting function
---
.../client_server_mp/mp_server/main.c | 17 ++++++++++-------
1 file changed, 10 insertions(+), 7 deletions(-)
Comments
On 8/21/2020 6:10 PM, Bruce Richardson wrote:
> When producing a printable mac address the buffer was appropriately sized
> for holding the mac address exactly, but the actual snprintf included a
> '\n' character at the end, which means that the snprintf technically is
> getting truncated i.e. the \n would not be added due to lack of space.
> This gets flagged as a problem by modern versions of gcc, e.g. on Ubuntu
> 20.04.
>
> main.c:77:37: warning: ‘__builtin___snprintf_chk’ output truncated before the last format character [-Wformat-truncation=]
> 77 | "%02x:%02x:%02x:%02x:%02x:%02x\n",
> | ^
>
> Since the \n is getting stripped anyway, we can fix the issue by just
> removing it. In the process we can switch to using the standard ethernet
> address formating function from rte_ether.h.
>
> Fixes: af75078fece3 ("first public release")
> Cc: stable@dpdk.org
> Cc: Stephen Hemminger <stephen@networkplumber.org>
>
> Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
>
> ---
> V2: switched code to use standard formatting function
> ---
Acked-by: Radu Nicolau <radu.nicolau@intel.com>
@@ -59,12 +59,17 @@ static struct client_rx_buf *cl_rx_buf;
static const char *
get_printable_mac_addr(uint16_t port)
{
- static const char err_address[] = "00:00:00:00:00:00";
- static char addresses[RTE_MAX_ETHPORTS][sizeof(err_address)];
+ static const struct rte_ether_addr null_mac; /* static defaults to 0 */
+ static char err_address[32];
+ static char addresses[RTE_MAX_ETHPORTS][32];
int ret;
- if (unlikely(port >= RTE_MAX_ETHPORTS))
+ if (unlikely(port >= RTE_MAX_ETHPORTS)) {
+ if (err_address[0] == '\0')
+ rte_ether_format_addr(err_address,
+ sizeof(err_address), &null_mac);
return err_address;
+ }
if (unlikely(addresses[port][0]=='\0')){
struct rte_ether_addr mac;
ret = rte_eth_macaddr_get(port, &mac);
@@ -73,10 +78,8 @@ get_printable_mac_addr(uint16_t port)
port, rte_strerror(-ret));
return err_address;
}
- snprintf(addresses[port], sizeof(addresses[port]),
- "%02x:%02x:%02x:%02x:%02x:%02x\n",
- mac.addr_bytes[0], mac.addr_bytes[1], mac.addr_bytes[2],
- mac.addr_bytes[3], mac.addr_bytes[4], mac.addr_bytes[5]);
+ rte_ether_format_addr(addresses[port],
+ sizeof(addresses[port]), &mac);
}
return addresses[port];
}