net: lwip: Do not write past end of buffer
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]>
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.
--- 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
@@ -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
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")Reported-by Michal Simek <[email protected]>Tested-by Michal Simek <[email protected]>Signed-off-by Andrew Goodbody <[email protected]>
Signature · 1 line
Tom
Re: [PATCH v2 1/2] net: lwip: tftp: Do not write past buffer end
In reply to Andrew GoodbodyQuoted 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")Reported-by Michal Simek <[email protected]>Tested-by Michal Simek <[email protected]>Signed-off-by Andrew Goodbody <[email protected]>--- 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
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
Re: [PATCH v2 1/2] net: lwip: tftp: Do not write past buffer end
In reply to Andrew GoodbodyQuoted 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")Reported-by Michal Simek <[email protected]>Tested-by Michal Simek <[email protected]>Signed-off-by Andrew Goodbody <[email protected]>--- 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
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
PATCH v2 2/2 net: lwip: dhcp: Do not write past end of buffer
In reply to Andrew Goodbodysprintf 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.
--- net/lwip/dhcp.c | 6 +++--- net/lwip/net-lwip.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-)
net/lwip/dhcp.c +3 -3
@@ -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
@@ -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
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")Signed-off-by Andrew Goodbody <[email protected]>--- net/lwip/dhcp.c | 6 +++--- net/lwip/net-lwip.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-)net/lwip/dhcp.c +3
-3@@ -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@@ -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
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.
Signed-off-by Andrew Goodbody <[email protected]>---
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 bufferlib/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-3ab9bd66a6adBest 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
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.
Signed-off-by Andrew Goodbody <[email protected]>---
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 bufferlib/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
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.
Signed-off-by Andrew Goodbody <[email protected]>---
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 bufferlib/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