Skip to contentSkip to conversation

slirp: Correct size check in m_inc()

PATCH Applied Peter Maydell 6 participants 17 messages Aug 7, 2018 – Aug 10, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 slirp: Correct size check in m_inc()

The data in an mbuf buffer is not necessarily at the start of the allocated buffer. (For instance m_adj() allows data to be trimmed from the start by just advancing the pointer and reducing the length.) This means that the allocated buffer size (m->m_size) and the amount of space from the m_data pointer to the end of the buffer (M_ROOM(m)) are not necessarily the same.

Commit 864036e251f54c9 tried to change the m_inc() function from taking the new allocated-buffer-size to taking the new room-size, but forgot to change the initial "do we already have enough space" check. This meant that if we were trying to extend a buffer which had a leading gap between the buffer start and the data, we might incorrectly decide it didn't need to be extended, and then overrun the end of the buffer, causing memory corruption and an eventual crash.

Change the "already big enough?" condition from checking the argument against m->m_size to checking against M_ROOM(). This only makes a difference for the callsite in m_cat(); the other three callsites all start with a freshly allocated mbuf from m_get(), which will have m->m_size == M_ROOM(m).

---
 slirp/mbuf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
slirp/mbuf.c +1 -1
index 0c189e1a7bf..1b7868355a3 100644
@@ -154,7 +154,7 @@ m_inc(struct mbuf *m, int size)
     int datasize;
 
     /* some compilers throw up on gotos.  This one we can fake. */
-    if (m->m_size > size) {
+    if (M_ROOM(m) > size) {
         return;
     }
 
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw

Peter Maydell, le mar. 07 août 2018 12:45:01 +0100, a ecrit:

Quoted 22 lines

The data in an mbuf buffer is not necessarily at the start of the allocated buffer. (For instance m_adj() allows data to be trimmed from the start by just advancing the pointer and reducing the length.) This means that the allocated buffer size (m->m_size) and the amount of space from the m_data pointer to the end of the buffer (M_ROOM(m)) are not necessarily the same.

Commit 864036e251f54c9 tried to change the m_inc() function from taking the new allocated-buffer-size to taking the new room-size, but forgot to change the initial "do we already have enough space" check. This meant that if we were trying to extend a buffer which had a leading gap between the buffer start and the data, we might incorrectly decide it didn't need to be extended, and then overrun the end of the buffer, causing memory corruption and an eventual crash.

Change the "already big enough?" condition from checking the argument against m->m_size to checking against M_ROOM(). This only makes a difference for the callsite in m_cat(); the other three callsites all start with a freshly allocated mbuf from m_get(), which will have m->m_size == M_ROOM(m).

Quoted 3 lines
Quoted 21 lines
---
 slirp/mbuf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
slirp/mbuf.c +1 -1
index 0c189e1a7bf..1b7868355a3 100644
@@ -154,7 +154,7 @@ m_inc(struct mbuf *m, int size)
     int datasize;
 
     /* some compilers throw up on gotos.  This one we can fake. */
-    if (m->m_size > size) {
+    if (M_ROOM(m) > size) {
         return;
     }
 
Signature · 1 line
2.17.1
Signature · 5 lines
Samuel
"And the next time you consider complaining that running Lucid Emacs
19.05 via NFS from a remote Linux machine in Paraguay doesn't seem to
get the background colors right, you'll know who to thank."
(By Matt Welsh)
Tested-by on v1 1/1
#2 lore raw
In reply to Peter Maydell
Quoted 22 lines · * Peter Maydell ([email protected]) wrote:

The data in an mbuf buffer is not necessarily at the start of the allocated buffer. (For instance m_adj() allows data to be trimmed from the start by just advancing the pointer and reducing the length.) This means that the allocated buffer size (m->m_size) and the amount of space from the m_data pointer to the end of the buffer (M_ROOM(m)) are not necessarily the same.

Commit 864036e251f54c9 tried to change the m_inc() function from taking the new allocated-buffer-size to taking the new room-size, but forgot to change the initial "do we already have enough space" check. This meant that if we were trying to extend a buffer which had a leading gap between the buffer start and the data, we might incorrectly decide it didn't need to be extended, and then overrun the end of the buffer, causing memory corruption and an eventual crash.

Change the "already big enough?" condition from checking the argument against m->m_size to checking against M_ROOM(). This only makes a difference for the callsite in m_cat(); the other three callsites all start with a freshly allocated mbuf from m_get(), which will have m->m_size == M_ROOM(m).

Quoted 3 lines
Quoted 20 lines
---
 slirp/mbuf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
slirp/mbuf.c +1 -1
index 0c189e1a7bf..1b7868355a3 100644
@@ -154,7 +154,7 @@ m_inc(struct mbuf *m, int size)
     int datasize;
 
     /* some compilers throw up on gotos.  This one we can fake. */
-    if (m->m_size > size) {
+    if (M_ROOM(m) > size) {
         return;
     }
 
Signature · 1 line
2.17.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#3 lore raw
Quoted 17 lines · Dr. David Alan Gilbert
Quoted 16 lines · * Peter Maydell ([email protected]) wrote:

The data in an mbuf buffer is not necessarily at the start of the allocated buffer. (For instance m_adj() allows data to be trimmed from the start by just advancing the pointer and reducing the length.) This means that the allocated buffer size (m->m_size) and the amount of space from the m_data pointer to the end of the buffer (M_ROOM(m)) are not necessarily the same.

Commit 864036e251f54c9 tried to change the m_inc() function from taking the new allocated-buffer-size to taking the new room-size, but forgot to change the initial "do we already have enough space" check. This meant that if we were trying to extend a buffer which had a leading gap between the buffer start and the data, we might incorrectly decide it didn't need to be extended, and then overrun the end of the buffer, causing memory corruption and an eventual crash.

Quoted 7 lines
Quoted 7 lines

Change the "already big enough?" condition from checking the argument against m->m_size to checking against M_ROOM(). This only makes a difference for the callsite in m_cat(); the other three callsites all start with a freshly allocated mbuf from m_get(), which will have m->m_size == M_ROOM(m).

Fixes 864036e251f54c9

IIUC, this changeset was a security fix for CVE-2018-11806.

Given that the fix was flawed and allowed guest to crash the host with a new buffer overrun, it seems we need to get a new CVE allocated too.

Quoted 22 lines
Quoted 2 lines
Quoted 17 lines
---
 slirp/mbuf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
slirp/mbuf.c +1 -1
index 0c189e1a7bf..1b7868355a3 100644
@@ -154,7 +154,7 @@ m_inc(struct mbuf *m, int size)
     int datasize;
 
     /* some compilers throw up on gotos.  This one we can fake. */
-    if (m->m_size > size) {
+    if (M_ROOM(m) > size) {
         return;
     }
 
Quoted 6 lines
Quoted 3 lines
Signature · 1 line
2.17.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#4 lore raw
Quoted 24 lines · Daniel P. Berrangé
Quoted 23 lines · Dr. David Alan Gilbert
Quoted 22 lines · * Peter Maydell ([email protected]) wrote:

The data in an mbuf buffer is not necessarily at the start of the allocated buffer. (For instance m_adj() allows data to be trimmed from the start by just advancing the pointer and reducing the length.) This means that the allocated buffer size (m->m_size) and the amount of space from the m_data pointer to the end of the buffer (M_ROOM(m)) are not necessarily the same.

Commit 864036e251f54c9 tried to change the m_inc() function from taking the new allocated-buffer-size to taking the new room-size, but forgot to change the initial "do we already have enough space" check. This meant that if we were trying to extend a buffer which had a leading gap between the buffer start and the data, we might incorrectly decide it didn't need to be extended, and then overrun the end of the buffer, causing memory corruption and an eventual crash.

Change the "already big enough?" condition from checking the argument against m->m_size to checking against M_ROOM(). This only makes a difference for the callsite in m_cat(); the other three callsites all start with a freshly allocated mbuf from m_get(), which will have m->m_size == M_ROOM(m).

Quoted 7 lines
Quoted 1 line
Quoted 1 line
Fixes 864036e251f54c9

IIUC, this changeset was a security fix for CVE-2018-11806.

Given that the fix was flawed and allowed guest to crash the host with a new buffer overrun, it seems we need to get a new CVE allocated too.

But 864036e251f54c9 was never part of an official QEMU release, was it? Or did it go into a stable release already? If not, I think you simply need both patches to fix the CVE instead.

Thomas

#5 lore raw
Quoted 29 lines · Thomas Huth
Quoted 28 lines · Daniel P. Berrangé
Quoted 17 lines · Dr. David Alan Gilbert
Quoted 16 lines · * Peter Maydell ([email protected]) wrote:

The data in an mbuf buffer is not necessarily at the start of the allocated buffer. (For instance m_adj() allows data to be trimmed from the start by just advancing the pointer and reducing the length.) This means that the allocated buffer size (m->m_size) and the amount of space from the m_data pointer to the end of the buffer (M_ROOM(m)) are not necessarily the same.

Commit 864036e251f54c9 tried to change the m_inc() function from taking the new allocated-buffer-size to taking the new room-size, but forgot to change the initial "do we already have enough space" check. This meant that if we were trying to extend a buffer which had a leading gap between the buffer start and the data, we might incorrectly decide it didn't need to be extended, and then overrun the end of the buffer, causing memory corruption and an eventual crash.

Quoted 7 lines
Quoted 7 lines

Change the "already big enough?" condition from checking the argument against m->m_size to checking against M_ROOM(). This only makes a difference for the callsite in m_cat(); the other three callsites all start with a freshly allocated mbuf from m_get(), which will have m->m_size == M_ROOM(m).

Fixes 864036e251f54c9

IIUC, this changeset was a security fix for CVE-2018-11806.

Quoted 7 lines
Quoted 3 lines

Given that the fix was flawed and allowed guest to crash the host with a new buffer overrun, it seems we need to get a new CVE allocated too.

But 864036e251f54c9 was never part of an official QEMU release, was it? Or did it go into a stable release already? If not, I think you simply need both patches to fix the CVE instead.

Ah possibly - I didn't look at where 864036e251f54c9 was actually release or not. If its onyl git master, then yeah, we can use the same CVE we already have.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#6 lore raw
Quoted 8 lines · Daniel P. Berrangé
Quoted 3 lines · Thomas Huth

But 864036e251f54c9 was never part of an official QEMU release, was it? Or did it go into a stable release already? If not, I think you simply need both patches to fix the CVE instead.

Ah possibly - I didn't look at where 864036e251f54c9 was actually release or not. If its onyl git master, then yeah, we can use the same CVE we already have.

Yeah, we haven't released anything with 864036e251f54c9 in it yet. (In particular we did not flag it up for stable and so it is not in 2.12.1...)

thanks
-- PMM

#7 lore raw
Quoted 6 lines · Peter Maydell
Quoted 5 lines · Daniel P. Berrangé
Quoted 3 lines · Thomas Huth

But 864036e251f54c9 was never part of an official QEMU release, was it? Or did it go into a stable release already? If not, I think you simply need both patches to fix the CVE instead.

Quoted 7 lines
Quoted 3 lines

Ah possibly - I didn't look at where 864036e251f54c9 was actually release or not. If its onyl git master, then yeah, we can use the same CVE we already have.

Yeah, we haven't released anything with 864036e251f54c9 in it yet. (In particular we did not flag it up for stable and so it is not in 2.12.1...)

Pointing out the obvious: this is a second opportunity to flag the CVE fix for stable.

#8 lore raw
Quoted 12 lines · Markus Armbruster
Quoted 10 lines · Peter Maydell
Quoted 8 lines · Daniel P. Berrangé
Quoted 3 lines · Thomas Huth

But 864036e251f54c9 was never part of an official QEMU release, was it? Or did it go into a stable release already? If not, I think you simply need both patches to fix the CVE instead.

Ah possibly - I didn't look at where 864036e251f54c9 was actually release or not. If its onyl git master, then yeah, we can use the same CVE we already have.

Quoted 6 lines
Quoted 3 lines

Yeah, we haven't released anything with 864036e251f54c9 in it yet. (In particular we did not flag it up for stable and so it is not in 2.12.1...)

Pointing out the obvious: this is a second opportunity to flag the CVE fix for stable.

Well, as long as we get both halves of it, yes :-) (ie commits 864036e251f54c9 + 09b94ac0f29db3b022a77a).

thanks
-- PMM

author Applied
#9 lore raw
In reply to Peter Maydell
Quoted 22 lines · Peter Maydell

The data in an mbuf buffer is not necessarily at the start of the allocated buffer. (For instance m_adj() allows data to be trimmed from the start by just advancing the pointer and reducing the length.) This means that the allocated buffer size (m->m_size) and the amount of space from the m_data pointer to the end of the buffer (M_ROOM(m)) are not necessarily the same.

Commit 864036e251f54c9 tried to change the m_inc() function from taking the new allocated-buffer-size to taking the new room-size, but forgot to change the initial "do we already have enough space" check. This meant that if we were trying to extend a buffer which had a leading gap between the buffer start and the data, we might incorrectly decide it didn't need to be extended, and then overrun the end of the buffer, causing memory corruption and an eventual crash.

Change the "already big enough?" condition from checking the argument against m->m_size to checking against M_ROOM(). This only makes a difference for the callsite in m_cat(); the other three callsites all start with a freshly allocated mbuf from m_get(), which will have m->m_size == M_ROOM(m).

Quoted 4 lines

---

Applied to master, thanks.

-- PMM

#10 lore raw
In reply to Peter Maydell
Quoted 34 lines · * Peter Maydell ([email protected]) wrote:

The data in an mbuf buffer is not necessarily at the start of the allocated buffer. (For instance m_adj() allows data to be trimmed from the start by just advancing the pointer and reducing the length.) This means that the allocated buffer size (m->m_size) and the amount of space from the m_data pointer to the end of the buffer (M_ROOM(m)) are not necessarily the same.

Commit 864036e251f54c9 tried to change the m_inc() function from taking the new allocated-buffer-size to taking the new room-size, but forgot to change the initial "do we already have enough space" check. This meant that if we were trying to extend a buffer which had a leading gap between the buffer start and the data, we might incorrectly decide it didn't need to be extended, and then overrun the end of the buffer, causing memory corruption and an eventual crash.

Change the "already big enough?" condition from checking the argument against m->m_size to checking against M_ROOM(). This only makes a difference for the callsite in m_cat(); the other three callsites all start with a freshly allocated mbuf from m_get(), which will have m->m_size == M_ROOM(m).

---
 slirp/mbuf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
slirp/mbuf.c +0 -0
index 0c189e1a7bf..1b7868355a3 100644

slirp/mbuf.c m_inc(struct mbuf *m, int size) · line 159

Quoted 7 lines
slirp/mbuf.c +1 -1
@@ -154,7 +154,7 @@ m_inc(struct mbuf *m, int size)
     int datasize;
 
     /* some compilers throw up on gotos.  This one we can fake. */
-    if (m->m_size > size) {
+    if (M_ROOM(m) > size) {
         return;
     }

I'm worried about a side effect of this change.
A few lines below we have:

        datasize = m->m_data - m->m_dat;
        m->m_ext = g_malloc(size + datasize);
        memcpy(m->m_ext, m->m_dat, m->m_size);
        m->m_flags |= M_EXT;

Question: What guarantees there's m_size room for that memcpy in the new m_ext?

Before this fix, it used to be the case that m_size was smaller than size, so we knew it fitted; but that's no longer true.

I don't think I understand the relationship between datasize and m_size enough to know if anything is sufficient.

Dave

Quoted 5 lines
Signature · 2 lines
-- 
2.17.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#11 lore raw
Quoted 17 lines · Dr. David Alan Gilbert

slirp/mbuf.c m_inc(struct mbuf *m, int size) · line 159

Quoted 12 lines · * Peter Maydell ([email protected]) wrote:
slirp/mbuf.c +1 -1
index 0c189e1a7bf..1b7868355a3 100644
@@ -154,7 +154,7 @@ m_inc(struct mbuf *m, int size)
     int datasize;
 
     /* some compilers throw up on gotos.  This one we can fake. */
-    if (m->m_size > size) {
+    if (M_ROOM(m) > size) {
         return;
     }

I'm worried about a side effect of this change.
A few lines below we have:

Quoted 7 lines
        datasize = m->m_data - m->m_dat;
        m->m_ext = g_malloc(size + datasize);
        memcpy(m->m_ext, m->m_dat, m->m_size);
        m->m_flags |= M_EXT;

Question: What guarantees there's m_size room for that memcpy in the new m_ext?

It did take me a while to convince myself that that was true when I was writing the patch... Here's the ASCII art:

   |--datasize---->|---m_len------->
   |----------m_size------------------------------>
                   |----M_ROOM-------------------->
                                    |-M_FREEROOM-->
   ^               ^                               ^
   m_dat           m_data                          end of buffer

("datasize" is a bit misnamed, as it's "size of the leading gap between the start of the buffer and the data"; "gapsize" would be more helpful.)

Anyway, we allocate size + datasize, and
m_size == datasize + M_ROOM. We know that size >= M_ROOM, so the allocated buffer must be at least m_size big.

thanks
-- PMM

#12 lore raw
Quoted 38 lines · * Peter Maydell ([email protected]) wrote:
Quoted 17 lines · Dr. David Alan Gilbert

slirp/mbuf.c m_inc(struct mbuf *m, int size) · line 159

Quoted 12 lines · * Peter Maydell ([email protected]) wrote:
slirp/mbuf.c +1 -1
index 0c189e1a7bf..1b7868355a3 100644
@@ -154,7 +154,7 @@ m_inc(struct mbuf *m, int size)
     int datasize;
 
     /* some compilers throw up on gotos.  This one we can fake. */
-    if (m->m_size > size) {
+    if (M_ROOM(m) > size) {
         return;
     }

I'm worried about a side effect of this change.
A few lines below we have:

Quoted 7 lines
        datasize = m->m_data - m->m_dat;
        m->m_ext = g_malloc(size + datasize);
        memcpy(m->m_ext, m->m_dat, m->m_size);
        m->m_flags |= M_EXT;

Question: What guarantees there's m_size room for that memcpy in the new m_ext?

It did take me a while to convince myself that that was true when I was writing the patch... Here's the ASCII art:

   |--datasize---->|---m_len------->
   |----------m_size------------------------------>
                   |----M_ROOM-------------------->
                                    |-M_FREEROOM-->
   ^               ^                               ^
   m_dat           m_data                          end of buffer
Quoted 7 lines

("datasize" is a bit misnamed, as it's "size of the leading gap between the start of the buffer and the data"; "gapsize" would be more helpful.)

Anyway, we allocate size + datasize, and
m_size == datasize + M_ROOM. We know that size >= M_ROOM, so the allocated buffer must be at least m_size big.

Ah OK, thanks.
(That ascii art could do with being in a comment somewhere!)

Dave

Quoted 2 lines

thanks
-- PMM

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#13 lore raw

Dr. David Alan Gilbert, le jeu. 09 août 2018 12:32:05 +0100, a ecrit:

Quoted 12 lines
Quoted 12 lines
   |--datasize---->|---m_len------->
   |----------m_size------------------------------>
                   |----M_ROOM-------------------->
                                    |-M_FREEROOM-->
   ^               ^                               ^
   m_dat           m_data                          end of buffer

("datasize" is a bit misnamed, as it's "size of the leading gap between the start of the buffer and the data"; "gapsize" would be more helpful.)

Quoted 6 lines
Quoted 3 lines

Anyway, we allocate size + datasize, and
m_size == datasize + M_ROOM. We know that size >= M_ROOM, so the allocated buffer must be at least m_size big.

Ah OK, thanks.
(That ascii art could do with being in a comment somewhere!)

Indeed. Peter, maybe your Signed-off-by on this? :)

Samuel

commit 4be85a1eeb6b19e91491e689d4d0d054030cbb49

Author: Peter Maydell <[email protected]>
Date:   Thu Aug 9 23:52:59 2018 +0200

slirp: document mbuf pointers and sizes

slirp/mbuf.h +10 -0
index 33b84485d6..a5bb3f9e66 100644
@@ -47,6 +47,16 @@
  * free the m_ext.  This is inefficient memory-wise, but who cares.
  */
 
+/*
+ *  |--gapsize----->|---m_len------->
+ *  |----------m_size------------------------------>
+ *                  |----M_ROOM-------------------->
+ *                                   |-M_FREEROOM-->
+ *
+ *  ^               ^                               ^
+ *  m_dat/m_ext     m_data                          end of buffer
+ */
+
 /*
  * How much room is in the mbuf, from m_data to the end of the mbuf
  */
#14 lore raw
Quoted 13 lines · Samuel Thibault

Dr. David Alan Gilbert, le jeu. 09 août 2018 12:32:05 +0100, a ecrit:

Quoted 12 lines
Quoted 12 lines
   |--datasize---->|---m_len------->
   |----------m_size------------------------------>
                   |----M_ROOM-------------------->
                                    |-M_FREEROOM-->
   ^               ^                               ^
   m_dat           m_data                          end of buffer

("datasize" is a bit misnamed, as it's "size of the leading gap between the start of the buffer and the data"; "gapsize" would be more helpful.)

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Anyway, we allocate size + datasize, and
m_size == datasize + M_ROOM. We know that size >= M_ROOM, so the allocated buffer must be at least m_size big.

Ah OK, thanks.
(That ascii art could do with being in a comment somewhere!)

Indeed. Peter, maybe your Signed-off-by on this? :)

Quoted 18 lines

Samuel

commit 4be85a1eeb6b19e91491e689d4d0d054030cbb49

Author: Peter Maydell <[email protected]>
Date:   Thu Aug 9 23:52:59 2018 +0200

slirp: document mbuf pointers and sizes

slirp/mbuf.h +0 -0
index 33b84485d6..a5bb3f9e66 100644
@@ -47,6 +47,16 @@
  * free the m_ext.  This is inefficient memory-wise, but who cares.
  */
 

slirp/mbuf.h line 59

Quoted 10 lines
slirp/mbuf.h +10 -0
+/*
+ *  |--gapsize----->|---m_len------->
+ *  |----------m_size------------------------------>
+ *                  |----M_ROOM-------------------->
+ *                                   |-M_FREEROOM-->
+ *
+ *  ^               ^                               ^
+ *  m_dat/m_ext     m_data                          end of buffer
+ */
+

...but
(a) you should add a comment describing what 'gapsize' is, ie that there may be a gap between the in-use data and the start of the allocated buffer, and
(b) m_inc() should change its variable name to match.

thanks
-- PMM

#15 lore raw

Peter Maydell, le ven. 10 août 2018 10:02:37 +0100, a ecrit:

Quoted 4 lines

(a) you should add a comment describing what 'gapsize' is, ie that there may be a gap between the in-use data and the start of the allocated buffer, and
(b) m_inc() should change its variable name to match.

Right, I have done so in my tree. Do you prefer to have it pushed for qemu 3.0?

Samuel

#16 lore raw
Quoted 8 lines · Samuel Thibault

Peter Maydell, le ven. 10 août 2018 10:02:37 +0100, a ecrit:

Quoted 4 lines

(a) you should add a comment describing what 'gapsize' is, ie that there may be a gap between the in-use data and the start of the allocated buffer, and
(b) m_inc() should change its variable name to match.

Right, I have done so in my tree. Do you prefer to have it pushed for qemu 3.0?

No, this is definitely 3.1 material (also you should post the patch for review as its own email, please).

thanks
-- PMM