Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp647574pxb; Fri, 15 Oct 2021 13:00:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxgVPa7YlwGcnJ7Zj91wO8z5keI9Nhnnfh0k31VqveASnN5/8jrPUMqC0MrA1x1ggL2EceQ X-Received: by 2002:a63:b006:: with SMTP id h6mr2902388pgf.366.1634328023295; Fri, 15 Oct 2021 13:00:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634328023; cv=none; d=google.com; s=arc-20160816; b=NAvYID924F+z70mRD04r9kcESORBvCd6KEuFnhsEFeEVvHvhx6PFyJRhTGnLvLFupf SK6GlrT9L4VTmUgb/ANfT6p79y2iDwI0aoyXl0KLQMRVZmU+1jFwkxd0Y6LBELzhtc3J mU7CgdeDkTbL20RSWql2jcPKXP3pU6llrXgc3Ce/I7UkxUN6Z7jsDw63eONWsUAoO/DJ EXofGM3rJ+u7G1c0q9qKE+s6JRd3tNTx2X86n5HxMyBEM5L5sefBz05xPrg6EEmd1TKP OIFu3lnoTmctvly9Zf6k/ibQx2U9BLlvuY2hCM2xFLTH4X9YPYKnkF+WV8zv8/zNbTN5 kYJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=M2KhKg7mGMM/7Z7JCzghnvL0t/VBs24STBJ7ab9m2xs=; b=jX/BaaNsoxIMiwQTiSAvu2lH2mn29X3O3rV7EuLtuCLwcnO76Xvk2bkxPNg64Hi8RM Mq81tLO1l5Pgia9tveWogTlR6ucj5ULpZNXRR+Yj0p0fiaiqhyWcZNOh4aLeHWX7mxxG 6ZnxrA/4N0zc/ADVfSVk7wcFT++1gZwj+Q2LV6s+QGUtK6Iw3qCsNuvKXsbtTt4fU/PC g8ibP7fjU36PbGUjp2kYxzhvmrS20qE7H2zhi5Q4u2l22TSOrQ0Ou14zdvInGXr5U6+H FGSgv/uNzZvAov0BNHQUpthyz4AlRC2UBYOuMnxtBFOKvdgLldA9X5fwiEFMlCJRBxl3 +b9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google header.b=LEW7hQIj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id hk5si9482126pjb.114.2021.10.15.13.00.10; Fri, 15 Oct 2021 13:00:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google header.b=LEW7hQIj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238495AbhJOL01 (ORCPT + 99 others); Fri, 15 Oct 2021 07:26:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238489AbhJOL0Z (ORCPT ); Fri, 15 Oct 2021 07:26:25 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3063DC061766 for ; Fri, 15 Oct 2021 04:24:19 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id u8-20020a05600c440800b0030d90076dabso1448083wmn.1 for ; Fri, 15 Oct 2021 04:24:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=M2KhKg7mGMM/7Z7JCzghnvL0t/VBs24STBJ7ab9m2xs=; b=LEW7hQIj+g8zU+1jSb8o1CI776sI1BeWIOXbGZZw4qccJdVXXSW0t94y9WWmBac6b2 aX0Lvr7DSbMyUYzsR15w9Tb48Yk1Nmfs9+2aqCKKNr+ThpH/KmR/mRKYZD/uVQIn41Hn EnLoRi6U43BLhSQglz+SdeSzJtII2mbQhRlNI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=M2KhKg7mGMM/7Z7JCzghnvL0t/VBs24STBJ7ab9m2xs=; b=yuxZQx6BSO3DCEI+r2rX6KXVXdyU5jK/NVXKCQMRZVX1nBVh800p6RV/OF8pA9LJn8 tfXS67W6Ws7nfri7nesxtYW5ldY3AxfEefJSCQxCo4m6a0/YFgX1I3t8YwM/xjSd3XJ2 2yWkrdg7FfKddRDaSQpW0bg0NUZfjZL0ZHVZ2+7BjJpL7HrF4tiuXC3kJGaVhT1zxcwQ /HSde4YIghZ00DtYEdoiISXp3Z6buYit6SI648ExcMVXjTAmLSDclIJOZR7SS69REaDi TDYtWtKQIJk1N5fAxYzZ1SVxW7Y9Jlil51NXZnuo5+6Go97l5/6+XAGJT5Lywgv1MYK1 cK/w== X-Gm-Message-State: AOAM5312s4QycASuwaQUFnXv26VtPNaPr3uMRt7P/3cNEy0ZADHpk/C5 iUuQwiW3e/TP6sqn7ciKyuWorOPliJr7KUQq X-Received: by 2002:a7b:c193:: with SMTP id y19mr25939309wmi.125.1634297057696; Fri, 15 Oct 2021 04:24:17 -0700 (PDT) Received: from kharboze.dr-pashinator-m-d.gmail.com.beta.tailscale.net ([2a02:390:85ca:6:be5f:f4ff:fe85:e406]) by smtp.gmail.com with ESMTPSA id o12sm4631223wrv.78.2021.10.15.04.24.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Oct 2021 04:24:17 -0700 (PDT) From: Mark Pashmfouroush To: markpash@cloudflare.com Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , "David S. Miller" , Jakub Kicinski , Hideaki YOSHIFUJI , David Ahern , Shuah Khan , Florent Revest , Brendan Jackman , Joe Stringer , Jesper Dangaard Brouer , Hangbin Liu , =?UTF-8?q?Toke=20H=C3=B8iland-J=C3=B8rgensen?= , Lorenz Bauer , Dave Marchevsky , Luke Nelson , =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 2/2] selftests/bpf: Add tests for accessing ifindex in bpf_sk_lookup Date: Fri, 15 Oct 2021 12:23:30 +0100 Message-Id: <20211015112336.1973229-3-markpash@cloudflare.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211015112336.1973229-1-markpash@cloudflare.com> References: <20211015112336.1973229-1-markpash@cloudflare.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org A new field was added to the bpf_sk_lookup data that users can access. Add tests that validate that the new ifindex field contains the right data. Signed-off-by: Mark Pashmfouroush diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index aee41547e7f4..951e5dcf297e 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -937,6 +937,37 @@ static void test_drop_on_lookup(struct test_sk_lookup *skel) .connect_to = { EXT_IP6, EXT_PORT }, .listen_at = { EXT_IP6, INT_PORT }, }, + /* The program will drop on success, meaning that the ifindex + * was 1. + */ + { + .desc = "TCP IPv4 drop on valid ifindex", + .lookup_prog = skel->progs.check_ifindex, + .sotype = SOCK_STREAM, + .connect_to = { EXT_IP4, EXT_PORT }, + .listen_at = { EXT_IP4, EXT_PORT }, + }, + { + .desc = "TCP IPv6 drop on valid ifindex", + .lookup_prog = skel->progs.check_ifindex, + .sotype = SOCK_STREAM, + .connect_to = { EXT_IP6, EXT_PORT }, + .listen_at = { EXT_IP6, EXT_PORT }, + }, + { + .desc = "UDP IPv4 drop on valid ifindex", + .lookup_prog = skel->progs.check_ifindex, + .sotype = SOCK_DGRAM, + .connect_to = { EXT_IP4, EXT_PORT }, + .listen_at = { EXT_IP4, EXT_PORT }, + }, + { + .desc = "UDP IPv6 drop on valid ifindex", + .lookup_prog = skel->progs.check_ifindex, + .sotype = SOCK_DGRAM, + .connect_to = { EXT_IP6, EXT_PORT }, + .listen_at = { EXT_IP6, EXT_PORT }, + }, }; const struct test *t; diff --git a/tools/testing/selftests/bpf/progs/test_sk_lookup.c b/tools/testing/selftests/bpf/progs/test_sk_lookup.c index 19d2465d9442..0f3283bfe3b6 100644 --- a/tools/testing/selftests/bpf/progs/test_sk_lookup.c +++ b/tools/testing/selftests/bpf/progs/test_sk_lookup.c @@ -84,6 +84,14 @@ int lookup_drop(struct bpf_sk_lookup *ctx) return SK_DROP; } +SEC("sk_lookup") +int check_ifindex(struct bpf_sk_lookup *ctx) +{ + if (ctx->ifindex == 1) + return SK_DROP; + return SK_PASS; +} + SEC("sk_reuseport") int reuseport_pass(struct sk_reuseport_md *ctx) { diff --git a/tools/testing/selftests/bpf/verifier/ctx_sk_lookup.c b/tools/testing/selftests/bpf/verifier/ctx_sk_lookup.c index d78627be060f..0b3088da1e89 100644 --- a/tools/testing/selftests/bpf/verifier/ctx_sk_lookup.c +++ b/tools/testing/selftests/bpf/verifier/ctx_sk_lookup.c @@ -229,6 +229,24 @@ BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, offsetof(struct bpf_sk_lookup, local_port)), + /* 1-byte read from ifindex field */ + BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_sk_lookup, ifindex)), + BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_sk_lookup, ifindex) + 1), + BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_sk_lookup, ifindex) + 2), + BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_sk_lookup, ifindex) + 3), + /* 2-byte read from ifindex field */ + BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_sk_lookup, ifindex)), + BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_sk_lookup, ifindex) + 2), + /* 4-byte read from ifindex field */ + BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_sk_lookup, ifindex)), + /* 8-byte read from sk field */ BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, offsetof(struct bpf_sk_lookup, sk)), @@ -351,6 +369,20 @@ .expected_attach_type = BPF_SK_LOOKUP, .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS, }, +{ + "invalid 8-byte read from bpf_sk_lookup ifindex field", + .insns = { + BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, + offsetof(struct bpf_sk_lookup, ifindex)), + BPF_MOV32_IMM(BPF_REG_0, 0), + BPF_EXIT_INSN(), + }, + .errstr = "invalid bpf_context access", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_SK_LOOKUP, + .expected_attach_type = BPF_SK_LOOKUP, + .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS, +}, /* invalid 1,2,4-byte reads from 8-byte fields in bpf_sk_lookup */ { "invalid 4-byte read from bpf_sk_lookup sk field", -- 2.31.1