Skip to content

Commit 5590db4

Browse files
kaushlenrafaeljw
authored andcommitted
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(). This maintains existing functionality while improving input validation robustness and following kernel coding best practices for string parsing. Signed-off-by: Kaushlendra Kumar <kaushlendra.kumar@intel.com> Acked-by: Viresh Kumar <viresh.kumar@linaro.org> Link: https://patch.msgid.link/20250906115316.3010384-1-kaushlendra.kumar@intel.com [ rjw: Dropped duplicate paragraph from the changelog ] Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent ae1bdd2 commit 5590db4

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

drivers/cpufreq/cpufreq_conservative.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ static ssize_t sampling_down_factor_store(struct gov_attr_set *attr_set,
152152
struct dbs_data *dbs_data = to_dbs_data(attr_set);
153153
unsigned int input;
154154
int ret;
155-
ret = sscanf(buf, "%u", &input);
155+
ret = kstrtouint(buf, 0, &input);
156156

157-
if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
157+
if (ret || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
158158
return -EINVAL;
159159

160160
dbs_data->sampling_down_factor = input;
@@ -168,9 +168,9 @@ static ssize_t up_threshold_store(struct gov_attr_set *attr_set,
168168
struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
169169
unsigned int input;
170170
int ret;
171-
ret = sscanf(buf, "%u", &input);
171+
ret = kstrtouint(buf, 0, &input);
172172

173-
if (ret != 1 || input > 100 || input <= cs_tuners->down_threshold)
173+
if (ret || input > 100 || input <= cs_tuners->down_threshold)
174174
return -EINVAL;
175175

176176
dbs_data->up_threshold = input;
@@ -184,10 +184,10 @@ static ssize_t down_threshold_store(struct gov_attr_set *attr_set,
184184
struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
185185
unsigned int input;
186186
int ret;
187-
ret = sscanf(buf, "%u", &input);
187+
ret = kstrtouint(buf, 0, &input);
188188

189189
/* cannot be lower than 1 otherwise freq will not fall */
190-
if (ret != 1 || input < 1 || input >= dbs_data->up_threshold)
190+
if (ret || input < 1 || input >= dbs_data->up_threshold)
191191
return -EINVAL;
192192

193193
cs_tuners->down_threshold = input;
@@ -201,9 +201,9 @@ static ssize_t ignore_nice_load_store(struct gov_attr_set *attr_set,
201201
unsigned int input;
202202
int ret;
203203

204-
ret = sscanf(buf, "%u", &input);
205-
if (ret != 1)
206-
return -EINVAL;
204+
ret = kstrtouint(buf, 0, &input);
205+
if (ret)
206+
return ret;
207207

208208
if (input > 1)
209209
input = 1;
@@ -226,10 +226,10 @@ static ssize_t freq_step_store(struct gov_attr_set *attr_set, const char *buf,
226226
struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
227227
unsigned int input;
228228
int ret;
229-
ret = sscanf(buf, "%u", &input);
229+
ret = kstrtouint(buf, 0, &input);
230230

231-
if (ret != 1)
232-
return -EINVAL;
231+
if (ret)
232+
return ret;
233233

234234
if (input > 100)
235235
input = 100;

0 commit comments

Comments
 (0)