Skip to contentSkip to conversation

Fix /proc/pid/pagemap return length calculation

PATCH In review Dave Boutcher 2 participants 3 messages Aug 6, 2007

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

PATCH 1/1 Fix /proc/pid/pagemap return length calculation

/proc/pid/pagemap has a header (usually 8 bytes) the length of which needs to be compensated for when converting from proc file offset to page number. The calculation of the starting page number (svpfn) compensates for this, but the calculation of the ending page number (evpfn) does not, resulting in reads returning 8 bytes more than were asked for and nastily overwriting userspace memory.

Diffed against 2.6.23-rc1-mm2

---
 fs/proc/task_mmu.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
fs/proc/task_mmu.c +1 -1
index 4594f15..b2baeab 100644
@@ -627,7 +627,7 @@ static ssize_t pagemap_read(struct file *file, char __user *buf,
 	addr = PAGE_SIZE * svpfn;
 	if ((svpfn + 1) * sizeof(unsigned long) != src)
 		goto out;
-	evpfn = min((src + count) / sizeof(unsigned long),
+	evpfn = min((src + count) / sizeof(unsigned long) - 1,
 		    ((~0UL) >> PAGE_SHIFT) + 1);
 	count = (evpfn - svpfn) * sizeof(unsigned long);
 	end = PAGE_SIZE * evpfn;
Signature · 1 line
1.4.4.2
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Dave Boutcher

/proc/pid/pagemap has a header (usually 8 bytes) the length of which needs to be compensated for when converting from proc file offset to page number. The calculation of the starting page number (svpfn) compensates for this, but the calculation of the ending page number (evpfn) does not, resulting in reads returning 8 bytes more than were asked for and nastily overwriting userspace memory.

Does this mean you're running on a 64-bit arch? I'd already fixed this locally, but it was off by 4 for me.

Quoted 22 lines

Diffed against 2.6.23-rc1-mm2

---
 fs/proc/task_mmu.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
fs/proc/task_mmu.c +1 -1
index 4594f15..b2baeab 100644
@@ -627,7 +627,7 @@ static ssize_t pagemap_read(struct file *file, char __user *buf,
 	addr = PAGE_SIZE * svpfn;
 	if ((svpfn + 1) * sizeof(unsigned long) != src)
 		goto out;
-	evpfn = min((src + count) / sizeof(unsigned long),
+	evpfn = min((src + count) / sizeof(unsigned long) - 1,
 		    ((~0UL) >> PAGE_SHIFT) + 1);
 	count = (evpfn - svpfn) * sizeof(unsigned long);
 	end = PAGE_SIZE * evpfn;
Signature · 1 line
1.4.4.2
Signature · 1 line
Mathematics is the supreme nostalgia of our time.
#2 lore raw
Quoted 11 lines · Matt Mackall
Quoted 8 lines · Dave Boutcher

/proc/pid/pagemap has a header (usually 8 bytes) the length of which needs to be compensated for when converting from proc file offset to page number. The calculation of the starting page number (svpfn) compensates for this, but the calculation of the ending page number (evpfn) does not, resulting in reads returning 8 bytes more than were asked for and nastily overwriting userspace memory.

Quoted 4 lines

Does this mean you're running on a 64-bit arch? I'd already fixed this locally, but it was off by 4 for me.

Yeah, and there is going to be at least one more patch coming, since with this fix, which is a righteous fix, things don't get copied up to user space correctly since some other code was dependent on the borken length :-)

I like the /proc/xxx/pagemap function though...thanks for writing it.

Dave B