Skip to contentSkip to conversation

mm, pagemap: Fix swap offset value for PMD migration entry

PATCH In review Huang, Ying 3 participants 6 messages Apr 8, 2018 – Apr 11, 2018

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

PATCH 1/1 mm, pagemap: Fix swap offset value for PMD migration entry

From: Huang Ying <[email protected]>

The swap offset reported by /proc/<pid>/pagemap may be not correct for PMD migration entry. If addr passed into pagemap_range() isn't aligned with PMD start address, the swap offset reported doesn't reflect this. And in the loop to report information of each sub-page, the swap offset isn't increased accordingly as that for PFN.

BTW: migration swap entries have PFN information, do we need to restrict whether to show them?

Cc Michal Hocko <[email protected]>
Cc "Kirill A. Shutemov" <[email protected]>
Cc Andrei Vagin <[email protected]>
Cc Dan Williams <[email protected]>
Cc "Jerome Glisse" <[email protected]>
Cc Daniel Colascione <[email protected]>
Cc Zi Yan <[email protected]>
Cc Naoya Horiguchi <[email protected]>
---
 fs/proc/task_mmu.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/proc/task_mmu.c +5 -1
index 65ae54659833..757e748da613 100644
@@ -1310,9 +1310,11 @@ static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
 		else if (is_swap_pmd(pmd)) {
 			swp_entry_t entry = pmd_to_swp_entry(pmd);
+			unsigned long offset = swp_offset(entry);
 
+			offset += (addr & ~PMD_MASK) >> PAGE_SHIFT;
 			frame = swp_type(entry) |
-				(swp_offset(entry) << MAX_SWAPFILES_SHIFT);
+				(offset << MAX_SWAPFILES_SHIFT);
 			flags |= PM_SWAP;
 			if (pmd_swp_soft_dirty(pmd))
 				flags |= PM_SOFT_DIRTY;
@@ -1332,6 +1334,8 @@ static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
 				break;
 			if (pm->show_pfn && (flags & PM_PRESENT))
 				frame++;
+			else if (flags | PM_SWAP)
+				frame += (1 << MAX_SWAPFILES_SHIFT);
 		}
 		spin_unlock(ptl);
 		return err;
Signature · 1 line
2.15.1
maintainer
#1 lore raw
Quoted 4 lines · Ying

From: Huang Ying <[email protected]>

The swap offset reported by /proc/<pid>/pagemap may be not correct for PMD migration entry. If addr passed into pagemap_range() isn't

pagemap_pmd_range(), yes?

Quoted 1 line

aligned with PMD start address,

How can this situation come about?

Quoted 6 lines

the swap offset reported doesn't
reflect this. And in the loop to report information of each sub-page, the swap offset isn't increased accordingly as that for PFN.

BTW: migration swap entries have PFN information, do we need to restrict whether to show them?

For what reason? Address obfuscation?

author Acked-by Konstantin Khlebnikov Acked-by Andy Lutomirski
#2 lore raw
Quoted 8 lines · Andrew Morton
Quoted 4 lines · Ying

From: Huang Ying <[email protected]>

The swap offset reported by /proc/<pid>/pagemap may be not correct for PMD migration entry. If addr passed into pagemap_range() isn't

pagemap_pmd_range(), yes?

Yes. Sorry for typo.

Quoted 3 lines
Quoted 1 line

aligned with PMD start address,

How can this situation come about?

After open /proc/<pid>/pagemap, if user seeks to a page whose address doesn't aligned with PMD start address. I have verified this with a simple test program.

Quoted 8 lines
Quoted 6 lines

the swap offset reported doesn't
reflect this. And in the loop to report information of each sub-page, the swap offset isn't increased accordingly as that for PFN.

BTW: migration swap entries have PFN information, do we need to restrict whether to show them?

For what reason? Address obfuscation?

This is an existing feature for PFN report of /proc/<pid>/pagemap, reason is in following commit log. I am wondering whether that is necessary for migration swap entries too.

ab676b7d6fbf4b294bf198fb27ade5b0e865c7ce

Author:     Kirill A. Shutemov <[email protected]>
AuthorDate: Mon Mar 9 23:11:12 2015 +0200
Commit:     Linus Torvalds <[email protected]>
CommitDate: Tue Mar 17 09:31:30 2015 -0700

pagemap: do not leak physical addresses to non-privileged userspace

As pointed by recent post[1] on exploiting DRAM physical imperfection, /proc/PID/pagemap exposes sensitive information which can be used to do attacks.

This disallows anybody without CAP_SYS_ADMIN to read the pagemap.

[1] http://googleprojectzero.blogspot.com/2015/03/exploiting-dram-rowhammer-bug-to-gain.html

[ Eventually we might want to do anything more finegrained, but for now
this is the simple model. - Linus ]

Best Regards,
Huang, Ying

#5 lore raw
In reply to Huang, Ying

Hi, Andrew,

Quoted 42 lines · Ying

From: Huang Ying <[email protected]>

The swap offset reported by /proc/<pid>/pagemap may be not correct for PMD migration entry. If addr passed into pagemap_range() isn't aligned with PMD start address, the swap offset reported doesn't reflect this. And in the loop to report information of each sub-page, the swap offset isn't increased accordingly as that for PFN.

BTW: migration swap entries have PFN information, do we need to restrict whether to show them?

Cc Michal Hocko <[email protected]>
Cc "Kirill A. Shutemov" <[email protected]>
Cc Andrei Vagin <[email protected]>
Cc Dan Williams <[email protected]>
Cc "Jerome Glisse" <[email protected]>
Cc Daniel Colascione <[email protected]>
Cc Zi Yan <[email protected]>
Cc Naoya Horiguchi <[email protected]>
---
 fs/proc/task_mmu.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/proc/task_mmu.c +3 -1
index 65ae54659833..757e748da613 100644
@@ -1310,9 +1310,11 @@ static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
 		else if (is_swap_pmd(pmd)) {
 			swp_entry_t entry = pmd_to_swp_entry(pmd);
+			unsigned long offset = swp_offset(entry);
 
+			offset += (addr & ~PMD_MASK) >> PAGE_SHIFT;
 			frame = swp_type(entry) |
-				(swp_offset(entry) << MAX_SWAPFILES_SHIFT);
+				(offset << MAX_SWAPFILES_SHIFT);
 			flags |= PM_SWAP;
 			if (pmd_swp_soft_dirty(pmd))
 				flags |= PM_SOFT_DIRTY;

fs/proc/task_mmu.c static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end, · line 1337

Quoted 4 lines
fs/proc/task_mmu.c +1 -0
@@ -1332,6 +1334,8 @@ static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
 				break;
 			if (pm->show_pfn && (flags & PM_PRESENT))
 				frame++;
+			else if (flags | PM_SWAP)

Oops, I just found a typo here, it should be,

+ else if (flags & PM_SWAP)

Sorry about that. Do I need to refresh the patch or you will fix it inline?

Best Regards,
Huang, Ying

Quoted 4 lines
fs/proc/task_mmu.c +1 -0
+				frame += (1 << MAX_SWAPFILES_SHIFT);
 		}
 		spin_unlock(ptl);
 		return err;
#3 lore raw
In reply to Huang, Ying
Quoted 25 lines · Ying
Quoted 8 lines
Quoted 6 lines

the swap offset reported doesn't
reflect this. And in the loop to report information of each sub-page, the swap offset isn't increased accordingly as that for PFN.

BTW: migration swap entries have PFN information, do we need to restrict whether to show them?

For what reason? Address obfuscation?

This is an existing feature for PFN report of /proc/<pid>/pagemap, reason is in following commit log. I am wondering whether that is necessary for migration swap entries too.

ab676b7d6fbf4b294bf198fb27ade5b0e865c7ce

Author:     Kirill A. Shutemov <[email protected]>
AuthorDate: Mon Mar 9 23:11:12 2015 +0200
Commit:     Linus Torvalds <[email protected]>
CommitDate: Tue Mar 17 09:31:30 2015 -0700

pagemap: do not leak physical addresses to non-privileged userspace

As pointed by recent post[1] on exploiting DRAM physical imperfection, /proc/PID/pagemap exposes sensitive information which can be used to do attacks.

Quoted 6 lines

This disallows anybody without CAP_SYS_ADMIN to read the pagemap.

[1] http://googleprojectzero.blogspot.com/2015/03/exploiting-dram-rowhammer-bug-to-gain.html

[ Eventually we might want to do anything more finegrained, but for now
this is the simple model. - Linus ]

Note that there's follow up to the commit:

1c90308e7a77 ("pagemap: hide physical addresses from non-privileged users")

It introduces pm->show_pfn and it should be applied to swap entries too.

Signature · 1 line
 Kirill A. Shutemov
#4 lore raw
Quoted 30 lines · Kirill A. Shutemov
Quoted 29 lines · Ying
Quoted 8 lines
Quoted 6 lines

the swap offset reported doesn't
reflect this. And in the loop to report information of each sub-page, the swap offset isn't increased accordingly as that for PFN.

BTW: migration swap entries have PFN information, do we need to restrict whether to show them?

For what reason? Address obfuscation?

This is an existing feature for PFN report of /proc/<pid>/pagemap, reason is in following commit log. I am wondering whether that is necessary for migration swap entries too.

ab676b7d6fbf4b294bf198fb27ade5b0e865c7ce

Author:     Kirill A. Shutemov <[email protected]>
AuthorDate: Mon Mar 9 23:11:12 2015 +0200
Commit:     Linus Torvalds <[email protected]>
CommitDate: Tue Mar 17 09:31:30 2015 -0700

pagemap: do not leak physical addresses to non-privileged userspace

As pointed by recent post[1] on exploiting DRAM physical imperfection, /proc/PID/pagemap exposes sensitive information which can be used to do attacks.

This disallows anybody without CAP_SYS_ADMIN to read the pagemap.

[1] http://googleprojectzero.blogspot.com/2015/03/exploiting-dram-rowhammer-bug-to-gain.html

Quoted 8 lines
Quoted 2 lines

[ Eventually we might want to do anything more finegrained, but for now
this is the simple model. - Linus ]

Note that there's follow up to the commit:

1c90308e7a77 ("pagemap: hide physical addresses from non-privileged users")

It introduces pm->show_pfn and it should be applied to swap entries too.

So you think we should hide all swap entry information if (!pm->show_pfn) regardless they are migration swap entries or not?

Best Regards,
Huang, Ying