Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp881001pxj; Wed, 16 Jun 2021 16:10:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzrD28oxTfsaFUWYyCnKLtvLiLDtm+qhT8SH0FEX/liUEkGjS0pT8l8DX2CGUFz6rqvyFZS X-Received: by 2002:a6b:7b0a:: with SMTP id l10mr1315852iop.120.1623885016535; Wed, 16 Jun 2021 16:10:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623885016; cv=none; d=google.com; s=arc-20160816; b=BKp5nyjcUtc3xZUnS+zuSS7LiGl9flCUqdaVT69xDYp14KSHWCAEpqbpmAq42XuAin nOfKriJwQ9RclUu7uIkMns0fC0AK9MkUYReT6kulcuYgeNmV13YAKMe8P5TWvOaQ20Sm 4lWd0Fqw5+lsrGro/HmQ54vYjG5P4nRodZems44AnBaqi3OaMTK8EwytfKhDmQtg8VJR 05GKgrHPQf+ym8oj+T6F1oZoqHvWl0oUEHFMYyZ7UoLzvQykUCLBIcgnsfqxsifCMSDg Mkl3qoCCjUDPEm/8tYM5ghVKnyVnx4vP6ZKLxATHobewP2qiLiKQFJcceN+C2tE1ZR5+ Zt5w== 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=PdKIXykUjf4LF3GTyMGGtJqD8/ohir3O9MymRjBO50Q=; b=khbYvMUnWKBvyhuqLRKmJDbHB8ZJro0gYmHTlLTgly8OuoBErKDUNiY2o+DYrLrCp4 FU6uT8gaOaSI0kOzwXXK0Zfyf9/PvoA5+ysZSZq9O7fClbvDEwE0sy4WynzVR8v5Mxeg h0ll/4W8PGYmRHfIbnWywexpmme06K42OlOTCsIKtSqJ6CxFZtjART94rFUvZj8aoj+g ozo77WECvDwWAX3A9tlE9cmnZRaAFcvzL0Dt5Itf/Qj2cuFvX1cSXtcRNQSDHMWo61pf bGcgKPVV6Arqdk9nZ/Ytv2alRqvALsYO+6pA3M15RwXDs3ey1kEgiJrnkkuzpyb/tAtU aScw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=s9MlY+1y; 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 z4si3976027iod.34.2021.06.16.16.10.05; Wed, 16 Jun 2021 16:10:16 -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=s9MlY+1y; 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 S235474AbhFPPoi (ORCPT + 99 others); Wed, 16 Jun 2021 11:44:38 -0400 Received: from mail.kernel.org ([198.145.29.99]:51224 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235475AbhFPPmA (ORCPT ); Wed, 16 Jun 2021 11:42:00 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 9AE6B613FB; Wed, 16 Jun 2021 15:37:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1623857868; bh=fA/354/L+S/ZajiGO7SQs/tFxlMSF58jI9v+vBeDN9c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=s9MlY+1yyDGLAwMXnNwydrsJGVeqxShKYsHAY8H2xi++OTk5i+b7a6+L17YrfWzVk qEU55tTKSCYTeuTkmNlCB32+95lQjzBacrvVaT6j6lfJxO/5xnVRqLNwkrXTnOCaTC mIy2vQMyj0aMEqVB5kfn6K3d2DL9Tj5HIrwLufyA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jiansong Chen , Jack Gui , Alex Deucher , Sasha Levin Subject: [PATCH 5.12 41/48] drm/amdgpu: refine amdgpu_fru_get_product_info Date: Wed, 16 Jun 2021 17:33:51 +0200 Message-Id: <20210616152837.941396447@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210616152836.655643420@linuxfoundation.org> References: <20210616152836.655643420@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: Jiansong Chen [ Upstream commit 5cfc912582e13b05d71fb7acc4ec69ddfa9af320 ] 1. eliminate potential array index out of bounds. 2. return meaningful value for failure. Signed-off-by: Jiansong Chen Reviewed-by: Jack Gui Signed-off-by: Alex Deucher Signed-off-by: Sasha Levin --- .../gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c | 42 ++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c index 8f4a8f8d8146..39b6c6bfab45 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c @@ -101,7 +101,8 @@ static int amdgpu_fru_read_eeprom(struct amdgpu_device *adev, uint32_t addrptr, int amdgpu_fru_get_product_info(struct amdgpu_device *adev) { unsigned char buff[34]; - int addrptr = 0, size = 0; + int addrptr, size; + int len; if (!is_fru_eeprom_supported(adev)) return 0; @@ -109,7 +110,7 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev) /* If algo exists, it means that the i2c_adapter's initialized */ if (!adev->pm.smu_i2c.algo) { DRM_WARN("Cannot access FRU, EEPROM accessor not initialized"); - return 0; + return -ENODEV; } /* There's a lot of repetition here. This is due to the FRU having @@ -128,7 +129,7 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev) size = amdgpu_fru_read_eeprom(adev, addrptr, buff); if (size < 1) { DRM_ERROR("Failed to read FRU Manufacturer, ret:%d", size); - return size; + return -EINVAL; } /* Increment the addrptr by the size of the field, and 1 due to the @@ -138,43 +139,45 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev) size = amdgpu_fru_read_eeprom(adev, addrptr, buff); if (size < 1) { DRM_ERROR("Failed to read FRU product name, ret:%d", size); - return size; + return -EINVAL; } + len = size; /* Product name should only be 32 characters. Any more, * and something could be wrong. Cap it at 32 to be safe */ - if (size > 32) { + if (len >= sizeof(adev->product_name)) { DRM_WARN("FRU Product Number is larger than 32 characters. This is likely a mistake"); - size = 32; + len = sizeof(adev->product_name) - 1; } /* Start at 2 due to buff using fields 0 and 1 for the address */ - memcpy(adev->product_name, &buff[2], size); - adev->product_name[size] = '\0'; + memcpy(adev->product_name, &buff[2], len); + adev->product_name[len] = '\0'; addrptr += size + 1; size = amdgpu_fru_read_eeprom(adev, addrptr, buff); if (size < 1) { DRM_ERROR("Failed to read FRU product number, ret:%d", size); - return size; + return -EINVAL; } + len = size; /* Product number should only be 16 characters. Any more, * and something could be wrong. Cap it at 16 to be safe */ - if (size > 16) { + if (len >= sizeof(adev->product_number)) { DRM_WARN("FRU Product Number is larger than 16 characters. This is likely a mistake"); - size = 16; + len = sizeof(adev->product_number) - 1; } - memcpy(adev->product_number, &buff[2], size); - adev->product_number[size] = '\0'; + memcpy(adev->product_number, &buff[2], len); + adev->product_number[len] = '\0'; addrptr += size + 1; size = amdgpu_fru_read_eeprom(adev, addrptr, buff); if (size < 1) { DRM_ERROR("Failed to read FRU product version, ret:%d", size); - return size; + return -EINVAL; } addrptr += size + 1; @@ -182,18 +185,19 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev) if (size < 1) { DRM_ERROR("Failed to read FRU serial number, ret:%d", size); - return size; + return -EINVAL; } + len = size; /* Serial number should only be 16 characters. Any more, * and something could be wrong. Cap it at 16 to be safe */ - if (size > 16) { + if (len >= sizeof(adev->serial)) { DRM_WARN("FRU Serial Number is larger than 16 characters. This is likely a mistake"); - size = 16; + len = sizeof(adev->serial) - 1; } - memcpy(adev->serial, &buff[2], size); - adev->serial[size] = '\0'; + memcpy(adev->serial, &buff[2], len); + adev->serial[len] = '\0'; return 0; } -- 2.30.2