From patchwork Wed Aug 11 22:27:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 12432059 X-Patchwork-Delegate: bpf@iogearbox.net 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=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL 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 2EC73C4338F for ; Wed, 11 Aug 2021 22:27:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 128946101E for ; Wed, 11 Aug 2021 22:27:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232545AbhHKW2R (ORCPT ); Wed, 11 Aug 2021 18:28:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232319AbhHKW2Q (ORCPT ); Wed, 11 Aug 2021 18:28:16 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC341C061765 for ; Wed, 11 Aug 2021 15:27:52 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id p71-20020a25424a0000b029056092741626so4075349yba.19 for ; Wed, 11 Aug 2021 15:27:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=DMaGpGpDFx/eWPsONiwHAvxlw8b7WueF5ejMV9lg1IM=; b=ZBT5+l7+B+sMrD2HXZvD+UcepjaegcwbhX2Lw3WhWH0cl2zi23vhLymzAPhvgGUrHw DSANX9OeG6knW1PfUcUFuEYirerCwA3vuYsO6K0gIkvsjlTSW+jY41I9SRc+2XAlYYZL eXy8dCUDC+u/18GOPychVljK8R/4/4WYC5QasTDpEh1pcCnnzQ8vMAzhjmoFavhkTS5u CjfpB1khlZeVbCjQ/Yg/wW4p/2f1pLxbc9F+NWxWZT+T4IFzcItHHIQ0ybmScgiDIIVx J0u03cQAt2nrgpqn4j72hhxnqodGjg1jFjm1v/SmbwUN7bRdRQRH14sYMu3AdWTRTSct DVgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=DMaGpGpDFx/eWPsONiwHAvxlw8b7WueF5ejMV9lg1IM=; b=c1/riPtlwmpfFWGzWQYWVOJuCb8x3gfyQDiHFG2E6b7mjhYUkw+Zd4UyCYBUdTvGAS +gCbiZyaAE9HcbtUMIZWg2jTJJtEPNqMwmzZOWhrcVNTo2/Yg1sPnnAfsv9CCbIGBVTF NIJlX+cMuO2IeyudoWXoN2avBllfCNS7dpgCMUEtqDxTqji7nO7DcIb+JDGU/CqHxQMu dnAu7h89PTlCswHCbfo+EVVsF6GAxrRPxe1+YOh+0lrLZv/sAzqGlpUys4Atbdh6kFFW Neef5tpD+BCXYHtP+oEmdKQncMpkwpYqpTGE/CuWBsE8zKwwL7O4vLKyH+I28vjzdbu9 Tkhg== X-Gm-Message-State: AOAM533WdEYk55wzhDSY6XvSdtZV7D+dWg13mi7FCkQgpGIvL0ibdREQ ZBJ37aFimVLjgNcrvWRyHLiK1Xo= X-Google-Smtp-Source: ABdhPJy3uM21txOa70ZVyG2UWlDoqXk4C9HFKyFD9Y6MACW2+oN3ZE9eAFxD+alNJ9UAhqFmkqhOESI= X-Received: from sdf2.svl.corp.google.com ([2620:15c:2c4:201:c78e:f5dc:8780:ed29]) (user=sdf job=sendgmr) by 2002:a25:ce01:: with SMTP id x1mr338537ybe.360.1628720871965; Wed, 11 Aug 2021 15:27:51 -0700 (PDT) Date: Wed, 11 Aug 2021 15:27:46 -0700 In-Reply-To: <20210811222747.3041445-1-sdf@google.com> Message-Id: <20210811222747.3041445-2-sdf@google.com> Mime-Version: 1.0 References: <20210811222747.3041445-1-sdf@google.com> X-Mailer: git-send-email 2.33.0.rc1.237.g0d66db33f3-goog Subject: [PATCH bpf-next 1/2] bpf: Allow bpf_get_netns_cookie in BPF_PROG_TYPE_CGROUP_SOCKOPT From: Stanislav Fomichev To: netdev@vger.kernel.org, bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, Stanislav Fomichev Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net This is similar to existing BPF_PROG_TYPE_CGROUP_SOCK and BPF_PROG_TYPE_CGROUP_SOCK_ADDR. Signed-off-by: Stanislav Fomichev Reported-by: kernel test robot --- kernel/bpf/cgroup.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c index b567ca46555c..2428ecf2b2cf 100644 --- a/kernel/bpf/cgroup.c +++ b/kernel/bpf/cgroup.c @@ -1846,10 +1846,27 @@ const struct bpf_verifier_ops cg_sysctl_verifier_ops = { const struct bpf_prog_ops cg_sysctl_prog_ops = { }; +BPF_CALL_1(bpf_get_netns_cookie_sockopt, struct bpf_sockopt_kern *, ctx) +{ + struct sock *sk = ctx ? ctx->sk : NULL; + const struct net *net = sk ? sock_net(sk) : &init_net; + + return net->net_cookie; +} + +static const struct bpf_func_proto bpf_get_netns_cookie_sockopt_proto = { + .func = bpf_get_netns_cookie_sockopt, + .gpl_only = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_CTX_OR_NULL, +}; + static const struct bpf_func_proto * cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) { switch (func_id) { + case BPF_FUNC_get_netns_cookie: + return &bpf_get_netns_cookie_sockopt_proto; #ifdef CONFIG_NET case BPF_FUNC_sk_storage_get: return &bpf_sk_storage_get_proto; From patchwork Wed Aug 11 22:27:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 12432061 X-Patchwork-Delegate: bpf@iogearbox.net 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=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL 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 B9912C4338F for ; Wed, 11 Aug 2021 22:27:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9C0A060FD9 for ; Wed, 11 Aug 2021 22:27:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232586AbhHKW2T (ORCPT ); Wed, 11 Aug 2021 18:28:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232564AbhHKW2T (ORCPT ); Wed, 11 Aug 2021 18:28:19 -0400 Received: from mail-qv1-xf4a.google.com (mail-qv1-xf4a.google.com [IPv6:2607:f8b0:4864:20::f4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2F354C0613D3 for ; Wed, 11 Aug 2021 15:27:55 -0700 (PDT) Received: by mail-qv1-xf4a.google.com with SMTP id ay14-20020a056214048eb0290357469934easo2151482qvb.8 for ; Wed, 11 Aug 2021 15:27:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=FKmBhBBmRifwAw1wud9HfRzter1UnR4c92xc+2b6/5s=; b=KtorBMIjMc7PRX+Eu8q9UZfgz7ozjZS/YIQxhZ4KL2/4npw/Ogxow4rDpoky8ge4wv GshTjP/p/8DWUfc0HZ0Y+Y4ouTaLdunkIN62NhsetlQ3m6MJ94G9o87D9Bm22V193IzH 31YGKStXfGLmoTo+khXW+88VsRbjHBxAuF4Hp96MM/EzVXEuHdn6gAuQm1Xj4tDdmueY 0eXLXctdGi/e+K3LxI4858JIrEa+lnb5ywM5jMd8Nc73ZKL5CLgZ5957OdyDbR9PM6ET +9bc1Gpucb7t5QEBxooDyIXpHAWE27/4h1D5vXWwiab1J0GYLfyrygDL+Vvw+Rzf+jmn JzWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=FKmBhBBmRifwAw1wud9HfRzter1UnR4c92xc+2b6/5s=; b=UVnLE/3Xn5Wh/OGc/SYGqWgDpfw5paNo7No9AtroKoh3uVfW3QamknsoeDzCWNbIv5 Ae0x22qm5NiXM2f1Ma7Xzr6O2AhWQx33jN2pZIa9SImRFZACmUlPjZuwyOTFDdGjwKwP O41c4ZZtqEQgw5r2IP05geSTB2PoZrFbwUBDc4a0GxyCq6B/6wyelWPMf8CTzTRANcLu GiGGP/RRojS8l5A3Jf8P/SR9txt3OteBcNboNmH5kXMRcUtVs+VsFwlQBQxYATUVyGB4 WADT/GMwgoBBix2/ATmP8rQG/0YfhVuY8tQhUcnH3cLEnS4cPy1xy+8PwQRvzmEDvrzB juaA== X-Gm-Message-State: AOAM532cu0Xi83tHIyc4QVnlNaaHaiHNnTLppeRv55SQrxvrc9nNC+u5 XVh9w6xzx+vvYlgvAJU9gzJuvdU= X-Google-Smtp-Source: ABdhPJwLgIPSwzO3YGooos4qruEZJUyvIjVnraAgI9++Ha844zvdXoIktyQ1NnwmY8hMo+rSbaGgx68= X-Received: from sdf2.svl.corp.google.com ([2620:15c:2c4:201:c78e:f5dc:8780:ed29]) (user=sdf job=sendgmr) by 2002:a0c:a321:: with SMTP id u30mr846227qvu.57.1628720874259; Wed, 11 Aug 2021 15:27:54 -0700 (PDT) Date: Wed, 11 Aug 2021 15:27:47 -0700 In-Reply-To: <20210811222747.3041445-1-sdf@google.com> Message-Id: <20210811222747.3041445-3-sdf@google.com> Mime-Version: 1.0 References: <20210811222747.3041445-1-sdf@google.com> X-Mailer: git-send-email 2.33.0.rc1.237.g0d66db33f3-goog Subject: [PATCH bpf-next 2/2] selftests/bpf: verify bpf_get_netns_cookie in BPF_PROG_TYPE_CGROUP_SOCKOPT From: Stanislav Fomichev To: netdev@vger.kernel.org, bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, Stanislav Fomichev Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Add verifier ctx test to call bpf_get_netns_cookie from cgroup/setsockopt. #269/p pass ctx or null check, 1: ctx Did not run the program (not supported) OK #270/p pass ctx or null check, 2: null Did not run the program (not supported) OK #271/p pass ctx or null check, 3: 1 OK #272/p pass ctx or null check, 4: ctx - const OK #273/p pass ctx or null check, 5: null (connect) Did not run the program (not supported) OK #274/p pass ctx or null check, 6: null (bind) Did not run the program (not supported) OK #275/p pass ctx or null check, 7: ctx (bind) Did not run the program (not supported) OK #276/p pass ctx or null check, 8: null (bind) OK #277/p pass ctx or null check, 9: ctx (cgroup/setsockopt) Did not run the program (not supported) OK #278/p pass ctx or null check, 10: null (cgroup/setsockopt) Did not run the program (not supported) OK Signed-off-by: Stanislav Fomichev --- tools/testing/selftests/bpf/verifier/ctx.c | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tools/testing/selftests/bpf/verifier/ctx.c b/tools/testing/selftests/bpf/verifier/ctx.c index 23080862aafd..3e7fdbf898b1 100644 --- a/tools/testing/selftests/bpf/verifier/ctx.c +++ b/tools/testing/selftests/bpf/verifier/ctx.c @@ -195,3 +195,28 @@ .result = REJECT, .errstr = "R1 type=inv expected=ctx", }, +{ + "pass ctx or null check, 9: ctx (cgroup/setsockopt)", + .insns = { + BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, + BPF_FUNC_get_netns_cookie), + BPF_MOV64_IMM(BPF_REG_0, 0), + BPF_EXIT_INSN(), + }, + .prog_type = BPF_PROG_TYPE_CGROUP_SOCKOPT, + .expected_attach_type = BPF_CGROUP_SETSOCKOPT, + .result = ACCEPT, +}, +{ + "pass ctx or null check, 10: null (cgroup/setsockopt)", + .insns = { + BPF_MOV64_IMM(BPF_REG_1, 0), + BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, + BPF_FUNC_get_netns_cookie), + BPF_MOV64_IMM(BPF_REG_0, 0), + BPF_EXIT_INSN(), + }, + .prog_type = BPF_PROG_TYPE_CGROUP_SOCKOPT, + .expected_attach_type = BPF_CGROUP_SETSOCKOPT, + .result = ACCEPT, +},