net: tftp: fix malformed option string parsing
PATCH 1/1 net: tftp: fix malformed option string parsing
When formatting the TFTP options (tsize, blksize, windowsize), the code previously used sprintf() with embedded null bytes (%c). Because sprintf() returns the length excluding the terminating null byte, subsequent sprintf() calls overwrote the required null separators.
This caused the options to be concatenated into a single malformed string (e.g., "tsize0blksize1468") which breaks strict TFTP servers like atftpd, causing them to reply with empty packets or drop the connection.
Fix this by explicitly using strcpy() for the option names and manually advancing the pointer to ensure the null bytes are preserved.
--- net/tftp.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-)
net/tftp.c +21 -12
@@ -355,20 +355,29 @@ static void tftp_send(void)debug ("send option \"timeout %s\"\n" , (char *)pkt); pkt +=strlen ((char *)pkt) +1 ;#ifdef CONFIG_TFTP_TSIZE - pkt +=sprintf ((char *)pkt,"tsize%c%u%c" , -0 , net_boot_file_size,0 ); +/* Write "tsize" + \0 */ +strcpy ((char *)pkt,"tsize" ); + pkt +=6 ;/* 5 chars + 1 null */ + +/* Write the value + \0 */ + pkt +=sprintf ((char *)pkt,"%u" , net_boot_file_size) +1 ;#endif -/* try for more effic. blk size */ - pkt +=sprintf ((char *)pkt,"blksize%c%d%c" , -0 , tftp_block_size_option,0 ); -/* try for more effic. window size. -* Implemented only for tftp get. -* Don't bother sending if it's 1 -*/ -if (tftp_state ==STATE_SEND_RRQ && tftp_window_size_option >1 ) - pkt +=sprintf ((char *)pkt,"windowsize%c%d%c" , -0 , tftp_window_size_option,0 ); +/* Write "blksize" + \0 */ +strcpy ((char *)pkt,"blksize" ); + pkt +=8 ;/* 7 chars + 1 null */ + +/* Write the value + \0 */ + pkt +=sprintf ((char *)pkt,"%d" , tftp_block_size_option) +1 ; + +if (tftp_state ==STATE_SEND_RRQ && tftp_window_size_option >1 ) { +/* Write "windowsize" + \0 */ +strcpy ((char *)pkt,"windowsize" ); + pkt +=11 ;/* 10 chars + 1 null */ + +/* Write the value + \0 */ + pkt +=sprintf ((char *)pkt,"%d" , tftp_window_size_option) +1 ; + } len = pkt - xp;break ;
Signature · 1 line
2.34.1
Hi Chencheng,
Quoted 4 lines · Chencheng Zhang
When formatting the TFTP options (tsize, blksize, windowsize), the code previously used
sprintf()with embedded null bytes (%c). Becausesprintf()returns the length excluding the terminating null byte, subsequentsprintf()calls overwrote the required null separators.
I'm not convinced, the analysis explains the observed effect,
Quoted 2 lines
This caused the options to be concatenated into a single malformed string (e.g., "tsize0blksize1468") which breaks strict TFTP servers
^^
... as missing 0 byte between 'e' and '0' is not even at the end...
Quoted 16 lines
like atftpd, causing them to reply with empty packets or drop the connection.
Fix this by explicitly using
strcpy()for the option names and manually advancing the pointer to ensure the null bytes are preserved.Signed-off-by Chencheng Zhang <[email protected]>--- net/tftp.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-)net/tftp.c +0
-0
net/tftp.c static void tftp_send(void) · line 358
Quoted 4 lines
net/tftp.c +0
-1@@ -355,20 +355,29 @@ static void tftp_send(void)debug ("send option \"timeout %s\"\n" , (char *)pkt); pkt +=strlen ((char *)pkt) +1 ;#ifdef CONFIG_TFTP_TSIZE - pkt += sprintf((char *)pkt, "tsize%c%u%c",
^^^
... of this format string.
Are you using USE_TINY_PRINTF or not?
Do you test with commit 1bc125becaa5 ("tiny-printf: emit \0 as %c")?
Quoted 37 lines
net/tftp.c +21
-11- 0, net_boot_file_size, 0); +/* Write "tsize" + \0 */ +strcpy ((char *)pkt,"tsize" ); + pkt +=6 ;/* 5 chars + 1 null */ + +/* Write the value + \0 */ + pkt +=sprintf ((char *)pkt,"%u" , net_boot_file_size) +1 ;#endif -/* try for more effic. blk size */ - pkt +=sprintf ((char *)pkt,"blksize%c%d%c" , -0 , tftp_block_size_option,0 ); -/* try for more effic. window size. -* Implemented only for tftp get. -* Don't bother sending if it's 1 -*/ -if (tftp_state ==STATE_SEND_RRQ && tftp_window_size_option >1 ) - pkt +=sprintf ((char *)pkt,"windowsize%c%d%c" , -0 , tftp_window_size_option,0 ); +/* Write "blksize" + \0 */ +strcpy ((char *)pkt,"blksize" ); + pkt +=8 ;/* 7 chars + 1 null */ + +/* Write the value + \0 */ + pkt +=sprintf ((char *)pkt,"%d" , tftp_block_size_option) +1 ; + +if (tftp_state ==STATE_SEND_RRQ && tftp_window_size_option >1 ) { +/* Write "windowsize" + \0 */ +strcpy ((char *)pkt,"windowsize" ); + pkt +=11 ;/* 10 chars + 1 null */ + +/* Write the value + \0 */ + pkt +=sprintf ((char *)pkt,"%d" , tftp_window_size_option) +1 ; + } len = pkt - xp;break ;
Signature · 3 lines
Alexander Sverdlin Siemens AG www.siemens.com
Quoted 36 lines · Alexander (SI B PRO TI EAC CCP)
Hi Chencheng,
Quoted 4 lines · Chencheng Zhang
When formatting the TFTP options (tsize, blksize, windowsize), the code previously used
sprintf()with embedded null bytes (%c). Becausesprintf()returns the length excluding the terminating null byte, subsequentsprintf()calls overwrote the required null separators.I'm not convinced, the analysis explains the observed effect,
Quoted 2 lines
This caused the options to be concatenated into a single malformed string (e.g., "tsize0blksize1468") which breaks strict TFTP servers
^^... as missing 0 byte between 'e' and '0' is not even at the end...
Quoted 16 lines
like atftpd, causing them to reply with empty packets or drop the connection.
Fix this by explicitly using
strcpy()for the option names and manually advancing the pointer to ensure the null bytes are preserved.Signed-off-by Chencheng Zhang <[email protected]>--- net/tftp.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-)net/tftp.c +0
-0net/tftp.c static void tftp_send(void) · line 358
Quoted 4 lines
net/tftp.c +0
-1@@ -355,20 +355,29 @@ static void tftp_send(void)debug ("send option \"timeout %s\"\n" , (char *)pkt); pkt +=strlen ((char *)pkt) +1 ;#ifdef CONFIG_TFTP_TSIZE - pkt += sprintf((char *)pkt, "tsize%c%u%c",^^^
Quoted 1 line
... of this format string.
+1, and NACK. The original code is perfectly fine assuming a compliant snprintf() implementation.
Quoted 3 lines
Are you using
USE_TINY_PRINTFor not?
Do you test with commit1bc125becaa5("tiny-printf: emit \0 as %c")?
Nice hint :)
Thanks,
Signature · 1 line
Jerome
Quoted 38 lines
Quoted 37 lines
net/tftp.c +21
-11- 0, net_boot_file_size, 0); +/* Write "tsize" + \0 */ +strcpy ((char *)pkt,"tsize" ); + pkt +=6 ;/* 5 chars + 1 null */ + +/* Write the value + \0 */ + pkt +=sprintf ((char *)pkt,"%u" , net_boot_file_size) +1 ;#endif -/* try for more effic. blk size */ - pkt +=sprintf ((char *)pkt,"blksize%c%d%c" , -0 , tftp_block_size_option,0 ); -/* try for more effic. window size. -* Implemented only for tftp get. -* Don't bother sending if it's 1 -*/ -if (tftp_state ==STATE_SEND_RRQ && tftp_window_size_option >1 ) - pkt +=sprintf ((char *)pkt,"windowsize%c%d%c" , -0 , tftp_window_size_option,0 ); +/* Write "blksize" + \0 */ +strcpy ((char *)pkt,"blksize" ); + pkt +=8 ;/* 7 chars + 1 null */ + +/* Write the value + \0 */ + pkt +=sprintf ((char *)pkt,"%d" , tftp_block_size_option) +1 ; + +if (tftp_state ==STATE_SEND_RRQ && tftp_window_size_option >1 ) { +/* Write "windowsize" + \0 */ +strcpy ((char *)pkt,"windowsize" ); + pkt +=11 ;/* 10 chars + 1 null */ + +/* Write the value + \0 */ + pkt +=sprintf ((char *)pkt,"%d" , tftp_window_size_option) +1 ; + } len = pkt - xp;break ;
Hi Sverdin and Jerome,
Quoted 1 line
Do you test with commit
1bc125becaa5("tiny-printf: emit \0 as %c")?
Thanks for pointing that out!
I have checked my baseline which is TI u-boot v2025-01. Clearly 1bc125becaa5 is not included. I patched my baseline with 1bc125becaa5 and it works.
You can reject my patch.
BR/Chencheng
Quoted 99 lines · Jerome Forissier
Quoted 36 lines · Alexander (SI B PRO TI EAC CCP)
Hi Chencheng,
Quoted 4 lines · Chencheng Zhang
When formatting the TFTP options (tsize, blksize, windowsize), the code previously used
sprintf()with embedded null bytes (%c). Becausesprintf()returns the length excluding the terminating null byte, subsequentsprintf()calls overwrote the required null separators.I'm not convinced, the analysis explains the observed effect,
Quoted 2 lines
This caused the options to be concatenated into a single malformed string (e.g., "tsize0blksize1468") which breaks strict TFTP servers
^^... as missing 0 byte between 'e' and '0' is not even at the end...
Quoted 16 lines
like atftpd, causing them to reply with empty packets or drop the connection.
Fix this by explicitly using
strcpy()for the option names and manually advancing the pointer to ensure the null bytes are preserved.Signed-off-by Chencheng Zhang <[email protected]>--- net/tftp.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-)net/tftp.c +0
-0net/tftp.c static void tftp_send(void) · line 358
Quoted 4 lines
net/tftp.c +0
-1@@ -355,20 +355,29 @@ static void tftp_send(void)debug ("send option \"timeout %s\"\n" , (char *)pkt); pkt +=strlen ((char *)pkt) +1 ;#ifdef CONFIG_TFTP_TSIZE - pkt += sprintf((char *)pkt, "tsize%c%u%c",^^^Quoted 1 line
... of this format string.
+1, and NACK. The original code is perfectly fine assuming a compliant
snprintf()implementation.Quoted 3 lines
Are you using
USE_TINY_PRINTFor not?
Do you test with commit1bc125becaa5("tiny-printf: emit \0 as %c")?Nice hint :)
Thanks,
--
JeromeQuoted 12 lines
Quoted 12 lines
net/tftp.c +6
-4- 0, net_boot_file_size, 0); +/* Write "tsize" + \0 */ +strcpy ((char *)pkt,"tsize" ); + pkt +=6 ;/* 5 chars + 1 null */ + +/* Write the value + \0 */ + pkt +=sprintf ((char *)pkt,"%u" , net_boot_file_size) +1 ;#endif -/* try for more effic. blk size */ - pkt +=sprintf ((char *)pkt,"blksize%c%d%c" , -0 , tftp_block_size_option,0 );Quoted 5 lines
net/tftp.c static void tftp_send(void)
Quoted 5 lines
net/tftp.c +0
-5-/* try for more effic. window size. -* Implemented only for tftp get. -* Don't bother sending if it's 1 -*/ - if (tftp_state == STATE_SEND_RRQ &&
tftp_window_size_option> 1)Quoted 8 lines
net/tftp.c static void tftp_send(void)
Quoted 8 lines
net/tftp.c +6
-2- pkt +=sprintf ((char *)pkt,"windowsize%c%d%c" , -0 , tftp_window_size_option,0 ); +/* Write "blksize" + \0 */ +strcpy ((char *)pkt,"blksize" ); + pkt +=8 ;/* 7 chars + 1 null */ + +/* Write the value + \0 */ + pkt +=sprintf ((char *)pkt,"%d" , tftp_block_size_option)+ 1;
Quoted 2 lines
net/tftp.c static void tftp_send(void) · line 373
Quoted 2 lines
net/tftp.c +2
-0+ + if (tftp_state == STATE_SEND_RRQ &&
tftp_window_size_option> 1) {Quoted 6 lines
net/tftp.c static void tftp_send(void)
Quoted 6 lines
net/tftp.c +6
-0+/* Write "windowsize" + \0 */ +strcpy ((char *)pkt,"windowsize" ); + pkt +=11 ;/* 10 chars + 1 null */ + +/* Write the value + \0 */ + pkt += sprintf((char *)pkt, "%d",
tftp_window_size_option) + 1;Quoted 5 lines
Quoted 4 lines
net/tftp.c +1
-0+ } len = pkt - xp;break ;
Quoted 7 lines · Chencheng Zhang
Hi Sverdin and Jerome,
Quoted 1 line
Do you test with commit
1bc125becaa5("tiny-printf: emit \0 as %c")?Thanks for pointing that out!
I have checked my baseline which is TI u-boot v2025-01. Clearly 1bc125becaa5 is not included. I patched my baseline with1bc125becaa5and it works.You can reject my patch.
OK. Thanks for contributing nevertheless.
Signature · 1 line
Jerome
Quoted 98 lines
BR/Chencheng
On Mon, 23 Mar 2026 at 13:10, Jerome Forissier <[email protected] <mailto:[email protected]>> wrote:
On 23/03/2026 12:58, Sverdlin, Alexander (SI B PRO TI EAC CCP) wrote: > Hi Chencheng, > > On Mon, 2026-03-23 at 12:05 +0100, Chencheng Zhang wrote: >> When formatting the TFTP options (tsize, blksize, windowsize), the >> code previously used sprintf() with embedded null bytes (%c). Because >> sprintf() returns the length excluding the terminating null byte, >> subsequent sprintf() calls overwrote the required null separators. > > I'm not convinced, the analysis explains the observed effect, > >> This caused the options to be concatenated into a single malformed >> string (e.g., "tsize0blksize1468") which breaks strict TFTP servers > ^^ > ... as missing 0 byte between 'e' and '0' is not even at the end... > >> like atftpd, causing them to reply with empty packets or drop the >> connection. >> >> Fix this by explicitly using strcpy() for the option names and manually >> advancing the pointer to ensure the null bytes are preserved. >> >> Signed-off-by: Chencheng Zhang <[email protected] <mailto:[email protected]>> >> --- >> net/tftp.c | 33 +++++++++++++++++++++------------ >> 1 file changed, 21 insertions(+), 12 deletions(-) >> >> diff --git a/net/tftp.c b/net/tftp.c >> index 5f2e0a2bc06..5a8094791ab 100644 >> --- a/net/tftp.c >> +++ b/net/tftp.c >> @@ -355,20 +355,29 @@ static void tftp_send(void) >> debug("send option \"timeout %s\"\n", (char *)pkt); >> pkt += strlen((char *)pkt) + 1; >> #ifdef CONFIG_TFTP_TSIZE >> - pkt += sprintf((char *)pkt, "tsize%c%u%c", > ^^^ > ... of this format string. +1, and NACK. The original code is perfectly fine assuming a compliant snprintf() implementation. > > Are you using USE_TINY_PRINTF or not? > Do you test with commit 1bc125becaa5 ("tiny-printf: emit \0 as %c")? Nice hint :) Thanks, -- Jerome >> - 0, net_boot_file_size, 0); >> + /* Write "tsize" + \0 */ >> + strcpy((char *)pkt, "tsize"); >> + pkt += 6; /* 5 chars + 1 null */ >> + >> + /* Write the value + \0 */ >> + pkt += sprintf((char *)pkt, "%u", net_boot_file_size) + 1; >> #endif >> - /* try for more effic. blk size */ >> - pkt += sprintf((char *)pkt, "blksize%c%d%c", >> - 0, tftp_block_size_option, 0); >> >> - /* try for more effic. window size. >> - * Implemented only for tftp get. >> - * Don't bother sending if it's 1 >> - */ >> - if (tftp_state == STATE_SEND_RRQ && tftp_window_size_option > 1) >> - pkt += sprintf((char *)pkt, "windowsize%c%d%c", >> - 0, tftp_window_size_option, 0); >> + /* Write "blksize" + \0 */ >> + strcpy((char *)pkt, "blksize"); >> + pkt += 8; /* 7 chars + 1 null */ >> + >> + /* Write the value + \0 */ >> + pkt += sprintf((char *)pkt, "%d", tftp_block_size_option) + 1; >> + >> + if (tftp_state == STATE_SEND_RRQ && tftp_window_size_option > 1) { >> + /* Write "windowsize" + \0 */ >> + strcpy((char *)pkt, "windowsize"); >> + pkt += 11; /* 10 chars + 1 null */ >> + >> + /* Write the value + \0 */ >> + pkt += sprintf((char *)pkt, "%d", tftp_window_size_option) + 1; >> + } >> len = pkt - xp; >> break; >> >