bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexei Starovoitov <alexei.starovoitov@gmail.com>
To: Dave Marchevsky <davemarchevsky@fb.com>
Cc: bpf <bpf@vger.kernel.org>, Kernel Team <kernel-team@fb.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Song Liu <songliubraving@fb.com>
Subject: Re: [PATCH bpf] bpf: refcount task stack in bpf_get_task_stack
Date: Thu, 1 Apr 2021 14:00:17 -0700	[thread overview]
Message-ID: <CAADnVQLik1UD+=+oAW1aq7HXdHwDBewcnYGyVcmZG2r5WT9YKQ@mail.gmail.com> (raw)
In-Reply-To: <20210401000747.3648767-1-davemarchevsky@fb.com>

On Wed, Mar 31, 2021 at 5:08 PM Dave Marchevsky <davemarchevsky@fb.com> wrote:
>
> On x86 the struct pt_regs * grabbed by task_pt_regs() points to an
> offset of task->stack. The pt_regs are later dereferenced in
> __bpf_get_stack (e.g. by user_mode() check). This can cause a fault if
> the task in question exits while bpf_get_task_stack is executing, as
> warned by task_stack_page's comment:
>
> * When accessing the stack of a non-current task that might exit, use
> * try_get_task_stack() instead.  task_stack_page will return a pointer
> * that could get freed out from under you.
>
> Taking the comment's advice and using try_get_task_stack() and
> put_task_stack() to hold task->stack refcount, or bail early if it's
> already 0. Incrementing stack_refcount will ensure the task's stack
> sticks around while we're using its data.
>
> I noticed this bug while testing a bpf task iter similar to
> bpf_iter_task_stack in selftests, except mine grabbed user stack, and
> getting intermittent crashes, which resulted in dumps like:
>
>   BUG: unable to handle page fault for address: 0000000000003fe0
>   \#PF: supervisor read access in kernel mode
>   \#PF: error_code(0x0000) - not-present page
>   RIP: 0010:__bpf_get_stack+0xd0/0x230
>   <snip...>
>   Call Trace:
>   bpf_prog_0a2be35c092cb190_get_task_stacks+0x5d/0x3ec
>   bpf_iter_run_prog+0x24/0x81
>   __task_seq_show+0x58/0x80
>   bpf_seq_read+0xf7/0x3d0
>   vfs_read+0x91/0x140
>   ksys_read+0x59/0xd0
>   do_syscall_64+0x48/0x120
>   entry_SYSCALL_64_after_hwframe+0x44/0xa9
>
> Fixes: fa28dcb82a38 ("bpf: Introduce helper bpf_get_task_stack()")
> Signed-off-by: Dave Marchevsky <davemarchevsky@fb.com>

Applied. Thanks

      parent reply	other threads:[~2021-04-01 21:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-01  0:07 [PATCH bpf] bpf: refcount task stack in bpf_get_task_stack Dave Marchevsky
2021-04-01  6:48 ` Song Liu
2021-04-01 17:47   ` Song Liu
2021-04-01 21:00 ` Alexei Starovoitov [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAADnVQLik1UD+=+oAW1aq7HXdHwDBewcnYGyVcmZG2r5WT9YKQ@mail.gmail.com' \
    --to=alexei.starovoitov@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davemarchevsky@fb.com \
    --cc=kernel-team@fb.com \
    --cc=songliubraving@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).