Skip to contentSkip to conversation

net: String format safety updates

PATCH Applied Simon Horman 2 participants 4 messages Oct 14, 2024 – Oct 15, 2024

Subsystems (5)
lore
Share & tools
lore

Simon Horman

author reviewer
#0 lore raw

v2 cover net: String format safety updates

Hi,

This series addresses string format safety issues that are flagged by tooling in files touched by recent patches.

I do not believe that any of these issues are bugs. Rather, I am providing these updates as I think there is a value in addressing such warnings so real problems stand out.

---
Changes in v2:
- Dropped accel/qaic patch; it is not for net-next
- See per-patch changelogs
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Simon Horman (2):
net: dsa: microchip: copy string using strscpy
net: txgbe: Pass string literal as format argument of alloc_workqueue()

 drivers/net/dsa/microchip/ksz_ptp.c            | 2 +-
 drivers/net/ethernet/wangxun/txgbe/txgbe_phy.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

base-commit: 6aac56631831e1386b6edd3c583c8afb2abfd267

author reviewer
#1 lore raw

PATCH v2 1/2 net: dsa: microchip: copy string using strscpy

Prior to this patch ksz_ptp_msg_irq_setup() uses snprintf() to copy strings. It does so by passing strings as the format argument of snprintf(). This appears to be safe, due to the absence of format specifiers in the strings, which are declared within the same function. But nonetheless GCC 14 warns about it:

.../ksz_ptp.c:1109:55: warning: format string is not a string literal (potentially insecure) [-Wformat-security]
1109 | snprintf(ptpmsg_irq->name, sizeof(ptpmsg_irq->name), name[n]);
| ^~~~~~~
.../ksz_ptp.c:1109:55: note: treat the string as an argument to avoid this

 1109 |         snprintf(ptpmsg_irq->name, sizeof(ptpmsg_irq->name), name[n]);
      |                                                              ^
      |                                                              "%s",

As what we are really dealing with here is a string copy, it seems make sense to use a function designed for this purpose. In this case null padding is not required, so strscpy is appropriate. And as the destination is an array of fixed size, the 2-argument variant may be used.

--
v2
- Add Reviewed-by from Daniel Machon
- Tweaked patch description, thanks to Daniel Machon

---
 drivers/net/dsa/microchip/ksz_ptp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/dsa/microchip/ksz_ptp.c +1 -1
index 050f17c43ef6..22fb9ef4645c 100644
@@ -1106,7 +1106,7 @@ static int ksz_ptp_msg_irq_setup(struct ksz_port *port, u8 n)
 	ptpmsg_irq->port = port;
 	ptpmsg_irq->ts_reg = ops->get_port_addr(port->num, ts_reg[n]);
 
-	snprintf(ptpmsg_irq->name, sizeof(ptpmsg_irq->name), name[n]);
+	strscpy(ptpmsg_irq->name, name[n]);
 
 	ptpmsg_irq->num = irq_find_mapping(port->ptpirq.domain, n);
 	if (ptpmsg_irq->num < 0)
Signature · 1 line
2.45.2
author reviewer
#2 lore raw

PATCH v2 2/2 net: txgbe: Pass string literal as format argument of alloc_workqueue()

In reply to Simon Horman

Recently I noticed that both gcc-14 and clang-18 report that passing a non-string literal as the format argument of clkdev_create() is potentially insecure.

E.g. clang-18 says:

.../txgbe_phy.c:582:35: warning: format string is not a string literal (potentially insecure) [-Wformat-security]
581 | clock = clkdev_create(clk, NULL, clk_name);
| ^~~~~~~~
.../txgbe_phy.c:582:35: note: treat the string as an argument to avoid this

  581 |         clock = clkdev_create(clk, NULL, clk_name);
      |                                          ^
      |                                          "%s",

It is always the case where the contents of clk_name is safe to pass as the format argument. That is, in my understanding, it never contains any format escape sequences.

However, it seems better to be safe than sorry. And, as a bonus, compiler output becomes less verbose by addressing this issue as suggested by clang-18.

Compile tested only.

--
v2
- No changes

---
 drivers/net/ethernet/wangxun/txgbe/txgbe_phy.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/wangxun/txgbe/txgbe_phy.c +1 -1
index 3dd89dafe7c7..a0e4920b4761 100644
@@ -578,7 +578,7 @@ static int txgbe_clock_register(struct txgbe *txgbe)
 	if (IS_ERR(clk))
 		return PTR_ERR(clk);
 
-	clock = clkdev_create(clk, NULL, clk_name);
+	clock = clkdev_create(clk, NULL, "%s", clk_name);
 	if (!clock) {
 		clk_unregister(clk);
 		return -ENOMEM;
Signature · 1 line
2.45.2
#3 lore raw
In reply to Simon Horman
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 10 lines · On Mon, 14 Oct 2024 09:52:24 +0100 you wrote:

Hi,

This series addresses string format safety issues that are flagged by tooling in files touched by recent patches.

I do not believe that any of these issues are bugs. Rather, I am providing these updates as I think there is a value in addressing such warnings so real problems stand out.

[...]

Here is the summary with links:
- [net-next,v2,1/2] net: dsa: microchip: copy string using strscpy

    https://git.kernel.org/netdev/net-next/c/26919411acfa
  - [net-next,v2,2/2] net: txgbe: Pass string literal as format argument of alloc_workqueue()
    https://git.kernel.org/netdev/net-next/c/d6488e77725e

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html