Received: by 10.213.65.68 with SMTP id h4csp38043imn; Mon, 19 Mar 2018 18:47:11 -0700 (PDT) X-Google-Smtp-Source: AG47ELu4VrxX2/EH1IfeyAOrWJKyRyHgg48btISJKy6DXgTIJ1ZBgAV5cqeCaJ+irIZdfIngMDyd X-Received: by 10.99.125.75 with SMTP id m11mr10674222pgn.391.1521510431082; Mon, 19 Mar 2018 18:47:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521510431; cv=none; d=google.com; s=arc-20160816; b=Tn12MKd49UZ1lNbeyHli6ctzGHsiRzukZKJ3Eargx03L8CNENlCrvR9m57hzJHxIK0 I24A8NkZbv/2fPY6JTOZJVABfVHvbr27R0U26qIHUUr3WJRezV12gQ+4upiSDx4elt/j rRDs6CAUPBICdxLCXMWezi2iRapW5BJP+NZNKZJPHc5XGucSY2VpuRUpSbmGg1mD7rfY /Sdp8VMJAe6vwuu5sjzfzKcfdvWbVegi9C9fMSvRqu3kRNeDLnRP+SbiBkCT7aizXlb0 2OHtQ2K2L+CBdBvRrIUM3JAgii8wuesdym55r8BINNOgzJGjXDLxBQ1U6IGxb67w9WBM gg3Q== 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=ZWl8xQpkQjpDhMqJaiCk+PAz48ocYvif44z06dMNrq8=; b=XTJtU2fD0trAv4DYhL2pUXIwPB3pbjMKWndp7sYGQ1QaINyTA8gEmMuWOxQd51DL1h TtMOkAgEg2Zj8HUN19yPPhKJL9NnguiBrat1BR/29Kz4t7JsMVk+yqbycpSf/w/rEK81 OUqLEl4cYTQ5lR6we9Oar64HMSOGn59CuCmZgNaLqACqVAfRsKv7FIilUqvWi1lF6Dci AYy58eODzue0Dx/YbWoYxVgxmqUWv6swXQeu7ZdxfcLcf0WrsTXDbRlXQC2gCKYYSRLP J5PUuZaSSdoDbpK4d5kw92Xy3kV/NqzU/r2TL7wOjppSRaSvhO1LD63b4QRegsyhgsU4 7Tkg== 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 44-v6si486584plb.635.2018.03.19.18.46.57; Mon, 19 Mar 2018 18:47:11 -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 S935957AbeCSTZ6 (ORCPT + 99 others); Mon, 19 Mar 2018 15:25:58 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:49976 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S969676AbeCSSYd (ORCPT ); Mon, 19 Mar 2018 14:24:33 -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 2B17FF91; Mon, 19 Mar 2018 18:24:33 +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.9 149/241] scsi: sg: check for valid direction before starting the request Date: Mon, 19 Mar 2018 19:06:54 +0100 Message-Id: <20180319180757.351846763@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180319180751.172155436@linuxfoundation.org> References: <20180319180751.172155436@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: 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 @@ -663,18 +663,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; @@ -753,6 +749,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) @@ -773,6 +792,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,