Skip to contentSkip to conversation

devfreq: add error check for sscanf in userspace governor

PATCH New Santosh Mardi 5 participants 6 messages Aug 7, 2017 – Aug 8, 2017 Device frequency (devfreq)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 devfreq: add error check for sscanf in userspace governor

What this patch does:

Current implementation of store_freq function in devfreq userspace governor executes further, even if error is returned from sscanf. This will result in setting up wrong frequency value.

This patch adds proper error check to bail out if any error is returned.

Santosh Mardi (1):
devfreq: add error check for sscanf in userspace governor

 drivers/devfreq/governor_userspace.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Signature · 1 line
1.9.1
#1 lore raw

PATCH 1/1 devfreq: add error check for sscanf in userspace governor

store_freq function of devfreq userspace governor executes further, even if error is returned from sscanf, this will result in setting up wrong frequency value.

Add proper error check to bail out if any error is returned.

---
 drivers/devfreq/governor_userspace.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/devfreq/governor_userspace.c +4 -1
index 77028c2..1d0c9cc 100644
@@ -53,12 +53,15 @@ static ssize_t store_freq(struct device *dev, struct device_attribute *attr,
 	mutex_lock(&devfreq->lock);
 	data = devfreq->data;
 
-	sscanf(buf, "%lu", &wanted);
+	err = sscanf(buf, "%lu", &wanted);
+	if (err != 1)
+		goto out;
 	data->user_frequency = wanted;
 	data->valid = true;
 	err = update_devfreq(devfreq);
 	if (err == 0)
 		err = count;
+out:
 	mutex_unlock(&devfreq->lock);
 	return err;
 }
Signature · 1 line
1.9.1
maintainer Acked-by on v1 1/1
#2 lore raw
Quoted 7 lines

store_freq function of devfreq userspace governor executes further, even if error is returned from sscanf, this will result in setting up wrong frequency value.

Add proper error check to bail out if any error is returned.

Quoted 3 lines
---
 drivers/devfreq/governor_userspace.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Santosh Mardi

Hi,

Quoted 23 lines · Santosh Mardi

store_freq function of devfreq userspace governor executes further, even if error is returned from sscanf, this will result in setting up wrong frequency value.

Add proper error check to bail out if any error is returned.

---
 drivers/devfreq/governor_userspace.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/devfreq/governor_userspace.c +2 -1
index 77028c2..1d0c9cc 100644
@@ -53,12 +53,15 @@ static ssize_t store_freq(struct device *dev, struct device_attribute *attr,
 	mutex_lock(&devfreq->lock);
 	data = devfreq->data;
 
-	sscanf(buf, "%lu", &wanted);
+	err = sscanf(buf, "%lu", &wanted);
+	if (err != 1)

drivers/devfreq/governor_userspace.c static ssize_t store_freq(struct device *dev, struct device_attribute *attr, · line 67

Quoted 10 lines
drivers/devfreq/governor_userspace.c +2 -0
+		goto out;
 	data->user_frequency = wanted;
 	data->valid = true;
 	err = update_devfreq(devfreq);
 	if (err == 0)
 		err = count;
+out:
 	mutex_unlock(&devfreq->lock);
 	return err;
 }

Looks good to me.

Signature · 3 lines
Best Regards,
Chanwoo Choi
Samsung Electronics
#4 lore raw
In reply to Santosh Mardi

Hi Santosh,

Quoted 16 lines · Santosh Mardi

store_freq function of devfreq userspace governor executes further, even if error is returned from sscanf, this will result in setting up wrong frequency value.

Add proper error check to bail out if any error is returned.

---
 drivers/devfreq/governor_userspace.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/devfreq/governor_userspace.c +0 -0
index 77028c2..1d0c9cc 100644

drivers/devfreq/governor_userspace.c static ssize_t store_freq(struct device *dev, struct device_attribute *attr, · line 58

Quoted 7 lines
drivers/devfreq/governor_userspace.c +3 -1
@@ -53,12 +53,15 @@ static ssize_t store_freq(struct device *dev, struct device_attribute *attr,
        mutex_lock(&devfreq->lock);
        data = devfreq->data;
 
-       sscanf(buf, "%lu", &wanted);
+       err = sscanf(buf, "%lu", &wanted);
+       if (err != 1)
+               goto out;

You can save this goto statement by moving this sscanf checking to before taking the mutex.

Quoted 12 lines
drivers/devfreq/governor_userspace.c +1 -1
        data->user_frequency = wanted;
        data->valid = true;
        err = update_devfreq(devfreq);
        if (err == 0)
                err = count;
+out:
        mutex_unlock(&devfreq->lock);
        return err;
 }
--

1.9.1

Signature · 3 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a
Linux Foundation Collaborative Project
#5 lore raw
Quoted 22 lines · Pavan Kondeti

Hi Santosh,

Quoted 19 lines · Santosh Mardi

store_freq function of devfreq userspace governor executes further, even if error is returned from sscanf, this will result in setting up wrong frequency value.

Add proper error check to bail out if any error is returned.

---
  drivers/devfreq/governor_userspace.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
drivers/devfreq/governor_userspace.c +0 -0
index 77028c2..1d0c9cc 100644
@@ -53,12 +53,15 @@ static ssize_t store_freq(struct device *dev, struct device_attribute *attr,
         mutex_lock(&devfreq->lock);
         data = devfreq->data;
 
Quoted 2 lines

drivers/devfreq/governor_userspace.c static ssize_t store_freq(struct device *dev, struct device_attribute *attr, · line 56

Quoted 2 lines
drivers/devfreq/governor_userspace.c +1 -1
-       sscanf(buf, "%lu", &wanted);
+       err = sscanf(buf, "%lu", &wanted);

Also, we could just use kstroul().

Quoted 18 lines

drivers/devfreq/governor_userspace.c static ssize_t store_freq(struct device *dev, struct device_attribute *attr, · line 58

Quoted 2 lines
drivers/devfreq/governor_userspace.c +2 -0
+       if (err != 1)
+               goto out;

You can save this goto statement by moving this sscanf checking to before taking the mutex.

Quoted 12 lines
drivers/devfreq/governor_userspace.c +1 -1
         data->user_frequency = wanted;
         data->valid = true;
         err = update_devfreq(devfreq);
         if (err == 0)
                 err = count;
+out:
         mutex_unlock(&devfreq->lock);
         return err;
  }
--

1.9.1

-Saravana

Signature · 3 lines
Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project