David Rientjes 32f8516a8c mm, mempolicy: fix printing stack contents in numa_maps
When reading /proc/pid/numa_maps, it's possible to return the contents of
the stack where the mempolicy string should be printed if the policy gets
freed from beneath us.

This happens because mpol_to_str() may return an error the
stack-allocated buffer is then printed without ever being stored.

There are two possible error conditions in mpol_to_str():

 - if the buffer allocated is insufficient for the string to be stored,
   and

 - if the mempolicy has an invalid mode.

The first error condition is not triggered in any of the callers to
mpol_to_str(): at least 50 bytes is always allocated on the stack and this
is sufficient for the string to be written.  A future patch should convert
this into BUILD_BUG_ON() since we know the maximum strlen possible, but
that's not -rc material.

The second error condition is possible if a race occurs in dropping a
reference to a task's mempolicy causing it to be freed during the read().
The slab poison value is then used for the mode and mpol_to_str() returns
-EINVAL.

This race is only possible because get_vma_policy() believes that
mm->mmap_sem protects task->mempolicy, which isn't true.  The exit path
does not hold mm->mmap_sem when dropping the reference or setting
task->mempolicy to NULL: it uses task_lock(task) instead.

Thus, it's required for the caller of a task mempolicy to hold
task_lock(task) while grabbing the mempolicy and reading it.  Callers with
a vma policy store their mempolicy earlier and can simply increment the
reference count so it's guaranteed not to be freed.

Reported-by: Dave Jones <davej@redhat.com>
Signed-off-by: David Rientjes <rientjes@google.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-10-16 18:00:50 -07:00
..
2012-07-14 16:38:34 +04:00
2012-10-09 23:33:38 -04:00
2012-10-09 16:22:17 +09:00
2012-10-09 23:33:38 -04:00
2012-10-09 16:22:17 +09:00
2012-10-09 23:33:38 -04:00
2012-10-09 15:04:25 +01:00
2012-10-03 08:48:21 -07:00
2012-10-09 18:35:22 -04:00
2012-10-09 16:22:17 +09:00
2012-10-09 16:22:17 +09:00
2012-10-06 03:05:31 +09:00
2012-07-14 16:34:47 +04:00
2012-10-09 23:33:39 -04:00
2012-10-09 16:22:17 +09:00
2012-10-09 23:33:39 -04:00
2012-09-07 14:57:46 -04:00
2012-07-14 16:34:47 +04:00
2012-03-20 21:29:46 -04:00
2012-03-20 21:29:46 -04:00
2012-10-09 15:52:31 +09:00
2012-07-14 16:32:47 +04:00
2012-10-09 18:35:22 -04:00
2012-07-14 16:32:48 +04:00
2012-09-26 21:08:52 -04:00
2012-10-09 23:33:39 -04:00