Skip to contentSkip to conversation

mm/mempolicy: fix copy_nodes_to_user() for odd inputs

PATCH Applied Arnd Bergmann 3 participants 7 messages Nov 20 – Nov 22

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 mm/mempolicy: fix copy_nodes_to_user() for odd inputs

From: Arnd Bergmann <[email protected]>

copy_nodes_to_user() is broken for compat userspace in three distinct ways:

- Passing a power-of-two size plus 1 as most normal userspace does can end up calling compat_put_bitmap() with the wrong size, copying an extra 32-bit word from the stack into userspace and overflowing both arrays as compat_put_bitmap() expects the actual number of bits, not the number plus 1.

- Asking for an odd number of 32-bit words rounds up the userspace buffer size full 64-bit words, leading to an out-of-bounds access in userspace.

Fix the calculation of 'copy' to be based on the userspace word size and use this to for calculating the final number of bits to copy as well.

---
v3: don't introduce a bug that skip setting a 32-bit word to zero
where 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 | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
mm/mempolicy.c +5 -4
index 62d4c9e57035..afe614efb77c 100644
@@ -1772,12 +1772,14 @@ 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)
+	if (compat) {
+		copy = ALIGN(maxnode-1, BITS_PER_COMPAT_LONG) / 8;
 		nbytes = BITS_TO_COMPAT_LONGS(nr_node_ids) * sizeof(compat_long_t);
+	}
 
 	if (copy > nbytes) {
 		if (copy > PAGE_SIZE)
@@ -1785,12 +1787,11 @@ 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;
 	}
 
 	if (compat)
 		return compat_put_bitmap((compat_ulong_t __user *)mask,
-					 nodes_addr(*nodes), maxnode);
+					 nodes_addr(*nodes), copy * 8);
 
 	return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0;
 }
Signature · 1 line
2.53.0
#1 lore raw

PATCH v3 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, and restructure it for readability.

---
 mm/mempolicy.c | 48 +++++++++++++++++++++++++++++-------------------
 1 file changed, 29 insertions(+), 19 deletions(-)
mm/mempolicy.c +29 -19
index afe614efb77c..b240acaa8cf1 100644
@@ -1736,7 +1736,9 @@ 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;
+        /* User space passes the number of bits plus 1. Fix that up. */
+	maxnode -= 1;
+
 	nodes_clear(*nodes);
 	if (maxnode == 0 || !nmask)
 		return 0;
@@ -1769,31 +1771,39 @@ 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();
+	const bool compat = in_compat_syscall();
+	const unsigned int word_bits = compat ? BITS_PER_COMPAT_LONG : BITS_PER_LONG;
+	unsigned long bits, node_bits;
 
-	if (compat) {
-		copy = ALIGN(maxnode-1, BITS_PER_COMPAT_LONG) / 8;
-		nbytes = BITS_TO_COMPAT_LONGS(nr_node_ids) * sizeof(compat_long_t);
-	}
+	/* User space passes the number of bits plus 1. Fix that up. */
+	maxnode -= 1;
 
-	if (copy > nbytes) {
-		if (copy > PAGE_SIZE)
-			return -EINVAL;
-		if (clear_user((char __user *)mask + nbytes, copy - nbytes))
+	/* Don't exceed what would fit into a single page. */
+	if (maxnode > PAGE_SIZE * BITS_PER_BYTE)
+		return -EINVAL;
+
+	/* Process full words, which depend on the user space ABI. */
+	bits = ALIGN(maxnode, word_bits);
+	node_bits = ALIGN(nr_node_ids, word_bits);
+
+	/* Clear anything we won't copy below. */
+	if (bits > node_bits) {
+		if (clear_user((char __user *)mask + node_bits / BITS_PER_BYTE,
+			       (bits - node_bits) / BITS_PER_BYTE))
 			return -EFAULT;
-		copy = nbytes;
+		bits = node_bits;
 	}
 
+	/* Finally, copy the bitmap. */
 	if (compat)
 		return compat_put_bitmap((compat_ulong_t __user *)mask,
-					 nodes_addr(*nodes), copy * 8);
-
-	return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0;
+					 nodes_addr(*nodes), bits);
+	if (copy_to_user(mask, nodes_addr(*nodes), bits / BITS_PER_BYTE))
+		return -EFAULT;
+	return 0;
 }
 
 /* Basic parameter sanity check used by both mbind() and set_mempolicy() */
@@ -2054,7 +2064,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
maintainer Reviewed-by on v3 2/2
#2 lore raw

Re: [PATCH 2/2] [v3] mm/mempolicy: replace copy_nodes_to_user() interface with put_nodes()

Quoted 10 lines · Arnd Bergmann

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, and restructure it for readability.

Could briefly mention here the change in get_nodes() for consistency.

Quoted 23 lines
---
 mm/mempolicy.c | 48 +++++++++++++++++++++++++++++-------------------
 1 file changed, 29 insertions(+), 19 deletions(-)
mm/mempolicy.c +3 -1
index afe614efb77c..b240acaa8cf1 100644
@@ -1736,7 +1736,9 @@ 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;
+        /* User space passes the number of bits plus 1. Fix that up. */
+	maxnode -= 1;
+
 	nodes_clear(*nodes);
 	if (maxnode == 0 || !nmask)
 		return 0;

mm/mempolicy.c static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask, · line 1775

Quoted 7 lines
mm/mempolicy.c +2 -2
@@ -1769,31 +1771,39 @@ 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)

Two tabs here here is the preferred way in MM. Can fix that up when applying.

Quoted 50 lines
mm/mempolicy.c +23 -15
 {
-	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();
+	const bool compat = in_compat_syscall();
+	const unsigned int word_bits = compat ? BITS_PER_COMPAT_LONG : BITS_PER_LONG;
+	unsigned long bits, node_bits;
 
-	if (compat) {
-		copy = ALIGN(maxnode-1, BITS_PER_COMPAT_LONG) / 8;
-		nbytes = BITS_TO_COMPAT_LONGS(nr_node_ids) * sizeof(compat_long_t);
-	}
+	/* User space passes the number of bits plus 1. Fix that up. */
+	maxnode -= 1;
 
-	if (copy > nbytes) {
-		if (copy > PAGE_SIZE)
-			return -EINVAL;
-		if (clear_user((char __user *)mask + nbytes, copy - nbytes))
+	/* Don't exceed what would fit into a single page. */
+	if (maxnode > PAGE_SIZE * BITS_PER_BYTE)
+		return -EINVAL;
+
+	/* Process full words, which depend on the user space ABI. */
+	bits = ALIGN(maxnode, word_bits);
+	node_bits = ALIGN(nr_node_ids, word_bits);
+
+	/* Clear anything we won't copy below. */
+	if (bits > node_bits) {
+		if (clear_user((char __user *)mask + node_bits / BITS_PER_BYTE,
+			       (bits - node_bits) / BITS_PER_BYTE))
 			return -EFAULT;
-		copy = nbytes;
+		bits = node_bits;
 	}
 
+	/* Finally, copy the bitmap. */
 	if (compat)
 		return compat_put_bitmap((compat_ulong_t __user *)mask,
-					 nodes_addr(*nodes), copy * 8);
-
-	return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0;
+					 nodes_addr(*nodes), bits);
+	if (copy_to_user(mask, nodes_addr(*nodes), bits / BITS_PER_BYTE))
+		return -EFAULT;
+	return 0;
 }
 
 /* Basic parameter sanity check used by both mbind() and set_mempolicy() */

mm/mempolicy.c static int kernel_get_mempolicy(int __user *policy, · line 2070

Quoted 8 lines
mm/mempolicy.c +1 -1
@@ -2054,7 +2064,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 · 3 lines
Cheers,

David
maintainer Tested-by Acked-by on v3 2/2
#3 lore raw

Re: [PATCH 2/2] [v3] mm/mempolicy: replace copy_nodes_to_user() interface with put_nodes()

In reply to Arnd Bergmann
Quoted 7 lines · Arnd Bergmann

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.

Quoted 6 lines

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, and restructure it for readability.

Signature · 1 line
~Gregory
maintainer Reviewed-by on v3 1/2
#4 lore raw
In reply to Arnd Bergmann
Quoted 4 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

copy_nodes_to_user() is broken for compat userspace in three distinct ways:

"Three" but below you list two. So I assume this should be "two" ?

Quoted 7 lines

- Passing a power-of-two size plus 1 as most normal userspace does can end up calling compat_put_bitmap() with the wrong size, copying an extra 32-bit word from the stack into userspace and overflowing both arrays as compat_put_bitmap() expects the actual number of bits, not the number plus 1.

Quoted 6 lines

- Asking for an odd number of 32-bit words rounds up the userspace buffer size full 64-bit words, leading to an out-of-bounds access in userspace.

Fix the calculation of 'copy' to be based on the userspace word size and use this to for calculating the final number

s/to for/for/

Quoted 2 lines

of bits to copy as well.

You might also want to link to Gregory's test case.

Quoted 4 lines
Fixes e130242dc351 ("mm: simplify compat numa syscalls")

Should this be a Closes: for Gregory's report?

Quoted 8 lines

---
v3: don't introduce a bug that skip setting a 32-bit word to zero
where 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.
---

I can fix the description up when applying.

Signature · 3 lines
Cheers,

David
maintainer Tested-by Acked-by on v3 1/2
#5 lore raw
In reply to Arnd Bergmann
Quoted 19 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

copy_nodes_to_user() is broken for compat userspace in three distinct ways:

- Passing a power-of-two size plus 1 as most normal userspace does can end up calling compat_put_bitmap() with the wrong size, copying an extra 32-bit word from the stack into userspace and overflowing both arrays as compat_put_bitmap() expects the actual number of bits, not the number plus 1.

- Asking for an odd number of 32-bit words rounds up the userspace buffer size full 64-bit words, leading to an out-of-bounds access in userspace.

Fix the calculation of 'copy' to be based on the userspace word size and use this to for calculating the final number of bits to copy as well.

Quoted 6 lines
Signature · 1 line
~Gregory
maintainer Applied
#6 lore raw
In reply to Arnd Bergmann
Quoted 10 lines · Arnd Bergmann

copy_nodes_to_user() is broken for compat userspace in three distinct ways:

- Passing a power-of-two size plus 1 as most normal userspace does can end up calling compat_put_bitmap() with the wrong size, copying an extra 32-bit word from the stack into userspace and overflowing both arrays as compat_put_bitmap() expects the actual number of bits, not the number plus 1.

[...]

Applied, thanks!

tree: mm/core.git
branch: mempolicy-7.5.misc

Applied patches will appear soon in mm-next, followed by linux-next.

Please report any outstanding bugs that were missed during review, so we can either drop the patch series or decide how to fix it up.

It is encouraged to provide Acked-bys, Reviewed-bys, and Tested-bys even though the patches have already been applied. If possible, patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. Further, this is not a guarantee that the patches will go upstream. If in doubt, please check the listed branch.

[1/2] mm/mempolicy: fix copy_nodes_to_user() for odd inputs

      https://git.kernel.org/mm/core/c/50d3f729
[2/2] mm/mempolicy: replace copy_nodes_to_user() interface with put_nodes()
      https://git.kernel.org/mm/core/c/995fcdf5
Signature · 3 lines
Cheers,

David