Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1914779pxb; Thu, 16 Sep 2021 20:07:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy8+yqrQy057H//uqqKMI/imvy4qc1NUg2949R0ALGM7toIDpojk/hRfHMvjXidnu0fFLP4 X-Received: by 2002:a17:906:fc09:: with SMTP id ov9mr9809846ejb.128.1631848038184; Thu, 16 Sep 2021 20:07:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631848038; cv=none; d=google.com; s=arc-20160816; b=uEvZcvfRGTzE6a+GFoiT5a6scPcsAFHwOczHBph5wgQM43UiHLLIow9nzSJM6J44Dd ChX0m1x4ppji8LQf8NzzLnm7snjFMPp6VjGZ8zI0kMnPnqGYh36Sg4XsRGnXGBMCGKD7 MCnQs1KN4mmVa9BlhJZd6HXaeTFKCiQXt/9sn9uDsZZ+ly9QmeSfKqMO/mw+Feije1Nw Tc7NOmXZtS4Ho8XnQXQOQXBMPgmeIDwTw1BZsPiIn7QgtXiRSPUzbtfYSVjci4MttKqG ceMqwysnJ0+Vm9rEVKUPWGSnrrzVlz8frMG3Gsxw+F+3teRLtvqa+bwYOKp8uJaDBwju QiQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=SFelQ3M72+0u5zgPn4OW8mcdet0fCYxIYSLhplOd6+M=; b=WvGBSR25AkWQ3zfrl6hTkZhemtIUBHCj5ldrfQtx9gsJ2YX8a1NwIlS1lO7pXGOG1v misuFrpgCL+wUAO9+wwuswnOuNoqzb0uTGNsMJeE03a4rTlwA32dcJj+C0ggioC3OAMC 28IulNYzdKQxFZiR36NIQJAZWcrSEFhAJTD+1jo6rVNu+64UBE7LaUzKywEiirbj71xp mNdDVo6r84qAGATdT1wbOGwqhVOjMPeRMFS+zuLyydy+Xjq00bNa/pnlhf9WF61w5nhP yue9oqgSiJGZJGaMSwKqCwOy+zuh1HTMwlrI1i4eULwGNxazEPZ8QcWu3jQrbedxMBxA 6Wxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fr5GS6IK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p15si6895616edj.97.2021.09.16.20.06.55; Thu, 16 Sep 2021 20:07:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fr5GS6IK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352581AbhIPRY6 (ORCPT + 99 others); Thu, 16 Sep 2021 13:24:58 -0400 Received: from mail.kernel.org ([198.145.29.99]:39922 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343575AbhIPRQG (ORCPT ); Thu, 16 Sep 2021 13:16:06 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id D806C61BBE; Thu, 16 Sep 2021 16:40:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631810417; bh=2xJEJpfSoyORzGxGTI3s7ieeq25y1kEm0yKthuAsI48=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fr5GS6IKl7iRlP1CBo3IyJNjwsZdfwohHYleENJYjjEnKde75OUVOXkMqmxTSxkDy kSKDpoHJvesTkzVgDAL2UbWj41pvZrLpD83hl3h4V0aelg3NEjalR3sBQgkMMz/c9m 1lEsCg7Ju/Ix2ekHoQLdoZYbzhgOh6KmU/+fSjoo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Avri Altman , Daejun Park , Bart Van Assche , "Martin K. Petersen" , Sasha Levin Subject: [PATCH 5.14 091/432] scsi: ufs: Fix memory corruption by ufshcd_read_desc_param() Date: Thu, 16 Sep 2021 17:57:20 +0200 Message-Id: <20210916155813.863232564@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210916155810.813340753@linuxfoundation.org> References: <20210916155810.813340753@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bart Van Assche [ Upstream commit d3d9c4570285090b533b00946b72647361f0345b ] If param_offset > buff_len then the memcpy() statement in ufshcd_read_desc_param() corrupts memory since it copies 256 + buff_len - param_offset bytes into a buffer with size buff_len. Since param_offset < 256 this results in writing past the bound of the output buffer. Link: https://lore.kernel.org/r/20210722033439.26550-2-bvanassche@acm.org Fixes: cbe193f6f093 ("scsi: ufs: Fix potential NULL pointer access during memcpy") Reviewed-by: Avri Altman Reviewed-by: Daejun Park Signed-off-by: Bart Van Assche Signed-off-by: Martin K. Petersen Signed-off-by: Sasha Levin --- drivers/scsi/ufs/ufshcd.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c index 708b3b62fc4d..179227180961 100644 --- a/drivers/scsi/ufs/ufshcd.c +++ b/drivers/scsi/ufs/ufshcd.c @@ -3419,9 +3419,11 @@ int ufshcd_read_desc_param(struct ufs_hba *hba, if (is_kmalloc) { /* Make sure we don't copy more data than available */ - if (param_offset + param_size > buff_len) - param_size = buff_len - param_offset; - memcpy(param_read_buf, &desc_buf[param_offset], param_size); + if (param_offset >= buff_len) + ret = -EINVAL; + else + memcpy(param_read_buf, &desc_buf[param_offset], + min_t(u32, param_size, buff_len - param_offset)); } out: if (is_kmalloc) -- 2.30.2