Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp73629pxa; Thu, 13 Aug 2020 20:04:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz67r1Jlo/6JCXTo9CSJwyth/1uE7sJ2CnPRn/q9aej+J0UFM/M1+Gxz8aFGKZI3mU72k2f X-Received: by 2002:a17:906:98c1:: with SMTP id zd1mr515721ejb.410.1597374273372; Thu, 13 Aug 2020 20:04:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597374273; cv=none; d=google.com; s=arc-20160816; b=ROX19skU6NJuMl+qsl8zrSebEYPluo7EL3P8xw3ASNP8tBL0nfThJaqcrgpin4Ipsn bkub7uu6JPvoBV9WBEkYNSaSQ9lKKlU/swaRupU0dCVyaUIFEfSWZ2RGKnr5WUQBhfZA ibeHRjJTJXK67YERGO1xlszI4ss8LGjYX+52Z+PNuWLn9npAw1iJZ7uUUB9zTLengQ2+ e5cjmd2KkatTHNg/ByJaqiSyiEGlBhvgkkV6j7XKKiGAAzZpm26Ju8RN+kbxp+U233yb e8p7Yu2HhuspUGsD2M7zGZ1wzHnC5U/MLEhUJnZnINOsZ/+L3pHN9/KBMSIChihgo/AI qLWw== 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=SzksHG2/7mqMdlyjbclLuTzBQf6Hq/dTY34QsLR1ij0=; b=ZDUWoIgQ+nI4DwsQXyRjkP7BwNCjlsjFr7wTszcGC+LUz807rztpmi+kIAvaTOmlkV daBpOspVEJkzk13K7IeBEbkPFVnDDX3VjsV8HPVgd2vQaZJr7/FXeMsOZjLi/NahMUj+ b+DrpndZM6pPpaMZ8dnJc467JtsJAYFoacSuBkvt9fum6xwO1pLQsZIUbgkvBx7TFRla pemi09+GcG4UbZooTV74Hil3EKVVoKUCzujqpvfNgSDblDbY6vTSyo7JNBgF4nB7UN7H tlFwG+JpkfoYMqdx2VleFkdoabxTvqlUq3yK3eOYnT3Jyxoa9No1BSabbs1HpDdayrD4 Tm9A== 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 zn8si4667580ejb.70.2020.08.13.20.04.00; Thu, 13 Aug 2020 20:04:33 -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 S1726640AbgHNDCz (ORCPT + 99 others); Thu, 13 Aug 2020 23:02:55 -0400 Received: from mga07.intel.com ([134.134.136.100]:48346 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726583AbgHNDCz (ORCPT ); Thu, 13 Aug 2020 23:02:55 -0400 IronPort-SDR: slOS+5gjyNI3HbwVa7/OZUoRtPG9gzP8GDWHHBsd6D6woiLKF6SniKKEb2odvcyXKJM0P+0XFK 9E1r4/eQ2JKw== X-IronPort-AV: E=McAfee;i="6000,8403,9712"; a="218683649" X-IronPort-AV: E=Sophos;i="5.76,310,1592895600"; d="scan'208";a="218683649" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Aug 2020 20:02:54 -0700 IronPort-SDR: wEhkRU/DQPC9pX4DpyoPjXFa9SctLxpLCacV6p3VqTHLmP/IYd6gRRjH/Yt4wk5gy8JlMIq6OU TJPk0Stmdx9A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.76,310,1592895600"; d="scan'208";a="399357936" Received: from ipu5-build.bj.intel.com ([10.238.232.196]) by fmsmga001.fm.intel.com with ESMTP; 13 Aug 2020 20:02:52 -0700 From: Bingbu Cao To: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, helgaas@kernel.org Cc: sakari.ailus@linux.intel.com, tian.shu.qiu@intel.com, bingbu.cao@intel.com, bingbu.cao@linux.intel.com Subject: [PATCH v3] nvmem: core: add sanity check in nvmem_device_read() Date: Fri, 14 Aug 2020 11:01:46 +0800 Message-Id: <1597374106-15974-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/write could be called directly once nvmem device registered, the sanity check should be done before each nvmem_reg_read/write(). Signed-off-by: Bingbu Cao --- drivers/nvmem/core.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 927eb5f6003f..09ad5a06efee 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -69,6 +69,9 @@ static BLOCKING_NOTIFIER_HEAD(nvmem_notifier); static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset, void *val, size_t bytes) { + if (bytes + offset > nvmem->size) + return -EINVAL; + if (nvmem->reg_read) return nvmem->reg_read(nvmem->priv, offset, val, bytes); @@ -80,6 +83,9 @@ static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset, { int ret; + if (bytes + offset > nvmem->size) + return -EINVAL; + if (nvmem->reg_write) { gpiod_set_value_cansleep(nvmem->wp_gpio, 0); ret = nvmem->reg_write(nvmem->priv, offset, val, bytes); -- 2.7.4