Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp217790pxa; Tue, 4 Aug 2020 03:58:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyBrmgdZzggdu8GbvclqnzdiuKuujq2Dd3pmek56y96FKtdcsJ/5MPIt83mkSXv7Ppf7whY X-Received: by 2002:a17:906:924b:: with SMTP id c11mr21458197ejx.75.1596538692421; Tue, 04 Aug 2020 03:58:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596538692; cv=none; d=google.com; s=arc-20160816; b=0qP3TlqWlkI67gQ9kyRZR0aLXg1cCxnKnDgcZzM6Ev/Qjq3mgzVr9hpFKmlmnuWq1r 3xR4K3XWgKhQxSP0JXa0OvEU7D+jpEBw48ier8H36MsLMB0cfAep1/7SGqiTufHHypay oaZpg1HYerULtf2+ys3AIjTYCmYB5B5YMXuoEqQJSMfmuCeneA3CSOFOcyzrGBLe6hT8 GpwKth3FEYi4VVX7lbQoLl79wWdmIEnxSFEQSeF+6aYJ1kKEHfyhSj9qPXcHc7GL3ZgX mf91XmCipkOfTSvUWWTVi3kmNAba1/hyrWVZL1OsPNa7LDsBC+ybYEp8+mh0fDHZ2hez YtgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:ironport-sdr :ironport-sdr; bh=v8iOyle+EeNozf+kOGPP6Okq7P0xtWDXDNbjTX72FwY=; b=XzeZ+Nr0ZFgJyyydOwXprY33PB5hhNrdI74taEPpMu/16w48Xkhkh1ZaOBOhNAJdmg qq1XnexATEdx6Pn8AdZpR33uGw511MrpbYwaRpOpRWc7hq2fLUKCHZ0AuVibzJ2j6LRW MB49tkwRkld30vsRJ8iw4ouhiLWJNMJR1VTJ0/KfmdBh6JO/wYU+5g/DGnKNCVW8a1op kYBPlY20Lss9vmczizzccIR3ytUUmJ9F5qlN+iQoVonCBBwdVzRqupAXbdYL151gfhun YjHh2/YOvpC0U8GAHOXVoYKjYN7rFso41SQLQNvfOx2CSKoYnvvTHkClvqwGfXX7Lg6Q EsUw== 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u16si12000569ejg.142.2020.08.04.03.57.48; Tue, 04 Aug 2020 03:58:12 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730031AbgHDK4z (ORCPT + 99 others); Tue, 4 Aug 2020 06:56:55 -0400 Received: from mga01.intel.com ([192.55.52.88]:30872 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729549AbgHDK4x (ORCPT ); Tue, 4 Aug 2020 06:56:53 -0400 IronPort-SDR: dwA5Z+dNBSjKEXfmxYOW7onJDA3OLXZuHXxqdzzoqgepxyYg2Hpd8NHbqmlcmF2mAEFJKvscOO 8Nl3KxCE9EWQ== X-IronPort-AV: E=McAfee;i="6000,8403,9702"; a="170373297" X-IronPort-AV: E=Sophos;i="5.75,433,1589266800"; d="scan'208";a="170373297" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Aug 2020 03:56:53 -0700 IronPort-SDR: GA/hxUYtLCY4tWeJlOq1OnY61DR1NJPKT9f5Z7/8Xcbk03qNHUzQWKkSNKxM5oyKoapbkO+bGM Y/ziYhlodoUw== X-IronPort-AV: E=Sophos;i="5.75,433,1589266800"; d="scan'208";a="306309145" Received: from paasikivi.fi.intel.com ([10.237.72.42]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Aug 2020 03:56:51 -0700 Received: by paasikivi.fi.intel.com (Postfix, from userid 1000) id AD7DF20606; Tue, 4 Aug 2020 13:56:49 +0300 (EEST) Date: Tue, 4 Aug 2020 13:56:49 +0300 From: Sakari Ailus To: Bingbu Cao Cc: Srinivas Kandagatla , Bingbu Cao , linux-kernel@vger.kernel.org Subject: Re: [PATCH] nvmem: core: add sanity check in nvmem_device_read() Message-ID: <20200804105649.GG13316@paasikivi.fi.intel.com> References: <1596532436-19073-1-git-send-email-bingbu.cao@intel.com> <20200804095815.GF13316@paasikivi.fi.intel.com> <0e8d60cf-7f8b-c6ab-4184-b869c16b603e@linaro.org> <48f2d716-dad9-054d-d049-452fa272d53b@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <48f2d716-dad9-054d-d049-452fa272d53b@linux.intel.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Aug 04, 2020 at 06:44:27PM +0800, Bingbu Cao wrote: > > On 8/4/20 6:03 PM, Srinivas Kandagatla wrote: > > > > > > 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 and Sakari, thanks for your review. > > Is it OK just return INVAL with simple check like below? > > if (bytes + offset > nvmem->size || > bytes != round_down(bytes, nvmem->word_size)) > return -EINVAL; This changes what is currently supported so I'd say no. -- Sakari Ailus