xref: /openbsd-src/regress/usr.bin/lastcomm/syscallwx.c (revision c0a6b811701bdbfe01fb72ed4246ca86eedef8aa)
1 /*	$OpenBSD: syscallwx.c,v 1.2 2023/01/09 11:50:01 anton Exp $	*/
2 /*
3  * Copyright (c) 2018 Todd Mortimer <mortimer@openbsd.org>
4  * Copyright (c) 2019 Alexander Bluhm <bluhm@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/mman.h>
20 
21 #include <err.h>
22 #include <signal.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 extern void gadget_getpid(void);
27 static void handler(int);
28 
29 int
main(int argc,char * argv[])30 main(int argc, char *argv[])
31 {
32 	union {
33 		void *p;
34 		void (*gadget)(void);
35 	} addr;
36 	int psz = getpagesize();
37 
38 	if (signal(SIGSEGV, handler) == SIG_ERR)
39 		err(1, "signal");
40 
41 	addr.p = mmap(NULL, psz, PROT_READ | PROT_WRITE | PROT_EXEC,
42 	    MAP_PRIVATE | MAP_ANON, -1, 0);
43 	if (addr.p == NULL)
44 		err(1, "mmap");
45 	memcpy(addr.p, gadget_getpid, psz);
46 	addr.gadget();
47 	return 3;
48 }
49 
50 static void
handler(int signum)51 handler(int signum)
52 {
53 	_exit(0);
54 }
55