Skip to content

Commit e3bbab4

Browse files
JustinStittkuba-moo
authored andcommitted
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: KSPP/linux#90 Signed-off-by: Justin Stitt <[email protected]> Reviewed-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/r/20231010-strncpy-drivers-net-dsa-vitesse-vsc73xx-core-c-v2-1-ba4416a9ff23@google.com Signed-off-by: Jakub Kicinski <[email protected]>
1 parent cf8b49f commit e3bbab4

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

drivers/net/dsa/vitesse-vsc73xx-core.c

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,8 @@ static void vsc73xx_get_strings(struct dsa_switch *ds, int port, u32 stringset,
928928
const struct vsc73xx_counter *cnt;
929929
struct vsc73xx *vsc = ds->priv;
930930
u8 indices[6];
931-
int i, j;
931+
u8 *buf = data;
932+
int i;
932933
u32 val;
933934
int ret;
934935

@@ -948,10 +949,7 @@ static void vsc73xx_get_strings(struct dsa_switch *ds, int port, u32 stringset,
948949
indices[5] = ((val >> 26) & 0x1f); /* TX counter 2 */
949950

950951
/* The first counters is the RX octets */
951-
j = 0;
952-
strncpy(data + j * ETH_GSTRING_LEN,
953-
"RxEtherStatsOctets", ETH_GSTRING_LEN);
954-
j++;
952+
ethtool_sprintf(&buf, "RxEtherStatsOctets");
955953

956954
/* Each port supports recording 3 RX counters and 3 TX counters,
957955
* 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,
961959
*/
962960
for (i = 0; i < 3; i++) {
963961
cnt = vsc73xx_find_counter(vsc, indices[i], false);
964-
if (cnt)
965-
strncpy(data + j * ETH_GSTRING_LEN,
966-
cnt->name, ETH_GSTRING_LEN);
967-
j++;
962+
ethtool_sprintf(&buf, "%s", cnt ? cnt->name : "");
968963
}
969964

970965
/* TX stats begins with the number of TX octets */
971-
strncpy(data + j * ETH_GSTRING_LEN,
972-
"TxEtherStatsOctets", ETH_GSTRING_LEN);
973-
j++;
966+
ethtool_sprintf(&buf, "TxEtherStatsOctets");
974967

975968
for (i = 3; i < 6; i++) {
976969
cnt = vsc73xx_find_counter(vsc, indices[i], true);
977-
if (cnt)
978-
strncpy(data + j * ETH_GSTRING_LEN,
979-
cnt->name, ETH_GSTRING_LEN);
980-
j++;
970+
ethtool_sprintf(&buf, "%s", cnt ? cnt->name : "");
971+
981972
}
982973
}
983974

0 commit comments

Comments
 (0)