Skip to contentSkip to conversation

socket: Print pf->create() when it does not clear sock->sk on failure.

PATCH Applied Kuniyuki Iwashima 3 participants 3 messages Oct 24, 2024 – Oct 29, 2024

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

PATCH v2 1/1 socket: Print pf->create() when it does not clear sock->sk on failure.

I suggested to put DEBUG_NET_WARN_ON_ONCE() in __sock_create() to catch possible use-after-free.

But the warning itself was not useful because our interest is in the callee than the caller.

Let's define DEBUG_NET_WARN_ONCE() and print the name of pf->create() and the socket identifier.

While at it, we enclose DEBUG_NET_WARN_ON_ONCE() in parentheses too to avoid a checkpatch error.

Note that %pf or %pF were obsoleted and will be removed later as per comment in lib/vsprintf.c.

---
v2:
* Use %ps instead of %pS as the offset is always zero

v1: https://lore.kernel.org/netdev/[email protected]/

---
 include/net/net_debug.h | 4 +++-
 net/socket.c            | 4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)
include/net/net_debug.h +3 -1
index 1e74684cbbdb..9fecb1496be3 100644
@@ -149,9 +149,11 @@ do {								\
 
 
 #if defined(CONFIG_DEBUG_NET)
-#define DEBUG_NET_WARN_ON_ONCE(cond) (void)WARN_ON_ONCE(cond)
+#define DEBUG_NET_WARN_ON_ONCE(cond) ((void)WARN_ON_ONCE(cond))
+#define DEBUG_NET_WARN_ONCE(cond, format...) ((void)WARN_ONCE(cond, format))
 #else
 #define DEBUG_NET_WARN_ON_ONCE(cond) BUILD_BUG_ON_INVALID(cond)
+#define DEBUG_NET_WARN_ONCE(cond, format...) BUILD_BUG_ON_INVALID(cond)
 #endif
 
 #endif	/* _LINUX_NET_DEBUG_H */
net/socket.c +3 -1
index 9a8e4452b9b2..5fb3d265e492 100644
@@ -1578,7 +1578,9 @@ int __sock_create(struct net *net, int family, int type, int protocol,
 		/* ->create should release the allocated sock->sk object on error
 		 * and make sure sock->sk is set to NULL to avoid use-after-free
 		 */
-		DEBUG_NET_WARN_ON_ONCE(sock->sk);
+		DEBUG_NET_WARN_ONCE(sock->sk,
+				    "%ps must clear sock->sk on failure, family: %d, type: %d, protocol: %d\n",
+				    pf->create, family, type, protocol);
 		goto out_module_put;
 	}
 
Signature · 1 line
2.39.5 (Apple Git-154)
Reviewed-by on v2 1/1
#1 lore raw
Quoted 13 lines · Kuniyuki Iwashima

I suggested to put DEBUG_NET_WARN_ON_ONCE() in __sock_create() to catch possible use-after-free.

But the warning itself was not useful because our interest is in the callee than the caller.

Let's define DEBUG_NET_WARN_ONCE() and print the name of pf->create() and the socket identifier.

While at it, we enclose DEBUG_NET_WARN_ON_ONCE() in parentheses too to avoid a checkpatch error.

Quoted 6 lines

Note that %pf or %pF were obsoleted and will be removed later as per comment in lib/vsprintf.c.

---

#2 lore raw
In reply to Kuniyuki Iwashima
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

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

Quoted 10 lines · On Thu, 24 Oct 2024 13:14:58 -0700 you wrote:

I suggested to put DEBUG_NET_WARN_ON_ONCE() in __sock_create() to catch possible use-after-free.

But the warning itself was not useful because our interest is in the callee than the caller.

Let's define DEBUG_NET_WARN_ONCE() and print the name of pf->create() and the socket identifier.

[...]

Here is the summary with links:
- [v2,net-next] socket: Print pf->create() when it does not clear sock->sk on failure.

    https://git.kernel.org/netdev/net-next/c/4bbd360a5084

You are awesome, thank you!

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