From patchwork Thu Feb 29 19:30:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Stern X-Patchwork-Id: 208493 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp630391dyb; Thu, 29 Feb 2024 11:30:33 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVxd4fWFT+8qiaJ6wcwFxWICGUi48cvVyDgc5rNFbR8RkF1871Cnjzc0kIpLRStFyvpmpt9PPyrmotxrqgKn71vAazykw== X-Google-Smtp-Source: AGHT+IEFgs/UKkPhuOPUrDUnLJR/2RtACPqGuw6ixHOabiht3xk0VMTApYluRfKEVGjm3uf67I+z X-Received: by 2002:ad4:5987:0:b0:68f:5ac3:8af3 with SMTP id ek7-20020ad45987000000b0068f5ac38af3mr2987215qvb.22.1709235032860; Thu, 29 Feb 2024 11:30:32 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709235032; cv=pass; d=google.com; s=arc-20160816; b=YDjWD9dflM60thktJEt4yUBjvxj61zMHP/jjug5nuQq5JXX74T8lXq0ftxjIbcSBjQ XvCnHsXwx9rdRttWS/EB6Q5fxWrsRbhTanht6vOnrAVVLdw5GhiUZE3up8GcecySe5xW 5yZP2T4fg60uztB6MbYBaUMgprPrKmzoG732aSzS3ENhXcrCBAxikBDbKZgSLvdbHreE T4usHNRtXhrQtl+6SHd4j1H0KkqFEsTBZBLwcQrcEgKY6nNt9FGPhu4joqb6zUfzcoLZ Ajgyv29QXA5UnoCu/Pot7mls76QM4FrJ4J+wDFkQqh0/aQm3Uy1m4QY2L8anKjkKlfAh H+Tw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date; bh=ICSB4xeDLeqFBFTH6yl+oSQ7QOnAEYnWtnT9I+eXmYI=; fh=0Y+E/WOvavyGJiV9+Dsjp/9pROdIE6oM6qmTFjjNljQ=; b=KZVGfuXeLmMNTXG38RQov+Bw15IsxIU/veAEcdTZPGs++HL4EAJ6SjCHNbbF+Y+r4a y6dQK+O/4itb67aGJ8weGfZvSZwtrcU6Cjlp5FmPDzQq3XKNm6slLYJ3syn/QZIJv2jF rlyBzcZgPaGHd5B2MsM6OZ2GJh4bPPn24sUprTcKz5puXF1Mtc3Hy4UbzOuzfV3yiTat q4Z9lowv9HjrRpVZesVy6K6tY+Y0/LnrpjcBnDu9BXLyCSHaIvusrMfaQJ0dHxuIxU37 WbNUrJGstAUWouOJudQRajjBz4TVOeiMYv/1wXQbimmTiYHoBxoo2IkVE7SxG63NEbaD UnNQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=netrider.rowland.org); spf=pass (google.com: domain of linux-kernel+bounces-87354-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-87354-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=harvard.edu Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id pj1-20020a0562144b0100b00690171413dfsi2027838qvb.498.2024.02.29.11.30.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 11:30:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-87354-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=netrider.rowland.org); spf=pass (google.com: domain of linux-kernel+bounces-87354-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-87354-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=harvard.edu Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 7EA601C2205B for ; Thu, 29 Feb 2024 19:30:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 94BC513C9F4; Thu, 29 Feb 2024 19:30:12 +0000 (UTC) Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by smtp.subspace.kernel.org (Postfix) with SMTP id 870DF12FB0B for ; Thu, 29 Feb 2024 19:30:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.131.102.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709235011; cv=none; b=gSDy5yJncT+Lr65xmvuSA5KWJ92Iu5xYhb6/mCqoruWKLafjkpn4XYSP3PHLkuo5vr+aFJ8tFS3SJzV3cXfkQqM2XZwjEey1VYJ5FrddLGFUomjniVjOXxK6IPWcNwJA7dmcF6zJyFZNIX4B/XKB85jQoZmgwF1FD5dcTN6bWgQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709235011; c=relaxed/simple; bh=zIkZcYh+X6JTE1BjxXkppg86AYseCYK4cRr264Zk2Jg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=u37eD8AW1SnsDJnsWElDt6ORHxuWZZCKy5Rf7mlhIa9Wtr9SdNrG0DLLuYh4YjVKLeqdeMg+v8ucb9+DU37mTDFfWiPLyCgzfpi25KFyQgtmbUalg/ZjjhnP9YJ/qsYdgT3RcD3KBPMEwWMXvgl+RcwzMl7OCR97Udtlbt/CRBQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=rowland.harvard.edu; spf=pass smtp.mailfrom=netrider.rowland.org; arc=none smtp.client-ip=192.131.102.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=rowland.harvard.edu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=netrider.rowland.org Received: (qmail 12670 invoked by uid 1000); 29 Feb 2024 14:30:06 -0500 Date: Thu, 29 Feb 2024 14:30:06 -0500 From: Alan Stern To: Greg KH Cc: syzbot , bvanassche@acm.org, emilne@redhat.com, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, martin.petersen@oracle.com, nogikh@google.com, syzkaller-bugs@googlegroups.com, tasos@tasossah.com, usb-storage@lists.one-eyed-alien.net Subject: [PATCH] USB: usb-storage: Prevent divide-by-0 error in isd200_ata_command Message-ID: References: <380909e4-6e0a-402f-b3ac-de07e520c910@rowland.harvard.edu> <000000000000102fe606127a67f6@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <000000000000102fe606127a67f6@google.com> X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792262833632631978 X-GMAIL-MSGID: 1792262833632631978 The isd200 sub-driver in usb-storage uses the HEADS and SECTORS values in the ATA ID information to calculate cylinder and head values when creating a CDB for READ or WRITE commands. The calculation involves division and modulus operations, which will cause a crash if either of these values is 0. While this never happens with a genuine device, it could happen with a flawed or subversive emulation, as reported by the syzbot fuzzer. Protect against this possibility by refusing to bind to the device if either the ATA_ID_HEADS or ATA_ID_SECTORS value in the device's ID information is 0. This requires isd200_Initialization() to return a negative error code when initialization fails; currently it always returns 0 (even when there is an error). Signed-off-by: Alan Stern Reported-and-tested-by: syzbot+28748250ab47a8f04100@syzkaller.appspotmail.com Link: https://lore.kernel.org/linux-usb/0000000000003eb868061245ba7f@google.com/ Fixes: 1da177e4c3f4 ("v2.6.12-rc2") Cc: Reviewed-by: Martin K. Petersen Reviewed-by: PrasannaKumar Muralidharan --- drivers/usb/storage/isd200.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) Index: usb-devel/drivers/usb/storage/isd200.c =================================================================== --- usb-devel.orig/drivers/usb/storage/isd200.c +++ usb-devel/drivers/usb/storage/isd200.c @@ -1105,7 +1105,7 @@ static void isd200_dump_driveid(struct u static int isd200_get_inquiry_data( struct us_data *us ) { struct isd200_info *info = (struct isd200_info *)us->extra; - int retStatus = ISD200_GOOD; + int retStatus; u16 *id = info->id; usb_stor_dbg(us, "Entering isd200_get_inquiry_data\n"); @@ -1137,6 +1137,13 @@ static int isd200_get_inquiry_data( stru isd200_fix_driveid(id); isd200_dump_driveid(us, id); + /* Prevent division by 0 in isd200_scsi_to_ata() */ + if (id[ATA_ID_HEADS] == 0 || id[ATA_ID_SECTORS] == 0) { + usb_stor_dbg(us, " Invalid ATA Identify data\n"); + retStatus = ISD200_ERROR; + goto Done; + } + memset(&info->InquiryData, 0, sizeof(info->InquiryData)); /* Standard IDE interface only supports disks */ @@ -1202,6 +1209,7 @@ static int isd200_get_inquiry_data( stru } } + Done: usb_stor_dbg(us, "Leaving isd200_get_inquiry_data %08X\n", retStatus); return(retStatus); @@ -1481,22 +1489,27 @@ static int isd200_init_info(struct us_da static int isd200_Initialization(struct us_data *us) { + int rc = 0; + usb_stor_dbg(us, "ISD200 Initialization...\n"); /* Initialize ISD200 info struct */ - if (isd200_init_info(us) == ISD200_ERROR) { + if (isd200_init_info(us) < 0) { usb_stor_dbg(us, "ERROR Initializing ISD200 Info struct\n"); + rc = -ENOMEM; } else { /* Get device specific data */ - if (isd200_get_inquiry_data(us) != ISD200_GOOD) + if (isd200_get_inquiry_data(us) != ISD200_GOOD) { usb_stor_dbg(us, "ISD200 Initialization Failure\n"); - else + rc = -EINVAL; + } else { usb_stor_dbg(us, "ISD200 Initialization complete\n"); + } } - return 0; + return rc; }