public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mm: __nr_to_section - make it safe against overflow
@ 2009-01-05  9:40 Cyrill Gorcunov
  2009-01-05 10:00 ` Pekka Enberg
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Cyrill Gorcunov @ 2009-01-05  9:40 UTC (permalink / raw)
  To: Andrew Morton, Nick Piggin, Rik van Riel; +Cc: LKML, Jiri Slaby

__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@openvz.org>
---

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.

 include/linux/mmzone.h |   13 +++++++++++--
 1 file changed, 11 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,12 @@ 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_ONCE(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);

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2009-01-05 16:12 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-01-05  9:40 [PATCH] mm: __nr_to_section - make it safe against overflow Cyrill Gorcunov
2009-01-05 10:00 ` Pekka Enberg
2009-01-05 10:03   ` Cyrill Gorcunov
2009-01-05 10:01 ` Cyrill Gorcunov
2009-01-05 15:10 ` Christoph Lameter
2009-01-05 15:28   ` Cyrill Gorcunov
2009-01-05 15:34     ` Nick Piggin
2009-01-05 16:12       ` Cyrill Gorcunov
2009-01-05 15:37     ` Christoph Lameter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox