Commit e3bbab47 authored by Justin Stitt's avatar Justin Stitt Committed by Jakub Kicinski

net: dsa: vsc73xx: replace deprecated strncpy with ethtool_sprintf

`strncpy` is deprecated for use on NUL-terminated destination strings
[1] and as such we should prefer more robust and less ambiguous string
interfaces.

ethtool_sprintf() is designed specifically for get_strings() usage.
Let's replace strncpy in favor of this more robust and easier to
understand interface.

This change could result in misaligned strings when if(cnt) fails. To
combat this, use ternary to place empty string in buffer and properly
increment pointer to next string slot.

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [2]
Link: https://github.com/KSPP/linux/issues/90Signed-off-by: default avatarJustin Stitt <justinstitt@google.com>
Reviewed-by: default avatarKees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20231010-strncpy-drivers-net-dsa-vitesse-vsc73xx-core-c-v2-1-ba4416a9ff23@google.comSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent cf8b49fb
...@@ -928,7 +928,8 @@ static void vsc73xx_get_strings(struct dsa_switch *ds, int port, u32 stringset, ...@@ -928,7 +928,8 @@ static void vsc73xx_get_strings(struct dsa_switch *ds, int port, u32 stringset,
const struct vsc73xx_counter *cnt; const struct vsc73xx_counter *cnt;
struct vsc73xx *vsc = ds->priv; struct vsc73xx *vsc = ds->priv;
u8 indices[6]; u8 indices[6];
int i, j; u8 *buf = data;
int i;
u32 val; u32 val;
int ret; int ret;
...@@ -948,10 +949,7 @@ static void vsc73xx_get_strings(struct dsa_switch *ds, int port, u32 stringset, ...@@ -948,10 +949,7 @@ static void vsc73xx_get_strings(struct dsa_switch *ds, int port, u32 stringset,
indices[5] = ((val >> 26) & 0x1f); /* TX counter 2 */ indices[5] = ((val >> 26) & 0x1f); /* TX counter 2 */
/* The first counters is the RX octets */ /* The first counters is the RX octets */
j = 0; ethtool_sprintf(&buf, "RxEtherStatsOctets");
strncpy(data + j * ETH_GSTRING_LEN,
"RxEtherStatsOctets", ETH_GSTRING_LEN);
j++;
/* Each port supports recording 3 RX counters and 3 TX counters, /* Each port supports recording 3 RX counters and 3 TX counters,
* figure out what counters we use in this set-up and return the * figure out what counters we use in this set-up and return the
...@@ -961,23 +959,16 @@ static void vsc73xx_get_strings(struct dsa_switch *ds, int port, u32 stringset, ...@@ -961,23 +959,16 @@ static void vsc73xx_get_strings(struct dsa_switch *ds, int port, u32 stringset,
*/ */
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
cnt = vsc73xx_find_counter(vsc, indices[i], false); cnt = vsc73xx_find_counter(vsc, indices[i], false);
if (cnt) ethtool_sprintf(&buf, "%s", cnt ? cnt->name : "");
strncpy(data + j * ETH_GSTRING_LEN,
cnt->name, ETH_GSTRING_LEN);
j++;
} }
/* TX stats begins with the number of TX octets */ /* TX stats begins with the number of TX octets */
strncpy(data + j * ETH_GSTRING_LEN, ethtool_sprintf(&buf, "TxEtherStatsOctets");
"TxEtherStatsOctets", ETH_GSTRING_LEN);
j++;
for (i = 3; i < 6; i++) { for (i = 3; i < 6; i++) {
cnt = vsc73xx_find_counter(vsc, indices[i], true); cnt = vsc73xx_find_counter(vsc, indices[i], true);
if (cnt) ethtool_sprintf(&buf, "%s", cnt ? cnt->name : "");
strncpy(data + j * ETH_GSTRING_LEN,
cnt->name, ETH_GSTRING_LEN);
j++;
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment