Skip to content

Commit cf96ab1

Browse files
Ahmed S. DarwishKAGA-KOKO
authored andcommitted
tools/x86/kcpuid: Protect against faulty "max subleaf" values
Protect against the kcpuid code parsing faulty max subleaf numbers through a min() expression. Thus, ensuring that max_subleaf will always be ≤ MAX_SUBLEAF_NUM. Use "u32" for the subleaf numbers since kcpuid is compiled with -Wextra, which includes signed/unsigned comparisons warnings. Signed-off-by: Ahmed S. Darwish <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Link: https://lore.kernel.org/all/[email protected]
1 parent 5dd7ca4 commit cf96ab1

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

tools/arch/x86/kcpuid/kcpuid.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
#include <string.h>
88
#include <getopt.h>
99

10-
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
10+
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
11+
#define min(a, b) (((a) < (b)) ? (a) : (b))
1112

1213
typedef unsigned int u32;
1314
typedef unsigned long long u64;
@@ -206,12 +207,9 @@ static void raw_dump_range(struct cpuid_range *range)
206207
#define MAX_SUBLEAF_NUM 64
207208
struct cpuid_range *setup_cpuid_range(u32 input_eax)
208209
{
209-
u32 max_func, idx_func;
210-
int subleaf;
210+
u32 max_func, idx_func, subleaf, max_subleaf;
211+
u32 eax, ebx, ecx, edx, f = input_eax;
211212
struct cpuid_range *range;
212-
u32 eax, ebx, ecx, edx;
213-
u32 f = input_eax;
214-
int max_subleaf;
215213
bool allzero;
216214

217215
eax = input_eax;
@@ -256,7 +254,7 @@ struct cpuid_range *setup_cpuid_range(u32 input_eax)
256254
* others have to be tried (0xf)
257255
*/
258256
if (f == 0x7 || f == 0x14 || f == 0x17 || f == 0x18)
259-
max_subleaf = (eax & 0xff) + 1;
257+
max_subleaf = min((eax & 0xff) + 1, max_subleaf);
260258

261259
if (f == 0xb)
262260
max_subleaf = 2;

0 commit comments

Comments
 (0)