Skip to content

Commit 2985b18

Browse files
toddkjosbjorn-helgaas
authored andcommitted
PCI: Fix reset_method_store() memory leak
In reset_method_store(), a string is allocated via kstrndup() and assigned to the local "options". options is then used in with strsep() to find spaces: while ((name = strsep(&options, " ")) != NULL) { If there are no remaining spaces, then options is set to NULL by strsep(), so the subsequent kfree(options) doesn't free the memory allocated via kstrndup(). Fix by using a separate tmp_options to iterate with strsep() so options is preserved. Link: https://lore.kernel.org/r/20241001231147.3583649-1-tkjos@google.com Fixes: d88f521 ("PCI: Allow userspace to query and set device reset mechanism") Signed-off-by: Todd Kjos <tkjos@google.com> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
1 parent 43ee11a commit 2985b18

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

drivers/pci/pci.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5244,7 +5244,7 @@ static ssize_t reset_method_store(struct device *dev,
52445244
const char *buf, size_t count)
52455245
{
52465246
struct pci_dev *pdev = to_pci_dev(dev);
5247-
char *options, *name;
5247+
char *options, *tmp_options, *name;
52485248
int m, n;
52495249
u8 reset_methods[PCI_NUM_RESET_METHODS] = { 0 };
52505250

@@ -5264,7 +5264,8 @@ static ssize_t reset_method_store(struct device *dev,
52645264
return -ENOMEM;
52655265

52665266
n = 0;
5267-
while ((name = strsep(&options, " ")) != NULL) {
5267+
tmp_options = options;
5268+
while ((name = strsep(&tmp_options, " ")) != NULL) {
52685269
if (sysfs_streq(name, ""))
52695270
continue;
52705271

0 commit comments

Comments
 (0)