Skip to contentSkip to conversation

mm/swapfile: unuse_pte can map random data if swap read fails

PATCH Superseded Miaohe Lin 4 participants 6 messages Apr 1, 2022 – Apr 6, 2022

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Miaohe Lin

author
#0 lore raw

PATCH 1/1 mm/swapfile: unuse_pte can map random data if swap read fails

There is a bug in unuse_pte(): when swap page happens to be unreadable, page filled with random data is mapped into user address space. The fix is to check for PageUptodate and fail swapoff in case of error.

---
 mm/swapfile.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/swapfile.c +4 -0
index 63c61f8b2611..e72a35de7a0f 100644
@@ -1795,6 +1795,10 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
 		ret = 0;
 		goto out;
 	}
+	if (unlikely(!PageUptodate(page))) {
+		ret = -EIO;
+		goto out;
+	}
 
 	dec_mm_counter(vma->vm_mm, MM_SWAPENTS);
 	inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
Signature · 1 line
2.23.0
#1 lore raw
Quoted 14 lines · Miaohe Lin

There is a bug in unuse_pte(): when swap page happens to be unreadable, page filled with random data is mapped into user address space. The fix is to check for PageUptodate and fail swapoff in case of error.

---
 mm/swapfile.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/swapfile.c +0 -0
index 63c61f8b2611..e72a35de7a0f 100644

mm/swapfile.c static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, · line 1801

Quoted 7 lines
mm/swapfile.c +4 -0
@@ -1795,6 +1795,10 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
 		ret = 0;
 		goto out;
 	}
+	if (unlikely(!PageUptodate(page))) {
+		ret = -EIO;
+		goto out;
+	}

Yeah, we have the same handling in do_swap_page(), whereby we send a SIGBUS because we're dealing with an actual access.

Interestingly, folio_test_uptodate() states:

"Anonymous and CoW folios are always uptodate."

@Willy, is that true or is the swapin case not documented there?

Signature · 3 lines
Thanks,

David / dhildenb
#2 lore raw
Quoted 26 lines · David Hildenbrand
Quoted 14 lines · Miaohe Lin

There is a bug in unuse_pte(): when swap page happens to be unreadable, page filled with random data is mapped into user address space. The fix is to check for PageUptodate and fail swapoff in case of error.

---
 mm/swapfile.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/swapfile.c +0 -0
index 63c61f8b2611..e72a35de7a0f 100644

mm/swapfile.c static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, · line 1801

Quoted 7 lines
mm/swapfile.c +4 -0
@@ -1795,6 +1795,10 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
 		ret = 0;
 		goto out;
 	}
+	if (unlikely(!PageUptodate(page))) {
+		ret = -EIO;
+		goto out;
+	}

Yeah, we have the same handling in do_swap_page(), whereby we send a SIGBUS because we're dealing with an actual access.

Quoted 5 lines

Interestingly, folio_test_uptodate() states:

"Anonymous and CoW folios are always uptodate."

@Willy, is that true or is the swapin case not documented there?

Why do we keep a !Uptodate page in the swap cache? If it can't be read in from swap, I thought we just freed the page. Since Miaohe has observed that not happening, I guess it doesn't work that way, but why not make it work that way?

author
#3 lore raw
Quoted 27 lines · Matthew Wilcox
Quoted 26 lines · David Hildenbrand
Quoted 14 lines · Miaohe Lin

There is a bug in unuse_pte(): when swap page happens to be unreadable, page filled with random data is mapped into user address space. The fix is to check for PageUptodate and fail swapoff in case of error.

---
 mm/swapfile.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/swapfile.c +0 -0
index 63c61f8b2611..e72a35de7a0f 100644

mm/swapfile.c static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, · line 1801

Quoted 7 lines
mm/swapfile.c +4 -0
@@ -1795,6 +1795,10 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
 		ret = 0;
 		goto out;
 	}
+	if (unlikely(!PageUptodate(page))) {
+		ret = -EIO;
+		goto out;
+	}

Yeah, we have the same handling in do_swap_page(), whereby we send a SIGBUS because we're dealing with an actual access.

Quoted 8 lines
Quoted 5 lines

Interestingly, folio_test_uptodate() states:

"Anonymous and CoW folios are always uptodate."

@Willy, is that true or is the swapin case not documented there?

Why do we keep a !Uptodate page in the swap cache? If it can't be read in from swap, I thought we just freed the page. Since Miaohe

We could free the bad page. But we still need a way to prevent user from accessing the wrong data.

Quoted 2 lines

has observed that not happening, I guess it doesn't work that way, but why not make it work that way?

How could we make it work that way? Could you please tell me in more detail? Or any suggestions?

Many thanks!

Quoted 3 lines

.

maintainer
#4 lore raw
In reply to Miaohe Lin
Quoted 9 lines · On Fri, 1 Apr 2022 15:29:26 +0800 Miaohe Lin <[email protected]> wrote:

There is a bug in unuse_pte(): when swap page happens to be unreadable, page filled with random data is mapped into user address space. The fix is to check for PageUptodate and fail swapoff in case of error.

...

mm/swapfile.c static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, · line 1804

Quoted 10 lines
mm/swapfile.c +4 -0
@@ -1795,6 +1795,10 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
 		ret = 0;
 		goto out;
 	}
+	if (unlikely(!PageUptodate(page))) {
+		ret = -EIO;
+		goto out;
+	}
 
 	dec_mm_counter(vma->vm_mm, MM_SWAPENTS);
 	inc_mm_counter(vma->vm_mm, MM_ANONPAGES);

Failing the swapoff after -EIO seems a bit rude. The user ends up with a permanently mounted swap because a sector was bad?

That would be like failing truncate() or close() or umount after -EIO on a regular file. Somewhat.

Can we do something better? Such as shooting down the page anyway and permitting the swapoff to proceed? Worst case, just leak the dang page with an apologetic message.

author
#5 lore raw
Quoted 19 lines · Andrew Morton
Quoted 17 lines · On Fri, 1 Apr 2022 15:29:26 +0800 Miaohe Lin <[email protected]> wrote:

There is a bug in unuse_pte(): when swap page happens to be unreadable, page filled with random data is mapped into user address space. The fix is to check for PageUptodate and fail swapoff in case of error.

...

mm/swapfile.c +4 -0
@@ -1795,6 +1795,10 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
 		ret = 0;
 		goto out;
 	}
+	if (unlikely(!PageUptodate(page))) {
+		ret = -EIO;
+		goto out;
+	}
 
Quoted 6 lines

mm/swapfile.c static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, · line 1804

Quoted 2 lines
mm/swapfile.c +0 -0
 	dec_mm_counter(vma->vm_mm, MM_SWAPENTS);
 	inc_mm_counter(vma->vm_mm, MM_ANONPAGES);

Failing the swapoff after -EIO seems a bit rude. The user ends up with a permanently mounted swap because a sector was bad?

This is really unfortunate. :(

Quoted 8 lines

That would be like failing truncate() or close() or umount after -EIO on a regular file. Somewhat.

Can we do something better? Such as shooting down the page anyway and permitting the swapoff to proceed? Worst case, just leak the dang page with an apologetic message.
.

We must have a way to prevent user from accessing the wrong data. One way is kept the page in the swap cache and kill the user when page is accessed. But this will end up with a permanently mounted swap.
Another way I can figure out now is that we could set the page table entry to some special swap entry, such as SWP_EIO like SWP_HWPOISON, we can thus kill the user when page is accessed while swapoff can proceed. But this makes the code more complicated... Any suggestions?

Many thanks!