Skip to contentSkip to conversation

net/af_xdp: use snprintf instead of strncpy

PATCH Superseded Ciara Loftus 5 participants 10 messages Oct 7, 2020 – Oct 9, 2020 Network drivers

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/af_xdp: use snprintf instead of strncpy

strncpy may leave the destination buffer not NULL terminated so use snprintf instead.

Coverity issue: 362975

Fixes 339b88c6a91f ("net/af_xdp: support multi-queue")
---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -1
index eaf2c9c873..52495cb8fb 100644
@@ -1362,7 +1362,7 @@ xdp_get_channels_info(const char *if_name, int *max_queues,
 
 	channels.cmd = ETHTOOL_GCHANNELS;
 	ifr.ifr_data = (void *)&channels;
-	strncpy(ifr.ifr_name, if_name, IFNAMSIZ);
+	snprintf(ifr.ifr_name, IFNAMSIZ, "%s", if_name);
 	ret = ioctl(fd, SIOCETHTOOL, &ifr);
 	if (ret) {
 		if (errno == EOPNOTSUPP) {
Signature · 1 line
2.17.1
#1 lore raw
Quoted 2 lines · Ciara Loftus

strncpy may leave the destination buffer not NULL terminated so use snprintf instead.

What do you think using 'strlcpy'?

Quoted 22 lines

Coverity issue: 362975

Fixes 339b88c6a91f ("net/af_xdp: support multi-queue")
---
  drivers/net/af_xdp/rte_eth_af_xdp.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -1
index eaf2c9c873..52495cb8fb 100644
@@ -1362,7 +1362,7 @@ xdp_get_channels_info(const char *if_name, int *max_queues,
 
  	channels.cmd = ETHTOOL_GCHANNELS;
  	ifr.ifr_data = (void *)&channels;
-	strncpy(ifr.ifr_name, if_name, IFNAMSIZ);
+	snprintf(ifr.ifr_name, IFNAMSIZ, "%s", if_name);
  	ret = ioctl(fd, SIOCETHTOOL, &ifr);
  	if (ret) {
  		if (errno == EOPNOTSUPP) {
#2 lore raw
Quoted 5 lines · Ferruh Yigit
Quoted 2 lines · Ciara Loftus

strncpy may leave the destination buffer not NULL terminated so use snprintf instead.

What do you think using 'strlcpy'?

Or even better, rte_strscpy()
https://git.dpdk.org/dpdk/commit/?id=b0236c7cf761

Quoted 23 lines
Quoted 22 lines

Coverity issue: 362975

Fixes 339b88c6a91f ("net/af_xdp: support multi-queue")
---
  drivers/net/af_xdp/rte_eth_af_xdp.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +1 -1
index eaf2c9c873..52495cb8fb 100644
@@ -1362,7 +1362,7 @@ xdp_get_channels_info(const char *if_name, int *max_queues,
  	channels.cmd = ETHTOOL_GCHANNELS;
  	ifr.ifr_data = (void *)&channels;
-	strncpy(ifr.ifr_name, if_name, IFNAMSIZ);
+	snprintf(ifr.ifr_name, IFNAMSIZ, "%s", if_name);
  	ret = ioctl(fd, SIOCETHTOOL, &ifr);
  	if (ret) {
  		if (errno == EOPNOTSUPP) {
 
#3 lore raw
Quoted 10 lines · Olivier Matz
Quoted 5 lines · Ferruh Yigit
Quoted 2 lines · Ciara Loftus

strncpy may leave the destination buffer not NULL terminated so use snprintf instead.

What do you think using 'strlcpy'?

Or even better, rte_strscpy()
https://git.dpdk.org/dpdk/commit/?id=b0236c7cf761

I think this is largely a matter of preference, and unless there is a good reason not to, I tend towards strlcpy as the older and more common (till now) interface. The main thing is just to use a function that will guarantee dest is null-terminated here, and both strlcpy and strscpy meet that criteria.

/Bruce

#4 lore raw
Quoted 8 lines · Bruce Richardson
Quoted 7 lines · Olivier Matz
Quoted 5 lines · Ferruh Yigit
Quoted 2 lines · Ciara Loftus

strncpy may leave the destination buffer not NULL terminated so use snprintf instead.

What do you think using 'strlcpy'?

Quoted 9 lines
Quoted 3 lines

Or even better, rte_strscpy()
https://git.dpdk.org/dpdk/commit/?id=b0236c7cf761

I think this is largely a matter of preference, and unless there is a good reason not to, I tend towards strlcpy as the older and more common (till now) interface. The main thing is just to use a function that will guarantee dest is null-terminated here, and both strlcpy and strscpy meet that criteria.

I'd also add that strlcpy is more likely to be recognised by tools like coverity, compared to rte_strscpy which is DPDK-specific.

/Bruce

#5 lore raw
Quoted 12 lines · Bruce Richardson
Quoted 11 lines · Bruce Richardson
Quoted 10 lines · Olivier Matz
Quoted 5 lines · Ferruh Yigit
Quoted 2 lines · Ciara Loftus

strncpy may leave the destination buffer not NULL terminated so use snprintf instead.

What do you think using 'strlcpy'?

Or even better, rte_strscpy()
https://git.dpdk.org/dpdk/commit/?id=b0236c7cf761

Quoted 9 lines
Quoted 6 lines

I think this is largely a matter of preference, and unless there is a good reason not to, I tend towards strlcpy as the older and more common (till now) interface. The main thing is just to use a function that will guarantee dest is null-terminated here, and both strlcpy and strscpy meet that criteria.

I'd also add that strlcpy is more likely to be recognised by tools like coverity, compared to rte_strscpy which is DPDK-specific.

+1 to 'strlcpy'

#6 lore raw
Quoted 19 lines · Ferruh Yigit
Quoted 18 lines · Bruce Richardson
Quoted 17 lines · Bruce Richardson
Quoted 10 lines · Olivier Matz
Quoted 5 lines · Ferruh Yigit
Quoted 2 lines · Ciara Loftus

strncpy may leave the destination buffer not NULL terminated so use snprintf instead.

What do you think using 'strlcpy'?

Or even better, rte_strscpy()
https://git.dpdk.org/dpdk/commit/?id=b0236c7cf761

I think this is largely a matter of preference, and unless there is a good reason not to, I tend towards strlcpy as the older and more common (till now) interface. The main thing is just to use a function that will guarantee dest is null-terminated here, and both strlcpy and strscpy meet that criteria.

Quoted 5 lines
Quoted 3 lines

I'd also add that strlcpy is more likely to be recognised by tools like coverity, compared to rte_strscpy which is DPDK-specific.

+1 to 'strlcpy'

Using strlcpy will be more recognized by static analyzer indeed.

But strscpy API is better:

* It helps checking string truncation by making it easier:

if (strlcpy(dst, src, dstsize) >= dstsize)
/* Dev + reviewer needs to think about using >= and not >, dstsize is
* repeated so either dst is an array or it needs a dedicated variable.
* Deal with truncation.
*/

if (rte_strscpy(dst, src, dstsize) < 0)

        /* deal with truncation. */

* It is safer when dealing with unknown data source. strlcpy will always read all of src, because the API (uselessly) defines the return value to strlen(src).

Having yet another string copy function is contentious, but we can avoid using worse API to please tools.

And detecting string truncation *is* helpful. String are used as IDs in DPDK for some objects. Using strlcpy / snprintf at least protects from buffer overflow, which is a bare minimum. A good implementation would also warn the user about a config error / memory corruption happening sooner.

In any case, sure to fix a sanity check strlcpy / snprintf will work.

Cheers,

Signature · 1 line
Gaëtan
#7 lore raw
Quoted 48 lines · Gaëtan Rivet
Quoted 19 lines · Ferruh Yigit
Quoted 18 lines · Bruce Richardson
Quoted 17 lines · Bruce Richardson
Quoted 10 lines · Olivier Matz
Quoted 5 lines · Ferruh Yigit
Quoted 2 lines · Ciara Loftus

strncpy may leave the destination buffer not NULL terminated so use snprintf instead.

What do you think using 'strlcpy'?

Or even better, rte_strscpy()
https://git.dpdk.org/dpdk/commit/?id=b0236c7cf761

I think this is largely a matter of preference, and unless there is a good reason not to, I tend towards strlcpy as the older and more common (till now) interface. The main thing is just to use a function that will guarantee dest is null-terminated here, and both strlcpy and strscpy meet that criteria.

Quoted 5 lines
Quoted 3 lines

I'd also add that strlcpy is more likely to be recognised by tools like coverity, compared to rte_strscpy which is DPDK-specific.

+1 to 'strlcpy'

Using strlcpy will be more recognized by static analyzer indeed.

But strscpy API is better:

* It helps checking string truncation by making it easier:

if (strlcpy(dst, src, dstsize) >= dstsize)
/* Dev + reviewer needs to think about using >= and not >, dstsize is
* repeated so either dst is an array or it needs a dedicated variable.
* Deal with truncation.
*/

if (rte_strscpy(dst, src, dstsize) < 0)

        /* deal with truncation. */

* It is safer when dealing with unknown data source. strlcpy will always read all of src, because the API (uselessly) defines the return value to strlen(src).

Having yet another string copy function is contentious, but we can avoid using worse API to please tools.

Quoted 8 lines

And detecting string truncation *is* helpful. String are used as IDs in DPDK for some objects. Using strlcpy / snprintf at least protects from buffer overflow, which is a bare minimum. A good implementation would also warn the user about a config error / memory corruption happening sooner.

In any case, sure to fix a sanity check strlcpy / snprintf will work.

Yes.

My main issue with strscpy right now is that it's got to be a DPDK-specific function, since AFAIK it's defined in no standard C library, just in the Linux kernel. If we get strscpy added to e.g. glibc, then we can see about starting to use it - letting meson do the work of detect if it's present and allowing us to define a fallback only in case it's not (i.e. as is done with strlcpy).

#8 lore raw
In reply to Gaëtan Rivet
Quoted 48 lines · Gaëtan Rivet
Quoted 19 lines · Ferruh Yigit
Quoted 18 lines · Bruce Richardson
Quoted 17 lines · Bruce Richardson
Quoted 10 lines · Olivier Matz
Quoted 5 lines · Ferruh Yigit
Quoted 2 lines · Ciara Loftus

strncpy may leave the destination buffer not NULL terminated so use snprintf instead.

What do you think using 'strlcpy'?

Or even better, rte_strscpy()
https://git.dpdk.org/dpdk/commit/?id=b0236c7cf761

I think this is largely a matter of preference, and unless there is a good reason not to, I tend towards strlcpy as the older and more common (till now) interface. The main thing is just to use a function that will guarantee dest is null-terminated here, and both strlcpy and strscpy meet that criteria.

Quoted 5 lines
Quoted 3 lines

I'd also add that strlcpy is more likely to be recognised by tools like coverity, compared to rte_strscpy which is DPDK-specific.

+1 to 'strlcpy'

Using strlcpy will be more recognized by static analyzer indeed.

But strscpy API is better:

* It helps checking string truncation by making it easier:

if (strlcpy(dst, src, dstsize) >= dstsize)
/* Dev + reviewer needs to think about using >= and not >, dstsize is
* repeated so either dst is an array or it needs a dedicated variable.
* Deal with truncation.
*/

if (rte_strscpy(dst, src, dstsize) < 0)

         /* deal with truncation. */

* It is safer when dealing with unknown data source. strlcpy will always read all of src, because the API (uselessly) defines the return value to strlen(src).

Having yet another string copy function is contentious, but we can avoid using worse API to please tools.

Quoted 8 lines

And detecting string truncation *is* helpful. String are used as IDs in DPDK for some objects. Using strlcpy / snprintf at least protects from buffer overflow, which is a bare minimum. A good implementation would also warn the user about a config error / memory corruption happening sooner.

In any case, sure to fix a sanity check strlcpy / snprintf will work.

I also think 'strscpy' is better API, and we had similar discussion before [1] and the decision was to prefer 'strlcpy'.

[1] http://inbox.dpdk.org/dev/[email protected]/#t

#9 lore raw
Quoted 55 lines · Ferruh Yigit
Quoted 54 lines · Gaëtan Rivet
Quoted 19 lines · Ferruh Yigit
Quoted 18 lines · Bruce Richardson
Quoted 17 lines · Bruce Richardson
Quoted 10 lines · Olivier Matz
Quoted 5 lines · Ferruh Yigit
Quoted 2 lines · Ciara Loftus

strncpy may leave the destination buffer not NULL terminated so use snprintf instead.

What do you think using 'strlcpy'?

Or even better, rte_strscpy()
https://git.dpdk.org/dpdk/commit/?id=b0236c7cf761

I think this is largely a matter of preference, and unless there is a good reason not to, I tend towards strlcpy as the older and more common (till now) interface. The main thing is just to use a function that will guarantee dest is null-terminated here, and both strlcpy and strscpy meet that criteria.

Quoted 5 lines
Quoted 3 lines

I'd also add that strlcpy is more likely to be recognised by tools like coverity, compared to rte_strscpy which is DPDK-specific.

+1 to 'strlcpy'

Using strlcpy will be more recognized by static analyzer indeed.

But strscpy API is better:

* It helps checking string truncation by making it easier:

if (strlcpy(dst, src, dstsize) >= dstsize)
/* Dev + reviewer needs to think about using >= and not >, dstsize is
* repeated so either dst is an array or it needs a dedicated variable.
* Deal with truncation.
*/

if (rte_strscpy(dst, src, dstsize) < 0)

         /* deal with truncation. */

* It is safer when dealing with unknown data source. strlcpy will always read all of src, because the API (uselessly) defines the return value to strlen(src).

Having yet another string copy function is contentious, but we can avoid using worse API to please tools.

And detecting string truncation *is* helpful. String are used as IDs in DPDK for some objects. Using strlcpy / snprintf at least protects from buffer overflow, which is a bare minimum. A good implementation would also warn the user about a config error / memory corruption happening sooner.

Quoted 7 lines
Quoted 2 lines

In any case, sure to fix a sanity check strlcpy / snprintf will work.

I also think 'strscpy' is better API, and we had similar discussion before
[1] and the decision was to prefer 'strlcpy'.

[1] http://inbox.dpdk.org/dev/[email protected]/#t

Good memory! I hadn't seen this thread, thanks.

Signature · 1 line
Gaëtan