Skip to contentSkip to conversation

bsg: mitigate read/write abuse, block uaccess in release

PATCH In review Jann Horn 2 participants 4 messages Jul 16, 2018

Subsystems (3)
lore
Share & tools
lore

Jann Horn

author
#0 lore raw

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*.

Fixes 3d6392cfbd7d ("bsg: support for full generic block layer SG v3")

---
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
index 9419def8c017..e21f246526e2 100644
@@ -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
index dac37b6e00ec..c22bc359552a 100644
@@ -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
#1 lore raw
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

author
#2 lore raw
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.

#3 lore raw
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