From patchwork Thu Oct 29 17:36:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aleksandr Nogikh X-Patchwork-Id: 315614 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.6 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7B902C2D0A3 for ; Thu, 29 Oct 2020 17:37:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 20CC82076D for ; Thu, 29 Oct 2020 17:37:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Qh7iYeJw" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728156AbgJ2Rg6 (ORCPT ); Thu, 29 Oct 2020 13:36:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727373AbgJ2Rg6 (ORCPT ); Thu, 29 Oct 2020 13:36:58 -0400 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CE2CDC0613CF; Thu, 29 Oct 2020 10:36:57 -0700 (PDT) Received: by mail-wm1-x344.google.com with SMTP id w23so605838wmi.4; Thu, 29 Oct 2020 10:36:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TB+DUFEnKNYJXJ7LZxFHnOPKfGyG/Mg3M5rLxoluigk=; b=Qh7iYeJwxrFrzso7bTrTPPTL1IlIclaWX7zNptG2g7B9U4vuiPzVqvzNVKvJlg9zt8 s+J9bUcDUvm8D5Itg/YxW7HRKPZvr4FwxWMSyCcnJqwZMw3nGhNccXjDohGKjvdQrOt+ 7X8RX4/QOMjMcLL300aoBTR4Q9jhH6r7djpCM7U9dc44Ul0bjTOIOyab0ygzgqHWu/g3 /goC6QCeJQjV7QMN3QvedAyntonsaSfzV34J2PiqN0FC35e1aPWdJWA1AFvbnZT8D0jV pZX8kiQuJ/BvPXzL8SRIF5sa6ozDBpU3EjAAX4UBBiqZiUmPoeJjsqyUmZbaX3b2lqkb QiRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TB+DUFEnKNYJXJ7LZxFHnOPKfGyG/Mg3M5rLxoluigk=; b=t4p+/f/e61oRBma3erI3DjgoTPd9AB4tyRkAR7V5F3HPuqQ7lEqitTcBbQLueRCKn6 VcQWNr7VNNm/vU6HMazmwVYBUW4JQMVahu0Lgvmc0MSXgN+M+aWNjjvI3bhQiIHOj0FJ TSOaBEuHTpsRlLW1wEqIIxHOEohGBk+4claA1QN3UEw3bIM50t5zHbJOUVFwLfjIi0Gk wuiMzANfgiEGnftAFdURyjYu5j34XXnIX4Dgb+HjQNyjMHz+922hl+qUj795XSapxbyU n9R4DKwe3IXMo8z9WS9MGQjHlHj2zNrW1znsn4qvokeB9N9EOzTtVbN75hm8HHHE4VTM qEPA== X-Gm-Message-State: AOAM532RhKRR+CYFAJasKDoRhd89DGLEOrExha0auafVcNFJGuY8wFXe WUsXJKF1WsPAEjIqUdTM0rs= X-Google-Smtp-Source: ABdhPJwv+dMDxHi+FmnULoPMfVYEAgPflZ9cis8xNrdMt+tDZDBnPsSoEUhQYueibuKdMi8ERMFbrQ== X-Received: by 2002:a1c:a145:: with SMTP id k66mr196783wme.177.1603993016660; Thu, 29 Oct 2020 10:36:56 -0700 (PDT) Received: from nogikh.c.googlers.com.com (88.140.78.34.bc.googleusercontent.com. [34.78.140.88]) by smtp.gmail.com with ESMTPSA id t4sm852122wmb.20.2020.10.29.10.36.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Oct 2020 10:36:56 -0700 (PDT) From: Aleksandr Nogikh To: davem@davemloft.net, kuba@kernel.org, johannes@sipsolutions.net Cc: edumazet@google.com, andreyknvl@google.com, dvyukov@google.com, elver@google.com, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-wireless@vger.kernel.org, willemdebruijn.kernel@gmail.com, Aleksandr Nogikh Subject: [PATCH v5 1/3] kernel: make kcov_common_handle consider the current context Date: Thu, 29 Oct 2020 17:36:18 +0000 Message-Id: <20201029173620.2121359-2-aleksandrnogikh@gmail.com> X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog In-Reply-To: <20201029173620.2121359-1-aleksandrnogikh@gmail.com> References: <20201029173620.2121359-1-aleksandrnogikh@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org From: Aleksandr Nogikh 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 Reviewed-by: Andrey Konovalov --- 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); From patchwork Thu Oct 29 17:36:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aleksandr Nogikh X-Patchwork-Id: 315615 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.6 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6D7C2C2D0A3 for ; Thu, 29 Oct 2020 17:37:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 07FC32076A for ; Thu, 29 Oct 2020 17:37:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="qH4T72sB" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728326AbgJ2RhE (ORCPT ); Thu, 29 Oct 2020 13:37:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728216AbgJ2RhB (ORCPT ); Thu, 29 Oct 2020 13:37:01 -0400 Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C59E6C0613CF; Thu, 29 Oct 2020 10:36:59 -0700 (PDT) Received: by mail-wr1-x442.google.com with SMTP id b8so3735267wrn.0; Thu, 29 Oct 2020 10:36:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=EE2UZTtouYkdRt9VsdJlUNUI9Bx0xFL0RJikrCr3W/k=; b=qH4T72sBD3aFcMkN7XAt9TOHLxIjRmPBcM2oaMTaYbUcvH16zNuIIBGB6+V+ViM2WJ 99qNGVNgYX8tuU/mYX9j4WX9OkiHY62105Uyg2PYpiLC4qAjNF51FNiorkv02vSBYisT nrOvKP7qKl8qWMFBzgDhsP8Pzfy9YKZ3Wmegrs2cXufwqBR47KH9MVCtpOQ8gLea7H6d Ip9MWZc7UvE4O0JduYBjverWhgK2EFNNKLTz0VYAtAT/lJC+oF3L4w+Ei+85zbueBmMx JXkw4jdPuCkVmJumkc6T7/ftrd7QoonyNlFAU8+GdTRVdBc4uGpqI8/nFmsujdKY/czn MGzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=EE2UZTtouYkdRt9VsdJlUNUI9Bx0xFL0RJikrCr3W/k=; b=ebQkt7038CsNY1Mc2FmJQrnDnx3fu421rEAyvEPOuaWzzCV81YojP25tcZJ8GTz4B7 7tkQeKXP+Fp+WjZuEixKLUbBe/9dhz6iidLpbbNPPwIjn+3sy/VVGG/1q/T1tZLJWLwc b7SvyL+oONZ75N1r1Vj5d7hqTlNCLOh7pDbg9S9CrKwjUb/G2qZqFumAnEhETIVt+Tjm ugYVZt8TpJR6MkTULvnHxj2yNDVq3NzigEiy9KX2G0x+4MFpurjgiZQ5K1CTeYYxKUm5 dLdEq4I0GOWkFkxqLb/STHohIwUPlF9PnCvsSUu0833RC63H4NKS1u3rDiF1zunskxfy ACoA== X-Gm-Message-State: AOAM531OxO06xl03zkuTfFzZDMUv5rjoqOdMHpQsY2fUf3jl0lyW+cmp GZ20LVEh/0dur56yrIEEhIo7OLIZ4kz7/g== X-Google-Smtp-Source: ABdhPJzO2HQw3IQI9U7ZAE0CzF/fyysKV7IQ8mMnw5R92YS4dDebTZsJpzD8R/+KGf8k5ypj5xybYg== X-Received: by 2002:a5d:5703:: with SMTP id a3mr7209148wrv.67.1603993018558; Thu, 29 Oct 2020 10:36:58 -0700 (PDT) Received: from nogikh.c.googlers.com.com (88.140.78.34.bc.googleusercontent.com. [34.78.140.88]) by smtp.gmail.com with ESMTPSA id t4sm852122wmb.20.2020.10.29.10.36.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Oct 2020 10:36:57 -0700 (PDT) From: Aleksandr Nogikh To: davem@davemloft.net, kuba@kernel.org, johannes@sipsolutions.net Cc: edumazet@google.com, andreyknvl@google.com, dvyukov@google.com, elver@google.com, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-wireless@vger.kernel.org, willemdebruijn.kernel@gmail.com, Aleksandr Nogikh , Willem de Bruijn Subject: [PATCH v5 2/3] net: add kcov handle to skb extensions Date: Thu, 29 Oct 2020 17:36:19 +0000 Message-Id: <20201029173620.2121359-3-aleksandrnogikh@gmail.com> X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog In-Reply-To: <20201029173620.2121359-1-aleksandrnogikh@gmail.com> References: <20201029173620.2121359-1-aleksandrnogikh@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org From: Aleksandr Nogikh 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 Acked-by: Willem de Bruijn --- 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; }