linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] avoid null pointer access in vm_struct
@ 2011-08-17 13:28 Mitsuo Hayasaka
  2011-08-17 19:39 ` Andrew Morton
  0 siblings, 1 reply; 3+ messages in thread
From: Mitsuo Hayasaka @ 2011-08-17 13:28 UTC (permalink / raw)
  To: linux-mm, linux-kernel
  Cc: KOSAKI Motohiro, yrl.pp-manager.tt, Mitsuo Hayasaka,
	Andrew Morton, Namhyung Kim, David Rientjes, Paul E. McKenney,
	Jeremy Fitzhardinge

The /proc/vmallocinfo shows information about vmalloc allocations in vmlist
that is a linklist of vm_struct. It, however, may access pages field of
vm_struct where a page was not allocated, which results in a null pointer
access and leads to a kernel panic.

Why this happen:
For example, in __vmalloc_area_node, the nr_pages field of vm_struct are
set to the expected number of pages to be allocated, before the actual
pages allocations. At the same time, when the /proc/vmallocinfo is read, it
accesses the pages field of vm_struct according to the nr_pages field at
show_numa_info(). Thus, a null pointer access happens.

Patch:
This patch avoids accessing the pages field with unallocated page when
show_numa_info() is called. So, it can solve this problem.

Signed-off-by: Mitsuo Hayasaka <mitsuo.hayasaka.hu@hitachi.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Namhyung Kim <namhyung@gmail.com>
Cc: David Rientjes <rientjes@google.com>
Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
Cc: Jeremy Fitzhardinge <jeremy.fitzhardinge@citrix.com>
---

 mm/vmalloc.c |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 7ef0903..e2ec5b0 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -2472,13 +2472,16 @@ static void show_numa_info(struct seq_file *m, struct vm_struct *v)
 	if (NUMA_BUILD) {
 		unsigned int nr, *counters = m->private;
 
-		if (!counters)
+		if (!counters || !v->nr_pages || !v->pages)
 			return;
 
 		memset(counters, 0, nr_node_ids * sizeof(unsigned int));
 
-		for (nr = 0; nr < v->nr_pages; nr++)
+		for (nr = 0; nr < v->nr_pages; nr++) {
+			if (!v->pages[nr])
+				break;
 			counters[page_to_nid(v->pages[nr])]++;
+		}
 
 		for_each_node_state(nr, N_HIGH_MEMORY)
 			if (counters[nr])

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH] avoid null pointer access in vm_struct
  2011-08-17 13:28 [PATCH] avoid null pointer access in vm_struct Mitsuo Hayasaka
@ 2011-08-17 19:39 ` Andrew Morton
  2011-08-18  9:21   ` HAYASAKA Mitsuo
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Morton @ 2011-08-17 19:39 UTC (permalink / raw)
  To: Mitsuo Hayasaka
  Cc: linux-mm, linux-kernel, KOSAKI Motohiro, yrl.pp-manager.tt,
	Namhyung Kim, David Rientjes, Paul E. McKenney,
	Jeremy Fitzhardinge

On Wed, 17 Aug 2011 22:28:48 +0900
Mitsuo Hayasaka <mitsuo.hayasaka.hu@hitachi.com> wrote:

> The /proc/vmallocinfo shows information about vmalloc allocations in vmlist
> that is a linklist of vm_struct. It, however, may access pages field of
> vm_struct where a page was not allocated, which results in a null pointer
> access and leads to a kernel panic.
> 
> Why this happen:
> For example, in __vmalloc_area_node, the nr_pages field of vm_struct are
> set to the expected number of pages to be allocated, before the actual
> pages allocations. At the same time, when the /proc/vmallocinfo is read, it
> accesses the pages field of vm_struct according to the nr_pages field at
> show_numa_info(). Thus, a null pointer access happens.
> 
> Patch:
> This patch avoids accessing the pages field with unallocated page when
> show_numa_info() is called. So, it can solve this problem.

Do we have a similar race when running __vunmap() in parallel with
show_numa_info()?

> index 7ef0903..e2ec5b0 100644
> --- a/mm/vmalloc.c
> +++ b/mm/vmalloc.c
> @@ -2472,13 +2472,16 @@ static void show_numa_info(struct seq_file *m, struct vm_struct *v)
>  	if (NUMA_BUILD) {
>  		unsigned int nr, *counters = m->private;
>  
> -		if (!counters)
> +		if (!counters || !v->nr_pages || !v->pages)
>  			return;
>  
>  		memset(counters, 0, nr_node_ids * sizeof(unsigned int));
>  
> -		for (nr = 0; nr < v->nr_pages; nr++)
> +		for (nr = 0; nr < v->nr_pages; nr++) {
> +			if (!v->pages[nr])
> +				break;
>  			counters[page_to_nid(v->pages[nr])]++;
> +		}
>  
>  		for_each_node_state(nr, N_HIGH_MEMORY)
>  			if (counters[nr])

I think this has memory ordering issues: it requires that this CPU see
the modification to ->nr_pages and ->pages in the same order as the CPU
which is writing ->nr_pages, ->pages and ->pages[x].  Perhaps fixable
by taking vmlist_lock appropriately.

I suspect that the real bug is that __vmalloc_area_node() and its
caller made the new vmap_area globally visible before it was fully
initialised.  If we were to fix that, the /proc/vmallocinfo read would
not encounter this vm_struct at all.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH] avoid null pointer access in vm_struct
  2011-08-17 19:39 ` Andrew Morton
@ 2011-08-18  9:21   ` HAYASAKA Mitsuo
  0 siblings, 0 replies; 3+ messages in thread
From: HAYASAKA Mitsuo @ 2011-08-18  9:21 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Namhyung Kim, David Rientjes, Paul E. McKenney,
	Jeremy Fitzhardinge, KOSAKI Motohiro, linux-kernel, linux-mm,
	yrl.pp-manager.tt

Andrew Morton a??a??a??ae?,a??a? 3/4 a??a??:
> On Wed, 17 Aug 2011 22:28:48 +0900
> Mitsuo Hayasaka <mitsuo.hayasaka.hu@hitachi.com> wrote:
> 
>> The /proc/vmallocinfo shows information about vmalloc allocations in vmlist
>> that is a linklist of vm_struct. It, however, may access pages field of
>> vm_struct where a page was not allocated, which results in a null pointer
>> access and leads to a kernel panic.
>>
>> Why this happen:
>> For example, in __vmalloc_area_node, the nr_pages field of vm_struct are
>> set to the expected number of pages to be allocated, before the actual
>> pages allocations. At the same time, when the /proc/vmallocinfo is read, it
>> accesses the pages field of vm_struct according to the nr_pages field at
>> show_numa_info(). Thus, a null pointer access happens.
>>
>> Patch:
>> This patch avoids accessing the pages field with unallocated page when
>> show_numa_info() is called. So, it can solve this problem.
> 
> Do we have a similar race when running __vunmap() in parallel with
> show_numa_info()?
> 

No. This does not happen when running __vunmap() because the vm_struct
is released after it is removed from vmlist.


>> index 7ef0903..e2ec5b0 100644
>> --- a/mm/vmalloc.c
>> +++ b/mm/vmalloc.c
>> @@ -2472,13 +2472,16 @@ static void show_numa_info(struct seq_file *m, struct vm_struct *v)
>>  	if (NUMA_BUILD) {
>>  		unsigned int nr, *counters = m->private;
>>  
>> -		if (!counters)
>> +		if (!counters || !v->nr_pages || !v->pages)
>>  			return;
>>  
>>  		memset(counters, 0, nr_node_ids * sizeof(unsigned int));
>>  
>> -		for (nr = 0; nr < v->nr_pages; nr++)
>> +		for (nr = 0; nr < v->nr_pages; nr++) {
>> +			if (!v->pages[nr])
>> +				break;
>>  			counters[page_to_nid(v->pages[nr])]++;
>> +		}
>>  
>>  		for_each_node_state(nr, N_HIGH_MEMORY)
>>  			if (counters[nr])
> 
> I think this has memory ordering issues: it requires that this CPU see
> the modification to ->nr_pages and ->pages in the same order as the CPU
> which is writing ->nr_pages, ->pages and ->pages[x].  Perhaps fixable
> by taking vmlist_lock appropriately.
> 
> I suspect that the real bug is that __vmalloc_area_node() and its
> caller made the new vmap_area globally visible before it was fully
> initialised.  If we were to fix that, the /proc/vmallocinfo read would
> not encounter this vm_struct at all.
> 

I agreed.
I'd like to revise __vmalloc_area_node() and submit the patch again.

Thanks.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

end of thread, other threads:[~2011-08-18  9:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-08-17 13:28 [PATCH] avoid null pointer access in vm_struct Mitsuo Hayasaka
2011-08-17 19:39 ` Andrew Morton
2011-08-18  9:21   ` HAYASAKA Mitsuo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).