public inbox for linux-pm@vger.kernel.org
 help / color / mirror / Atom feed
From: Shen Kai <shenkai8@huawei.com>
To: <viresh.kumar@linaro.org>
Cc: <hushiyuan@huawei.com>, <linfeilong@huawei.com>,
	<rafael.j.wysocki@intel.com>, <linux-pm@vger.kernel.org>,
	<rjw@rjwysocki.net>
Subject: [PATCH v3]cpufreq: add NULL check to the store method of cpufreq
Date: Thu, 7 Nov 2019 05:08:17 +0000	[thread overview]
Message-ID: <1573103297-7252-1-git-send-email-shenkai8@huawei.com> (raw)
In-Reply-To: <20191107030601.xqe2tpcprkg7epul@vireshk-i7>

From: Kai Shen <shenkai8@huawei.com>

Add NULL check in the store function here to avoid NULL callback invoking.
Though some interfaces of cpufreq are set as read-only, user can still get 
write permission using chmod which can lead to a kernel crash.

The following operations can lead to a kernel crash.

chmod +w /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
echo 1 >  /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq

This bug was found on linux 4.19

Signed-off-by: Kai Shen <shenkai8@huawei.com>
Reported-by: Feilong Lin <linfeilong@huawei.com>
Reviewed-by: Feilong Lin <linfeilong@huawei.com>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
---
V2->V3
- use return value -EIO instead.
- do NULL check in show method too.

 drivers/cpufreq/cpufreq.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index 48a224a..bc19d6c 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -933,6 +933,9 @@ static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
 	struct freq_attr *fattr = to_attr(attr);
 	ssize_t ret;
 
+	if (!fattr->show)
+		return -EIO;
+
 	down_read(&policy->rwsem);
 	ret = fattr->show(policy, buf);
 	up_read(&policy->rwsem);
@@ -947,6 +950,9 @@ static ssize_t store(struct kobject *kobj, struct attribute *attr,
 	struct freq_attr *fattr = to_attr(attr);
 	ssize_t ret = -EINVAL;
 
+	if (!fattr->store)
+		return -EIO;
+
 	/*
 	 * cpus_read_trylock() is used here to work around a circular lock
 	 * dependency problem with respect to the cpufreq_register_driver().
-- 
2.6.4.windows.1



  reply	other threads:[~2019-11-07  5:08 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-06  7:46 [PATCH]cpufreq: add NULL check to the store method of cpufreq Shen Kai
2019-11-06 12:32 ` Rafael J. Wysocki
2019-11-07  1:53   ` Shen Kai
2019-11-07  2:32     ` Viresh Kumar
2019-11-07  2:34       ` Viresh Kumar
2019-11-07  2:53         ` Shen Kai
2019-11-07  3:06           ` Viresh Kumar
2019-11-07  5:08             ` Shen Kai [this message]
2019-11-07  5:48               ` [PATCH v3]cpufreq: " Viresh Kumar
2019-11-08 11:11               ` Rafael J. Wysocki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1573103297-7252-1-git-send-email-shenkai8@huawei.com \
    --to=shenkai8@huawei.com \
    --cc=hushiyuan@huawei.com \
    --cc=linfeilong@huawei.com \
    --cc=linux-pm@vger.kernel.org \
    --cc=rafael.j.wysocki@intel.com \
    --cc=rjw@rjwysocki.net \
    --cc=viresh.kumar@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox