Skip to contentSkip to conversation

valgrind problem in sun4u_load_kernel()

Qemu-devel Thomas Huth 3 participants 4 messages Jun 15, 2018 – Aug 8, 2018

lore
Share & tools
lore
#0 lore raw

Hi Mark, hi Artyom,

while using valgrind to fix some issues with the rom_ptr() function today, I noticed that there is one more problem in sun4u_load_kernel():
The kernel_top variable can be used uninitialized in some cases:
If load_elf() fails and the kernel is loaded via load_aout() or load_image_targphys(), the kernel_top variable is never set to a valid value. This could cause some trouble when loading the initrd later. When you've got some spare time, could you please have a look?

Thanks,
Thomas

#1 lore raw
Quoted 9 lines · Thomas Huth

Hi Mark, hi Artyom,

while using valgrind to fix some issues with the rom_ptr() function today, I noticed that there is one more problem in sun4u_load_kernel():
The kernel_top variable can be used uninitialized in some cases:
If load_elf() fails and the kernel is loaded via load_aout() or load_image_targphys(), the kernel_top variable is never set to a valid value. This could cause some trouble when loading the initrd later. When you've got some spare time, could you please have a look?

Hmmm that's an interesting one - I'm not immediately aware of any 64-bit kernels that are a.out rather than ELF, so I wonder if this has been used to run a.out executables on startup? Any thoughts, Artyom?

If this is the case then we can only have a valid kernel_top with an ELF kernel then maybe the following is a good enough solution:

hw/sparc64/sun4u.c +2 -2
index 3975a7b65a..35acf8c96e 100644
@@ -139,7 +139,7 @@ static uint64_t sun4u_load_kernel(const char *kernel_filename,
     unsigned int i;
     long kernel_size;
     uint8_t *ptr;
-    uint64_t kernel_top;
+    uint64_t kernel_top = 0;
 
     linux_boot = (kernel_filename != NULL);
 
@@ -172,7 +172,7 @@ static uint64_t sun4u_load_kernel(const char *kernel_filename,
         }
         /* load initrd above kernel */
         *initrd_size = 0;
-        if (initrd_filename) {
+        if (initrd_filename && kernel_top) {
             *initrd_addr = TARGET_PAGE_ALIGN(kernel_top);
 
             *initrd_size = load_image_targphys(initrd_filename,

ATB,

Mark.

#2 lore raw
Quoted 13 lines · Mark Cave-Ayland
Quoted 9 lines · Thomas Huth

Hi Mark, hi Artyom,

while using valgrind to fix some issues with the rom_ptr() function today, I noticed that there is one more problem in sun4u_load_kernel():
The kernel_top variable can be used uninitialized in some cases:
If load_elf() fails and the kernel is loaded via load_aout() or load_image_targphys(), the kernel_top variable is never set to a valid value. This could cause some trouble when loading the initrd later. When you've got some spare time, could you please have a look?

Quoted 3 lines

Hmmm that's an interesting one - I'm not immediately aware of any 64-bit kernels that are a.out rather than ELF, so I wonder if this has been used to run a.out executables on startup? Any thoughts, Artyom?

I think there are no Linux/SPARC64 kernels in the a.out format. NetBSD should not be a problem either

Quoted 27 lines

If this is the case then we can only have a valid kernel_top with an ELF kernel then maybe the following is a good enough solution:

hw/sparc64/sun4u.c +2 -2
index 3975a7b65a..35acf8c96e 100644
@@ -139,7 +139,7 @@ static uint64_t sun4u_load_kernel(const char
 *kernel_filename,
     unsigned int i;
     long kernel_size;
     uint8_t *ptr;
-    uint64_t kernel_top;
+    uint64_t kernel_top = 0;
 
     linux_boot = (kernel_filename != NULL);
@@ -172,7 +172,7 @@ static uint64_t sun4u_load_kernel(const char
 *kernel_filename,
         }
         /* load initrd above kernel */
         *initrd_size = 0;
-        if (initrd_filename) {
+        if (initrd_filename && kernel_top) {
             *initrd_addr = TARGET_PAGE_ALIGN(kernel_top);
 
Quoted 3 lines

*initrd_size = load_image_targphys(initrd_filename,

Looks good to me.

Signature · 4 lines
Regards,
Artyom Tarasenko

SPARC and PPC PReP under qemu blog: http://tyom.blogspot.com/search/label/qemu
#3 lore raw
Quoted 49 lines · Artyom Tarasenko
Quoted 13 lines · Mark Cave-Ayland
Quoted 9 lines · Thomas Huth

Hi Mark, hi Artyom,

while using valgrind to fix some issues with the rom_ptr() function today, I noticed that there is one more problem in sun4u_load_kernel():
The kernel_top variable can be used uninitialized in some cases:
If load_elf() fails and the kernel is loaded via load_aout() or load_image_targphys(), the kernel_top variable is never set to a valid value. This could cause some trouble when loading the initrd later. When you've got some spare time, could you please have a look?

Quoted 3 lines

Hmmm that's an interesting one - I'm not immediately aware of any 64-bit kernels that are a.out rather than ELF, so I wonder if this has been used to run a.out executables on startup? Any thoughts, Artyom?

I think there are no Linux/SPARC64 kernels in the a.out format. NetBSD should not be a problem either

Quoted 27 lines

If this is the case then we can only have a valid kernel_top with an ELF kernel then maybe the following is a good enough solution:

hw/sparc64/sun4u.c +2 -2
index 3975a7b65a..35acf8c96e 100644
@@ -139,7 +139,7 @@ static uint64_t sun4u_load_kernel(const char
 *kernel_filename,
     unsigned int i;
     long kernel_size;
     uint8_t *ptr;
-    uint64_t kernel_top;
+    uint64_t kernel_top = 0;
 
     linux_boot = (kernel_filename != NULL);
@@ -172,7 +172,7 @@ static uint64_t sun4u_load_kernel(const char
 *kernel_filename,
         }
         /* load initrd above kernel */
         *initrd_size = 0;
-        if (initrd_filename) {
+        if (initrd_filename && kernel_top) {
             *initrd_addr = TARGET_PAGE_ALIGN(kernel_top);
 
Quoted 5 lines
Quoted 3 lines

*initrd_size = load_image_targphys(initrd_filename,

Looks good to me.

Hi Mark,

that looked also fine to me. Could you post it as a proper patch, please?

Thanks,
Thomas