From patchwork Sun Feb 4 04:33:51 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 10199267 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 2DFBA6020F for ; Sun, 4 Feb 2018 04:34:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1AB7928F97 for ; Sun, 4 Feb 2018 04:34:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0F34928FD7; Sun, 4 Feb 2018 04:34:12 +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=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 813F128F97 for ; Sun, 4 Feb 2018 04:34:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751283AbeBDEeK (ORCPT ); Sat, 3 Feb 2018 23:34:10 -0500 Received: from mail-pg0-f65.google.com ([74.125.83.65]:39700 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750744AbeBDEeJ (ORCPT ); Sat, 3 Feb 2018 23:34:09 -0500 Received: by mail-pg0-f65.google.com with SMTP id w17so16000358pgv.6; Sat, 03 Feb 2018 20:34:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=r7XH9Ptr1urOndQ5qmaAAIGUPD8Cd+2KNw/my2HNA24=; b=SolsZrG4xgGcdnf0HZ5Z1sIXkB7qFoNhmn2eemzhIFPn9teY/oIt/UfeRrAQkWE9+R 1kz8oOR5vKpNg6AzRPNvC8hVj4P9MxDz2DfDbLy9QIF/fkmpKb38CtlD1e5kW6GPscxf wyrDht0pKrFmfujTfOio7LxWTdDHe/Bu2ZPCBJJ2XgcHRsDET5+8esXLZFXF9hZhX+eJ H1dg9p0HNROdYj1IsGXpUX5Te1etm8753QyutSicJ7ZDMP/jEfQWtHxlr0DU3ekd7mpu HVvRzYqWNPrZ9W7x229r1Jjft0K3RAxSkmEpouHIEHT7hHW4pDHokZffmnqTy60+qZeH nvUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=r7XH9Ptr1urOndQ5qmaAAIGUPD8Cd+2KNw/my2HNA24=; b=t4qP99dFaYqlHFm/kwpi05Syf3MO7fqcOZ8VhN4OQnzeh8O0eNqm8/qEz2pBeYhPob IsbvzZjUkBAyzjIGbfKpLFSk51ixB+XlZN8yBpi0N+T4WW+6GVcUZ8EgrNsAHViHEPoP LIKEDHXYJ+pNawO722q7NryzcTtN5jCPbygPIWvrSlBsrOnZnJvHBLAcd23f3j0KAd3U 5paFG+vkTrbqugUf4r8LspjWrrESwx0QukYBhxxGGkX94n11RA8QqEwtqHzkDHjpr/Iv Zqi0G6qQHI0g/vZYQfsJZvIebj8lTcBxKRVW3jx2NhD9GALJ80p6Ey+QMsGRFyoHNHGQ adBA== X-Gm-Message-State: AKwxytfSKCJ7tEsb1uHzld8srd/t4kEOmnPvbVXYQ3mqVadYhRpEyNS4 msHkHgHkKY1+6CA3VOE7QCXRfNIL X-Google-Smtp-Source: AH8x224IT7rzR7EXKXgN/4/oMN7xrHiQgvC9+O4Xsp1C1mh6vg5S3/WzYBwAGtLl8C1WxMVKKytBXg== X-Received: by 10.101.97.139 with SMTP id c11mr5569210pgv.219.1517718848537; Sat, 03 Feb 2018 20:34:08 -0800 (PST) Received: from zzz.localdomain (c-67-185-97-198.hsd1.wa.comcast.net. [67.185.97.198]) by smtp.gmail.com with ESMTPSA id f72sm12328839pff.145.2018.02.03.20.34.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 03 Feb 2018 20:34:08 -0800 (PST) From: Eric Biggers To: linux-ide@vger.kernel.org, Tejun Heo Cc: linux-scsi@vger.kernel.org, syzkaller-bugs@googlegroups.com, Vinayak Kale , Eric Biggers Subject: [PATCH] libata: don't try to pass through NCQ commands to non-NCQ devices Date: Sat, 3 Feb 2018 20:33:51 -0800 Message-Id: <20180204043351.16812-1-ebiggers3@gmail.com> X-Mailer: git-send-email 2.16.1 In-Reply-To: <94eb2c1fae0400ce57055cd4e9bb@google.com> References: <94eb2c1fae0400ce57055cd4e9bb@google.com> Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Eric Biggers syzkaller hit a WARN() in ata_bmdma_qc_issue() when writing to /dev/sg0. This happened because it issued an ATA pass-through command (ATA_16) where the protocol field indicated that NCQ should be used -- but the device did not support NCQ. We could just remove the WARN() from libata-sff.c, but the real problem seems to be that the SCSI -> ATA translation code passes through NCQ commands without verifying that the device actually supports NCQ. Fix this by adding the appropriate check to ata_scsi_pass_thru(). Here's reproducer that works in QEMU when /dev/sg0 refers to a disk of the default type ("82371SB PIIX3 IDE"): #include #include int main() { char buf[53] = { 0 }; buf[36] = 0x85; /* ATA_16 */ buf[37] = (12 << 1); /* FPDMA */ buf[38] = 0x1; /* Has data */ buf[51] = 0xC8; /* ATA_CMD_READ */ write(open("/dev/sg0", O_RDWR), buf, sizeof(buf)); } Fixes: ee7fb331c3ac ("libata: add support for NCQ commands for SG interface") Reported-by: syzbot+2f69ca28df61bdfc77cd36af2e789850355a221e@syzkaller.appspotmail.com Cc: # v4.4+ Signed-off-by: Eric Biggers --- drivers/ata/libata-scsi.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/ata/libata-scsi.c b/drivers/ata/libata-scsi.c index 47d421666451c..197e110f8ac75 100644 --- a/drivers/ata/libata-scsi.c +++ b/drivers/ata/libata-scsi.c @@ -3316,6 +3316,12 @@ static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc) goto invalid_fld; } + /* We may not issue NCQ commands to devices not supporting NCQ */ + if (ata_is_ncq(tf->protocol) && !ata_ncq_enabled(dev)) { + fp = 1; + goto invalid_fld; + } + /* sanity check for pio multi commands */ if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf)) { fp = 1;