All of lore.kernel.org
 help / color / mirror / Atom feed
From: "mlevitsk@redhat.com" <mlevitsk@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>, qemu-devel@nongnu.org
Cc: Mauro Matteo Cascella <mcascell@redhat.com>,
	Jordy Zomer <jordyzomer@google.com>
Subject: Re: [PATCH] system: fix buffer overflow in tdx_parse_panic_message
Date: Wed, 23 Sep 2026 11:32:24 -0400	[thread overview]
Message-ID: <8fd04ba4a6076feca2c2fc02c56dde56ab4dc0fa.camel@redhat.com> (raw)
In-Reply-To: <d92766bd-b542-477f-9caa-c09978080858@redhat.com>

On Wed, 2026-09-23 at 16:36 +0200, Paolo Bonzini wrote:
> On 9/23/26 02:45, Maxim Levitsky wrote:
> > tdx_parse_panic_message has code that detects if the guest supplied
> > message contains some unprintable characters and in this case prints a hex dump.
> > 
> > The code has a bug: it allocates 3 bytes for 'sprintf("%02x ")',
> > but sprintf in this case actually writes 4 bytes, because it adds the NULL
> > terminator.
> > 
> > To fix this, just use qemu_hexdump_line instead.
> > This patch was only compile tested.
> > 
> > Reported-by: Jordy Zomer <jordyzomer@google.com>
> > Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3905
> > Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
> > ---
> >   system/runstate.c | 35 +++++++----------------------------
> >   1 file changed, 7 insertions(+), 28 deletions(-)
> > 
> > diff --git a/system/runstate.c b/system/runstate.c
> > index d3e64d2b6258..ebf09b192b4e 100644
> > --- a/system/runstate.c
> > +++ b/system/runstate.c
> > @@ -50,6 +50,7 @@
> >   #include "qemu/sockets.h"
> >   #include "qemu/timer.h"
> >   #include "qemu/thread.h"
> > +#include "qemu/cutils.h"
> >   #include "qom/object.h"
> >   #include "qom/object_interfaces.h"
> >   #include "system/cpu-timers.h"
> > @@ -760,10 +761,9 @@ static void qemu_system_wakeup(void)
> >       }
> >   }
> >   
> > -static char *tdx_parse_panic_message(char *message)
> > +static GString *tdx_parse_panic_message(char *message)
> >   {
> >       bool printable = false;
> > -    char *buf = NULL;
> >       int len = 0, i;
> >   
> >       /*
> > @@ -784,32 +784,11 @@ static char *tdx_parse_panic_message(char *message)
> >           }
> >       }
> >   
> > -    if (len == 0) {
> > -        buf = g_malloc(1);
> > -        buf[0] = '\0';
> > +    if (printable) {
> > +        return g_string_new(message);
> >       } else {
> > -        if (!printable) {
> > -            /* 3 = length of "%02x " */
> > -            buf = g_malloc(len * 3);
> > -            for (i = 0; i < len; i++) {
> > -                if (message[i] == '\0') {
> > -                    break;
> > -                } else {
> > -                    sprintf(buf + 3 * i, "%02x ", message[i]);
> > -                }
> > -            }
> > -            if (i > 0) {
> > -                /* replace the last ' '(space) to NULL */
> > -                buf[i * 3 - 1] = '\0';
> > -            } else {
> > -                buf[0] = '\0';
> > -            }
> > -        } else {
> > -            buf = g_strdup(message);
> > -        }
> > +        return qemu_hexdump_line(NULL, message, len, 1, 0);
> >       }
> > -
> > -    return buf;
> >   }
> >   
> >   void qemu_system_guest_panicked(GuestPanicInformation *info)
> > @@ -854,11 +833,11 @@ void qemu_system_guest_panicked(GuestPanicInformation *info)
> >                             info->u.s390.psw_mask,
> >                             info->u.s390.psw_addr);
> >           } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_TDX) {
> > -            char *message = tdx_parse_panic_message(info->u.tdx.message);
> > +            GString *message = tdx_parse_panic_message(info->u.tdx.message);
> >               qemu_log_mask(LOG_GUEST_ERROR,
> >                             "\nTDX guest reports fatal error."
> >                             " error code: 0x%" PRIx32 " error message:\"%s\"\n",
> > -                          info->u.tdx.error_code, message);
> > +                          info->u.tdx.error_code, message->str);
> >               g_free(message);
> 
> Freeing the GString* leaks message->str.
> 
> The easiest way to do it, which I have squashed on top of your patch, is
> 
>     g_autoptr(GString) message = ...
> 
> and dropping the g_free(message) line.  See for example sdbus_write_dump().

Oops, thanks for fixing this, I thought about that g_free(), but I guess not enough.

Best regards,
	Maxim Levitsky

> 
> Paolo

      reply	other threads:[~2026-09-23 15:32 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-23  0:45 [PATCH] system: fix buffer overflow in tdx_parse_panic_message Maxim Levitsky
2026-09-23 14:36 ` Paolo Bonzini
2026-09-23 15:32   ` mlevitsk [this message]

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=8fd04ba4a6076feca2c2fc02c56dde56ab4dc0fa.camel@redhat.com \
    --to=mlevitsk@redhat.com \
    --cc=jordyzomer@google.com \
    --cc=mcascell@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.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.