Received: by 10.213.65.68 with SMTP id h4csp1789330imn; Mon, 19 Mar 2018 13:18:46 -0700 (PDT) X-Google-Smtp-Source: AG47ELvtkUajvp+fgquZ1hdj3V2BvD3ZCS3vgWPc89q9B40gThP8ZnojJDIP8mt9FznFdN4hpKGP X-Received: by 10.98.170.13 with SMTP id e13mr11350245pff.113.1521490725996; Mon, 19 Mar 2018 13:18:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521490725; cv=none; d=google.com; s=arc-20160816; b=Z8YmtR/vnkxZjG5Czlq0OVSV2BuAh6FGRZAXWlkZhvruH1+/sM8vd2+z1CZJTr+nZg JRLeu338LUPWNDmS3pT+Wke4mmy3hcdBFAvp3bIr/RaDZeLYt+hQE/hj+OQUCeVGcPhb 6juztaiClnLYP9tjI+7/C1s5a0jQbuMjmWne5ZS6Zbfli89XL8sAfPmg+P2uFBkSswWw q8kUnzmeDtVgOda87ewtXlF2QsFjzmQSl44cngbKEsv75M7QgGdZZpMIbMXoUitmw2lG l8UNnBfKLqT1rSh4+/xZMR7YmFIdceE31/Cn47R8M9j5yTj8QHPfOkdLUdXD3nfENLTk nNJA== 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=0zFlNecLEs3yDpwvPJw+sNpz1pxY2QPpV358U0aI54g=; b=v/19dy+wol0DxZN/e69nSVVJOZcY+7uDqpGSFNkIWHTOSzBbDcxukq1TOsJ3t5BjIu s46mgAxfTDQf9rIipTyGnWQKFEYRvo7Bo3qe2GfDYZTCf9LcXoPeVxnPoaDvX9Q/D5L1 issEagDw5lBZuFP0Yn9c0nu8JshNbEIR9/dMf+NwTYY2PgulVUmdNdelJuCSCOawWXU9 yw6VQlXxasx/POAYoSxMrPxS054DTae+uKLS9+VicaKlFrkmlLTGzoj4HyySED+/yplC eVgfB+glqiExB9IMj2AZxK9zs+MMQB9aH+7ulMnA7LaGnQKUU5aATFICjjXqcOsu3z61 zj4g== 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 x1si14275pgc.27.2018.03.19.13.18.30; Mon, 19 Mar 2018 13:18:45 -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 S971192AbeCSUQg (ORCPT + 99 others); Mon, 19 Mar 2018 16:16:36 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:44732 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S968999AbeCSSPs (ORCPT ); Mon, 19 Mar 2018 14:15:48 -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 6DCCD10CC; Mon, 19 Mar 2018 18:15:46 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Johannes Thumshirn , Dmitry Vyukov , Hannes Reinecke , Christoph Hellwig , "Martin K. Petersen" , Sasha Levin Subject: [PATCH 4.4 073/134] scsi: sg: check for valid direction before starting the request Date: Mon, 19 Mar 2018 19:05:56 +0100 Message-Id: <20180319171859.817392692@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180319171849.024066323@linuxfoundation.org> References: <20180319171849.024066323@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.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: Johannes Thumshirn [ Upstream commit 28676d869bbb5257b5f14c0c95ad3af3a7019dd5 ] Check for a valid direction before starting the request, otherwise we risk running into an assertion in the scsi midlayer checking for valid requests. [mkp: fixed typo] Signed-off-by: Johannes Thumshirn Link: http://www.spinics.net/lists/linux-scsi/msg104400.html Reported-by: Dmitry Vyukov Signed-off-by: Hannes Reinecke Tested-by: Johannes Thumshirn Reviewed-by: Christoph Hellwig Signed-off-by: Martin K. Petersen Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- drivers/scsi/sg.c | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) --- a/drivers/scsi/sg.c +++ b/drivers/scsi/sg.c @@ -674,18 +674,14 @@ sg_write(struct file *filp, const char _ * is a non-zero input_size, so emit a warning. */ if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) { - static char cmd[TASK_COMM_LEN]; - if (strcmp(current->comm, cmd)) { - printk_ratelimited(KERN_WARNING - "sg_write: data in/out %d/%d bytes " - "for SCSI command 0x%x-- guessing " - "data in;\n program %s not setting " - "count and/or reply_len properly\n", - old_hdr.reply_len - (int)SZ_SG_HEADER, - input_size, (unsigned int) cmnd[0], - current->comm); - strcpy(cmd, current->comm); - } + printk_ratelimited(KERN_WARNING + "sg_write: data in/out %d/%d bytes " + "for SCSI command 0x%x-- guessing " + "data in;\n program %s not setting " + "count and/or reply_len properly\n", + old_hdr.reply_len - (int)SZ_SG_HEADER, + input_size, (unsigned int) cmnd[0], + current->comm); } k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking); return (k < 0) ? k : count; @@ -764,6 +760,29 @@ sg_new_write(Sg_fd *sfp, struct file *fi return count; } +static bool sg_is_valid_dxfer(sg_io_hdr_t *hp) +{ + switch (hp->dxfer_direction) { + case SG_DXFER_NONE: + if (hp->dxferp || hp->dxfer_len > 0) + return false; + return true; + case SG_DXFER_TO_DEV: + case SG_DXFER_FROM_DEV: + case SG_DXFER_TO_FROM_DEV: + if (!hp->dxferp || hp->dxfer_len == 0) + return false; + return true; + case SG_DXFER_UNKNOWN: + if ((!hp->dxferp && hp->dxfer_len) || + (hp->dxferp && hp->dxfer_len == 0)) + return false; + return true; + default: + return false; + } +} + static int sg_common_write(Sg_fd * sfp, Sg_request * srp, unsigned char *cmnd, int timeout, int blocking) @@ -784,6 +803,9 @@ sg_common_write(Sg_fd * sfp, Sg_request "sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) cmnd[0], (int) hp->cmd_len)); + if (!sg_is_valid_dxfer(hp)) + return -EINVAL; + k = sg_start_req(srp, cmnd); if (k) { SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sfp->parentdp,