Skip to content

Commit d9f421d

Browse files
Shyam Sundar S Kij-intel
authored andcommitted
platform/x86/amd/pmc: Use flex array when calling amd_pmc_stb_debugfs_open_v2()
Currently in amd_pmc_stb_debugfs_open_v2() the buffer size is assumed to be fixed and a second call to amd_pmc_stb_debugfs_open_v2() may race with a process holding open another fd. This could change "fsize" to a bigger size causing an out of bounds read. Instead create a struct with a flexarray to solve this. Suggested-by: Hans de Goede <[email protected]> Reviewed-by: Hans de Goede <[email protected]> Signed-off-by: Sanket Goswami <[email protected]> Signed-off-by: Shyam Sundar S K <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ij: renamed flex_arr -> stb_data_arr] Reviewed-by: Ilpo Järvinen <[email protected]> Signed-off-by: Ilpo Järvinen <[email protected]>
1 parent 5a02676 commit d9f421d

File tree

1 file changed

+19
-13
lines changed
  • drivers/platform/x86/amd/pmc

1 file changed

+19
-13
lines changed

drivers/platform/x86/amd/pmc/pmc.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
#define AMD_S2D_REGISTER_ARGUMENT 0xA88
5353

5454
/* STB Spill to DRAM Parameters */
55-
#define S2D_TELEMETRY_BYTES_MAX 0x100000
55+
#define S2D_TELEMETRY_BYTES_MAX 0x100000U
5656
#define S2D_TELEMETRY_DRAMBYTES_MAX 0x1000000
5757

5858
/* STB Spill to DRAM Message Definition */
@@ -122,6 +122,11 @@ enum s2d_arg {
122122
S2D_DRAM_SIZE,
123123
};
124124

125+
struct amd_pmc_stb_v2_data {
126+
size_t size;
127+
u8 data[] __counted_by(size);
128+
};
129+
125130
struct amd_pmc_bit_map {
126131
const char *name;
127132
u32 bit_mask;
@@ -239,18 +244,15 @@ static const struct file_operations amd_pmc_stb_debugfs_fops = {
239244
static int amd_pmc_stb_debugfs_open_v2(struct inode *inode, struct file *filp)
240245
{
241246
struct amd_pmc_dev *dev = filp->f_inode->i_private;
242-
u32 *buf, fsize, num_samples, val, stb_rdptr_offset = 0;
247+
u32 fsize, num_samples, val, stb_rdptr_offset = 0;
248+
struct amd_pmc_stb_v2_data *stb_data_arr;
243249
int ret;
244250

245251
/* Write dummy postcode while reading the STB buffer */
246252
ret = amd_pmc_write_stb(dev, AMD_PMC_STB_DUMMY_PC);
247253
if (ret)
248254
dev_err(dev->dev, "error writing to STB: %d\n", ret);
249255

250-
buf = kzalloc(S2D_TELEMETRY_BYTES_MAX, GFP_KERNEL);
251-
if (!buf)
252-
return -ENOMEM;
253-
254256
/* Spill to DRAM num_samples uses separate SMU message port */
255257
dev->msg_port = 1;
256258

@@ -264,10 +266,16 @@ static int amd_pmc_stb_debugfs_open_v2(struct inode *inode, struct file *filp)
264266
dev->msg_port = 0;
265267
if (ret) {
266268
dev_err(dev->dev, "error: S2D_NUM_SAMPLES not supported : %d\n", ret);
267-
kfree(buf);
268269
return ret;
269270
}
270271

272+
fsize = min(num_samples, S2D_TELEMETRY_BYTES_MAX);
273+
stb_data_arr = kmalloc(struct_size(stb_data_arr, data, fsize), GFP_KERNEL);
274+
if (!stb_data_arr)
275+
return -ENOMEM;
276+
277+
stb_data_arr->size = fsize;
278+
271279
/* Start capturing data from the last push location */
272280
if (num_samples > S2D_TELEMETRY_BYTES_MAX) {
273281
fsize = S2D_TELEMETRY_BYTES_MAX;
@@ -277,20 +285,18 @@ static int amd_pmc_stb_debugfs_open_v2(struct inode *inode, struct file *filp)
277285
stb_rdptr_offset = 0;
278286
}
279287

280-
memcpy_fromio(buf, dev->stb_virt_addr + stb_rdptr_offset, fsize);
281-
filp->private_data = buf;
288+
memcpy_fromio(stb_data_arr->data, dev->stb_virt_addr + stb_rdptr_offset, fsize);
289+
filp->private_data = stb_data_arr;
282290

283291
return 0;
284292
}
285293

286294
static ssize_t amd_pmc_stb_debugfs_read_v2(struct file *filp, char __user *buf, size_t size,
287295
loff_t *pos)
288296
{
289-
if (!filp->private_data)
290-
return -EINVAL;
297+
struct amd_pmc_stb_v2_data *data = filp->private_data;
291298

292-
return simple_read_from_buffer(buf, size, pos, filp->private_data,
293-
S2D_TELEMETRY_BYTES_MAX);
299+
return simple_read_from_buffer(buf, size, pos, data->data, data->size);
294300
}
295301

296302
static int amd_pmc_stb_debugfs_release_v2(struct inode *inode, struct file *filp)

0 commit comments

Comments
 (0)