Skip to content

Commit 86b6c1f

Browse files
Denys Vlasenkotorvalds
authored andcommitted
ptrace: simplify PTRACE_foo constants and PTRACE_SETOPTIONS code
Exchange PT_TRACESYSGOOD and PT_PTRACE_CAP bit positions, which makes PT_option bits contiguous and therefore makes code in ptrace_setoptions() much simpler. Every PTRACE_O_TRACEevent is defined to (1 << PTRACE_EVENT_event) instead of using explicit numeric constants, to ensure we don't mess up relationship between bit positions and event ids. PT_EVENT_FLAG_SHIFT was not particularly useful, PT_OPT_FLAG_SHIFT with value of PT_EVENT_FLAG_SHIFT-1 is easier to use. PT_TRACE_MASK constant is nuked, the only its use is replaced by (PTRACE_O_MASK << PT_OPT_FLAG_SHIFT). Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com> Acked-by: Tejun Heo <tj@kernel.org> Reviewed-by: Oleg Nesterov <oleg@redhat.com> Cc: Pedro Alves <palves@redhat.com> Cc: Jan Kratochvil <jan.kratochvil@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 8c5cf9e commit 86b6c1f

File tree

2 files changed

+23
-41
lines changed

2 files changed

+23
-41
lines changed

include/linux/ptrace.h

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,6 @@
5454
/* flags in @data for PTRACE_SEIZE */
5555
#define PTRACE_SEIZE_DEVEL 0x80000000 /* temp flag for development */
5656

57-
/* options set using PTRACE_SETOPTIONS */
58-
#define PTRACE_O_TRACESYSGOOD 0x00000001
59-
#define PTRACE_O_TRACEFORK 0x00000002
60-
#define PTRACE_O_TRACEVFORK 0x00000004
61-
#define PTRACE_O_TRACECLONE 0x00000008
62-
#define PTRACE_O_TRACEEXEC 0x00000010
63-
#define PTRACE_O_TRACEVFORKDONE 0x00000020
64-
#define PTRACE_O_TRACEEXIT 0x00000040
65-
66-
#define PTRACE_O_MASK 0x0000007f
67-
6857
/* Wait extended result codes for the above trace options. */
6958
#define PTRACE_EVENT_FORK 1
7059
#define PTRACE_EVENT_VFORK 2
@@ -74,6 +63,17 @@
7463
#define PTRACE_EVENT_EXIT 6
7564
#define PTRACE_EVENT_STOP 7
7665

66+
/* options set using PTRACE_SETOPTIONS */
67+
#define PTRACE_O_TRACESYSGOOD 1
68+
#define PTRACE_O_TRACEFORK (1 << PTRACE_EVENT_FORK)
69+
#define PTRACE_O_TRACEVFORK (1 << PTRACE_EVENT_VFORK)
70+
#define PTRACE_O_TRACECLONE (1 << PTRACE_EVENT_CLONE)
71+
#define PTRACE_O_TRACEEXEC (1 << PTRACE_EVENT_EXEC)
72+
#define PTRACE_O_TRACEVFORKDONE (1 << PTRACE_EVENT_VFORK_DONE)
73+
#define PTRACE_O_TRACEEXIT (1 << PTRACE_EVENT_EXIT)
74+
75+
#define PTRACE_O_MASK 0x0000007f
76+
7777
#include <asm/ptrace.h>
7878

7979
#ifdef __KERNEL__
@@ -88,22 +88,19 @@
8888
#define PT_SEIZED 0x00010000 /* SEIZE used, enable new behavior */
8989
#define PT_PTRACED 0x00000001
9090
#define PT_DTRACE 0x00000002 /* delayed trace (used on m68k, i386) */
91-
#define PT_TRACESYSGOOD 0x00000004
92-
#define PT_PTRACE_CAP 0x00000008 /* ptracer can follow suid-exec */
91+
#define PT_PTRACE_CAP 0x00000004 /* ptracer can follow suid-exec */
9392

93+
#define PT_OPT_FLAG_SHIFT 3
9494
/* PT_TRACE_* event enable flags */
95-
#define PT_EVENT_FLAG_SHIFT 4
96-
#define PT_EVENT_FLAG(event) (1 << (PT_EVENT_FLAG_SHIFT + (event) - 1))
97-
95+
#define PT_EVENT_FLAG(event) (1 << (PT_OPT_FLAG_SHIFT + (event)))
96+
#define PT_TRACESYSGOOD PT_EVENT_FLAG(0)
9897
#define PT_TRACE_FORK PT_EVENT_FLAG(PTRACE_EVENT_FORK)
9998
#define PT_TRACE_VFORK PT_EVENT_FLAG(PTRACE_EVENT_VFORK)
10099
#define PT_TRACE_CLONE PT_EVENT_FLAG(PTRACE_EVENT_CLONE)
101100
#define PT_TRACE_EXEC PT_EVENT_FLAG(PTRACE_EVENT_EXEC)
102101
#define PT_TRACE_VFORK_DONE PT_EVENT_FLAG(PTRACE_EVENT_VFORK_DONE)
103102
#define PT_TRACE_EXIT PT_EVENT_FLAG(PTRACE_EVENT_EXIT)
104103

105-
#define PT_TRACE_MASK 0x000003f4
106-
107104
/* single stepping state bits (used on ARM and PA-RISC) */
108105
#define PT_SINGLESTEP_BIT 31
109106
#define PT_SINGLESTEP (1<<PT_SINGLESTEP_BIT)

kernel/ptrace.c

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ static int ptrace_attach(struct task_struct *task, long request,
262262

263263
/*
264264
* Protect exec's credential calculations against our interference;
265-
* interference; SUID, SGID and LSM creds get determined differently
265+
* SUID, SGID and LSM creds get determined differently
266266
* under ptrace.
267267
*/
268268
retval = -ERESTARTNOINTR;
@@ -528,31 +528,16 @@ int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long ds
528528

529529
static int ptrace_setoptions(struct task_struct *child, unsigned long data)
530530
{
531+
unsigned flags;
532+
531533
if (data & ~(unsigned long)PTRACE_O_MASK)
532534
return -EINVAL;
533535

534-
child->ptrace &= ~PT_TRACE_MASK;
535-
536-
if (data & PTRACE_O_TRACESYSGOOD)
537-
child->ptrace |= PT_TRACESYSGOOD;
538-
539-
if (data & PTRACE_O_TRACEFORK)
540-
child->ptrace |= PT_TRACE_FORK;
541-
542-
if (data & PTRACE_O_TRACEVFORK)
543-
child->ptrace |= PT_TRACE_VFORK;
544-
545-
if (data & PTRACE_O_TRACECLONE)
546-
child->ptrace |= PT_TRACE_CLONE;
547-
548-
if (data & PTRACE_O_TRACEEXEC)
549-
child->ptrace |= PT_TRACE_EXEC;
550-
551-
if (data & PTRACE_O_TRACEVFORKDONE)
552-
child->ptrace |= PT_TRACE_VFORK_DONE;
553-
554-
if (data & PTRACE_O_TRACEEXIT)
555-
child->ptrace |= PT_TRACE_EXIT;
536+
/* Avoid intermediate state when all opts are cleared */
537+
flags = child->ptrace;
538+
flags &= ~(PTRACE_O_MASK << PT_OPT_FLAG_SHIFT);
539+
flags |= (data << PT_OPT_FLAG_SHIFT);
540+
child->ptrace = flags;
556541

557542
return 0;
558543
}

0 commit comments

Comments
 (0)