slirp: Correct size check in m_inc()
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
@@ -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
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 them_datapointer to the end of the buffer (M_ROOM(m)) are not necessarily the same.Commit
864036e251f54c9tried to change them_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_sizeto checking againstM_ROOM(). This only makes a difference for the callsite inm_cat(); the other three callsites all start with a freshly allocated mbuf fromm_get(), which will havem->m_size==M_ROOM(m).
Quoted 3 lines
Fixes 864036e251f54c9Signed-off-by Peter Maydell <[email protected]>
Quoted 21 lines
--- slirp/mbuf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)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 ; }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)
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 them_datapointer to the end of the buffer (M_ROOM(m)) are not necessarily the same.Commit
864036e251f54c9tried to change them_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_sizeto checking againstM_ROOM(). This only makes a difference for the callsite inm_cat(); the other three callsites all start with a freshly allocated mbuf fromm_get(), which will havem->m_size==M_ROOM(m).
Quoted 3 lines
Fixes 864036e251f54c9Signed-off-by Peter Maydell <[email protected]>
Quoted 20 lines
--- slirp/mbuf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)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 ; }Signature · 1 line
2.17.1
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
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 them_datapointer to the end of the buffer (M_ROOM(m)) are not necessarily the same.Commit
864036e251f54c9tried to change them_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_sizeto checking againstM_ROOM(). This only makes a difference for the callsite inm_cat(); the other three callsites all start with a freshly allocated mbuf fromm_get(), which will havem->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
Signed-off-by Peter Maydell <[email protected]>Quoted 17 lines
--- slirp/mbuf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)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 ; }
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 :|
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 them_datapointer to the end of the buffer (M_ROOM(m)) are not necessarily the same.Commit
864036e251f54c9tried to change them_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_sizeto checking againstM_ROOM(). This only makes a difference for the callsite inm_cat(); the other three callsites all start with a freshly allocated mbuf fromm_get(), which will havem->m_size==M_ROOM(m).
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Fixes 864036e251f54c9IIUC, 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
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 them_datapointer to the end of the buffer (M_ROOM(m)) are not necessarily the same.Commit
864036e251f54c9tried to change them_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_sizeto checking againstM_ROOM(). This only makes a difference for the callsite inm_cat(); the other three callsites all start with a freshly allocated mbuf fromm_get(), which will havem->m_size==M_ROOM(m).Fixes 864036e251f54c9IIUC, 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
864036e251f54c9was 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 :|
Quoted 8 lines · Daniel P. Berrangé
Quoted 3 lines · Thomas Huth
But
864036e251f54c9was 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
864036e251f54c9was 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
Quoted 6 lines · Peter Maydell
Quoted 5 lines · Daniel P. Berrangé
Quoted 3 lines · Thomas Huth
But
864036e251f54c9was 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
864036e251f54c9was 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
864036e251f54c9in 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.
Quoted 12 lines · Markus Armbruster
Quoted 10 lines · Peter Maydell
Quoted 8 lines · Daniel P. Berrangé
Quoted 3 lines · Thomas Huth
But
864036e251f54c9was 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
864036e251f54c9was 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
864036e251f54c9in 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
Peter Maydell
AppliedQuoted 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 them_datapointer to the end of the buffer (M_ROOM(m)) are not necessarily the same.Commit
864036e251f54c9tried to change them_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_sizeto checking againstM_ROOM(). This only makes a difference for the callsite inm_cat(); the other three callsites all start with a freshly allocated mbuf fromm_get(), which will havem->m_size==M_ROOM(m).
Quoted 4 lines
Fixes 864036e251f54c9Signed-off-by Peter Maydell <[email protected]>---
Applied to master, thanks.
-- PMM
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 them_datapointer to the end of the buffer (M_ROOM(m)) are not necessarily the same.Commit
864036e251f54c9tried to change them_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_sizeto checking againstM_ROOM(). This only makes a difference for the callsite inm_cat(); the other three callsites all start with a freshly allocated mbuf fromm_get(), which will havem->m_size==M_ROOM(m).Fixes 864036e251f54c9Signed-off-by Peter Maydell <[email protected]>--- slirp/mbuf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)slirp/mbuf.c +0
-0
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
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@@ -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_sizeroom for that memcpy in the newm_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, andm_size == datasize + M_ROOM. We know that size >= M_ROOM, so the allocated buffer must be at least m_size big.
thanks
-- PMM
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@@ -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_sizeroom for that memcpy in the newm_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 leastm_sizebig.
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
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 leastm_sizebig.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
@@ -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 */
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 leastm_sizebig.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
4be85a1eeb6b19e91491e689d4d0d054030cbb49Author: Peter Maydell <[email protected]> Date: Thu Aug 9 23:52:59 2018 +0200slirp: document mbuf pointers and sizes
Signed-off-by Samuel Thibault <[email protected]>slirp/mbuf.h +0
-0@@ -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
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
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