Skip to contentSkip to conversation

dma-buf: Reject attaching a zero-sized dma-buf

PATCH New Lizhi Hou 2 participants 2 messages Nov 23

Subsystems (2)
lore
Share & tools
lore

Lizhi Hou

author
#0 lore raw

PATCH 1/1 dma-buf: Reject attaching a zero-sized dma-buf

Attaching to a zero-sized dma-buf does not make sense at all: there is nothing for an importer to map.

So reject the attach, using WARN_ON() because a zero-sized dma-buf is an exporter bug that userspace cannot trigger on its own.

Adding the check to dma_buf_export() instead was considered, which would prevent zero-sized DMA-bufs from being created in the first place. It is not done here because drivers may be using zero-sized DMA-bufs for driver internal buffers, and refusing them at creation time risks regressing those users.

Fixes d15bd7ee445d ("dma-buf: Introduce dma buffer sharing mechanism")
---
 drivers/dma-buf/dma-buf.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/dma-buf/dma-buf.c +3 -0
index 5c6957f8839a..8be7b6023e4a 100644
@@ -1050,6 +1050,9 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
 	if (WARN_ON(!dmabuf || !dev))
 		return ERR_PTR(-EINVAL);
 
+	if (WARN_ON(!dmabuf->size))
+		return ERR_PTR(-EINVAL);
+
 	attach = kzalloc_obj(*attach);
 	if (!attach)
 		return ERR_PTR(-ENOMEM);
Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] dma-buf: userspace-triggerable WARN_ON() in dma_buf_dynamic_attach() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] dma-buf: userspace-triggerable WARN_ON() in dma_buf_dynamic_attach()
--

commit ce500544d70a1177e5ffafe3281fb66a51101188
Author: Lizhi Hou <[email protected]>

dma-buf: Reject attaching a zero-sized dma-buf

This commit rejects attaching to a zero-sized dma-buf in dma_buf_dynamic_attach() with a WARN_ON() and -EINVAL.

drivers/dma-buf/dma-buf.c dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev, · line 1054

Quoted 10 lines
drivers/dma-buf/dma-buf.c +2 -0
index 5c6957f8839a8..8be7b6023e4a7 100644
@@ -1050,6 +1050,9 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
 	if (WARN_ON(!dmabuf || !dev))
 		return ERR_PTR(-EINVAL);
 
+	if (WARN_ON(!dmabuf->size))
+		return ERR_PTR(-EINVAL);

[Severity: Medium]
Can userspace trigger this WARN_ON(!dmabuf->size)?

Because dma_buf_export() continues to allow exp_info->size == 0, any exporter that does not reject a zero length from userspace before exporting allows an unprivileged user to hit this warning (and crash systems with panic_on_warn).

For example, DRM_IOCTL_AMDXDNA_CREATE_BO reaches amdxdna_gem_create_ubuf_object() -> amdxdna_get_ubuf(), where va_ent[0].len == 0 passes IS_ALIGNED(0, PAGE_SIZE) and exports a zero-sized dma_buf that amdxdna_gem_prime_import() immediately attaches:

drivers/accel/amdxdna/amdxdna_ubuf.c:amdxdna_get_ubuf() {
	...
	for (i = 0, exp_info.size = 0; i < num_entries; i++) {
		if (!IS_ALIGNED(va_ent[i].vaddr, PAGE_SIZE) ||
		    !IS_ALIGNED(va_ent[i].len, PAGE_SIZE)) {
	...
		if (check_add_overflow(exp_info.size, va_ent[i].len, &exp_info.size)) {
	...
	dmabuf = dma_buf_export(&exp_info);
}

Calling dma_buf_attach() on that buffer then triggers this unratelimited WARN_ON() on every ioctl call.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1