Skip to contentSkip to conversation

cpufreq: conservative: Replace sscanf() with kstrtouint()

PATCH Applied Kaushlendra Kumar 4 participants 5 messages Sep 6, 2025 – Sep 10, 2025 CPU frequency scaling framework

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpufreq: conservative: Replace sscanf() with kstrtouint()

Replace sscanf() with kstrtouint() in all sysfs store functions to improve input validation and security. The kstrtouint() function provides better error detection, overflow protection, and consistent error handling compared to sscanf().

The kstrtouint() function provides:
- Robust error detection for invalid input strings
- Built-in overflow protection and boundary checking
- Consistent error reporting (0 for success, negative for failure)

This maintains existing functionality while improving input validation robustness and following kernel coding best practices for string parsing.

---
 drivers/cpufreq/cpufreq_conservative.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
drivers/cpufreq/cpufreq_conservative.c +12 -12
index 56500b25d77c..cce6a8d113e1 100644
@@ -152,9 +152,9 @@ static ssize_t sampling_down_factor_store(struct gov_attr_set *attr_set,
 	struct dbs_data *dbs_data = to_dbs_data(attr_set);
 	unsigned int input;
 	int ret;
-	ret = sscanf(buf, "%u", &input);
+	ret = kstrtouint(buf, 0, &input);
 
-	if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
+	if (ret || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
 		return -EINVAL;
 
 	dbs_data->sampling_down_factor = input;
@@ -168,9 +168,9 @@ static ssize_t up_threshold_store(struct gov_attr_set *attr_set,
 	struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
 	unsigned int input;
 	int ret;
-	ret = sscanf(buf, "%u", &input);
+	ret = kstrtouint(buf, 0, &input);
 
-	if (ret != 1 || input > 100 || input <= cs_tuners->down_threshold)
+	if (ret || input > 100 || input <= cs_tuners->down_threshold)
 		return -EINVAL;
 
 	dbs_data->up_threshold = input;
@@ -184,10 +184,10 @@ static ssize_t down_threshold_store(struct gov_attr_set *attr_set,
 	struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
 	unsigned int input;
 	int ret;
-	ret = sscanf(buf, "%u", &input);
+	ret = kstrtouint(buf, 0, &input);
 
 	/* cannot be lower than 1 otherwise freq will not fall */
-	if (ret != 1 || input < 1 || input >= dbs_data->up_threshold)
+	if (ret || input < 1 || input >= dbs_data->up_threshold)
 		return -EINVAL;
 
 	cs_tuners->down_threshold = input;
@@ -201,9 +201,9 @@ static ssize_t ignore_nice_load_store(struct gov_attr_set *attr_set,
 	unsigned int input;
 	int ret;
 
-	ret = sscanf(buf, "%u", &input);
-	if (ret != 1)
-		return -EINVAL;
+	ret = kstrtouint(buf, 0, &input);
+	if (ret)
+		return ret;
 
 	if (input > 1)
 		input = 1;
@@ -226,10 +226,10 @@ static ssize_t freq_step_store(struct gov_attr_set *attr_set, const char *buf,
 	struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
 	unsigned int input;
 	int ret;
-	ret = sscanf(buf, "%u", &input);
+	ret = kstrtouint(buf, 0, &input);
 
-	if (ret != 1)
-		return -EINVAL;
+	if (ret)
+		return ret;
 
 	if (input > 100)
 		input = 100;
Signature · 1 line
2.34.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 76 lines · Kaushlendra Kumar

Replace sscanf() with kstrtouint() in all sysfs store functions to improve input validation and security. The kstrtouint() function provides better error detection, overflow protection, and consistent error handling compared to sscanf().

The kstrtouint() function provides:
- Robust error detection for invalid input strings
- Built-in overflow protection and boundary checking
- Consistent error reporting (0 for success, negative for failure)

This maintains existing functionality while improving input validation robustness and following kernel coding best practices for string parsing.

---
 drivers/cpufreq/cpufreq_conservative.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
drivers/cpufreq/cpufreq_conservative.c +9 -9
index 56500b25d77c..cce6a8d113e1 100644
@@ -152,9 +152,9 @@ static ssize_t sampling_down_factor_store(struct gov_attr_set *attr_set,
 	struct dbs_data *dbs_data = to_dbs_data(attr_set);
 	unsigned int input;
 	int ret;
-	ret = sscanf(buf, "%u", &input);
+	ret = kstrtouint(buf, 0, &input);
 
-	if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
+	if (ret || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
 		return -EINVAL;
 
 	dbs_data->sampling_down_factor = input;
@@ -168,9 +168,9 @@ static ssize_t up_threshold_store(struct gov_attr_set *attr_set,
 	struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
 	unsigned int input;
 	int ret;
-	ret = sscanf(buf, "%u", &input);
+	ret = kstrtouint(buf, 0, &input);
 
-	if (ret != 1 || input > 100 || input <= cs_tuners->down_threshold)
+	if (ret || input > 100 || input <= cs_tuners->down_threshold)
 		return -EINVAL;
 
 	dbs_data->up_threshold = input;
@@ -184,10 +184,10 @@ static ssize_t down_threshold_store(struct gov_attr_set *attr_set,
 	struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
 	unsigned int input;
 	int ret;
-	ret = sscanf(buf, "%u", &input);
+	ret = kstrtouint(buf, 0, &input);
 
 	/* cannot be lower than 1 otherwise freq will not fall */
-	if (ret != 1 || input < 1 || input >= dbs_data->up_threshold)
+	if (ret || input < 1 || input >= dbs_data->up_threshold)
 		return -EINVAL;
 
 	cs_tuners->down_threshold = input;
@@ -201,9 +201,9 @@ static ssize_t ignore_nice_load_store(struct gov_attr_set *attr_set,
 	unsigned int input;
 	int ret;
 
-	ret = sscanf(buf, "%u", &input);
-	if (ret != 1)
-		return -EINVAL;
+	ret = kstrtouint(buf, 0, &input);
+	if (ret)
+		return ret;
 
 	if (input > 1)
 		input = 1;
@@ -226,10 +226,10 @@ static ssize_t freq_step_store(struct gov_attr_set *attr_set, const char *buf,
 	struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
 	unsigned int input;
 	int ret;

drivers/cpufreq/cpufreq_conservative.c static ssize_t freq_step_store(struct gov_attr_set *attr_set, const char *buf, · line 235

Quoted 10 lines
drivers/cpufreq/cpufreq_conservative.c +3 -3
-	ret = sscanf(buf, "%u", &input);
+	ret = kstrtouint(buf, 0, &input);
 
-	if (ret != 1)
-		return -EINVAL;
+	if (ret)
+		return ret;
 
 	if (input > 100)
 		input = 100;
Signature · 1 line
viresh
maintainer Applied
#2 lore raw
Quoted 86 lines · Viresh Kumar
Quoted 84 lines · Kaushlendra Kumar

Replace sscanf() with kstrtouint() in all sysfs store functions to improve input validation and security. The kstrtouint() function provides better error detection, overflow protection, and consistent error handling compared to sscanf().

The kstrtouint() function provides:
- Robust error detection for invalid input strings
- Built-in overflow protection and boundary checking
- Consistent error reporting (0 for success, negative for failure)

This maintains existing functionality while improving input validation robustness and following kernel coding best practices for string parsing.

---
 drivers/cpufreq/cpufreq_conservative.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
drivers/cpufreq/cpufreq_conservative.c +12 -12
index 56500b25d77c..cce6a8d113e1 100644
@@ -152,9 +152,9 @@ static ssize_t sampling_down_factor_store(struct gov_attr_set *attr_set,
      struct dbs_data *dbs_data = to_dbs_data(attr_set);
      unsigned int input;
      int ret;
-     ret = sscanf(buf, "%u", &input);
+     ret = kstrtouint(buf, 0, &input);
 
-     if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
+     if (ret || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
              return -EINVAL;
 
      dbs_data->sampling_down_factor = input;
@@ -168,9 +168,9 @@ static ssize_t up_threshold_store(struct gov_attr_set *attr_set,
      struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
      unsigned int input;
      int ret;
-     ret = sscanf(buf, "%u", &input);
+     ret = kstrtouint(buf, 0, &input);
 
-     if (ret != 1 || input > 100 || input <= cs_tuners->down_threshold)
+     if (ret || input > 100 || input <= cs_tuners->down_threshold)
              return -EINVAL;
 
      dbs_data->up_threshold = input;
@@ -184,10 +184,10 @@ static ssize_t down_threshold_store(struct gov_attr_set *attr_set,
      struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
      unsigned int input;
      int ret;
-     ret = sscanf(buf, "%u", &input);
+     ret = kstrtouint(buf, 0, &input);
 
      /* cannot be lower than 1 otherwise freq will not fall */
-     if (ret != 1 || input < 1 || input >= dbs_data->up_threshold)
+     if (ret || input < 1 || input >= dbs_data->up_threshold)
              return -EINVAL;
 
      cs_tuners->down_threshold = input;
@@ -201,9 +201,9 @@ static ssize_t ignore_nice_load_store(struct gov_attr_set *attr_set,
      unsigned int input;
      int ret;
 
-     ret = sscanf(buf, "%u", &input);
-     if (ret != 1)
-             return -EINVAL;
+     ret = kstrtouint(buf, 0, &input);
+     if (ret)
+             return ret;
 
      if (input > 1)
              input = 1;
@@ -226,10 +226,10 @@ static ssize_t freq_step_store(struct gov_attr_set *attr_set, const char *buf,
      struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
      unsigned int input;
      int ret;
-     ret = sscanf(buf, "%u", &input);
+     ret = kstrtouint(buf, 0, &input);
 
-     if (ret != 1)
-             return -EINVAL;
+     if (ret)
+             return ret;
 
Quoted 4 lines

drivers/cpufreq/cpufreq_conservative.c static ssize_t freq_step_store(struct gov_attr_set *attr_set, const char *buf, · line 235

Quoted 2 lines
drivers/cpufreq/cpufreq_conservative.c +0 -0
      if (input > 100)
              input = 100;

Applied as 6.18 material, thanks!

#3 lore raw
In reply to Kaushlendra Kumar

Hi,

Quoted 5 lines · Kaushlendra Kumar

Replace sscanf() with kstrtouint() in all sysfs store functions to improve input validation and security. The kstrtouint() function provides better error detection, overflow protection, and consistent error handling compared to sscanf().

Quoted 7 lines

The kstrtouint() function provides:
- Robust error detection for invalid input strings
- Built-in overflow protection and boundary checking
- Consistent error reporting (0 for success, negative for failure)

This maintains existing functionality while improving input validation robustness and following kernel coding best practices for string parsing.

looking at the patch while it is a good thing, something struck me ...

Quoted 10 lines
---
  drivers/cpufreq/cpufreq_conservative.c | 24 ++++++++++++------------
  1 file changed, 12 insertions(+), 12 deletions(-)
drivers/cpufreq/cpufreq_conservative.c +0 -0
index 56500b25d77c..cce6a8d113e1 100644

drivers/cpufreq/cpufreq_conservative.c static ssize_t sampling_down_factor_store(struct gov_attr_set *attr_set, · line 158

Quoted 9 lines
drivers/cpufreq/cpufreq_conservative.c +2 -2
@@ -152,9 +152,9 @@ static ssize_t sampling_down_factor_store(struct gov_attr_set *attr_set,
  	struct dbs_data *dbs_data = to_dbs_data(attr_set);
  	unsigned int input;
  	int ret;
-	ret = sscanf(buf, "%u", &input);
+	ret = kstrtouint(buf, 0, &input);
 
-	if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
+	if (ret || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  		return -EINVAL;

... the parsing itself, followed by a check for bounds ...

drivers/cpufreq/cpufreq_conservative.c static ssize_t up_threshold_store(struct gov_attr_set *attr_set, · line 174

Quoted 11 lines
drivers/cpufreq/cpufreq_conservative.c +2 -2
  	dbs_data->sampling_down_factor = input;
@@ -168,9 +168,9 @@ static ssize_t up_threshold_store(struct gov_attr_set *attr_set,
  	struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  	unsigned int input;
  	int ret;
-	ret = sscanf(buf, "%u", &input);
+	ret = kstrtouint(buf, 0, &input);
 
-	if (ret != 1 || input > 100 || input <= cs_tuners->down_threshold)
+	if (ret || input > 100 || input <= cs_tuners->down_threshold)
  		return -EINVAL;

... and here again. It seems to me that if you always follow the parsing with a check for bounds, then to reduce code duplication you really want a function that takes an upper and a lower bound as parameters and checks against them.
In that sense, I am afraid I have to say that your patch stops in the middle of the journey.

Regards
Oliver

#4 lore raw
Quoted 47 lines

Hi,

Quoted 5 lines · Kaushlendra Kumar

Replace sscanf() with kstrtouint() in all sysfs store functions to improve input validation and security. The kstrtouint() function provides better error detection, overflow protection, and consistent error handling compared to sscanf().

Quoted 7 lines

The kstrtouint() function provides:
- Robust error detection for invalid input strings
- Built-in overflow protection and boundary checking
- Consistent error reporting (0 for success, negative for failure)

This maintains existing functionality while improving input validation robustness and following kernel coding best practices for string parsing.

looking at the patch while it is a good thing, something struck me ...

Quoted 11 lines
---
  drivers/cpufreq/cpufreq_conservative.c | 24 ++++++++++++------------
  1 file changed, 12 insertions(+), 12 deletions(-)
drivers/cpufreq/cpufreq_conservative.c +0 -0
index 56500b25d77c..cce6a8d113e1 100644

drivers/cpufreq/cpufreq_conservative.c static ssize_t sampling_down_factor_store(struct gov_attr_set *attr_set, · line 158

Quoted 9 lines
drivers/cpufreq/cpufreq_conservative.c +2 -2
@@ -152,9 +152,9 @@ static ssize_t sampling_down_factor_store(struct gov_attr_set *attr_set,
  	struct dbs_data *dbs_data = to_dbs_data(attr_set);
  	unsigned int input;
  	int ret;
-	ret = sscanf(buf, "%u", &input);
+	ret = kstrtouint(buf, 0, &input);
 
-	if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
+	if (ret || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  		return -EINVAL;

... the parsing itself, followed by a check for bounds ...

Quoted 8 lines
drivers/cpufreq/cpufreq_conservative.c +0 -0
  	dbs_data->sampling_down_factor = input; @@ -168,9 +168,9 @@ static 
 ssize_t up_threshold_store(struct gov_attr_set *attr_set,
  	struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
  	unsigned int input;
  	int ret;
drivers/cpufreq/cpufreq_conservative.c +1 -1
-	ret = sscanf(buf, "%u", &input);
+	ret = kstrtouint(buf, 0, &input);
Quoted 6 lines

drivers/cpufreq/cpufreq_conservative.c static ssize_t up_threshold_store(struct gov_attr_set *attr_set, · line 174

Quoted 3 lines
drivers/cpufreq/cpufreq_conservative.c +1 -1
-	if (ret != 1 || input > 100 || input <= cs_tuners->down_threshold)
+	if (ret || input > 100 || input <= cs_tuners->down_threshold)
  		return -EINVAL;

... and here again. It seems to me that if you always follow the parsing with a check for bounds, then to reduce code duplication you really want a function that takes an upper and a lower bound as parameters and checks against them. In that sense, I am afraid I have to say that your patch stops in the middle of the journey.

Thank you for the feedback! You made a valid point.

This current patch focuses specifically on replacing sscanf() with kstrtouint() for the security and robustness benefits, but I agree that the bounds checking pattern deserves its own optimization.

I'll prepare a separate follow-up patch that introduces a helper function to eliminate the code duplication you've identified. This would make the code much cleaner and more maintainable.

Can I prepare a separate patch
where this sscanf replacement goes first, followed by the bounds checking consolidation?

Best regards,
Kaushlendra Kumar