qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Peter Maydell <peter.maydell@linaro.org>
To: Jean-Christophe Dubois <jcd@tribudubois.net>
Cc: "QEMU Developers" <qemu-devel@nongnu.org>,
	"Marcin Krzemiński" <mar.krzeminski@gmail.com>
Subject: Re: [Qemu-devel] [PATCH] [m25p80] Abort in case we overrun the internal data buffer
Date: Thu, 5 Jan 2017 18:38:56 +0000	[thread overview]
Message-ID: <CAFEAcA91iJ_8MeKmMp7_fDfdc+aKLHHs4--CyYh=0FQP+SmxLw@mail.gmail.com> (raw)
In-Reply-To: <20170103211705.27876-1-jcd@tribudubois.net>

On 3 January 2017 at 21:17, Jean-Christophe Dubois <jcd@tribudubois.net> wrote:
> Signed-off-by: Jean-Christophe Dubois <jcd@tribudubois.net>
> ---
>  hw/block/m25p80.c | 19 +++++++++++++++++--
>  1 file changed, 17 insertions(+), 2 deletions(-)
>
> diff --git a/hw/block/m25p80.c b/hw/block/m25p80.c
> index d29ff4c..6c374cf 100644
> --- a/hw/block/m25p80.c
> +++ b/hw/block/m25p80.c
> @@ -28,6 +28,7 @@
>  #include "hw/ssi/ssi.h"
>  #include "qemu/bitops.h"
>  #include "qemu/log.h"
> +#include "qemu/error-report.h"
>  #include "qapi/error.h"
>
>  #ifndef M25P80_ERR_DEBUG
> @@ -376,6 +377,8 @@ typedef enum {
>      MAN_GENERIC,
>  } Manufacturer;
>
> +#define _INTERNAL_DATA_SIZE 16
> +

Don't use leading underscores, please.

>  typedef struct Flash {
>      SSISlave parent_obj;
>
> @@ -386,7 +389,7 @@ typedef struct Flash {
>      int page_size;
>
>      uint8_t state;
> -    uint8_t data[16];
> +    uint8_t data[_INTERNAL_DATA_SIZE];
>      uint32_t len;
>      uint32_t pos;
>      uint8_t needed_bytes;
> @@ -1114,6 +1117,12 @@ static uint32_t m25p80_transfer8(SSISlave *ss, uint32_t tx)
>
>      case STATE_COLLECTING_DATA:
>      case STATE_COLLECTING_VAR_LEN_DATA:
> +
> +        if (s->len >= _INTERNAL_DATA_SIZE) {
> +            error_report("Bug - Write overrun internal data buffer");
> +            abort();
> +        }
> +
>          s->data[s->len] = (uint8_t)tx;
>          s->len++;
>
> @@ -1123,6 +1132,12 @@ static uint32_t m25p80_transfer8(SSISlave *ss, uint32_t tx)
>          break;
>
>      case STATE_READING_DATA:
> +
> +        if (s->pos >= _INTERNAL_DATA_SIZE) {
> +            error_report("Bug - Read overrun internal data buffer");
> +            abort();
> +        }
> +

If these are "can't happen unless some other part of QEMU
is buggy" cases, then we can just assert():

    assert(s->pos < ARRAY_SIZE(s->data));

A comment about what kind of other part of QEMU might be buggy
if the assertion fires would also be helpful for future readers.

(If they're "could happen if the guest does something wrong"
cases, we shouldn't just abort(), but if I'm reading the previous
mail thread correctly, that's not the situation here.)

>          r = s->data[s->pos];
>          s->pos++;
>          if (s->pos == s->len) {
> @@ -1195,7 +1210,7 @@ static const VMStateDescription vmstate_m25p80 = {
>      .pre_save = m25p80_pre_save,
>      .fields = (VMStateField[]) {
>          VMSTATE_UINT8(state, Flash),
> -        VMSTATE_UINT8_ARRAY(data, Flash, 16),
> +        VMSTATE_UINT8_ARRAY(data, Flash, _INTERNAL_DATA_SIZE),
>          VMSTATE_UINT32(len, Flash),
>          VMSTATE_UINT32(pos, Flash),
>          VMSTATE_UINT8(needed_bytes, Flash),
> --
> 2.9.3

thanks
-- PMM

  reply	other threads:[~2017-01-05 18:39 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-03 21:17 [Qemu-devel] [PATCH] [m25p80] Abort in case we overrun the internal data buffer Jean-Christophe Dubois
2017-01-05 18:38 ` Peter Maydell [this message]
2017-01-05 20:04   ` mar.krzeminski
2017-01-05 20:18     ` Jean-Christophe DUBOIS
2017-01-05 20:51       ` Peter Maydell
2017-01-05 21:39         ` Jean-Christophe DUBOIS
2017-01-06 10:18           ` Peter Maydell
2017-01-06 18:20             ` Jean-Christophe DUBOIS

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='CAFEAcA91iJ_8MeKmMp7_fDfdc+aKLHHs4--CyYh=0FQP+SmxLw@mail.gmail.com' \
    --to=peter.maydell@linaro.org \
    --cc=jcd@tribudubois.net \
    --cc=mar.krzeminski@gmail.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 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).