io_uring: fix possible use after free to sqd
PATCH 1/1 io_uring: fix possible use after free to sqd
Reading codes finds a possible use after free issue to sqd:
thread1 | thread2
==> io_attach_sq_data() |
===> sqd = ctx_attach->sq_data;|
| ==> io_put_sq_data()
| ===> refcount_dec_and_test(&sqd->refs)
| If sqd->refs is zero, will free sqd.
|
===> refcount_inc(&sqd->refs); |
|
| ====> kfree(sqd);
===> now use after free to sqd |Use refcount_inc_not_zero() to fix this issue.
--- fs/io_uring.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
fs/io_uring.c +5 -1
@@ -6868,7 +6868,11 @@ static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)return ERR_PTR (-EINVAL ); } -refcount_inc (&sqd->refs); +if (!refcount_inc_not_zero (&sqd->refs)) { +fdput (f); +return ERR_PTR (-EINVAL ); + } +fdput (f);return sqd; }
Signature · 1 line
2.17.2
Quoted 13 lines · Xiaoguang Wang
Reading codes finds a possible use after free issue to sqd:
thread1 | thread2 ==> io_attach_sq_data() | ===> sqd = ctx_attach->sq_data;| | ==> io_put_sq_data() | ===> refcount_dec_and_test(&sqd->refs) | If sqd->refs is zero, will free sqd. | ===> refcount_inc(&sqd->refs); | | | ====> kfree(sqd); ===> now use after free to sqd |
IIUC the io_attach_sq_data() is called only during the setup of an io_uring context, before that the fd is returned to the user space.
So, I'm not sure a second thread can call io_put_sq_data() while the first thread is in io_attach_sq_data().
Can you check if this situation can really happen?
Thanks,
Stefano
Quoted 27 lines
Use
refcount_inc_not_zero()to fix this issue.Signed-off-by Xiaoguang Wang <[email protected]>--- fs/io_uring.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)fs/io_uring.c +5
-1@@ -6868,7 +6868,11 @@ static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)return ERR_PTR (-EINVAL ); } -refcount_inc (&sqd->refs); +if (!refcount_inc_not_zero (&sqd->refs)) { +fdput (f); +return ERR_PTR (-EINVAL ); + } +fdput (f);return sqd; }Signature · 1 line
2.17.2
hi,
Quoted 15 lines · Xiaoguang Wang
Quoted 4 lines
Reading codes finds a possible use after free issue to sqd:
thread1 | thread2
==>io_attach_sq_data()|
===> sqd = ctx_attach->sq_data;|Quoted 9 lines
| ==> io_put_sq_data() | ===> refcount_dec_and_test(&sqd->refs) | If sqd->refs is zero, will free sqd. | ===> refcount_inc(&sqd->refs); | | | ====> kfree(sqd);===> now use after free to sqd |
Quoted 2 lines
IIUC the
io_attach_sq_data()is called only during the setup of anio_uringcontext, before that the fd is returned to the user space.
Sorry I didn't make it clear in commit message.
In io_attach_sq_data(), we'll try to attach to a previous io_uring instance indicated by p->wq_fd, this p->wq_fd could be closed independently.
Regards,
Xiaoguang Wang
Quoted 36 lines
So, I'm not sure a second thread can call
io_put_sq_data()while the first thread is inio_attach_sq_data().Can you check if this situation can really happen?
Thanks,
StefanoQuoted 27 lines
Use
refcount_inc_not_zero()to fix this issue.Signed-off-by Xiaoguang Wang <[email protected]>--- fs/io_uring.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)fs/io_uring.c +5
-1@@ -6868,7 +6868,11 @@ static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)return ERR_PTR (-EINVAL ); } -refcount_inc (&sqd->refs); +if (!refcount_inc_not_zero (&sqd->refs)) { +fdput (f); +return ERR_PTR (-EINVAL ); + } +fdput (f);return sqd; }Signature · 1 line
2.17.2
Quoted 17 lines · Xiaoguang Wang
hi閿涳拷
Quoted 15 lines · Xiaoguang Wang
Quoted 13 lines
Reading codes finds a possible use after free issue to sqd:
thread1 | thread2
==>io_attach_sq_data()|
===> sqd = ctx_attach->sq_data;|| ==> io_put_sq_data() | ===> refcount_dec_and_test(&sqd->refs) | If sqd->refs is zero, will free sqd. | ===> refcount_inc(&sqd->refs); | | | ====> kfree(sqd);===> now use after free to sqd |
Quoted 5 lines
Quoted 2 lines
IIUC the
io_attach_sq_data()is called only during the setup of anio_uringcontext, before that the fd is returned to the user space.Sorry I didn't make it clear in commit message.
Inio_attach_sq_data(), we'll try to attach to a previousio_uringinstance indicated byp->wq_fd, thisp->wq_fdcould be closed independently.
Thanks to clarify! Got it.
Also in this case, IIUC io_put_sq_data() is called only when io_uring_release() is invoked on the previous io_uring instance.
Since we are taking a reference with fdget at the begin of io_attach_sq_data() and we release this reference only at the end of io_attach_sq_data(), can io_put_sq_data() runs in another thread while we have the reference?
Thanks,
Stefano
Quoted 39 lines
Regards,
Xiaoguang WangQuoted 35 lines
So, I'm not sure a second thread can call
io_put_sq_data()while the first thread is inio_attach_sq_data().Can you check if this situation can really happen?
Thanks,
StefanoQuoted 26 lines
Use
refcount_inc_not_zero()to fix this issue.Signed-off-by Xiaoguang Wang <[email protected]>--- fs/io_uring.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)fs/io_uring.c +5
-1@@ -6868,7 +6868,11 @@ static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)return ERR_PTR (-EINVAL ); } -refcount_inc (&sqd->refs); +if (!refcount_inc_not_zero (&sqd->refs)) { +fdput (f); +return ERR_PTR (-EINVAL ); + } +fdput (f);return sqd; }Signature · 1 line
2.17.2
Quoted 24 lines · Xiaoguang Wang
Quoted 17 lines
hi閿涳拷
Quoted 15 lines · Xiaoguang Wang
Quoted 13 lines
Reading codes finds a possible use after free issue to sqd:
thread1 | thread2
==>io_attach_sq_data()|
===> sqd = ctx_attach->sq_data;|| ==> io_put_sq_data() | ===> refcount_dec_and_test(&sqd->refs) | If sqd->refs is zero, will free sqd. | ===> refcount_inc(&sqd->refs); | | | ====> kfree(sqd);===> now use after free to sqd |
Quoted 5 lines
Quoted 2 lines
IIUC the
io_attach_sq_data()is called only during the setup of anio_uringcontext, before that the fd is returned to the user space.Sorry I didn't make it clear in commit message.
Inio_attach_sq_data(), we'll try to attach to a previousio_uringinstance indicated byp->wq_fd, thisp->wq_fdcould be closed independently.
Quoted 8 lines
Thanks to clarify! Got it.
Also in this case, IIUC
io_put_sq_data()is called only whenio_uring_release()is invoked on the previousio_uringinstance.Since we are taking a reference with fdget at the begin of
io_attach_sq_data()and we release this reference only at the end ofio_attach_sq_data(), canio_put_sq_data()runs in another thread while we have the reference?
Oh, right, thanks. Jens, you can ignore this patch, thanks.
Regards,
Xiaoguang Wang
Quoted 43 lines
Thanks,
StefanoQuoted 39 lines
Regards,
Xiaoguang WangQuoted 35 lines
So, I'm not sure a second thread can call
io_put_sq_data()while the first thread is inio_attach_sq_data().Can you check if this situation can really happen?
Thanks,
StefanoQuoted 26 lines
Use
refcount_inc_not_zero()to fix this issue.Signed-off-by Xiaoguang Wang <[email protected]>--- fs/io_uring.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)fs/io_uring.c +5
-1@@ -6868,7 +6868,11 @@ static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)return ERR_PTR (-EINVAL ); } -refcount_inc (&sqd->refs); +if (!refcount_inc_not_zero (&sqd->refs)) { +fdput (f); +return ERR_PTR (-EINVAL ); + } +fdput (f);return sqd; }Signature · 1 line
2.17.2