public inbox for linux-pm@vger.kernel.org
 help / color / mirror / Atom feed
From: Oliver Neukum <oneukum@suse.com>
To: Kaushlendra Kumar <kaushlendra.kumar@intel.com>,
	rafael@kernel.org, viresh.kumar@linaro.org
Cc: linux-pm@vger.kernel.org
Subject: Re: [PATCH] cpufreq: conservative: Replace sscanf() with kstrtouint()
Date: Mon, 8 Sep 2025 10:36:57 +0200	[thread overview]
Message-ID: <93d51f53-8530-46b4-a2f3-d01046d9d583@suse.com> (raw)
In-Reply-To: <20250906115316.3010384-1-kaushlendra.kumar@intel.com>

Hi,

On 9/6/25 13:53, Kaushlendra Kumar wrote:
> 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.

looking at the patch while it is a good thing, something struck me ...
> Signed-off-by: Kaushlendra Kumar <kaushlendra.kumar@intel.com>
> ---
>   drivers/cpufreq/cpufreq_conservative.c | 24 ++++++++++++------------
>   1 file changed, 12 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/cpufreq/cpufreq_conservative.c b/drivers/cpufreq/cpufreq_conservative.c
> index 56500b25d77c..cce6a8d113e1 100644
> --- a/drivers/cpufreq/cpufreq_conservative.c
> +++ b/drivers/cpufreq/cpufreq_conservative.c
> @@ -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 ...   
>   	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


  parent reply	other threads:[~2025-09-08  8:37 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-06 11:53 [PATCH] cpufreq: conservative: Replace sscanf() with kstrtouint() Kaushlendra Kumar
2025-09-08  7:02 ` Viresh Kumar
2025-09-10 10:21   ` Rafael J. Wysocki
2025-09-08  8:36 ` Oliver Neukum [this message]
2025-09-08  9:01   ` Kumar, Kaushlendra

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=93d51f53-8530-46b4-a2f3-d01046d9d583@suse.com \
    --to=oneukum@suse.com \
    --cc=kaushlendra.kumar@intel.com \
    --cc=linux-pm@vger.kernel.org \
    --cc=rafael@kernel.org \
    --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