]> granicus.if.org Git - strace/blob - tests/prctl-dumpable.c
strace: terminate itself if interrupted by a signal
[strace] / tests / prctl-dumpable.c
1 /*
2  * Check decoding of prctl PR_GET_DUMPABLE/PR_SET_DUMPABLE operations.
3  *
4  * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
5  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
6  * Copyright (c) 2016-2018 The strace developers.
7  * All rights reserved.
8  *
9  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
11
12 #include "tests.h"
13 #include <asm/unistd.h>
14 #include <linux/prctl.h>
15
16 #if defined __NR_prctl && defined PR_GET_DUMPABLE && defined PR_SET_DUMPABLE \
17  && !defined __ia64__
18
19 # include <stdio.h>
20 # include <unistd.h>
21
22 static const char *errstr;
23
24 static long
25 prctl(kernel_ulong_t arg1, kernel_ulong_t arg2)
26 {
27         static const kernel_ulong_t bogus_arg =
28                 (kernel_ulong_t) 0xdeadbeefbadc0dedULL;
29         long rc = syscall(__NR_prctl, arg1, arg2, bogus_arg);
30         errstr = sprintrc(rc);
31         return rc;
32 }
33
34 int
35 main(void)
36 {
37         static const kernel_ulong_t bogus_dumpable1 =
38                 (kernel_ulong_t) 0xdeadc0de00000001ULL;
39         static const kernel_ulong_t bogus_dumpable2 =
40                 (kernel_ulong_t) 0xdeadc0defacebeefULL;
41
42         static const char * const args[] = {
43                 "SUID_DUMP_DISABLE",
44                 "SUID_DUMP_USER",
45                 "SUID_DUMP_ROOT",
46         };
47
48         unsigned int i;
49
50         prctl(PR_SET_DUMPABLE, 3);
51         printf("prctl(PR_SET_DUMPABLE, 0x3 /* SUID_DUMP_??? */) = %s\n",
52                errstr);
53
54         prctl(PR_SET_DUMPABLE, bogus_dumpable1);
55         if (bogus_dumpable1 == 1) {
56                 printf("prctl(PR_SET_DUMPABLE, SUID_DUMP_USER) = %s\n", errstr);
57         } else {
58                 printf("prctl(PR_SET_DUMPABLE, %#llx /* SUID_DUMP_??? */)"
59                        " = %s\n",
60                        (unsigned long long) bogus_dumpable1, errstr);
61         }
62
63         prctl(PR_SET_DUMPABLE, bogus_dumpable2);
64         printf("prctl(PR_SET_DUMPABLE, %#llx /* SUID_DUMP_??? */) = %s\n",
65                (unsigned long long) bogus_dumpable2, errstr);
66
67         for (i = 0; i < ARRAY_SIZE(args); ++i) {
68                 prctl(PR_SET_DUMPABLE, i);
69                 printf("prctl(PR_SET_DUMPABLE, %s) = %s\n", args[i], errstr);
70
71                 long rc = prctl(PR_GET_DUMPABLE, bogus_dumpable2);
72                 if (rc >= 0 && rc < (long) ARRAY_SIZE(args)) {
73                         printf("prctl(PR_GET_DUMPABLE) = %s (%s)\n",
74                                errstr, args[rc]);
75                 } else {
76                         printf("prctl(PR_GET_DUMPABLE) = %s\n", errstr);
77                 }
78         }
79
80         puts("+++ exited with 0 +++");
81         return 0;
82 }
83
84 #else
85
86 SKIP_MAIN_UNDEFINED("__NR_prctl && PR_GET_DUMPABLE && PR_SET_DUMPABLE"
87                     " && !__ia64__")
88
89 #endif