bsg: mitigate read/write abuse, block uaccess in release
Subsystems (3)
PATCH v4 1/1 bsg: mitigate read/write abuse, block uaccess in release
As Al Viro noted in commit 128394eff343 ("sg_write()/bsg_write() is not fit to be called under KERNEL_DS"), bsg improperly accesses userspace memory outside the provided buffer, permitting kernel memory corruption via splice().
But bsg doesn't just do it on ->write(), also on ->read() and ->release().
As a band-aid, make sure that the ->read() and ->write() handlers can not be called in weird contexts (kernel context or credentials different from file opener), like for ib_safe_file_access().
Also, completely prevent user memory accesses in ->release() context.
Also put a deprecation warning in the read/write handlers.
This is similar to commit 26b5b874aff5 ("scsi: sg: mitigate read/write abuse"), which deals with similar issues in /dev/sg*.
---
Resending for bsg as requested by Christoph Hellwig.
("PATCH v4" is a bit of a misnomer, but probably less confusing than anything else I could have put in the subject line? Is there a canonical way to deal with patch series that have been split up?)
changes:
- fix control flow in bsg_transport_complete_rq (v1 had a bug there)
- extract bsg part, since sg part has already landed separately (Christoph Hellwig)
- put deprecation warning in read/write handlers, similar to Linus' suggested patch for sg
block/bsg-lib.c | 7 +++++-- block/bsg.c | 43 ++++++++++++++++++++++++++++++++++--------- include/linux/bsg.h | 3 ++- 3 files changed, 41 insertions(+), 12 deletions(-)
block/bsg-lib.c +5 -2
@@ -53,7 +53,8 @@ static int bsg_transport_fill_hdr(struct request *rq, struct sg_io_v4 *hdr,return 0 ; } -static int bsg_transport_complete_rq (struct request *rq,struct sg_io_v4 *hdr) +static int bsg_transport_complete_rq (struct request *rq,struct sg_io_v4 *hdr, +bool cleaning_up) {struct bsg_job *job =blk_mq_rq_to_pdu (rq);int ret =0 ;
@@ -79,7 +80,9 @@ static int bsg_transport_complete_rq(struct request *rq, struct sg_io_v4 *hdr)if (job->reply_len && hdr->response) {int len =min (hdr->max_response_len, job->reply_len); -if (copy_to_user (uptr64 (hdr->response), job->reply, len)) +if (cleaning_up) + ret = -EINVAL ; +else if (copy_to_user (uptr64 (hdr->response), job->reply, len)) ret = -EFAULT ;else hdr->response_len = len;
block/bsg.c +34 -9
106 lines not on this page · raw message
include/linux/bsg.h +2 -1
@@ -11,7 +11,8 @@ struct bsg_ops {int (*check_proto)(struct sg_io_v4 *hdr);int (*fill_hdr)(struct request *rq,struct sg_io_v4 *hdr,fmode_t mode); -int (*complete_rq)(struct request *rq,struct sg_io_v4 *hdr); +int (*complete_rq)(struct request *rq,struct sg_io_v4 *hdr, +bool cleaning_up);void (*free_rq)(struct request *rq); };
Signature · 1 line
2.18.0.203.gfac676dfb9-goog
Quoted 2 lines · Jann Horn
But bsg doesn't just do it on ->write(), also on ->read() and ->release().
I think Jens already applied Christoph's patch to just remove the bsg read/write entirely into the 4.19 tree.
Linus
Quoted 7 lines · Linus Torvalds
Quoted 2 lines · Jann Horn
But bsg doesn't just do it on ->write(), also on ->read() and ->release().
I think Jens already applied Christoph's patch to just remove the bsg read/write entirely into the 4.19 tree.
I thought Christoph wanted this one for stable. Maybe I misunderstood.
Quoted 2 lines · Jann Horn
I thought Christoph wanted this one for stable. Maybe I misunderstood.
If we can remove it in mainline, we should remove it in stable too. There's no situation where compatibility would matter in stable but not in mainline.
Linus