Skip to contentSkip to conversation

memory corruption in HYPERVISOR_physdev_op()

DISCUSSION Dan Carpenter 3 participants 13 messages Sep 14, 2012 – Oct 16, 2012 virtio core

lore
Share & tools
lore
#0 lore raw

Hi Jeremy,

My static analyzer complains about potential memory corruption in HYPERVISOR_physdev_op()

arch/x86/include/asm/xen/hypercall.h

   389  static inline int
   390  HYPERVISOR_physdev_op(int cmd, void *arg)
   391  {
   392          int rc = _hypercall2(int, physdev_op, cmd, arg);
   393          if (unlikely(rc == -ENOSYS)) {
   394                  struct physdev_op op;
   395                  op.cmd = cmd;
   396                  memcpy(&op.u, arg, sizeof(op.u));
   397                  rc = _hypercall1(int, physdev_op_compat, &op);
   398                  memcpy(arg, &op.u, sizeof(op.u));
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Some of the arg buffers are not as large as sizeof(op.u) which is either 12 or 16 depending on the size of longs in struct physdev_apic.

399 }
400 return rc;
401 }

One example of this is in xen_initdom_restore_msi_irqs().

arch/x86/pci/xen.c
337 struct physdev_pci_device restore_ext;
338

   339                  restore_ext.seg = pci_domain_nr(dev->bus);
   340                  restore_ext.bus = dev->bus->number;
   341                  restore_ext.devfn = dev->devfn;
   342                  ret = HYPERVISOR_physdev_op(PHYSDEVOP_restore_msi_ext,
   343                                          &restore_ext);
                                                ^^^^^^^^^^^^

There are only 4 bytes here.

344 if (ret == -ENOSYS)

                            ^^^^^^^^^^^^^^

If we hit this condition, we have corrupted some memory.

   345                          pci_seg_supported = false;

regards,
dan carpenter

#1 lore raw
Quoted 1 line · Dan Carpenter

Hi Jeremy,

Jeremy doesn't work on Xen much any more. Adding Konrad and the xen-devel@ list.

Quoted 15 lines

My static analyzer complains about potential memory corruption in HYPERVISOR_physdev_op()

arch/x86/include/asm/xen/hypercall.h

   389  static inline int
   390  HYPERVISOR_physdev_op(int cmd, void *arg)
   391  {
   392          int rc = _hypercall2(int, physdev_op, cmd, arg);
   393          if (unlikely(rc == -ENOSYS)) {
   394                  struct physdev_op op;
   395                  op.cmd = cmd;
   396                  memcpy(&op.u, arg, sizeof(op.u));
   397                  rc = _hypercall1(int, physdev_op_compat, &op);
   398                  memcpy(arg, &op.u, sizeof(op.u));
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 2 lines

Some of the arg buffers are not as large as sizeof(op.u) which is either 12 or 16 depending on the size of longs in struct physdev_apic.

Nasty!

Quoted 16 lines
   399          }
   400          return rc;
   401  }

One example of this is in xen_initdom_restore_msi_irqs().

arch/x86/pci/xen.c

   337                  struct physdev_pci_device restore_ext;
   338  
   339                  restore_ext.seg = pci_domain_nr(dev->bus);
   340                  restore_ext.bus = dev->bus->number;
   341                  restore_ext.devfn = dev->devfn;
   342                  ret = HYPERVISOR_physdev_op(PHYSDEVOP_restore_msi_ext,
   343                                          &restore_ext);
                                                ^^^^^^^^^^^^
Quoted 5 lines

There are only 4 bytes here.

   344                  if (ret == -ENOSYS)
                            ^^^^^^^^^^^^^^

If we hit this condition, we have corrupted some memory.

I can see the memory corruption but how does it relate to ret == -ENOSYS?

Quoted 10 lines
   345                          pci_seg_supported = false;

regards,
dan carpenter
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Signature · 5 lines
Ian Campbell
Current Noise: Therapy? - Femtex

Riffle West Virginia is so small that the Boy Scout had to double as the
town drunk.
#12 lore raw
In reply to Dan Carpenter
Quoted 1 line · Dan Carpenter

Hi Jeremy,

Jeremy doesn't work on Xen much any more. Adding Konrad and the xen-devel@ list.

Quoted 15 lines

My static analyzer complains about potential memory corruption in HYPERVISOR_physdev_op()

arch/x86/include/asm/xen/hypercall.h

   389  static inline int
   390  HYPERVISOR_physdev_op(int cmd, void *arg)
   391  {
   392          int rc = _hypercall2(int, physdev_op, cmd, arg);
   393          if (unlikely(rc == -ENOSYS)) {
   394                  struct physdev_op op;
   395                  op.cmd = cmd;
   396                  memcpy(&op.u, arg, sizeof(op.u));
   397                  rc = _hypercall1(int, physdev_op_compat, &op);
   398                  memcpy(arg, &op.u, sizeof(op.u));
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 2 lines

Some of the arg buffers are not as large as sizeof(op.u) which is either 12 or 16 depending on the size of longs in struct physdev_apic.

Nasty!

Quoted 16 lines
   399          }
   400          return rc;
   401  }

One example of this is in xen_initdom_restore_msi_irqs().

arch/x86/pci/xen.c

   337                  struct physdev_pci_device restore_ext;
   338  
   339                  restore_ext.seg = pci_domain_nr(dev->bus);
   340                  restore_ext.bus = dev->bus->number;
   341                  restore_ext.devfn = dev->devfn;
   342                  ret = HYPERVISOR_physdev_op(PHYSDEVOP_restore_msi_ext,
   343                                          &restore_ext);
                                                ^^^^^^^^^^^^
Quoted 5 lines

There are only 4 bytes here.

   344                  if (ret == -ENOSYS)
                            ^^^^^^^^^^^^^^

If we hit this condition, we have corrupted some memory.

I can see the memory corruption but how does it relate to ret == -ENOSYS?

Quoted 10 lines
   345                          pci_seg_supported = false;

regards,
dan carpenter
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Signature · 5 lines
Ian Campbell
Current Noise: Therapy? - Femtex

Riffle West Virginia is so small that the Boy Scout had to double as the
town drunk.
#2 lore raw
In reply to Ian Campbell
Quoted 20 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
Quoted 15 lines · Dan Carpenter

My static analyzer complains about potential memory corruption in HYPERVISOR_physdev_op()

arch/x86/include/asm/xen/hypercall.h

   389  static inline int
   390  HYPERVISOR_physdev_op(int cmd, void *arg)
   391  {
   392          int rc = _hypercall2(int, physdev_op, cmd, arg);
   393          if (unlikely(rc == -ENOSYS)) {
   394                  struct physdev_op op;
   395                  op.cmd = cmd;
   396                  memcpy(&op.u, arg, sizeof(op.u));
   397                  rc = _hypercall1(int, physdev_op_compat, &op);
   398                  memcpy(arg, &op.u, sizeof(op.u));
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 2 lines

Some of the arg buffers are not as large as sizeof(op.u) which is either 12 or 16 depending on the size of longs in struct physdev_apic.

Quoted 1 line

Nasty!

Wasn't it that pv-ops expects Xen 4.0.1 or newer anyway? If so, what does this code exist for in the first place (it's framed by #if CONFIG_XEN_COMPAT <= 0x030002 in the Xenified kernel)?

Quoted 18 lines
Quoted 6 lines
   399          }
   400          return rc;
   401  }

One example of this is in xen_initdom_restore_msi_irqs().

Quoted 7 lines

arch/x86/pci/xen.c

   337                  struct physdev_pci_device restore_ext;
   338  
   339                  restore_ext.seg = pci_domain_nr(dev->bus);
   340                  restore_ext.bus = dev->bus->number;
   341                  restore_ext.devfn = dev->devfn;
   342                  ret = 

HYPERVISOR_physdev_op(PHYSDEVOP_restore_msi_ext,

Quoted 4 lines
   343                                          &restore_ext);
                                                ^^^^^^^^^^^^

There are only 4 bytes here.

Quoted 6 lines
Quoted 3 lines
   344                  if (ret == -ENOSYS)
                            ^^^^^^^^^^^^^^

If we hit this condition, we have corrupted some memory.

I can see the memory corruption but how does it relate to ret == -ENOSYS?

The (supposedly) corrupting code site inside an

	if (unlikely(rc == -ENOSYS)) {

Supposedly because as long as the argument passed to the function is in memory accessed by the local CPU only and doesn't overlap with storage used for "rc" (e.g. living in a register), there's no corruption possible afaict - the second memcpy() would just copy back what the first one obtained from there.

Fixing this other than by removing the broken code would be pretty hard I'm afraid (and I tend to leave the code untouched altogether in the Xenified tree).

Jan

#7 lore raw
In reply to Ian Campbell
Quoted 20 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
Quoted 15 lines · Dan Carpenter

My static analyzer complains about potential memory corruption in HYPERVISOR_physdev_op()

arch/x86/include/asm/xen/hypercall.h

   389  static inline int
   390  HYPERVISOR_physdev_op(int cmd, void *arg)
   391  {
   392          int rc = _hypercall2(int, physdev_op, cmd, arg);
   393          if (unlikely(rc == -ENOSYS)) {
   394                  struct physdev_op op;
   395                  op.cmd = cmd;
   396                  memcpy(&op.u, arg, sizeof(op.u));
   397                  rc = _hypercall1(int, physdev_op_compat, &op);
   398                  memcpy(arg, &op.u, sizeof(op.u));
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 2 lines

Some of the arg buffers are not as large as sizeof(op.u) which is either 12 or 16 depending on the size of longs in struct physdev_apic.

Quoted 1 line

Nasty!

Wasn't it that pv-ops expects Xen 4.0.1 or newer anyway? If so, what does this code exist for in the first place (it's framed by #if CONFIG_XEN_COMPAT <= 0x030002 in the Xenified kernel)?

Quoted 18 lines
Quoted 6 lines
   399          }
   400          return rc;
   401  }

One example of this is in xen_initdom_restore_msi_irqs().

Quoted 7 lines

arch/x86/pci/xen.c

   337                  struct physdev_pci_device restore_ext;
   338  
   339                  restore_ext.seg = pci_domain_nr(dev->bus);
   340                  restore_ext.bus = dev->bus->number;
   341                  restore_ext.devfn = dev->devfn;
   342                  ret = 

HYPERVISOR_physdev_op(PHYSDEVOP_restore_msi_ext,

Quoted 4 lines
   343                                          &restore_ext);
                                                ^^^^^^^^^^^^

There are only 4 bytes here.

Quoted 6 lines
Quoted 3 lines
   344                  if (ret == -ENOSYS)
                            ^^^^^^^^^^^^^^

If we hit this condition, we have corrupted some memory.

I can see the memory corruption but how does it relate to ret == -ENOSYS?

The (supposedly) corrupting code site inside an

	if (unlikely(rc == -ENOSYS)) {

Supposedly because as long as the argument passed to the function is in memory accessed by the local CPU only and doesn't overlap with storage used for "rc" (e.g. living in a register), there's no corruption possible afaict - the second memcpy() would just copy back what the first one obtained from there.

Fixing this other than by removing the broken code would be pretty hard I'm afraid (and I tend to leave the code untouched altogether in the Xenified tree).

Jan

#3 lore raw
In reply to Jan Beulich
Quoted 20 lines · Jan Beulich
Quoted 20 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
Quoted 17 lines · Dan Carpenter

My static analyzer complains about potential memory corruption in HYPERVISOR_physdev_op()

arch/x86/include/asm/xen/hypercall.h

   389  static inline int
   390  HYPERVISOR_physdev_op(int cmd, void *arg)
   391  {
   392          int rc = _hypercall2(int, physdev_op, cmd, arg);
   393          if (unlikely(rc == -ENOSYS)) {
   394                  struct physdev_op op;
   395                  op.cmd = cmd;
   396                  memcpy(&op.u, arg, sizeof(op.u));
   397                  rc = _hypercall1(int, physdev_op_compat, &op);
   398                  memcpy(arg, &op.u, sizeof(op.u));
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Some of the arg buffers are not as large as sizeof(op.u) which is either 12 or 16 depending on the size of longs in struct physdev_apic.

Quoted 5 lines
Quoted 1 line

Nasty!

Wasn't it that pv-ops expects Xen 4.0.1 or newer anyway? If so, what does this code exist for in the first place (it's framed by #if CONFIG_XEN_COMPAT <= 0x030002 in the Xenified kernel)?

I think the 4.0.1 or newer requirement is for dom0 only. I guess physdev op is only used in dom0 though? Or does passthrough need it?

Quoted 23 lines
Quoted 23 lines
Quoted 7 lines
   399          }
   400          return rc;
   401  }

One example of this is in xen_initdom_restore_msi_irqs().

Quoted 7 lines

arch/x86/pci/xen.c

   337                  struct physdev_pci_device restore_ext;
   338  
   339                  restore_ext.seg = pci_domain_nr(dev->bus);
   340                  restore_ext.bus = dev->bus->number;
   341                  restore_ext.devfn = dev->devfn;
   342                  ret = 

HYPERVISOR_physdev_op(PHYSDEVOP_restore_msi_ext,

Quoted 7 lines
   343                                          &restore_ext);
                                                ^^^^^^^^^^^^

There are only 4 bytes here.

   344                  if (ret == -ENOSYS)
                            ^^^^^^^^^^^^^^

If we hit this condition, we have corrupted some memory.

Quoted 6 lines
Quoted 2 lines

I can see the memory corruption but how does it relate to ret == -ENOSYS?

The (supposedly) corrupting code site inside an

	if (unlikely(rc == -ENOSYS)) {

Ah, for some reason I assumed this was in the eventual caller, even though it was staring me right in the face in the full quote.

Quoted 10 lines

Supposedly because as long as the argument passed to the function is in memory accessed by the local CPU only and doesn't overlap with storage used for "rc" (e.g. living in a register), there's no corruption possible afaict - the second memcpy() would just copy back what the first one obtained from there.

Fixing this other than by removing the broken code would be pretty hard I'm afraid (and I tend to leave the code untouched altogether in the Xenified tree).

Given that it is compat code the list of subops which needs to supported in this case is small and finite so a simple lookup table or even switch stmt for the size might be an option.

Ian.

#6 lore raw
In reply to Jan Beulich
Quoted 20 lines · Jan Beulich
Quoted 20 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
Quoted 17 lines · Dan Carpenter

My static analyzer complains about potential memory corruption in HYPERVISOR_physdev_op()

arch/x86/include/asm/xen/hypercall.h

   389  static inline int
   390  HYPERVISOR_physdev_op(int cmd, void *arg)
   391  {
   392          int rc = _hypercall2(int, physdev_op, cmd, arg);
   393          if (unlikely(rc == -ENOSYS)) {
   394                  struct physdev_op op;
   395                  op.cmd = cmd;
   396                  memcpy(&op.u, arg, sizeof(op.u));
   397                  rc = _hypercall1(int, physdev_op_compat, &op);
   398                  memcpy(arg, &op.u, sizeof(op.u));
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Some of the arg buffers are not as large as sizeof(op.u) which is either 12 or 16 depending on the size of longs in struct physdev_apic.

Quoted 5 lines
Quoted 1 line

Nasty!

Wasn't it that pv-ops expects Xen 4.0.1 or newer anyway? If so, what does this code exist for in the first place (it's framed by #if CONFIG_XEN_COMPAT <= 0x030002 in the Xenified kernel)?

I think the 4.0.1 or newer requirement is for dom0 only. I guess physdev op is only used in dom0 though? Or does passthrough need it?

Quoted 23 lines
Quoted 23 lines
Quoted 7 lines
   399          }
   400          return rc;
   401  }

One example of this is in xen_initdom_restore_msi_irqs().

Quoted 7 lines

arch/x86/pci/xen.c

   337                  struct physdev_pci_device restore_ext;
   338  
   339                  restore_ext.seg = pci_domain_nr(dev->bus);
   340                  restore_ext.bus = dev->bus->number;
   341                  restore_ext.devfn = dev->devfn;
   342                  ret = 

HYPERVISOR_physdev_op(PHYSDEVOP_restore_msi_ext,

Quoted 7 lines
   343                                          &restore_ext);
                                                ^^^^^^^^^^^^

There are only 4 bytes here.

   344                  if (ret == -ENOSYS)
                            ^^^^^^^^^^^^^^

If we hit this condition, we have corrupted some memory.

Quoted 6 lines
Quoted 2 lines

I can see the memory corruption but how does it relate to ret == -ENOSYS?

The (supposedly) corrupting code site inside an

	if (unlikely(rc == -ENOSYS)) {

Ah, for some reason I assumed this was in the eventual caller, even though it was staring me right in the face in the full quote.

Quoted 10 lines

Supposedly because as long as the argument passed to the function is in memory accessed by the local CPU only and doesn't overlap with storage used for "rc" (e.g. living in a register), there's no corruption possible afaict - the second memcpy() would just copy back what the first one obtained from there.

Fixing this other than by removing the broken code would be pretty hard I'm afraid (and I tend to leave the code untouched altogether in the Xenified tree).

Given that it is compat code the list of subops which needs to supported in this case is small and finite so a simple lookup table or even switch stmt for the size might be an option.

Ian.

#4 lore raw
In reply to Ian Campbell
Quoted 22 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
Quoted 20 lines · Jan Beulich
Quoted 20 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
Quoted 17 lines · Dan Carpenter

My static analyzer complains about potential memory corruption in HYPERVISOR_physdev_op()

arch/x86/include/asm/xen/hypercall.h

   389  static inline int
   390  HYPERVISOR_physdev_op(int cmd, void *arg)
   391  {
   392          int rc = _hypercall2(int, physdev_op, cmd, arg);
   393          if (unlikely(rc == -ENOSYS)) {
   394                  struct physdev_op op;
   395                  op.cmd = cmd;
   396                  memcpy(&op.u, arg, sizeof(op.u));
   397                  rc = _hypercall1(int, physdev_op_compat, &op);
   398                  memcpy(arg, &op.u, sizeof(op.u));
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Some of the arg buffers are not as large as sizeof(op.u) which is either 12 or 16 depending on the size of longs in struct physdev_apic.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Nasty!

Wasn't it that pv-ops expects Xen 4.0.1 or newer anyway? If so, what does this code exist for in the first place (it's framed by #if CONFIG_XEN_COMPAT <= 0x030002 in the Xenified kernel)?

I think the 4.0.1 or newer requirement is for dom0 only. I guess physdev op is only used in dom0 though? Or does passthrough need it?

No, it's only platform_op that is Dom0-only.

Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

I can see the memory corruption but how does it relate to ret == -ENOSYS?

The (supposedly) corrupting code site inside an

	if (unlikely(rc == -ENOSYS)) {

Ah, for some reason I assumed this was in the eventual caller, even though it was staring me right in the face in the full quote.

I think Dan's reference was to an eventual caller - it would see the -ENOSYS, as the compat call wouldn't return anything else than the modern one, and the modern one (to enter the code in question) must have returned -ENOSYS.

Quoted 7 lines
Quoted 3 lines

Fixing this other than by removing the broken code would be pretty hard I'm afraid (and I tend to leave the code untouched altogether in the Xenified tree).

Given that it is compat code the list of subops which needs to supported in this case is small and finite so a simple lookup table or even switch stmt for the size might be an option.

Ugly, particularly for an inline function. But possible of course.

Jan

#5 lore raw
In reply to Ian Campbell
Quoted 22 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
Quoted 20 lines · Jan Beulich
Quoted 20 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
Quoted 17 lines · Dan Carpenter

My static analyzer complains about potential memory corruption in HYPERVISOR_physdev_op()

arch/x86/include/asm/xen/hypercall.h

   389  static inline int
   390  HYPERVISOR_physdev_op(int cmd, void *arg)
   391  {
   392          int rc = _hypercall2(int, physdev_op, cmd, arg);
   393          if (unlikely(rc == -ENOSYS)) {
   394                  struct physdev_op op;
   395                  op.cmd = cmd;
   396                  memcpy(&op.u, arg, sizeof(op.u));
   397                  rc = _hypercall1(int, physdev_op_compat, &op);
   398                  memcpy(arg, &op.u, sizeof(op.u));
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Some of the arg buffers are not as large as sizeof(op.u) which is either 12 or 16 depending on the size of longs in struct physdev_apic.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Nasty!

Wasn't it that pv-ops expects Xen 4.0.1 or newer anyway? If so, what does this code exist for in the first place (it's framed by #if CONFIG_XEN_COMPAT <= 0x030002 in the Xenified kernel)?

I think the 4.0.1 or newer requirement is for dom0 only. I guess physdev op is only used in dom0 though? Or does passthrough need it?

No, it's only platform_op that is Dom0-only.

Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

I can see the memory corruption but how does it relate to ret == -ENOSYS?

The (supposedly) corrupting code site inside an

	if (unlikely(rc == -ENOSYS)) {

Ah, for some reason I assumed this was in the eventual caller, even though it was staring me right in the face in the full quote.

I think Dan's reference was to an eventual caller - it would see the -ENOSYS, as the compat call wouldn't return anything else than the modern one, and the modern one (to enter the code in question) must have returned -ENOSYS.

Quoted 7 lines
Quoted 3 lines

Fixing this other than by removing the broken code would be pretty hard I'm afraid (and I tend to leave the code untouched altogether in the Xenified tree).

Given that it is compat code the list of subops which needs to supported in this case is small and finite so a simple lookup table or even switch stmt for the size might be an option.

Ugly, particularly for an inline function. But possible of course.

Jan

#8 lore raw
In reply to Ian Campbell
Quoted 19 lines
Quoted 16 lines
Quoted 1 line · Ian Campbell

My static analyzer complains about potential memory corruption in HYPERVISOR_physdev_op()

arch/x86/include/asm/xen/hypercall.h

   389  static inline int
   390  HYPERVISOR_physdev_op(int cmd, void *arg)
   391  {
   392          int rc = _hypercall2(int, physdev_op, cmd, arg);
   393          if (unlikely(rc == -ENOSYS)) {
   394                  struct physdev_op op;
   395                  op.cmd = cmd;
   396                  memcpy(&op.u, arg, sizeof(op.u));
   397                  rc = _hypercall1(int, physdev_op_compat, &op);
   398                  memcpy(arg, &op.u, sizeof(op.u));
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 2 lines

Some of the arg buffers are not as large as sizeof(op.u) which is either 12 or 16 depending on the size of longs in struct physdev_apic.

Quoted 1 line

Nasty!

Doesn't the same problem also exist for HYPERVISOR_event_channel_op() then, at least theoretically (EVTCHNOP_reset is apparently the only addition here so far, but is being used by the tools only afaics)?

Jan

#11 lore raw
In reply to Ian Campbell
Quoted 19 lines
Quoted 16 lines
Quoted 1 line · Ian Campbell

My static analyzer complains about potential memory corruption in HYPERVISOR_physdev_op()

arch/x86/include/asm/xen/hypercall.h

   389  static inline int
   390  HYPERVISOR_physdev_op(int cmd, void *arg)
   391  {
   392          int rc = _hypercall2(int, physdev_op, cmd, arg);
   393          if (unlikely(rc == -ENOSYS)) {
   394                  struct physdev_op op;
   395                  op.cmd = cmd;
   396                  memcpy(&op.u, arg, sizeof(op.u));
   397                  rc = _hypercall1(int, physdev_op_compat, &op);
   398                  memcpy(arg, &op.u, sizeof(op.u));
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 2 lines

Some of the arg buffers are not as large as sizeof(op.u) which is either 12 or 16 depending on the size of longs in struct physdev_apic.

Quoted 1 line

Nasty!

Doesn't the same problem also exist for HYPERVISOR_event_channel_op() then, at least theoretically (EVTCHNOP_reset is apparently the only addition here so far, but is being used by the tools only afaics)?

Jan

#9 lore raw
In reply to Jan Beulich
Quoted 20 lines
Quoted 20 lines
Quoted 19 lines · Jan Beulich
Quoted 1 line · Ian Campbell

My static analyzer complains about potential memory corruption in HYPERVISOR_physdev_op()

arch/x86/include/asm/xen/hypercall.h

   389  static inline int
   390  HYPERVISOR_physdev_op(int cmd, void *arg)
   391  {
   392          int rc = _hypercall2(int, physdev_op, cmd, arg);
   393          if (unlikely(rc == -ENOSYS)) {
   394                  struct physdev_op op;
   395                  op.cmd = cmd;
   396                  memcpy(&op.u, arg, sizeof(op.u));
   397                  rc = _hypercall1(int, physdev_op_compat, &op);
   398                  memcpy(arg, &op.u, sizeof(op.u));
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Some of the arg buffers are not as large as sizeof(op.u) which is either 12 or 16 depending on the size of longs in struct physdev_apic.

Quoted 6 lines
Quoted 1 line

Nasty!

Doesn't the same problem also exist for HYPERVISOR_event_channel_op() then, at least theoretically (EVTCHNOP_reset is apparently the only addition here so far, but is being used by the tools only afaics)?

Actually, the problem isn't tied to new additions of sub-hypercalls (I was wrongly implying this from the example originally provided), and should be visible e.g. on any use of EVTCHNOP_unmask.

Jan

#10 lore raw
In reply to Jan Beulich
Quoted 20 lines
Quoted 20 lines
Quoted 19 lines · Jan Beulich
Quoted 1 line · Ian Campbell

My static analyzer complains about potential memory corruption in HYPERVISOR_physdev_op()

arch/x86/include/asm/xen/hypercall.h

   389  static inline int
   390  HYPERVISOR_physdev_op(int cmd, void *arg)
   391  {
   392          int rc = _hypercall2(int, physdev_op, cmd, arg);
   393          if (unlikely(rc == -ENOSYS)) {
   394                  struct physdev_op op;
   395                  op.cmd = cmd;
   396                  memcpy(&op.u, arg, sizeof(op.u));
   397                  rc = _hypercall1(int, physdev_op_compat, &op);
   398                  memcpy(arg, &op.u, sizeof(op.u));
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Some of the arg buffers are not as large as sizeof(op.u) which is either 12 or 16 depending on the size of longs in struct physdev_apic.

Quoted 6 lines
Quoted 1 line

Nasty!

Doesn't the same problem also exist for HYPERVISOR_event_channel_op() then, at least theoretically (EVTCHNOP_reset is apparently the only addition here so far, but is being used by the tools only afaics)?

Actually, the problem isn't tied to new additions of sub-hypercalls (I was wrongly implying this from the example originally provided), and should be visible e.g. on any use of EVTCHNOP_unmask.

Jan