diff mbox series

[net-next] net: warn if mac header was not set

Message ID 20220620093017.3366713-1-eric.dumazet@gmail.com (mailing list archive)
State Accepted
Commit f9aefd6b2aa38b9787d2705f0f1161dfd23cdb8f
Delegated to: Netdev Maintainers
Headers show
Series [net-next] net: warn if mac header was not set | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 5647 this patch: 5647
netdev/cc_maintainers warning 1 maintainers not CCed: imagedong@tencent.com
netdev/build_clang success Errors and warnings before: 1214 this patch: 1214
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
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: 5796 this patch: 5796
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 29 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Eric Dumazet June 20, 2022, 9:30 a.m. UTC
From: Eric Dumazet <edumazet@google.com>

Make sure skb_mac_header(), skb_mac_offset() and skb_mac_header_len() uses
are not fooled if the mac header has not been set.

These checks are enabled if CONFIG_DEBUG_NET=y

This commit will likely expose existing bugs in linux networking stacks.

Signed-off-by: Eric Dumazet <edumazet@google.com>
---
 include/linux/skbuff.h | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

Comments

Paolo Abeni June 21, 2022, 9:21 a.m. UTC | #1
Hello,

On Mon, 2022-06-20 at 02:30 -0700, Eric Dumazet wrote:
> From: Eric Dumazet <edumazet@google.com>
> 
> Make sure skb_mac_header(), skb_mac_offset() and skb_mac_header_len() uses
> are not fooled if the mac header has not been set.
> 
> These checks are enabled if CONFIG_DEBUG_NET=y
> 
> This commit will likely expose existing bugs in linux networking stacks.
> 
> Signed-off-by: Eric Dumazet <edumazet@google.com>
> ---
>  include/linux/skbuff.h | 12 +++++++-----
>  1 file changed, 7 insertions(+), 5 deletions(-)
> 
> diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
> index 82edf0359ab32d0d7cd583676b38d569ce0b24cc..cd4a8268894acce4bde16dc0fedb7eb13706f515 100644
> --- a/include/linux/skbuff.h
> +++ b/include/linux/skbuff.h
> @@ -2763,8 +2763,14 @@ static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
>  	skb->network_header += offset;
>  }
>  
> +static inline int skb_mac_header_was_set(const struct sk_buff *skb)
> +{
> +	return skb->mac_header != (typeof(skb->mac_header))~0U;
> +}
> +
>  static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
>  {
> +	DEBUG_NET_WARN_ON_ONCE(!skb_mac_header_was_set(skb));
>  	return skb->head + skb->mac_header;
>  }
>  
> @@ -2775,14 +2781,10 @@ static inline int skb_mac_offset(const struct sk_buff *skb)
>  
>  static inline u32 skb_mac_header_len(const struct sk_buff *skb)
>  {
> +	DEBUG_NET_WARN_ON_ONCE(!skb_mac_header_was_set(skb));

The patch LGTM. 

I'm wondering if it's worthy adding similar debug checks for network
and transport offset (with more patches). e.g. still in
skb_mac_header_len():

	DEBUG_NET_WARN_ON_ONCE(skb->network_header < skb->mac_header);

Thanks!

Paolo
patchwork-bot+netdevbpf@kernel.org June 21, 2022, 9:40 a.m. UTC | #2
Hello:

This patch was applied to netdev/net-next.git (master)
by Paolo Abeni <pabeni@redhat.com>:

On Mon, 20 Jun 2022 02:30:17 -0700 you wrote:
> From: Eric Dumazet <edumazet@google.com>
> 
> Make sure skb_mac_header(), skb_mac_offset() and skb_mac_header_len() uses
> are not fooled if the mac header has not been set.
> 
> These checks are enabled if CONFIG_DEBUG_NET=y
> 
> [...]

Here is the summary with links:
  - [net-next] net: warn if mac header was not set
    https://git.kernel.org/netdev/net-next/c/f9aefd6b2aa3

You are awesome, thank you!
diff mbox series

Patch

diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
index 82edf0359ab32d0d7cd583676b38d569ce0b24cc..cd4a8268894acce4bde16dc0fedb7eb13706f515 100644
--- a/include/linux/skbuff.h
+++ b/include/linux/skbuff.h
@@ -2763,8 +2763,14 @@  static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
 	skb->network_header += offset;
 }
 
+static inline int skb_mac_header_was_set(const struct sk_buff *skb)
+{
+	return skb->mac_header != (typeof(skb->mac_header))~0U;
+}
+
 static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
 {
+	DEBUG_NET_WARN_ON_ONCE(!skb_mac_header_was_set(skb));
 	return skb->head + skb->mac_header;
 }
 
@@ -2775,14 +2781,10 @@  static inline int skb_mac_offset(const struct sk_buff *skb)
 
 static inline u32 skb_mac_header_len(const struct sk_buff *skb)
 {
+	DEBUG_NET_WARN_ON_ONCE(!skb_mac_header_was_set(skb));
 	return skb->network_header - skb->mac_header;
 }
 
-static inline int skb_mac_header_was_set(const struct sk_buff *skb)
-{
-	return skb->mac_header != (typeof(skb->mac_header))~0U;
-}
-
 static inline void skb_unset_mac_header(struct sk_buff *skb)
 {
 	skb->mac_header = (typeof(skb->mac_header))~0U;