All of lore.kernel.org
 help / color / mirror / Atom feed
From: Darren Kenny <darren.kenny@oracle.com>
To: grub-devel@gnu.org
Cc: darren.kenny@oracle.com
Subject: [PATCH 2/2] kern: Ensure that parser allocated memory is not leaked
Date: Wed, 16 Mar 2022 17:25:05 +0000	[thread overview]
Message-ID: <179d782a3f2b4f177f6396612eb47da1fc7995ed.1647451397.git.darren.kenny@oracle.com> (raw)
In-Reply-To: <a0802259765b7304c38d9b48cad152fcd970ef19.1647451397.git.darren.kenny@oracle.com>

While it would appear unlikely that the memory allocated in *argv in
grub_parser_split_cmdline() would be leaked, we should try ensure that
it doesn't leak by calling grub_free() before we return from
grub_rescue_parse_line().

To avoid a possible double-free, grub_parser_split_cmdline() is being
changed to assign *argv = NULL when we've called grub_free() in the fail
section.

Fixes: CID 96680

Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
---
 grub-core/kern/parser.c        |  2 ++
 grub-core/kern/rescue_parser.c | 10 ++++++++--
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/grub-core/kern/parser.c b/grub-core/kern/parser.c
index 6ab7aa427cca..9b7b31a5162f 100644
--- a/grub-core/kern/parser.c
+++ b/grub-core/kern/parser.c
@@ -298,6 +298,8 @@ grub_parser_split_cmdline (const char *cmdline,
 
  fail:
   grub_free (*argv);
+  *argv = NULL;
+  *argc = 0;
   goto out;
 }
 
diff --git a/grub-core/kern/rescue_parser.c b/grub-core/kern/rescue_parser.c
index 63383669977a..3520aed40668 100644
--- a/grub-core/kern/rescue_parser.c
+++ b/grub-core/kern/rescue_parser.c
@@ -36,10 +36,16 @@ grub_rescue_parse_line (char *line,
 
   if (grub_parser_split_cmdline (line, getline, getline_data, &n, &args)
       || n < 0)
-    return grub_errno;
+    { 
+      grub_free(args);
+      return grub_errno;
+    }
 
   if (n == 0)
-    return GRUB_ERR_NONE;
+    { 
+      grub_free(args);
+      return GRUB_ERR_NONE;
+    }
 
   /* In case of an assignment set the environment accordingly
      instead of calling a function.  */
-- 
2.27.0



  reply	other threads:[~2022-03-16 17:25 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-16 17:25 [PATCH 1/2] grub-mkimage: Only check aarch64 relocations when built for aarch64 Darren Kenny
2022-03-16 17:25 ` Darren Kenny [this message]
2022-03-16 18:19   ` [PATCH 2/2] kern: Ensure that parser allocated memory is not leaked Daniel Kiper
2022-03-16 18:17 ` [PATCH 1/2] grub-mkimage: Only check aarch64 relocations when built for aarch64 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=179d782a3f2b4f177f6396612eb47da1fc7995ed.1647451397.git.darren.kenny@oracle.com \
    --to=darren.kenny@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.