Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp487220pxu; Thu, 3 Dec 2020 05:34:03 -0800 (PST) X-Google-Smtp-Source: ABdhPJz9ZSGrQl3KYncDOWqljXYin/c8CG+x7v6HA9ue0OS5NWWFZGLHKz8eQpFiVo89aHPzWbCl X-Received: by 2002:a17:906:3ad5:: with SMTP id z21mr2481110ejd.35.1607002443426; Thu, 03 Dec 2020 05:34:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607002443; cv=none; d=google.com; s=arc-20160816; b=N08l/UTtMov+beO7Z67KR2AG45vNsDW6orerimOmUk9DBQ6FZxkIaDEiwYdBWzoTji XtTumc3YaPFvKY6hFVV8zgltw4KPBCPAFhJptV/4x81RHkevBvumwIoYcskG9gDKJaYx 8VMH36Qvtnoa+KXsR55izf27QQwMRNPvaeHlpjOvdJY/QNdeRvDdissCvnmUTgb3vHFJ f+TWxyjrXyJo+xD0MG0v9LH1JFggljVNXMo99KmbxAhj0rHDuwkpTA/8q+68m1JyzSG2 oyBa0Pv3BVzLuV+FgczR5Aun0Dgi+oMCesnBG79L0+RM8m2Ddo/aURGM750Smcblxg5E ek8w== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=ut0A3zvV+R33itDrv1zWkE3Q83eVL4pU3zvAk7YgjMQ=; b=wLUNZ7sxvuhpaANFdV1qC1/K+Sy1APSbpnDMyDh7ute4y6kCTosgACDAV67LGQvYQH 4z0TcqwkYPpPPieZYU8SWKjJDE46/L9dsvYYVTHzH5QEIYRZdL72EfeEETEE5sDmew40 v6pwXnEhIZXy88/UbDqaPgLdVf5I3Ukdn6My1SSUw2nmjUBPm2VSlS/Qj0e2KVeaz0f8 3NMnyo9zIg0+xN01JIPqnZR3P0C9mMyGL5AQ8DhZEGNjvHX/WvheloKDFQfwg58UVqE/ zf8urVf+2zg4/PhWSoNr//LMBwTdze4YUW4YsTX3ojiXr3sYoumqyrEVo48EYfILJnZ1 zBpg== ARC-Authentication-Results: i=1; mx.google.com; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g6si1107721ejw.417.2020.12.03.05.33.30; Thu, 03 Dec 2020 05:34:03 -0800 (PST) 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; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2436547AbgLCNaT (ORCPT + 99 others); Thu, 3 Dec 2020 08:30:19 -0500 Received: from mail.kernel.org ([198.145.29.99]:47984 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2436463AbgLCNaF (ORCPT ); Thu, 3 Dec 2020 08:30:05 -0500 From: Sasha Levin Authentication-Results: mail.kernel.org; dkim=permerror (bad message/signature format) To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Can Guo , Asutosh Das , Daejun Park , "Martin K . Petersen" , Sasha Levin , linux-scsi@vger.kernel.org Subject: [PATCH AUTOSEL 5.9 12/39] scsi: ufs: Fix unexpected values from ufshcd_read_desc_param() Date: Thu, 3 Dec 2020 08:28:06 -0500 Message-Id: <20201203132834.930999-12-sashal@kernel.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20201203132834.930999-1-sashal@kernel.org> References: <20201203132834.930999-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Can Guo [ Upstream commit 1699f980d87fb678a669490462cf0b9517c1fb47 ] WB-related sysfs entries can be accessed even when an UFS device does not support the feature. The descriptors which are not supported by the UFS device may be wrongly reported when they are accessed from their corrsponding sysfs entries. Fix it by adding a sanity check of parameter offset against the actual decriptor length. Link: https://lore.kernel.org/r/1603346348-14149-1-git-send-email-cang@codeaurora.org Reviewed-by: Asutosh Das Acked-by: Daejun Park Signed-off-by: Can Guo Signed-off-by: Martin K. Petersen Signed-off-by: Sasha Levin --- drivers/scsi/ufs/ufshcd.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c index 9dd32bb0ff2be..cbcdd79a1f76f 100644 --- a/drivers/scsi/ufs/ufshcd.c +++ b/drivers/scsi/ufs/ufshcd.c @@ -3163,13 +3163,19 @@ int ufshcd_read_desc_param(struct ufs_hba *hba, /* Get the length of descriptor */ ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len); if (!buff_len) { - dev_err(hba->dev, "%s: Failed to get desc length", __func__); + dev_err(hba->dev, "%s: Failed to get desc length\n", __func__); + return -EINVAL; + } + + if (param_offset >= buff_len) { + dev_err(hba->dev, "%s: Invalid offset 0x%x in descriptor IDN 0x%x, length 0x%x\n", + __func__, param_offset, desc_id, buff_len); return -EINVAL; } /* Check whether we need temp memory */ if (param_offset != 0 || param_size < buff_len) { - desc_buf = kmalloc(buff_len, GFP_KERNEL); + desc_buf = kzalloc(buff_len, GFP_KERNEL); if (!desc_buf) return -ENOMEM; } else { @@ -3183,14 +3189,14 @@ int ufshcd_read_desc_param(struct ufs_hba *hba, desc_buf, &buff_len); if (ret) { - dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d", + dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d\n", __func__, desc_id, desc_index, param_offset, ret); goto out; } /* Sanity check */ if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) { - dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header", + dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header\n", __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]); ret = -EINVAL; goto out; @@ -3200,12 +3206,12 @@ int ufshcd_read_desc_param(struct ufs_hba *hba, buff_len = desc_buf[QUERY_DESC_LENGTH_OFFSET]; ufshcd_update_desc_length(hba, desc_id, desc_index, buff_len); - /* Check wherher we will not copy more data, than available */ - if (is_kmalloc && (param_offset + param_size) > buff_len) - param_size = buff_len - param_offset; - - if (is_kmalloc) + 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); + } out: if (is_kmalloc) kfree(desc_buf); -- 2.27.0