From patchwork Sun Feb 4 04:30:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 10199261 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 D90F86020F for ; Sun, 4 Feb 2018 04:33:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C53F128F97 for ; Sun, 4 Feb 2018 04:33:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B6EDB28FDB; Sun, 4 Feb 2018 04:33:53 +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 18C3728F97 for ; Sun, 4 Feb 2018 04:33:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750798AbeBDEdV (ORCPT ); Sat, 3 Feb 2018 23:33:21 -0500 Received: from mail-pl0-f66.google.com ([209.85.160.66]:42683 "EHLO mail-pl0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750744AbeBDEdT (ORCPT ); Sat, 3 Feb 2018 23:33:19 -0500 Received: by mail-pl0-f66.google.com with SMTP id 11so9395846plc.9; Sat, 03 Feb 2018 20:33:19 -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=eV98AD8pr57lsPF2Rera7Wrf4s3JaZR77vFPYMvgsmo=; b=MZ4XwZWPDlUFtrDDs/vcywEwnGF1L/Z44jpUd+to9ah3/u1M+cdMIswDrSzKk2e8KW 5FIO+jaaxNvdvc0UZARvnlfFh9SLYHyLHoMrem/wu14qsjdBzjB2tVO1kFs5MPlfUIrN H1BsS47xLWJj9hyNV4Qw/iLWBnY8EHuwXXzHaX66SDOEDguYIZ0Xc9AFCG36ZyVRgZxG iqtnNSZKqHAz3JBLlHV7soBlthQi9JanKFtW7gSmXXaurl/3RaE05vRDm/A3jK2OYMQY g7rVxQfK4t1vPRJut6kza+mLDskFKEtTRno/xaND38MN996M49fchcdUIwEsJGJtwc7D tEmw== 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=eV98AD8pr57lsPF2Rera7Wrf4s3JaZR77vFPYMvgsmo=; b=oL+IKGOD16hbBZTcXNWr3Og0NgYkGNzlWdIcU5XuWdp9mWuyPoATq6ukPAHgT5p7If qxO+ebCWRpvPtDJ8X+A1Z776qj2PIfXgj7DdBXfqlw0gKOGtYo7uKT8vkE8BUWaoQK1/ yeeJT9nsRKxK9nuSFM/3WysiJCzos427mjIgFg3GIdmqEjnbu57o2npNdzf9bmm/zoMw 8pJgdSkXXWjrgzWYtXoiqDfvCP/3aMgNux1HnIb4l4+24N1h9nT+dP0pzQ13TRq00gxM bElxLBHZ6/kTcf00XB2cc2KiVWOsaQP7aaO3wcXT0jxlVHypCxYrd/rfvR9uZ7Xg5Vpn Tafg== X-Gm-Message-State: AKwxyteJG65d/BSjK433VxfxK9kmEClnHyvX64IMpXp10pJ6hKEPQcPG 7svJBpP5C+hkw4Lw4rXCp9mo5O8e X-Google-Smtp-Source: AH8x227fObMueGP1ZTlSOGjlhbaTVy9PUitFBfyswN+/Ph6WcCpD78woE5p74Nz2cLbBJi8+vTRc5g== X-Received: by 2002:a17:902:9003:: with SMTP id a3-v6mr40045507plp.338.1517718798667; Sat, 03 Feb 2018 20:33:18 -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 p68sm10119583pfp.30.2018.02.03.20.33.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 03 Feb 2018 20:33:17 -0800 (PST) From: Eric Biggers To: linux-ide@vger.kernel.org, Tejun Heo Cc: linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org, syzkaller-bugs@googlegroups.com, Mark Lord , Eric Biggers Subject: [PATCH] libata: fix length validation of ATAPI-relayed SCSI commands Date: Sat, 3 Feb 2018 20:30:56 -0800 Message-Id: <20180204043056.16466-1-ebiggers3@gmail.com> X-Mailer: git-send-email 2.16.1 In-Reply-To: <001a114aa95cfa4339055cd4e8e0@google.com> References: <001a114aa95cfa4339055cd4e8e0@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 reported a crash in ata_bmdma_fill_sg() when writing to /dev/sg1. The immediate cause was that the ATA command's scatterlist was not DMA-mapped, which causes 'pi - 1' to underflow, resulting in a write to 'qc->ap->bmdma_prd[0xffffffff]'. Strangely though, the flag ATA_QCFLAG_DMAMAP was set in qc->flags. The root cause is that when __ata_scsi_queuecmd() is preparing to relay a SCSI command to an ATAPI device, it doesn't correctly validate the CDB length before copying it into the 16-byte buffer 'cdb' in 'struct ata_queued_cmd'. Namely, it validates the fixed CDB length expected based on the SCSI opcode but not the actual CDB length, which can be larger due to the use of the SG_NEXT_CMD_LEN ioctl. Since 'flags' is the next member in ata_queued_cmd, a buffer overflow corrupts it. Fix it by requiring that the actual CDB length be <= 16 (ATAPI_CDB_LEN). [Really it seems the length should be required to be <= dev->cdb_len, but the current behavior seems to have been intentionally introduced by commit 607126c2a21c ("libata-scsi: be tolerant of 12-byte ATAPI commands in 16-byte CDBs") to work around a userspace bug in mplayer. Probably the workaround is no longer needed (mplayer was fixed in 2007), but continuing to allow lengths to up 16 appears harmless for now.] Here's a reproducer that works in QEMU when /dev/sg1 refers to the CD-ROM drive that qemu-system-x86_64 creates by default: #include #include #include #define SG_NEXT_CMD_LEN 0x2283 int main() { char buf[53] = { [36] = 0x7e, [52] = 0x02 }; int fd = open("/dev/sg1", O_RDWR); ioctl(fd, SG_NEXT_CMD_LEN, &(int){ 17 }); write(fd, buf, sizeof(buf)); } The crash was: BUG: unable to handle kernel paging request at ffff8cb97db37ffc IP: ata_bmdma_fill_sg drivers/ata/libata-sff.c:2623 [inline] IP: ata_bmdma_qc_prep+0xa4/0xc0 drivers/ata/libata-sff.c:2727 PGD fb6c067 P4D fb6c067 PUD 0 Oops: 0002 [#1] SMP CPU: 1 PID: 150 Comm: syz_ata_bmdma_q Not tainted 4.15.0-next-20180202 #99 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.11.0-20171110_100015-anatol 04/01/2014 [...] Call Trace: ata_qc_issue+0x100/0x1d0 drivers/ata/libata-core.c:5421 ata_scsi_translate+0xc9/0x1a0 drivers/ata/libata-scsi.c:2024 __ata_scsi_queuecmd drivers/ata/libata-scsi.c:4326 [inline] ata_scsi_queuecmd+0x8c/0x210 drivers/ata/libata-scsi.c:4375 scsi_dispatch_cmd+0xa2/0xe0 drivers/scsi/scsi_lib.c:1727 scsi_request_fn+0x24c/0x530 drivers/scsi/scsi_lib.c:1865 __blk_run_queue_uncond block/blk-core.c:412 [inline] __blk_run_queue+0x3a/0x60 block/blk-core.c:432 blk_execute_rq_nowait+0x93/0xc0 block/blk-exec.c:78 sg_common_write.isra.7+0x272/0x5a0 drivers/scsi/sg.c:806 sg_write+0x1ef/0x340 drivers/scsi/sg.c:677 __vfs_write+0x31/0x160 fs/read_write.c:480 vfs_write+0xa7/0x160 fs/read_write.c:544 SYSC_write fs/read_write.c:589 [inline] SyS_write+0x4d/0xc0 fs/read_write.c:581 do_syscall_64+0x5e/0x110 arch/x86/entry/common.c:287 entry_SYSCALL_64_after_hwframe+0x21/0x86 Fixes: 607126c2a21c ("libata-scsi: be tolerant of 12-byte ATAPI commands in 16-byte CDBs") Reported-by: syzbot+1ff6f9fcc3c35f1c72a95e26528c8e7e3276e4da@syzkaller.appspotmail.com Cc: # v2.6.24+ Signed-off-by: Eric Biggers --- drivers/ata/libata-scsi.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/ata/libata-scsi.c b/drivers/ata/libata-scsi.c index 66be961c93a4e..47d421666451c 100644 --- a/drivers/ata/libata-scsi.c +++ b/drivers/ata/libata-scsi.c @@ -4309,7 +4309,9 @@ static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd, if (likely((scsi_op != ATA_16) || !atapi_passthru16)) { /* relay SCSI command to ATAPI device */ int len = COMMAND_SIZE(scsi_op); - if (unlikely(len > scmd->cmd_len || len > dev->cdb_len)) + if (unlikely(len > scmd->cmd_len || + len > dev->cdb_len || + scmd->cmd_len > ATAPI_CDB_LEN)) goto bad_cdb_len; xlat_func = atapi_xlat;