Skip to content

Commit 9c460b4

Browse files
xuyang0410metan-ucw
authored andcommitted
syscalls/ptrace11: Add test for tracing init process
Before kernel 2.6.26, ptrace can't trace init process and will get EPERM error. Now, it should succeed when we have enough privileges. Signed-off-by: Yang Xu <[email protected]>
1 parent 0253495 commit 9c460b4

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

runtest/syscalls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,7 @@ ptrace07 ptrace07
10001000
ptrace08 ptrace08
10011001
ptrace09 ptrace09
10021002
ptrace10 ptrace10
1003+
ptrace11 ptrace11
10031004

10041005
pwrite01 pwrite01
10051006
pwrite02 pwrite02

testcases/kernel/syscalls/ptrace/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
/ptrace08
88
/ptrace09
99
/ptrace10
10+
/ptrace11
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
* Before kernel 2.6.26, we can't trace init(1) process and ptrace() will
7+
* get EPERM error. This case just check whether we can trace init(1)
8+
* process and doesn't trigger error.
9+
*/
10+
11+
#include <errno.h>
12+
#include <signal.h>
13+
#include <sys/wait.h>
14+
#include <pwd.h>
15+
#include <config.h>
16+
#include <stdlib.h>
17+
#include "ptrace.h"
18+
#include "tst_test.h"
19+
20+
static void verify_ptrace(void)
21+
{
22+
TEST(ptrace(PTRACE_ATTACH, 1, NULL, NULL));
23+
if (TST_RET == 0)
24+
tst_res(TPASS, "ptrace() traces init process successfully");
25+
else
26+
tst_res(TFAIL | TTERRNO,
27+
"ptrace() returns %ld, failed unexpectedly", TST_RET);
28+
29+
/*
30+
* Running attach/detach more times will trigger a ESRCH error because
31+
* ptrace_check_attach function in kernel will report it if its process
32+
* stats is not __TASK_TRACED.
33+
*/
34+
TST_RETRY_FUNC(ptrace(PTRACE_DETACH, 1, NULL, NULL), TST_RETVAL_EQ0);
35+
}
36+
37+
static struct tst_test test = {
38+
.test_all = verify_ptrace,
39+
.needs_root = 1,
40+
};

0 commit comments

Comments
 (0)