Re: [PATCH] mm: __nr_to_section - make it safe against overflow

From: Cyrill Gorcunov
Date: Mon Jan 05 2009 - 05:01:36 EST


[Cyrill Gorcunov - Mon, Jan 05, 2009 at 12:40:34PM +0300]
| __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.
|
| Signed-off-by: Cyrill Gorcunov <gorcunov@xxxxxxxxxx>
| ---
|
| Please review. Some __nr_to_section callers don't check
| for NULL returned so this patch could be a bit dangerous
| but should reveal the problems eventually.
|
...

Populating WARN_ON_ONCE by inline is not good. Fixed.

- Cyrill -
---

[PATCH] mm: __nr_to_section - make it safe against overflow

__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.

Signed-off-by: Cyrill Gorcunov <gorcunov@xxxxxxxxxx>
---
include/linux/mmzone.h | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)

Index: linux-2.6.git/include/linux/mmzone.h
===================================================================
--- linux-2.6.git.orig/include/linux/mmzone.h
+++ 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,13 @@ 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);
+
+ WARN_ON(idx >= NR_SECTION_ROOTS);
+ if (idx >= NR_SECTION_ROOTS || !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);
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/