Received: by 2002:ab2:69cc:0:b0:1fd:c486:4f03 with SMTP id n12csp275262lqp; Tue, 11 Jun 2024 04:24:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXAE6VAuW2NDHBZDKjIqvTs4kDk6qKfXt+pqymeT7+VdUa6OtdvhTQSbl7PtK+pU86r5NTIepVxmyLem+vTLzlLKcUchn1gJN4kSxsByA== X-Google-Smtp-Source: AGHT+IEwUqoPiacTgubRJM2NWMVEY3d7PIzgoKdHW2nd5X94Eeurol0EIHjUP9jxLvhCo+nEU0Ko X-Received: by 2002:a05:6a20:5613:b0:1af:9ec6:afbd with SMTP id adf61e73a8af0-1b2f96a39d3mr9946072637.11.1718105048667; Tue, 11 Jun 2024 04:24:08 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718105048; cv=pass; d=google.com; s=arc-20160816; b=VHr4hY9XEd5rvWfrhr57voccPo8gvtp7D4aVWJIVay4wImWkbuxtzjmHOAsc3lCV8L iLaZIYV22xACy4oYFb97viEcBK/AfFqtqKMsnQbyeu5xpsXawp1R+MnUBxMzJqlYvac6 3MeUY2bRPaoXXqxCMNWNjOVTW5N4Z4eZasBuanutODjsTYWpgc2Vfoy/Fp7/fb8MvR9J oNks2n3EfKQcPv2AYZZrO2jXr9O/No18bCDTXc+1tjdrpNd5GUVuO0az/MkoejApM3dy d9Yizr3JHIGBRrUFhNaj8wl6NIgYi2JrCUxyz6x8SFqUJmPCQ8mVjegnJYDtOx4TNI+B szDw== 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=kweMqbJbiVd2zzaQtEYzuvfU5p5ert9gG1vFRZFfWgw=; fh=nmDo0evxWwTAvBlxNELHY/fYZbxD4K9pluaUjPt1MrQ=; b=HBk9VGglRQtwO0SkXOEFcoXeaEj7YikIoQs7xS2/mbLGjnp87o/tTiobWi0YNF8VXG ggYJ6m7ON/S1a4M2/XaN77O11DOJSdHbCMIM0ujqRVQ1k1fNGdfseC2izL8lZraxQuCu xICD/iZzwkTrV7AmFqnFhWG7VtLtn7jy4iaJXscbCib7r8LwEKeacOdBYYY61c/5QHuK 5CeQJykYlhT8nVIjcLXeMZQycWKB985cIAz74V17YAbnBFBChyZcHg9pRUGOP4mQYLwb 9ySdnksPXj+QSZbHyVC7LAHqUlB3IfOP3z4Q19BdRVsraRAD/BLpjyVnwFlUTun7jtgv 9tsg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=UFIshC4R; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-209722-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-209722-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. [139.178.88.99]) by mx.google.com with ESMTPS id d2e1a72fcca58-705a3055ab5si2103237b3a.320.2024.06.11.04.24.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jun 2024 04:24:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-209722-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=UFIshC4R; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-209722-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-209722-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 AE1A32853F1 for ; Tue, 11 Jun 2024 11:24:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F1C7017B403; Tue, 11 Jun 2024 11:23:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="UFIshC4R" 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 D593B17B402; Tue, 11 Jun 2024 11:23:19 +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=1718105000; cv=none; b=V4LWkRAGeI7tasoNp53Seitpcp5d9VtidLQQZ67GhjcV8iOFPumikVvTBZedS8p8Oir/dfaWSodeQ5qnyT2VMwq04nvGpq2R932CeDs+p7RllUBCAKPnDbW0yoFvlKLX0z+PRRwMoZde0dIZJh15CxeSHXZhk/B2cH3klG5wkAo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718105000; c=relaxed/simple; bh=/rGaUwZojZm+iVZUAVKKmhU9Rybqz7uXbb6C9HW4Hd4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sCedSZyhxhZF9dX/2a91BmZVsezX6ReAMUoCXdJpiaSlFA0iupg6F3QDQTaDuAoZNi+NJkgpXmzk5O2DgDwc3Q04PpqUfiCnZUtB5tbZ+mYEWUee0ddC4U6yVSxpCzI6dn0aLWFEMOulBUHZsJYWzOAMF11kIxXg9mRIHEuixX4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=UFIshC4R; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 54AF1C2BD10; Tue, 11 Jun 2024 11:23:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718104999; bh=/rGaUwZojZm+iVZUAVKKmhU9Rybqz7uXbb6C9HW4Hd4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UFIshC4RaZsaVk6NxvqntoUH/QDMZb7MMrx2y8FviPRHMaR6qKP0WPtyNeofoOe4U m8PEeeu6fHmAGJlSmC/PBjwDh+cLZvb03GPz+rzDGGRcaks3zaDraQOMfnToq1u1NS rKFyTj408jJ8MRTrnhhuXDT9f88kNe+BQkSvQqHj/Q5KzojtyrnMyq9ukM4JYldutB SF0G2tmrTJLVmDg8fOSwrXaEJYbfzCqOLFMgKDR4sH4XpAlJhQwSxy1c908yo63WYS ALVUDADa/XxJ1pMvR0VN4Cf3CHdOtN8KJcmmwPEz6K5sRpnrQZLLPylUdaFxkbVVpS vIGAtT1a/JTvQ== 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, linux-man@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 , "Edgecombe, Rick P" , Deepak Gupta Subject: [PATCHv8 bpf-next 6/9] selftests/bpf: Add uretprobe syscall test for regs changes Date: Tue, 11 Jun 2024 13:21:55 +0200 Message-ID: <20240611112158.40795-7-jolsa@kernel.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240611112158.40795-1-jolsa@kernel.org> References: <20240611112158.40795-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 Reviewed-by: Masami Hiramatsu (Google) 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 0a09732cde4b..6cbbecb1e23c 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -18,6 +18,7 @@ #include #include #include +#include #include "bpf_testmod.h" #include "bpf_testmod_kfunc.h" @@ -358,6 +359,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 = 0; + int err = 0; + + 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) @@ -912,7 +1026,13 @@ static int bpf_testmod_init(void) return -EINVAL; sock = NULL; mutex_init(&sock_lock); - 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) @@ -927,6 +1047,7 @@ static void bpf_testmod_exit(void) bpf_kfunc_close_sock(); 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.45.1