diff mbox

[v1,03/10] xen/pt: Check if reg->init function sets the 'data' past the reg->size

Message ID 1435866681-18468-4-git-send-email-konrad.wilk@oracle.com
State New
Headers show

Commit Message

Konrad Rzeszutek Wilk July 2, 2015, 7:51 p.m. UTC
It should never happen, but in case it does (an developer adds
a new register and the 'init_val' expands past the register
size) we want to report. The code will only write up to
reg->size so there is no runtime danger of the register spilling
across other ones - however to catch this sort of thing
we still return an error.

Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
---
 hw/xen/xen_pt_config_init.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

Comments

Stefano Stabellini July 17, 2015, 4:03 p.m. UTC | #1
On Thu, 2 Jul 2015, Konrad Rzeszutek Wilk wrote:
> It should never happen, but in case it does (an developer adds
> a new register and the 'init_val' expands past the register
> size) we want to report. The code will only write up to
> reg->size so there is no runtime danger of the register spilling
> across other ones - however to catch this sort of thing
> we still return an error.
> 
> Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
> ---
>  hw/xen/xen_pt_config_init.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/hw/xen/xen_pt_config_init.c b/hw/xen/xen_pt_config_init.c
> index 3938afd..09309ba 100644
> --- a/hw/xen/xen_pt_config_init.c
> +++ b/hw/xen/xen_pt_config_init.c
> @@ -1904,9 +1904,15 @@ static int xen_pt_config_reg_init(XenPCIPassthroughState *s,
>          } else
>              val = data;
>  
> +        if (val & ~size_mask) {
> +            XEN_PT_ERR(&s->dev,"Offset 0x%04x:0x%04x expands past register size(%d)!\n",
> +                       offset, val, reg->size);
> +            g_free(reg_entry);
> +            return -ENXIO;
> +        }

If we worry about changes to init_val, wouldn't it be better to add
QEMU_BUILD_BUG_ON(data & ~size_mask)?


>          /* This could be just pci_set_long as we don't modify the bits
> -         * past reg->size, but in case this routine is run in parallel
> -         * we do not want to over-write other registers. */
> +         * past reg->size, but in case this routine is run in parallel or the
> +         * init value is larger, we do not want to over-write registers. */
>          switch (reg->size) {
>          case 1: pci_set_byte(s->dev.config + offset, (uint8_t)val); break;
>          case 2: pci_set_word(s->dev.config + offset, (uint16_t)val); break;
> -- 
> 2.1.0
>
Konrad Rzeszutek Wilk July 17, 2015, 4:47 p.m. UTC | #2
On Fri, Jul 17, 2015 at 05:03:44PM +0100, Stefano Stabellini wrote:
> On Thu, 2 Jul 2015, Konrad Rzeszutek Wilk wrote:
> > It should never happen, but in case it does (an developer adds
> > a new register and the 'init_val' expands past the register
> > size) we want to report. The code will only write up to
> > reg->size so there is no runtime danger of the register spilling
> > across other ones - however to catch this sort of thing
> > we still return an error.
> > 
> > Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
> > ---
> >  hw/xen/xen_pt_config_init.c | 10 ++++++++--
> >  1 file changed, 8 insertions(+), 2 deletions(-)
> > 
> > diff --git a/hw/xen/xen_pt_config_init.c b/hw/xen/xen_pt_config_init.c
> > index 3938afd..09309ba 100644
> > --- a/hw/xen/xen_pt_config_init.c
> > +++ b/hw/xen/xen_pt_config_init.c
> > @@ -1904,9 +1904,15 @@ static int xen_pt_config_reg_init(XenPCIPassthroughState *s,
> >          } else
> >              val = data;
> >  
> > +        if (val & ~size_mask) {
> > +            XEN_PT_ERR(&s->dev,"Offset 0x%04x:0x%04x expands past register size(%d)!\n",
> > +                       offset, val, reg->size);
> > +            g_free(reg_entry);
> > +            return -ENXIO;
> > +        }
> 
> If we worry about changes to init_val, wouldn't it be better to add
> QEMU_BUILD_BUG_ON(data & ~size_mask)?

Duh. Yes :-)
> 
> 
> >          /* This could be just pci_set_long as we don't modify the bits
> > -         * past reg->size, but in case this routine is run in parallel
> > -         * we do not want to over-write other registers. */
> > +         * past reg->size, but in case this routine is run in parallel or the
> > +         * init value is larger, we do not want to over-write registers. */
> >          switch (reg->size) {
> >          case 1: pci_set_byte(s->dev.config + offset, (uint8_t)val); break;
> >          case 2: pci_set_word(s->dev.config + offset, (uint16_t)val); break;
> > -- 
> > 2.1.0
> >
Konrad Rzeszutek Wilk Aug. 14, 2015, 8:42 p.m. UTC | #3
On Fri, Jul 17, 2015 at 05:03:44PM +0100, Stefano Stabellini wrote:
> On Thu, 2 Jul 2015, Konrad Rzeszutek Wilk wrote:
> > It should never happen, but in case it does (an developer adds
> > a new register and the 'init_val' expands past the register
> > size) we want to report. The code will only write up to
> > reg->size so there is no runtime danger of the register spilling
> > across other ones - however to catch this sort of thing
> > we still return an error.
> > 
> > Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
> > ---
> >  hw/xen/xen_pt_config_init.c | 10 ++++++++--
> >  1 file changed, 8 insertions(+), 2 deletions(-)
> > 
> > diff --git a/hw/xen/xen_pt_config_init.c b/hw/xen/xen_pt_config_init.c
> > index 3938afd..09309ba 100644
> > --- a/hw/xen/xen_pt_config_init.c
> > +++ b/hw/xen/xen_pt_config_init.c
> > @@ -1904,9 +1904,15 @@ static int xen_pt_config_reg_init(XenPCIPassthroughState *s,
> >          } else
> >              val = data;
> >  
> > +        if (val & ~size_mask) {
> > +            XEN_PT_ERR(&s->dev,"Offset 0x%04x:0x%04x expands past register size(%d)!\n",
> > +                       offset, val, reg->size);
> > +            g_free(reg_entry);
> > +            return -ENXIO;
> > +        }
> 
> If we worry about changes to init_val, wouldn't it be better to add
> QEMU_BUILD_BUG_ON(data & ~size_mask)?

I couldnt' figure out how to make that work nicely.

The QEMU_BUILD_BUG_ON look to be build time - not run-time.

Which means that doing:
	for (i = 0; i < grp_entries; i++)
	{
		entries = grp_entries[i]...
		for (j = 0; j < entries; j++)
			QEMU_BUILD_BUG_ON(entries[j].init_val & ~size_mask)
	}

is not something I can image the compiler working with?

> 
> 
> >          /* This could be just pci_set_long as we don't modify the bits
> > -         * past reg->size, but in case this routine is run in parallel
> > -         * we do not want to over-write other registers. */
> > +         * past reg->size, but in case this routine is run in parallel or the
> > +         * init value is larger, we do not want to over-write registers. */
> >          switch (reg->size) {
> >          case 1: pci_set_byte(s->dev.config + offset, (uint8_t)val); break;
> >          case 2: pci_set_word(s->dev.config + offset, (uint16_t)val); break;
> > -- 
> > 2.1.0
> >
diff mbox

Patch

diff --git a/hw/xen/xen_pt_config_init.c b/hw/xen/xen_pt_config_init.c
index 3938afd..09309ba 100644
--- a/hw/xen/xen_pt_config_init.c
+++ b/hw/xen/xen_pt_config_init.c
@@ -1904,9 +1904,15 @@  static int xen_pt_config_reg_init(XenPCIPassthroughState *s,
         } else
             val = data;
 
+        if (val & ~size_mask) {
+            XEN_PT_ERR(&s->dev,"Offset 0x%04x:0x%04x expands past register size(%d)!\n",
+                       offset, val, reg->size);
+            g_free(reg_entry);
+            return -ENXIO;
+        }
         /* This could be just pci_set_long as we don't modify the bits
-         * past reg->size, but in case this routine is run in parallel
-         * we do not want to over-write other registers. */
+         * past reg->size, but in case this routine is run in parallel or the
+         * init value is larger, we do not want to over-write registers. */
         switch (reg->size) {
         case 1: pci_set_byte(s->dev.config + offset, (uint8_t)val); break;
         case 2: pci_set_word(s->dev.config + offset, (uint16_t)val); break;