1/*	$OpenBSD: t_sigaltstack.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $	*/
2/* $NetBSD: t_sigaltstack.c,v 1.2 2020/05/01 21:35:30 christos Exp $ */
3
4/*-
5 * Copyright (c) 2020 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29#include "macros.h"
30
31#include <signal.h>
32#include <stdbool.h>
33
34#include "atf-c.h"
35
36#include "h_macros.h"
37
38static stack_t sigstk;
39static bool handler_called;
40static bool handler_use_altstack;
41
42static void
43handler(int signo __unused)
44{
45	char sp[128];
46
47	handler_called = true;
48
49	/* checking if the stack pointer is within the range of altstack */
50	if ((char *)sigstk.ss_sp <= sp &&
51	    ((char *)sigstk.ss_sp + sigstk.ss_size) > sp)
52		handler_use_altstack = true;
53	else
54		handler_use_altstack = false;
55}
56
57ATF_TC(sigaltstack_onstack);
58ATF_TC_HEAD(sigaltstack_onstack, tc)
59{
60	atf_tc_set_md_var(tc, "descr",
61	    "Checks for using signal stack with SA_ONSTACK");
62}
63
64ATF_TC_BODY(sigaltstack_onstack, tc)
65{
66	struct sigaction sa;
67	int i;
68
69	/* set a signal handler use alternative stack */
70	memset(&sigstk, 0, sizeof(sigstk));
71	sigstk.ss_sp = malloc(SIGSTKSZ);
72	ATF_REQUIRE(sigstk.ss_sp != NULL);
73	sigstk.ss_size = SIGSTKSZ;
74	sigstk.ss_flags = 0;
75	ATF_REQUIRE(sigaltstack(&sigstk, 0) == 0);
76
77	sigemptyset(&sa.sa_mask);
78	sa.sa_handler = handler;
79	sa.sa_flags = SA_ONSTACK;
80	sigaction(SIGUSR1, &sa, NULL);
81
82	/* test several times */
83	for (i = 1; i <= 5; i++) {
84		handler_called = false;
85		kill(getpid(), SIGUSR1);
86
87		if (!handler_called)
88			atf_tc_fail("signal handler wasn't called (count=%d)", i);
89		if (!handler_use_altstack)
90			atf_tc_fail("alternative stack wasn't used (count=%d)", i);
91	}
92}
93
94ATF_TP_ADD_TCS(tp)
95{
96	ATF_TP_ADD_TC(tp, sigaltstack_onstack);
97
98	return atf_no_error();
99}
100