Skip to contentSkip to conversation

dma-buf/udmabuf: Disable the size limit by default

PATCH Superseded Robert Mader 3 participants 4 messages Jul 17 – Jul 22

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dma-buf/udmabuf: Disable the size limit by default

As udmabuf increasingly enjoys popularity - being used in projects like libcamera, Gstreamer, Mesa, KWin and Weston - users more frequently encounter cases where the current default size limit of 64MB is too low. Examples include allocating video buffers at a 8K resolution - and even 4K is affected when using non-subsampled video formats and high bit depths.

In its current form the size limit for individual buffers does not seem to provide any additional level of protection - such as limiting the amount of memory a process can pin - as the later can just allocate multiple buffers. If additional guardrails are desired, they would likely require some kind accounting not limited to individual buffers.

Therefor let's disable the size limit by default. Use the special value of zero to do so, which prevously could be used to effectively disable the interface. Using other means, such as file permissions, appears to be a much better fit for that purpose.

---

Please let me know if changing the meaning of the parameter value of zero is considered a breaking change / not acceptable. In that case INT_MAX might be a better option.

See
https://lore.kernel.org/dri-devel/[email protected]/
for a previous attempt to make the value configurable via kconfig - and in particular
https://lore.kernel.org/dri-devel/[email protected]/
for the suggestion and discussion to remove the default limit.

---
 drivers/dma-buf/udmabuf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/dma-buf/udmabuf.c +3 -3
index bced421c0d65..3509b690d8e2 100644
@@ -20,9 +20,9 @@ static int list_limit = 1024;
 module_param(list_limit, int, 0644);
 MODULE_PARM_DESC(list_limit, "udmabuf_create_list->count limit. Default is 1024.");
 
-static int size_limit_mb = 64;
+static int size_limit_mb = 0;
 module_param(size_limit_mb, int, 0644);
-MODULE_PARM_DESC(size_limit_mb, "Max size of a dmabuf, in megabytes. Default is 64.");
+MODULE_PARM_DESC(size_limit_mb, "Max size of a dmabuf, in megabytes. Setting 0 disables the limit. Default is 0.");
 
 struct udmabuf {
 	pgoff_t pagecount;
@@ -373,7 +373,7 @@ static long udmabuf_create(struct miscdevice *device,
 
 		subpgcnt = list[i].size >> PAGE_SHIFT;
 		pgcnt += subpgcnt;
-		if (pgcnt > pglimit)
+		if (pglimit && pglimit < pgcnt)
 			goto err_noinit;
 
 		max_nr_folios = max_t(unsigned long, subpgcnt, max_nr_folios);
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] Removing the default size limit exposes an integer overflow on 32-bit architectures during…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] Removing the default size limit exposes an integer overflow on 32-bit architectures during pgcnt accumulation, leading to a critical heap buffer overflow.
- [High] Removing the default size_limit_mb allows an unprivileged user to force arbitrary unbound kernel memory allocations via kvmalloc(), leading to a global OOM. --

--- Patch [1]: [PATCH v1] dma-buf/udmabuf: Disable the size limit by default --- commit 99ebd05d75131f1777563136d3ba9224beb0dc68
Author: Robert Mader <[email protected]>

dma-buf/udmabuf: Disable the size limit by default

This commit changes the default size limit for udmabuf to 0, which disables the limit. It modifies the size check in udmabuf_create() to bypass validation when the limit is 0.

Quoted 4 lines
drivers/dma-buf/udmabuf.c +0 -0
index 08f57bc1294df..3ebc1742f3ff7 100644

[ ... ]

drivers/dma-buf/udmabuf.c static long udmabuf_create(struct miscdevice *device, · line 378

Quoted 7 lines
drivers/dma-buf/udmabuf.c +1 -1
@@ -374,7 +374,7 @@ static long udmabuf_create(struct miscdevice *device,
 
 		subpgcnt = list[i].size >> PAGE_SHIFT;
 		pgcnt += subpgcnt;
-		if (pgcnt > pglimit)
+		if (pglimit && pglimit < pgcnt)
 			goto err_noinit;

[Severity: High]
Does removing this default size limit allow unprivileged users to force arbitrary unbound kernel memory allocations and trigger a global OOM?

With pglimit bypassed, a user could submit an ioctl(UDMABUF_CREATE) with a massive size. The unbound pgcnt is then passed to init_udmabuf(), which allocates metadata arrays via kvmalloc_objs() using GFP_KERNEL:

drivers/dma-buf/udmabuf.c:init_udmabuf() {
    ...
    ubuf->pages = kvmalloc_objs(*ubuf->pages, pgcnt);
    ...
}

For huge requests, the vmalloc fallback executes with GFP_KERNEL and without __GFP_RETRY_MAYFAIL or __GFP_ACCOUNT. Could this force the page allocator to invoke the global OOM killer?

[Severity: Critical]
Does bypassing this size check also expose an integer overflow regression on 32-bit architectures?

On 32-bit systems, pgcnt is a 32-bit pgoff_t. A user could submit an ioctl with multiple items whose subpgcnt values sum to exceed U32_MAX. This would wrap pgcnt around to a small positive value.

init_udmabuf() would then allocate a tiny array for ubuf->pages:

drivers/dma-buf/udmabuf.c:init_udmabuf() {
    ...
    ubuf->pages = kvmalloc_objs(*ubuf->pages, pgcnt);
    ...
}

However, udmabuf_pin_folios() processes each item individually based on its original size and writes sequentially into ubuf->pages. This could massively overflow the undersized allocation:

drivers/dma-buf/udmabuf.c:udmabuf_pin_folios() {
    ...
    ubuf->pages[upgcnt] = folio_page(folios[cur_folio],
                                     subpgoff >> PAGE_SHIFT);
    ++upgcnt;
    ...
}

Could this lead to a heap buffer overflow?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Robert Mader

Hi Robert,

Quoted 19 lines

Subject: [PATCH v1] dma-buf/udmabuf: Disable the size limit by default

As udmabuf increasingly enjoys popularity - being used in projects like libcamera, Gstreamer, Mesa, KWin and Weston - users more frequently encounter cases where the current default size limit of 64MB is too low. Examples include allocating video buffers at a 8K resolution - and even 4K is affected when using non-subsampled video formats and high bit depths.

In its current form the size limit for individual buffers does not seem to provide any additional level of protection - such as limiting the amount of memory a process can pin - as the later can just allocate multiple buffers. If additional guardrails are desired, they would likely require some kind accounting not limited to individual buffers.

Therefor let's disable the size limit by default. Use the special value of zero to do so, which prevously could be used to effectively disable the interface. Using other means, such as file permissions, appears to be a much better fit for that purpose.

Quoted 7 lines

---

Please let me know if changing the meaning of the parameter value of zero is considered a breaking change / not acceptable. In that case INT_MAX might be a better option.

Yeah, I think using INT_MAX might be better.

Thanks,
Vivek

Quoted 45 lines

See
https://lore.kernel.org/dri-devel/20260711144814.8205-1-
[email protected]/
for a previous attempt to make the value configurable via kconfig - and in particular
https://lore.kernel.org/dri-devel/6764ca6f-b4d8-4baa-9d27-
[email protected]/
for the suggestion and discussion to remove the default limit.

---
 drivers/dma-buf/udmabuf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/dma-buf/udmabuf.c +2 -2
index bced421c0d65..3509b690d8e2 100644
@@ -20,9 +20,9 @@ static int list_limit = 1024;
 module_param(list_limit, int, 0644);
 MODULE_PARM_DESC(list_limit, "udmabuf_create_list->count limit.
 Default is 1024.");
 
-static int size_limit_mb = 64;
+static int size_limit_mb = 0;
 module_param(size_limit_mb, int, 0644);
-MODULE_PARM_DESC(size_limit_mb, "Max size of a dmabuf, in
 megabytes. Default is 64.");
+MODULE_PARM_DESC(size_limit_mb, "Max size of a dmabuf, in
 megabytes. Setting 0 disables the limit. Default is 0.");

struct udmabuf {

drivers/dma-buf/udmabuf.c +1 -1
 	pgoff_t pagecount;
@@ -373,7 +373,7 @@ static long udmabuf_create(struct miscdevice
 *device,
 
 		subpgcnt = list[i].size >> PAGE_SHIFT;
 		pgcnt += subpgcnt;
-		if (pgcnt > pglimit)
+		if (pglimit && pglimit < pgcnt)
 			goto err_noinit;
 

max_nr_folios = max_t(unsigned long, subpgcnt,
max_nr_folios);
--
2.55.0

#3 lore raw

Hi Vivek,

Quoted 21 lines · Vivek

Hi Robert,

Quoted 19 lines

Subject: [PATCH v1] dma-buf/udmabuf: Disable the size limit by default

As udmabuf increasingly enjoys popularity - being used in projects like libcamera, Gstreamer, Mesa, KWin and Weston - users more frequently encounter cases where the current default size limit of 64MB is too low. Examples include allocating video buffers at a 8K resolution - and even 4K is affected when using non-subsampled video formats and high bit depths.

In its current form the size limit for individual buffers does not seem to provide any additional level of protection - such as limiting the amount of memory a process can pin - as the later can just allocate multiple buffers. If additional guardrails are desired, they would likely require some kind accounting not limited to individual buffers.

Therefor let's disable the size limit by default. Use the special value of zero to do so, which prevously could be used to effectively disable the interface. Using other means, such as file permissions, appears to be a much better fit for that purpose.

Quoted 8 lines
Quoted 7 lines

---

Please let me know if changing the meaning of the parameter value of zero is considered a breaking change / not acceptable. In that case INT_MAX might be a better option.

Yeah, I think using INT_MAX might be better.

did that in v2, thanks!

Quoted 48 lines

Thanks,
Vivek

Quoted 44 lines

See
https://lore.kernel.org/dri-devel/20260711144814.8205-1-
[email protected]/
for a previous attempt to make the value configurable via kconfig - and in particular
https://lore.kernel.org/dri-devel/6764ca6f-b4d8-4baa-9d27-
[email protected]/
for the suggestion and discussion to remove the default limit.

---
  drivers/dma-buf/udmabuf.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/dma-buf/udmabuf.c +2 -2
index bced421c0d65..3509b690d8e2 100644
@@ -20,9 +20,9 @@ static int list_limit = 1024;
  module_param(list_limit, int, 0644);
  MODULE_PARM_DESC(list_limit, "udmabuf_create_list->count limit.
 Default is 1024.");
 
-static int size_limit_mb = 64;
+static int size_limit_mb = 0;
  module_param(size_limit_mb, int, 0644);
-MODULE_PARM_DESC(size_limit_mb, "Max size of a dmabuf, in
 megabytes. Default is 64.");
+MODULE_PARM_DESC(size_limit_mb, "Max size of a dmabuf, in
 megabytes. Setting 0 disables the limit. Default is 0.");
  struct udmabuf {
  	pgoff_t pagecount;
drivers/dma-buf/udmabuf.c +1 -1
@@ -373,7 +373,7 @@ static long udmabuf_create(struct miscdevice
 *device,
 
  		subpgcnt = list[i].size >> PAGE_SHIFT;
  		pgcnt += subpgcnt;
-		if (pgcnt > pglimit)
+		if (pglimit && pglimit < pgcnt)
  			goto err_noinit;
 

max_nr_folios = max_t(unsigned long, subpgcnt,
max_nr_folios);
--
2.55.0

Signature · 6 lines
Robert Mader
Consultant Software Developer

Collabora Ltd.
Platinum Building, St John's Innovation Park, Cambridge CB4 0DS, UK
Registered in England & Wales, no. 5513718