early: Fix possible overlapping data buffer
PATCH 1/1 early: Fix possible overlapping data buffer
Thanks Heiko. This patch hasn't been tested yet. I've modifed the Signed=off-by to make it identical to From. Here is the patch:
-------------------------------------------------------
This patch fixed bugzilla #12965:
https://bugzilla.kernel.org/show_bug.cgi?id=12965
The original code contains some inproper use of sprintf function where a buffer is used both as input string as well as output string. It should remember the written bytes in the previous and use that as the offset for later writing. Also replace sprintf with snprintf.
--- arch/s390/kernel/early.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
arch/s390/kernel/early.c +11 -5
@@ -94,6 +94,7 @@ static noinline __init void create_kerneunsigned int sinitrd_pfn, einitrd_pfn;#endif int response; +int hlen;size_t len;char *savesys_ptr;char defsys_cmd[DEFSYS_CMD_SIZE ];
@@ -124,22 +125,27 @@ static noinline __init void create_kerne end_pfn =PFN_UP (__pa (&_end)); min_size = end_pfn <<2 ; -sprintf (defsys_cmd,"DEFSYS %s 00000-%.5X EW %.5X-%.5X SR %.5X-%.5X" , +snprintf (defsys_cmd,sizeof (defsys_cmd), +"DEFSYS %s 00000-%.5X EW %.5X-%.5X SR %.5X-%.5X" , kernel_nss_name, stext_pfn -1 , stext_pfn, eshared_pfn -1 , eshared_pfn, end_pfn); + defsys_cmd[DEFSYS_CMD_SIZE -1 ] ='\0' ;#ifdef CONFIG_BLK_DEV_INITRD if (INITRD_START &&INITRD_SIZE ) { sinitrd_pfn =PFN_DOWN (__pa (INITRD_START )); einitrd_pfn =PFN_UP (__pa (INITRD_START +INITRD_SIZE )); min_size = einitrd_pfn <<2 ; -sprintf (defsys_cmd,"%s EW %.5X-%.5X" , defsys_cmd, - sinitrd_pfn, einitrd_pfn); + hlen +=snprintf (defsys_cmd,DEFSYS_CMD_SIZE - hlen, +" EW %.5X-%.5X" , defsys_cmd, + sinitrd_pfn, einitrd_pfn); + defsys_cmd[DEFSYS_CMD_SIZE -1 ] ='\0' ; }#endif -sprintf (defsys_cmd,"%s EW MINSIZE=%.7iK PARMREGS=0-13" , - defsys_cmd, min_size); +snprintf (defsys_cmd + hlen,DEFSYS_CMD_SIZE - hlen, +" EW MINSIZE=%.7iK PARMREGS=0-13" , min_size); + defsys_cmd[DEFSYS_CMD_SIZE -1 ] ='\0' ;sprintf (savesys_cmd,"SAVESYS %s \n IPL %s" , kernel_nss_name, kernel_nss_name);
Quoted 3 lines · On Mon, Mar 14, 2011 at 11:25:32AM -0400, [email protected] wrote:
Thanks Heiko. This patch hasn't been tested yet. I've modifed the Signed=off-by to make it identical to From. Here is the patch:
Yes, obviously untested (see below).
Quoted 25 lines
Signed-off-by Chen Liu <[email protected]>--- arch/s390/kernel/early.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)arch/s390/kernel/early.c +3
-1@@ -94,6 +94,7 @@ static noinline __init void create_kerneunsigned int sinitrd_pfn, einitrd_pfn;#endif int response; +int hlen;size_t len;char *savesys_ptr;char defsys_cmd[DEFSYS_CMD_SIZE ];@@ -124,22 +125,27 @@ static noinline __init void create_kerne end_pfn =PFN_UP (__pa (&_end)); min_size = end_pfn <<2 ; -sprintf (defsys_cmd,"DEFSYS %s 00000-%.5X EW %.5X-%.5X SR %.5X-%.5X" , +snprintf (defsys_cmd,sizeof (defsys_cmd), +"DEFSYS %s 00000-%.5X EW %.5X-%.5X SR %.5X-%.5X" , kernel_nss_name, stext_pfn -1 , stext_pfn, eshared_pfn -1 , eshared_pfn, end_pfn);
arch/s390/kernel/early.c static noinline __init void create_kerne · line 139
Quoted 10 lines
arch/s390/kernel/early.c +2
-2+ defsys_cmd[DEFSYS_CMD_SIZE -1 ] ='\0' ;#ifdef CONFIG_BLK_DEV_INITRD if (INITRD_START &&INITRD_SIZE ) { sinitrd_pfn =PFN_DOWN (__pa (INITRD_START )); einitrd_pfn =PFN_UP (__pa (INITRD_START +INITRD_SIZE )); min_size = einitrd_pfn <<2 ; -sprintf (defsys_cmd,"%s EW %.5X-%.5X" , defsys_cmd, - sinitrd_pfn, einitrd_pfn); + hlen += snprintf(defsys_cmd, DEFSYS_CMD_SIZE - hlen,
hlen is unitialized here, since you forgot to save the size in the snprintf statement above.
arch/s390/kernel/early.c static noinline __init void create_kerne · line 140
Quoted 1 line
arch/s390/kernel/early.c +1
-0+ " EW %.5X-%.5X", defsys_cmd,
defsys_cmd is still in the input parameter list. The whole point was to remove it. ;)
Anyway, I fixed it and applied your patch. Thanks!