PCI/sysfs: Fix double free in error path
authorSascha Hauer <s.hauer@pengutronix.de>
Tue, 8 Nov 2022 23:05:59 +0000 (17:05 -0600)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 7 Jan 2023 11:07:37 +0000 (12:07 +0100)
commit5e411983bb1e839fd2eead5a53b7b888b845318e
treeee8c71439e9926cbc648f6021072d38995e3cafd
parent0f4dfff7dc7df794b8e8b93a84330a5cbaf62b37
PCI/sysfs: Fix double free in error path

commit aa382ffa705bea9931ec92b6f3c70e1fdb372195 upstream.

When pci_create_attr() fails, pci_remove_resource_files() is called which
will iterate over the res_attr[_wc] arrays and frees every non NULL entry.
To avoid a double free here set the array entry only after it's clear we
successfully initialized it.

Fixes: b562ec8f74e4 ("PCI: Don't leak memory if sysfs_create_bin_file() fails")
Link: https://lore.kernel.org/r/20221007070735.GX986@pengutronix.de/
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Cc: stable@vger.kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/pci/pci-sysfs.c