p9_check_errors() validate PDU length
Subsystems (3)
PATCH 1/1 p9_check_errors() validate PDU length
p9_check_errors() does not validate the size of the PDU read
in p9_parse_header(). Any size can be passed, provoking out-of-bound reads.
---
As suggested by Dominique:
https://lkml.org/lkml/2018/7/9/688
Such check is not enough as it will prevent to read more than how it has been allocated but it won't prevent to read more than how it has been read So this patch will require some more changes to prevent bad sizes.
net/9p/client.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
net/9p/client.c +4 -1
@@ -520,10 +520,13 @@ EXPORT_SYMBOL(p9_parse_header);static int p9_check_errors (struct p9_client *c,struct p9_req_t *req) {int8_t type; +int32_t size;int err;int ecode; - err =p9_parse_header (req->rc,NULL , &type,NULL ,0 ); + err =p9_parse_header (req->rc, &size, &type,NULL ,0 ); +if (size > req->rc->capacity) +return -EINVAL ;/* * dump the response from server * This should be after check errors which poplulate pdu_fcall.
Signature · 1 line
2.11.0
Dominique Martinet
maintainer(extra Cc: Al Viro, as it's kind of the continuation of the other thread)
Tomas Bortoli wrote on Tue, Jul 10, 2018:
Quoted 2 lines
p9_check_errors()does not validate the size of the PDU read
inp9_parse_header(). Any size can be passed, provoking out-of-bound reads.
cf. what I just said in other patch, I think this check needs to be moved up to p9_parse_header as p9_check_zc_error has the same problem.
Also, they really need to check against the actual read size, not just capacity.
For virtio/rdma, something like this ought to fix pdu->size, then p9_parse_header can just never overwrite it (untested but it's useless on its own, I'll test the full patch with the parse header change)
net/9p/trans_rdma.c +1 -0
@@ -320,6 +320,7 @@ recv_done(struct ib_cq *cq, struct ib_wc *wc)if (wc->status !=IB_WC_SUCCESS ) goto err_out; + c->rc->size = wc->byte_len; err =p9_parse_header (c->rc,NULL ,NULL , &tag,1 );if (err) goto err_out;
net/9p/trans_virtio.c +3 -2
@@ -159,8 +159,10 @@ static void req_done(struct virtqueue *vq)spin_unlock_irqrestore (&chan->lock, flags);/* Wakeup if anyone waiting for VirtIO ring space. */ wake_up (chan->vc_wq); -if (len) +if (len) { + req->rc->size = len;p9_client_cb (chan->client, req,REQ_STATUS_RCVD ); + } } } --
Dominique Martinet | Asmadeus
Hi Tomas,
Quoted 21 lines · Tomas Bortoli
p9_check_errors()does not validate the size of the PDU read
inp9_parse_header(). Any size can be passed, provoking out-of-bound reads.Signed-off-by Tomas Bortoli <[email protected]>Reported-by [email protected]---
As suggested by Dominique:
https://lkml.org/lkml/2018/7/9/688
Such check is not enough as it will prevent to read more than how it has been allocated but it won't prevent to read more than how it has been read So this patch will require some more changes to prevent bad sizes.net/9p/client.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)net/9p/client.c +0
-0@@ -520,10 +520,13 @@ EXPORT_SYMBOL(p9_parse_header);static int p9_check_errors (struct p9_client *c,struct p9_req_t *req)
net/9p/client.c EXPORT_SYMBOL(p9_parse_header); · line 529
Quoted 10 lines
net/9p/client.c +4
-1{int8_t type; +int32_t size;int err;int ecode; - err =p9_parse_header (req->rc,NULL , &type,NULL ,0 ); + err =p9_parse_header (req->rc, &size, &type,NULL ,0 ); +if (size > req->rc->capacity) +return -EINVAL ;
I think we should print some error logs here to remind uppper users.
Thanks,
Jun
Quoted 4 lines
net/9p/client.c +0
-0/* * dump the response from server * This should be after check errors which poplulate pdu_fcall.
Dominique Martinet
maintainerTomas Bortoli wrote on Tue, Jul 10, 2018:
Quoted 5 lines
As suggested by Dominique:
https://lkml.org/lkml/2018/7/9/688
Such check is not enough as it will prevent to read more than how it has been allocated but it won't prevent to read more than how it has been read So this patch will require some more changes to prevent bad sizes.
Sorry, I'm the one who suggested to put a note after the commit message and I didn't see it.
Let's get the proper fix right away, it's not much further.
Quoted 5 lines
Also, they really need to check against the actual read size, not just capacity.
For virtio/rdma, something like this ought to fixpdu->size, thenp9_parse_headercan just never overwrite it (untested but it's useless on its own, I'll test the full patch with the parse header change)
I actually took the time to test a bit; I had only suggested something for virtio/rdma because I had assumed trans_fd (the socket transport actually used by syzbot) was setting the length in the fcall, but I read that code too fast this morning and it is not (it only sets the size in its private struct)
Something like that ought to work for trans_fd:
net/9p/trans_fd.c +1 -0
@@ -369,6 +370,7 @@ static void p9_read_work(struct work_struct *work)*/ if ((m->req) && (m->rc.offset == m->rc.capacity)) {p9_debug (P9_DEBUG_TRANS ,"got new packet\n" ); + m->req->rc->size = m->rc.offset;spin_lock (&m->client->lock);if (m->req->status !=REQ_STATUS_ERROR ) status =REQ_STATUS_RCVD ;
---
This however gets more complicated once you start factoring in that change I suggested about p9_parse_header not setting size (and checking size) because trans_fd relies on it; so I'm not sure how we should proceed.
Do you have a working 9p tcp server to test changes are valid, or are you only working off the syzbot reproducer?
In the first place, are you willing to take the time to do that bigger fix?
At this point I can either help you get a working setup and let you do the rest, or just finish the bigger patch myself and add you as whatever tag you feel comfortable with (persumably Signed-off-by)
Thanks again for starting this,
Signature · 1 line
Dominique Martinet | Asmadeus
Quoted 10 lines · Dominique Martinet
Tomas Bortoli wrote on Tue, Jul 10, 2018:
Quoted 5 lines
As suggested by Dominique:
https://lkml.org/lkml/2018/7/9/688
Such check is not enough as it will prevent to read more than how it has been allocated but it won't prevent to read more than how it has been read So this patch will require some more changes to prevent bad sizes.Sorry, I'm the one who suggested to put a note after the commit message and I didn't see it.
Let's get the proper fix right away, it's not much further.
I agree.
Quoted 27 lines
Quoted 6 lines
Also, they really need to check against the actual read size, not just capacity.
For virtio/rdma, something like this ought to fixpdu->size, thenp9_parse_headercan just never overwrite it (untested but it's useless on its own, I'll test the full patch with the parse header change)I actually took the time to test a bit; I had only suggested something for virtio/rdma because I had assumed
trans_fd(the socket transport actually used by syzbot) was setting the length in the fcall, but I read that code too fast this morning and it is not (it only sets the size in its private struct)Something like that ought to work for
trans_fd:net/9p/trans_fd.c +1
-1@@ -369,6 +370,7 @@ static void p9_read_work(struct work_struct *work)*/ if ((m->req) && (m->rc.offset == m->rc.capacity)) {p9_debug (P9_DEBUG_TRANS ,"got new packet\n" ); + m->req->rc->size = m->rc.offset;spin_lock (&m->client->lock);if (m->req->status !=REQ_STATUS_ERROR ) status =REQ_STATUS_RCVD ; ---
Quoted 4 lines
This however gets more complicated once you start factoring in that change I suggested about
p9_parse_headernot setting size (and checking size) becausetrans_fdrelies on it; so I'm not sure how we should proceed.
Mmh, me neither. I don't see where the *actual* PDU length is stored.
Quoted 3 lines
Do you have a working 9p tcp server to test changes are valid, or are you only working off the syzbot reproducer?
No, I was just using the reproducer to test.
Quoted 2 lines
In the first place, are you willing to take the time to do that bigger fix?
Yes.
Quoted 6 lines
At this point I can either help you get a working setup and let you do the rest, or just finish the bigger patch myself and add you as whatever tag you feel comfortable with (persumably Signed-off-by)
Thanks again for starting this,
For me both ways are good. Signed-off-by will be good.
You know for sure more than me about 9p as I started delving it for the first time yesterday. I can also make and test a patch but I'll need to understand more about it. Let me know if you find out more.
Dominique Martinet
maintainerTomas Bortoli wrote on Tue, Jul 10, 2018:
Quoted 6 lines
Quoted 4 lines
This however gets more complicated once you start factoring in that change I suggested about
p9_parse_headernot setting size (and checking size) becausetrans_fdrelies on it; so I'm not sure how we should proceed.Mmh, me neither. I don't see where the *actual* PDU length is stored.
That's precisely the problem, none of the transports actually write in the pdu how much has been read.
Instead, we blindly trust how much the client says it has written and p9_parse_header will write pdu->size which is then used as the 'actual' PDU length.
With the three lines I added in each file all transports will store the PDU length in pdu->size so my following suggestion would be to just check that in p9_parse_header the length read from the packet match that of the received data and flag any packet where this is wrong as invalid.
The only problem with that is that TCP will use p9_parse_header with a partial packet to know how much it needs to read, so that modification needs actual testing (hence my question below) as it is more intrusive than a simple boundary check.
I'd suggest implementing some logic like already here with the if (pdu->size == 0) -- only check if pdu->size was previously set, and set it if it wasn't... But feel free to try something else.
With that done I don't expect more problems but if I do it there's little point in making you do it as well ;)
Quoted 9 lines
Quoted 2 lines
Do you have a working 9p tcp server to test changes are valid, or are you only working off the syzbot reproducer?
No, I was just using the reproducer to test.
Quoted 2 lines
In the first place, are you willing to take the time to do that bigger fix?
Yes.
Ok, let's get you a working setup for TCP then.
You need to install a 9p server, the two I'm aware of are diod and nfs-ganesha (I'm using ganesha); once you have a working config you can mount with something like this:
mount -t 9p -o aname=path,trans=fd <server-ip> <mount-point>
Once you have that you should be able to fiddle with things until it works as expected.
(for virtio I use qemu, that's probably easy to test as well; for RDMA you can use rxe to setup a virtual interface (with rxe_cfg) and then use either diod or ganesha again but the setup is more complicated so it's OK to leave that aside for now)
Quoted 4 lines
For me both ways are good. Signed-off-by will be good.
You know for sure more than me about 9p as I started delving it for the first time yesterday. I can also make and test a patch but I'll need to understand more about it. Let me know if you find out more.
There's a saying about giving a fish or teaching how to fish, so let's have you try if you're motivated :)
I don't think much is left if you can sew the pieces together, the most difficult part will probably to set the test environment up if you want to do this properly.
Please ask if you run into any trouble,
Signature · 1 line
Dominique Martinet | Asmadeus
Quoted 21 lines · Tomas Bortoli
p9_check_errors()does not validate the size of the PDU read
inp9_parse_header(). Any size can be passed, provoking out-of-bound reads.Signed-off-by Tomas Bortoli <[email protected]>Reported-by [email protected]---
As suggested by Dominique:
https://lkml.org/lkml/2018/7/9/688
Such check is not enough as it will prevent to read more than how it has been allocated but it won't prevent to read more than how it has been read So this patch will require some more changes to prevent bad sizes.net/9p/client.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)net/9p/client.c +0
-0@@ -520,10 +520,13 @@ EXPORT_SYMBOL(p9_parse_header);static int p9_check_errors (struct p9_client *c,struct p9_req_t *req)
net/9p/client.c EXPORT_SYMBOL(p9_parse_header); · line 529
Quoted 10 lines
net/9p/client.c +4
-1{int8_t type; +int32_t size;int err;int ecode; - err =p9_parse_header (req->rc,NULL , &type,NULL ,0 ); + err =p9_parse_header (req->rc, &size, &type,NULL ,0 ); +if (size > req->rc->capacity) +return -EINVAL ;
Yes, currently 9p client is lacking of properly check for data from server. Maybe we should have a bigger patch to solve this problem.
Quoted 4 lines
net/9p/client.c +0
-0/* * dump the response from server * This should be after check errors which poplulate pdu_fcall.