grub-devel.gnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Snowberg <eric.snowberg@oracle.com>
To: grub-devel@gnu.org
Cc: Eric Snowberg <eric.snowberg@oracle.com>
Subject: [PATCH 07/15] ofdisk: memory corruption fix
Date: Wed, 29 Jun 2016 14:43:20 -0700	[thread overview]
Message-ID: <e0c67c6ff8b48a6e179a086d7e94d08cf388422e.1467232724.git.eric.snowberg@oracle.com> (raw)
In-Reply-To: <cover.1467232724.git.eric.snowberg@oracle.com>
In-Reply-To: <cover.1467232724.git.eric.snowberg@oracle.com>

The goal of this patch is to clean up memory corruption by having
memory allocation take place in a single location, while not causing
any new memory leaks.  In various parts of the code the same path is
called different things, for example it is called curcan, device,
name_dup, can, and devpath,   These are all the same thing.

Within ofdisk_hash_add_real p->devpath it stores a pointer
that later can get freed, causing memory corruption problems.

The following code path is an example of the memory
corruption this patch will fix:

devpath created in grub_ofdisk_open
  it then calls ofdisk_hash_add with devpath
    it then calls ofdisk_hash_add_real with devpath
      ofdisk_hash_add_real saves pointer of devpath
    return
  return
free devpath

dangling pointer/memory corruption with what is stored in ofdisk_hash_add_real

The patch fixes this problem and prevents a memory leak by cleaning up
the new copy when it is no longer needed.

Signed-off-by: Eric Snowberg <eric.snowberg@oracle.com>
---
 grub-core/disk/ieee1275/ofdisk.c |   30 +++++++++++++-----------------
 1 files changed, 13 insertions(+), 17 deletions(-)

diff --git a/grub-core/disk/ieee1275/ofdisk.c b/grub-core/disk/ieee1275/ofdisk.c
index 235c0fe..18d2e95 100644
--- a/grub-core/disk/ieee1275/ofdisk.c
+++ b/grub-core/disk/ieee1275/ofdisk.c
@@ -74,7 +74,7 @@ ofdisk_hash_find (const char *devpath)
 }
 
 static struct ofdisk_hash_ent *
-ofdisk_hash_add_real (char *devpath)
+ofdisk_hash_add_real (const char *devpath)
 {
   struct ofdisk_hash_ent *p;
   struct ofdisk_hash_ent **head = &ofdisk_hash[ofdisk_hash_fn(devpath)];
@@ -85,13 +85,20 @@ ofdisk_hash_add_real (char *devpath)
   if (!p)
     return NULL;
 
-  p->devpath = devpath;
+  p->devpath = grub_strdup (devpath);
+
+  if (!p->devpath)
+    {
+      grub_free (p);
+      return NULL;
+    }
 
   p->grub_devpath = grub_malloc (sizeof ("ieee1275/")
 				 + 2 * grub_strlen (p->devpath));
 
   if (!p->grub_devpath)
     {
+      grub_free (p->devpath);
       grub_free (p);
       return NULL;
     }
@@ -101,6 +108,7 @@ ofdisk_hash_add_real (char *devpath)
       p->open_path = grub_malloc (grub_strlen (p->devpath) + 3);
       if (!p->open_path)
 	{
+          grub_free (p->devpath);
 	  grub_free (p->grub_devpath);
 	  grub_free (p);
 	  return NULL;
@@ -140,7 +148,7 @@ check_string_removable (const char *str)
 }
 
 static struct ofdisk_hash_ent *
-ofdisk_hash_add (char *devpath, char *curcan)
+ofdisk_hash_add (const char *devpath, const char *curcan)
 {
   struct ofdisk_hash_ent *p, *pcan;
 
@@ -160,8 +168,6 @@ ofdisk_hash_add (char *devpath, char *curcan)
   pcan = ofdisk_hash_find (curcan);
   if (!pcan)
     pcan = ofdisk_hash_add_real (curcan);
-  else
-    grub_free (curcan);
 
   if (check_string_removable (devpath) || check_string_removable (curcan))
     pcan->is_removable = 1;
@@ -191,18 +197,7 @@ dev_iterate_real (const char *name, const char *path)
 
   op = ofdisk_hash_find (path);
   if (!op)
-    {
-      char *name_dup = grub_strdup (name);
-      char *can = grub_strdup (path);
-      if (!name_dup || !can)
-	{
-	  grub_errno = GRUB_ERR_NONE;
-	  grub_free (name_dup);
-	  grub_free (can);
-	  return;
-	}
-      op = ofdisk_hash_add (name_dup, can);
-    }
+    op = ofdisk_hash_add (name, path);
   return;
 }
 
@@ -658,6 +653,7 @@ insert_bootpath (void)
       char *device = grub_ieee1275_get_devname (bootpath);
       op = ofdisk_hash_add (device, NULL);
       op->is_boot = 1;
+      grub_free (device);
     }
   grub_free (type);
   grub_free (bootpath);
-- 
1.7.1



  parent reply	other threads:[~2016-06-29 21:45 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-29 21:43 [PATCH 00/15] Add missing SPARC support Eric Snowberg
2016-06-29 21:43 ` [PATCH 01/15] sparc64: fix OF path names for sun4v systems Eric Snowberg
2016-06-29 21:43 ` [PATCH 02/15] sparc64: Add blocklist GPT support for SPARC Eric Snowberg
2017-05-10 22:42   ` Vladimir 'phcoder' Serbinenko
2017-05-10 23:20     ` Eric Snowberg
2017-05-11  0:37       ` Vladimir 'phcoder' Serbinenko
2017-05-11  0:40         ` Vladimir 'phcoder' Serbinenko
2017-05-11  2:05           ` Eric Snowberg
2016-06-29 21:43 ` [PATCH 03/15] grub-install: fix memory leak Eric Snowberg
2016-06-29 21:43 ` [PATCH 04/15] sparc64: Use the correct disk name in core.img Eric Snowberg
2016-06-29 21:43 ` [PATCH 05/15] ieee1275: fix segfault in grub-ofpathname Eric Snowberg
2016-06-29 21:43 ` [PATCH 06/15] ieee1275: add nvme support within ofpath Eric Snowberg
2016-06-29 21:43 ` Eric Snowberg [this message]
2016-06-29 21:43 ` [PATCH 08/15] ofdisk: move open logic Eric Snowberg
2016-06-29 21:43 ` [PATCH 09/15] ieee1275: ofdisk - don't continue to query block-size after we have it Eric Snowberg
2016-06-29 21:43 ` [PATCH 10/15] ofdisk: refactor open logic Eric Snowberg
2016-06-29 21:43 ` [PATCH 11/15] sparc64: boot performance improvements Eric Snowberg
2016-06-29 21:43 ` [PATCH 12/15] ofdisk: only add aliases that exist Eric Snowberg
2016-06-29 21:43 ` [PATCH 13/15] sparc64: add disks that don't have a devalias to the device list Eric Snowberg
2016-06-29 21:43 ` [PATCH 14/15] parser: Remove escape from the state transitions Eric Snowberg
2016-06-29 21:43 ` [PATCH 15/15] sparc64: ignore hypervisor reboot memory block device Eric Snowberg
2016-07-01  6:52 ` [PATCH 00/15] Add missing SPARC support Daniel Kiper
2016-07-26 10:24   ` Daniel Kiper

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e0c67c6ff8b48a6e179a086d7e94d08cf388422e.1467232724.git.eric.snowberg@oracle.com \
    --to=eric.snowberg@oracle.com \
    --cc=grub-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).