Skip to contentSkip to conversation

nodemask: Consider MAX_NUMNODES inside node_isset

RFC In review Anshuman Khandual 2 participants 5 messages Jan 3, 2017

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

RFC 1/1 nodemask: Consider MAX_NUMNODES inside node_isset

node_isset can give incorrect result if the node number is beyond the bitmask size (MAX_NUMNODES in this case) which is not checked inside test_bit. Hence check for the bit limits (MAX_NUMNODES) inside the node_isset function before calling test_bit.

---
 include/linux/nodemask.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
include/linux/nodemask.h +7 -1
index 6e66cfd..0aee588b 100644
@@ -139,7 +139,13 @@ static inline void __nodes_clear(nodemask_t *dstp, unsigned int nbits)
 }
 
 /* No static inline type checking - see Subtlety (1) above. */
-#define node_isset(node, nodemask) test_bit((node), (nodemask).bits)
+#define node_isset(node, nodemask) node_test_bit(node, nodemask, MAX_NUMNODES)
+static inline int node_test_bit(int node, nodemask_t nodemask, int maxnodes)
+{
+	if (node >= maxnodes)
+		return 0;
+	return test_bit((node), (nodemask).bits);
+}
 
 #define node_test_and_set(node, nodemask) \
 			__node_test_and_set((node), &(nodemask))
Signature · 7 lines
1.8.3.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw
Quoted 4 lines · Anshuman Khandual

node_isset can give incorrect result if the node number is beyond the bitmask size (MAX_NUMNODES in this case) which is not checked inside test_bit. Hence check for the bit limits (MAX_NUMNODES) inside the node_isset function before calling test_bit.

Could you be more specific when such a thing might happen? Have you seen any in-kernel user who would give such a bogus node?

Quoted 27 lines
---
 include/linux/nodemask.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
include/linux/nodemask.h +7 -1
index 6e66cfd..0aee588b 100644
@@ -139,7 +139,13 @@ static inline void __nodes_clear(nodemask_t *dstp, unsigned int nbits)
 }
 
 /* No static inline type checking - see Subtlety (1) above. */
-#define node_isset(node, nodemask) test_bit((node), (nodemask).bits)
+#define node_isset(node, nodemask) node_test_bit(node, nodemask, MAX_NUMNODES)
+static inline int node_test_bit(int node, nodemask_t nodemask, int maxnodes)
+{
+	if (node >= maxnodes)
+		return 0;
+	return test_bit((node), (nodemask).bits);
+}
 
 #define node_test_and_set(node, nodemask) \
 			__node_test_and_set((node), &(nodemask))
Signature · 1 line
1.8.3.1
Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#2 lore raw
Quoted 7 lines · Michal Hocko
Quoted 4 lines · Anshuman Khandual

node_isset can give incorrect result if the node number is beyond the bitmask size (MAX_NUMNODES in this case) which is not checked inside test_bit. Hence check for the bit limits (MAX_NUMNODES) inside the node_isset function before calling test_bit.

Could you be more specific when such a thing might happen? Have you seen any in-kernel user who would give such a bogus node?

Have not seen this through any in-kernel use case. While rebasing the CDM zonelist rebuilding series, I came across this through an error path when a bogus node value of 256 (MAX_NUMNODES on POWER) is received when we call first_node() on an empty nodemask (which itself seems weird as well).

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw
Quoted 11 lines · Anshuman Khandual
Quoted 7 lines · Michal Hocko
Quoted 4 lines · Anshuman Khandual

node_isset can give incorrect result if the node number is beyond the bitmask size (MAX_NUMNODES in this case) which is not checked inside test_bit. Hence check for the bit limits (MAX_NUMNODES) inside the node_isset function before calling test_bit.

Could you be more specific when such a thing might happen? Have you seen any in-kernel user who would give such a bogus node?

Have not seen this through any in-kernel use case. While rebasing the CDM zonelist rebuilding series,

Then fix this particular code path...

Quoted 3 lines

I came across this through an error path when
a bogus node value of 256 (MAX_NUMNODES on POWER) is received when we call first_node() on an empty nodemask (which itself seems weird as well).

Does calling first_node on an empty nodemask make any sense? If there is a risk then I would expect nodes_empty() check before doing any mask related operations.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#4 lore raw
Quoted 7 lines · Michal Hocko
Quoted 6 lines · Anshuman Khandual
Quoted 5 lines · Michal Hocko
Quoted 4 lines · Anshuman Khandual

node_isset can give incorrect result if the node number is beyond the bitmask size (MAX_NUMNODES in this case) which is not checked inside test_bit. Hence check for the bit limits (MAX_NUMNODES) inside the node_isset function before calling test_bit.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Could you be more specific when such a thing might happen? Have you seen any in-kernel user who would give such a bogus node?

Have not seen this through any in-kernel use case. While rebasing the CDM zonelist rebuilding series,

Then fix this particular code path...

Yeah I did.

Quoted 8 lines
Quoted 4 lines

I came across this through an error path when
a bogus node value of 256 (MAX_NUMNODES on POWER) is received when we call first_node() on an empty nodemask (which itself seems weird as well).

Does calling first_node on an empty nodemask make any sense? If there is a risk then I would expect nodes_empty() check before doing any mask related operations.

Hmm, you are right. All these checks should be done by the caller not these nodemask helper functions.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>