linux-next/lib/kunit/device.c
David Gow f2c6dbd220 kunit: Device wrappers should also manage driver name
kunit_driver_create() accepts a name for the driver, but does not copy
it, so if that name is either on the stack, or otherwise freed, we end
up with a use-after-free when the driver is cleaned up.

Instead, strdup() the name, and manage it as another KUnit allocation.
As there was no existing kunit_kstrdup(), we add one. Further, add a
kunit_ variant of strdup_const() and kfree_const(), so we don't need to
allocate and manage the string in the majority of cases where it's a
constant.

However, these are inline functions, and is_kernel_rodata() only works
for built-in code. This causes problems in two cases:
- If kunit is built as a module, __{start,end}_rodata is not defined.
- If a kunit test using these functions is built as a module, it will
  suffer the same fate.

This fixes a KASAN splat with overflow.overflow_allocation_test, when
built as a module.

Restrict the is_kernel_rodata() case to when KUnit is built as a module,
which fixes the first case, at the cost of losing the optimisation.

Also, make kunit_{kstrdup,kfree}_const non-inline, so that other modules
using them will not accidentally depend on is_kernel_rodata(). If KUnit
is built-in, they'll benefit from the optimisation, if KUnit is not,
they won't, but the string will be properly duplicated.

Fixes: d03c720e03 ("kunit: Add APIs for managing devices")
Reported-by: Nico Pache <npache@redhat.com>
Closes: https://groups.google.com/g/kunit-dev/c/81V9b9QYON0
Reviewed-by: Kees Cook <kees@kernel.org>
Reviewed-by: Maxime Ripard <mripard@kernel.org>
Reviewed-by: Rae Moar <rmoar@google.com>
Signed-off-by: David Gow <davidgow@google.com>
Tested-by: Rae Moar <rmoar@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2024-08-26 07:03:46 -06:00

203 lines
5.2 KiB
C

// SPDX-License-Identifier: GPL-2.0
/*
* KUnit-managed device implementation
*
* Implementation of struct kunit_device helpers for fake devices whose
* lifecycle is managed by KUnit.
*
* Copyright (C) 2023, Google LLC.
* Author: David Gow <davidgow@google.com>
*/
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <kunit/test.h>
#include <kunit/device.h>
#include <kunit/resource.h>
#include "device-impl.h"
/* Wrappers for use with kunit_add_action() */
KUNIT_DEFINE_ACTION_WRAPPER(device_unregister_wrapper, device_unregister, struct device *);
KUNIT_DEFINE_ACTION_WRAPPER(driver_unregister_wrapper, driver_unregister, struct device_driver *);
/* The root device for the KUnit bus, parent of all kunit_devices. */
static struct device *kunit_bus_device;
/* A device owned by a KUnit test. */
struct kunit_device {
struct device dev;
/* The KUnit test which owns this device. */
struct kunit *owner;
/* If the driver is managed by KUnit and unique to this device. */
const struct device_driver *driver;
};
#define to_kunit_device(d) container_of_const(d, struct kunit_device, dev)
static const struct bus_type kunit_bus_type = {
.name = "kunit",
};
/* Register the 'kunit_bus' used for fake devices. */
int kunit_bus_init(void)
{
int error;
kunit_bus_device = root_device_register("kunit");
if (IS_ERR(kunit_bus_device))
return PTR_ERR(kunit_bus_device);
error = bus_register(&kunit_bus_type);
if (error)
root_device_unregister(kunit_bus_device);
return error;
}
/* Unregister the 'kunit_bus' in case the KUnit module is unloaded. */
void kunit_bus_shutdown(void)
{
/* Make sure the bus exists before we unregister it. */
if (IS_ERR_OR_NULL(kunit_bus_device))
return;
bus_unregister(&kunit_bus_type);
root_device_unregister(kunit_bus_device);
kunit_bus_device = NULL;
}
/* Release a 'fake' KUnit device. */
static void kunit_device_release(struct device *d)
{
kfree(to_kunit_device(d));
}
/*
* Create and register a KUnit-managed struct device_driver on the kunit_bus.
* Returns an error pointer on failure.
*/
struct device_driver *kunit_driver_create(struct kunit *test, const char *name)
{
struct device_driver *driver;
int err = -ENOMEM;
driver = kunit_kzalloc(test, sizeof(*driver), GFP_KERNEL);
if (!driver)
return ERR_PTR(err);
driver->name = kunit_kstrdup_const(test, name, GFP_KERNEL);
driver->bus = &kunit_bus_type;
driver->owner = THIS_MODULE;
err = driver_register(driver);
if (err) {
kunit_kfree(test, driver);
return ERR_PTR(err);
}
kunit_add_action(test, driver_unregister_wrapper, driver);
return driver;
}
EXPORT_SYMBOL_GPL(kunit_driver_create);
/* Helper which creates a kunit_device, attaches it to the kunit_bus*/
static struct kunit_device *kunit_device_register_internal(struct kunit *test,
const char *name,
const struct device_driver *drv)
{
struct kunit_device *kunit_dev;
int err = -ENOMEM;
kunit_dev = kzalloc(sizeof(*kunit_dev), GFP_KERNEL);
if (!kunit_dev)
return ERR_PTR(err);
kunit_dev->owner = test;
err = dev_set_name(&kunit_dev->dev, "%s.%s", test->name, name);
if (err) {
kfree(kunit_dev);
return ERR_PTR(err);
}
kunit_dev->dev.release = kunit_device_release;
kunit_dev->dev.bus = &kunit_bus_type;
kunit_dev->dev.parent = kunit_bus_device;
err = device_register(&kunit_dev->dev);
if (err) {
put_device(&kunit_dev->dev);
return ERR_PTR(err);
}
kunit_dev->dev.dma_mask = &kunit_dev->dev.coherent_dma_mask;
kunit_dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
kunit_add_action(test, device_unregister_wrapper, &kunit_dev->dev);
return kunit_dev;
}
/*
* Create and register a new KUnit-managed device, using the user-supplied device_driver.
* On failure, returns an error pointer.
*/
struct device *kunit_device_register_with_driver(struct kunit *test,
const char *name,
const struct device_driver *drv)
{
struct kunit_device *kunit_dev = kunit_device_register_internal(test, name, drv);
if (IS_ERR_OR_NULL(kunit_dev))
return ERR_CAST(kunit_dev);
return &kunit_dev->dev;
}
EXPORT_SYMBOL_GPL(kunit_device_register_with_driver);
/*
* Create and register a new KUnit-managed device, including a matching device_driver.
* On failure, returns an error pointer.
*/
struct device *kunit_device_register(struct kunit *test, const char *name)
{
struct device_driver *drv;
struct kunit_device *dev;
drv = kunit_driver_create(test, name);
if (IS_ERR(drv))
return ERR_CAST(drv);
dev = kunit_device_register_internal(test, name, drv);
if (IS_ERR(dev)) {
kunit_release_action(test, driver_unregister_wrapper, (void *)drv);
return ERR_CAST(dev);
}
/* Request the driver be freed. */
dev->driver = drv;
return &dev->dev;
}
EXPORT_SYMBOL_GPL(kunit_device_register);
/* Unregisters a KUnit-managed device early (including the driver, if automatically created). */
void kunit_device_unregister(struct kunit *test, struct device *dev)
{
const struct device_driver *driver = to_kunit_device(dev)->driver;
kunit_release_action(test, device_unregister_wrapper, dev);
if (driver) {
const char *driver_name = driver->name;
kunit_release_action(test, driver_unregister_wrapper, (void *)driver);
kunit_kfree_const(test, driver_name);
}
}
EXPORT_SYMBOL_GPL(kunit_device_unregister);