Skip to contentSkip to conversation

bsd_user: Fix potential null pointer dereference

PATCH Superseded Stefan Weil 3 participants 6 messages Nov 21, 2011 – Dec 11, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bsd_user: Fix potential null pointer dereference

This bug was spotted by cppcheck.

Using g_try_malloc0 (as does the linux-user code) fixes this.

---
 bsd-user/elfload.c |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)
bsd-user/elfload.c +2 -3
index 1ef1f97..1288884 100644
@@ -641,8 +641,7 @@ static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
                 offset = p % TARGET_PAGE_SIZE;
                 pag = (char *)page[p/TARGET_PAGE_SIZE];
                 if (!pag) {
-                    pag = (char *)malloc(TARGET_PAGE_SIZE);
-                    memset(pag, 0, TARGET_PAGE_SIZE);
+                    pag = g_try_malloc0(TARGET_PAGE_SIZE);
                     page[p/TARGET_PAGE_SIZE] = pag;
                     if (!pag)
                         return 0;
@@ -696,7 +695,7 @@ static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
             info->rss++;
             /* FIXME - check return value of memcpy_to_target() for failure */
             memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
-            free(bprm->page[i]);
+            g_free(bprm->page[i]);
         }
         stack_base += TARGET_PAGE_SIZE;
     }
Signature · 1 line
1.7.2.5
#1 lore raw
Quoted 5 lines · Stefan Weil

This bug was spotted by cppcheck.

Using g_try_malloc0 (as does the linux-user code) fixes this.

Oh look, another bug in bsd-user it wouldn't have if it shared code with linux-user :-)

Quoted 18 lines

 bsd-user/elfload.c |    5 ++---

 1 files changed, 2 insertions(+), 3 deletions(-)
bsd-user/elfload.c +1 -2
index 1ef1f97..1288884 100644
@@ -641,8 +641,7 @@ static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
                 offset = p % TARGET_PAGE_SIZE;
                 pag = (char *)page[p/TARGET_PAGE_SIZE];
                 if (!pag) {
-                    pag = (char *)malloc(TARGET_PAGE_SIZE);
-                    memset(pag, 0, TARGET_PAGE_SIZE);
+                    pag = g_try_malloc0(TARGET_PAGE_SIZE);
                     page[p/TARGET_PAGE_SIZE] = pag;
                     if (!pag)
                         return 0;

bsd-user/elfload.c static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm, · line 701

Quoted 8 lines
bsd-user/elfload.c +1 -1
@@ -696,7 +695,7 @@ static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
             info->rss++;
             /* FIXME - check return value of memcpy_to_target() for failure */
             memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
-            free(bprm->page[i]);
+            g_free(bprm->page[i]);
         }
         stack_base += TARGET_PAGE_SIZE;
     }

You've missed the s/free/g_free/ in bsd-user/bsdload.c. Compare commit 7dd47667b.

-- PMM

#2 lore raw

PATCH v2 1/1 bsd_user: Fix potential null pointer dereference

This bug was spotted by cppcheck.

Using g_try_malloc0 (as does the linux-user code) fixes this.

v2:
Use g_free in bsdload.c, too. Thanks to Peter Maydell for this hint.

---
 bsd-user/bsdload.c |    2 +-
 bsd-user/elfload.c |    5 ++---
 2 files changed, 3 insertions(+), 4 deletions(-)
bsd-user/bsdload.c +1 -1
index 6d9bb6f..2abc713 100644
@@ -196,7 +196,7 @@ int loader_exec(const char * filename, char ** argv, char ** envp,
 
     /* Something went wrong, return the inode and free the argument pages*/
     for (i=0 ; i<MAX_ARG_PAGES ; i++) {
-        free(bprm.page[i]);
+        g_free(bprm.page[i]);
     }
     return(retval);
 }
bsd-user/elfload.c +2 -3
index 1ef1f97..1288884 100644
@@ -641,8 +641,7 @@ static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
                 offset = p % TARGET_PAGE_SIZE;
                 pag = (char *)page[p/TARGET_PAGE_SIZE];
                 if (!pag) {
-                    pag = (char *)malloc(TARGET_PAGE_SIZE);
-                    memset(pag, 0, TARGET_PAGE_SIZE);
+                    pag = g_try_malloc0(TARGET_PAGE_SIZE);
                     page[p/TARGET_PAGE_SIZE] = pag;
                     if (!pag)
                         return 0;
@@ -696,7 +695,7 @@ static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
             info->rss++;
             /* FIXME - check return value of memcpy_to_target() for failure */
             memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
-            free(bprm->page[i]);
+            g_free(bprm->page[i]);
         }
         stack_base += TARGET_PAGE_SIZE;
     }
Signature · 1 line
1.7.2.5
#3 lore raw

Am 21.11.2011 21:06, schrieb Stefan Weil:

Quoted 42 lines

This bug was spotted by cppcheck.

Using g_try_malloc0 (as does the linux-user code) fixes this.

v2:
Use g_free in bsdload.c, too. Thanks to Peter Maydell for this hint.

---
  bsd-user/bsdload.c |    2 +-
  bsd-user/elfload.c |    5 ++---
  2 files changed, 3 insertions(+), 4 deletions(-)
bsd-user/bsdload.c +1 -1
index 6d9bb6f..2abc713 100644
@@ -196,7 +196,7 @@ int loader_exec(const char * filename, char ** argv, char ** envp,
 
      /* Something went wrong, return the inode and free the argument pages*/
      for (i=0 ; i<MAX_ARG_PAGES ; i++) {
-        free(bprm.page[i]);
+        g_free(bprm.page[i]);
      }
      return(retval);
  }
bsd-user/elfload.c +1 -2
index 1ef1f97..1288884 100644
@@ -641,8 +641,7 @@ static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
                  offset = p % TARGET_PAGE_SIZE;
                  pag = (char *)page[p/TARGET_PAGE_SIZE];
                  if (!pag) {
-                    pag = (char *)malloc(TARGET_PAGE_SIZE);
-                    memset(pag, 0, TARGET_PAGE_SIZE);
+                    pag = g_try_malloc0(TARGET_PAGE_SIZE);
                      page[p/TARGET_PAGE_SIZE] = pag;
                      if (!pag)
                          return 0;

bsd-user/elfload.c static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm, · line 701

Quoted 8 lines
bsd-user/elfload.c +1 -1
@@ -696,7 +695,7 @@ static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
              info->rss++;
              /* FIXME - check return value of memcpy_to_target() for failure */
              memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
-            free(bprm->page[i]);
+            g_free(bprm->page[i]);
          }
          stack_base += TARGET_PAGE_SIZE;
      }

Ping? Can this be fixed in QEMU 1.0?

Applied
#4 lore raw
In reply to Stefan Weil

Thanks, applied.

Quoted 53 lines · Stefan Weil

This bug was spotted by cppcheck.

Using g_try_malloc0 (as does the linux-user code) fixes this.

v2:
Use g_free in bsdload.c, too. Thanks to Peter Maydell for this hint.

---
 bsd-user/bsdload.c |    2 +-
 bsd-user/elfload.c |    5 ++---

 2 files changed, 3 insertions(+), 4 deletions(-)
bsd-user/bsdload.c +1 -1
index 6d9bb6f..2abc713 100644
@@ -196,7 +196,7 @@ int loader_exec(const char * filename, char ** argv, char ** envp,
 
     /* Something went wrong, return the inode and free the argument pages*/
     for (i=0 ; i<MAX_ARG_PAGES ; i++) {
-        free(bprm.page[i]);
+        g_free(bprm.page[i]);
     }
     return(retval);
  }
bsd-user/elfload.c +2 -4
index 1ef1f97..1288884 100644
@@ -641,8 +641,7 @@ static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
                 offset = p % TARGET_PAGE_SIZE;
                 pag = (char *)page[p/TARGET_PAGE_SIZE];
                 if (!pag) {
-                    pag = (char *)malloc(TARGET_PAGE_SIZE);
-                    memset(pag, 0, TARGET_PAGE_SIZE);
+                    pag = g_try_malloc0(TARGET_PAGE_SIZE);
                     page[p/TARGET_PAGE_SIZE] = pag;
                     if (!pag)
                         return 0;
@@ -696,7 +695,7 @@ static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
             info->rss++;
             /* FIXME - check return value of memcpy_to_target() for failure */
             memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
-            free(bprm->page[i]);
+            g_free(bprm->page[i]);
         }
         stack_base += TARGET_PAGE_SIZE;
     }
--

1.7.2.5

#5 lore raw
In reply to Peter Maydell

Am 21.11.2011 20:49, schrieb Peter Maydell:

Quoted 8 lines · Stefan Weil
Quoted 5 lines

This bug was spotted by cppcheck.

Using g_try_malloc0 (as does the linux-user code) fixes this.

Oh look, another bug in bsd-user it wouldn't have if it shared code with linux-user :-)

I had read your previous mail and was waiting for your comment when I sent my patch :-)

Nobody would mind if Linux and BSD shared more code. So somebody simply has to do the necessary changes.

Quoted 29 lines
Quoted 18 lines
  bsd-user/elfload.c |    5 ++---
  1 files changed, 2 insertions(+), 3 deletions(-)
bsd-user/elfload.c +1 -2
index 1ef1f97..1288884 100644
@@ -641,8 +641,7 @@ static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
                 offset = p % TARGET_PAGE_SIZE;
                 pag = (char *)page[p/TARGET_PAGE_SIZE];
                 if (!pag) {
-                    pag = (char *)malloc(TARGET_PAGE_SIZE);
-                    memset(pag, 0, TARGET_PAGE_SIZE);
+                    pag = g_try_malloc0(TARGET_PAGE_SIZE);
                     page[p/TARGET_PAGE_SIZE] = pag;
                     if (!pag)
                         return 0;

bsd-user/elfload.c static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm, · line 701

Quoted 8 lines
bsd-user/elfload.c +1 -1
@@ -696,7 +695,7 @@ static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
             info->rss++;
             /* FIXME - check return value of memcpy_to_target() for failure */
             memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
-            free(bprm->page[i]);
+            g_free(bprm->page[i]);
         }
         stack_base += TARGET_PAGE_SIZE;
     }

You've missed the s/free/g_free/ in bsd-user/bsdload.c. Compare commit 7dd47667b.

Quoted 1 line

-- PMM

I fixed this in new patch, thank you.

Regards,
Stefan Weil