From patchwork Thu May 21 20:07:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 218762 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=-6.6 required=3.0 tests=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 autolearn=ham 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 1ACA8C433DF for ; Thu, 21 May 2020 20:08:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EE7AB207D3 for ; Thu, 21 May 2020 20:08:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="atShi6pi" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729944AbgEUUIB (ORCPT ); Thu, 21 May 2020 16:08:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51666 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726814AbgEUUIB (ORCPT ); Thu, 21 May 2020 16:08:01 -0400 Received: from mail-pg1-x542.google.com (mail-pg1-x542.google.com [IPv6:2607:f8b0:4864:20::542]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C3F39C061A0E; Thu, 21 May 2020 13:08:00 -0700 (PDT) Received: by mail-pg1-x542.google.com with SMTP id u5so3771249pgn.5; Thu, 21 May 2020 13:08:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=jEH0Av+/h2ap9TKD35Gd3IZLyPPmODgO6Lkjc2gXzJM=; b=atShi6piwwU9fnDvasB2qiC0HZlLLS9ByPNWNXWFrBPYaf8FLdcfm4nCW/D6NK7+ov M51Xp+uUbT7nvwXOSQEM8KzW2SgoN7YmnbM42LUXxQZYYObL/h+6JBsfCoE8nhZ42zLv xT1UuY4goX0GiSQLTnNu7tSonc20z4WzCQjx1B6nKLtCL1WA+szNzF5Ze8q8IOWyeckQ d80n+D7fewF6Z6Gw+IWrT2DNoRq8zyjfPKuzN3ckGLzNZ5538fDz6sZA843+glLuatz6 FH3c1haUMlJMCYIRfjMZf1G3MfOx0MDKAOSBUprkDKnk4/X7q0usSNqDG32SPE5X/Hy7 5Hxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=jEH0Av+/h2ap9TKD35Gd3IZLyPPmODgO6Lkjc2gXzJM=; b=LCY+X1dCE9QtzOsRgDVuEiA/3WRoo8fwaFS6Zw/LmZaQM8PYuNrJBi9LbPgyuwaWkF URewPaDm1PDEDOwVM+qp5l3I0U7zYMp6Cz3qZXjKxnX5bBOO0pTC2AE4RfcHn6f92V9o dlkgZBMoaPe0saCgR+rVwxC0mJCkPabeNOU9w/0bgXWPrtygkOyEHrrDuXWRXSwd6hC2 cKR542OYsTQPp5VU/lnfyYN6vn+tVsxO8o8a4UEEJhvB+Ls+oGSuNC1gslMTD2XB1hQV 7Xb07QIgYj/pvh74fZtq1hugTUvC0tQZKo/SzUj3eTr26XzBrA2uhIlRrPL4x5/tDDXq HcDA== X-Gm-Message-State: AOAM533fiOBs5F+WQy4p10Hq6i6ulr4vgA83M3Ay28HXuDZQN39znEAV h9l+mZ71TPu0/GW12uq4BuNCnhSf X-Google-Smtp-Source: ABdhPJzp3QkWxEcYmLscwq26nfzfkF4FEUIlw8sQ0HUyM37ViW/3VpVtjJhJCbm/z8GqNHV6vLv8PA== X-Received: by 2002:a62:e10e:: with SMTP id q14mr392127pfh.88.1590091680385; Thu, 21 May 2020 13:08:00 -0700 (PDT) Received: from [127.0.1.1] ([184.63.162.180]) by smtp.gmail.com with ESMTPSA id o14sm5156276pfp.89.2020.05.21.13.07.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 21 May 2020 13:07:59 -0700 (PDT) Subject: [bpf-next PATCH v2 2/4] bpf: selftests, verifier case for non null pointer check branch taken From: John Fastabend To: yhs@fb.com, andrii.nakryiko@gmail.com, ast@kernel.org, daniel@iogearbox.net Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, john.fastabend@gmail.com Date: Thu, 21 May 2020 13:07:46 -0700 Message-ID: <159009166599.6313.1593680633787453767.stgit@john-Precision-5820-Tower> In-Reply-To: <159009128301.6313.11384218513010252427.stgit@john-Precision-5820-Tower> References: <159009128301.6313.11384218513010252427.stgit@john-Precision-5820-Tower> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org When we have pointer type that is known to be non-null and comparing against zero we only follow the non-null branch. This adds tests to cover this case for reference tracking. Also add the other case when comparison against a non-zero value and ensure we still fail with unreleased reference. Signed-off-by: John Fastabend --- .../testing/selftests/bpf/verifier/ref_tracking.c | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tools/testing/selftests/bpf/verifier/ref_tracking.c b/tools/testing/selftests/bpf/verifier/ref_tracking.c index 604b461..056e027 100644 --- a/tools/testing/selftests/bpf/verifier/ref_tracking.c +++ b/tools/testing/selftests/bpf/verifier/ref_tracking.c @@ -821,3 +821,36 @@ .result = REJECT, .errstr = "invalid mem access", }, +{ + "reference tracking: branch tracking valid pointer null comparison", + .insns = { + BPF_SK_LOOKUP(sk_lookup_tcp), + BPF_MOV64_REG(BPF_REG_6, BPF_REG_0), + BPF_MOV64_IMM(BPF_REG_3, 1), + BPF_JMP_IMM(BPF_JNE, BPF_REG_6, 0, 1), + BPF_MOV64_IMM(BPF_REG_3, 0), + BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 2), + BPF_MOV64_REG(BPF_REG_1, BPF_REG_6), + BPF_EMIT_CALL(BPF_FUNC_sk_release), + BPF_EXIT_INSN(), + }, + .prog_type = BPF_PROG_TYPE_SCHED_CLS, + .result = ACCEPT, +}, +{ + "reference tracking: branch tracking valid pointer value comparison", + .insns = { + BPF_SK_LOOKUP(sk_lookup_tcp), + BPF_MOV64_REG(BPF_REG_6, BPF_REG_0), + BPF_MOV64_IMM(BPF_REG_3, 1), + BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 4), + BPF_MOV64_IMM(BPF_REG_3, 0), + BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 1234, 2), + BPF_MOV64_REG(BPF_REG_1, BPF_REG_6), + BPF_EMIT_CALL(BPF_FUNC_sk_release), + BPF_EXIT_INSN(), + }, + .prog_type = BPF_PROG_TYPE_SCHED_CLS, + .errstr = "Unreleased reference", + .result = REJECT, +}, From patchwork Thu May 21 20:08:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 218761 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=-6.6 required=3.0 tests=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, URIBL_BLOCKED autolearn=ham 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 5811DC433E0 for ; Thu, 21 May 2020 20:08:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3A00720823 for ; Thu, 21 May 2020 20:08:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="epK5J1uY" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730146AbgEUUIk (ORCPT ); Thu, 21 May 2020 16:08:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726814AbgEUUIk (ORCPT ); Thu, 21 May 2020 16:08:40 -0400 Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CFA5C061A0E; Thu, 21 May 2020 13:08:40 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id a5so3762282pjh.2; Thu, 21 May 2020 13:08:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=h8798kykb/Brr0NlbljMYDJsYj/j/qJhu/3+WCso6Tc=; b=epK5J1uYN8YNE1E8c45S1rfMeYoE9cSTW90L3IUND9tO9Cwc1fBXdpPKsLJaBkDrv7 y//y6K7X5ulv5pyFdWtvoetaBqXr8HOHUcuBMJwYOxU4+Hh0lgwbJPTP/s/c7WslMJ++ S2CMNFD8vO6oI/Qae/8/v/zjirc5Ah0J0ZFxz40logz/oaC2RLALGswil0zdzavXD3l4 a9jZwSX5s01F8X0ewk0bna/9CQF/nd7DU5EoGOwXrHyjva3L0otTBYtO8JUSU6dV4/qF TkCzFO9fdK/9hgQoVVjbYq9S91MzvWUT9bkwtyvjZQgYUEsIEYNrnncErc0rUoQFLPt8 T3HA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=h8798kykb/Brr0NlbljMYDJsYj/j/qJhu/3+WCso6Tc=; b=YgiIFsE5GBFdLZHqhNBINMNWTVumaWUMDqW/D/yXFr9sdfcwnwiVleFEewXP+KPc6u FTgX0cXYDcTjRUBAkbNua168eP6Ij0lDhrDrLKy/i5RLY6mAtliD4JAo4o6CMkqj2efc ik9tQt3oG2Ygx4Q4xq0nwZ6JbUUx2nrBalXwtlcFWGwKUbTkO5HxQdAXa/cOL3bjscF4 WARIA7wMfLKGUZh1dTM+kdcT9ySLsordSJpd4i+le4e+UDst6gkq/UZQPVJ4Z5cQtOGT z9BPtXqLQX68cHBltCMuRWcunK0mPtqsbBW+HqimJqpDINBWBvoQDA2dDbxOCgKQkbs1 SNog== X-Gm-Message-State: AOAM532flEAz4zwQVMo7P9awZ2zlW2rEjEg1Xg+tLC9UvYBzpAGpOEdr +K9bsa9CPtKx9VVqH701w2Y= X-Google-Smtp-Source: ABdhPJz1V77OVN0A5a85/y8hSJtTxsg2bQYoWCIKXLOdNkY3bFJYHAhYGXtpvXMmkCPcmz7w6N2JBQ== X-Received: by 2002:a17:902:b68d:: with SMTP id c13mr846910pls.210.1590091720009; Thu, 21 May 2020 13:08:40 -0700 (PDT) Received: from [127.0.1.1] ([184.63.162.180]) by smtp.gmail.com with ESMTPSA id q3sm4364649pgp.69.2020.05.21.13.08.31 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 21 May 2020 13:08:39 -0700 (PDT) Subject: [bpf-next PATCH v2 4/4] bpf: selftests, add printk to test_sk_lookup_kern to encode null ptr check From: John Fastabend To: yhs@fb.com, andrii.nakryiko@gmail.com, ast@kernel.org, daniel@iogearbox.net Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, john.fastabend@gmail.com Date: Thu, 21 May 2020 13:08:26 -0700 Message-ID: <159009170603.6313.1715279795045285176.stgit@john-Precision-5820-Tower> In-Reply-To: <159009128301.6313.11384218513010252427.stgit@john-Precision-5820-Tower> References: <159009128301.6313.11384218513010252427.stgit@john-Precision-5820-Tower> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Adding a printk to test_sk_lookup_kern created the reported failure where a pointer type is checked twice for NULL. Lets add it to the progs test test_sk_lookup_kern.c so we test the case from C all the way into the verifier. We already have printk's in selftests so seems OK to add another one. Acked-by: Andrii Nakryiko Signed-off-by: John Fastabend --- .../selftests/bpf/progs/test_sk_lookup_kern.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/testing/selftests/bpf/progs/test_sk_lookup_kern.c b/tools/testing/selftests/bpf/progs/test_sk_lookup_kern.c index d2b38fa..e83d0b4 100644 --- a/tools/testing/selftests/bpf/progs/test_sk_lookup_kern.c +++ b/tools/testing/selftests/bpf/progs/test_sk_lookup_kern.c @@ -73,6 +73,7 @@ int bpf_sk_lookup_test0(struct __sk_buff *skb) tuple_len = ipv4 ? sizeof(tuple->ipv4) : sizeof(tuple->ipv6); sk = bpf_sk_lookup_tcp(skb, tuple, tuple_len, BPF_F_CURRENT_NETNS, 0); + bpf_printk("sk=%d\n", sk ? 1 : 0); if (sk) bpf_sk_release(sk); return sk ? TC_ACT_OK : TC_ACT_UNSPEC;