From: Len Brown <lenb@kernel.org>
To: linux-pm@vger.kernel.org
Cc: Andy Shevchenko <andriy.shevchenko@intel.com>,
Len Brown <len.brown@intel.com>
Subject: [PATCH 04/18] x86_energy_perf_policy: Cleanup: Harden with strtol()
Date: Mon, 19 Oct 2026 12:43:25 -0400 [thread overview]
Message-ID: <4a9e2fb9f00e2ead8dc9af9e285c19eb21b5f9a4.1792428130.git.len.brown@intel.com> (raw)
In-Reply-To: <e160a0ae497702e1c32ab163faa63c0043ea9298.1792428130.git.len.brown@intel.com>
From: Len Brown <len.brown@intel.com>
Replace atoi() with strtol() and proper error checking when parsing the
--hwp-use-pkg command-line argument value, and other spots.
Also add explicit check that value is in valid range [0, 1].
Assisted-by: GitHub-Copilot:Claude-Sonnet-4.6
Signed-off-by: Len Brown <len.brown@intel.com>
---
.../x86_energy_perf_policy.c | 46 +++++++++++--------
1 file changed, 26 insertions(+), 20 deletions(-)
diff --git a/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c b/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
index 3c4e376f6c77..98e5aa9aec51 100644
--- a/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
+++ b/tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
@@ -191,7 +191,7 @@ int parse_cmdline_epb(int i)
return ENERGY_PERF_BIAS_PERFORMANCE;
}
if (i < 0 || i > ENERGY_PERF_BIAS_POWERSAVE)
- errx(1, "--epb must be from 0 to 15");
+ errx(1, "--epb must be 0 through 15");
return i;
}
@@ -305,8 +305,8 @@ int parse_cmdline_hwp_epp(int i)
case OPTARG_PERFORMANCE:
return HWP_EPP_PERFORMANCE;
}
- if (i < 0 || i > 0xff) {
- fprintf(stderr, "--hwp-epp must be from 0 to 0xff\n");
+ if (i < 0 || i > 255) {
+ fprintf(stderr, "--hwp-epp must be 0 through 255\n");
usage();
}
return i;
@@ -355,19 +355,13 @@ int parse_optarg_string(char *s)
if (!strncmp(s, "performance", 11))
return OPTARG_PERFORMANCE;
+ errno = 0;
i = strtol(s, &endptr, 0);
- if (s == endptr) {
- fprintf(stderr, "no digits in \"%s\"\n", s);
+ if (endptr == s || errno == ERANGE || i < 0 || i > 256) {
+ warnx("bad value \"%s\"\n", s);
usage();
}
- if (i == LONG_MIN || i == LONG_MAX)
- errx(-1, "%s", s);
- if (i > 0xFF)
- errx(-1, "%d (0x%x) must be < 256", i, i);
-
- if (i < 0)
- errx(-1, "%d (0x%x) must be >= 0", i, i);
return i;
}
@@ -425,7 +419,7 @@ void parse_cmdline_cpu(char *s)
startp++;
end_cpu = strtol(startp, &endp, 10);
- if (startp == endp)
+ if (endp == startp)
continue;
while (cpu <= end_cpu) {
@@ -469,11 +463,11 @@ void parse_cmdline_cpu(char *s)
break;
}
+ errno = 0;
cpu = strtol(startp, &endp, 10);
- if (startp == endp)
+ if (endp == startp || errno == ERANGE || cpu < 0 || cpu > max_cpu_num)
errx(1, "--cpu cpu-set: confused by '%s'", startp);
- if (cpu > max_cpu_num)
- errx(1, "Requested cpu%d exceeds max cpu%d", cpu, max_cpu_num);
+
CPU_SET_S(cpu, cpu_setsize, cpu_selected_set);
startp = endp;
}
@@ -505,7 +499,7 @@ void parse_cmdline_pkg(char *s)
startp++;
end_pkg = strtol(startp, &endp, 10);
- if (startp == endp)
+ if (endp == startp)
continue;
while (pkg <= end_pkg) {
@@ -526,6 +520,7 @@ void parse_cmdline_pkg(char *s)
pkg = strtol(startp, &endp, 10);
if (pkg > max_pkg_num)
errx(1, "Requested pkg%d Exceeds max pkg%d", pkg, max_pkg_num);
+
pkg_selected_set |= 1 << pkg;
startp = endp;
}
@@ -546,6 +541,7 @@ static int parse_cmdline_int(const char *s, int *out)
char *endp;
long val;
+ errno = 0;
val = strtol(s, &endp, 0);
if (endp == s || errno == ERANGE)
return -1;
@@ -716,7 +712,15 @@ void cmdline(int argc, char **argv)
break;
case 'u':
update_hwp_use_pkg++;
- if (atoi(optarg) == 0)
+ char *endp;
+ long val;
+
+ errno = 0;
+ val = strtol(optarg, &endp, 10);
+ if (endp == optarg || errno == ERANGE || val < 0 || val > 1)
+ errx(1, "--hwp-use-pkg: must be 0 or 1");
+
+ if (val == 0)
req_update.hwp_use_pkg = 0;
else
req_update.hwp_use_pkg = 1;
@@ -1015,8 +1019,9 @@ static int get_epb_sysfs(int cpu)
if (!read_sysfs(path, linebuf, 3))
return -1;
+ errno = 0;
val = strtol(linebuf, &endp, 0);
- if (endp == linebuf || errno == ERANGE)
+ if (endp == linebuf || errno == ERANGE || val < 0 || val > 15)
return -1;
return (int)val;
@@ -1039,8 +1044,9 @@ static int set_epb_sysfs(int cpu, int val)
if (ret <= 0)
return -1;
+ errno = 0;
val = strtol(linebuf, &endp, 0);
- if (endp == linebuf || errno == ERANGE)
+ if (endp == linebuf || errno == ERANGE || val < 0 || val > 15)
return -1;
return (int)val;
--
2.45.2
next prev parent reply other threads:[~2026-10-19 16:52 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-19 16:43 [PATCH 0/18] x86_energy_perf_policy cleanups Len Brown
2026-10-19 16:43 ` [PATCH 01/18] x86_energy_perf_policy: Create msr_path helper Len Brown
2026-10-19 16:43 ` [PATCH 02/18] x86_energy_perf_policy: Enhance MSR probe Len Brown
2026-10-20 9:44 ` Andy Shevchenko
2026-10-19 16:43 ` [PATCH 03/18] x86_energy_perf_policy: Prevent unlikely buffer overflow Len Brown
2026-10-20 9:46 ` Andy Shevchenko
2026-10-19 16:43 ` Len Brown [this message]
2026-10-21 15:56 ` [PATCH 04/18] x86_energy_perf_policy: Cleanup: Harden with strtol() Andy Shevchenko
2026-10-19 16:43 ` [PATCH 05/18] x86_energy_perf_policy: Cleanup: Use snprintf() for platform-profile Len Brown
2026-10-21 15:57 ` Andy Shevchenko
2026-10-19 16:43 ` [PATCH 06/18] x86_energy_perf_policy: Fix optarg keyword matching Len Brown
2026-10-21 15:58 ` Andy Shevchenko
2026-10-19 16:43 ` [PATCH 07/18] x86_energy_perf_policy: Cleanup: Fix potential ratio overflow Len Brown
2026-10-21 16:06 ` Andy Shevchenko
2026-10-19 16:43 ` [PATCH 08/18] x86_energy_perf_policy: Bound package iteration Len Brown
2026-10-21 16:08 ` Andy Shevchenko
2026-10-19 16:43 ` [PATCH 09/18] x86_energy_perf_policy: Cleanup: Define HWP window max Len Brown
2026-10-21 16:10 ` Andy Shevchenko
2026-10-19 16:43 ` [PATCH 10/18] x86_energy_perf_policy: Cleanup: Define BCLK_MHZ Len Brown
2026-10-21 16:16 ` Andy Shevchenko
2026-10-19 16:43 ` [PATCH 11/18] x86_energy_perf_policy: Cleanup: Check fseek() in VM path Len Brown
2026-10-21 16:19 ` Andy Shevchenko
2026-10-19 16:43 ` [PATCH 12/18] x86_energy_perf_policy: Document MSRHEADER Len Brown
2026-10-21 16:31 ` Andy Shevchenko
2026-10-19 16:43 ` [PATCH 13/18] x86_energy_perf_policy: Cleanup: Drop unused signal.h Len Brown
2026-10-21 16:38 ` Andy Shevchenko
2026-10-19 16:43 ` [PATCH 14/18] x86_energy_perf_policy: Cleanup: Constify profile name arg Len Brown
2026-10-21 16:39 ` Andy Shevchenko
2026-10-19 16:43 ` [PATCH 15/18] x86_energy_perf_policy: Cleanup: Constify HWP strings Len Brown
2026-10-21 16:39 ` Andy Shevchenko
2026-10-19 16:43 ` [PATCH 16/18] x86_energy_perf_policy: Fix use-after-free in err_on_hypervisor() Len Brown
2026-10-21 16:42 ` Andy Shevchenko
2026-10-19 16:43 ` [PATCH 17/18] x86_energy_perf_policy: Fix write_sysfs() return type Len Brown
2026-10-21 16:45 ` Andy Shevchenko
2026-10-19 16:43 ` [PATCH 18/18] MAINTAINERS: Add x86_energy_perf_policy Len Brown
2026-10-21 16:46 ` Andy Shevchenko
2026-10-19 18:33 ` [PATCH 01/18] x86_energy_perf_policy: Create msr_path helper Andy Shevchenko
2026-10-20 9:35 ` Andy Shevchenko
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=4a9e2fb9f00e2ead8dc9af9e285c19eb21b5f9a4.1792428130.git.len.brown@intel.com \
--to=lenb@kernel.org \
--cc=andriy.shevchenko@intel.com \
--cc=len.brown@intel.com \
--cc=linux-pm@vger.kernel.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