Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp2009188imm; Tue, 10 Jul 2018 11:31:17 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfu7p2suEkYoFMKvMTLPH38A5UT3sXK03tTP1TgHgAD/2mC5RCyhzxu8hS/R2kJIcMbbuhu X-Received: by 2002:a17:902:6b05:: with SMTP id o5-v6mr25907023plk.67.1531247477374; Tue, 10 Jul 2018 11:31:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531247477; cv=none; d=google.com; s=arc-20160816; b=TczE2kmTsnS26ZhPBSdv3W7GKgLKKso4O8XGaXyZSZc1LvTBUPQgCgxaLbU8HngeIE rUWCf4I3yKOj+YuvedbK+bN0UBV7VusP2USnc0eglngsYy8Ch6h0VIHQquKsKgZQvlv4 TD87ayMEKVqZjG4lSJA888aYJf4MsxPLp4b/c31TZth2zc8rWZd/0NwOreXOmYtTQil6 17GDHaFLGCeCdUIyjHpyMY9A1aKWbNcIBmJ1shVkcE6wib8CQosxmk3FfOdA5PZlyVo2 wt5IEiolzlxjH6m1JxjfjkkD0nVDGsZ43J5R4u18NbLyLNjnAYFXVtBzV1hjmRJURf3L s2oA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=ThVCGpPRs47ehdRTK2oASAxYb8W/gD3wkoIvGxRGGIA=; b=zTnPdFRklcH/qkMnFTM0IP3S2CrZcRqMHkdCOBcfuPmvwfS1ANOoQ7ET4yndfji9Pe 2fUL3g25bsHasCDBm7l6ATkKdY7Jvss8UVnJgllVdrl9U/WW4BEYg8+PFlQxDg0OgwyA 3kYRdnmAl0pgh7oD5yRLGH+zsgFucdAEVqxgfU5LofkclN/ei6Sof/gkpjrupgPnR4+7 HZDiELYWDhO4sNkzuSGV+4/+hDuf5pxkN+19IiGxb2mhmVLSzQb5xmXp+MNFnQduL0j2 vUNa0yOfo3dUsqRTqeJhYxx3fA95jcW6qJLF2bHnvS7butXkateEod9ybCEGAesqtKIl 3QLQ== 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 u6-v6si17366794pld.74.2018.07.10.11.31.01; Tue, 10 Jul 2018 11:31:17 -0700 (PDT) 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 S2387914AbeGJSaB (ORCPT + 99 others); Tue, 10 Jul 2018 14:30:01 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:45476 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732596AbeGJSaA (ORCPT ); Tue, 10 Jul 2018 14:30:00 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 02996EB8; Tue, 10 Jul 2018 18:29:48 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jann Horn , Douglas Gilbert , "Martin K. Petersen" Subject: [PATCH 4.9 21/52] scsi: sg: mitigate read/write abuse Date: Tue, 10 Jul 2018 20:24:49 +0200 Message-Id: <20180710182451.470326535@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180710182449.285532226@linuxfoundation.org> References: <20180710182449.285532226@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.9-stable 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") Cc: Signed-off-by: Jann Horn Acked-by: Douglas Gilbert Signed-off-by: Martin K. Petersen Signed-off-by: Greg Kroah-Hartman --- drivers/scsi/sg.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) --- a/drivers/scsi/sg.c +++ b/drivers/scsi/sg.c @@ -51,6 +51,7 @@ static int sg_version_num = 30536; /* 2 #include #include #include +#include /* for sg_check_file_access() */ #include "scsi.h" #include @@ -210,6 +211,33 @@ static void sg_device_destroy(struct kre sdev_prefix_printk(prefix, (sdp)->device, \ (sdp)->disk->disk_name, fmt, ##a) +/* + * 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; @@ -394,6 +422,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, sg_printk(KERN_INFO, sdp, @@ -581,9 +617,11 @@ sg_write(struct file *filp, const char _ struct sg_header old_hdr; sg_io_hdr_t *hp; unsigned char cmnd[SG_MAX_CDB_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;