From patchwork Wed Feb 1 13:57:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13124346 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 74166C636D3 for ; Wed, 1 Feb 2023 13:58:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1FD976B0075; Wed, 1 Feb 2023 08:58:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1AC676B007B; Wed, 1 Feb 2023 08:58:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 074D96B007D; Wed, 1 Feb 2023 08:58:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id EDDEA6B0075 for ; Wed, 1 Feb 2023 08:58:37 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id A537A80DE5 for ; Wed, 1 Feb 2023 13:58:37 +0000 (UTC) X-FDA: 80418878274.24.8444D80 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf10.hostedemail.com (Postfix) with ESMTP id E1146C002A for ; Wed, 1 Feb 2023 13:58:35 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=XJ3SKUrQ; spf=pass (imf10.hostedemail.com: domain of jolsa@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=jolsa@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675259916; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Mjx+8xVvWjllN48WqXEcZO36K7F05llFubGc6MsHgnk=; b=JBIs8BpKgNY42wQZevu8bwiit5lMbMkIeONXvgl4hVIXTA8ZpZheF/l8JDdP2kqnJ17UvB NXl5aU70rcGrvKp/qH75AfBz4cPuHoMn5tXWviEtQAhrNEQStUJSsabJgVPcXjmNSOwzCn 06a9MjZ5X2+BiiMjXgYNNnBkSSbFhUA= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=XJ3SKUrQ; spf=pass (imf10.hostedemail.com: domain of jolsa@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=jolsa@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675259916; a=rsa-sha256; cv=none; b=siCA+nHAE+IOV2AUvcVv1F07vtJlStVBELbj2bG8/3t8LVe2oTBe/bv50BvrXZQ+q5YsQL XFHklyrN439dQ4xp5IwLnR36iRhQWoc1Azx+eEmSzciPvnTbR1TAA/SeadDeeyAvpnbgoH xXjhxPx5Z5u7gjRYUe0/BH822iBxbcw= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 08A11617B6; Wed, 1 Feb 2023 13:58:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ABA6EC433EF; Wed, 1 Feb 2023 13:58:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1675259914; bh=9vGidv5KK+VK6x+sZbtxgG6kbbBhATOZQnveCyOWlzU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XJ3SKUrQ3KRicKqjZGSlmATsqBY/2uxl+ittryw9F94qxr3IHhp8yiAmNVgZV4F28 BvIzPZXif+N2jAVTa87Q4OyhJ+2kVDPKOU/IfJHNjiICJ3BTzDoCKr81v0+8jZF2/3 tyG7KooQLVd33ycGsDNsM+PVm4rVb8RcSASZfDR52CRsHXcm23iTtf32iutEA3ibfw mw2MTKI3hM/tHgLmqSy652HghxvCdCrkKNsbVbt5x3rkV5FjD4Jg+YJvC+SdbAj0nR i3tcDShTPNAaM96Nt1HVkQOA9p15tNuJ5D77H274EWMbGtBD12KW7WqTpQGtJ9TkDo L9AEDZVsw0XpA== From: Jiri Olsa To: Alexei Starovoitov , Andrii Nakryiko , Hao Luo , Andrew Morton , Alexander Viro , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo Cc: bpf@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-perf-users@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Daniel Borkmann Subject: [PATCH RFC 4/5] selftests/bpf: Add file_build_id test Date: Wed, 1 Feb 2023 14:57:36 +0100 Message-Id: <20230201135737.800527-5-jolsa@kernel.org> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230201135737.800527-1-jolsa@kernel.org> References: <20230201135737.800527-1-jolsa@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: E1146C002A X-Stat-Signature: y5utk4h1gzf6x8nojsc35hntzcx59p1w X-Rspam-User: X-HE-Tag: 1675259915-890673 X-HE-Meta: U2FsdGVkX18vKKW1/f3GZxkF637gKwsV+5oRKfncZgLeRVM4gofu8hqm17Mi9aoR59P/dhjC5aCZrT+9pmmeKTrIC42hkFdZOsvmE6KviPArYv6+4UaFUMcjxKJAkJ/auYbTEJoF7YCwek26vgROWyE2406/+N3+8IKso+u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6HWOJ+YjwzfZl5mx3vqeDaXD1KpSBvPvOn4Y3DXo60+zUea8BYw8YVEmDelUHbsQdZg/b4JkI8OnN6CvQpmLrGDJMUljBAj7YzimMaYRFIXUovRcVou14lETEKE1Q+aQo8M+8pcc0gyseNEoT9o3gZ/IhBmxglMdPerWF9aeB6A8P4X4JwRxN9yKfYN411JDpKw4njCuiPjGbHLl6tdIlntkIgaX409u0tepEe X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The test attaches bpf program to sched_process_exec tracepoint and gets build of executed file from bprm->file object. Signed-off-by: Jiri Olsa --- .../selftests/bpf/prog_tests/file_build_id.c | 70 +++++++++++++++++++ .../selftests/bpf/progs/file_build_id.c | 34 +++++++++ tools/testing/selftests/bpf/trace_helpers.c | 35 ++++++++++ tools/testing/selftests/bpf/trace_helpers.h | 1 + 4 files changed, 140 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/file_build_id.c create mode 100644 tools/testing/selftests/bpf/progs/file_build_id.c diff --git a/tools/testing/selftests/bpf/prog_tests/file_build_id.c b/tools/testing/selftests/bpf/prog_tests/file_build_id.c new file mode 100644 index 000000000000..a7b6307cc0f7 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/file_build_id.c @@ -0,0 +1,70 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include "file_build_id.skel.h" +#include "trace_helpers.h" + +#define BUILDID_STR_SIZE (BPF_BUILD_ID_SIZE*2 + 1) + +void test_file_build_id(void) +{ + int go[2], err, child_pid, child_status, c = 1, i; + char bpf_build_id[BUILDID_STR_SIZE] = {}; + struct file_build_id *skel; + char *bid = NULL; + + skel = file_build_id__open_and_load(); + if (!ASSERT_OK_PTR(skel, "file_build_id__open_and_load")) + return; + + if (!ASSERT_OK(pipe(go), "pipe")) + goto out; + + child_pid = fork(); + if (child_pid < 0) + goto out; + + /* child */ + if (child_pid == 0) { + /* wait for parent's pid update */ + err = read(go[0], &c, 1); + if (!ASSERT_EQ(err, 1, "child_read_pipe")) + exit(err); + + execle("/bin/bash", "bash", "-c", "exit 0", NULL, NULL); + exit(errno); + } + + /* parent, update child's pid and kick it */ + skel->bss->pid = child_pid; + + err = file_build_id__attach(skel); + if (!ASSERT_OK(err, "file_build_id__attach")) + goto out; + + err = write(go[1], &c, 1); + if (!ASSERT_EQ(err, 1, "child_write_pipe")) + goto out; + + /* wait for child to exit */ + waitpid(child_pid, &child_status, 0); + if (!ASSERT_EQ(WEXITSTATUS(child_status), 0, "child_exit_value")) + goto out; + + if (!ASSERT_OK(read_buildid("/bin/bash", &bid), "read_buildid")) + goto out; + + ASSERT_EQ(skel->bss->build_id_size, strlen(bid)/2, "build_id_size"); + + /* Convert bpf build id to string, so we can compare it later. */ + for (i = 0; i < skel->bss->build_id_size; i++) { + sprintf(bpf_build_id + i*2, "%02x", + (unsigned char) skel->bss->build_id[i]); + } + ASSERT_STREQ(bpf_build_id, bid, "build_id_data"); + +out: + file_build_id__destroy(skel); + free(bid); +} diff --git a/tools/testing/selftests/bpf/progs/file_build_id.c b/tools/testing/selftests/bpf/progs/file_build_id.c new file mode 100644 index 000000000000..639a7217a927 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/file_build_id.c @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include +#include +#include + +char _license[] SEC("license") = "GPL"; + +int pid; +u32 build_id_size; +char build_id[20]; + +SEC("tp_btf/sched_process_exec") +int BPF_PROG(prog, struct task_struct *p, pid_t old_pid, struct linux_binprm *bprm) +{ + int cur_pid = bpf_get_current_pid_tgid() >> 32; + struct build_id *bid; + + if (pid != cur_pid) + return 0; + + if (!bprm->file || !bprm->file->f_bid) + return 0; + + bid = bprm->file->f_bid; + build_id_size = bid->sz; + + if (build_id_size > 20) + return 0; + + memcpy(build_id, bid->data, 20); + return 0; +} diff --git a/tools/testing/selftests/bpf/trace_helpers.c b/tools/testing/selftests/bpf/trace_helpers.c index 09a16a77bae4..f5557890e383 100644 --- a/tools/testing/selftests/bpf/trace_helpers.c +++ b/tools/testing/selftests/bpf/trace_helpers.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include "trace_helpers.h" @@ -230,3 +231,37 @@ ssize_t get_rel_offset(uintptr_t addr) fclose(f); return -EINVAL; } + +int read_buildid(const char *path, char **build_id) +{ + char tmp[] = "/tmp/dataXXXXXX"; + char buf[PATH_MAX + 200]; + int err, fd; + FILE *f; + + fd = mkstemp(tmp); + if (fd == -1) + return -1; + close(fd); + + snprintf(buf, sizeof(buf), + "readelf -n %s 2>/dev/null | grep 'Build ID' | awk '{print $3}' > %s", + path, tmp); + + err = system(buf); + if (err) + goto out; + + f = fopen(tmp, "r"); + if (f) { + if (fscanf(f, "%ms$*\n", build_id) != 1) { + *build_id = NULL; + err = -1; + } + fclose(f); + } + +out: + unlink(tmp); + return err; +} diff --git a/tools/testing/selftests/bpf/trace_helpers.h b/tools/testing/selftests/bpf/trace_helpers.h index 53efde0e2998..1a38c808b6c2 100644 --- a/tools/testing/selftests/bpf/trace_helpers.h +++ b/tools/testing/selftests/bpf/trace_helpers.h @@ -23,4 +23,5 @@ void read_trace_pipe(void); ssize_t get_uprobe_offset(const void *addr); ssize_t get_rel_offset(uintptr_t addr); +int read_buildid(const char *path, char **build_id); #endif