Skip to contentSkip to conversation

mm: Check writable zero page in page table check

PATCH In review Rick Edgecombe 4 participants 10 messages Sep 2, 2022 – Sep 6, 2022

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

PATCH 1/1 mm: Check writable zero page in page table check

The zero page should remain all zero, so that it can be mapped as read-only for read faults of memory that should be zeroed. If it is ever mapped writable to userspace, it could become non-zero and so other apps would unexpectedly get non-zero data. So the zero page should never be mapped writable to userspace. Check for this condition in page_table_check_set().

---

Hi,

CONFIG_PAGE_TABLE_CHECK is pretty explicit about what it checks (and doesn't mention the zero page), but this condition seems to fit with the general category of "pages mapped wrongly to userspace". I added it locally to help me debug something. Maybe it's more widely useful.

 mm/page_table_check.c | 2 ++
 1 file changed, 2 insertions(+)
mm/page_table_check.c +2 -0
index e2062748791a..665ece0d55d4 100644
@@ -102,6 +102,8 @@ static void page_table_check_set(struct mm_struct *mm, unsigned long addr,
 	if (!pfn_valid(pfn))
 		return;
 
+	BUG_ON(is_zero_pfn(pfn) && rw);
+
 	page = pfn_to_page(pfn);
 	page_ext = lookup_page_ext(page);
 	anon = PageAnon(page);

base-commit: b90cb1053190353cc30f0fef0ef1f378ccc063c5

Signature · 1 line
2.17.1
#1 lore raw
Quoted 26 lines · Rick Edgecombe

The zero page should remain all zero, so that it can be mapped as read-only for read faults of memory that should be zeroed. If it is ever mapped writable to userspace, it could become non-zero and so other apps would unexpectedly get non-zero data. So the zero page should never be mapped writable to userspace. Check for this condition in page_table_check_set().

---

Hi,

CONFIG_PAGE_TABLE_CHECK is pretty explicit about what it checks (and doesn't mention the zero page), but this condition seems to fit with the general category of "pages mapped wrongly to userspace". I added it locally to help me debug something. Maybe it's more widely useful.

  mm/page_table_check.c | 2 ++
  1 file changed, 2 insertions(+)
mm/page_table_check.c +0 -0
index e2062748791a..665ece0d55d4 100644

mm/page_table_check.c static void page_table_check_set(struct mm_struct *mm, unsigned long addr, · line 106

Quoted 5 lines
mm/page_table_check.c +2 -0
@@ -102,6 +102,8 @@ static void page_table_check_set(struct mm_struct *mm, unsigned long addr,
  	if (!pfn_valid(pfn))
  		return;
 
+	BUG_ON(is_zero_pfn(pfn) && rw);
+

Why we need use BUG_ON() here? Based on [1], we should avoid to use the BUG_ON() due to it will panic the machine.

[1]: https://lore.kernel.org/lkml/[email protected]/

Quoted 5 lines
mm/page_table_check.c +0 -0
  	page = pfn_to_page(pfn);
  	page_ext = lookup_page_ext(page);
  	anon = PageAnon(page);

base-commit: b90cb1053190353cc30f0fef0ef1f378ccc063c5

#2 lore raw
Quoted 9 lines · Shaoqin

mm/page_table_check.c static void page_table_check_set(struct mm_struct *mm, unsigned long addr, · line 106

Quoted 2 lines
mm/page_table_check.c +2 -0
+     BUG_ON(is_zero_pfn(pfn) && rw);
+

Why we need use BUG_ON() here? Based on [1], we should avoid to use the
BUG_ON() due to it will panic the machine.

[1]:
https://lore.kernel.org/lkml/[email protected]/

Yea, you are probably right. All the rest of this checker uses BUG_ON() though. Maybe they should all be something else? Just felt weird to have this be the only check that is different.

I don't have any objections to changing it to WARN_ON(). Should I switch the rest of the checks here while I'm at it?

#3 lore raw
Quoted 11 lines · Rick P
Quoted 9 lines · Shaoqin

mm/page_table_check.c static void page_table_check_set(struct mm_struct *mm, unsigned long addr, · line 106

Quoted 2 lines
mm/page_table_check.c +2 -0
+     BUG_ON(is_zero_pfn(pfn) && rw);
+

Why we need use BUG_ON() here? Based on [1], we should avoid to use the
BUG_ON() due to it will panic the machine.

[1]:
https://lore.kernel.org/lkml/[email protected]/

Quoted 6 lines

Yea, you are probably right. All the rest of this checker uses BUG_ON() though. Maybe they should all be something else? Just felt weird to have this be the only check that is different.

I don't have any objections to changing it to WARN_ON(). Should I switch the rest of the checks here while I'm at it?

Yes. I think in most situation, WARN_ON() or WARN_ON_ONCE() is ok.

#4 lore raw
Quoted 14 lines · Shaoqin
Quoted 11 lines · Rick P
Quoted 9 lines · Shaoqin

mm/page_table_check.c static void page_table_check_set(struct mm_struct *mm, unsigned long addr, · line 106

Quoted 2 lines
mm/page_table_check.c +2 -0
+     BUG_ON(is_zero_pfn(pfn) && rw);
+

Why we need use BUG_ON() here? Based on [1], we should avoid to use the
BUG_ON() due to it will panic the machine.

[1]:
https://lore.kernel.org/lkml/[email protected]/

Quoted 9 lines
Quoted 6 lines

Yea, you are probably right. All the rest of this checker uses BUG_ON() though. Maybe they should all be something else? Just felt weird to have this be the only check that is different.

I don't have any objections to changing it to WARN_ON(). Should I switch the rest of the checks here while I'm at it?

Yes. I think in most situation, WARN_ON() or WARN_ON_ONCE() is ok.

+1 for WARN_ON_ONCE(), and also for changing the other cases as you find them, yes.

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
maintainer
#5 lore raw
In reply to Huang, Shaoqin

Hi Shaoqin,

The idea behind page table check is to prevent some types of memory
corruptions: i.e. prevent false page sharing, and memory leaking between address spaces. This is an optional security feature for setups where it is more dangerous to leak data than to crash the machine. Therefore, when page table check detects illegal page sharing it immediately crashes the kernel. I think we can have a page_table_check option that would change BUG_ON to WARN_ON() (or to WARN_ON_ONCE(), since once corruption is detected I believe it might show up many times again)

Pasha

Quoted 46 lines · Shaoqin
Quoted 26 lines · Rick Edgecombe

The zero page should remain all zero, so that it can be mapped as read-only for read faults of memory that should be zeroed. If it is ever mapped writable to userspace, it could become non-zero and so other apps would unexpectedly get non-zero data. So the zero page should never be mapped writable to userspace. Check for this condition in page_table_check_set().

---

Hi,

CONFIG_PAGE_TABLE_CHECK is pretty explicit about what it checks (and doesn't mention the zero page), but this condition seems to fit with the general category of "pages mapped wrongly to userspace". I added it locally to help me debug something. Maybe it's more widely useful.

  mm/page_table_check.c | 2 ++
  1 file changed, 2 insertions(+)
mm/page_table_check.c +0 -0
index e2062748791a..665ece0d55d4 100644

mm/page_table_check.c static void page_table_check_set(struct mm_struct *mm, unsigned long addr, · line 106

Quoted 5 lines
mm/page_table_check.c +2 -0
@@ -102,6 +102,8 @@ static void page_table_check_set(struct mm_struct *mm, unsigned long addr,
      if (!pfn_valid(pfn))
              return;
 
+     BUG_ON(is_zero_pfn(pfn) && rw);
+

Why we need use BUG_ON() here? Based on [1], we should avoid to use the BUG_ON() due to it will panic the machine.

[1]: https://lore.kernel.org/lkml/[email protected]/

Quoted 5 lines
mm/page_table_check.c +0 -0
      page = pfn_to_page(pfn);
      page_ext = lookup_page_ext(page);
      anon = PageAnon(page);

base-commit: b90cb1053190353cc30f0fef0ef1f378ccc063c5

#6 lore raw
Quoted 11 lines · Pasha Tatashin

Hi Shaoqin,

The idea behind page table check is to prevent some types of memory
corruptions: i.e. prevent false page sharing, and memory leaking between address spaces. This is an optional security feature for setups where it is more dangerous to leak data than to crash the machine. Therefore, when page table check detects illegal page sharing it immediately crashes the kernel. I think we can have a page_table_check option that would change BUG_ON to WARN_ON() (or to WARN_ON_ONCE(), since once corruption is detected I believe it might show up many times again)

Hi, Pasha,

Thanks for your explanation. That's make sense.

Quoted 50 lines

Pasha

Quoted 45 lines · Shaoqin
Quoted 25 lines · Rick Edgecombe

The zero page should remain all zero, so that it can be mapped as read-only for read faults of memory that should be zeroed. If it is ever mapped writable to userspace, it could become non-zero and so other apps would unexpectedly get non-zero data. So the zero page should never be mapped writable to userspace. Check for this condition in page_table_check_set().

---

Hi,

CONFIG_PAGE_TABLE_CHECK is pretty explicit about what it checks (and doesn't mention the zero page), but this condition seems to fit with the general category of "pages mapped wrongly to userspace". I added it locally to help me debug something. Maybe it's more widely useful >>>

   mm/page_table_check.c | 2 ++
   1 file changed, 2 insertions(+)
mm/page_table_check.c +0 -0
index e2062748791a..665ece0d55d4 100644

mm/page_table_check.c static void page_table_check_set(struct mm_struct *mm, unsigned long addr, · line 106

Quoted 5 lines
mm/page_table_check.c +2 -0
@@ -102,6 +102,8 @@ static void page_table_check_set(struct mm_struct *mm, unsigned long addr,
       if (!pfn_valid(pfn))
               return;
 
+     BUG_ON(is_zero_pfn(pfn) && rw);
+

Why we need use BUG_ON() here? Based on [1], we should avoid to use the BUG_ON() due to it will panic the machine.

[1]: https://lore.kernel.org/lkml/[email protected]/

Quoted 5 lines
mm/page_table_check.c +0 -0
       page = pfn_to_page(pfn);
       page_ext = lookup_page_ext(page);
       anon = PageAnon(page);

base-commit: b90cb1053190353cc30f0fef0ef1f378ccc063c5

#7 lore raw
In reply to Pasha Tatashin
Quoted 10 lines · Pasha Tatashin

The idea behind page table check is to prevent some types of memory
corruptions: i.e. prevent false page sharing, and memory leaking between address spaces. This is an optional security feature for setups where it is more dangerous to leak data than to crash the machine. Therefore, when page table check detects illegal page sharing
it immediately crashes the kernel. I think we can have a page_table_check option that would change BUG_ON to WARN_ON() (or to WARN_ON_ONCE(), since once corruption is detected I believe it might show up many times again)

Do you think there are a lot of people that would want to set page table check to BUG_ON mode, that wouldn't already be setting panic_on_warn?

I didn't realize page table check was meant to be a security feature as well. I thought it was more of a debug time checker.

Looking through more related discussions, there seems to be a strong aversion to "crash the kernel" features. Especially if they are meant to run in a non-testing context.

maintainer
#8 lore raw
In reply to Rick Edgecombe

Hi Rock,

Good idea to add a check for write access to zero page. Can you please also update Documentation/mm/page_table_check.rst ?

Thank you,
Pasha

Quoted 40 lines · Rick Edgecombe

The zero page should remain all zero, so that it can be mapped as read-only for read faults of memory that should be zeroed. If it is ever mapped writable to userspace, it could become non-zero and so other apps would unexpectedly get non-zero data. So the zero page should never be mapped writable to userspace. Check for this condition in page_table_check_set().

---

Hi,

CONFIG_PAGE_TABLE_CHECK is pretty explicit about what it checks (and doesn't mention the zero page), but this condition seems to fit with the general category of "pages mapped wrongly to userspace". I added it locally to help me debug something. Maybe it's more widely useful.

 mm/page_table_check.c | 2 ++
 1 file changed, 2 insertions(+)
mm/page_table_check.c +2 -0
index e2062748791a..665ece0d55d4 100644
@@ -102,6 +102,8 @@ static void page_table_check_set(struct mm_struct *mm, unsigned long addr,
        if (!pfn_valid(pfn))
                return;
 
+       BUG_ON(is_zero_pfn(pfn) && rw);
+
        page = pfn_to_page(pfn);
        page_ext = lookup_page_ext(page);
        anon = PageAnon(page);

base-commit: b90cb1053190353cc30f0fef0ef1f378ccc063c5 --
2.17.1

maintainer
#9 lore raw

Sorry: s/Rock/Rick

:-)

Quoted 51 lines · Pasha Tatashin

Hi Rock,

Good idea to add a check for write access to zero page. Can you please also update Documentation/mm/page_table_check.rst ?

Thank you,
Pasha

Quoted 40 lines · Rick Edgecombe

The zero page should remain all zero, so that it can be mapped as read-only for read faults of memory that should be zeroed. If it is ever mapped writable to userspace, it could become non-zero and so other apps would unexpectedly get non-zero data. So the zero page should never be mapped writable to userspace. Check for this condition in page_table_check_set().

---

Hi,

CONFIG_PAGE_TABLE_CHECK is pretty explicit about what it checks (and doesn't mention the zero page), but this condition seems to fit with the general category of "pages mapped wrongly to userspace". I added it locally to help me debug something. Maybe it's more widely useful.

 mm/page_table_check.c | 2 ++
 1 file changed, 2 insertions(+)
mm/page_table_check.c +2 -0
index e2062748791a..665ece0d55d4 100644
@@ -102,6 +102,8 @@ static void page_table_check_set(struct mm_struct *mm, unsigned long addr,
        if (!pfn_valid(pfn))
                return;
 
+       BUG_ON(is_zero_pfn(pfn) && rw);
+
        page = pfn_to_page(pfn);
        page_ext = lookup_page_ext(page);
        anon = PageAnon(page);

base-commit: b90cb1053190353cc30f0fef0ef1f378ccc063c5 --
2.17.1