Skip to contentSkip to conversation

net: lwip: Do not write past end of buffer

PATCH In review Andrew Goodbody 5 participants 10 messages Jan 14 – Feb 4 Networking

lore
Share & tools
lore
#0 lore raw

v2 cover net: lwip: Do not write past end of buffer

Fix some cases where the trailing \0 written by sprintf is not allowed for in the target buffer resulting in writing past the end of the buffer.

---
Changes in v2:
- Address review comments from Michal Simek
- Add a second patch to address some more instances of sprintf writing past the end of the target buffer found by code inspection while looking for an issue.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Andrew Goodbody (2):
net: lwip: tftp: Do not write past buffer end
net: lwip: dhcp: Do not write past end of buffer

 lib/lwip/lwip/src/apps/tftp/tftp.c | 2 +-
 net/lwip/dhcp.c                    | 6 +++---
 net/lwip/net-lwip.c                | 6 +++---
 3 files changed, 7 insertions(+), 7 deletions(-)

---
base-commit: c05dba22f1f2b0b2655ee3971644acf1936cd07a
change-id: 20260109-tftp_fix-3ab9bd66a6ad

Best regards,

Signature · 1 line
Andrew Goodbody <[email protected]>
#1 lore raw

PATCH v2 1/2 net: lwip: tftp: Do not write past buffer end

sprintf will add a trailing \0 so manually adding a trailing \0 will result in an extra unaccounted for character being written. This overwrote the first byte of the following allocation block resulting in unexpected behavior.

This was found by Running 'pxe get' with no available file resulting in multiple attempts, using the default algorithm, to attempt to find a file. Eventually there would be a failed assert when free() was called. Failing the assert would result in a system reset.

Fixes 27d7ccda94fa ("net: lwip: tftp: add support of blksize option to client")
---
 lib/lwip/lwip/src/apps/tftp/tftp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/lwip/lwip/src/apps/tftp/tftp.c +1 -1
index ecb6c55ae1100779187e7b138d098a0ef1e48ca1..25da952e92566cbca1c64bc89c89102e74d0a42c 100644
@@ -191,7 +191,7 @@ send_request(const ip_addr_t *addr, u16_t port, u16_t opcode, const char* fname,
   MEMCPY(payload+2,              fname, fname_length);
   MEMCPY(payload+2+fname_length, mode,  mode_length);
   if (tftp_state.blksize)
-    sprintf(payload+2+fname_length+mode_length, "blksize%c%d%c", 0, tftp_state.blksize, 0);
+    sprintf(payload+2+fname_length+mode_length, "blksize%c%d", 0, tftp_state.blksize);
 
   tftp_state.wait_oack = true;
   ret = udp_sendto(tftp_state.upcb, p, addr, port);
Signature · 1 line
2.47.3
Tested-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] net: lwip: tftp: Do not write past buffer end

Quoted 10 lines · Andrew Goodbody

sprintf will add a trailing \0 so manually adding a trailing \0 will result in an extra unaccounted for character being written. This overwrote the first byte of the following allocation block resulting in unexpected behavior.

This was found by Running 'pxe get' with no available file resulting in multiple attempts, using the default algorithm, to attempt to find a file. Eventually there would be a failed assert when free() was called. Failing the assert would result in a system reset.

Quoted 5 lines
Fixes 27d7ccda94fa ("net: lwip: tftp: add support of blksize option to client")
Signature · 1 line
Tom
Reviewed-by on v2 1/2
#3 lore raw

Re: [PATCH v2 1/2] net: lwip: tftp: Do not write past buffer end

In reply to Andrew Goodbody
Quoted 25 lines · Andrew Goodbody

sprintf will add a trailing \0 so manually adding a trailing \0 will result in an extra unaccounted for character being written. This overwrote the first byte of the following allocation block resulting in unexpected behavior.

This was found by Running 'pxe get' with no available file resulting in multiple attempts, using the default algorithm, to attempt to find a file. Eventually there would be a failed assert when free() was called. Failing the assert would result in a system reset.

Fixes 27d7ccda94fa ("net: lwip: tftp: add support of blksize option to client")
---
 lib/lwip/lwip/src/apps/tftp/tftp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/lwip/lwip/src/apps/tftp/tftp.c +0 -0
index ecb6c55ae1100779187e7b138d098a0ef1e48ca1..25da952e92566cbca1c64bc89c89102e74d0a42c 100644

lib/lwip/lwip/src/apps/tftp/tftp.c send_request(const ip_addr_t *addr, u16_t port, u16_t opcode, const char* fname, · line 197

Quoted 8 lines
lib/lwip/lwip/src/apps/tftp/tftp.c +1 -1
@@ -191,7 +191,7 @@ send_request(const ip_addr_t *addr, u16_t port, u16_t opcode, const char* fname,
   MEMCPY(payload+2,              fname, fname_length);
   MEMCPY(payload+2+fname_length, mode,  mode_length);
   if (tftp_state.blksize)
-    sprintf(payload+2+fname_length+mode_length, "blksize%c%d%c", 0, tftp_state.blksize, 0);
+    sprintf(payload+2+fname_length+mode_length, "blksize%c%d", 0, tftp_state.blksize);
 
   tftp_state.wait_oack = true;
   ret = udp_sendto(tftp_state.upcb, p, addr, port);

Thanks,

Signature · 1 line
Jerome
Reviewed-by on v2 1/2
#4 lore raw

Re: [PATCH v2 1/2] net: lwip: tftp: Do not write past buffer end

In reply to Andrew Goodbody
Quoted 25 lines · Andrew Goodbody

sprintf will add a trailing \0 so manually adding a trailing \0 will result in an extra unaccounted for character being written. This overwrote the first byte of the following allocation block resulting in unexpected behavior.

This was found by Running 'pxe get' with no available file resulting in multiple attempts, using the default algorithm, to attempt to find a file. Eventually there would be a failed assert when free() was called. Failing the assert would result in a system reset.

Fixes 27d7ccda94fa ("net: lwip: tftp: add support of blksize option to client")
---
 lib/lwip/lwip/src/apps/tftp/tftp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/lwip/lwip/src/apps/tftp/tftp.c +0 -0
index ecb6c55ae1100779187e7b138d098a0ef1e48ca1..25da952e92566cbca1c64bc89c89102e74d0a42c 100644

lib/lwip/lwip/src/apps/tftp/tftp.c send_request(const ip_addr_t *addr, u16_t port, u16_t opcode, const char* fname, · line 197

Quoted 8 lines
lib/lwip/lwip/src/apps/tftp/tftp.c +1 -1
@@ -191,7 +191,7 @@ send_request(const ip_addr_t *addr, u16_t port, u16_t opcode, const char* fname,
   MEMCPY(payload+2,              fname, fname_length);
   MEMCPY(payload+2+fname_length, mode,  mode_length);
   if (tftp_state.blksize)
-    sprintf(payload+2+fname_length+mode_length, "blksize%c%d%c", 0, tftp_state.blksize, 0);
+    sprintf(payload+2+fname_length+mode_length, "blksize%c%d", 0, tftp_state.blksize);
 
   tftp_state.wait_oack = true;
   ret = udp_sendto(tftp_state.upcb, p, addr, port);

Thanks,

Signature · 1 line
Jerome
#5 lore raw

PATCH v2 2/2 net: lwip: dhcp: Do not write past end of buffer

In reply to Andrew Goodbody

sprintf will write a trailing \0 at the end of the string so when writing into a buffer, that buffer must be sized to allow for that trailing zero. In the DHCP code when the index is a number needing two digits to express the index would use up the two \0 bytes in the buffer and the trailing \0 from sprintf would be beyond the end of the allocation. Fix this by adding a third \0 in the buffer.

This was found by code inspection when looking for an issue reported by Michal Simek, but I do not have the hardware to reproduce, so cannot confirm if this addresses that issue or not.

Fixes 98ad145db61a ("net: lwip: add DHCP support and dhcp commmand")
---
 net/lwip/dhcp.c     | 6 +++---
 net/lwip/net-lwip.c | 6 +++---
 2 files changed, 6 insertions(+), 6 deletions(-)
net/lwip/dhcp.c +3 -3
index b798014ebcb97dee688e29ccb34da916296c6772..731b57de3ba4336a22fee5957b6e2163360d78a1 100644
@@ -30,9 +30,9 @@ static void call_lwip_dhcp_fine_tmr(void *ctx)
 
 static int dhcp_loop(struct udevice *udev)
 {
-	char ipstr[] = "ipaddr\0\0";
-	char maskstr[] = "netmask\0\0";
-	char gwstr[] = "gatewayip\0\0";
+	char ipstr[] = "ipaddr\0\0\0";
+	char maskstr[] = "netmask\0\0\0";
+	char gwstr[] = "gatewayip\0\0\0";
 	const ip_addr_t *ntpserverip;
 	unsigned long start;
 	struct netif *netif;
net/lwip/net-lwip.c +3 -3
index 8741f65fe12e23cc30068506453a762533f8a67f..0fa33730a636727d0718625215a69002242810d0 100644
@@ -108,9 +108,9 @@ struct netif *net_lwip_get_netif(void)
 static int get_udev_ipv4_info(struct udevice *dev, ip4_addr_t *ip,
 			      ip4_addr_t *mask, ip4_addr_t *gw)
 {
-	char ipstr[] = "ipaddr\0\0";
-	char maskstr[] = "netmask\0\0";
-	char gwstr[] = "gatewayip\0\0";
+	char ipstr[] = "ipaddr\0\0\0";
+	char maskstr[] = "netmask\0\0\0";
+	char gwstr[] = "gatewayip\0\0\0";
 	int idx = dev_seq(dev);
 	char *env;
 
Signature · 1 line
2.47.3
Reviewed-by on v2 2/2
#6 lore raw

Re: [PATCH v2 2/2] net: lwip: dhcp: Do not write past end of buffer

Quoted 43 lines · Andrew Goodbody

sprintf will write a trailing \0 at the end of the string so when writing into a buffer, that buffer must be sized to allow for that trailing zero. In the DHCP code when the index is a number needing two digits to express the index would use up the two \0 bytes in the buffer and the trailing \0 from sprintf would be beyond the end of the allocation. Fix this by adding a third \0 in the buffer.

This was found by code inspection when looking for an issue reported by Michal Simek, but I do not have the hardware to reproduce, so cannot confirm if this addresses that issue or not.

Fixes 98ad145db61a ("net: lwip: add DHCP support and dhcp commmand")
---
 net/lwip/dhcp.c     | 6 +++---
 net/lwip/net-lwip.c | 6 +++---
 2 files changed, 6 insertions(+), 6 deletions(-)
net/lwip/dhcp.c +3 -3
index b798014ebcb97dee688e29ccb34da916296c6772..731b57de3ba4336a22fee5957b6e2163360d78a1 100644
@@ -30,9 +30,9 @@ static void call_lwip_dhcp_fine_tmr(void *ctx)
 
 static int dhcp_loop(struct udevice *udev)
 {
-	char ipstr[] = "ipaddr\0\0";
-	char maskstr[] = "netmask\0\0";
-	char gwstr[] = "gatewayip\0\0";
+	char ipstr[] = "ipaddr\0\0\0";
+	char maskstr[] = "netmask\0\0\0";
+	char gwstr[] = "gatewayip\0\0\0";
 	const ip_addr_t *ntpserverip;
 	unsigned long start;
 	struct netif *netif;
net/lwip/net-lwip.c +0 -0
index 8741f65fe12e23cc30068506453a762533f8a67f..0fa33730a636727d0718625215a69002242810d0 100644
@@ -108,9 +108,9 @@ struct netif *net_lwip_get_netif(void)
 static int get_udev_ipv4_info(struct udevice *dev, ip4_addr_t *ip,
 			      ip4_addr_t *mask, ip4_addr_t *gw)

net/lwip/net-lwip.c struct netif *net_lwip_get_netif(void) · line 116

Quoted 10 lines
net/lwip/net-lwip.c +3 -3
 {
-	char ipstr[] = "ipaddr\0\0";
-	char maskstr[] = "netmask\0\0";
-	char gwstr[] = "gatewayip\0\0";
+	char ipstr[] = "ipaddr\0\0\0";
+	char maskstr[] = "netmask\0\0\0";
+	char gwstr[] = "gatewayip\0\0\0";
 	int idx = dev_seq(dev);
 	char *env;
 

Thanks,

Signature · 1 line
Jerome
#7 lore raw
In reply to Andrew Goodbody

Hi Jerome,

st 14. 1. 2026 v 16:12 odesílatel Andrew Goodbody
<[email protected]> napsal:

Quoted 23 lines

Fix some cases where the trailing \0 written by sprintf is not allowed for in the target buffer resulting in writing past the end of the buffer.

---
Changes in v2:
- Address review comments from Michal Simek
- Add a second patch to address some more instances of sprintf writing past the end of the target buffer found by code inspection while looking for an issue.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Andrew Goodbody (2):
net: lwip: tftp: Do not write past buffer end
net: lwip: dhcp: Do not write past end of buffer

 lib/lwip/lwip/src/apps/tftp/tftp.c | 2 +-
 net/lwip/dhcp.c                    | 6 +++---
 net/lwip/net-lwip.c                | 6 +++---
 3 files changed, 7 insertions(+), 7 deletions(-)
Quoted 8 lines

---
base-commit: c05dba22f1f2b0b2655ee3971644acf1936cd07a
change-id: 20260109-tftp_fix-3ab9bd66a6ad

Best regards,
--
Andrew Goodbody <[email protected]>

Are you going to apply it? I would be good to get it in.

Thanks,
Michal

Signature · 5 lines
Michal Simek, Ing. (M.Eng), OpenPGP -> KeyID: FE3D1F91
w: www.monstr.eu p: +42-0-721842854
Maintainer of Linux kernel - Xilinx Microblaze
Maintainer of Linux kernel - Xilinx Zynq ARM and ZynqMP ARM64 SoCs
U-Boot custodian - Xilinx Microblaze/Zynq/ZynqMP/Versal SoCs
#8 lore raw
Quoted 31 lines · Michal Simek

Hi Jerome,

st 14. 1. 2026 v 16:12 odesílatel Andrew Goodbody
<[email protected]> napsal:

Quoted 27 lines

Fix some cases where the trailing \0 written by sprintf is not allowed for in the target buffer resulting in writing past the end of the buffer.

---
Changes in v2:
- Address review comments from Michal Simek
- Add a second patch to address some more instances of sprintf writing past the end of the target buffer found by code inspection while looking for an issue.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Andrew Goodbody (2):
net: lwip: tftp: Do not write past buffer end
net: lwip: dhcp: Do not write past end of buffer

 lib/lwip/lwip/src/apps/tftp/tftp.c | 2 +-
 net/lwip/dhcp.c                    | 6 +++---
 net/lwip/net-lwip.c                | 6 +++---
 3 files changed, 7 insertions(+), 7 deletions(-)

---
base-commit: c05dba22f1f2b0b2655ee3971644acf1936cd07a
change-id: 20260109-tftp_fix-3ab9bd66a6ad

Quoted 6 lines
Quoted 4 lines

Best regards,
--
Andrew Goodbody <[email protected]>

Are you going to apply it? I would be good to get it in.

I'll second that ask since it's what's keeping:
https://source.denx.de/u-boot/u-boot/-/jobs/1370910
failing and not passing.

Signature · 1 line
Tom
#9 lore raw

Hi,

Quoted 32 lines · Tom Rini
Quoted 31 lines · Michal Simek

Hi Jerome,

st 14. 1. 2026 v 16:12 odesílatel Andrew Goodbody
<[email protected]> napsal:

Quoted 27 lines

Fix some cases where the trailing \0 written by sprintf is not allowed for in the target buffer resulting in writing past the end of the buffer.

---
Changes in v2:
- Address review comments from Michal Simek
- Add a second patch to address some more instances of sprintf writing past the end of the target buffer found by code inspection while looking for an issue.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Andrew Goodbody (2):
net: lwip: tftp: Do not write past buffer end
net: lwip: dhcp: Do not write past end of buffer

 lib/lwip/lwip/src/apps/tftp/tftp.c | 2 +-
 net/lwip/dhcp.c                    | 6 +++---
 net/lwip/net-lwip.c                | 6 +++---
 3 files changed, 7 insertions(+), 7 deletions(-)

---
base-commit: c05dba22f1f2b0b2655ee3971644acf1936cd07a
change-id: 20260109-tftp_fix-3ab9bd66a6ad

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

Best regards,
--
Andrew Goodbody <[email protected]>

Are you going to apply it? I would be good to get it in.

Sorry Michal your email ended up in my spam folder :-/

Quoted 4 lines

I'll second that ask since it's what's keeping:
https://source.denx.de/u-boot/u-boot/-/jobs/1370910
failing and not passing.

I'll send a pull request today.

Thanks,

Signature · 1 line
Jerome