Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3317048imu; Sun, 11 Nov 2018 12:13:42 -0800 (PST) X-Google-Smtp-Source: AJdET5dTV/pQSR3tyBVMufqboBJpejNW9XzdhvSpHDudZLSUOzdc+3tCGA2MhsrawcMGrRYhOzkN X-Received: by 2002:a62:8a0d:: with SMTP id y13-v6mr17469739pfd.142.1541967222817; Sun, 11 Nov 2018 12:13:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541967222; cv=none; d=google.com; s=arc-20160816; b=nyg09dDKAE/yV8j9uq1wi5x5eo69CJZxyKuijxGPJLUaWdwkCj5Q8szreu8Gw/hF6H s+8JrLuDZcUAKb8DLakNvjs+hTyy6IVw7HBWU59NUJv69OY8tXpl9niUt/tLN4LpNy4f vzr+9gKavyYrbmbwijNiR2PP8pbnpA987gPixkfIA3guo2BGzdyWvi4VWMw2aJzorlRO Wiz8IS0d20qv2lknygeMEqJIN9RJXuztDptaaY51bCPP2TpHf4aXH3gqo587ula99zM5 LMQ3cMLgkjup8raBjWHD4i+dnkyzb1t+U5ltX4GjBb45nCW93BSilRTklZwihR+Svj/b aCfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition; bh=U0RezNquXgkQmrCJQPoNGwlJiQZ4S3e0Uo4NM9/I1Fc=; b=dT5Y97YggNFyOOkuxh4wQ+rTv8HEktTb57W4lRaqOloXnvMTUgglBmcIftvKkQDoI/ FCtC3N8O6saianW+7Bt/QppD2Rl9bAqtZwv20S1jUg+g3MfQP5Eddl66pr3bq76b1Ow5 W7xsOrY3mzelhuTSjWDkSGAuHhWM1WWOFxo1I3Nz8dwrM2xDjKRqyFcbqt/H0YVmQEHc v244SoBZ5InLApCGW5C4NKR/mJ6EK9exO/o4/XZX3teeW2RnjQV25ZMRaarO6mNCvzOy d9nI5H7u4FGHxLvq2y+6kju4kde4fcAL1Hcm3/Ah/8elS/m85qVJbvpl16a9uDVwsPBa Br+w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l6si5797624pgg.592.2018.11.11.12.13.27; Sun, 11 Nov 2018 12:13:42 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731348AbeKLGC0 (ORCPT + 99 others); Mon, 12 Nov 2018 01:02:26 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:52650 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726709AbeKLGC0 (ORCPT ); Mon, 12 Nov 2018 01:02:26 -0500 Received: from [192.168.4.242] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1gLvsw-0000oG-EQ; Sun, 11 Nov 2018 19:59:06 +0000 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1gLvsU-0001dt-JO; Sun, 11 Nov 2018 19:58:38 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Douglas Gilbert" , "Jann Horn" , "Martin K. Petersen" Date: Sun, 11 Nov 2018 19:49:05 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 202/366] scsi: sg: mitigate read/write abuse In-Reply-To: X-SA-Exim-Connect-IP: 192.168.4.242 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.61-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Jann Horn commit 26b5b874aff5659a7e26e5b1997e3df2c41fa7fd upstream. As Al Viro noted in commit 128394eff343 ("sg_write()/bsg_write() is not fit to be called under KERNEL_DS"), sg improperly accesses userspace memory outside the provided buffer, permitting kernel memory corruption via splice(). But it doesn't just do it on ->write(), also on ->read(). 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(). If someone needs to use these interfaces from different security contexts, a new interface should be written that goes through the ->ioctl() handler. I've mostly copypasted ib_safe_file_access() over as sg_safe_file_access() because I couldn't find a good common header - please tell me if you know a better way. [mkp: s/_safe_/_check_/] Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Jann Horn Acked-by: Douglas Gilbert Signed-off-by: Martin K. Petersen [bwh: Backported to 3.16: open-code uaccess_kernel()] Signed-off-by: Ben Hutchings --- drivers/scsi/sg.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) --- a/drivers/scsi/sg.c +++ b/drivers/scsi/sg.c @@ -52,6 +52,7 @@ static int sg_version_num = 30534; /* 2 #include #include #include +#include /* for sg_check_file_access() */ #include "scsi.h" #include @@ -215,6 +216,33 @@ static void sg_put_dev(Sg_device *sdp); #define SZ_SG_IOVEC sizeof(sg_iovec_t) #define SZ_SG_REQ_INFO sizeof(sg_req_info_t) +/* + * The SCSI interfaces that use read() and write() as an asynchronous variant of + * ioctl(..., SG_IO, ...) are fundamentally unsafe, since there are lots of ways + * to trigger read() and write() calls from various contexts with elevated + * privileges. This can lead to kernel memory corruption (e.g. if these + * interfaces are called through splice()) and privilege escalation inside + * userspace (e.g. if a process with access to such a device passes a file + * descriptor to a SUID binary as stdin/stdout/stderr). + * + * This function provides protection for the legacy API by restricting the + * calling context. + */ +static int sg_check_file_access(struct file *filp, const char *caller) +{ + if (filp->f_cred != current_real_cred()) { + pr_err_once("%s: process %d (%s) changed security contexts after opening file descriptor, this is not allowed.\n", + caller, task_tgid_vnr(current), current->comm); + return -EPERM; + } + if (unlikely(segment_eq(get_fs(), KERNEL_DS))) { + pr_err_once("%s: process %d (%s) called from kernel context, this is not allowed.\n", + caller, task_tgid_vnr(current), current->comm); + return -EACCES; + } + return 0; +} + static int sg_allow_access(struct file *filp, unsigned char *cmd) { struct sg_fd *sfp = filp->private_data; @@ -382,6 +410,14 @@ sg_read(struct file *filp, char __user * struct sg_header *old_hdr = NULL; int retval = 0; + /* + * This could cause a response to be stranded. Close the associated + * file descriptor to free up any resources being held. + */ + retval = sg_check_file_access(filp, __func__); + if (retval) + return retval; + if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))) return -ENXIO; SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n", @@ -567,9 +603,11 @@ sg_write(struct file *filp, const char _ struct sg_header old_hdr; sg_io_hdr_t *hp; unsigned char cmnd[MAX_COMMAND_SIZE]; + int retval; - if (unlikely(segment_eq(get_fs(), KERNEL_DS))) - return -EINVAL; + retval = sg_check_file_access(filp, __func__); + if (retval) + return retval; if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))) return -ENXIO;