Skip to contentSkip to conversation

mm/vmalloc: reject overflowing vm_map_ram page counts

PATCH In review Shubhang Kaushik (Ampere) 3 participants 5 messages Oct 23 – Nov 10

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm/vmalloc: reject overflowing vm_map_ram page counts

vm_map_ram() accepts a page count and must either map that many pages or fail. It currently converts the count to a byte size with:

(unsigned long)count << PAGE_SHIFT

On 32-bit kernels, this conversion overflows when count is greater than ULONG_MAX >> PAGE_SHIFT. vm_map_ram() then allocates and maps only the wrapped byte size, but returns a non-NULL address to its caller.

For example, with 4 KiB pages, a count of 1048577 wraps to PAGE_SIZE. vm_map_ram() therefore returns a mapping containing one page instead of the requested 1048577 pages.

If a caller uses the successful result as a mapping of all requested pages, it can access beyond the allocated mapping. Such an access can fault. If the accessed virtual address is occupied by another vmalloc mapping, it can instead access and corrupt unrelated kernel memory. In a userspace-reachable call path, this can result in a failed operation, a kernel oops or panic, or the effects of corrupted kernel data.

Return NULL before allocating or mapping anything when count * PAGE_SIZE cannot be represented in an unsigned long.

Fixes db64fe02258f ("mm: rewrite vmap layer")

---
Changes in v2:
- Use check_mul_overflow() to validate the page-count-to-byte conversion.
- Describe the short-mapping failure mode and its worst-case effects.

Link to v1: https://lore.kernel.org/r/[email protected]

---
 mm/vmalloc.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/vmalloc.c +4 -1
index bea9f76ed7e74247d4ce919250c2f02dd5c36483..8a056e424a4f2c037c10f736002839014f1f57dc 100644
@@ -3083,10 +3083,13 @@ EXPORT_SYMBOL(vm_unmap_ram);
  */
 void *vm_map_ram(struct page **pages, unsigned int count, int node)
 {
-	unsigned long size = (unsigned long)count << PAGE_SHIFT;
+	unsigned long size;
 	unsigned long addr;
 	void *mem;
 
+	if (check_mul_overflow((unsigned long)count, PAGE_SIZE, &size))
+		return NULL;
+
 	if (likely(count <= VMAP_MAX_ALLOC)) {
 		mem = vb_alloc(size, GFP_KERNEL);
 		if (IS_ERR(mem))

---
base-commit: 734bef548a96e8774d6eef5dfa2026c6edc4e80b
change-id: 20261022-vmalloc-reject-overflow-e532c556f3fa

Best regards,

Signature · 1 line
Shubhang Kaushik (Ampere) <[email protected]>
maintainer
#1 lore raw
Quoted 20 lines · On Thu, 22 Oct 2026 23:43:47 -0700 "Shubhang Kaushik (Ampere)" <[email protected]>…

vm_map_ram() accepts a page count and must either map that many pages or fail. It currently converts the count to a byte size with:

(unsigned long)count << PAGE_SHIFT

On 32-bit kernels, this conversion overflows when count is greater than ULONG_MAX >> PAGE_SHIFT. vm_map_ram() then allocates and maps only the wrapped byte size, but returns a non-NULL address to its caller.

For example, with 4 KiB pages, a count of 1048577 wraps to PAGE_SIZE. vm_map_ram() therefore returns a mapping containing one page instead of the requested 1048577 pages.

If a caller uses the successful result as a mapping of all requested pages, it can access beyond the allocated mapping. Such an access can fault. If the accessed virtual address is occupied by another vmalloc mapping, it can instead access and corrupt unrelated kernel memory. In a userspace-reachable call path, this can result in a failed operation, a kernel oops or panic, or the effects of corrupted kernel data.

Quoted 2 lines

Return NULL before allocating or mapping anything when count * PAGE_SIZE cannot be represented in an unsigned long.

Thanks. Let's cc the developers who commented on the v1 patch.

Quoted 40 lines
Fixes db64fe02258f ("mm: rewrite vmap layer")

---
Changes in v2:
- Use check_mul_overflow() to validate the page-count-to-byte conversion.
- Describe the short-mapping failure mode and its worst-case effects.

Link to v1: https://lore.kernel.org/r/[email protected]

---
 mm/vmalloc.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/vmalloc.c +4 -2
index bea9f76ed7e74247d4ce919250c2f02dd5c36483..8a056e424a4f2c037c10f736002839014f1f57dc 100644
@@ -3083,10 +3083,13 @@ EXPORT_SYMBOL(vm_unmap_ram);
  */
 void *vm_map_ram(struct page **pages, unsigned int count, int node)
 {
-	unsigned long size = (unsigned long)count << PAGE_SHIFT;
+	unsigned long size;
 	unsigned long addr;
 	void *mem;
 
+	if (check_mul_overflow((unsigned long)count, PAGE_SIZE, &size))
+		return NULL;
+
 	if (likely(count <= VMAP_MAX_ALLOC)) {
 		mem = vb_alloc(size, GFP_KERNEL);
 		if (IS_ERR(mem))
 
---

base-commit: 734bef548a96e8774d6eef5dfa2026c6edc4e80b
change-id: 20261022-vmalloc-reject-overflow-e532c556f3fa

Best regards,

Signature · 1 line
Shubhang Kaushik (Ampere) <[email protected]>
Reviewed-by on v2 1/1
#2 lore raw
In reply to Shubhang Kaushik (Ampere)
Quoted 20 lines · Shubhang Kaushik (Ampere)

vm_map_ram() accepts a page count and must either map that many pages or fail. It currently converts the count to a byte size with:

(unsigned long)count << PAGE_SHIFT

On 32-bit kernels, this conversion overflows when count is greater than ULONG_MAX >> PAGE_SHIFT. vm_map_ram() then allocates and maps only the wrapped byte size, but returns a non-NULL address to its caller.

For example, with 4 KiB pages, a count of 1048577 wraps to PAGE_SIZE. vm_map_ram() therefore returns a mapping containing one page instead of the requested 1048577 pages.

If a caller uses the successful result as a mapping of all requested pages, it can access beyond the allocated mapping. Such an access can fault. If the accessed virtual address is occupied by another vmalloc mapping, it can instead access and corrupt unrelated kernel memory. In a userspace-reachable call path, this can result in a failed operation, a kernel oops or panic, or the effects of corrupted kernel data.

Quoted 7 lines

Return NULL before allocating or mapping anything when count * PAGE_SIZE cannot be represented in an unsigned long.

Fixes db64fe02258f ("mm: rewrite vmap layer")

---

I don't have much knowledge about 32-bit stuff but upon some discussion with LLM the problem seems legit, maybe just hard to reproduce. Also there are sufficient users of vm_map_ram() to care about this:

Quoted 34 lines

Changes in v2:
- Use check_mul_overflow() to validate the page-count-to-byte conversion.
- Describe the short-mapping failure mode and its worst-case effects.

Link to v1: https://lore.kernel.org/r/[email protected]

---
 mm/vmalloc.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/vmalloc.c +4 -2
index bea9f76ed7e74247d4ce919250c2f02dd5c36483..8a056e424a4f2c037c10f736002839014f1f57dc 100644
@@ -3083,10 +3083,13 @@ EXPORT_SYMBOL(vm_unmap_ram);
  */
 void *vm_map_ram(struct page **pages, unsigned int count, int node)
 {
-	unsigned long size = (unsigned long)count << PAGE_SHIFT;
+	unsigned long size;
 	unsigned long addr;
 	void *mem;
 
+	if (check_mul_overflow((unsigned long)count, PAGE_SIZE, &size))
+		return NULL;
+
 	if (likely(count <= VMAP_MAX_ALLOC)) {
 		mem = vb_alloc(size, GFP_KERNEL);
 		if (IS_ERR(mem))
 
---

base-commit: 734bef548a96e8774d6eef5dfa2026c6edc4e80b
change-id: 20261022-vmalloc-reject-overflow-e532c556f3fa

Best regards,

maintainer
#3 lore raw
In reply to Shubhang Kaushik (Ampere)
Quoted 20 lines · On Thu, 22 Oct 2026 23:43:47 -0700 "Shubhang Kaushik (Ampere)" <[email protected]>…

vm_map_ram() accepts a page count and must either map that many pages or fail. It currently converts the count to a byte size with:

(unsigned long)count << PAGE_SHIFT

On 32-bit kernels, this conversion overflows when count is greater than ULONG_MAX >> PAGE_SHIFT. vm_map_ram() then allocates and maps only the wrapped byte size, but returns a non-NULL address to its caller.

For example, with 4 KiB pages, a count of 1048577 wraps to PAGE_SIZE. vm_map_ram() therefore returns a mapping containing one page instead of the requested 1048577 pages.

If a caller uses the successful result as a mapping of all requested pages, it can access beyond the allocated mapping. Such an access can fault. If the accessed virtual address is occupied by another vmalloc mapping, it can instead access and corrupt unrelated kernel memory. In a userspace-reachable call path, this can result in a failed operation, a kernel oops or panic, or the effects of corrupted kernel data.

Quoted 2 lines

Return NULL before allocating or mapping anything when count * PAGE_SIZE cannot be represented in an unsigned long.

Thanks.

Is there a case where userspace can actually hit this, or is it a theoretical case which can't actually be encountered, or something else?

Quoted 18 lines
mm/vmalloc.c +4 -1
@@ -3083,10 +3083,13 @@ EXPORT_SYMBOL(vm_unmap_ram);
  */
 void *vm_map_ram(struct page **pages, unsigned int count, int node)
 {
-	unsigned long size = (unsigned long)count << PAGE_SHIFT;
+	unsigned long size;
 	unsigned long addr;
 	void *mem;
 
+	if (check_mul_overflow((unsigned long)count, PAGE_SIZE, &size))
+		return NULL;
+
 	if (likely(count <= VMAP_MAX_ALLOC)) {
 		mem = vb_alloc(size, GFP_KERNEL);
 		if (IS_ERR(mem))
author
#4 lore raw

Hi Andrew,

Quoted 4 lines · Andrew Morton

Is there a case where userspace can actually hit this, or is it a theoretical case which can't actually be encountered, or something else?

Reaching vm_map_ram() with an overflowing count from userspace is real (as noted in the udmabuf path earlier), but actually hitting an oops or corruption remains theoretical because in-tree callers don't access beyond the truncated size.

The changelog described what happens if a caller does access the full
requested range. I can send a v3 clarifying that the failure mode is theoretical if you'd prefer.

Thanks,
Shubhang Kaushik