Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp161172pxa; Tue, 4 Aug 2020 02:16:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy6rEjBQ7ds5Eg0w9+bkVn3qri3RXVwq61+SxgizXVkmu/CY2I6RsKnkksw/YgN53hIZCT0 X-Received: by 2002:a17:907:444c:: with SMTP id on20mr21551198ejb.77.1596532571469; Tue, 04 Aug 2020 02:16:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596532571; cv=none; d=google.com; s=arc-20160816; b=IzKdLdQa0UXeuFSzO4ZvEJMtgapOmeF3YSGNpfPmLnfKgBHDIahM3gticM5DowPEDI ItuBhgc2gaDgvTGNCAxje2eCjBrg3AxT4f8FBj481JQzw5RMW7JJ4w0uYNZRgchyFTe7 aqhrfgMzyEz+XCSrco85wXVMu6BJozucYWGXz8cYJq+vdfiijiFBJ3kVh02bl9LSg7Nh Jr7K7sb3hBtfHhGdckDsLgKmP/wW9EXP2fSBvRLLwLmy/5ebOLysdK+d/HLMICrGDcik lbtQDXifL2exAmB5fLmIXzeNx/SNlrNeTrGd++lXQyE61aVDXxojVyM5+sHb/UNGuegv NvFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr; bh=IeZ7gqXCgb7Gk6gTjOVHhMVDOevZcPvh9C0V6/9bvdM=; b=lQfjrEUTEI3L+eGtxjeHdDRWhKgZ8ZKMI83oE3/R5Ygz/ujI1VO7rIjYIMUiN/dLPX 4ShcCz4dr4yo3+W2QZEMY8fEesc3rUulkqUL2IqOFEQItet1Z4QJa7LGP4rgCYYC9ScW bzQz94wGcFkfiBkSfny8RX0JbJKjJEouXvbSETvMU0pV4DNO7OzT3xUaUhfPvnh0SmKV /QQpQ9tvZOd9LJCA4tvXi7u1VmW5CTG4ybW7O5vofMihV4qA7Zj8G061zwj1e27k0rdK EZf5W792E2KLxZ2vZIwLgK9jk644Cw49MXE6zf3lIrHnRokSQ5zo96l5tevfaGcMsQih j7KQ== 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 by25si7246915ejb.281.2020.08.04.02.15.49; Tue, 04 Aug 2020 02:16:11 -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 S1727066AbgHDJPR (ORCPT + 99 others); Tue, 4 Aug 2020 05:15:17 -0400 Received: from mga12.intel.com ([192.55.52.136]:26085 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725856AbgHDJPQ (ORCPT ); Tue, 4 Aug 2020 05:15:16 -0400 IronPort-SDR: etIyCEtHzeIxmC6DOJyM876zRR8mbmnhoVYoz/eUitHAbZtQ2yoS/d+YgeVDEoEwi2/by75XLN d8xPGFoN8JSA== X-IronPort-AV: E=McAfee;i="6000,8403,9702"; a="131846744" X-IronPort-AV: E=Sophos;i="5.75,433,1589266800"; d="scan'208";a="131846744" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Aug 2020 02:15:16 -0700 IronPort-SDR: uOX8vFjOZCA1KulpAbQNwzaINFIwCQ+himuKYXfYAYTJUZraK3mJnel0teLCwuD0AK3Kqquc4E c7bG0JaWRNIw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.75,433,1589266800"; d="scan'208";a="466989286" Received: from ipu5-build.bj.intel.com ([10.238.232.196]) by orsmga005.jf.intel.com with ESMTP; 04 Aug 2020 02:15:14 -0700 From: Bingbu Cao To: srinivas.kandagatla@linaro.org, linux-kernel@vger.kernel.org Cc: sakari.ailus@linux.intel.com, bingbu.cao@intel.com, bingbu.cao@linux.intel.com Subject: [PATCH] nvmem: core: add sanity check in nvmem_device_read() Date: Tue, 4 Aug 2020 17:13:56 +0800 Message-Id: <1596532436-19073-1-git-send-email-bingbu.cao@intel.com> X-Mailer: git-send-email 2.7.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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; + + bytes = round_down(bytes, nvmem->word_size); rc = nvmem_reg_read(nvmem, offset, buf, bytes); if (rc) -- 2.7.4