Skip to contentSkip to conversation

proc: use untagged_addr() for pagemap_read addresses

PATCH Superseded Miles Chen 3 participants 5 messages Nov 27, 2020 – Dec 4, 2020

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Miles Chen

author
#0 lore raw

PATCH v2 1/1 proc: use untagged_addr() for pagemap_read addresses

When we try to visit the pagemap of a tagged userspace pointer, we find that the start_vaddr is not correct because of the tag.
To fix it, we should untag the usespace pointers in pagemap_read().

I tested with 5.10-rc4 and the issue remains.

Explaination from Catalin in [1]:

"
Arguably, that's a user-space bug since tagged file offsets were never supported. In this case it's not even a tag at bit 56 as per the arm64 tagged address ABI but rather down to bit 47. You could say that the problem is caused by the C library (malloc()) or whoever created the tagged vaddr and passed it to this function. It's not a kernel regression as we've never supported it.

Now, pagemap is a special case where the offset is usually not generated as a classic file offset but rather derived by shifting a user virtual address. I guess we can make a concession for pagemap (only) and allow such offset with the tag at bit (56 - PAGE_SHIFT + 3).
"

My test code is baed on [2]:

A userspace pointer which has been tagged by 0xb4: 0xb400007662f541c8

=== userspace program ===

uint64 OsLayer::VirtualToPhysical(void *vaddr) {
	uint64 frame, paddr, pfnmask, pagemask;
	int pagesize = sysconf(_SC_PAGESIZE);
	off64_t off = ((uintptr_t)vaddr) / pagesize * 8; // off = 0xb400007662f541c8 / pagesize * 8 = 0x5a00003b317aa0
	int fd = open(kPagemapPath, O_RDONLY);
	...

	if (lseek64(fd, off, SEEK_SET) != off || read(fd, &frame, 8) != 8) {
		int err = errno;
		string errtxt = ErrorString(err);
		if (fd >= 0)
			close(fd);
		return 0;
	}

...
}

=== kernel fs/proc/task_mmu.c ===

static ssize_t pagemap_read(struct file *file, char __user *buf,
size_t count, loff_t *ppos)

{
	...
	src = *ppos;
	svpfn = src / PM_ENTRY_BYTES; // svpfn == 0xb400007662f54
	start_vaddr = svpfn << PAGE_SHIFT; // start_vaddr == 0xb400007662f54000
	end_vaddr = mm->task_size;

	/* watch out for wraparound */
	// svpfn == 0xb400007662f54
	// (mm->task_size >> PAGE) == 0x8000000
	if (svpfn > mm->task_size >> PAGE_SHIFT) // the condition is true because of the tag 0xb4
		start_vaddr = end_vaddr;

	ret = 0;
	while (count && (start_vaddr < end_vaddr)) { // we cannot visit correct entry because start_vaddr is set to end_vaddr
		int len;
		unsigned long end;
		...
	}
	...
}

[1] https://lore.kernel.org/patchwork/patch/1343258/
[2] https://github.com/stressapptest/stressapptest/blob/master/src/os.cc#L158

Cc Andrew Morton <[email protected]>
Cc Alexey Dobriyan <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Alexander Potapenko <[email protected]>
Cc Vincenzo Frascino <[email protected]>
Cc Andrey Ryabinin <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Marco Elver <[email protected]>
Cc Will Deacon <[email protected]>
Cc Eric W. Biederman <[email protected]>
Cc Song Bao Hua (Barry Song) <[email protected]>

---

Change since v1:

1. Follow Eirc's and Catalin's suggestion to avoid overflow
2. Cc to stable v5.4-
3. add explaination from Catalin to the commit message

---
 fs/proc/task_mmu.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
fs/proc/task_mmu.c +6 -2
index 217aa2705d5d..92b277388f05 100644
@@ -1599,11 +1599,15 @@ static ssize_t pagemap_read(struct file *file, char __user *buf,
 
 	src = *ppos;
 	svpfn = src / PM_ENTRY_BYTES;
-	start_vaddr = svpfn << PAGE_SHIFT;
 	end_vaddr = mm->task_size;
 
 	/* watch out for wraparound */
-	if (svpfn > mm->task_size >> PAGE_SHIFT)
+	start_vaddr = end_vaddr;
+	if (svpfn < (ULONG_MAX >> PAGE_SHIFT))
+		start_vaddr = untagged_addr(svpfn << PAGE_SHIFT);
+
+	/* Ensure the address is inside the task */
+	if (start_vaddr > mm->task_size)
 		start_vaddr = end_vaddr;
 
 	/*
Signature · 1 line
2.18.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 1 line · Miles Chen
Cc Will Deacon <[email protected]>

That should be [email protected].

Quoted 5 lines
fs/proc/task_mmu.c +0 -0
index 217aa2705d5d..92b277388f05 100644

fs/proc/task_mmu.c static ssize_t pagemap_read(struct file *file, char __user *buf, · line 1606

Quoted 10 lines
fs/proc/task_mmu.c +2 -2
@@ -1599,11 +1599,15 @@ static ssize_t pagemap_read(struct file *file, char __user *buf,
 
 	src = *ppos;
 	svpfn = src / PM_ENTRY_BYTES;
-	start_vaddr = svpfn << PAGE_SHIFT;
 	end_vaddr = mm->task_size;
 
 	/* watch out for wraparound */
-	if (svpfn > mm->task_size >> PAGE_SHIFT)
+	start_vaddr = end_vaddr;
+	if (svpfn < (ULONG_MAX >> PAGE_SHIFT))

Does this need to be strict less-than? I think a less-than or equal would work better.

fs/proc/task_mmu.c static ssize_t pagemap_read(struct file *file, char __user *buf, · line 1611

Quoted 5 lines
fs/proc/task_mmu.c +4 -0
+		start_vaddr = untagged_addr(svpfn << PAGE_SHIFT);
+
+	/* Ensure the address is inside the task */
+	if (start_vaddr > mm->task_size)
 		start_vaddr = end_vaddr;

Otherwise the logic looks fine to me. With the above:

_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek

author
#2 lore raw
Quoted 4 lines · Catalin Marinas
Quoted 1 line · Miles Chen
Cc Will Deacon <[email protected]>

That should be [email protected].

ok, I will fix it and submit v3

Quoted 12 lines
Quoted 12 lines
fs/proc/task_mmu.c +0 -1
index 217aa2705d5d..92b277388f05 100644
@@ -1599,11 +1599,15 @@ static ssize_t pagemap_read(struct file *file, char __user *buf,
 
 	src = *ppos;
 	svpfn = src / PM_ENTRY_BYTES;
-	start_vaddr = svpfn << PAGE_SHIFT;
 	end_vaddr = mm->task_size;
 
Quoted 7 lines

fs/proc/task_mmu.c static ssize_t pagemap_read(struct file *file, char __user *buf, · line 1606

Quoted 4 lines
fs/proc/task_mmu.c +2 -1
 	/* watch out for wraparound */
-	if (svpfn > mm->task_size >> PAGE_SHIFT)
+	start_vaddr = end_vaddr;
+	if (svpfn < (ULONG_MAX >> PAGE_SHIFT))

Does this need to be strict less-than? I think a less-than or equal would work better.

Thanks, I will fix it and submit v3.

Quoted 10 lines

fs/proc/task_mmu.c static ssize_t pagemap_read(struct file *file, char __user *buf, · line 1611

Quoted 6 lines
fs/proc/task_mmu.c +4 -0
+		start_vaddr = untagged_addr(svpfn << PAGE_SHIFT);
+
+	/* Ensure the address is inside the task */
+	if (start_vaddr > mm->task_size)
 		start_vaddr = end_vaddr;

Otherwise the logic looks fine to me. With the above:

Reviewed-by on v2 1/1
#3 lore raw
In reply to Miles Chen

Hi Miles,

Quoted 4 lines · Miles Chen

When we try to visit the pagemap of a tagged userspace pointer, we find that the start_vaddr is not correct because of the tag.
To fix it, we should untag the usespace pointers in pagemap_read().

Nit: s/usespace/userspace/ (please search and replace all occurrences :) )

Quoted 4 lines

I tested with 5.10-rc4 and the issue remains.

Explaination from Catalin in [1]:

Nit: s/Explaination/Explanation/ (please search and replace all occurrences :) )

Quoted 8 lines

"
Arguably, that's a user-space bug since tagged file offsets were never supported. In this case it's not even a tag at bit 56 as per the arm64 tagged address ABI but rather down to bit 47. You could say that the problem is caused by the C library (malloc()) or whoever created the tagged vaddr and passed it to this function. It's not a kernel regression as we've never supported it.

Quoted 7 lines

Now, pagemap is a special case where the offset is usually not generated as a classic file offset but rather derived by shifting a user virtual address. I guess we can make a concession for pagemap (only) and allow such offset with the tag at bit (56 - PAGE_SHIFT + 3).
"

My test code is baed on [2]:

Nit: s/baed/based/ (please search and replace all occurrences :) )

[...]

Quoted 9 lines
---
 fs/proc/task_mmu.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
fs/proc/task_mmu.c +0 -0
index 217aa2705d5d..92b277388f05 100644

fs/proc/task_mmu.c static ssize_t pagemap_read(struct file *file, char __user *buf, · line 1606

Quoted 10 lines
fs/proc/task_mmu.c +2 -2
@@ -1599,11 +1599,15 @@ static ssize_t pagemap_read(struct file *file, char __user *buf,
 
 	src = *ppos;
 	svpfn = src / PM_ENTRY_BYTES;
-	start_vaddr = svpfn << PAGE_SHIFT;
 	end_vaddr = mm->task_size;
 
 	/* watch out for wraparound */
-	if (svpfn > mm->task_size >> PAGE_SHIFT)
+	start_vaddr = end_vaddr;
+	if (svpfn < (ULONG_MAX >> PAGE_SHIFT))

It seems that 'svpfn' should be less-then-equal (<=) '(ULONG_MAX >> PAGE_SHIFT)'. Is there any specific reason why this is not the case?

fs/proc/task_mmu.c static ssize_t pagemap_read(struct file *file, char __user *buf, · line 1613

Quoted 8 lines
fs/proc/task_mmu.c +4 -0
+		start_vaddr = untagged_addr(svpfn << PAGE_SHIFT);
+
+	/* Ensure the address is inside the task */
+	if (start_vaddr > mm->task_size)
 		start_vaddr = end_vaddr;
 
 	/*

Otherwise:

Signature · 7 lines
Regards,
Vincenzo

_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek
author
#4 lore raw
Quoted 8 lines · Vincenzo Frascino

Hi Miles,

Quoted 4 lines · Miles Chen

When we try to visit the pagemap of a tagged userspace pointer, we find that the start_vaddr is not correct because of the tag.
To fix it, we should untag the usespace pointers in pagemap_read().

Quoted 8 lines

Nit: s/usespace/userspace/ (please search and replace all occurrences :) )

Quoted 4 lines

I tested with 5.10-rc4 and the issue remains.

Explaination from Catalin in [1]:

Nit: s/Explaination/Explanation/ (please search and replace all occurrences :) )

thanks, I will fix this and push v3

Quoted 15 lines
Quoted 15 lines

"
Arguably, that's a user-space bug since tagged file offsets were never supported. In this case it's not even a tag at bit 56 as per the arm64 tagged address ABI but rather down to bit 47. You could say that the problem is caused by the C library (malloc()) or whoever created the tagged vaddr and passed it to this function. It's not a kernel regression as we've never supported it.

Now, pagemap is a special case where the offset is usually not generated as a classic file offset but rather derived by shifting a user virtual address. I guess we can make a concession for pagemap (only) and allow such offset with the tag at bit (56 - PAGE_SHIFT + 3).
"

Quoted 3 lines
Quoted 1 line

My test code is baed on [2]:

Nit: s/baed/based/ (please search and replace all occurrences :) )

thanks, I will fix this and push v3

Miles

Quoted 32 lines

[...]

Quoted 9 lines
---
 fs/proc/task_mmu.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
fs/proc/task_mmu.c +0 -0
index 217aa2705d5d..92b277388f05 100644

fs/proc/task_mmu.c static ssize_t pagemap_read(struct file *file, char __user *buf, · line 1606

Quoted 10 lines
fs/proc/task_mmu.c +2 -2
@@ -1599,11 +1599,15 @@ static ssize_t pagemap_read(struct file *file, char __user *buf,
 
 	src = *ppos;
 	svpfn = src / PM_ENTRY_BYTES;
-	start_vaddr = svpfn << PAGE_SHIFT;
 	end_vaddr = mm->task_size;
 
 	/* watch out for wraparound */
-	if (svpfn > mm->task_size >> PAGE_SHIFT)
+	start_vaddr = end_vaddr;
+	if (svpfn < (ULONG_MAX >> PAGE_SHIFT))

It seems that 'svpfn' should be less-then-equal (<=) '(ULONG_MAX >> PAGE_SHIFT)'. Is there any specific reason why this is not the case?

Quoted 6 lines
fs/proc/task_mmu.c +4 -0
+		start_vaddr = untagged_addr(svpfn << PAGE_SHIFT);
+
+	/* Ensure the address is inside the task */
+	if (start_vaddr > mm->task_size)
 		start_vaddr = end_vaddr;
 
Quoted 7 lines

fs/proc/task_mmu.c static ssize_t pagemap_read(struct file *file, char __user *buf, · line 1613

Quoted 2 lines
fs/proc/task_mmu.c +0 -0
 	/*

Otherwise:

_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek