diff mbox series

[v1,net-next] socket: Print pf->create() when it does not clear sock->sk on failure.

Message ID 20241023191757.56735-1-kuniyu@amazon.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [v1,net-next] socket: Print pf->create() when it does not clear sock->sk on failure. | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 44 this patch: 44
netdev/build_tools success Errors and warnings before: 157 (+0) this patch: 157 (+0)
netdev/cc_maintainers warning 1 maintainers not CCed: horms@kernel.org
netdev/build_clang success Errors and warnings before: 85 this patch: 85
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 5022 this patch: 5022
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 22 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 24 this patch: 24
netdev/source_inline success Was 0 now: 0

Commit Message

Kuniyuki Iwashima Oct. 23, 2024, 7:17 p.m. UTC
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 later removed 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>
---
 include/net/net_debug.h | 4 +++-
 net/socket.c            | 4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)

Comments

Eric Dumazet Oct. 23, 2024, 7:45 p.m. UTC | #1
On Wed, Oct 23, 2024 at 9:18 PM Kuniyuki Iwashima <kuniyu@amazon.com> 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.
>
> 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 later removed 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>
> ---
>  include/net/net_debug.h | 4 +++-
>  net/socket.c            | 4 +++-
>  2 files changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/include/net/net_debug.h b/include/net/net_debug.h
> index 1e74684cbbdb..9fecb1496be3 100644
> --- a/include/net/net_debug.h
> +++ b/include/net/net_debug.h
> @@ -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 */
> diff --git a/net/socket.c b/net/socket.c
> index 9a8e4452b9b2..da00db3824e3 100644
> --- a/net/socket.c
> +++ b/net/socket.c
> @@ -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",

%ps would be more appropriate, the offset should be zero, no need to
display a full 'symbol+0x0/0x<size>'
Kuniyuki Iwashima Oct. 23, 2024, 7:51 p.m. UTC | #2
From: Eric Dumazet <edumazet@google.com>
Date: Wed, 23 Oct 2024 21:45:26 +0200
> On Wed, Oct 23, 2024 at 9:18 PM Kuniyuki Iwashima <kuniyu@amazon.com> 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.
> >
> > 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 later removed 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>
> > ---
> >  include/net/net_debug.h | 4 +++-
> >  net/socket.c            | 4 +++-
> >  2 files changed, 6 insertions(+), 2 deletions(-)
> >
> > diff --git a/include/net/net_debug.h b/include/net/net_debug.h
> > index 1e74684cbbdb..9fecb1496be3 100644
> > --- a/include/net/net_debug.h
> > +++ b/include/net/net_debug.h
> > @@ -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 */
> > diff --git a/net/socket.c b/net/socket.c
> > index 9a8e4452b9b2..da00db3824e3 100644
> > --- a/net/socket.c
> > +++ b/net/socket.c
> > @@ -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",
> 
> %ps would be more appropriate, the offset should be zero, no need to
> display a full 'symbol+0x0/0x<size>'

Ah, exactly!
Will use %ps in v2.

Thanks!

---
pw-bot: cr
diff mbox series

Patch

diff --git a/include/net/net_debug.h b/include/net/net_debug.h
index 1e74684cbbdb..9fecb1496be3 100644
--- a/include/net/net_debug.h
+++ b/include/net/net_debug.h
@@ -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 */
diff --git a/net/socket.c b/net/socket.c
index 9a8e4452b9b2..da00db3824e3 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -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;
 	}