All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 0/3] net, mac80211, kernel: enable KCOV remote coverage collection for 802.11 frame handling
@ 2020-10-28 18:20 Aleksandr Nogikh
  2020-10-28 18:20 ` [PATCH v4 1/3] kernel: make kcov_common_handle consider the current context Aleksandr Nogikh
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Aleksandr Nogikh @ 2020-10-28 18:20 UTC (permalink / raw)
  To: davem, kuba, johannes
  Cc: edumazet, andreyknvl, dvyukov, elver, linux-kernel, netdev,
	linux-wireless, willemdebruijn.kernel, Aleksandr Nogikh

From: Aleksandr Nogikh <nogikh@google.com>

This patch series enables remote KCOV coverage collection during
802.11 frames processing. These changes make it possible to perform
coverage-guided fuzzing in search of remotely triggerable bugs.

Normally, KCOV collects coverage information for the code that is
executed inside the system call context. It is easy to identify where
that coverage should go and whether it should be collected at all by
looking at the current process. If KCOV was enabled on that process,
coverage will be stored in a buffer specific to that process.
Howerever, it is not always enough as handling can happen elsewhere
(e.g. in separate kernel threads).

When it is impossible to infer KCOV-related info just by looking at
the currently running process, one needs to manually pass some
information to the code that should be instrumented. The information
takes the form of 64 bit integers (KCOV remote handles). Zero is the
special value that corresponds to an empty handle. More details on
KCOV and remote coverage collection can be found in
Documentation/dev-tools/kcov.rst.

The series consists of three commits.
1. Apply a minor fix to kcov_common_handle() so that it returns a
valid handle (zero) when called in an interrupt context.
2. Take the remote handle from KCOV and attach it to newly allocated
SKBs as an skb extension. If the allocation happens inside a system
call context, the SKB will be tied to the process that issued the
syscall (if that process is interested in remote coverage collection).
3. Annotate the code that processes incoming 802.11 frames with
kcov_remote_start()/kcov_remote_stop().

v4:
* CONFIG_SKB_EXTENSIONS is now automatically selected by CONFIG_KCOV.
* Elaborated on a minor optimization in skb_set_kcov_handle().

v3:
https://lkml.kernel.org/r/20201026150851.528148-1-aleksandrnogikh@gmail.com
* kcov_handle is now stored in skb extensions instead of sk_buff
  itself.
* Updated the cover letter.

v2:
https://lkml.kernel.org/r/20201009170202.103512-1-a.nogikh@gmail.com
* Moved KCOV annotations from ieee80211_tasklet_handler to
  ieee80211_rx.
* Updated kcov_common_handle() to return 0 if it is called in
  interrupt context.
* Updated the cover letter.

v1:
https://lkml.kernel.org/r/20201007101726.3149375-1-a.nogikh@gmail.com

Aleksandr Nogikh (3):
  kernel: make kcov_common_handle consider the current context
  net: add kcov handle to skb extensions
  mac80211: add KCOV remote annotations to incoming frame processing

 include/linux/skbuff.h | 36 ++++++++++++++++++++++++++++++++++++
 include/net/mac80211.h |  2 ++
 kernel/kcov.c          |  2 ++
 lib/Kconfig.debug      |  1 +
 net/core/skbuff.c      | 11 +++++++++++
 net/mac80211/iface.c   |  2 ++
 6 files changed, 54 insertions(+)


base-commit: 1c86f90a16d413621918ae1403842b43632f0b3d
-- 
2.29.0.rc2.309.g374f81d7ae-goog


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v4 1/3] kernel: make kcov_common_handle consider the current context
  2020-10-28 18:20 [PATCH v4 0/3] net, mac80211, kernel: enable KCOV remote coverage collection for 802.11 frame handling Aleksandr Nogikh
@ 2020-10-28 18:20 ` Aleksandr Nogikh
  2020-10-28 18:20 ` [PATCH v4 2/3] net: add kcov handle to skb extensions Aleksandr Nogikh
  2020-10-28 18:20 ` [PATCH v4 3/3] mac80211: add KCOV remote annotations to incoming frame processing Aleksandr Nogikh
  2 siblings, 0 replies; 7+ messages in thread
From: Aleksandr Nogikh @ 2020-10-28 18:20 UTC (permalink / raw)
  To: davem, kuba, johannes
  Cc: edumazet, andreyknvl, dvyukov, elver, linux-kernel, netdev,
	linux-wireless, willemdebruijn.kernel, Aleksandr Nogikh

From: Aleksandr Nogikh <nogikh@google.com>

kcov_common_handle is a method that is used to obtain a "default" KCOV
remote handle of the current process. The handle can later be passed
to kcov_remote_start in order to collect coverage for the processing
that is initiated by one process, but done in another. For details see
Documentation/dev-tools/kcov.rst and comments in kernel/kcov.c.

Presently, if kcov_common_handle is called in an IRQ context, it will
return a handle for the interrupted process. This may lead to
unreliable and incorrect coverage collection.

Adjust the behavior of kcov_common_handle in the following way. If it
is called in a task context, return the common handle for the
currently running task. Otherwise, return 0.

Signed-off-by: Aleksandr Nogikh <nogikh@google.com>
Reviewed-by: Andrey Konovalov <andreyknvl@google.com>
---
 kernel/kcov.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/kernel/kcov.c b/kernel/kcov.c
index 6b8368be89c8..80bfe71bbe13 100644
--- a/kernel/kcov.c
+++ b/kernel/kcov.c
@@ -1023,6 +1023,8 @@ EXPORT_SYMBOL(kcov_remote_stop);
 /* See the comment before kcov_remote_start() for usage details. */
 u64 kcov_common_handle(void)
 {
+	if (!in_task())
+		return 0;
 	return current->kcov_handle;
 }
 EXPORT_SYMBOL(kcov_common_handle);
-- 
2.29.0.rc2.309.g374f81d7ae-goog


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v4 2/3] net: add kcov handle to skb extensions
  2020-10-28 18:20 [PATCH v4 0/3] net, mac80211, kernel: enable KCOV remote coverage collection for 802.11 frame handling Aleksandr Nogikh
  2020-10-28 18:20 ` [PATCH v4 1/3] kernel: make kcov_common_handle consider the current context Aleksandr Nogikh
@ 2020-10-28 18:20 ` Aleksandr Nogikh
  2020-10-28 20:42   ` Willem de Bruijn
  2020-10-28 18:20 ` [PATCH v4 3/3] mac80211: add KCOV remote annotations to incoming frame processing Aleksandr Nogikh
  2 siblings, 1 reply; 7+ messages in thread
From: Aleksandr Nogikh @ 2020-10-28 18:20 UTC (permalink / raw)
  To: davem, kuba, johannes
  Cc: edumazet, andreyknvl, dvyukov, elver, linux-kernel, netdev,
	linux-wireless, willemdebruijn.kernel, Aleksandr Nogikh

From: Aleksandr Nogikh <nogikh@google.com>

Remote KCOV coverage collection enables coverage-guided fuzzing of the
code that is not reachable during normal system call execution. It is
especially helpful for fuzzing networking subsystems, where it is
common to perform packet handling in separate work queues even for the
packets that originated directly from the user space.

Enable coverage-guided frame injection by adding kcov remote handle to
skb extensions. Default initialization in __alloc_skb and
__build_skb_around ensures that no socket buffer that was generated
during a system call will be missed.

Code that is of interest and that performs packet processing should be
annotated with kcov_remote_start()/kcov_remote_stop().

An alternative approach is to determine kcov_handle solely on the
basis of the device/interface that received the specific socket
buffer. However, in this case it would be impossible to distinguish
between packets that originated during normal background network
processes or were intentionally injected from the user space.

Signed-off-by: Aleksandr Nogikh <nogikh@google.com>
---
v3 -> v4:
* CONFIG_SKB_EXTENSIONS is now automatically selected by CONFIG_KCOV.
* Elaborated on a minor optimization in skb_set_kcov_handle().
v2 -> v3:
* Reimplemented this change. Now kcov handle is added to skb
extensions instead of sk_buff.
v1 -> v2:
* Updated the commit message.
---
 include/linux/skbuff.h | 36 ++++++++++++++++++++++++++++++++++++
 lib/Kconfig.debug      |  1 +
 net/core/skbuff.c      | 11 +++++++++++
 3 files changed, 48 insertions(+)

diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
index a828cf99c521..d1cc1597d566 100644
--- a/include/linux/skbuff.h
+++ b/include/linux/skbuff.h
@@ -4150,6 +4150,9 @@ enum skb_ext_id {
 #endif
 #if IS_ENABLED(CONFIG_MPTCP)
 	SKB_EXT_MPTCP,
+#endif
+#if IS_ENABLED(CONFIG_KCOV)
+	SKB_EXT_KCOV_HANDLE,
 #endif
 	SKB_EXT_NUM, /* must be last */
 };
@@ -4605,5 +4608,38 @@ static inline void skb_reset_redirect(struct sk_buff *skb)
 #endif
 }
 
+#ifdef CONFIG_KCOV
+
+static inline void skb_set_kcov_handle(struct sk_buff *skb, const u64 kcov_handle)
+{
+	/* Do not allocate skb extensions only to set kcov_handle to zero
+	 * (as it is zero by default). However, if the extensions are
+	 * already allocated, update kcov_handle anyway since
+	 * skb_set_kcov_handle can be called to zero a previously set
+	 * value.
+	 */
+	if (skb_has_extensions(skb) || kcov_handle) {
+		u64 *kcov_handle_ptr = skb_ext_add(skb, SKB_EXT_KCOV_HANDLE);
+
+		if (kcov_handle_ptr)
+			*kcov_handle_ptr = kcov_handle;
+	}
+}
+
+static inline u64 skb_get_kcov_handle(struct sk_buff *skb)
+{
+	u64 *kcov_handle = skb_ext_find(skb, SKB_EXT_KCOV_HANDLE);
+
+	return kcov_handle ? *kcov_handle : 0;
+}
+
+#else
+
+static inline void skb_set_kcov_handle(struct sk_buff *skb, const u64 kcov_handle) { }
+
+static inline u64 skb_get_kcov_handle(struct sk_buff *skb) { return 0; }
+
+#endif /* CONFIG_KCOV */
+
 #endif	/* __KERNEL__ */
 #endif	/* _LINUX_SKBUFF_H */
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 537cf3c2937d..9df33cf81d2b 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -1873,6 +1873,7 @@ config KCOV
 	depends on CC_HAS_SANCOV_TRACE_PC || GCC_PLUGINS
 	select DEBUG_FS
 	select GCC_PLUGIN_SANCOV if !CC_HAS_SANCOV_TRACE_PC
+	select SKB_EXTENSIONS
 	help
 	  KCOV exposes kernel code coverage information in a form suitable
 	  for coverage-guided fuzzing (randomized testing).
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index 1ba8f0163744..c5e6c0b83a92 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -249,6 +249,9 @@ struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
 
 		fclones->skb2.fclone = SKB_FCLONE_CLONE;
 	}
+
+	skb_set_kcov_handle(skb, kcov_common_handle());
+
 out:
 	return skb;
 nodata:
@@ -282,6 +285,8 @@ static struct sk_buff *__build_skb_around(struct sk_buff *skb,
 	memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
 	atomic_set(&shinfo->dataref, 1);
 
+	skb_set_kcov_handle(skb, kcov_common_handle());
+
 	return skb;
 }
 
@@ -4203,6 +4208,9 @@ static const u8 skb_ext_type_len[] = {
 #if IS_ENABLED(CONFIG_MPTCP)
 	[SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext),
 #endif
+#if IS_ENABLED(CONFIG_KCOV)
+	[SKB_EXT_KCOV_HANDLE] = SKB_EXT_CHUNKSIZEOF(u64),
+#endif
 };
 
 static __always_inline unsigned int skb_ext_total_length(void)
@@ -4219,6 +4227,9 @@ static __always_inline unsigned int skb_ext_total_length(void)
 #endif
 #if IS_ENABLED(CONFIG_MPTCP)
 		skb_ext_type_len[SKB_EXT_MPTCP] +
+#endif
+#if IS_ENABLED(CONFIG_KCOV)
+		skb_ext_type_len[SKB_EXT_KCOV_HANDLE] +
 #endif
 		0;
 }
-- 
2.29.0.rc2.309.g374f81d7ae-goog


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v4 3/3] mac80211: add KCOV remote annotations to incoming frame processing
  2020-10-28 18:20 [PATCH v4 0/3] net, mac80211, kernel: enable KCOV remote coverage collection for 802.11 frame handling Aleksandr Nogikh
  2020-10-28 18:20 ` [PATCH v4 1/3] kernel: make kcov_common_handle consider the current context Aleksandr Nogikh
  2020-10-28 18:20 ` [PATCH v4 2/3] net: add kcov handle to skb extensions Aleksandr Nogikh
@ 2020-10-28 18:20 ` Aleksandr Nogikh
  2020-10-28 19:24   ` Johannes Berg
  2 siblings, 1 reply; 7+ messages in thread
From: Aleksandr Nogikh @ 2020-10-28 18:20 UTC (permalink / raw)
  To: davem, kuba, johannes
  Cc: edumazet, andreyknvl, dvyukov, elver, linux-kernel, netdev,
	linux-wireless, willemdebruijn.kernel, Aleksandr Nogikh

From: Aleksandr Nogikh <nogikh@google.com>

Add KCOV remote annotations to ieee80211_iface_work and
ieee80211_rx. This will enable coverage-guided fuzzing of
mac80211 code that processes incoming 802.11 frames.

Signed-off-by: Aleksandr Nogikh <nogikh@google.com>
---
v1 -> v2:
* The commit now affects ieee80211_rx instead of
  ieee80211_tasklet_handler.
---
 include/net/mac80211.h | 2 ++
 net/mac80211/iface.c   | 2 ++
 2 files changed, 4 insertions(+)

diff --git a/include/net/mac80211.h b/include/net/mac80211.h
index e8e295dae744..f4c37a1b381e 100644
--- a/include/net/mac80211.h
+++ b/include/net/mac80211.h
@@ -4499,7 +4499,9 @@ void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
  */
 static inline void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
 {
+	kcov_remote_start_common(skb_get_kcov_handle(skb));
 	ieee80211_rx_napi(hw, NULL, skb, NULL);
+	kcov_remote_stop();
 }
 
 /**
diff --git a/net/mac80211/iface.c b/net/mac80211/iface.c
index 1be775979132..56a1bcea2c1c 100644
--- a/net/mac80211/iface.c
+++ b/net/mac80211/iface.c
@@ -1356,6 +1356,7 @@ static void ieee80211_iface_work(struct work_struct *work)
 	while ((skb = skb_dequeue(&sdata->skb_queue))) {
 		struct ieee80211_mgmt *mgmt = (void *)skb->data;
 
+		kcov_remote_start_common(skb_get_kcov_handle(skb));
 		if (ieee80211_is_action(mgmt->frame_control) &&
 		    mgmt->u.action.category == WLAN_CATEGORY_BACK) {
 			int len = skb->len;
@@ -1465,6 +1466,7 @@ static void ieee80211_iface_work(struct work_struct *work)
 		}
 
 		kfree_skb(skb);
+		kcov_remote_stop();
 	}
 
 	/* then other type-dependent work */
-- 
2.29.0.rc2.309.g374f81d7ae-goog


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 3/3] mac80211: add KCOV remote annotations to incoming frame processing
  2020-10-28 18:20 ` [PATCH v4 3/3] mac80211: add KCOV remote annotations to incoming frame processing Aleksandr Nogikh
@ 2020-10-28 19:24   ` Johannes Berg
  2020-10-29 17:42     ` Aleksandr Nogikh
  0 siblings, 1 reply; 7+ messages in thread
From: Johannes Berg @ 2020-10-28 19:24 UTC (permalink / raw)
  To: Aleksandr Nogikh, davem, kuba
  Cc: edumazet, andreyknvl, dvyukov, elver, linux-kernel, netdev,
	linux-wireless, willemdebruijn.kernel, Aleksandr Nogikh

On Wed, 2020-10-28 at 18:20 +0000, Aleksandr Nogikh wrote:
> From: Aleksandr Nogikh <nogikh@google.com>
> 
> Add KCOV remote annotations to ieee80211_iface_work and
> ieee80211_rx. This will enable coverage-guided fuzzing of
> mac80211 code that processes incoming 802.11 frames.
> 
> Signed-off-by: Aleksandr Nogikh <nogikh@google.com>
> ---
> v1 -> v2:
> * The commit now affects ieee80211_rx instead of
>   ieee80211_tasklet_handler.
> ---
>  include/net/mac80211.h | 2 ++
>  net/mac80211/iface.c   | 2 ++
>  2 files changed, 4 insertions(+)
> 
> diff --git a/include/net/mac80211.h b/include/net/mac80211.h
> index e8e295dae744..f4c37a1b381e 100644
> --- a/include/net/mac80211.h
> +++ b/include/net/mac80211.h
> @@ -4499,7 +4499,9 @@ void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
>   */
>  static inline void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
>  {
> +	kcov_remote_start_common(skb_get_kcov_handle(skb));
>  	ieee80211_rx_napi(hw, NULL, skb, NULL);
> +	kcov_remote_stop();
>  }

Wouldn't it make more sense to push that a layer down
into ieee80211_rx_napi(), or actually now perhaps even
better ieee80211_rx_list(), so we get it even if the driver called that
API in the first place?

You might only care about hwsim at this point, but perhaps hwsim would
get optimised ..


johannes


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 2/3] net: add kcov handle to skb extensions
  2020-10-28 18:20 ` [PATCH v4 2/3] net: add kcov handle to skb extensions Aleksandr Nogikh
@ 2020-10-28 20:42   ` Willem de Bruijn
  0 siblings, 0 replies; 7+ messages in thread
From: Willem de Bruijn @ 2020-10-28 20:42 UTC (permalink / raw)
  To: Aleksandr Nogikh
  Cc: David Miller, Jakub Kicinski, Johannes Berg, Eric Dumazet,
	Andrey Konovalov, Dmitry Vyukov, Marco Elver, LKML,
	Network Development, linux-wireless, Aleksandr Nogikh

On Wed, Oct 28, 2020 at 2:21 PM Aleksandr Nogikh
<aleksandrnogikh@gmail.com> wrote:
>
> From: Aleksandr Nogikh <nogikh@google.com>
>
> Remote KCOV coverage collection enables coverage-guided fuzzing of the
> code that is not reachable during normal system call execution. It is
> especially helpful for fuzzing networking subsystems, where it is
> common to perform packet handling in separate work queues even for the
> packets that originated directly from the user space.
>
> Enable coverage-guided frame injection by adding kcov remote handle to
> skb extensions. Default initialization in __alloc_skb and
> __build_skb_around ensures that no socket buffer that was generated
> during a system call will be missed.
>
> Code that is of interest and that performs packet processing should be
> annotated with kcov_remote_start()/kcov_remote_stop().
>
> An alternative approach is to determine kcov_handle solely on the
> basis of the device/interface that received the specific socket
> buffer. However, in this case it would be impossible to distinguish
> between packets that originated during normal background network
> processes or were intentionally injected from the user space.
>
> Signed-off-by: Aleksandr Nogikh <nogikh@google.com>

Acked-by: Willem de Bruijn <willemb@google.com>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 3/3] mac80211: add KCOV remote annotations to incoming frame processing
  2020-10-28 19:24   ` Johannes Berg
@ 2020-10-29 17:42     ` Aleksandr Nogikh
  0 siblings, 0 replies; 7+ messages in thread
From: Aleksandr Nogikh @ 2020-10-29 17:42 UTC (permalink / raw)
  To: Johannes Berg
  Cc: Aleksandr Nogikh, David Miller, Jakub Kicinski, Eric Dumazet,
	Andrey Konovalov, Dmitry Vyukov, Marco Elver, LKML, netdev,
	linux-wireless, Willem de Bruijn

On Wed, Oct 28, 2020 at 10:25 PM Johannes Berg
<johannes@sipsolutions.net> wrote:
[...]
> Wouldn't it make more sense to push that a layer down
> into ieee80211_rx_napi(), or actually now perhaps even
> better ieee80211_rx_list(), so we get it even if the driver called that
> API in the first place?
>
> You might only care about hwsim at this point, but perhaps hwsim would
> get optimised ..

Yes, ieee80211_rx_list() seems to be a reasonable place to move these
annotations to. Thanks for the suggestion.

I've incorporated this change into v5:
https://lkml.kernel.org/r/20201029173620.2121359-1-aleksandrnogikh@gmail.com

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-10-29 17:42 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-28 18:20 [PATCH v4 0/3] net, mac80211, kernel: enable KCOV remote coverage collection for 802.11 frame handling Aleksandr Nogikh
2020-10-28 18:20 ` [PATCH v4 1/3] kernel: make kcov_common_handle consider the current context Aleksandr Nogikh
2020-10-28 18:20 ` [PATCH v4 2/3] net: add kcov handle to skb extensions Aleksandr Nogikh
2020-10-28 20:42   ` Willem de Bruijn
2020-10-28 18:20 ` [PATCH v4 3/3] mac80211: add KCOV remote annotations to incoming frame processing Aleksandr Nogikh
2020-10-28 19:24   ` Johannes Berg
2020-10-29 17:42     ` Aleksandr Nogikh

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.