syscalls/ptrace02: Add another EPERM error test
PATCH 2/2 syscalls/ptrace02: Add another EPERM error test
--- runtest/syscalls | 1 + testcases/kernel/syscalls/ptrace/.gitignore | 1 + testcases/kernel/syscalls/ptrace/ptrace02.c | 66 +++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 testcases/kernel/syscalls/ptrace/ptrace02.c
runtest/syscalls +1 -0
@@ -990,6 +990,7 @@ pselect03 pselect03 pselect03_64 pselect03_64 ptrace01 ptrace01 +ptrace02 ptrace02 ptrace03 ptrace03 ptrace04 ptrace04 ptrace05 ptrace05
testcases/kernel/syscalls/ptrace/.gitignore +1 -0
@@ -1,4 +1,5 @@ /ptrace01 +/ptrace02 /ptrace03 /ptrace04 /ptrace05
testcases/kernel/syscalls/ptrace/ptrace02.c new file +66 -0
66 lines not on this page · raw message
Signature · 1 line
2.23.0
PATCH 2/2 syscalls/ptrace02: Add another EPERM error test
Hi!
testcases/kernel/syscalls/ptrace/ptrace02.c line 25
Quoted 5 lines
testcases/kernel/syscalls/ptrace/ptrace02.c new file +5
-0+static void verify_ptrace (void ) +{ +int child_pid; + +tst_res (TINFO ,"Trace a process that don't have CAP_SYS_PTRACE capability(nobody user) for it" );
I wouldn't be printing this verbose info here, anyone who will have to debug the test failures will look into the source code and at the test description in the top level comment.
Quoted 12 lines
testcases/kernel/syscalls/ptrace/ptrace02.c new file +12
-0+ child_pid =SAFE_FORK (); +if (!child_pid) +pause (); + +if (!SAFE_FORK ()) { +SAFE_SETUID (uid); +TEST (ptrace (PTRACE_ATTACH , child_pid,NULL ,NULL )); +if (TST_RET ==0 ) { +tst_res (TFAIL ,"ptrace() succeeded unexpectedly" ); +TST_CHECKPOINT_WAKE (0 ); +exit (0 ); + }
testcases/kernel/syscalls/ptrace/ptrace02.c line 48
Quoted 10 lines
testcases/kernel/syscalls/ptrace/ptrace02.c new file +10
-0+if (TST_ERR ==EPERM ) +tst_res (TPASS |TTERRNO ,"ptrace() failed as expected" ); +else +tst_res (TFAIL |TTERRNO ,"ptrace() expected EPERM, but got" ); +TST_CHECKPOINT_WAKE (0 ); +exit (0 ); + } +TST_CHECKPOINT_WAIT (0 ); +SAFE_KILL (child_pid,SIGKILL ); +SAFE_WAITPID (child_pid,NULL ,0 );
We do not need the checkpoints here at all, we just need to waitpid for the second child before we kill the first one.
Quoted 26 lines
testcases/kernel/syscalls/ptrace/ptrace02.c new file +18
-0+tst_reap_children (); +} + +static void setup (void ) +{ +struct passwd *pw; + + pw =SAFE_GETPWNAM ("nobody" ); + uid = pw->pw_uid; +} + +static struct tst_test test = { + .setup = setup, + .test_all = verify_ptrace, + .forks_child =1 , + .needs_root =1 , + .needs_checkpoints =1 , +};Signature · 7 lines
2.23.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 2/2 syscalls/ptrace02: Add another EPERM error test
Hi Cyril
Quoted 10 lines
Hi!
testcases/kernel/syscalls/ptrace/ptrace02.c line 25
Quoted 5 lines
testcases/kernel/syscalls/ptrace/ptrace02.c new file +5
-0+static void verify_ptrace (void ) +{ +int child_pid; + +tst_res (TINFO ,"Trace a process that don't have CAP_SYS_PTRACE capability(nobody user) for it" );I wouldn't be printing this verbose info here, anyone who will have to debug the test failures will look into the source code and at the test description in the top level comment.
Will remove it.
Quoted 18 lines
Quoted 18 lines
testcases/kernel/syscalls/ptrace/ptrace02.c new file +17
-0+ child_pid =SAFE_FORK (); +if (!child_pid) +pause (); + +if (!SAFE_FORK ()) { +SAFE_SETUID (uid); +TEST (ptrace (PTRACE_ATTACH , child_pid,NULL ,NULL )); +if (TST_RET ==0 ) { +tst_res (TFAIL ,"ptrace() succeeded unexpectedly" ); +TST_CHECKPOINT_WAKE (0 ); +exit (0 ); + } +if (TST_ERR ==EPERM ) +tst_res (TPASS |TTERRNO ,"ptrace() failed as expected" ); +else +tst_res (TFAIL |TTERRNO ,"ptrace() expected EPERM, but got" ); +TST_CHECKPOINT_WAKE (0 );
Quoted 8 lines
testcases/kernel/syscalls/ptrace/ptrace02.c line 48
Quoted 5 lines
testcases/kernel/syscalls/ptrace/ptrace02.c new file +5
-0+exit (0 ); + } +TST_CHECKPOINT_WAIT (0 ); +SAFE_KILL (child_pid,SIGKILL ); +SAFE_WAITPID (child_pid,NULL ,0 );We do not need the checkpoints here at all, we just need to waitpid for the second child before we kill the first one.
Yes, Will fix it in v2.
Quoted 28 lines
Quoted 27 lines
testcases/kernel/syscalls/ptrace/ptrace02.c new file +18
-1+tst_reap_children (); +} + +static void setup (void ) +{ +struct passwd *pw; + + pw =SAFE_GETPWNAM ("nobody" ); + uid = pw->pw_uid; +} + +static struct tst_test test = { + .setup = setup, + .test_all = verify_ptrace, + .forks_child =1 , + .needs_root =1 , + .needs_checkpoints =1 , +}; --2.23.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 1/2 syscalls/ptrace02: Add another EPERM error test
In reply to Cyril Hrubis--- runtest/syscalls | 1 + testcases/kernel/syscalls/ptrace/.gitignore | 1 + testcases/kernel/syscalls/ptrace/ptrace02.c | 61 +++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 testcases/kernel/syscalls/ptrace/ptrace02.c
runtest/syscalls +1 -0
@@ -990,6 +990,7 @@ pselect03 pselect03 pselect03_64 pselect03_64 ptrace01 ptrace01 +ptrace02 ptrace02 ptrace03 ptrace03 ptrace04 ptrace04 ptrace05 ptrace05
testcases/kernel/syscalls/ptrace/.gitignore +1 -0
@@ -1,4 +1,5 @@ /ptrace01 +/ptrace02 /ptrace03 /ptrace04 /ptrace05
testcases/kernel/syscalls/ptrace/ptrace02.c new file +61 -0
61 lines not on this page · raw message
Signature · 1 line
2.23.0
PATCH v2 2/2 syscalls/ptrace11: Add test for tracing init process
--- runtest/syscalls | 1 + testcases/kernel/syscalls/ptrace/.gitignore | 1 + testcases/kernel/syscalls/ptrace/ptrace11.c | 39 +++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 testcases/kernel/syscalls/ptrace/ptrace11.c
runtest/syscalls +1 -0
@@ -1000,6 +1000,7 @@ ptrace07 ptrace07 ptrace08 ptrace08 ptrace09 ptrace09 ptrace10 ptrace10 +ptrace11 ptrace11 pwrite01 pwrite01 pwrite02 pwrite02
testcases/kernel/syscalls/ptrace/.gitignore +1 -0
@@ -7,3 +7,4 @@ /ptrace08 /ptrace09 /ptrace10 +/ptrace11
testcases/kernel/syscalls/ptrace/ptrace11.c new file +39 -0
@@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2020 FUJITSU LIMITED. All rights reserved. +* Author: Yang Xu <[email protected] +* +* This case just check whether we can trace init(1) process and +* doesn't trigger error. +*/ + +#include <errno.h> +#include <signal.h> +#include <sys/wait.h> +#include <pwd.h> +#include <config.h> +#include <stdlib.h> +#include "ptrace.h" +#include "tst_test.h" + +static void verify_ptrace (void ) +{ +TEST (ptrace (PTRACE_ATTACH ,1 ,NULL ,NULL )); +if (TST_RET ==0 ) +tst_res (TPASS ,"ptrace() traces init process succeeded" ); +else +tst_res (TFAIL |TTERRNO , +"ptrace() returns %ld, failed unexpectedly" ,TST_RET ); + +/* +* Running attach/detach more times will trigger a ESRCH error because +* ptrace_check_attach function in kernel will report it if its process +* stats is not __TASK_TRACED. +*/ +TST_RETRY_FUNC (ptrace (PTRACE_DETACH ,1 ,NULL ,NULL ),TST_RETVAL_EQ0 ); +} + +static struct tst_test test = { + .test_all = verify_ptrace, + .needs_root =1 , +};
Signature · 1 line
2.23.0
PATCH v2 2/2 syscalls/ptrace11: Add test for tracing init process
Hi!
testcases/kernel/syscalls/ptrace/ptrace11.c line 8
Quoted 8 lines
testcases/kernel/syscalls/ptrace/ptrace11.c new file +8
-0+// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2020 FUJITSU LIMITED. All rights reserved. +* Author: Yang Xu <[email protected] +* +* This case just check whether we can trace init(1) process and +* doesn't trigger error. +*/
Why is init(1) special here? Is this a regression test?
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH v2 2/2 syscalls/ptrace11: Add test for tracing init process
Hi!
Quoted 10 lines
testcases/kernel/syscalls/ptrace/ptrace11.c line 8
Quoted 8 lines
testcases/kernel/syscalls/ptrace/ptrace11.c new file +8
-0+// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2020 FUJITSU LIMITED. All rights reserved. +* Author: Yang Xu <[email protected] +* +* This case just check whether we can trace init(1) process and +* doesn't trigger error. +*/ Why is init(1) special here? Is this a regression test?
Looking into the manual page this wasn't supported until 2.6.26. I guess that we should mention that here in the test description.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH v2 2/2 syscalls/ptrace11: Add test for tracing init process
Hi Cyril
Quoted 12 lines
Hi!
Quoted 10 lines
testcases/kernel/syscalls/ptrace/ptrace11.c line 8
Quoted 8 lines
testcases/kernel/syscalls/ptrace/ptrace11.c new file +8
-0+// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2020 FUJITSU LIMITED. All rights reserved. +* Author: Yang Xu<[email protected] +* +* This case just check whether we can trace init(1) process and +* doesn't trigger error. +*/ Why is init(1) special here? Is this a regression test?
Quoted 2 lines
Looking into the manual page this wasn't supported until 2.6.26. I guess that we should mention that here in the test description.
Yes, it should be added into the test description. Sorry for not explaining why testing init process. Will add it in v2.
Quoted 1 line
PATCH v3 1/1 syscalls/ptrace11: Add test for tracing init process
In reply to Cyril HrubisBefore kernel 2.6.26, ptrace can't trace init process and will get EPERM error. Now, it should succeed when we have enough privileges.
--- runtest/syscalls | 1 + testcases/kernel/syscalls/ptrace/.gitignore | 1 + testcases/kernel/syscalls/ptrace/ptrace11.c | 40 +++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 testcases/kernel/syscalls/ptrace/ptrace11.c
runtest/syscalls +1 -0
@@ -1000,6 +1000,7 @@ ptrace07 ptrace07 ptrace08 ptrace08 ptrace09 ptrace09 ptrace10 ptrace10 +ptrace11 ptrace11 pwrite01 pwrite01 pwrite02 pwrite02
testcases/kernel/syscalls/ptrace/.gitignore +1 -0
@@ -7,3 +7,4 @@ /ptrace08 /ptrace09 /ptrace10 +/ptrace11
testcases/kernel/syscalls/ptrace/ptrace11.c new file +40 -0
@@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2020 FUJITSU LIMITED. All rights reserved. +* Author: Yang Xu <[email protected]> +* +* Before kernel 2.6.26, we can't trace init(1) process and ptrace() will +* get EPERM error. This case just check whether we can trace init(1) +* process and doesn't trigger error. +*/ + +#include <errno.h> +#include <signal.h> +#include <sys/wait.h> +#include <pwd.h> +#include <config.h> +#include <stdlib.h> +#include "ptrace.h" +#include "tst_test.h" + +static void verify_ptrace (void ) +{ +TEST (ptrace (PTRACE_ATTACH ,1 ,NULL ,NULL )); +if (TST_RET ==0 ) +tst_res (TPASS ,"ptrace() traces init process successfully" ); +else +tst_res (TFAIL |TTERRNO , +"ptrace() returns %ld, failed unexpectedly" ,TST_RET ); + +/* +* Running attach/detach more times will trigger a ESRCH error because +* ptrace_check_attach function in kernel will report it if its process +* stats is not __TASK_TRACED. +*/ +TST_RETRY_FUNC (ptrace (PTRACE_DETACH ,1 ,NULL ,NULL ),TST_RETVAL_EQ0 ); +} + +static struct tst_test test = { + .test_all = verify_ptrace, + .needs_root =1 , +};
Signature · 1 line
2.23.0
PATCH v3 1/1 syscalls/ptrace11: Add test for tracing init process
Hi!
testcases/kernel/syscalls/ptrace/ptrace11.c line 34
Quoted 6 lines
testcases/kernel/syscalls/ptrace/ptrace11.c new file +6
-0+/* +* Running attach/detach more times will trigger a ESRCH error because +* ptrace_check_attach function in kernel will report it if its process +* stats is not __TASK_TRACED. +*/ +TST_RETRY_FUNC (ptrace (PTRACE_DETACH ,1 ,NULL ,NULL ),TST_RETVAL_EQ0 );
Why do we have to retry the detach here?
Other than that the rest looks fine now.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH v3 1/1 syscalls/ptrace11: Add test for tracing init process
Hi Cyril
Quoted 9 lines
Hi!
testcases/kernel/syscalls/ptrace/ptrace11.c line 34
Quoted 6 lines
testcases/kernel/syscalls/ptrace/ptrace11.c new file +6
-0+/* +* Running attach/detach more times will trigger a ESRCH error because +* ptrace_check_attach function in kernel will report it if its process +* stats is not __TASK_TRACED. +*/ +TST_RETRY_FUNC (ptrace (PTRACE_DETACH ,1 ,NULL ,NULL ),TST_RETVAL_EQ0 );Why do we have to retry the detach here?
I add a retry here because running attach/detach serval times may make init process isn't traced status . Even we have do attach action, detach will get ESRCH error .
In kernel/ptrace.c code, it has the following calltrace SYSCALL_DEFINE4(ptrace...
=>ptrace_check_attach //if we don't use PTRACE_ATTACH/PTRACE_SEIZE
=>ptrace_freeze_traced
/* Ensure that nothing can wake it up, even SIGKILL */ static bool ptrace_freeze_traced(struct task_struct *task)
{
bool ret = false;
/* Lockless, nobody but us can set this flag */
if (task->jobctl & JOBCTL_LISTENING)
return ret;
spin_lock_irq(&task->sighand->siglock);
if (task_is_traced(task) && !__fatal_signal_pending(task)) {
task->state = __TASK_TRACED;
ret = true;
}
spin_unlock_irq(&task->sighand->siglock);
return ret;
}ptrace_freeze_traced() may returns false when we run attach/detach serval times, so ptrace_check_attach returns ESRCH error .
To be honset, I don't figure out why task->stat is not task_traced status after attaching. I am looking into this.
Best Regards
Yang Xu
Quoted 3 lines
Other than that the rest looks fine now.
PATCH v3 1/1 syscalls/ptrace11: Add test for tracing init process
Hi!
Quoted 9 lines
Quoted 8 lines
testcases/kernel/syscalls/ptrace/ptrace11.c line 34
Quoted 6 lines
testcases/kernel/syscalls/ptrace/ptrace11.c new file +6
-0+/* +* Running attach/detach more times will trigger a ESRCH error because +* ptrace_check_attach function in kernel will report it if its process +* stats is not __TASK_TRACED. +*/ +TST_RETRY_FUNC (ptrace (PTRACE_DETACH ,1 ,NULL ,NULL ),TST_RETVAL_EQ0 );Why do we have to retry the detach here?
Quoted 3 lines
I add a retry here because running attach/detach serval times may make init process isn't traced status . Even we have do attach action, detach will get ESRCH error .
Looking at the manual page it says:
PTRACE_ATTACH
...
The tracee is sent a SIGSTOP, but will not necessarily have stopped by the completion of this call; use waitpid(2) to wait for the tracee to stop.
...
So my guess is that if we call PTRACE_ATTACH followed by the PTRACE_DETACH we may end up in a state where the SIGSTOP for the traced process haven't arrived yet and in this case we should get ESCRCH.
So the correct solution is waitpid() for the traced process before we detach it.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH v3 1/1 syscalls/ptrace11: Add test for tracing init process
Hi Cyril
Quoted 14 lines
Hi!
Quoted 9 lines
Quoted 8 lines
testcases/kernel/syscalls/ptrace/ptrace11.c line 34
Quoted 6 lines
testcases/kernel/syscalls/ptrace/ptrace11.c new file +6
-0+/* +* Running attach/detach more times will trigger a ESRCH error because +* ptrace_check_attach function in kernel will report it if its process +* stats is not __TASK_TRACED. +*/ +TST_RETRY_FUNC (ptrace (PTRACE_DETACH ,1 ,NULL ,NULL ),TST_RETVAL_EQ0 );Why do we have to retry the detach here?
Quoted 3 lines
I add a retry here because running attach/detach serval times may make init process isn't traced status . Even we have do attach action, detach will get ESRCH error .
Quoted 8 lines
Looking at the manual page it says:
PTRACE_ATTACH...
The tracee is sent a SIGSTOP, but will not necessarily have stopped by the completion of this call; use waitpid(2) to wait for the tracee to stop.
Oh, I ignored it. Sorry.
Quoted 8 lines
...
So my guess is that if we call
PTRACE_ATTACHfollowed by thePTRACE_DETACHwe may end up in a state where the SIGSTOP for the traced process haven't arrived yet and in this case we should get ESCRCH.So the correct solution is
waitpid()for the traced process before we detach it.
Yes, using waitpid() is ok. I Will send a v4 soon.
Quoted 1 line
PATCH v4 1/1 syscalls/ptrace11: Add test for tracing init process
In reply to Cyril HrubisBefore kernel 2.6.26, ptrace can't trace init process and will get EPERM error. Now, it should succeed when we have enough privileges.
--- runtest/syscalls | 1 + testcases/kernel/syscalls/ptrace/.gitignore | 1 + testcases/kernel/syscalls/ptrace/ptrace11.c | 46 +++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 testcases/kernel/syscalls/ptrace/ptrace11.c
runtest/syscalls +1 -0
@@ -1000,6 +1000,7 @@ ptrace07 ptrace07 ptrace08 ptrace08 ptrace09 ptrace09 ptrace10 ptrace10 +ptrace11 ptrace11 pwrite01 pwrite01 pwrite02 pwrite02
testcases/kernel/syscalls/ptrace/.gitignore +1 -0
@@ -7,3 +7,4 @@ /ptrace08 /ptrace09 /ptrace10 +/ptrace11
testcases/kernel/syscalls/ptrace/ptrace11.c new file +46 -0
46 lines not on this page · raw message
Signature · 1 line
2.23.0
PATCH v4 1/1 syscalls/ptrace11: Add test for tracing init process
Hi!
testcases/kernel/syscalls/ptrace/ptrace11.c line 9
Quoted 9 lines
testcases/kernel/syscalls/ptrace/ptrace11.c new file +9
-0+// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2020 FUJITSU LIMITED. All rights reserved. +* Author: Yang Xu <[email protected]> +* +* Before kernel 2.6.26, we can't trace init(1) process and ptrace() will +* get EPERM error. This case just check whether we can trace init(1) +* process and doesn't trigger error. +*/
I've reformatted this comment so that it's picked up by documentation parser.
Quoted 17 lines
testcases/kernel/syscalls/ptrace/ptrace11.c new file +17
-0+#include <errno.h> +#include <signal.h> +#include <sys/wait.h> +#include <pwd.h> +#include <config.h> +#include <stdlib.h> +#include "ptrace.h" +#include "tst_test.h" + +static void verify_ptrace (void ) +{ +TEST (ptrace (PTRACE_ATTACH ,1 ,NULL ,NULL )); +if (TST_RET ==0 ) +tst_res (TPASS ,"ptrace() traces init process successfully" ); +else +tst_res (TFAIL |TTERRNO , +"ptrace() returns %ld, failed unexpectedly" ,TST_RET );
testcases/kernel/syscalls/ptrace/ptrace11.c line 37
Quoted 10 lines
testcases/kernel/syscalls/ptrace/ptrace11.c new file +10
-0+ +/* +* As ptrace(2) man-page said, when using PTRACE_ATTACH option, the +* tracee is sent a SIGSTOP, but will not necessarily have stopped +* by the completion of this call. Use waitpid(2) to wait for the +* tracee into stop. Otherwise it may get ESRCH error. +* As waitpid(2) man-pages said, status for traced children which have +* stopped is provided even if WUNTRACED option is not specified. +* So using 0 option is enough. +*/
Simplified this comment.
And pushed, thanks.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH v2 1/2 syscalls/ptrace02: Add another EPERM error test
In reply to Yang XuHi!
Pushed, thanks.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH v2 1/2 syscalls/ptrace02: Add another EPERM error test
Hi!
Quoted 1 line
Pushed, thanks.
I've pushed a follow up patch that adds an explicit test for return value.
Please be strict in checking the return values in your testcases from now on.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH v2 1/2 syscalls/ptrace02: Add another EPERM error test
Hi Cyril
Quoted 7 lines
Hi!
Quoted 1 line
Pushed, thanks.
I've pushed a follow up patch that adds an explicit test for return value.
Please be strict in checking the return values in your testcases from now on.
Ok. I Will notice this.
Best Regards
Yang Xu