Skip to content

Commit 2b56484

Browse files
nertpinxbp3tk0v
authored andcommitted
x86/resctrl: Avoid overflow in MB settings in bw_validate()
The resctrl schemata file supports specifying memory bandwidth associated with the Memory Bandwidth Allocation (MBA) feature via a percentage (this is the default) or bandwidth in MiBps (when resctrl is mounted with the "mba_MBps" option). The allowed range for the bandwidth percentage is from /sys/fs/resctrl/info/MB/min_bandwidth to 100, using a granularity of /sys/fs/resctrl/info/MB/bandwidth_gran. The supported range for the MiBps bandwidth is 0 to U32_MAX. There are two issues with parsing of MiBps memory bandwidth: * The user provided MiBps is mistakenly rounded up to the granularity that is unique to percentage input. * The user provided MiBps is parsed using unsigned long (thus accepting values up to ULONG_MAX), and then assigned to u32 that could result in overflow. Do not round up the MiBps value and parse user provided bandwidth as the u32 it is intended to be. Use the appropriate kstrtou32() that can detect out of range values. Fixes: 8205a07 ("x86/intel_rdt/mba_sc: Add schemata support") Fixes: 6ce1560 ("x86/resctrl: Switch over to the resctrl mbps_val list") Co-developed-by: Reinette Chatre <[email protected]> Signed-off-by: Reinette Chatre <[email protected]> Signed-off-by: Martin Kletzander <[email protected]> Signed-off-by: Borislav Petkov (AMD) <[email protected]> Reviewed-by: Reinette Chatre <[email protected]> Reviewed-by: Tony Luck <[email protected]>
1 parent f8bc84b commit 2b56484

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

arch/x86/kernel/cpu/resctrl/ctrlmondata.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
* hardware. The allocated bandwidth percentage is rounded to the next
3030
* control step available on the hardware.
3131
*/
32-
static bool bw_validate(char *buf, unsigned long *data, struct rdt_resource *r)
32+
static bool bw_validate(char *buf, u32 *data, struct rdt_resource *r)
3333
{
34-
unsigned long bw;
3534
int ret;
35+
u32 bw;
3636

3737
/*
3838
* Only linear delay values is supported for current Intel SKUs.
@@ -42,16 +42,21 @@ static bool bw_validate(char *buf, unsigned long *data, struct rdt_resource *r)
4242
return false;
4343
}
4444

45-
ret = kstrtoul(buf, 10, &bw);
45+
ret = kstrtou32(buf, 10, &bw);
4646
if (ret) {
47-
rdt_last_cmd_printf("Non-decimal digit in MB value %s\n", buf);
47+
rdt_last_cmd_printf("Invalid MB value %s\n", buf);
4848
return false;
4949
}
5050

51-
if ((bw < r->membw.min_bw || bw > r->default_ctrl) &&
52-
!is_mba_sc(r)) {
53-
rdt_last_cmd_printf("MB value %ld out of range [%d,%d]\n", bw,
54-
r->membw.min_bw, r->default_ctrl);
51+
/* Nothing else to do if software controller is enabled. */
52+
if (is_mba_sc(r)) {
53+
*data = bw;
54+
return true;
55+
}
56+
57+
if (bw < r->membw.min_bw || bw > r->default_ctrl) {
58+
rdt_last_cmd_printf("MB value %u out of range [%d,%d]\n",
59+
bw, r->membw.min_bw, r->default_ctrl);
5560
return false;
5661
}
5762

@@ -65,7 +70,7 @@ int parse_bw(struct rdt_parse_data *data, struct resctrl_schema *s,
6570
struct resctrl_staged_config *cfg;
6671
u32 closid = data->rdtgrp->closid;
6772
struct rdt_resource *r = s->res;
68-
unsigned long bw_val;
73+
u32 bw_val;
6974

7075
cfg = &d->staged_config[s->conf_type];
7176
if (cfg->have_new_ctrl) {

0 commit comments

Comments
 (0)