Skip to contentSkip to conversation

mptcp: Use struct_group() to avoid cross-field memset()

PATCH Applied Kees Cook 3 participants 3 messages Jan 21, 2022 – Jan 22, 2022

Subsystems (3)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 mptcp: Use struct_group() to avoid cross-field memset()

In preparation for FORTIFY_SOURCE performing compile-time and run-time field bounds checking for memcpy(), memmove(), and memset(), avoid intentionally writing across neighboring fields.

Use struct_group() to capture the fields to be reset, so that memset() can be appropriately bounds-checked by the compiler.

Cc Mat Martineau <[email protected]>
Cc Matthieu Baerts <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Jakub Kicinski <[email protected]>
---
 net/mptcp/protocol.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/mptcp/protocol.h +3 -3
index 0e6b42c76ea0..85317ce38e3f 100644
@@ -408,7 +408,7 @@ DECLARE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
 struct mptcp_subflow_context {
 	struct	list_head node;/* conn_list of subflows */
 
-	char	reset_start[0];
+	struct_group(reset,
 
 	unsigned long avg_pacing_rate; /* protected by msk socket lock */
 	u64	local_key;
@@ -458,7 +458,7 @@ struct mptcp_subflow_context {
 
 	long	delegated_status;
 
-	char	reset_end[0];
+	);
 
 	struct	list_head delegated_node;   /* link into delegated_action, protected by local BH */
 
@@ -494,7 +494,7 @@ mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow)
 static inline void
 mptcp_subflow_ctx_reset(struct mptcp_subflow_context *subflow)
 {
-	memset(subflow->reset_start, 0, subflow->reset_end - subflow->reset_start);
+	memset(&subflow->reset, 0, sizeof(subflow->reset));
 	subflow->request_mptcp = 1;
 }
 
Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Kees Cook

In preparation for FORTIFY_SOURCE performing compile-time and run-time field bounds checking for memcpy(), memmove(), and memset(), avoid intentionally writing across neighboring fields.

Use struct_group() to capture the fields to be reset, so that memset() can be appropriately bounds-checked by the compiler.

Cc Mat Martineau <[email protected]>
Cc Matthieu Baerts <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Jakub Kicinski <[email protected]>
Quoted 4 lines

---
net/mptcp/protocol.h | 6 +++---

1 file changed, 3 insertions(+), 3 deletions(-)

Thanks Kees, looks good to me. I checked around for other MPTCP structs that would need similar attention and didn't see any.

Quoted 28 lines
net/mptcp/protocol.h +2 -2
index 0e6b42c76ea0..85317ce38e3f 100644
@@ -408,7 +408,7 @@ DECLARE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
 struct mptcp_subflow_context {
 	struct	list_head node;/* conn_list of subflows */
 
-	char	reset_start[0];
+	struct_group(reset,
 
 	unsigned long avg_pacing_rate; /* protected by msk socket lock */
 	u64	local_key;
@@ -458,7 +458,7 @@ struct mptcp_subflow_context {
 
 	long	delegated_status;
 
-	char	reset_end[0];
+	);
 
 	struct	list_head delegated_node;   /* link into delegated_action, protected by local BH */
 
@@ -494,7 +494,7 @@ mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow)

static inline void
mptcp_subflow_ctx_reset(struct mptcp_subflow_context *subflow) {

net/mptcp/protocol.h +1 -1
-	memset(subflow->reset_start, 0, subflow->reset_end - subflow->reset_start);
+	memset(&subflow->reset, 0, sizeof(subflow->reset));
Quoted 7 lines
	subflow->request_mptcp = 1;
}
Signature · 1 line
2.30.2

--
Mat Martineau
Intel

#2 lore raw
In reply to Kees Cook
This patch was applied to netdev/net.git (master) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (master)
by Jakub Kicinski <[email protected]>:

Quoted 8 lines · On Thu, 20 Jan 2022 23:39:35 -0800 you wrote:

In preparation for FORTIFY_SOURCE performing compile-time and run-time field bounds checking for memcpy(), memmove(), and memset(), avoid intentionally writing across neighboring fields.

Use struct_group() to capture the fields to be reset, so that memset() can be appropriately bounds-checked by the compiler.

[...]

Here is the summary with links:
- mptcp: Use struct_group() to avoid cross-field memset()

    https://git.kernel.org/netdev/net/c/63ec72bd5848

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html