Skip to content

Commit 728356d

Browse files
Tomas BortoliDominique Martinet
Tomas Bortoli
authored and
Dominique Martinet
committed
9p: Add refcount to p9_req_t
To avoid use-after-free(s), use a refcount to keep track of the usable references to any instantiated struct p9_req_t. This commit adds p9_req_put(), p9_req_get() and p9_req_try_get() as wrappers to kref_put(), kref_get() and kref_get_unless_zero(). These are used by the client and the transports to keep track of valid requests' references. p9_free_req() is added back and used as callback by kref_put(). Add SLAB_TYPESAFE_BY_RCU as it ensures that the memory freed by kmem_cache_free() will not be reused for another type until the rcu synchronisation period is over, so an address gotten under rcu read lock is safe to inc_ref() without corrupting random memory while the lock is held. Link: http://lkml.kernel.org/r/[email protected] Co-developed-by: Dominique Martinet <[email protected]> Signed-off-by: Tomas Bortoli <[email protected]> Reported-by: [email protected] Signed-off-by: Dominique Martinet <[email protected]>
1 parent 43cbcbe commit 728356d

File tree

6 files changed

+98
-12
lines changed

6 files changed

+98
-12
lines changed

include/net/9p/client.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ enum p9_req_status_t {
9494
struct p9_req_t {
9595
int status;
9696
int t_err;
97+
struct kref refcount;
9798
wait_queue_head_t wq;
9899
struct p9_fcall tc;
99100
struct p9_fcall rc;
@@ -233,6 +234,19 @@ int p9_client_lock_dotl(struct p9_fid *fid, struct p9_flock *flock, u8 *status);
233234
int p9_client_getlock_dotl(struct p9_fid *fid, struct p9_getlock *fl);
234235
void p9_fcall_fini(struct p9_fcall *fc);
235236
struct p9_req_t *p9_tag_lookup(struct p9_client *, u16);
237+
238+
static inline void p9_req_get(struct p9_req_t *r)
239+
{
240+
kref_get(&r->refcount);
241+
}
242+
243+
static inline int p9_req_try_get(struct p9_req_t *r)
244+
{
245+
return kref_get_unless_zero(&r->refcount);
246+
}
247+
248+
int p9_req_put(struct p9_req_t *r);
249+
236250
void p9_client_cb(struct p9_client *c, struct p9_req_t *req, int status);
237251

238252
int p9_parse_header(struct p9_fcall *, int32_t *, int8_t *, int16_t *, int);

net/9p/client.c

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,18 @@ p9_tag_alloc(struct p9_client *c, int8_t type, unsigned int max_size)
307307
if (tag < 0)
308308
goto free;
309309

310+
/* Init ref to two because in the general case there is one ref
311+
* that is put asynchronously by a writer thread, one ref
312+
* temporarily given by p9_tag_lookup and put by p9_client_cb
313+
* in the recv thread, and one ref put by p9_tag_remove in the
314+
* main thread. The only exception is virtio that does not use
315+
* p9_tag_lookup but does not have a writer thread either
316+
* (the write happens synchronously in the request/zc_request
317+
* callback), so p9_client_cb eats the second ref there
318+
* as the pointer is duplicated directly by virtqueue_add_sgs()
319+
*/
320+
refcount_set(&req->refcount.refcount, 2);
321+
310322
return req;
311323

312324
free:
@@ -330,10 +342,21 @@ struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag)
330342
struct p9_req_t *req;
331343

332344
rcu_read_lock();
345+
again:
333346
req = idr_find(&c->reqs, tag);
334-
/* There's no refcount on the req; a malicious server could cause
335-
* us to dereference a NULL pointer
336-
*/
347+
if (req) {
348+
/* We have to be careful with the req found under rcu_read_lock
349+
* Thanks to SLAB_TYPESAFE_BY_RCU we can safely try to get the
350+
* ref again without corrupting other data, then check again
351+
* that the tag matches once we have the ref
352+
*/
353+
if (!p9_req_try_get(req))
354+
goto again;
355+
if (req->tc.tag != tag) {
356+
p9_req_put(req);
357+
goto again;
358+
}
359+
}
337360
rcu_read_unlock();
338361

339362
return req;
@@ -347,7 +370,7 @@ EXPORT_SYMBOL(p9_tag_lookup);
347370
*
348371
* Context: Any context.
349372
*/
350-
static void p9_tag_remove(struct p9_client *c, struct p9_req_t *r)
373+
static int p9_tag_remove(struct p9_client *c, struct p9_req_t *r)
351374
{
352375
unsigned long flags;
353376
u16 tag = r->tc.tag;
@@ -356,11 +379,23 @@ static void p9_tag_remove(struct p9_client *c, struct p9_req_t *r)
356379
spin_lock_irqsave(&c->lock, flags);
357380
idr_remove(&c->reqs, tag);
358381
spin_unlock_irqrestore(&c->lock, flags);
382+
return p9_req_put(r);
383+
}
384+
385+
static void p9_req_free(struct kref *ref)
386+
{
387+
struct p9_req_t *r = container_of(ref, struct p9_req_t, refcount);
359388
p9_fcall_fini(&r->tc);
360389
p9_fcall_fini(&r->rc);
361390
kmem_cache_free(p9_req_cache, r);
362391
}
363392

393+
int p9_req_put(struct p9_req_t *r)
394+
{
395+
return kref_put(&r->refcount, p9_req_free);
396+
}
397+
EXPORT_SYMBOL(p9_req_put);
398+
364399
/**
365400
* p9_tag_cleanup - cleans up tags structure and reclaims resources
366401
* @c: v9fs client struct
@@ -376,7 +411,9 @@ static void p9_tag_cleanup(struct p9_client *c)
376411
rcu_read_lock();
377412
idr_for_each_entry(&c->reqs, req, id) {
378413
pr_info("Tag %d still in use\n", id);
379-
p9_tag_remove(c, req);
414+
if (p9_tag_remove(c, req) == 0)
415+
pr_warn("Packet with tag %d has still references",
416+
req->tc.tag);
380417
}
381418
rcu_read_unlock();
382419
}
@@ -400,6 +437,7 @@ void p9_client_cb(struct p9_client *c, struct p9_req_t *req, int status)
400437

401438
wake_up(&req->wq);
402439
p9_debug(P9_DEBUG_MUX, "wakeup: %d\n", req->tc.tag);
440+
p9_req_put(req);
403441
}
404442
EXPORT_SYMBOL(p9_client_cb);
405443

@@ -640,9 +678,10 @@ static int p9_client_flush(struct p9_client *c, struct p9_req_t *oldreq)
640678
* if we haven't received a response for oldreq,
641679
* remove it from the list
642680
*/
643-
if (oldreq->status == REQ_STATUS_SENT)
681+
if (oldreq->status == REQ_STATUS_SENT) {
644682
if (c->trans_mod->cancelled)
645683
c->trans_mod->cancelled(c, oldreq);
684+
}
646685

647686
p9_tag_remove(c, req);
648687
return 0;
@@ -679,6 +718,8 @@ static struct p9_req_t *p9_client_prepare_req(struct p9_client *c,
679718
return req;
680719
reterr:
681720
p9_tag_remove(c, req);
721+
/* We have to put also the 2nd reference as it won't be used */
722+
p9_req_put(req);
682723
return ERR_PTR(err);
683724
}
684725

@@ -713,6 +754,8 @@ p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...)
713754

714755
err = c->trans_mod->request(c, req);
715756
if (err < 0) {
757+
/* write won't happen */
758+
p9_req_put(req);
716759
if (err != -ERESTARTSYS && err != -EFAULT)
717760
c->status = Disconnected;
718761
goto recalc_sigpending;
@@ -2238,7 +2281,7 @@ EXPORT_SYMBOL(p9_client_readlink);
22382281

22392282
int __init p9_client_init(void)
22402283
{
2241-
p9_req_cache = KMEM_CACHE(p9_req_t, 0);
2284+
p9_req_cache = KMEM_CACHE(p9_req_t, SLAB_TYPESAFE_BY_RCU);
22422285
return p9_req_cache ? 0 : -ENOMEM;
22432286
}
22442287

net/9p/trans_fd.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ struct p9_conn {
132132
struct list_head req_list;
133133
struct list_head unsent_req_list;
134134
struct p9_req_t *req;
135+
struct p9_req_t *wreq;
135136
char tmp_buf[7];
136137
struct p9_fcall rc;
137138
int wpos;
@@ -383,6 +384,7 @@ static void p9_read_work(struct work_struct *work)
383384
m->rc.sdata = NULL;
384385
m->rc.offset = 0;
385386
m->rc.capacity = 0;
387+
p9_req_put(m->req);
386388
m->req = NULL;
387389
}
388390

@@ -472,6 +474,8 @@ static void p9_write_work(struct work_struct *work)
472474
m->wbuf = req->tc.sdata;
473475
m->wsize = req->tc.size;
474476
m->wpos = 0;
477+
p9_req_get(req);
478+
m->wreq = req;
475479
spin_unlock(&m->client->lock);
476480
}
477481

@@ -492,8 +496,11 @@ static void p9_write_work(struct work_struct *work)
492496
}
493497

494498
m->wpos += err;
495-
if (m->wpos == m->wsize)
499+
if (m->wpos == m->wsize) {
496500
m->wpos = m->wsize = 0;
501+
p9_req_put(m->wreq);
502+
m->wreq = NULL;
503+
}
497504

498505
end_clear:
499506
clear_bit(Wworksched, &m->wsched);
@@ -694,6 +701,7 @@ static int p9_fd_cancel(struct p9_client *client, struct p9_req_t *req)
694701
if (req->status == REQ_STATUS_UNSENT) {
695702
list_del(&req->req_list);
696703
req->status = REQ_STATUS_FLSHD;
704+
p9_req_put(req);
697705
ret = 0;
698706
}
699707
spin_unlock(&client->lock);
@@ -711,6 +719,7 @@ static int p9_fd_cancelled(struct p9_client *client, struct p9_req_t *req)
711719
spin_lock(&client->lock);
712720
list_del(&req->req_list);
713721
spin_unlock(&client->lock);
722+
p9_req_put(req);
714723

715724
return 0;
716725
}

net/9p/trans_rdma.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ send_done(struct ib_cq *cq, struct ib_wc *wc)
365365
c->busa, c->req->tc.size,
366366
DMA_TO_DEVICE);
367367
up(&rdma->sq_sem);
368+
p9_req_put(c->req);
368369
kfree(c);
369370
}
370371

net/9p/trans_virtio.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req)
207207
return 1;
208208
}
209209

210+
/* Reply won't come, so drop req ref */
211+
static int p9_virtio_cancelled(struct p9_client *client, struct p9_req_t *req)
212+
{
213+
p9_req_put(req);
214+
return 0;
215+
}
216+
210217
/**
211218
* pack_sg_list_p - Just like pack_sg_list. Instead of taking a buffer,
212219
* this takes a list of pages.
@@ -404,15 +411,18 @@ p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
404411
struct scatterlist *sgs[4];
405412
size_t offs;
406413
int need_drop = 0;
414+
int kicked = 0;
407415

408416
p9_debug(P9_DEBUG_TRANS, "virtio request\n");
409417

410418
if (uodata) {
411419
__le32 sz;
412420
int n = p9_get_mapped_pages(chan, &out_pages, uodata,
413421
outlen, &offs, &need_drop);
414-
if (n < 0)
415-
return n;
422+
if (n < 0) {
423+
err = n;
424+
goto err_out;
425+
}
416426
out_nr_pages = DIV_ROUND_UP(n + offs, PAGE_SIZE);
417427
if (n != outlen) {
418428
__le32 v = cpu_to_le32(n);
@@ -428,8 +438,10 @@ p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
428438
} else if (uidata) {
429439
int n = p9_get_mapped_pages(chan, &in_pages, uidata,
430440
inlen, &offs, &need_drop);
431-
if (n < 0)
432-
return n;
441+
if (n < 0) {
442+
err = n;
443+
goto err_out;
444+
}
433445
in_nr_pages = DIV_ROUND_UP(n + offs, PAGE_SIZE);
434446
if (n != inlen) {
435447
__le32 v = cpu_to_le32(n);
@@ -498,6 +510,7 @@ p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
498510
}
499511
virtqueue_kick(chan->vq);
500512
spin_unlock_irqrestore(&chan->lock, flags);
513+
kicked = 1;
501514
p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
502515
err = wait_event_killable(req->wq, req->status >= REQ_STATUS_RCVD);
503516
/*
@@ -518,6 +531,10 @@ p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
518531
}
519532
kvfree(in_pages);
520533
kvfree(out_pages);
534+
if (!kicked) {
535+
/* reply won't come */
536+
p9_req_put(req);
537+
}
521538
return err;
522539
}
523540

@@ -750,6 +767,7 @@ static struct p9_trans_module p9_virtio_trans = {
750767
.request = p9_virtio_request,
751768
.zc_request = p9_virtio_zc_request,
752769
.cancel = p9_virtio_cancel,
770+
.cancelled = p9_virtio_cancelled,
753771
/*
754772
* We leave one entry for input and one entry for response
755773
* headers. We also skip one more entry to accomodate, address

net/9p/trans_xen.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ static int p9_xen_request(struct p9_client *client, struct p9_req_t *p9_req)
185185
ring->intf->out_prod = prod;
186186
spin_unlock_irqrestore(&ring->lock, flags);
187187
notify_remote_via_irq(ring->irq);
188+
p9_req_put(p9_req);
188189

189190
return 0;
190191
}

0 commit comments

Comments
 (0)