[RFC PATCH 3/3a] ptrace: add _ptrace_may_access()

From: Ingo Molnar
Date: Wed May 06 2009 - 04:01:59 EST



* Oleg Nesterov <oleg@xxxxxxxxxx> wrote:

> + task_lock(task);
> retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH);
> + task_unlock(task);
> if (retval)
> - goto bad;
> + goto unlock_creds;

Hm, that's a bit ugly - why dont you reuse ptrace_may_access(),
which does much of this already?

That way we could save a little bit of code size.

Something like the patch below allows the reuse of the locked
version of __ptrace_may_access and pushes the int->bool conversion
into an inline.

Note that this will also be a micro-optimization: the compiler will
be able to eliminate the inlined negation in the call sites (most of
which use the value directly) - and thus we save the negation.

( Untested - if you test it then it has my signoff. Patch also does
a small cleanup in _ptrace_may_access() )

Hm?

Ingo

diff --git a/include/linux/ptrace.h b/include/linux/ptrace.h
index 59e133d..2cc01ec 100644
--- a/include/linux/ptrace.h
+++ b/include/linux/ptrace.h
@@ -99,8 +99,13 @@ extern void exit_ptrace(struct task_struct *tracer);
#define PTRACE_MODE_ATTACH 2
/* Returns 0 on success, -errno on denial. */
extern int __ptrace_may_access(struct task_struct *task, unsigned int mode);
-/* Returns true on success, false on denial. */
-extern bool ptrace_may_access(struct task_struct *task, unsigned int mode);
+/* Also takes the task lock: */
+extern int _ptrace_may_access(struct task_struct *task, unsigned int mode);
+/* True on success, false on denial: */
+static inline bool ptrace_may_access(struct task_struct *task, unsigned int mode)
+{
+ return !_ptrace_may_access(task, mode);
+}

static inline int ptrace_reparented(struct task_struct *child)
{
diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index e950805..f7efcc9 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -155,13 +155,15 @@ int __ptrace_may_access(struct task_struct *task, unsigned int mode)
return security_ptrace_may_access(task, mode);
}

-bool ptrace_may_access(struct task_struct *task, unsigned int mode)
+int _ptrace_may_access(struct task_struct *task, unsigned int mode)
{
int err;
+
task_lock(task);
err = __ptrace_may_access(task, mode);
task_unlock(task);
- return !err;
+
+ return err;
}

int ptrace_attach(struct task_struct *task)
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/