9p: validate PDU length
Subsystems (3)
PATCH 1/1 9p: validate PDU length
This commit adds length check for the PDU size.
The size contained in the header has to match the actual size, except for TCP (trans_fd.c) where actual length is not known ahead and the header's length will be checked only against the validity range.
--- net/9p/client.c | 25 ++++++++++++++++--------- net/9p/trans_fd.c | 5 ++++- net/9p/trans_rdma.c | 1 + net/9p/trans_virtio.c | 4 +++- 4 files changed, 24 insertions(+), 11 deletions(-)
net/9p/client.c +16 -9
@@ -477,20 +477,11 @@ p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type, int16_t *tag,int err; pdu->offset =0 ; -if (pdu->size ==0 ) - pdu->size =7 ; err =p9pdu_readf (pdu,0 ,"dbw" , &r_size, &r_type, &r_tag);if (err) goto rewind_and_exit; - pdu->size = r_size; - pdu->id = r_type; - pdu->tag = r_tag; - -p9_debug (P9_DEBUG_9P ,"<<< size=%d type: %d tag: %d\n" , - pdu->size, pdu->id, pdu->tag); -if (type) *type = r_type;if (tag)
@@ -498,6 +489,16 @@ p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type, int16_t *tag,if (size) *size = r_size; +if (pdu->size != r_size || r_size <7 ) { + err = -EINVAL ; + goto rewind_and_exit; + } + + pdu->id = r_type; + pdu->tag = r_tag; + +p9_debug (P9_DEBUG_9P ,"<<< size=%d type: %d tag: %d\n" , + pdu->size, pdu->id, pdu->tag); rewind_and_exit:if (rewind)
@@ -524,6 +525,12 @@ static int p9_check_errors(struct p9_client *c, struct p9_req_t *req)int ecode; err =p9_parse_header (req->rc,NULL , &type,NULL ,0 ); +if (req->rc->size >= c->msize) { +p9_debug (P9_DEBUG_ERROR , +"requested packet size too big: %d\n" , + req->rc->size); +return -EIO ; + }/* * dump the response from server * This should be after check errors which poplulate pdu_fcall.
net/9p/trans_fd.c +4 -1
@@ -324,7 +324,9 @@ 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 header\n" ); - err =p9_parse_header (&m->rc,NULL ,NULL ,NULL ,0 ); +/* Header size */ + m->rc.size =7 ; + err =p9_parse_header (&m->rc, &m->rc.size,NULL ,NULL ,0 );if (err) {p9_debug (P9_DEBUG_ERROR ,"error parsing header: %d\n" , err);
@@ -369,6 +371,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 ;
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 -1
@@ -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 ); + } } }
Signature · 1 line
2.11.0
Dominique Martinet
maintainerTomas Bortoli wrote on Mon, Jul 23, 2018:
Quoted 8 lines
This commit adds length check for the PDU size.
The size contained in the header has to match the actual size, except for TCP (trans_fd.c) where actual length is not known ahead and the header's length will be checked only against the validity range.Signed-off-by Tomas Bortoli <[email protected]>Reported-by [email protected]
Ok, I've run some more in-depth testing this time and all appear in order.
Like I said last time, I cannot test the xen transport - if someone can point me to how to set it up or run some tests semi-regularily it'd be great.
Meanwhile, code looks good and appears to work, so I'll take this patch unless someone yells
Quoted 50 lines
--- net/9p/client.c | 25 ++++++++++++++++--------- net/9p/trans_fd.c | 5 ++++- net/9p/trans_rdma.c | 1 + net/9p/trans_virtio.c | 4 +++- 4 files changed, 24 insertions(+), 11 deletions(-)net/9p/client.c +10
-9@@ -477,20 +477,11 @@ p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type, int16_t *tag,int err; pdu->offset =0 ; -if (pdu->size ==0 ) - pdu->size =7 ; err =p9pdu_readf (pdu,0 ,"dbw" , &r_size, &r_type, &r_tag);if (err) goto rewind_and_exit; - pdu->size = r_size; - pdu->id = r_type; - pdu->tag = r_tag; - -p9_debug (P9_DEBUG_9P ,"<<< size=%d type: %d tag: %d\n" , - pdu->size, pdu->id, pdu->tag); -if (type) *type = r_type;if (tag)@@ -498,6 +489,16 @@ p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type, int16_t *tag,if (size) *size = r_size; +if (pdu->size != r_size || r_size <7 ) { + err = -EINVAL ; + goto rewind_and_exit; + } + + pdu->id = r_type; + pdu->tag = r_tag; + +p9_debug (P9_DEBUG_9P ,"<<< size=%d type: %d tag: %d\n" , + pdu->size, pdu->id, pdu->tag); rewind_and_exit:if (rewind)
net/9p/client.c static int p9_check_errors(struct p9_client *c, struct p9_req_t *req) · line 532
Quoted 8 lines
net/9p/client.c +5
-0@@ -524,6 +525,12 @@ static int p9_check_errors(struct p9_client *c, struct p9_req_t *req)int ecode; err =p9_parse_header (req->rc,NULL , &type,NULL ,0 ); +if (req->rc->size >= c->msize) { +p9_debug (P9_DEBUG_ERROR , +"requested packet size too big: %d\n" , + req->rc->size); +return -EIO ;
Indentation here looks wrong, I took the liberty of deindenting that return in my tree.
Quoted 55 lines
net/9p/client.c +1
-0+ }/* * dump the response from server * This should be after check errors which poplulate pdu_fcall. net/9p/trans_fd.c +4
-1@@ -324,7 +324,9 @@ 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 header\n" ); - err =p9_parse_header (&m->rc,NULL ,NULL ,NULL ,0 ); +/* Header size */ + m->rc.size =7 ; + err =p9_parse_header (&m->rc, &m->rc.size,NULL ,NULL ,0 );if (err) {p9_debug (P9_DEBUG_ERROR ,"error parsing header: %d\n" , err);@@ -369,6 +371,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 ;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
-1@@ -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 ); + } } }
Signature · 1 line
Dominique Martinet
Dominique Martinet
maintainerTomas Bortoli wrote on Mon, Jul 23, 2018:
net/9p/client.c static int p9_check_errors(struct p9_client *c, struct p9_req_t *req) · line 528
Quoted 9 lines
net/9p/client.c +1
-0@@ -524,6 +525,12 @@ static int p9_check_errors(struct p9_client *c, struct p9_req_t *req)int ecode; err =p9_parse_header (req->rc,NULL , &type,NULL ,0 ); +if (req->rc->size >= c->msize) {
I was looking at this again, I think it's more appropriate to use req->rc->capacity at this point like you did in the first version of the patch.
I had suggested msize in the common p9_parse_header function because that'd let us accept zc requests where the size in the pdu could be bigger than capacity, but this isn't the case in p9_check_errors.
If you're ok with this I'll edit your commit directly, this is less work for me than having to check a new patch.
Thanks,
Signature · 1 line
Dominique
Quoted 15 lines · Dominique Martinet
Tomas Bortoli wrote on Mon, Jul 23, 2018:
net/9p/client.c static int p9_check_errors(struct p9_client *c, struct p9_req_t *req) · line 528
Quoted 9 lines
net/9p/client.c +1
-0@@ -524,6 +525,12 @@ static int p9_check_errors(struct p9_client *c, struct p9_req_t *req)int ecode; err =p9_parse_header (req->rc,NULL , &type,NULL ,0 ); +if (req->rc->size >= c->msize) {I was looking at this again, I think it's more appropriate to use
req->rc->capacityat this point like you did in the first version of the patch.
Quoted 9 lines
I had suggested msize in the common
p9_parse_headerfunction because that'd let us accept zc requests where the size in the pdu could be bigger than capacity, but this isn't the case inp9_check_errors.If you're ok with this I'll edit your commit directly, this is less work for me than having to check a new patch.
Thanks,
Yes sure, just forward me the changes later.
Tomas