Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp392413rwr; Thu, 20 Apr 2023 00:29:38 -0700 (PDT) X-Google-Smtp-Source: AKy350YYU48HhFwifSjR3CBPM1rYO6vi6W4K86TK5wOSJ41EmGPHXAuiKYrVllxZpLtMMBqaaGpA X-Received: by 2002:a17:902:aa87:b0:1a4:fc40:bef6 with SMTP id d7-20020a170902aa8700b001a4fc40bef6mr599268plr.36.1681975777850; Thu, 20 Apr 2023 00:29:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681975777; cv=none; d=google.com; s=arc-20160816; b=zE9ZkVC2hjCEpJXb9dmxovl/RCr2iHpSWKlgyVnY4XEfpHyv+hcMtsJEiFSeqEIQqi FP1GD2D3A11F7AaYnqhxEvz5ObFVBgXiuqvita3el16SaBdPZL8MFryfU0hOp9CIPAZn QbRMYMtWW8jdS6dkVgxUIfkAlJLMJz5CumXBVqY381/3evtZ/VqBBEiwpo1BtG5MqS1e UjY7R7MDoDwLcrH7tbaNsG9Aan1qYxohYvTCc9VskSHrEmF2hq5wuN2Dj1DUv31IpQbQ AubFFR4AZkLZPZw/Bz4N/3Imuuq8O2tqqCltPEnaQAPB4nF+r18JFq00g6VcguqXRW26 E57Q== 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=KY6KaCFZ5vdzzIXTUyV6D8NEGya8RvpCjdxKUFkIlbo=; b=iDEFOzC8woEq5GEqjtBlDD7fZ97smnWUkrO4yAjhcm98uiEDvEIWYas2XjDCxVhoIR CbHmBj4fp0OawvGvIz1pcPXTXdKN/AHpwOsVN8vT/5UoYfouvg4Nt7VesLyvNNw/BmC8 tsPT6O7pQf6aaWRtPpiFxS+N4gqMhBCQosFTBZiNBZW6WczrbUtDUon8Y0SqD/AGri9O v7Kw/8IgfQGt6fOkDGcPhGJIsokVKWKJWG+4NJ/Ecu+waIgGxtHbpyDXEHpUqQtpxZFd BQ37p4cM1Vl0bS0h3qLNM0N84FXqsSipr9oP8sJYfdzLMBlZd0osrtrbrtsdhEJUEuv3 5OKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=JdpK091t; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n14-20020a170903110e00b001a1f6671a6dsi1224694plh.397.2023.04.20.00.29.24; Thu, 20 Apr 2023 00:29:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=JdpK091t; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234074AbjDTH2T (ORCPT + 99 others); Thu, 20 Apr 2023 03:28:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234087AbjDTH2Q (ORCPT ); Thu, 20 Apr 2023 03:28:16 -0400 Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 100644200 for ; Thu, 20 Apr 2023 00:27:27 -0700 (PDT) Received: by mail-pg1-x532.google.com with SMTP id 41be03b00d2f7-517c840f181so405680a12.3 for ; Thu, 20 Apr 2023 00:27:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1681975646; x=1684567646; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=KY6KaCFZ5vdzzIXTUyV6D8NEGya8RvpCjdxKUFkIlbo=; b=JdpK091tQZhcpVQCrGmT53jyzk0tdpPMrMjsOt2nhOO6VvJm4CHj6b3MGcEtFPjrmj jRNPw5HUcEZWIJGHvsLxe344JI6DV18Gtby29KPcbI49o3qCSmqB71Eg+4RPCbqs4iPm v3YHKLR5mv2ycE2z5Uzvthf87ASaR/OaOaKaMkZbPpGN/o1brGSdxKSc6IochSv6/AbZ gDpcAdKFTBC8fGCg660IDSJI/tyA2ywbL5RjXTgpq/uufdxPrCODT/WuDOdKNukitBLv z7znofi2TQ82y2kjpWhFiI7HRNVKU1n6s9umJI6PADeGkDWPZdOKeULaonHTZo5nIUD8 LGig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681975646; x=1684567646; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KY6KaCFZ5vdzzIXTUyV6D8NEGya8RvpCjdxKUFkIlbo=; b=i8xxWQRoViIuL6LLk/rf/2cMAMEeniyDP0bJGJjF65+Smom3KIGXF4U6rpIEXhKzMd 4izODaH/UL2p7MsuISD+BBqxjYEMm0149Tw7knNLaHW1ARf1aTbLxaXkg+63sGF2HruY X5XT3niBvAxNCzfdwEN53fpKOYKZbWY35B78DTX+RoJca4gOqAAwbfDjMkTi8YrrjdfF njVx5Tngb1OHR0lCe1r30WX1g+FO2MjlmIjApokhY9ksitMgOLVWYoiMrJBWNV0q0vMt Q0FeluEFjpGMSQP93PB0/mUb2yDvpfElFYqJTUw1GNOFvXuew3B6viPXHLwL8r3ZHtzn XRdA== X-Gm-Message-State: AAQBX9eKGGLE/OGygchu3aQpvCFmWUI22DTJbNcXVy2q6GzsOjQJ7j+B OX7tFxO/IpRZAHCfabtlC7KjgA== X-Received: by 2002:a17:90a:d381:b0:249:7958:ea36 with SMTP id q1-20020a17090ad38100b002497958ea36mr847077pju.19.1681975646451; Thu, 20 Apr 2023 00:27:26 -0700 (PDT) Received: from C02F52LSML85.bytedance.net ([139.177.225.238]) by smtp.gmail.com with ESMTPSA id y18-20020a17090ad71200b0023440af7aafsm612160pju.9.2023.04.20.00.27.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Apr 2023 00:27:26 -0700 (PDT) From: Feng zhou To: martin.lau@linux.dev, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mykolal@fb.com, shuah@kernel.org Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, yangzhenze@bytedance.com, wangdongdong.6@bytedance.com, zhoufeng.zf@bytedance.com Subject: [PATCH bpf-next 2/2] selftests/bpf: Add testcase for bpf_task_under_cgroup Date: Thu, 20 Apr 2023 15:26:57 +0800 Message-Id: <20230420072657.80324-3-zhoufeng.zf@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20230420072657.80324-1-zhoufeng.zf@bytedance.com> References: <20230420072657.80324-1-zhoufeng.zf@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Feng Zhou test_progs: Tests new ebpf helpers bpf_task_under_cgroup. The bpf program saves the pid which call the getuid syscall within a given cgroup to a map to the remote_pid, which is convenient for the user-mode program to verify the test correctness. The user-mode program creates its own mount namespace, and mounts the cgroupsv2 hierarchy in there, call the getuid syscall, then check if remote_pid and local_pid are equal. Signed-off-by: Feng Zhou --- .../bpf/prog_tests/task_under_cgroup.c | 49 +++++++++++++++++++ .../bpf/progs/test_task_under_cgroup.c | 31 ++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c create mode 100644 tools/testing/selftests/bpf/progs/test_task_under_cgroup.c diff --git a/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c b/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c new file mode 100644 index 000000000000..4dd704b11a95 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2023 Bytedance */ + +#include +#include "test_task_under_cgroup.skel.h" + +#define FOO "/foo" + +void test_task_under_cgroup(void) +{ + struct test_task_under_cgroup *skel; + int ret, foo = -1, idx = 0; + + skel = test_task_under_cgroup__open(); + if (!ASSERT_OK_PTR(skel, "test_task_under_cgroup__open")) + return; + + skel->rodata->local_pid = getpid(); + + ret = test_task_under_cgroup__load(skel); + if (!ASSERT_OK(ret, "test_task_under_cgroup__load")) + goto cleanup; + + ret = test_task_under_cgroup__attach(skel); + if (!ASSERT_OK(ret, "test_task_under_cgroup__attach")) + goto cleanup; + + foo = test__join_cgroup(FOO); + if (!ASSERT_OK(foo < 0, "cgroup_join_foo")) + goto cleanup; + + ret = bpf_map_update_elem(bpf_map__fd(skel->maps.cgroup_map), &idx, + &foo, BPF_ANY); + if (!ASSERT_OK(ret < 0, "cgroup_map update")) + goto cleanup; + + syscall(__NR_getuid); + + test_task_under_cgroup__detach(skel); + + ASSERT_EQ(skel->bss->remote_pid, skel->rodata->local_pid, + "test task_under_cgroup"); + +cleanup: + if (foo) + close(foo); + + test_task_under_cgroup__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c b/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c new file mode 100644 index 000000000000..0f3d53f636de --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2023 Bytedance */ + +#include "vmlinux.h" +#include + +const volatile int local_pid; +int remote_pid; + +struct { + __uint(type, BPF_MAP_TYPE_CGROUP_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, __u32); +} cgroup_map SEC(".maps"); + +SEC("tp/syscalls/sys_enter_getuid") +int sysenter_getuid(const void *ctx) +{ + if (local_pid != (bpf_get_current_pid_tgid() >> 32)) + return 0; + + if (!bpf_task_under_cgroup(&cgroup_map, bpf_get_current_task_btf(), 0)) + return 0; + + remote_pid = local_pid; + + return 0; +} + +char _license[] SEC("license") = "GPL"; -- 2.20.1