From patchwork Fri Oct 12 16:09:46 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kleber Sacilotto de Souza X-Patchwork-Id: 983164 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=canonical.com Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42Wt8V1hYLz9s55; Sat, 13 Oct 2018 03:10:02 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1gB00g-0002ZH-HA; Fri, 12 Oct 2018 16:09:54 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.86_2) (envelope-from ) id 1gB00f-0002Yp-0q for kernel-team@lists.ubuntu.com; Fri, 12 Oct 2018 16:09:53 +0000 Received: from mail-wr1-f69.google.com ([209.85.221.69]) by youngberry.canonical.com with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.76) (envelope-from ) id 1gB00e-000801-Pf for kernel-team@lists.ubuntu.com; Fri, 12 Oct 2018 16:09:52 +0000 Received: by mail-wr1-f69.google.com with SMTP id j17-v6so5831617wrm.11 for ; Fri, 12 Oct 2018 09:09:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=om6Y4wuGdl++rd0HNuzpw1/mavgj10Go0RPPgbgCb5U=; b=P7Gnhoh1D9EsbhICi6bZlKgEM198J1u2vWg9zn533Iitd9YwkDCsace0DoZ+g/qsHw Pljok4jBukBb3w8cBOHF04pt+2FqfTwCY3t92swiWLB0GSZWxQMz76cnzosUa+2JxBSG SPKCQLRPxOOmA4n+2vaz9O3fU29jDoax5ojvmrHAAl/U6kMTCIPzt9RpuSK2FDqroAYm NdKQcf8A7sd5m+y2EXCpw/bdhMmpuh7HJ0MTnbrDN9FyCjCp91P2QcKzh6v5kI79rIQZ DEPqMYz62PrgKKBcnvtJ63o7G0wWxIxW1Usgro8L4XGPnAwkgz9AmaBmWLH1oLGLJh61 Pypg== X-Gm-Message-State: ABuFfohXssQQoGgSpmTIRJ1qo6grFa2DFti0huJP/Oz9dpUYOygEWWbq +3gfGwxYO4SyNXwzwsNTUpb+8iWYr1r9N56NXbPGqbrWQnZqWHeaY/KVoJZybYYZUZtyxbhNbgU oZQZsn7d8naoIQdCHWLFE5iqNNI6++VrLQtV7h5glOw== X-Received: by 2002:a1c:87ca:: with SMTP id j193-v6mr5154989wmd.99.1539360592105; Fri, 12 Oct 2018 09:09:52 -0700 (PDT) X-Google-Smtp-Source: ACcGV61/k7icRGpEkZ9amomFLZ/L1bXPNBe01nlt3ZC0S/Oe1h51qBI/6ky6YXNfJe8pdlwkPdrI9A== X-Received: by 2002:a1c:87ca:: with SMTP id j193-v6mr5154970wmd.99.1539360591775; Fri, 12 Oct 2018 09:09:51 -0700 (PDT) Received: from localhost ([212.121.131.210]) by smtp.gmail.com with ESMTPSA id b71-v6sm2613236wma.13.2018.10.12.09.09.50 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 12 Oct 2018 09:09:50 -0700 (PDT) From: Kleber Sacilotto de Souza To: kernel-team@lists.ubuntu.com Subject: [SRU][Bionic][PATCH 1/1] scsi: sg: mitigate read/write abuse Date: Fri, 12 Oct 2018 18:09:46 +0200 Message-Id: <20181012160946.23935-3-kleber.souza@canonical.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181012160946.23935-1-kleber.souza@canonical.com> References: <20181012160946.23935-1-kleber.souza@canonical.com> X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Jann Horn 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 CVE-2017-13168 (cherry picked from commit 26b5b874aff5659a7e26e5b1997e3df2c41fa7fd) Signed-off-by: Kleber Sacilotto de Souza --- drivers/scsi/sg.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c index 83a52ef4feb3..7a3b601e3191 100644 --- a/drivers/scsi/sg.c +++ b/drivers/scsi/sg.c @@ -51,6 +51,7 @@ static int sg_version_num = 30536; /* 2 digits for each component */ #include #include #include +#include /* for sg_check_file_access() */ #include "scsi.h" #include @@ -210,6 +211,33 @@ static void sg_device_destroy(struct kref *kref); 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 (uaccess_kernel()) { + 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 *buf, size_t count, loff_t * ppos) 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 __user *buf, size_t count, loff_t * ppos) struct sg_header old_hdr; sg_io_hdr_t *hp; unsigned char cmnd[SG_MAX_CDB_SIZE]; + int retval; - if (unlikely(uaccess_kernel())) - 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;