The kprobe show() functions were using "current"'s creds instead of the file opener's creds for kallsyms visibility. Fix to use seq_file->file->f_cred.
Cc: stable@vger.kernel.org Fixes: 81365a947de4 ("kprobes: Show address of kprobes if kallsyms does") Fixes: ffb9bd68ebdb ("kprobes: Show blacklist addresses as same as kallsyms does") Signed-off-by: Kees Cook keescook@chromium.org --- kernel/kprobes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/kernel/kprobes.c b/kernel/kprobes.c index d4de217e4a91..2e97febeef77 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -2448,7 +2448,7 @@ static void report_probe(struct seq_file *pi, struct kprobe *p, else kprobe_type = "k";
- if (!kallsyms_show_value(current_cred())) + if (!kallsyms_show_value(pi->file->f_cred)) addr = NULL;
if (sym) @@ -2540,7 +2540,7 @@ static int kprobe_blacklist_seq_show(struct seq_file *m, void *v) * If /proc/kallsyms is not showing kernel address, we won't * show them here either. */ - if (!kallsyms_show_value(current_cred())) + if (!kallsyms_show_value(m->file->f_cred)) seq_printf(m, "0x%px-0x%px\t%ps\n", NULL, NULL, (void *)ent->start_addr); else
On Thu, Jul 2, 2020 at 4:26 PM Kees Cook keescook@chromium.org wrote:
The kprobe show() functions were using "current"'s creds instead of the file opener's creds for kallsyms visibility. Fix to use seq_file->file->f_cred.
Side note: I have a distinct - but despite that possibly quite incorrect - memory that I've discussed with somebody several years ago about making "current_cred()" simply warn in any IO context.
IOW, we could have read and write just increment/decrement a per-thread counter, and have current_cred() do a WARN_ON_ONCE() if it's called with that counter incremented.
The issue of ioctl's is a bit less obvious - there are reasons to argue those should also use open-time credentials, but on the other hand I think it's reasonable to pass a file descriptor to a suid app in order for that app to do things that the normal user cannot.
But read/write are dangerous because of the "it's so easy to fool suid apps to read/write stdin/stdout".
So pread/pwrite/ioctl/splice etc are things that suid applications very much do on purpose to affect a file descriptor. But plain read/write are things that might be accidental and used by attack vectors.
If somebody is interested in looking into things like that, it might be a good idea to have kernel threads with that counter incremented by default.
Just throwing this idea out in case somebody wants to try it. It's not just "current_cred", of course. It's all the current_cred_xxx() users too. But it may be that there are a ton of false positives because maybe some code on purpose ends up doing things like just *comparing* current_cred with file->f_cred and then that would warn too.
Linus
On Thu, Jul 02, 2020 at 06:00:17PM -0700, Linus Torvalds wrote:
On Thu, Jul 2, 2020 at 4:26 PM Kees Cook keescook@chromium.org wrote:
The kprobe show() functions were using "current"'s creds instead of the file opener's creds for kallsyms visibility. Fix to use seq_file->file->f_cred.
Side note: I have a distinct - but despite that possibly quite incorrect - memory that I've discussed with somebody several years ago about making "current_cred()" simply warn in any IO context.
IOW, we could have read and write just increment/decrement a per-thread counter, and have current_cred() do a WARN_ON_ONCE() if it's called with that counter incremented.
That does sound familiar. I can't find a thread on it, but my search abilities are poor. :) So an increment/decrement in all the IO-related syscalls, or were you thinking of some other location?
The issue of ioctl's is a bit less obvious - there are reasons to argue those should also use open-time credentials, but on the other hand I think it's reasonable to pass a file descriptor to a suid app in order for that app to do things that the normal user cannot.
But read/write are dangerous because of the "it's so easy to fool suid apps to read/write stdin/stdout".
So pread/pwrite/ioctl/splice etc are things that suid applications very much do on purpose to affect a file descriptor. But plain read/write are things that might be accidental and used by attack vectors.
So probably just start with read/write and tighten it over time, if we find other clear places, leaving ioctl/pread/pwrite/splice alone.
If somebody is interested in looking into things like that, it might be a good idea to have kernel threads with that counter incremented by
Just throwing this idea out in case somebody wants to try it. It's not just "current_cred", of course. It's all the current_cred_xxx() users too. But it may be that there are a ton of false positives because maybe some code on purpose ends up doing things like just *comparing* current_cred with file->f_cred and then that would warn too.
Yeah ... and I think the kthread test should answer that question.
On Thu, Jul 02, 2020 at 06:00:17PM -0700, Linus Torvalds wrote:
If somebody is interested in looking into things like that, it might be a good idea to have kernel threads with that counter incremented by default.
With 67 kthreads on a booted system, this patch does not immediately blow up... And it likely needs some beautification. (Note that current_cred_*() calls current_cred() under the hood, so AFAICT, only current_cred() needs coverage.)
diff --git a/include/linux/cred.h b/include/linux/cred.h index 18639c069263..a624847cb0ce 100644 --- a/include/linux/cred.h +++ b/include/linux/cred.h @@ -295,7 +295,10 @@ static inline void put_cred(const struct cred *_cred) * since nobody else can modify it. */ #define current_cred() \ - rcu_dereference_protected(current->cred, 1) +({ \ + WARN_ON_ONCE(current->warn_on_current_cred); \ + rcu_dereference_protected(current->cred, 1); \ +})
/** * current_real_cred - Access the current task's objective credentials diff --git a/include/linux/sched.h b/include/linux/sched.h index b62e6aaf28f0..21ab1b81aa40 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -652,6 +652,7 @@ struct task_struct { /* Per task flags (PF_*), defined further below: */ unsigned int flags; unsigned int ptrace; + unsigned int warn_on_current_cred;
#ifdef CONFIG_SMP struct llist_node wake_entry; diff --git a/kernel/fork.c b/kernel/fork.c index 142b23645d82..2e181b9bfd3f 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2527,8 +2527,12 @@ pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) .stack = (unsigned long)fn, .stack_size = (unsigned long)arg, }; + pid_t pid;
- return _do_fork(&args); + pid = _do_fork(&args); + if (pid == 0) + current->warn_on_current_cred = 1; + return pid; }
#ifdef __ARCH_WANT_SYS_FORK
On Fri, Jul 3, 2020 at 8:50 AM Kees Cook keescook@chromium.org wrote:
With 67 kthreads on a booted system, this patch does not immediately blow up...
Did you try making read/write inc/dec that thing too? Or does that just blow up with tons of warnings?
Linus
On Sun, Jul 05, 2020 at 01:10:54PM -0700, Linus Torvalds wrote:
On Fri, Jul 3, 2020 at 8:50 AM Kees Cook keescook@chromium.org wrote:
With 67 kthreads on a booted system, this patch does not immediately blow up...
Did you try making read/write inc/dec that thing too? Or does that just blow up with tons of warnings?
I hadn't gotten to that yet. I need to catch up on other stuff first, and I thought I'd give people time to scream if they tested this themselves. :)
On Thu, 2 Jul 2020 16:26:37 -0700 Kees Cook keescook@chromium.org wrote:
The kprobe show() functions were using "current"'s creds instead of the file opener's creds for kallsyms visibility. Fix to use seq_file->file->f_cred.
This looks good to me.
Acked-by: Masami Hiramatsu mhiramat@kernel.org
Thanks!
Cc: stable@vger.kernel.org Fixes: 81365a947de4 ("kprobes: Show address of kprobes if kallsyms does") Fixes: ffb9bd68ebdb ("kprobes: Show blacklist addresses as same as kallsyms does") Signed-off-by: Kees Cook keescook@chromium.org
kernel/kprobes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/kernel/kprobes.c b/kernel/kprobes.c index d4de217e4a91..2e97febeef77 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -2448,7 +2448,7 @@ static void report_probe(struct seq_file *pi, struct kprobe *p, else kprobe_type = "k";
- if (!kallsyms_show_value(current_cred()))
- if (!kallsyms_show_value(pi->file->f_cred)) addr = NULL;
if (sym) @@ -2540,7 +2540,7 @@ static int kprobe_blacklist_seq_show(struct seq_file *m, void *v) * If /proc/kallsyms is not showing kernel address, we won't * show them here either. */
- if (!kallsyms_show_value(current_cred()))
- if (!kallsyms_show_value(m->file->f_cred)) seq_printf(m, "0x%px-0x%px\t%ps\n", NULL, NULL, (void *)ent->start_addr); else
-- 2.25.1
linux-stable-mirror@lists.linaro.org