Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp189243pxa; Tue, 4 Aug 2020 03:07:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxe5yGy7Xkd/ILJ0+6zcXQ0LMbE0dPnm/wP3X8VWKyflCwZYScSat9/FO+STixlxAzc92VO X-Received: by 2002:a17:906:7b82:: with SMTP id s2mr19989607ejo.63.1596535637626; Tue, 04 Aug 2020 03:07:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596535637; cv=none; d=google.com; s=arc-20160816; b=uPYokuh3vm9ghcyfWGIYbGlNcMtsg83aQ0Eb9G9BZ1xHz9R1gIZWVLPMa4r9/YB+x+ dM4yyf/GsZN4B7EVbMOHgBRQUwXVeXSGlE3Tr0C01ecTFfUgwLsslf0KaHU9CKKLblap Fm2iIpbtr+jkL8l6H7Y3TWiqmOOQp+/zUs0W9voTszszcmYhjW6D2YIBQQaPRuwhJAQl OuNEL5W19AUcaDjxMyVEImHKPTGsRjXDleoEM1Xn8C/am/27cssSuCcYr2jIAD8VXN1n pUQMqu91Nj1G7k9SxXg7Ter8MGdLyypEUtL1pbuK34Rrcs97Z/Itz4HDQ7Jzbz1kCWWu Ffag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=lzbKl+uUFfh9XAYP8GcmWr39WT1Cz06a6UbBamDt1vA=; b=r/krwht9uwtj3ZK8c5a0lQX2Rpi4tKrPOgsyIGuxZG6ryZSYnHC+dV2WIJSqdVfDgS U6k0ogc7DGQiJxJb7bYuT5CHVrzHRowcznYpmrf3kd0+EJMy4nUc7BaC5ugDoOVdLmIN +dLn4h9iHj8EYEtnFDw/qXGcBzPVgneARyNrPk5k66CHUqi4yoKECzY3lpP5UhKtHaNj xvejFPqVsdwhJvKDmUAlmE9I6jOttfRwTGD+9w1g6karCEwCq3v5D6N9ql6O4WElEEY5 somp9w7GwH8sbHp9pR3mswAeTexTyZISdhup+CKuNrSawaTSeb8jTpa1/jE/eot43oPI cgNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Bv+t10uk; 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=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o89si3646918eda.223.2020.08.04.03.06.55; Tue, 04 Aug 2020 03:07:17 -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=@linaro.org header.s=google header.b=Bv+t10uk; 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=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730134AbgHDKD3 (ORCPT + 99 others); Tue, 4 Aug 2020 06:03:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44634 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727792AbgHDKD3 (ORCPT ); Tue, 4 Aug 2020 06:03:29 -0400 Received: from mail-ed1-x543.google.com (mail-ed1-x543.google.com [IPv6:2a00:1450:4864:20::543]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2BECC06174A for ; Tue, 4 Aug 2020 03:03:28 -0700 (PDT) Received: by mail-ed1-x543.google.com with SMTP id i6so3773493edy.5 for ; Tue, 04 Aug 2020 03:03:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=lzbKl+uUFfh9XAYP8GcmWr39WT1Cz06a6UbBamDt1vA=; b=Bv+t10ukpzqXnpeDVdA/0sv/wrBsyyxnYg5VPqUER0eYAtKneX2hO/6XQEfibbkG9T y1kp0dJ56fgFTc95lTehXdAZoCKO1Ov1uanAKP+EwnwBcDOEbWpseIdcJ73EMOie2zTr rz71UFOrnNQO86pLgSrLiBIqbDJbya9mLMRKWSl95K/f0Pv2u7f9eGCuRDmZk3ba/Uns E4MPruV0OWTaG2M2pZU3nwSs5KKXf4KaLncmKLeYtZJ8ObGLVLZI/jOzMvCoUHx3bgc8 YoVlJZ3NK8rjkr2aYSxGMMGwqtzSGzcsoO4uoAgLsVKQ4WbckQHa0iokZBwhdIodABny LYmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=lzbKl+uUFfh9XAYP8GcmWr39WT1Cz06a6UbBamDt1vA=; b=JnUAdtbai2CFVKxqJo4wLnMzx5dYk02zR20S4vOqSht8RB0w076pid1zjek/8QaLEa l7JNEp9ztVbTcvkyukLz+3X/TdEQ7vzmrlvxLiAnv4NSo+Q4jw7u/cPfmOuV9/aTgMso oawk7+c45YGneX87oqU7FPdFERt3qGslk4cDghGYINyPYhUqSWsX8xPNRctgXw91Unmb TPnj9mSmSHGhqYlvbiwXCJy3Ntlaw44T+pUHw+NHeEVfftR6qW3UiAaW5H+zf3ivTIp5 R5q5bRHz92sWT5H9PK/C5tWnmx4WdK+bQAqdOMJ94UtARGFxfjQRwFF1Kc1t2amJ6Gxq 49ww== X-Gm-Message-State: AOAM531tLCAPeZWXcXunqf33vL7jYYZU11TV0pa0zMMr3uYJoWuhjdV3 4tImacOfb+Kg7NYR3S2NltoXCA== X-Received: by 2002:aa7:d6c2:: with SMTP id x2mr15912050edr.332.1596535407703; Tue, 04 Aug 2020 03:03:27 -0700 (PDT) Received: from [192.168.86.34] (cpc86377-aztw32-2-0-cust226.18-1.cable.virginm.net. [92.233.226.227]) by smtp.googlemail.com with ESMTPSA id bt20sm18481153edb.62.2020.08.04.03.03.26 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 04 Aug 2020 03:03:27 -0700 (PDT) Subject: Re: [PATCH] nvmem: core: add sanity check in nvmem_device_read() To: Sakari Ailus , Bingbu Cao Cc: linux-kernel@vger.kernel.org, bingbu.cao@linux.intel.com References: <1596532436-19073-1-git-send-email-bingbu.cao@intel.com> <20200804095815.GF13316@paasikivi.fi.intel.com> From: Srinivas Kandagatla Message-ID: <0e8d60cf-7f8b-c6ab-4184-b869c16b603e@linaro.org> Date: Tue, 4 Aug 2020 11:03:26 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: <20200804095815.GF13316@paasikivi.fi.intel.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 04/08/2020 10:58, Sakari Ailus wrote: > Hi Bingbu, > > Thank you for the patch. > > On Tue, Aug 04, 2020 at 05:13:56PM +0800, Bingbu Cao wrote: >> nvmem_device_read() could be called directly once nvmem device >> registered, the sanity check should be done before call >> nvmem_reg_read() as cell and sysfs read did now. >> >> Signed-off-by: Bingbu Cao >> --- >> drivers/nvmem/core.c | 7 +++++++ >> 1 file changed, 7 insertions(+) >> >> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c >> index 927eb5f6003f..c9a77993f008 100644 >> --- a/drivers/nvmem/core.c >> +++ b/drivers/nvmem/core.c >> @@ -1491,6 +1491,13 @@ int nvmem_device_read(struct nvmem_device *nvmem, >> if (!nvmem) >> return -EINVAL; >> >> + if (offset >= nvmem->size || bytes < nvmem->word_size) >> + return 0; >> + >> + if (bytes + offset > nvmem->size) >> + bytes = nvmem->size - offset; > > The check is relevant for nvmem_device_write(), too. > > There are also other ways to access nvmem devices such as nvmem_cell_read > and others alike. Should they be considered as well? We should probably move these sanity checks to a common place like nvmem_reg_read() and nvmem_reg_write(), so the callers need not duplicate the same! --srini > >> + >> + bytes = round_down(bytes, nvmem->word_size); >> rc = nvmem_reg_read(nvmem, offset, buf, bytes); >> >> if (rc) >