Skip to content

Commit 55dc57e

Browse files
xuyang0410metan-ucw
authored andcommitted
syscalls/ptrace02: Add another EPERM error test
Signed-off-by: Yang Xu <[email protected]> Reviewed-by: Cyril Hrubis <[email protected]>
1 parent a07a83b commit 55dc57e

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

runtest/syscalls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,7 @@ pselect03 pselect03
990990
pselect03_64 pselect03_64
991991

992992
ptrace01 ptrace01
993+
ptrace02 ptrace02
993994
ptrace03 ptrace03
994995
ptrace04 ptrace04
995996
ptrace05 ptrace05

testcases/kernel/syscalls/ptrace/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/ptrace01
2+
/ptrace02
23
/ptrace03
34
/ptrace04
45
/ptrace05
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
4+
* Author: Yang Xu <[email protected]
5+
*
6+
* ptrace() returns -1 and sets errno to EPERM if tracer doesn't have
7+
* CAP_SYS_PTRACE capability for the process. Such as nobody user.
8+
*/
9+
10+
#include <errno.h>
11+
#include <signal.h>
12+
#include <sys/wait.h>
13+
#include <pwd.h>
14+
#include <config.h>
15+
#include <stdlib.h>
16+
#include "ptrace.h"
17+
#include "tst_test.h"
18+
19+
uid_t uid;
20+
21+
static void verify_ptrace(void)
22+
{
23+
int child_pid[2];
24+
25+
child_pid[0] = SAFE_FORK();
26+
if (!child_pid[0])
27+
pause();
28+
29+
child_pid[1] = SAFE_FORK();
30+
if (!child_pid[1]) {
31+
SAFE_SETUID(uid);
32+
TEST(ptrace(PTRACE_ATTACH, child_pid[0], NULL, NULL));
33+
if (TST_RET == 0) {
34+
tst_res(TFAIL, "ptrace() succeeded unexpectedly");
35+
exit(0);
36+
}
37+
if (TST_ERR == EPERM)
38+
tst_res(TPASS | TTERRNO, "ptrace() failed as expected");
39+
else
40+
tst_res(TFAIL | TTERRNO, "ptrace() expected EPERM, but got");
41+
exit(0);
42+
}
43+
SAFE_WAITPID(child_pid[1], NULL, 0);
44+
SAFE_KILL(child_pid[0], SIGKILL);
45+
SAFE_WAITPID(child_pid[0], NULL, 0);
46+
}
47+
48+
static void setup(void)
49+
{
50+
struct passwd *pw;
51+
52+
pw = SAFE_GETPWNAM("nobody");
53+
uid = pw->pw_uid;
54+
}
55+
56+
static struct tst_test test = {
57+
.setup = setup,
58+
.test_all = verify_ptrace,
59+
.forks_child = 1,
60+
.needs_root = 1,
61+
};

0 commit comments

Comments
 (0)