From c038f33d5e84adadea0ee683af3dfc8a9ecd1112 Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Wed, 21 Oct 2026 16:17:08 +0300 Subject: [PATCH] zephyr: sof_dma: validate syscalls against kernel-only DMA state The dma[] array is APP_SYSUSER_DATA, so user threads can write every field of it. The DMA syscalls trusted it in several ways: - the implementations re-read dma->z_dev after the validator checked it, so a forged device and its driver API could be swapped in - sof_dma_put() freed dma->chan through dma->heap, and the reference count was protected by dma->lock, all in user-writable memory - dma_init() allocated the channel array from a heap pointer read from user-writable memory, and read the array pointer back from dma->chan - channel numbers were passed to drivers that only assert on them, and asserts are compiled out in production builds - sof_dma_config() picked the address to validate from the user's channel_direction, while HDA DMA programs the buffer address that matches the device's own direction Add kernel-only state for each DMA controller, struct sof_dma_kinfo, filled in dmac_init() before user threads run. It is the authoritative state for the kernel, while dma[] stays as the untrusted user-visible view. It holds the reference count, the lock protecting it, and the channel array with its heap. With CONFIG_SOF_USERSPACE_LL, dma_init() checks the heap with zephyr_ll_user_heap_verify(), and it publishes the channel array only once it is initialized. The validators now look up the kernel state, bound the channel by the driver's kernel-only dma_context, require the transfer direction to match the device, and call the Zephyr DMA API on the validated device. Peripheral directions are refused for non-HDA DMA until FIFO addresses can be checked against a kernel-side list. Signed-off-by: Kai Vehmanen --- src/lib/dma.c | 110 ++++++++++++++++++++---- zephyr/include/sof/lib/sof_dma.h | 36 ++++++++ zephyr/lib/dma.c | 27 ++++++ zephyr/syscall/sof_dma.c | 141 +++++++++++++++++++++---------- 4 files changed, 254 insertions(+), 60 deletions(-) diff --git a/src/lib/dma.c b/src/lib/dma.c index 3fb883c1720a..8a5195212fb7 100644 --- a/src/lib/dma.c +++ b/src/lib/dma.c @@ -17,6 +17,7 @@ #include #include #include +#include /* zephyr_ll_user_heap_verify() */ #include #include #include @@ -37,6 +38,38 @@ DECLARE_TR_CTX(dma_tr, SOF_UUID(dma_uuid), LOG_LEVEL_INFO); #if CONFIG_ZEPHYR_NATIVE_DRIVERS static int dma_init(struct sof_dma *dma); +#ifdef CONFIG_SOF_USERSPACE_INTERFACE_DMA +/* + * dma[] is writable by user threads, so the reference count, the lock and + * the channel array to free are kept in the kernel-only struct + * sof_dma_kinfo. dma->sref is still updated for diagnostics, but never + * trusted. + */ +static int *dma_sref(struct sof_dma *dma) +{ + return &sof_dma_kinfo_get(dma)->sref; +} + +/* + * dma->lock is user-writable too: a user thread could release it while the + * kernel holds it, letting two cores update the reference count at once. + */ +static struct k_spinlock *dma_lock(struct sof_dma *dma) +{ + return &sof_dma_kinfo_get(dma)->lock; +} +#else +static int *dma_sref(struct sof_dma *dma) +{ + return &dma->sref; +} + +static struct k_spinlock *dma_lock(struct sof_dma *dma) +{ + return &dma->lock; +} +#endif + __cold struct sof_dma *z_impl_sof_dma_get(uint32_t dir, uint32_t cap, uint32_t dev, uint32_t flags) { const struct dma_info *info = dma_info_get(); @@ -111,9 +144,9 @@ __cold struct sof_dma *z_impl_sof_dma_get(uint32_t dir, uint32_t cap, uint32_t d * may be requested many times, let the probe() * do on-first-use initialization. */ - key = k_spin_lock(&dmin->lock); + key = k_spin_lock(dma_lock(dmin)); - if (!dmin->sref) { + if (!*dma_sref(dmin)) { ret = dma_init(dmin); if (ret < 0) { tr_err(&dma_tr, "dma_get(): dma-probe failed id = %d, ret = %d", @@ -122,13 +155,15 @@ __cold struct sof_dma *z_impl_sof_dma_get(uint32_t dir, uint32_t cap, uint32_t d } } - dmin->sref++; + ++*dma_sref(dmin); + /* user-visible copy for diagnostics and DMAC selection, never trusted */ + dmin->sref = *dma_sref(dmin); tr_info(&dma_tr, "dma_get() ID %d sref = %d busy channels %ld", dmin->plat_data.id, dmin->sref, atomic_read(&dmin->num_channels_busy)); out: - k_spin_unlock(&dmin->lock, key); + k_spin_unlock(dma_lock(dmin), key); return !ret ? dmin : NULL; } @@ -138,43 +173,84 @@ __cold void z_impl_sof_dma_put(struct sof_dma *dma) assert_can_be_cold(); - key = k_spin_lock(&dma->lock); - if (--dma->sref == 0) { + key = k_spin_lock(dma_lock(dma)); + if (*dma_sref(dma) <= 0) { + tr_err(&dma_tr, "dma_put(), dma = %p not in use", dma); + k_spin_unlock(dma_lock(dma), key); + return; + } + + --*dma_sref(dma); + dma->sref = *dma_sref(dma); + if (!*dma_sref(dma)) { +#ifdef CONFIG_SOF_USERSPACE_INTERFACE_DMA + struct sof_dma_kinfo *kinfo = sof_dma_kinfo_get(dma); + + sof_heap_free(kinfo->heap, kinfo->chan); + kinfo->chan = NULL; +#else sof_heap_free(dma->heap, dma->chan); +#endif dma->chan = NULL; } tr_info(&dma_tr, "dma_put(), dma = %p, sref = %d", dma, dma->sref); - k_spin_unlock(&dma->lock, key); + k_spin_unlock(dma_lock(dma), key); } __cold static int dma_init(struct sof_dma *dma) { - struct dma_chan_data *chan; + struct dma_chan_data *chans, *chan; struct k_heap *heap = sof_sys_user_heap_get(); - int i; +#ifdef CONFIG_SOF_USERSPACE_INTERFACE_DMA + /* dma->plat_data is user-writable, use the kernel snapshot */ + const uint32_t channels = sof_dma_kinfo_get(dma)->channels; +#else + const uint32_t channels = dma->plat_data.channels; +#endif + uint32_t i; assert_can_be_cold(); - /* allocate dma channels */ - dma->chan = sof_heap_alloc(heap, SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT, - sizeof(struct dma_chan_data) * dma->plat_data.channels, 0); - if (!dma->chan) { +#if CONFIG_SOF_USERSPACE_INTERFACE_DMA && CONFIG_SOF_USERSPACE_LL + /* + * Called in supervisor mode from the sof_dma_get() system call. The + * heap pointer comes from user-writable memory, so check that it is + * the LL user heap before running the allocator on it. + */ + if (!zephyr_ll_user_heap_verify(heap)) { + tr_err(&dma_tr, "dma %d: invalid channel heap %p", dma->plat_data.id, heap); + return -EINVAL; + } +#endif + + /* + * allocate dma channels, use a local pointer until the array is + * initialized: dma->chan is user-writable and must not be read back + */ + chans = sof_heap_alloc(heap, SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT, + sizeof(struct dma_chan_data) * channels, 0); + if (!chans) { tr_err(&dma_tr, "dma %d allocation of channels failed", dma->plat_data.id); return -ENOMEM; } - dma->heap = heap; - memset(dma->chan, 0, sizeof(struct dma_chan_data) * dma->plat_data.channels); + memset(chans, 0, sizeof(struct dma_chan_data) * channels); /* init work */ - for (i = 0, chan = dma->chan; i < dma->plat_data.channels; - i++, chan++) { + for (i = 0, chan = chans; i < channels; i++, chan++) { chan->dma = dma; chan->index = i; } +#ifdef CONFIG_SOF_USERSPACE_INTERFACE_DMA + sof_dma_kinfo_get(dma)->heap = heap; + sof_dma_kinfo_get(dma)->chan = chans; +#endif + dma->heap = heap; + dma->chan = chans; + return 0; } #else diff --git a/zephyr/include/sof/lib/sof_dma.h b/zephyr/include/sof/lib/sof_dma.h index bdd64aeac9fa..86f36c3143a2 100644 --- a/zephyr/include/sof/lib/sof_dma.h +++ b/zephyr/include/sof/lib/sof_dma.h @@ -111,6 +111,42 @@ static inline int z_impl_sof_dma_resume(struct sof_dma *dma, uint32_t channel) #ifdef CONFIG_SOF_USERSPACE_INTERFACE_DMA +struct k_heap; +struct dma_chan_data; + +/** + * Kernel-only state of one struct sof_dma entry. + * + * This is the authoritative state for the kernel. The dma[] array is shared + * with user threads (APP_SYSUSER_DATA), so none of its fields can be trusted + * in supervisor mode: there, the DMA system call validators and the get/put + * reference counting use this structure. The dma[] fields stay as the + * user-visible view. The kernel may still read them for non-security + * purposes, such as picking a controller in sof_dma_get() or logging, but + * every decision that matters is taken from here. The constant fields are + * copied from dma[] in dmac_init(), before user threads run. + */ +struct sof_dma_kinfo { + const struct device *z_dev; /**< Zephyr DMA device */ + uint32_t dir; /**< SOF_DMA_DIR_* supported directions */ + uint32_t caps; /**< SOF_DMA_CAP_* capabilities */ + uint32_t channels; /**< size of the channel array */ + struct k_spinlock lock; /**< protects sref, heap and chan */ + int sref; /**< reference count, under lock */ + struct k_heap *heap; /**< heap the channel array came from */ + struct dma_chan_data *chan; /**< channel array to free on last put */ +}; + +/** + * Return the kernel-only state of @dma. + * + * @dma is compared against the dma[] array bounds only, it is not + * dereferenced, so an untrusted pointer may be passed. + * + * @return kernel-only state, or NULL if @dma is not an element of dma[] + */ +struct sof_dma_kinfo *sof_dma_kinfo_get(const struct sof_dma *dma); + /* include definitions from generated file */ #include diff --git a/zephyr/lib/dma.c b/zephyr/lib/dma.c index ad19a71ea84f..d9a9642e830b 100644 --- a/zephyr/lib/dma.c +++ b/zephyr/lib/dma.c @@ -304,6 +304,23 @@ const struct dma_info lib_dma = { .num_dmas = ARRAY_SIZE(dma) }; +#ifdef CONFIG_SOF_USERSPACE_INTERFACE_DMA +/* kernel-only, see struct sof_dma_kinfo */ +static struct sof_dma_kinfo dma_kinfo[ARRAY_SIZE(dma)]; + +struct sof_dma_kinfo *sof_dma_kinfo_get(const struct sof_dma *d) +{ + /* use the array symbol, not sof->dma_info, and never dereference d */ + uintptr_t offset = (uintptr_t)d - (uintptr_t)dma; + + if ((uintptr_t)d < (uintptr_t)dma || offset >= sizeof(dma) || + offset % sizeof(dma[0])) + return NULL; + + return &dma_kinfo[offset / sizeof(dma[0])]; +} +#endif + /* Initialize all platform DMAC's */ __cold int dmac_init(struct sof *sof) { @@ -317,5 +334,15 @@ __cold int dmac_init(struct sof *sof) for (i = 0; i < sof->dma_info->num_dmas; i++) k_spinlock_init(&sof->dma_info->dma_array[i].lock); +#ifdef CONFIG_SOF_USERSPACE_INTERFACE_DMA + /* snapshot the constant fields before any user thread can modify dma[] */ + for (i = 0; i < ARRAY_SIZE(dma); i++) { + dma_kinfo[i].z_dev = dma[i].z_dev; + dma_kinfo[i].dir = dma[i].plat_data.dir; + dma_kinfo[i].caps = dma[i].plat_data.caps; + dma_kinfo[i].channels = dma[i].plat_data.channels; + } +#endif + return 0; } diff --git a/zephyr/syscall/sof_dma.c b/zephyr/syscall/sof_dma.c index 05d6df377c95..7e013728149b 100644 --- a/zephyr/syscall/sof_dma.c +++ b/zephyr/syscall/sof_dma.c @@ -9,32 +9,45 @@ #ifdef CONFIG_SOF_USERSPACE_INTERFACE_DMA -static inline bool sof_dma_has_access(struct sof_dma *dma) +/* + * Note: struct sof_dma (the dma[] array) is writable by user threads. The + * validators below must not read any of its fields. They use the kernel-only + * struct sof_dma_kinfo state, and call the Zephyr DMA API directly on the + * device found there, instead of the z_impl_sof_dma_*() wrappers, which + * would read dma->z_dev again. + */ + +/* @return true if the calling thread was granted access to the DMA device */ +static inline bool sof_dma_dev_has_access(const struct device *z_dev) { - /* - * use the Zephyr dma.h device handle to check calling - * thread has access to it - */ - return k_object_is_valid(dma->z_dev, K_OBJ_DRIVER_DMA); + return z_dev && k_object_is_valid(z_dev, K_OBJ_DRIVER_DMA); } -static inline bool sof_dma_is_valid(struct sof_dma *dma) +/* Check that @dma is a dma[] element the caller may use, return its kernel state */ +static const struct sof_dma_kinfo *sof_dma_vrfy(struct sof_dma *dma) { - const struct dma_info *info = dma_info_get(); - uintptr_t offset = (uintptr_t)dma - (uintptr_t)info->dma_array; - struct sof_dma *array_end = info->dma_array + info->num_dmas; + const struct sof_dma_kinfo *kinfo = sof_dma_kinfo_get(dma); - if (!info->num_dmas) - return false; + K_OOPS(!kinfo); + K_OOPS(!sof_dma_dev_has_access(kinfo->z_dev)); - /* - * The 'dma' pointer is not trusted, so we must first ensure it - * points to a valid "struct sof_dma *" kernel object. - */ - if (dma < info->dma_array || dma >= array_end || offset % sizeof(struct sof_dma)) - return false; + return kinfo; +} + +/* + * Check @channel against the channel count in the driver's kernel-only + * context. Zephyr DMA drivers only assert on the channel number, and + * asserts are compiled out in production builds. + */ +static const struct device *sof_dma_chan_vrfy(struct sof_dma *dma, uint32_t channel) +{ + const struct device *z_dev = sof_dma_vrfy(dma)->z_dev; + const struct dma_context *ctx = z_dev->data; - return sof_dma_has_access(dma); + K_OOPS(!ctx || ctx->magic != DMA_MAGIC); + K_OOPS(ctx->dma_channels < 0 || channel >= (uint32_t)ctx->dma_channels); + + return z_dev; } static inline struct sof_dma *z_vrfy_sof_dma_get(uint32_t dir, uint32_t cap, @@ -52,7 +65,9 @@ static inline struct sof_dma *z_vrfy_sof_dma_get(uint32_t dir, uint32_t cap, * permission. */ if (dma) { - if (sof_dma_has_access(dma)) + const struct sof_dma_kinfo *kinfo = sof_dma_kinfo_get(dma); + + if (kinfo && sof_dma_dev_has_access(kinfo->z_dev)) return dma; /* no access, release reference on error */ @@ -65,7 +80,7 @@ static inline struct sof_dma *z_vrfy_sof_dma_get(uint32_t dir, uint32_t cap, static inline void z_vrfy_sof_dma_put(struct sof_dma *dma) { - K_OOPS(!sof_dma_is_valid(dma)); + sof_dma_vrfy(dma); z_impl_sof_dma_put(dma); } @@ -73,27 +88,28 @@ static inline void z_vrfy_sof_dma_put(struct sof_dma *dma) static inline int z_vrfy_sof_dma_get_attribute(struct sof_dma *dma, uint32_t type, uint32_t *value) { - K_OOPS(!sof_dma_is_valid(dma)); + const struct device *z_dev = sof_dma_vrfy(dma)->z_dev; + K_OOPS(K_SYSCALL_MEMORY_WRITE(value, sizeof(*value))); - return z_impl_sof_dma_get_attribute(dma, type, value); + return dma_get_attribute(z_dev, type, value); } #include static inline int z_vrfy_sof_dma_request_channel(struct sof_dma *dma, uint32_t stream_tag) { - K_OOPS(!sof_dma_is_valid(dma)); + const struct device *z_dev = sof_dma_vrfy(dma)->z_dev; - return z_impl_sof_dma_request_channel(dma, stream_tag); + return dma_request_channel(z_dev, &stream_tag); } #include static inline void z_vrfy_sof_dma_release_channel(struct sof_dma *dma, uint32_t channel) { - K_OOPS(!sof_dma_is_valid(dma)); + const struct device *z_dev = sof_dma_chan_vrfy(dma, channel); - return z_impl_sof_dma_release_channel(dma, channel); + dma_release_channel(z_dev, channel); } #include @@ -204,6 +220,37 @@ static inline struct dma_block_config *deep_copy_dma_blk_cfg_list(struct dma_con return NULL; } +/* + * @return true if @kinfo's device supports transfer direction @dir, and the + * validators can check every address the device will use for it + */ +static bool sof_dma_dir_allowed(const struct sof_dma_kinfo *kinfo, uint32_t dir) +{ + switch (dir) { + case MEMORY_TO_MEMORY: + return kinfo->dir & SOF_DMA_DIR_MEM_TO_MEM; + case MEMORY_TO_HOST: + return kinfo->dir & SOF_DMA_DIR_LMEM_TO_HMEM; + case HOST_TO_MEMORY: + return kinfo->dir & SOF_DMA_DIR_HMEM_TO_LMEM; + case MEMORY_TO_PERIPHERAL: + case PERIPHERAL_TO_MEMORY: + /* + * The peripheral side address (FIFO) is not validated. That is + * safe for HDA link DMA, which has no such address, but not + * for general purpose DMA, which would transfer to or from any + * address given. Refuse those until FIFO addresses are checked + * against a kernel-side list. + */ + if (!(kinfo->caps & SOF_DMA_CAP_HDA)) + return false; + return kinfo->dir & (dir == MEMORY_TO_PERIPHERAL ? + SOF_DMA_DIR_MEM_TO_DEV : SOF_DMA_DIR_DEV_TO_MEM); + default: + return false; + } +} + static inline int z_vrfy_sof_dma_config(struct sof_dma *dma, uint32_t channel, struct dma_config *config) { @@ -212,9 +259,18 @@ static inline int z_vrfy_sof_dma_config(struct sof_dma *dma, uint32_t channel, struct dma_config user_cfg; int ret; - K_OOPS(!sof_dma_is_valid(dma)); + const struct sof_dma_kinfo *kinfo = sof_dma_vrfy(dma); + const struct device *z_dev = sof_dma_chan_vrfy(dma, channel); + K_OOPS(k_usermode_from_copy(&user_cfg, config, sizeof(user_cfg))); + /* + * Drivers such as Intel HDA ignore channel_direction and program the + * buffer field that matches the device's own direction. Require the + * two to match, so the validated address is the one the HW uses. + */ + K_OOPS(!sof_dma_dir_allowed(kinfo, user_cfg.channel_direction)); + /* use only DMA config attributes that are safe to use from user-space */ kern_cfg.dma_slot = user_cfg.dma_slot; kern_cfg.channel_direction = user_cfg.channel_direction; @@ -230,9 +286,7 @@ static inline int z_vrfy_sof_dma_config(struct sof_dma *dma, uint32_t channel, blk_cfgs = deep_copy_dma_blk_cfg_list(&kern_cfg); K_OOPS(blk_cfgs == NULL); - /* TODO: add checks for peripheral/host FIFO access? */ - - ret = z_impl_sof_dma_config(dma, channel, &kern_cfg); + ret = dma_config(z_dev, channel, &kern_cfg); rfree(blk_cfgs); @@ -242,51 +296,52 @@ static inline int z_vrfy_sof_dma_config(struct sof_dma *dma, uint32_t channel, static inline int z_vrfy_sof_dma_start(struct sof_dma *dma, uint32_t channel) { - K_OOPS(!sof_dma_is_valid(dma)); + const struct device *z_dev = sof_dma_chan_vrfy(dma, channel); - return z_impl_sof_dma_start(dma, channel); + return dma_start(z_dev, channel); } #include static inline int z_vrfy_sof_dma_stop(struct sof_dma *dma, uint32_t channel) { - K_OOPS(!sof_dma_is_valid(dma)); + const struct device *z_dev = sof_dma_chan_vrfy(dma, channel); - return z_impl_sof_dma_stop(dma, channel); + return dma_stop(z_dev, channel); } #include static inline int z_vrfy_sof_dma_get_status(struct sof_dma *dma, uint32_t channel, struct dma_status *stat) { - K_OOPS(!sof_dma_is_valid(dma)); + const struct device *z_dev = sof_dma_chan_vrfy(dma, channel); + K_OOPS(K_SYSCALL_MEMORY_WRITE(stat, sizeof(*stat))); - return z_impl_sof_dma_get_status(dma, channel, stat); + return dma_get_status(z_dev, channel, stat); } #include static inline int z_vrfy_sof_dma_reload(struct sof_dma *dma, uint32_t channel, size_t size) { - K_OOPS(!sof_dma_is_valid(dma)); + const struct device *z_dev = sof_dma_chan_vrfy(dma, channel); - return z_impl_sof_dma_reload(dma, channel, size); + return dma_reload(z_dev, channel, 0, 0, size); } #include static inline int z_vrfy_sof_dma_suspend(struct sof_dma *dma, uint32_t channel) { - K_OOPS(!sof_dma_is_valid(dma)); + const struct device *z_dev = sof_dma_chan_vrfy(dma, channel); - return z_impl_sof_dma_suspend(dma, channel); + return dma_suspend(z_dev, channel); } #include static inline int z_vrfy_sof_dma_resume(struct sof_dma *dma, uint32_t channel) { - K_OOPS(!sof_dma_is_valid(dma)); + const struct device *z_dev = sof_dma_chan_vrfy(dma, channel); - return z_impl_sof_dma_resume(dma, channel); + return dma_resume(z_dev, channel); } #include