File: prctl-pac-enabled-keys.c

package info (click to toggle)
strace 6.13%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 71,488 kB
  • sloc: ansic: 176,497; sh: 9,675; makefile: 4,133; cpp: 885; awk: 353; perl: 267; exp: 62; sed: 9
file content (128 lines) | stat: -rw-r--r-- 2,799 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Check decoding of prctl PR_PAC_RESET_KEYS operation.
 *
 * Copyright (c) 2021 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"
#include "scno.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/prctl.h>

#include "xlat.h"
#include "xlat/pr_pac_enabled_keys.h"

#ifdef INJECT_RETVAL
# define INJ_STR " (INJECTED)"
#else
# define INJ_STR ""
#endif

#define PR_PAC_ENABLE_FLAGS_MASK \
	(PR_PAC_APIAKEY|PR_PAC_APIBKEY|PR_PAC_APDAKEY|PR_PAC_APDBKEY)

int
main(int argc, char *argv[])
{
	prctl_marker();

#ifdef INJECT_RETVAL
	unsigned long num_skip;
	long inject_retval;
	bool locked = false;

	if (argc < 3)
		error_msg_and_fail("Usage: %s NUM_SKIP INJECT_RETVAL", argv[0]);

	num_skip = strtoul(argv[1], NULL, 0);
	inject_retval = strtol(argv[2], NULL, 0);

	for (size_t i = 0; i < num_skip; i++) {
		if (prctl_marker() != inject_retval)
			continue;

		locked = true;
		break;
	}

	if (!locked)
		error_msg_and_fail("Have not locked on prctl(-1, -2, -3, -4"
				   ", -5) returning %ld", inject_retval);
#endif /* INJECT_RETVAL */

	static const struct {
		kernel_ulong_t val;
		const char *str;
	} args[] = {
		{ ARG_STR(0) },
		{ ARG_STR(PR_PAC_APIAKEY) },
		{ ARG_STR(PR_PAC_APIBKEY|PR_PAC_APDAKEY|PR_PAC_APDBKEY|0x10) },
		{ 0x7ffffff0, NVERB("0x7ffffff0 /* ") "PR_PAC_???" NVERB(" */") },
	};
	long rc;
	const char *errstr;
	size_t i = 0;

	for (i = 0; i < ARRAY_SIZE(args); i++) {
		for (size_t j = 0; j < ARRAY_SIZE(args); j++) {
			rc = syscall(__NR_prctl, PR_PAC_SET_ENABLED_KEYS,
				     args[i].val, args[j].val, 0, 1);
			errstr = sprintrc(rc);
			printf("prctl("
			       XLAT_KNOWN(0x3c, "PR_PAC_SET_ENABLED_KEYS"));
			if (args[i].val) {
				printf(", " XLAT_FMT_LL,
				       XLAT_SEL((unsigned long long) args[i].val,
						args[i].str));
			} else {
				printf(", 0");
			}
			if (args[j].val) {
				printf(", " XLAT_FMT_LL,
				       XLAT_SEL((unsigned long long) args[j].val,
						args[j].str));
			} else {
				printf(", 0");
			}
			printf(", 0, 0x1) = %s" INJ_STR "\n", errstr);
		}
	}

	rc = syscall(__NR_prctl, PR_PAC_GET_ENABLED_KEYS, 0, 0, 0, 0);
	errstr = sprintrc(rc);
	printf("prctl(" XLAT_KNOWN(0x3d, "PR_PAC_GET_ENABLED_KEYS")
	       ", 0, 0, 0, 0) = ");

	if (rc > 0) {
		printf("%#lx", rc);
#if !XLAT_RAW
		for (i = 0; i < ARRAY_SIZE(args); i++) {
			if (!(rc & PR_PAC_ENABLE_FLAGS_MASK))
				break;

			if (args[i].val == (unsigned long) rc) {
				printf(" (%s)", args[i].str);
				break;
			}
		}

		if (i == ARRAY_SIZE(args)) {
			printf(" (");
			printflags(pr_pac_enabled_keys, rc, "PR_PAC_???");
			printf(")");
		}
#endif /* !XLAT_RAW */
		puts(INJ_STR);
	} else {
		printf("%s" INJ_STR "\n", errstr);
	}


	puts("+++ exited with 0 +++");
	return 0;
}