From patchwork Mon Jun 25 14:25:44 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jann Horn X-Patchwork-Id: 10486457 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0E9696038C for ; Mon, 25 Jun 2018 14:38:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D6B5F27852 for ; Mon, 25 Jun 2018 14:38:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CB50027F17; Mon, 25 Jun 2018 14:38:38 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id 76B7827852 for ; Mon, 25 Jun 2018 14:38:37 +0000 (UTC) Received: (qmail 8100 invoked by uid 550); 25 Jun 2018 14:38:35 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Delivered-To: moderator for kernel-hardening@lists.openwall.com Received: (qmail 28600 invoked from network); 25 Jun 2018 14:26:01 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:date:message-id:subject:from:to:cc; bh=BhDO8aPbef7UNHAyRB6gCF+msDGCfhuBkb/kmk1wzCc=; b=ZnDj2h3sW7fg8M9ME55S56Egez2ZVlY4wFHKZqJbSOZ2xxeKMzefqrfyif6OxokpqW d1j9k/beIL8kYgnav25hrQl+0cyQgyph584vziICnVWh7eBT/rHz7CuB9huCl4CTu0lx PitUkFUvHuCt6/HIlfo5806k4Ngewzemm4a6QxcnhdVU33rQTYWmpQuXyoqblTAgep5f aiOApP3TQeRviCZruoVVkbtQAr0V4wtANtfQR+sYy3+IyxaxJZ0ssLyXYkfciGW4orJY KUKE3fvrrKeNWaYISM211CvxjWCTe4SKMxtKzhn/Y8l7+NVCk+PsPq58OiSl+TCfLcI0 ak0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:date:message-id:subject:from:to:cc; bh=BhDO8aPbef7UNHAyRB6gCF+msDGCfhuBkb/kmk1wzCc=; b=NH09cc1alfTW1flUaoPj83RCobhlJyWrG86DgnC4uGcf3+eYaIsKKyj/sNULZ3EIt2 pUBWOk5snO7+YCuoXg532OLEmgEevt9IIjPZGtrqnB00O2J1U8waHk/biVaUOhA8eF9z SBvGlXPkP8N4TZgH8mK8KPTzGngqSCcOO9DBd5+1coVPJf6xTSxXlMTiXca+apF14gJE wrQkn4ST7WafMHl9eOOVAb2eoAO90MI1PtK9wi/HmkQitRLHAP0I5c9h/awDj1CaozVM SPRd8ymn6e9DQUdav39aH74p/sCSUW9hVi3S5Q/PZzHkAAbqL0JEFKTN3ApLP9D45dX/ r75g== X-Gm-Message-State: APt69E1wb4RkposELmKXb5AQyW3eDv+II5ePhHTAQ34ymSCoWyaVXatq OfnTNvmzNaf+XmrWTfVi2zJNFxjSkw== X-Google-Smtp-Source: ADUXVKKApkXBCVPMc5TC/w6BluVGRVPkjnY70krHfg/C3jNf/yzh+7N7Id5lHPCqq3O7Qz1fazWK3p2QBw== MIME-Version: 1.0 X-Received: by 2002:a1f:bac1:: with SMTP id k184-v6mr5821524vkf.40.1529936749025; Mon, 25 Jun 2018 07:25:49 -0700 (PDT) Date: Mon, 25 Jun 2018 16:25:44 +0200 Message-Id: <20180625142544.182673-1-jannh@google.com> X-Mailer: git-send-email 2.18.0.rc2.346.g013aa6912e-goog Subject: [PATCH v3] sg: mitigate read/write abuse From: Jann Horn To: Doug Gilbert , "James E.J. Bottomley" , "Martin K. Petersen" , linux-scsi@vger.kernel.org, Christoph Hellwig , Al Viro , jannh@google.com Cc: Andy Lutomirski , linux-kernel@vger.kernel.org, Jens Axboe , FUJITA Tomonori , kernel-hardening@lists.openwall.com, security@kernel.org, Benjamin Block X-Virus-Scanned: ClamAV using ClamSMTP 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. changed in v2: - remove the bsg parts per Christoph Hellwig's request changed in v3: - move error messages into helper function - use two different error messages and return values (Douglas Gilbert) - add comment on stranded responses (Douglas Gilbert) - use current_real_cred() instead of current_cred() (so that override_creds() can't bypass this check) Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Cc: Signed-off-by: Jann Horn Acked-by: Douglas Gilbert --- 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 53ae52dbff84..4f4e88ca8213 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_safe_file_access() */ #include "scsi.h" #include @@ -209,6 +210,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; @@ -393,6 +421,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, @@ -580,9 +616,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;