From: Martin KaFai Lau <martin.lau@linux.dev>
To: Yonghong Song <yhs@meta.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Kernel Team <kernel-team@meta.com>,
Martin KaFai Lau <martin.lau@kernel.org>,
"bpf@vger.kernel.org" <bpf@vger.kernel.org>
Subject: Re: [PATCH bpf-next v8 4/4] selftests/bpf: Add tests for bpf_rcu_read_lock()
Date: Wed, 23 Nov 2022 15:13:54 -0800 [thread overview]
Message-ID: <73adebc6-ab32-08a3-9e62-460160d108cc@linux.dev> (raw)
In-Reply-To: <SN6PR1501MB20649F820B6FFE58166817E5CA0C9@SN6PR1501MB2064.namprd15.prod.outlook.com>
On 11/22/22 9:29 PM, Yonghong Song wrote:
>
>
> From: Martin KaFai Lau <martin.lau@linux.dev>
> Date: Tuesday, November 22, 2022 at 5:53 PM
> To: Yonghong Song <yhs@meta.com>
> Cc: Alexei Starovoitov <ast@kernel.org>, Andrii Nakryiko <andrii@kernel.org>, Daniel Borkmann <daniel@iogearbox.net>, Kernel Team <kernel-team@meta.com>, Martin KaFai Lau <martin.lau@kernel.org>, bpf@vger.kernel.org <bpf@vger.kernel.org>
> Subject: Re: [PATCH bpf-next v8 4/4] selftests/bpf: Add tests for bpf_rcu_read_lock()
> On 11/22/22 5:39 PM, Martin KaFai Lau wrote:
>> On 11/22/22 5:13 PM, Yonghong Song wrote:
>>>
>>>
>>> On 11/22/22 4:56 PM, Martin KaFai Lau wrote:
>>>> On 11/22/22 11:53 AM, Yonghong Song wrote:
>>>>> +SEC("?fentry.s/" SYS_PREFIX "sys_nanosleep")
>>>>> +int task_acquire(void *ctx)
>>>>> +{
>>>>> + struct task_struct *task, *real_parent;
>>>>> +
>>>>> + task = bpf_get_current_task_btf();
>>>>> + bpf_rcu_read_lock();
>>>>> + real_parent = task->real_parent;
>>>>> + /* acquire a reference which can be used outside rcu read lock region */
>>>>> + real_parent = bpf_task_acquire(real_parent);
>>>> Does the bpf_task_acquire() kfunc need a change to do refcount_inc_not_zero()
>>>> and KF_RET_NULL?
>>>
>>> We have this definition in kernel:
>>> BTF_ID_FLAGS(func, bpf_task_acquire, KF_ACQUIRE | KF_TRUSTED_ARGS)
>>>
>>> So the argument is trusted args so, either marked as PTR_TRUSTED/MEM_ALLOC or
>>> have a reference acquired already, so
>>> I guess we should be fine here.
>>
>>
>> The verifier part is fine on {KF_TRUSTED_ARGS, PTR_TRUSTED}.
>>
>> iiuc, PTR_TRUSTED means the kfunc can safely dereference the pointer because the
>> ptr has not been freed yet but does not mean its refcnt > 0 and not on its way
>> to be freed after the rcu gp.
>>
>> If real_parent's refcnt is 0 here, bpf_task_acquire() will resurrect a task
>> which is on its way to be freed and the task can be stored in a map, so a UAF.
> I see. Maybe we need strong trusted vs. weak trusted variants. Strong trusted means refcnt > 0 and weak means no guarantee? Or we consider everything as week and tries to grab a reference anyway? In most if not all cases, ‘current’ should represent a strong trusted btf_id I guess.
yeah, "current" task here is fine. current->real_parent is questionable.
imo, I think this check may be better done in runtime. The bpf_*_acquire() kfunc
should always do refcount_inc_not_zero() + KF_RET_NULL. Otherwise, it may end up
requiring to tag which ctx has a zero/non-zero refcnt. eg. the
security_sk_alloc() hook, the sk's refcnt is 0 and later the kernel does a
refcount_set(&sk->sk_refcnt, 1).
>
>> This could be addressed as a follow up though since it is not specific to this set.
> Right, we have the same potential problem for both task and cgroup acquire functions.
>
prev parent reply other threads:[~2022-11-23 23:14 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-22 19:53 [PATCH bpf-next v8 0/4] bpf: Add bpf_rcu_read_lock() support Yonghong Song
2022-11-22 19:53 ` [PATCH bpf-next v8 1/4] compiler_types: Define __rcu as __attribute__((btf_type_tag("rcu"))) Yonghong Song
2022-11-22 19:53 ` [PATCH bpf-next v8 2/4] bpf: Introduce might_sleep field in bpf_func_proto Yonghong Song
2022-11-22 19:53 ` [PATCH bpf-next v8 3/4] bpf: Add kfunc bpf_rcu_read_lock/unlock() Yonghong Song
2022-11-23 0:24 ` Martin KaFai Lau
2022-11-23 1:06 ` Yonghong Song
2022-11-23 1:19 ` Martin KaFai Lau
2022-11-23 1:24 ` Yonghong Song
2022-11-22 19:53 ` [PATCH bpf-next v8 4/4] selftests/bpf: Add tests for bpf_rcu_read_lock() Yonghong Song
2022-11-23 0:56 ` Martin KaFai Lau
2022-11-23 1:13 ` Yonghong Song
2022-11-23 1:39 ` Martin KaFai Lau
2022-11-23 1:52 ` Martin KaFai Lau
[not found] ` <SN6PR1501MB20649F820B6FFE58166817E5CA0C9@SN6PR1501MB2064.namprd15.prod.outlook.com>
2022-11-23 23:13 ` Martin KaFai Lau [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=73adebc6-ab32-08a3-9e62-460160d108cc@linux.dev \
--to=martin.lau@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kernel-team@meta.com \
--cc=martin.lau@kernel.org \
--cc=yhs@meta.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.