Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp2994431lqo; Tue, 21 May 2024 03:50:53 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUE+4GXxraKlDCFFmM38ftPhwwkg8L+52WJZPN7njgA+2qN5G3HvaeiwNotCERx6LNOEGVu9atWRqW5stTkfMi6/QrZ5SdzrB36GD1HwQ== X-Google-Smtp-Source: AGHT+IFR1LG42SK5VOpciW6uLTxBBTw4K1aR8uvpDz5+9tVSjxyjw2QXkCHy5qeF6HHeP5nCjfI5 X-Received: by 2002:a05:620a:7284:b0:78d:5065:c5df with SMTP id af79cd13be357-792c7598d0cmr3094892985a.18.1716288652860; Tue, 21 May 2024 03:50:52 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716288652; cv=pass; d=google.com; s=arc-20160816; b=YoiKQAMMZrjoI3RCuTXLWcKdt/5guV65rPqIWV1f7HUx/URv/w/LD1kQl5bSDzogor Hs6GPaVBSmlW0sIVmIJ/LB1LTrA/2kM+LsopMMNUwSMofO/TakFrYD4I84MFwhVYgS+f YU6w4vVtTw+7g7sptkNjhATTmR2QpFMGDS/e2XeglT5AAYkw3Do9/MSx1tTC1U7S/bH3 cByKoT6ZLvAGd9S0lW2pnZaIdgHHNicSL/Yv/bB4mttW19Aqq9FdW7Y4Yx8sMszgGJv+ 0oXFzWr1KW/iz2vgvTL20r6m3YodR369fTv/9oh9VL5GRBysMVbvsJnCH6Y2xljaatmp KwhA== 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=yd9MF9eVPrT7Kq34zArOGRUFm5Ul95kHUcouatnFakc=; fh=nmDo0evxWwTAvBlxNELHY/fYZbxD4K9pluaUjPt1MrQ=; b=w/bkIru9pN2J+bO3Y/NIt2ab5UOikAmrWTEaofZIsfdIQf4Gp0M8LCw88zYhfWOHbY PHu6q+L6cQMRVtYIouqnzQgzSyEl9ofF9zaP7/xssYc7INtGIuWj67Ln4x5lDSflFBOZ 4WYWmwwhLIuxTAzPrGFMy4RUF//z9SKhok3quRYDVnsRixK1ydjyh2e+yxX8ThOdqkWs ov5rigswMTCLYiVyUDHZm6z/MuiaWOCowKW64N2aWPsvCRlZqyES1ryKJoTvXVqr/P2n 862oFsAp5DKW6D3l8tyPRIZCQ2AnEvJYbUZ84jgvnr0M9yrZwdmoLFBrc4xW6Jk98qE9 OENw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=oND2ZFGZ; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-184827-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-184827-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id af79cd13be357-7930bd3b8f8si689252385a.102.2024.05.21.03.50.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 03:50:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-184827-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=oND2ZFGZ; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-184827-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-184827-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 870991C21808 for ; Tue, 21 May 2024 10:50:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3D681770E5; Tue, 21 May 2024 10:50:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="oND2ZFGZ" 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 4372F73194; Tue, 21 May 2024 10:49:59 +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=1716288600; cv=none; b=GEb7dUaaXsVDvOKLFFKLzNKfe6nbx1YxVupRYgN0TNi+4m4M90Z1IjIecqLV/5dvyyClsKSkIBhhxbDGZYuaArWTBapRmkOjZ5odF4xjvHFmoj88cd+VrOd/RpdfynoQ926GoBBxa1qyWOrY9GBFtU4P7/uVpvlVoi+bkFPrl88= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716288600; c=relaxed/simple; bh=fhIu4hEsIOsOTozlOQHoi5IPGvZcip9NRTcmMNDhE4Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OM7BZYx2cv5ado/7ye+qFm32AmKPBvpIKK7AkPz0HSxJOF5jGeS1fuiutfNAHV9DMhZyEsF46P8JJDGIL5pGaXwN1qOaoTe8/1eted0BNskbompcBKIYw5yefzJ3BZ51hejjZmz1jNhE/kYOtBa64fB8dofkEJcOKYJJTx19KEc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oND2ZFGZ; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id A219EC2BD11; Tue, 21 May 2024 10:49:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1716288599; bh=fhIu4hEsIOsOTozlOQHoi5IPGvZcip9NRTcmMNDhE4Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oND2ZFGZF9ZsQhTJXctFSkiioRlt7bfrvUOptnlkArTj2H1140BYyJwv9uZ5Udp1E UBBJ1vB4y9bnTBCK42VU0FhFtJ1ZI0GWZACrd1HC9nX0lCwEbTJhrLUsn2DpunTH3m TEsFIx5oqv27CLcNegjQ7rJsDB2wiI3tQGdI3yAzxjDoX+/X03WPluxMs6XQIOOSij 8fpidg+tvEqWI8E11RLPB+RootPNHkVh3CnUFUuXwJPjJwhI82j39pq1S74Gb7vEe/ 6gz9PbX6gBi1/d+L/uU3Ki7uRc81RYfiELt0JCWpPhU7Z771odDlwiOq5UTO40TE3w lTPOdSemIroFg== 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: [PATCHv6 bpf-next 7/9] selftests/bpf: Add uretprobe syscall call from user space test Date: Tue, 21 May 2024 12:48:23 +0200 Message-ID: <20240521104825.1060966-8-jolsa@kernel.org> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240521104825.1060966-1-jolsa@kernel.org> References: <20240521104825.1060966-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 to verify that when called from outside of the trampoline provided by kernel, the uretprobe syscall will cause calling process to receive SIGILL signal and the attached bpf program is not executed. Acked-by: Andrii Nakryiko Reviewed-by: Masami Hiramatsu (Google) Signed-off-by: Jiri Olsa --- .../selftests/bpf/prog_tests/uprobe_syscall.c | 95 +++++++++++++++++++ .../bpf/progs/uprobe_syscall_executed.c | 17 ++++ 2 files changed, 112 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/uprobe_syscall_executed.c diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c b/tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c index 1a50cd35205d..3ef324c2db50 100644 --- a/tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c @@ -7,7 +7,10 @@ #include #include #include +#include +#include #include "uprobe_syscall.skel.h" +#include "uprobe_syscall_executed.skel.h" __naked unsigned long uretprobe_regs_trigger(void) { @@ -209,6 +212,91 @@ static void test_uretprobe_regs_change(void) } } +#ifndef __NR_uretprobe +#define __NR_uretprobe 462 +#endif + +__naked unsigned long uretprobe_syscall_call_1(void) +{ + /* + * Pretend we are uretprobe trampoline to trigger the return + * probe invocation in order to verify we get SIGILL. + */ + asm volatile ( + "pushq %rax\n" + "pushq %rcx\n" + "pushq %r11\n" + "movq $" __stringify(__NR_uretprobe) ", %rax\n" + "syscall\n" + "popq %r11\n" + "popq %rcx\n" + "retq\n" + ); +} + +__naked unsigned long uretprobe_syscall_call(void) +{ + asm volatile ( + "call uretprobe_syscall_call_1\n" + "retq\n" + ); +} + +static void test_uretprobe_syscall_call(void) +{ + LIBBPF_OPTS(bpf_uprobe_multi_opts, opts, + .retprobe = true, + ); + struct uprobe_syscall_executed *skel; + int pid, status, err, go[2], c; + + if (ASSERT_OK(pipe(go), "pipe")) + return; + + skel = uprobe_syscall_executed__open_and_load(); + if (!ASSERT_OK_PTR(skel, "uprobe_syscall_executed__open_and_load")) + goto cleanup; + + pid = fork(); + if (!ASSERT_GE(pid, 0, "fork")) + goto cleanup; + + /* child */ + if (pid == 0) { + close(go[1]); + + /* wait for parent's kick */ + err = read(go[0], &c, 1); + if (err != 1) + exit(-1); + + uretprobe_syscall_call(); + _exit(0); + } + + skel->links.test = bpf_program__attach_uprobe_multi(skel->progs.test, pid, + "/proc/self/exe", + "uretprobe_syscall_call", &opts); + if (!ASSERT_OK_PTR(skel->links.test, "bpf_program__attach_uprobe_multi")) + goto cleanup; + + /* kick the child */ + write(go[1], &c, 1); + err = waitpid(pid, &status, 0); + ASSERT_EQ(err, pid, "waitpid"); + + /* verify the child got killed with SIGILL */ + ASSERT_EQ(WIFSIGNALED(status), 1, "WIFSIGNALED"); + ASSERT_EQ(WTERMSIG(status), SIGILL, "WTERMSIG"); + + /* verify the uretprobe program wasn't called */ + ASSERT_EQ(skel->bss->executed, 0, "executed"); + +cleanup: + uprobe_syscall_executed__destroy(skel); + close(go[1]); + close(go[0]); +} #else static void test_uretprobe_regs_equal(void) { @@ -219,6 +307,11 @@ static void test_uretprobe_regs_change(void) { test__skip(); } + +static void test_uretprobe_syscall_call(void) +{ + test__skip(); +} #endif void test_uprobe_syscall(void) @@ -227,4 +320,6 @@ void test_uprobe_syscall(void) test_uretprobe_regs_equal(); if (test__start_subtest("uretprobe_regs_change")) test_uretprobe_regs_change(); + if (test__start_subtest("uretprobe_syscall_call")) + test_uretprobe_syscall_call(); } diff --git a/tools/testing/selftests/bpf/progs/uprobe_syscall_executed.c b/tools/testing/selftests/bpf/progs/uprobe_syscall_executed.c new file mode 100644 index 000000000000..0d7f1a7db2e2 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/uprobe_syscall_executed.c @@ -0,0 +1,17 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "vmlinux.h" +#include +#include + +struct pt_regs regs; + +char _license[] SEC("license") = "GPL"; + +int executed = 0; + +SEC("uretprobe.multi") +int test(struct pt_regs *regs) +{ + executed = 1; + return 0; +} -- 2.45.0