aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tools/testing/selftests/ptrace/get_set_sud.c
blob: 5297b10d25c3678defa73a92197a9174b89bccee (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include "../kselftest_harness.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/syscall.h>
#include <sys/prctl.h>

#include "linux/ptrace.h"

static int sys_ptrace(int request, pid_t pid, void *addr, void *data)
{
	return syscall(SYS_ptrace, request, pid, addr, data);
}

TEST(get_set_sud)
{
	struct ptrace_sud_config config;
	pid_t child;
	int ret = 0;
	int status;

	child = fork();
	ASSERT_GE(child, 0);
	if (child == 0) {
		ASSERT_EQ(0, sys_ptrace(PTRACE_TRACEME, 0, 0, 0)) {
			TH_LOG("PTRACE_TRACEME: %m");
		}
		kill(getpid(), SIGSTOP);
		_exit(1);
	}

	waitpid(child, &status, 0);

	memset(&config, 0xff, sizeof(config));
	config.mode = PR_SYS_DISPATCH_ON;

	ret = sys_ptrace(PTRACE_GET_SYSCALL_USER_DISPATCH_CONFIG, child,
			 (void *)sizeof(config), &config);

	ASSERT_EQ(ret, 0);
	ASSERT_EQ(config.mode, PR_SYS_DISPATCH_OFF);
	ASSERT_EQ(config.selector, 0);
	ASSERT_EQ(config.offset, 0);
	ASSERT_EQ(config.len, 0);

	config.mode = PR_SYS_DISPATCH_ON;
	config.selector = 0;
	config.offset = 0x400000;
	config.len = 0x1000;

	ret = sys_ptrace(PTRACE_SET_SYSCALL_USER_DISPATCH_CONFIG, child,
			 (void *)sizeof(config), &config);

	ASSERT_EQ(ret, 0);

	memset(&config, 1, sizeof(config));
	ret = sys_ptrace(PTRACE_GET_SYSCALL_USER_DISPATCH_CONFIG, child,
			 (void *)sizeof(config), &config);

	ASSERT_EQ(ret, 0);
	ASSERT_EQ(config.mode, PR_SYS_DISPATCH_ON);
	ASSERT_EQ(config.selector, 0);
	ASSERT_EQ(config.offset, 0x400000);
	ASSERT_EQ(config.len, 0x1000);

	kill(child, SIGKILL);
}

TEST_HARNESS_MAIN