mm/mempolicy: fix copy_nodes_to_user() for odd inputs
Subsystems (3)
PATCH v2 1/2 mm/mempolicy: fix copy_nodes_to_user() for odd inputs
From: Arnd Bergmann <[email protected]>
The compat_put_bitmap() argument has an off-by-one error that leads to copying an extra 32-bit word from the kernel stack to userspace when called with an argument that is one larger than a 64-bit number.
In addition, the size calculation rounds up to multiples of 64 bits instead of multiples of word size as documented in the man page and expected by numactl.
Fix the calculation where needed to use the correct -1 offset and pad as needed.
---
v2: as Lorenzo pointed out, the off-by-one mistake is part of the ABI
and cannot change, we just have to use it consistently.
--- mm/mempolicy.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
mm/mempolicy.c +4 -4
@@ -1749,12 +1749,12 @@ static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,static int copy_nodes_to_user (unsigned long __user *mask,unsigned long maxnode,nodemask_t *nodes) { -unsigned long copy =ALIGN (maxnode-1 ,64 ) /8 ; +unsigned long copy =ALIGN (maxnode-1 ,BITS_PER_LONG ) /8 ;unsigned int nbytes =BITS_TO_LONGS (nr_node_ids) *sizeof (long );bool compat =in_compat_syscall ();if (compat) - nbytes =BITS_TO_COMPAT_LONGS (nr_node_ids) *sizeof (compat_long_t); + copy =ALIGN (maxnode-1 ,BITS_PER_COMPAT_LONG ) /8 ;if (copy > nbytes) {if (copy >PAGE_SIZE )
@@ -1762,12 +1762,12 @@ static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,if (clear_user ((char __user *)mask + nbytes, copy - nbytes))return -EFAULT ; copy = nbytes; - maxnode = nr_node_ids; + maxnode = nr_node_ids+1 ; }if (compat)return compat_put_bitmap ((compat_ulong_t __user *)mask, -nodes_addr (*nodes), maxnode); +nodes_addr (*nodes), maxnode-1 );return copy_to_user (mask,nodes_addr (*nodes), copy) ? -EFAULT :0 ; }
Signature · 1 line
2.53.0
PATCH v2 2/2 mm/mempolicy: replace copy_nodes_to_user() interface with put_nodes()
From: Arnd Bergmann <[email protected]>
The get_nodes() function handles copying node masks from userspace, while copy_nodes_to_user() copies the same masks to userspace, but with a different argument order and slightly different meaning of the same variables.
To make that less confusing, change copy_nodes_to_user() to look more like get_nodes(), using similar name and arguments as well as adjusting the maxnode value once, with a comment explaining it in both places.
--- mm/mempolicy.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)
mm/mempolicy.c +11 -8
@@ -1713,7 +1713,7 @@ static int get_bitmap(unsigned long *mask, const unsigned long __user *nmask,static int get_nodes (nodemask_t *nodes,const unsigned long __user *nmask,unsigned long maxnode) { - --maxnode; + --maxnode;/* user space ABI uses number of bits plus 1 */ nodes_clear (*nodes);if (maxnode ==0 || !nmask)return 0 ;
@@ -1746,15 +1746,18 @@ static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask, }/* Copy a kernel node mask to user space */ -static int copy_nodes_to_user (unsigned long __user *mask,unsigned long maxnode, -nodemask_t *nodes) +static int put_nodes (unsigned long __user *mask,nodemask_t *nodes, +unsigned long maxnode) { -unsigned long copy =ALIGN (maxnode-1 ,BITS_PER_LONG ) /8 ;unsigned int nbytes =BITS_TO_LONGS (nr_node_ids) *sizeof (long );bool compat =in_compat_syscall (); +unsigned long copy; + + --maxnode;/* user space ABI uses number of bits plus 1 */ + copy =ALIGN (maxnode,BITS_PER_LONG ) /8 ;if (compat) - copy =ALIGN (maxnode-1 ,BITS_PER_COMPAT_LONG ) /8 ; + copy =ALIGN (maxnode,BITS_PER_COMPAT_LONG ) /8 ;if (copy > nbytes) {if (copy >PAGE_SIZE )
@@ -1762,12 +1765,12 @@ static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,if (clear_user ((char __user *)mask + nbytes, copy - nbytes))return -EFAULT ; copy = nbytes; - maxnode = nr_node_ids+1 ; + maxnode = nr_node_ids; }if (compat)return compat_put_bitmap ((compat_ulong_t __user *)mask, -nodes_addr (*nodes), maxnode-1 ); +nodes_addr (*nodes), maxnode);return copy_to_user (mask,nodes_addr (*nodes), copy) ? -EFAULT :0 ; }
@@ -2030,7 +2033,7 @@ static int kernel_get_mempolicy(int __user *policy,return -EFAULT ;if (nmask) - err =copy_nodes_to_user (nmask, maxnode, &nodes); + err =put_nodes (nmask, &nodes, maxnode);return err; }
Signature · 1 line
2.53.0
Gregory Price (Meta)
maintainerQuoted 32 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
The
compat_put_bitmap()argument has an off-by-one error that leads to copying an extra 32-bit word from the kernel stack to userspace when called with an argument that is one larger than a 64-bit number.In addition, the size calculation rounds up to multiples of 64 bits instead of multiples of word size as documented in the man page and expected by numactl.
Fix the calculation where needed to use the correct -1 offset and pad as needed.
Reported-by larips <[email protected]>Reported-by Gregory Price (Meta) <[email protected]>Fixes e130242dc351 ("mm: simplify compat numa syscalls")Signed-off-by Arnd Bergmann <[email protected]>---
v2: as Lorenzo pointed out, the off-by-one mistake is part of the ABI
and cannot change, we just have to use it consistently.--- mm/mempolicy.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)mm/mempolicy.c +0
-0@@ -1749,12 +1749,12 @@ static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask, static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,
mm/mempolicy.c static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask, · line 1757
Quoted 10 lines
mm/mempolicy.c +2
-2nodemask_t *nodes) { -unsigned long copy =ALIGN (maxnode-1 ,64 ) /8 ; +unsigned long copy =ALIGN (maxnode-1 ,BITS_PER_LONG ) /8 ;unsigned int nbytes =BITS_TO_LONGS (nr_node_ids) *sizeof (long );bool compat =in_compat_syscall ();if (compat) - nbytes =BITS_TO_COMPAT_LONGS (nr_node_ids) *sizeof (compat_long_t); + copy =ALIGN (maxnode-1 ,BITS_PER_COMPAT_LONG ) /8 ;
bleh this is such a mess to reason about.
Pretty sure you need to keep both of these lines:
if (compat) {
copy = ALIGN(maxnode, BITS_PER_COMPAT_LONG) / 8;
nbytes = BITS_TO_COMPAT_LONGS(nr_node_ids) * sizeof(compat_long_t);
}Otherwise now we can get the opposite problem. If the buffer is longer than nbytes, we'll write too few.
Test:
```
/* 32-bit get_mempolicy() leaves nodemask word 1 stale (needs <= 32 nodes). */
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
int main(void)
{
/* int $0x80 is a compat syscall: pointers must be below 4G */
unsigned int *m = mmap(0, 4096, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
long ret;
memset(m, 0xa5, 4096);
/* i386 get_mempolicy(275)(&policy, mask, maxnode=1025, 0, MPOL_F_MEMS_ALLOWED) */
asm volatile("int $0x80" : "=a"(ret)
: "a"(275), "b"(m + 512), "c"(m), "d"(1025), "S"(0), "D"(4)
: "memory");
printf("ret=%ld word0=%08x word1=%08x -> %s\n", ret, m[0], m[1],
m[1] == 0xa5a5a5a5 ? "FAIL: word 1 stale" : "ok");
return m[1] == 0xa5a5a5a5;
}
```base - word0=00000003 word1=00000000 -> ok this patch - word0=00000003 word1=a5a5a5a5 -> FAIL fix above - word0=00000003 word1=00000000 -> ok
Signature · 1 line
~Gregory