Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp1605479pxb; Thu, 4 Nov 2021 05:28:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzPqHa5Agz3m1qtT95aFN9FBaVRBFKJ1L17knIZLYAvrx4HdeByE0wrgnPulPVy5wgSiSPf X-Received: by 2002:a17:906:c301:: with SMTP id s1mr59921845ejz.56.1636028891965; Thu, 04 Nov 2021 05:28:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1636028891; cv=none; d=google.com; s=arc-20160816; b=kFXlp5jN5yoqKs5QrCmVwKbBLFsEKWXLF/8FipiDktnCDNpjJqlEsb9+dVwMG8DHxp PVs+otmzh8oCoMN9InzW/hAjfyjkDrCiIbs8tWd1W3xhV2FPgUdHte7qkYJXJa3i5jxX OH3Uf+O44g+Voq4ecKyTpzuMS0Nk/ijyidLbvs24cZH027wRcA2epysp5etfDIPlvIPK 6wXHsqKFq5NWRBxrLiRTB2yUIjIXrAw8SHmyAzxVpH/G+l/J8lTW+F1Fg6AmywwoFbyO c0yW2xpKld4KGhd7vuf5irgEFT76u9l5N2sDS1MHrmN1G9GCwLrbIXYbxxkUzSTI8GR4 bwDw== 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=DbdddvL3v1nKg0sWX7VozKa+Fy24SlndwT2dHYiunUc=; b=Q+MKQyIiKQxRv1hLal99eUDAr/duHcsJkNhAwGZomR0kAujXURNI64P1I7sYDLbgZd mxFW2bDdDZPyGOqPGl/3BV+aQHq7OQq5esmi5vOjiWipX8H9v/MRd7qUWgFibReTSgcS etOZhSJq08o/F4Ycr5LGrqRU3cGWc/LRsJN+5pdwbQlDEXzGYQa84YgC5Y1sRTCImDPt W3qk65Xz8mbJ9l324PNI4oBdOPWGWXxkP3EIGphUM9KBPKFTuCljvNNq5wAj07wwuuWX Om+a2rvlWADHXcPFAjTdDG3c9nTanO6ZVCMZpf3MZRKrtCIFS+XAvHQ527/Nbm4kNOHs K5AQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google header.b=Y1NZqfG2; 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 e6si7605188ejb.600.2021.11.04.05.27.41; Thu, 04 Nov 2021 05:28:11 -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=Y1NZqfG2; 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 S231605AbhKDM0Q (ORCPT + 99 others); Thu, 4 Nov 2021 08:26:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231571AbhKDM0M (ORCPT ); Thu, 4 Nov 2021 08:26:12 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C59E8C061714 for ; Thu, 4 Nov 2021 05:23:32 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id b184-20020a1c1bc1000000b0033140bf8dd5so4103651wmb.5 for ; Thu, 04 Nov 2021 05:23:32 -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=DbdddvL3v1nKg0sWX7VozKa+Fy24SlndwT2dHYiunUc=; b=Y1NZqfG2WZeoFgUOeBVL3uxtDdgUxoYu61tz/ya9iSTTtDPS/up2RrnH14i5S7soGP XZKJWXhM7MLPtrbP0iTxmzw5SppG8O8RvuxZJG5oZtgQub/NM6IioDpuOdGSIvC8nfWH 91miaDB4lDiOA50K0Ai4pNr8Am7zM1VU9mcps= 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=DbdddvL3v1nKg0sWX7VozKa+Fy24SlndwT2dHYiunUc=; b=dsD2C3HRshIV4hBqUnLq5T/uLtb6CUJ+el60SrqZNDa0p+P28IgOl3pK5iGUxX9Ihb gTVoo8eOJrEnk5Yb2xNJL16AIS+RE5miCjfPkN+w1VAr//QMBRUzAKmx5j9MhTYp7erV Rk9CS+66EkARcFcTn7ppWOOxfNXbhxVDxUAu9Dpl+egkGZOs2oFfI/4LIy8IWLC2q5zD p44+qwzwOZUJZID/imMDAJkirdzab2CJWf2uJFTk7Nl1Upa1ssDQkK9EFKe58w6nOXf3 a7Ml1Flhy1rhWdHzlKYNTX7vO3jnNY7JCl6hTI3LxMQy0rrBqmucOPDCLmZ0D0HbdD9V Cyyw== X-Gm-Message-State: AOAM5335KUYumQsLoIQxj3u6adL4TfKPUv6abxTJk5DWsQaY+lUCmajp iZpBv4vaK11Tv8EUg1cOlEdYZA== X-Received: by 2002:a1c:448b:: with SMTP id r133mr10372348wma.85.1636028611422; Thu, 04 Nov 2021 05:23:31 -0700 (PDT) Received: from kharboze.dr-pashinator-m-d.gmail.com.beta.tailscale.net (cust97-dsl60.idnet.net. [212.69.60.97]) by smtp.gmail.com with ESMTPSA id a4sm4797535wmb.39.2021.11.04.05.23.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Nov 2021 05:23:31 -0700 (PDT) From: Mark Pashmfouroush To: markpash@cloudflare.com, Shuah Khan , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: kernel-team@cloudflare.com, linux-kselftest@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next v2 2/2] selftests/bpf: Add tests for accessing ifindex in bpf_sk_lookup Date: Thu, 4 Nov 2021 12:23:04 +0000 Message-Id: <20211104122304.962104-3-markpash@cloudflare.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211104122304.962104-1-markpash@cloudflare.com> References: <20211104122304.962104-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 6db07401bc49..57846cc7ce36 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