Skip to contentSkip to conversation

mm/memory.c: make remap_pfn_range() reject unaligned addr

PATCH In review Kaiyu Zhang 2 participants 2 messages Jun 17, 2020

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

PATCH 1/1 mm/memory.c: make remap_pfn_range() reject unaligned addr

From: Alex Zhang <[email protected]>

This function implicitly assumes that the addr passed in is page aligned. A non page aligned addr could ultimately cause a kernel bug in remap_pte_range as the exit condition in the logic loop may never be satisfied. This patch documents the need for the requirement, as well as explicitly adding a check for it.

---
 mm/memory.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/memory.c +4 -1
index dc7f3543b1fd..9cb0a75f1555 100644
@@ -2081,7 +2081,7 @@ static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd,
 /**
  * remap_pfn_range - remap kernel memory to userspace
  * @vma: user vma to map to
- * @addr: target user address to start at
+ * @addr: target page aligned user address to start at
  * @pfn: page frame number of kernel physical memory address
  * @size: size of mapping area
  * @prot: page protection flags for this mapping
@@ -2100,6 +2100,9 @@ int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
 	unsigned long remap_pfn = pfn;
 	int err;
 
+	if (!PAGE_ALIGN(addr))
+		return -EINVAL;
+
 	/*
 	 * Physically remapped pages are special. Tell the
 	 * rest of the world about it:
Signature · 1 line
2.27.0.290.gba653c62da-goog
maintainer
#1 lore raw
Quoted 22 lines · On Wed, 17 Jun 2020 15:34:14 -0700 Kaiyu Zhang <[email protected]> wrote:

From: Alex Zhang <[email protected]>

This function implicitly assumes that the addr passed in is page aligned. A non page aligned addr could ultimately cause a kernel bug in remap_pte_range as the exit condition in the logic loop may never be satisfied. This patch documents the need for the requirement, as well as explicitly adding a check for it.

...

mm/memory.c +1 -1
@@ -2081,7 +2081,7 @@ static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd,
 /**
  * remap_pfn_range - remap kernel memory to userspace
  * @vma: user vma to map to
- * @addr: target user address to start at
+ * @addr: target page aligned user address to start at
  * @pfn: page frame number of kernel physical memory address
  * @size: size of mapping area
  * @prot: page protection flags for this mapping

mm/memory.c int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, · line 2105

Quoted 6 lines
mm/memory.c +3 -0
@@ -2100,6 +2100,9 @@ int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
 	unsigned long remap_pfn = pfn;
 	int err;
 
+	if (!PAGE_ALIGN(addr))
+		return -EINVAL;
+

That won't work. PAGE_ALIGNED() will do so.

Also, as this is an error in the calling code it would be better to do

	if (WARN_ON_ONCE(!PAGE_ALIGNED(addr)))
		return -EINVAL;

so that the offending code can be fixed up.

Is there any code in the kernel tree which actually has this error?