Skip to contentSkip to conversation

mm: __nr_to_section - make it safe against overflow v2

PATCH In review Cyrill Gorcunov 4 participants 8 messages Jan 5, 2009 – Jan 6, 2009

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

PATCH 1/1 mm: __nr_to_section - make it safe against overflow v2

__nr_to_section should check for array bound overflow. We should better get NULL dereference then silently pass some memory snippet out of bounds to a caller.

Also add a comment about mem_section structure.

---
 include/linux/mmzone.h |   15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
include/linux/mmzone.h +13 -2
Index: linux-2.6.git/include/linux/mmzone.h
===================================================================
@@ -935,6 +935,12 @@ static inline unsigned long early_pfn_to
 
 struct page;
 struct page_cgroup;
+
+/*
+ * NOTE: sizeof(struct mem_section) _must_ be power of 2
+ * otherwise SECTION_ROOT_MASK will be broken so be
+ * really cautious while modifying this structure
+ */
 struct mem_section {
 	/*
 	 * This is, logically, a pointer to an array of struct
@@ -980,9 +986,14 @@ extern struct mem_section mem_section[NR
 
 static inline struct mem_section *__nr_to_section(unsigned long nr)
 {
-	if (!mem_section[SECTION_NR_TO_ROOT(nr)])
+	unsigned long idx = SECTION_NR_TO_ROOT(nr);
+
+	if (WARN_ON(idx >= NR_SECTION_ROOTS))
+		return NULL;
+
+	if (!mem_section[idx])
 		return NULL;
-	return &mem_section[SECTION_NR_TO_ROOT(nr)][nr & SECTION_ROOT_MASK];
+	return &mem_section[idx][nr & SECTION_ROOT_MASK];
 }
 extern int __section_nr(struct mem_section* ms);
 extern unsigned long usemap_size(void);
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Cyrill Gorcunov

__nr_to_section should check for array bound overflow. We should better get NULL dereference then silently pass some memory snippet out of bounds to a caller.

Also add a comment about mem_section structure.

Quoted 38 lines
---
 include/linux/mmzone.h |   15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

Index: linux-2.6.git/include/linux/mmzone.h
===================================================================

include/linux/mmzone.h +13 -2
@@ -935,6 +935,12 @@ static inline unsigned long early_pfn_to
 
 struct page;
 struct page_cgroup;
+
+/*
+ * NOTE: sizeof(struct mem_section) _must_ be power of 2
+ * otherwise SECTION_ROOT_MASK will be broken so be
+ * really cautious while modifying this structure
+ */
 struct mem_section {
 	/*
 	 * This is, logically, a pointer to an array of struct
@@ -980,9 +986,14 @@ extern struct mem_section mem_section[NR
 
 static inline struct mem_section *__nr_to_section(unsigned long nr)
 {
-	if (!mem_section[SECTION_NR_TO_ROOT(nr)])
+	unsigned long idx = SECTION_NR_TO_ROOT(nr);
+
+	if (WARN_ON(idx >= NR_SECTION_ROOTS))
+		return NULL;
+
+	if (!mem_section[idx])
 		return NULL;
-	return &mem_section[SECTION_NR_TO_ROOT(nr)][nr & SECTION_ROOT_MASK];
+	return &mem_section[idx][nr & SECTION_ROOT_MASK];
 }
 extern int __section_nr(struct mem_section* ms);
 extern unsigned long usemap_size(void);
maintainer
#2 lore raw
In reply to Cyrill Gorcunov
Quoted 4 lines · On Mon, 5 Jan 2009 13:31:32 +0300 Cyrill Gorcunov <[email protected]> wrote:

__nr_to_section should check for array bound overflow. We should better get NULL dereference then silently pass some memory snippet out of bounds to a caller.

Are there actually any known problems here?

Quoted 28 lines
---
 include/linux/mmzone.h |   15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

Index: linux-2.6.git/include/linux/mmzone.h
===================================================================

include/linux/mmzone.h +6 -1
@@ -935,6 +935,12 @@ static inline unsigned long early_pfn_to
 
 struct page;
 struct page_cgroup;
+
+/*
+ * NOTE: sizeof(struct mem_section) _must_ be power of 2
+ * otherwise SECTION_ROOT_MASK will be broken so be
+ * really cautious while modifying this structure
+ */
 struct mem_section {
 	/*
 	 * This is, logically, a pointer to an array of struct
@@ -980,9 +986,14 @@ extern struct mem_section mem_section[NR
 
 static inline struct mem_section *__nr_to_section(unsigned long nr)
 {
-	if (!mem_section[SECTION_NR_TO_ROOT(nr)])

include/linux/mmzone.h extern struct mem_section mem_section[NR · line 997

Quoted 10 lines
include/linux/mmzone.h +7 -1
+	unsigned long idx = SECTION_NR_TO_ROOT(nr);
+
+	if (WARN_ON(idx >= NR_SECTION_ROOTS))
+		return NULL;
+
+	if (!mem_section[idx])
 		return NULL;
-	return &mem_section[SECTION_NR_TO_ROOT(nr)][nr & SECTION_ROOT_MASK];
+	return &mem_section[idx][nr & SECTION_ROOT_MASK];
 }

The patch adds nearly 300 bytes of stuff to mm/sparse.o, and for what??

#3 lore raw
Quoted 10 lines · On Mon, 5 Jan 2009 16:37:42 -0800 Andrew Morton <[email protected]>…
Quoted 4 lines · On Mon, 5 Jan 2009 13:31:32 +0300 Cyrill Gorcunov <[email protected]> wrote:

__nr_to_section should check for array bound overflow. We should better get NULL dereference then silently pass some memory snippet out of bounds to a caller.

Are there actually any known problems here?

IIRC, I never saw any problem. (But I may see in memory-hotplug development.) I don't like adding any check here.

This __nr_to_section[] is very internal function and will not be accessed directly by codes other than VM(of sparsemem and memory hotplug).

For general users, this function will be called via pfn_to_page(). So, this means what this patch should really do is checking "pfn" argugments to pfn_to_page(). Of course, I don't want any check in pfn_to_page()'s "pfn" range.But, pfn_to_page() is usually called with pfn_valid() if necessary. pfn_valid() and pfn_present(), other callers of this, check this in explicit way.

Cyrill, if you really want to do check "idx", please add

==
__nr_to_section_direct(unsigned long nr) (or some better name)

{
	BUG_ON(nr >NR_SECTION_ROOTS);
	return __nr_to_section(nr);
}

==
to, mm/internal.h or somewhere. and use this in sparse.c and memhotplug.c

So, Nack from me for now.

Thanks,
-Kame

#4 lore raw
Quoted 11 lines · On Tue, 6 Jan 2009 14:57:41 +0900 KAMEZAWA Hiroyuki…
Quoted 8 lines · On Mon, 5 Jan 2009 16:37:42 -0800 Andrew Morton <[email protected]>…
Quoted 4 lines · On Mon, 5 Jan 2009 13:31:32 +0300 Cyrill Gorcunov <[email protected]> wrote:

__nr_to_section should check for array bound overflow. We should better get NULL dereference then silently pass some memory snippet out of bounds to a caller.

Quoted 4 lines
Quoted 2 lines

Are there actually any known problems here?

IIRC, I never saw any problem. (But I may see in memory-hotplug development.)

I digged mails and seems this patch is from this thread.

=
http://lkml.org/lkml/2009/1/4/61
=

He seems to want to hunt boot time failure.
(Finally, BIOS-update was necessary for original problem of this thread.)

Cyrill, could you modify this WARN_ON() works works when __nr_to_section() is called directly by sparse.c and memhotplug.c ?

Adding WARN_ON() in pfn_to_page() is overkill.

Thanks,
-Kame

#5 lore raw

[KAMEZAWA Hiroyuki - Tue, Jan 06, 2009 at 03:30:36PM +0900]

| On Tue, 6 Jan 2009 14:57:41 +0900
| KAMEZAWA Hiroyuki <[email protected]> wrote:
| 
| > On Mon, 5 Jan 2009 16:37:42 -0800
| > Andrew Morton <[email protected]> wrote:
| > 
| > > On Mon, 5 Jan 2009 13:31:32 +0300
| > > Cyrill Gorcunov <[email protected]> wrote:
| > > 
| > > > __nr_to_section should check for array bound overflow.
| > > > We should better get NULL dereference then silently
| > > > pass some memory snippet out of bounds to a caller.
| > > > 
| > > 
| > > Are there actually any known problems here?
| > > 
| > 
| > IIRC, I never saw any problem. (But I may see in memory-hotplug development.)
| 
| I digged mails and seems this patch is from this thread.
| 
| =
| http://lkml.org/lkml/2009/1/4/61
| =
| 
| He seems to want to hunt boot time failure.
| (Finally, BIOS-update was necessary for original problem of this thread.)
| 
| Cyrill, could you modify this WARN_ON() works works when __nr_to_section()
| is called directly by sparse.c and memhotplug.c ?
| 
| Adding WARN_ON() in pfn_to_page() is overkill.
| 
| Thanks,
| -Kame
| 

Thanks for review Kame, this patch should be just dropped.

Actually the only code snippet which make me nervious is sizeof (mem_section) part. Which is to remain power of two and even doesn't have 'packed' attribute neither any comments above. And if it happens that it will be modified or say gcc decide to add some bytes here (bugs happens) we would silently address wrong mem_section. So I think at least the 'comment' part of my patch is deserve to be applied :)

- Cyrill -

#6 lore raw
In reply to Andrew Morton

[Andrew Morton - Mon, Jan 05, 2009 at 04:37:42PM -0800]
...
| The patch adds nearly 300 bytes of stuff to mm/sparse.o, and for what?? |
...

No need to apply it. Sorry for noise.

- Cyrill -

#7 lore raw
In reply to Andrew Morton

Hi Andrew,

Quoted 35 lines · Andrew Morton
Quoted 4 lines · On Mon, 5 Jan 2009 13:31:32 +0300 Cyrill Gorcunov <[email protected]> wrote:

__nr_to_section should check for array bound overflow. We should better get NULL dereference then silently pass some memory snippet out of bounds to a caller.

Are there actually any known problems here?

Quoted 25 lines
---
 include/linux/mmzone.h |   15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

Index: linux-2.6.git/include/linux/mmzone.h
===================================================================

include/linux/mmzone.h +6 -0
@@ -935,6 +935,12 @@ static inline unsigned long early_pfn_to
 
 struct page;
 struct page_cgroup;
+
+/*
+ * NOTE: sizeof(struct mem_section) _must_ be power of 2
+ * otherwise SECTION_ROOT_MASK will be broken so be
+ * really cautious while modifying this structure
+ */
 struct mem_section {
      /*
       * This is, logically, a pointer to an array of struct
@@ -980,9 +986,14 @@ extern struct mem_section mem_section[NR
 
Quoted 15 lines

include/linux/mmzone.h extern struct mem_section mem_section[NR · line 997

Quoted 13 lines
include/linux/mmzone.h +7 -2
 static inline struct mem_section *__nr_to_section(unsigned long nr)
 {
-     if (!mem_section[SECTION_NR_TO_ROOT(nr)])
+     unsigned long idx = SECTION_NR_TO_ROOT(nr);
+
+     if (WARN_ON(idx >= NR_SECTION_ROOTS))
+             return NULL;
+
+     if (!mem_section[idx])
              return NULL;
-     return &mem_section[SECTION_NR_TO_ROOT(nr)][nr & SECTION_ROOT_MASK];
+     return &mem_section[idx][nr & SECTION_ROOT_MASK];
 }

The patch adds nearly 300 bytes of stuff to mm/sparse.o, and for what??

I was thinking of things like commit bead9a3abd15710b0bdfd418daef606722d86282 ("mm: sparsemem memory_present() fix") and such but missed the fact that __nr_to_section() inlined all over the place. So yeah, it's not worth it.

Pekka