Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp2994020lqo; Tue, 21 May 2024 03:49:48 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVR0sqiODsWBUUkyppnjDayB5XsXdsuFwD/3+a0HrqeNWw7TNfD9Ewr0LyBkSC27FPwlIBR7I9MmnVQYDjAJpU1F7VeCJL0kxIK1XGkPA== X-Google-Smtp-Source: AGHT+IG1VWsd1FPowhsYS0RjWczJeeFN1jKMKV7yxIY0By1z0tnRm2YLnrMg6L9BhDtBcxx5qKlA X-Received: by 2002:a17:906:3197:b0:a59:c209:3e33 with SMTP id a640c23a62f3a-a5a2d53ad6cmr2097948566b.15.1716288588566; Tue, 21 May 2024 03:49:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716288588; cv=pass; d=google.com; s=arc-20160816; b=Qkrcasd7d6TN/vayH6FXtgOUVld1lAFLjCZM/yfGyXYUtmM3WDuZn8C1po0AsbzYEl d1RoHIjkKNLDjETPha7V6+NPy47xi1p45OOSwVGtsisyslgnSE62zNgb2pOpWznaQyXv 2Oox2GZJqQB6zKJgVANhZlvwSd9JbeeT2IdChvwpLGSiNXbTYB/BvjGAGUxIDHBYA3k6 AFI928X9XLF3BXa8FkRH5LecUZbeBRzwIHT9l5FLvrqs28PxYs6GQqLALfXWz1Ie1bjM sO8X+Pr4lyBvKGsvUkfbh7198RVr/N4AYxhb2YhbdS+I9h01koPeLO1jW5QITdAxIbWY NLBg== 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=VMm0Szo3M67FPgMbPJ/VNB4LM9j2AREwvgvxtP1+mmI=; fh=nmDo0evxWwTAvBlxNELHY/fYZbxD4K9pluaUjPt1MrQ=; b=pTuDBxmR83Db60Md8Wi3uJKOdD+NqRL1e345BJXj6Z6zXCouU1cXukzGeJbPMPc0MO kzCuRO6Fd6BOOWnC4dfMMMwqUWIiVW4RgSo5d2lodNcRTt4HwV61SpQHDnsPVfuLaFkP UH7p08ynUfpuQjz5hju8SqoICI9C6N1/3A399lOtUpDiJGcOrpGCdRCNLKiSJ2I1skSh iP6O/sDn7E25vHqAUg58U288/1yu9/KAQtlCvxZCGZutn5DHyrImWom58sxXVcu3YA7e GJuuuM2KGLNXrMrqqgjplPf9fRTQLuKpBOYlBHYHIrO2vs85WZrejx9hmn9f8E5clgCu LpTQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DiPhc3yH; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-184823-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-184823-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a5ceb4ccaebsi624965666b.831.2024.05.21.03.49.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 03:49:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-184823-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DiPhc3yH; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-184823-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-184823-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 4B73E1F22C93 for ; Tue, 21 May 2024 10:49:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8AB3C74E11; Tue, 21 May 2024 10:49:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DiPhc3yH" 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 976B9219EB; Tue, 21 May 2024 10:49:23 +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=1716288563; cv=none; b=BWwX6fUVKiAYeUJbr8337reONGx/K3Y3MpzM36tYtUARWL3dtBN7EP0ZIXr2Y/fvi9Wt9SoRuI2I/vsyoGU3m0jOv5FP/ibFHUJUjIc4i55alwYY6cqRepeezo1yTMp8GaoIWei3Ue+5Jy7an8UDatjSLEE2RkQKACkthN324Ts= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716288563; c=relaxed/simple; bh=TlvkotYY4vT5pChu211KhwIQkqphsg5aK16HXMTrdK8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SZg9Sc6rnUMOQxUy6SCR1OcZR1XoyJH7H2dS/aKKNUG/fqDZCZitsaRgxFzQ8TqCkCyU+tBL5RC3uvRS/ob7L40fykiLWc1bWUMkTXR5o2OelEf7HY2RrZMI0xVAZSk37rDyD0oGGkblTwXwbrb1ZkolS/+Bpq0lP6iTRceca/g= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DiPhc3yH; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 62DDDC32789; Tue, 21 May 2024 10:49:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1716288563; bh=TlvkotYY4vT5pChu211KhwIQkqphsg5aK16HXMTrdK8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DiPhc3yHftqJFKCo2U8HXhMyT6uDynpq/oRZGRNHajMLZyVztgn7UoUdROUWJzWqz +1bBicsDn7miwu735faA+S67Dh6le7sm+IVolJ7lmWP6SWB/qqmjuoDIoJgDXt/M6e Qd66LyykCbAXKSnChDSlp+ud2hdsps7zLLutYS5oOyOGyP7m7zMDNCEAaqr0fnMRua KItUMCWyg8lsFTRP6ez4bL4eI2YzZKocJ4+JER3q6BmDt+3xUl9ui79u+zpX2ccWBM 2wqYTwXey7sMbYfCROBq0iIlIZ/zOZl7BSz6RRucAAX0uEX5yQaZ6ZEb76z+HGKq3q QRt6ZVKQgQhxQ== 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 4/9] selftests/x86: Add return uprobe shadow stack test Date: Tue, 21 May 2024 12:48:20 +0200 Message-ID: <20240521104825.1060966-5-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 return uprobe test for shadow stack and making sure it's working properly. Borrowed some of the code from bpf selftests. Signed-off-by: Jiri Olsa --- .../testing/selftests/x86/test_shadow_stack.c | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) diff --git a/tools/testing/selftests/x86/test_shadow_stack.c b/tools/testing/selftests/x86/test_shadow_stack.c index 757e6527f67e..e3501b7e2ecc 100644 --- a/tools/testing/selftests/x86/test_shadow_stack.c +++ b/tools/testing/selftests/x86/test_shadow_stack.c @@ -34,6 +34,7 @@ #include #include #include +#include /* * Define the ABI defines if needed, so people can run the tests @@ -681,6 +682,144 @@ int test_32bit(void) return !segv_triggered; } +static int parse_uint_from_file(const char *file, const char *fmt) +{ + int err, ret; + FILE *f; + + f = fopen(file, "re"); + if (!f) { + err = -errno; + printf("failed to open '%s': %d\n", file, err); + return err; + } + err = fscanf(f, fmt, &ret); + if (err != 1) { + err = err == EOF ? -EIO : -errno; + printf("failed to parse '%s': %d\n", file, err); + fclose(f); + return err; + } + fclose(f); + return ret; +} + +static int determine_uprobe_perf_type(void) +{ + const char *file = "/sys/bus/event_source/devices/uprobe/type"; + + return parse_uint_from_file(file, "%d\n"); +} + +static int determine_uprobe_retprobe_bit(void) +{ + const char *file = "/sys/bus/event_source/devices/uprobe/format/retprobe"; + + return parse_uint_from_file(file, "config:%d\n"); +} + +static ssize_t get_uprobe_offset(const void *addr) +{ + size_t start, end, base; + char buf[256]; + bool found = false; + FILE *f; + + f = fopen("/proc/self/maps", "r"); + if (!f) + return -errno; + + while (fscanf(f, "%zx-%zx %s %zx %*[^\n]\n", &start, &end, buf, &base) == 4) { + if (buf[2] == 'x' && (uintptr_t)addr >= start && (uintptr_t)addr < end) { + found = true; + break; + } + } + + fclose(f); + + if (!found) + return -ESRCH; + + return (uintptr_t)addr - start + base; +} + +static __attribute__((noinline)) void uretprobe_trigger(void) +{ + asm volatile (""); +} + +/* + * This test setups return uprobe, which is sensitive to shadow stack + * (crashes without extra fix). After executing the uretprobe we fail + * the test if we receive SIGSEGV, no crash means we're good. + * + * Helper functions above borrowed from bpf selftests. + */ +static int test_uretprobe(void) +{ + const size_t attr_sz = sizeof(struct perf_event_attr); + const char *file = "/proc/self/exe"; + int bit, fd = 0, type, err = 1; + struct perf_event_attr attr; + struct sigaction sa = {}; + ssize_t offset; + + type = determine_uprobe_perf_type(); + if (type < 0) { + if (type == -ENOENT) + printf("[SKIP]\tUretprobe test, uprobes are not available\n"); + return 0; + } + + offset = get_uprobe_offset(uretprobe_trigger); + if (offset < 0) + return 1; + + bit = determine_uprobe_retprobe_bit(); + if (bit < 0) + return 1; + + sa.sa_sigaction = segv_gp_handler; + sa.sa_flags = SA_SIGINFO; + if (sigaction(SIGSEGV, &sa, NULL)) + return 1; + + /* Setup return uprobe through perf event interface. */ + memset(&attr, 0, attr_sz); + attr.size = attr_sz; + attr.type = type; + attr.config = 1 << bit; + attr.config1 = (__u64) (unsigned long) file; + attr.config2 = offset; + + fd = syscall(__NR_perf_event_open, &attr, 0 /* pid */, -1 /* cpu */, + -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); + if (fd < 0) + goto out; + + if (sigsetjmp(jmp_buffer, 1)) + goto out; + + ARCH_PRCTL(ARCH_SHSTK_ENABLE, ARCH_SHSTK_SHSTK); + + /* + * This either segfaults and goes through sigsetjmp above + * or succeeds and we're good. + */ + uretprobe_trigger(); + + printf("[OK]\tUretprobe test\n"); + err = 0; + +out: + ARCH_PRCTL(ARCH_SHSTK_DISABLE, ARCH_SHSTK_SHSTK); + signal(SIGSEGV, SIG_DFL); + if (fd) + close(fd); + return err; +} + void segv_handler_ptrace(int signum, siginfo_t *si, void *uc) { /* The SSP adjustment caused a segfault. */ @@ -867,6 +1006,12 @@ int main(int argc, char *argv[]) goto out; } + if (test_uretprobe()) { + ret = 1; + printf("[FAIL]\turetprobe test\n"); + goto out; + } + return ret; out: -- 2.45.0