[RESEND PATCH v1] proc: use untagged_addr() for pagemap_read addresses

Miles Chen miles.chen at mediatek.com
Thu Nov 26 02:49:06 EST 2020


On Thu, 2020-11-26 at 07:16 +0000, Song Bao Hua (Barry Song) wrote:
> 
> > -----Original Message-----
> > From: Miles Chen [mailto:miles.chen at mediatek.com]
> > Sent: Monday, November 23, 2020 7:39 PM
> > To: Alexey Dobriyan <adobriyan at gmail.com>; Andrew Morton
> > <akpm at linux-foundation.org>
> > Cc: linux-kernel at vger.kernel.org; linux-fsdevel at vger.kernel.org;
> > linux-mediatek at lists.infradead.org; wsd_upstream at mediatek.com; Miles
> > Chen <miles.chen at mediatek.com>
> > Subject: [RESEND PATCH v1] 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.
> > 
> > My test code is baed on [1]:
> > 
> > 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://github.com/stressapptest/stressapptest/blob/master/src/os.cc#L158
> > 
> > Signed-off-by: Miles Chen <miles.chen at mediatek.com>
> > ---
> >  fs/proc/task_mmu.c | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
> > index 217aa2705d5d..e9a70f7ee515 100644
> > --- a/fs/proc/task_mmu.c
> > +++ b/fs/proc/task_mmu.c
> > @@ -1599,11 +1599,11 @@ static ssize_t pagemap_read(struct file *file, char
> > __user *buf,
> > 
> >  	src = *ppos;
> >  	svpfn = src / PM_ENTRY_BYTES;
> > -	start_vaddr = svpfn << PAGE_SHIFT;
> > +	start_vaddr = untagged_addr(svpfn << PAGE_SHIFT);
> >  	end_vaddr = mm->task_size;
> > 
> >  	/* watch out for wraparound */
> > -	if (svpfn > mm->task_size >> PAGE_SHIFT)
> > +	if (start_vaddr > mm->task_size)
> >  		start_vaddr = end_vaddr;
> 
> Wouldn't the untag be done by the user reading pagemap file?
> With this patch, even users pass an illegal address, for example,
> users put a tag on a virtual address which hasn't really a tag,
> they will still get the right pagemap.
> 


I run arm64 devices.
If the tagged pointer is enabled, the ARM top-byte Ignore is also
enabled. It means the top-byte is always be ignored.

I think the kernel should not break existing userspace program, so it's
better to handle the tagged user pointer in kernel.

grep 'untag' mm -RnH to see existing examples.


thanks,
Miles

> > 
> >  	/*
> > --
> > 2.18.0
> 
> Thanks
> Barry
> 




More information about the Linux-mediatek mailing list