]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
socket: Print pf->create() when it does not clear sock->sk on failure.
authorKuniyuki Iwashima <kuniyu@amazon.com>
Thu, 24 Oct 2024 20:14:58 +0000 (13:14 -0700)
committerJakub Kicinski <kuba@kernel.org>
Tue, 29 Oct 2024 23:31:23 +0000 (16:31 -0700)
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.

Link: https://lore.kernel.org/netdev/202410231427.633734b3-lkp@intel.com/
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Reviewed-by: Eric Dumazet <edumazet@google.com>
Link: https://patch.msgid.link/20241024201458.49412-1-kuniyu@amazon.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
include/net/net_debug.h
net/socket.c

index 1e74684cbbdbcd5ba304b935e76376a72d6ba3f2..9fecb1496be3f0fca4da9ce83f6b69b27077483a 100644 (file)
@@ -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 */
index 9a8e4452b9b2178317369e4a8a2506ff8cc606ef..5fb3d265e492196fb7de6e6c2278c3c9b91ab00c 100644 (file)
@@ -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;
        }