Skip to content

Commit cc34d77

Browse files
Danilo Krummrichbroonie
authored andcommitted
spi: use generic driver_override infrastructure
When a driver is probed through __driver_attach(), the bus' match() callback is called without the device lock held, thus accessing the driver_override field without a lock, which can cause a UAF. Fix this by using the driver-core driver_override infrastructure taking care of proper locking internally. Note that calling match() from __driver_attach() without the device lock held is intentional. [1] Also note that we do not enable the driver_override feature of struct bus_type, as SPI - in contrast to most other buses - passes "" to sysfs_emit() when the driver_override pointer is NULL. Thus, printing "\n" instead of "(null)\n". Link: https://lore.kernel.org/driver-core/[email protected]/ [1] Reported-by: Gui-Dong Han <[email protected]> Closes: https://bugzilla.kernel.org/show_bug.cgi?id=220789 Fixes: 5039563 ("spi: Add driver_override SPI device attribute") Signed-off-by: Danilo Krummrich <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 63542bb commit cc34d77

2 files changed

Lines changed: 7 additions & 17 deletions

File tree

drivers/spi/spi.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ static void spidev_release(struct device *dev)
5050
struct spi_device *spi = to_spi_device(dev);
5151

5252
spi_controller_put(spi->controller);
53-
kfree(spi->driver_override);
5453
free_percpu(spi->pcpu_statistics);
5554
kfree(spi);
5655
}
@@ -73,10 +72,9 @@ static ssize_t driver_override_store(struct device *dev,
7372
struct device_attribute *a,
7473
const char *buf, size_t count)
7574
{
76-
struct spi_device *spi = to_spi_device(dev);
7775
int ret;
7876

79-
ret = driver_set_override(dev, &spi->driver_override, buf, count);
77+
ret = __device_set_driver_override(dev, buf, count);
8078
if (ret)
8179
return ret;
8280

@@ -86,13 +84,8 @@ static ssize_t driver_override_store(struct device *dev,
8684
static ssize_t driver_override_show(struct device *dev,
8785
struct device_attribute *a, char *buf)
8886
{
89-
const struct spi_device *spi = to_spi_device(dev);
90-
ssize_t len;
91-
92-
device_lock(dev);
93-
len = sysfs_emit(buf, "%s\n", spi->driver_override ? : "");
94-
device_unlock(dev);
95-
return len;
87+
guard(spinlock)(&dev->driver_override.lock);
88+
return sysfs_emit(buf, "%s\n", dev->driver_override.name ?: "");
9689
}
9790
static DEVICE_ATTR_RW(driver_override);
9891

@@ -376,10 +369,12 @@ static int spi_match_device(struct device *dev, const struct device_driver *drv)
376369
{
377370
const struct spi_device *spi = to_spi_device(dev);
378371
const struct spi_driver *sdrv = to_spi_driver(drv);
372+
int ret;
379373

380374
/* Check override first, and if set, only use the named driver */
381-
if (spi->driver_override)
382-
return strcmp(spi->driver_override, drv->name) == 0;
375+
ret = device_match_driver_override(dev, drv);
376+
if (ret >= 0)
377+
return ret;
383378

384379
/* Attempt an OF style match */
385380
if (of_driver_match_device(dev, drv))

include/linux/spi/spi.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,6 @@ extern void spi_transfer_cs_change_delay_exec(struct spi_message *msg,
159159
* @modalias: Name of the driver to use with this device, or an alias
160160
* for that name. This appears in the sysfs "modalias" attribute
161161
* for driver coldplugging, and in uevents used for hotplugging
162-
* @driver_override: If the name of a driver is written to this attribute, then
163-
* the device will bind to the named driver and only the named driver.
164-
* Do not set directly, because core frees it; use driver_set_override() to
165-
* set or clear it.
166162
* @pcpu_statistics: statistics for the spi_device
167163
* @word_delay: delay to be inserted between consecutive
168164
* words of a transfer
@@ -224,7 +220,6 @@ struct spi_device {
224220
void *controller_state;
225221
void *controller_data;
226222
char modalias[SPI_NAME_SIZE];
227-
const char *driver_override;
228223

229224
/* The statistics */
230225
struct spi_statistics __percpu *pcpu_statistics;

0 commit comments

Comments
 (0)