Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp1863425lqt; Sun, 21 Apr 2024 12:43:27 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXZz3Q2mNmOQ1QYbGGIEyXuKxDQ0HyK6a6cTRZnRy99Ue7vjw7cr01u2noJr6MJ+GdeX3lmBDz+3vFiuJJqP79Nf2VgVKhHhrgBqw5pGA== X-Google-Smtp-Source: AGHT+IHd8qmCO2XHA9BEz5IoN/4Ip4XdnQAPgAQGGh0UceKtf74okeeoxZY3Dwm6ESgWmF7P6G5K X-Received: by 2002:a17:90a:8c4:b0:2a5:f19:978d with SMTP id 4-20020a17090a08c400b002a50f19978dmr6459195pjn.37.1713728607319; Sun, 21 Apr 2024 12:43:27 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713728607; cv=pass; d=google.com; s=arc-20160816; b=SERRUFPDI8hnulOl6JZWMH8wv0P8CD3Xxfqbxa4bcOK3wNoz0vnA/gjSyaHOJtsJwJ ub5Iyxr6TFiAukEoFwVfIMSoRVhYZ/KRwy9nMyHN1J3MOPChMiAMcq8g9bqpf9goRIOP OnjKeKwhKTSjT6RGm8lmMftBhkEXCP9kadZExUNAZ5U+ih1gAroYXlpsWmAsfPmRXWSs 1mUNs4nSIjgDl7SV9olmhEd2oMkWI3aBC9kApslv8uEiOTr8WuNB+yulge2x3bJaEjdN Y/8lyUNyC2MV8IHNrz17A+p+fP1dUvidYD/5kszrP+mSsTLSJI+449KPd4rgM9UqHN/f aT+g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=XooIH1ZiSTLuhRyUzq0rDYyVVzBqDxDizg8QDxHmU9Q=; fh=BGjS9EmzIs1KVwcaqROnfMbpy2e6oo3cY1daNsDmauE=; b=yHDd/diB5ZdRjkbsleU8uIaPcHIQ2LgnbZV5lPSXhfUgopmjqgOHwshIoK9TNlPRTa qj8xGKeQR39Fns79AdiTTs/HccRMusvfVcHVQgprnWblwCU0lJzOEZ3GeUqaVsnfcVJ/ 6vG14VHRRhntEpo+rvA7es3vj0hBW6Ca//pUgP4JLACzV4gw7L0DvuN7+lX/CKAzNRp+ pJ7xmhztEYuL5Q6KBF68qIw1vZrRIaSrNu8v2pUHwuW/8LqwOemFS6Tjrrl8thzx0iQG NiRrFy5MVucIo8KYozTCN7N/Eqa7ozr3K+UTQT+1AqKDrZY1WKPDJmTC11x4qwDOiXTq g/Ew==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Gc9SytYn; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-152600-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-152600-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id cp9-20020a17090afb8900b002a4aba1542asi8326503pjb.79.2024.04.21.12.43.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Apr 2024 12:43:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-152600-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Gc9SytYn; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-152600-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-152600-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id E82AF2811E1 for ; Sun, 21 Apr 2024 19:43:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CB30C42044; Sun, 21 Apr 2024 19:43:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Gc9SytYn" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A84DB3FB14; Sun, 21 Apr 2024 19:43:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713728584; cv=none; b=gtcWsyvfbsi6YhPdiDgkNip76dBUISNtRQJHIgvQGkItlMvf3tsXNKRzySSaHrf8cT2REfmUGdd5jYQP7JLoPVHFzwxbzxCwZ7RLHo7cXvg8AS80hHYnJu2cSKWfK6NmPGPB//YlZCkxCMQ5F11p3mODWEhPmmGjgiP3ue/HBt4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713728584; c=relaxed/simple; bh=ICIP2D4Qfft573U04UqTjchind/ZIj/yAT/llU/A5k0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CUxnL2Fzuqz5GU96AZH1YaUxjc6tY4yYrl9H3/orutSie5K7NaSbtsc0gjmfa7n2DB7R22eMlEKu9vqWqVeeN3WS+XDpWoKpX5Sy7q5s7pXy8n+1idSMv3b8eyg1p1oujn2ncXn4PgIhO74s7C3lH0SnaeywTmb9dPJxQO1zGEA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Gc9SytYn; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id F12E5C113CE; Sun, 21 Apr 2024 19:42:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713728584; bh=ICIP2D4Qfft573U04UqTjchind/ZIj/yAT/llU/A5k0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Gc9SytYnKoUHTSESPO8SHBF8ja7pmOqvoLiySMjlmyOrNfRQtcvFj8EmmLXLRPz30 qSRS8CYJZHZuh2xea9TqBVLn/nVI//DJ0WUjS7L13xhKRkHJ6BbfN3CrFZi/DsLwdJ f7TXdXzQxC6tWMafN+M0IOke78hxbV+olxEVWaug0Q9miSTgul2/QQKuQvbC5TK68W HSyx+IvMOqdC0T1IU+jJtMsQ0w+QdpjAPbiCNFBjzMNUx0Fapd5FXRtD6icNFBtcM1 LYlyhOX15zG7zDzLADoymQD1DrJTSLGZ4GIh/lD6DdVbnF/nEgeeg18qTrFY9bgiQc mVh9OiuZAaWzA== From: Jiri Olsa To: Steven Rostedt , Masami Hiramatsu , Oleg Nesterov , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-api@vger.kernel.org, x86@kernel.org, bpf@vger.kernel.org, Song Liu , Yonghong Song , John Fastabend , Peter Zijlstra , Thomas Gleixner , "Borislav Petkov (AMD)" , Ingo Molnar , Andy Lutomirski Subject: [PATCHv3 bpf-next 4/7] selftests/bpf: Add uretprobe syscall test for regs changes Date: Sun, 21 Apr 2024 21:42:03 +0200 Message-ID: <20240421194206.1010934-5-jolsa@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240421194206.1010934-1-jolsa@kernel.org> References: <20240421194206.1010934-1-jolsa@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Adding test that creates uprobe consumer on uretprobe which changes some of the registers. Making sure the changed registers are propagated to the user space when the ureptobe syscall trampoline is used on x86_64. To be able to do this, adding support to bpf_testmod to create uprobe via new attribute file: /sys/kernel/bpf_testmod_uprobe This file is expecting file offset and creates related uprobe on current process exe file and removes existing uprobe if offset is 0. The can be only single uprobe at any time. The uprobe has specific consumer that changes registers used in ureprobe syscall trampoline and which are later checked in the test. Acked-by: Andrii Nakryiko Signed-off-by: Jiri Olsa --- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 123 +++++++++++++++++- .../selftests/bpf/prog_tests/uprobe_syscall.c | 67 ++++++++++ 2 files changed, 189 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c index 39ad96a18123..c832cbb42e74 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "bpf_testmod.h" #include "bpf_testmod_kfunc.h" @@ -343,6 +344,119 @@ static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = { .write = bpf_testmod_test_write, }; +/* bpf_testmod_uprobe sysfs attribute is so far enabled for x86_64 only, + * please see test_uretprobe_regs_change test + */ +#ifdef __x86_64__ + +static int +uprobe_ret_handler(struct uprobe_consumer *self, unsigned long func, + struct pt_regs *regs) + +{ + regs->ax = 0x12345678deadbeef; + regs->cx = 0x87654321feebdaed; + regs->r11 = (u64) -1; + return true; +} + +struct testmod_uprobe { + struct path path; + loff_t offset; + struct uprobe_consumer consumer; +}; + +static DEFINE_MUTEX(testmod_uprobe_mutex); + +static struct testmod_uprobe uprobe = { + .consumer.ret_handler = uprobe_ret_handler, +}; + +static int testmod_register_uprobe(loff_t offset) +{ + int err = -EBUSY; + + if (uprobe.offset) + return -EBUSY; + + mutex_lock(&testmod_uprobe_mutex); + + if (uprobe.offset) + goto out; + + err = kern_path("/proc/self/exe", LOOKUP_FOLLOW, &uprobe.path); + if (err) + goto out; + + err = uprobe_register_refctr(d_real_inode(uprobe.path.dentry), + offset, 0, &uprobe.consumer); + if (err) + path_put(&uprobe.path); + else + uprobe.offset = offset; + +out: + mutex_unlock(&testmod_uprobe_mutex); + return err; +} + +static void testmod_unregister_uprobe(void) +{ + mutex_lock(&testmod_uprobe_mutex); + + if (uprobe.offset) { + uprobe_unregister(d_real_inode(uprobe.path.dentry), + uprobe.offset, &uprobe.consumer); + uprobe.offset = 0; + } + + mutex_unlock(&testmod_uprobe_mutex); +} + +static ssize_t +bpf_testmod_uprobe_write(struct file *file, struct kobject *kobj, + struct bin_attribute *bin_attr, + char *buf, loff_t off, size_t len) +{ + unsigned long offset; + int err; + + if (kstrtoul(buf, 0, &offset)) + return -EINVAL; + + if (offset) + err = testmod_register_uprobe(offset); + else + testmod_unregister_uprobe(); + + return err ?: strlen(buf); +} + +static struct bin_attribute bin_attr_bpf_testmod_uprobe_file __ro_after_init = { + .attr = { .name = "bpf_testmod_uprobe", .mode = 0666, }, + .write = bpf_testmod_uprobe_write, +}; + +static int register_bpf_testmod_uprobe(void) +{ + return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_uprobe_file); +} + +static void unregister_bpf_testmod_uprobe(void) +{ + testmod_unregister_uprobe(); + sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_uprobe_file); +} + +#else +static int register_bpf_testmod_uprobe(void) +{ + return 0; +} + +static void unregister_bpf_testmod_uprobe(void) { } +#endif + BTF_KFUNCS_START(bpf_testmod_common_kfunc_ids) BTF_ID_FLAGS(func, bpf_iter_testmod_seq_new, KF_ITER_NEW) BTF_ID_FLAGS(func, bpf_iter_testmod_seq_next, KF_ITER_NEXT | KF_RET_NULL) @@ -650,7 +764,13 @@ static int bpf_testmod_init(void) return ret; if (bpf_fentry_test1(0) < 0) return -EINVAL; - return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file); + ret = sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file); + if (ret < 0) + return ret; + ret = register_bpf_testmod_uprobe(); + if (ret < 0) + return ret; + return 0; } static void bpf_testmod_exit(void) @@ -664,6 +784,7 @@ static void bpf_testmod_exit(void) msleep(20); sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file); + unregister_bpf_testmod_uprobe(); } module_init(bpf_testmod_init); diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c b/tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c index 311ac19d8992..1a50cd35205d 100644 --- a/tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c @@ -149,15 +149,82 @@ static void test_uretprobe_regs_equal(void) cleanup: uprobe_syscall__destroy(skel); } + +#define BPF_TESTMOD_UPROBE_TEST_FILE "/sys/kernel/bpf_testmod_uprobe" + +static int write_bpf_testmod_uprobe(unsigned long offset) +{ + size_t n, ret; + char buf[30]; + int fd; + + n = sprintf(buf, "%lu", offset); + + fd = open(BPF_TESTMOD_UPROBE_TEST_FILE, O_WRONLY); + if (fd < 0) + return -errno; + + ret = write(fd, buf, n); + close(fd); + return ret != n ? (int) ret : 0; +} + +static void test_uretprobe_regs_change(void) +{ + struct pt_regs before = {}, after = {}; + unsigned long *pb = (unsigned long *) &before; + unsigned long *pa = (unsigned long *) &after; + unsigned long cnt = sizeof(before)/sizeof(*pb); + unsigned int i, err, offset; + + offset = get_uprobe_offset(uretprobe_regs_trigger); + + err = write_bpf_testmod_uprobe(offset); + if (!ASSERT_OK(err, "register_uprobe")) + return; + + uretprobe_regs(&before, &after); + + err = write_bpf_testmod_uprobe(0); + if (!ASSERT_OK(err, "unregister_uprobe")) + return; + + for (i = 0; i < cnt; i++) { + unsigned int offset = i * sizeof(unsigned long); + + switch (offset) { + case offsetof(struct pt_regs, rax): + ASSERT_EQ(pa[i], 0x12345678deadbeef, "rax"); + break; + case offsetof(struct pt_regs, rcx): + ASSERT_EQ(pa[i], 0x87654321feebdaed, "rcx"); + break; + case offsetof(struct pt_regs, r11): + ASSERT_EQ(pa[i], (__u64) -1, "r11"); + break; + default: + if (!ASSERT_EQ(pa[i], pb[i], "register before-after value check")) + fprintf(stdout, "failed register offset %u\n", offset); + } + } +} + #else static void test_uretprobe_regs_equal(void) { test__skip(); } + +static void test_uretprobe_regs_change(void) +{ + test__skip(); +} #endif void test_uprobe_syscall(void) { if (test__start_subtest("uretprobe_regs_equal")) test_uretprobe_regs_equal(); + if (test__start_subtest("uretprobe_regs_change")) + test_uretprobe_regs_change(); } -- 2.44.0