From: Daniel Borkmann <daniel@iogearbox.net>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: bpf <bpf@vger.kernel.org>, Shung-Hsi Yu <shung-hsi.yu@suse.com>,
Andrii Nakryiko <andrii@kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
kongln9170@gmail.com
Subject: Re: [PATCH bpf-next v4 5/8] bpf: Zero former ARG_PTR_TO_{LONG,INT} args in case of error
Date: Thu, 12 Sep 2024 11:47:15 +0200 [thread overview]
Message-ID: <a86eb76d-f52f-dee4-e5d2-87e45de3e16f@iogearbox.net> (raw)
In-Reply-To: <fb38bb54-c59b-ba36-821f-f7dfcaa390cc@iogearbox.net>
On 9/9/24 2:16 PM, Daniel Borkmann wrote:
> On 9/7/24 12:35 AM, Alexei Starovoitov wrote:
>> On Fri, Sep 6, 2024 at 6:56 AM Daniel Borkmann <daniel@iogearbox.net> wrote:
>>>
>>> - if (unlikely(flags & ~(BPF_MTU_CHK_SEGS)))
>>> - return -EINVAL;
>>> -
>>> - if (unlikely(flags & BPF_MTU_CHK_SEGS && (len_diff || *mtu_len)))
>>> + if (unlikely((flags & ~(BPF_MTU_CHK_SEGS)) ||
>>> + (flags & BPF_MTU_CHK_SEGS && (len_diff || *mtu_len)))) {
>>> + *mtu_len = 0;
>>> return -EINVAL;
>>> + }
>>>
>>> dev = __dev_via_ifindex(dev, ifindex);
>>> - if (unlikely(!dev))
>>> + if (unlikely(!dev)) {
>>> + *mtu_len = 0;
>>> return -ENODEV;
>>> + }
>>
>> I don't understand this mtu_len clearing.
>>
>> My earlier comment was that mtu is in&out argument.
>> The program has to set it to something. It cannot be uninit.
>> So zeroing it on error looks very odd.
>>
>> In that sense the patch 3 looks wrong. Instead of:
>>
>> @@ -6346,7 +6346,9 @@ static const struct bpf_func_proto
>> bpf_skb_check_mtu_proto = {
>> .ret_type = RET_INTEGER,
>> .arg1_type = ARG_PTR_TO_CTX,
>> .arg2_type = ARG_ANYTHING,
>> - .arg3_type = ARG_PTR_TO_INT,
>> + .arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM |
>> + MEM_UNINIT | MEM_ALIGNED,
>> + .arg3_size = sizeof(u32),
>>
>> MEM_UNINIT should be removed, because
>> bpf_xdp_check_mtu, bpf_skb_check_mtu will read it.
>>
>> If there is a program out there that calls this helper without
>> initializing mtu_len it will be rejected after we fix it,
>> but I think that's a good thing.
>> Passing random mtu_len and let helper do:
>>
>> skb_len = *mtu_len ? *mtu_len + dev->hard_header_len : skb->len;
>>
>> is just bad.
>
> Ok, fair. Removing MEM_UNINIT sounds reasonable, was mostly thinking
> that even if its garbage MTU being passed in, mtu_len gets filled in
> either case (BPF_MTU_CHK_RET_{SUCCESS,FRAG_NEEDED}) assuming no invalid
> ifindex e.g.:
>
> __u32 mtu_len;
> bpf_skb_check_mtu(skb, skb->ifindex, &mtu_len, 0, 0);
Getting back at this, removing MEM_UNINIT needs more verifier rework:
MEM_UNINIT right now implies two things actually: i) write into memory,
ii) memory does not have to be initialized. If we lift MEM_UNINIT, it
then becomes: i) read into memory, ii) memory must be initialized.
This means that for bpf_*_check_mtu() we're readding the issue we're
trying to fix, that is, it would then be able to write back into things
like .rodata maps.
My suggestion is for this series is to go with MEM_UNINIT tag and
clearing on error case to avoid leaking, and then in a subsequent
series to break up MEM_UNINIT in the verifier into two properties:
MEM_WRITE and MEM_UNINIT to better declare intent of the helpers. Then
the bpf_*_check_mtu() will have MEM_WRITE but not MEM_UNINIT.
Thoughts? (If preference is to further extend this series, I can also
look into that ofc.)
Thanks,
Daniel
next prev parent reply other threads:[~2024-09-12 9:47 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-06 13:56 [PATCH bpf-next v4 1/8] bpf: Fix bpf_strtol and bpf_strtoul helpers for 32bit Daniel Borkmann
2024-09-06 13:56 ` [PATCH bpf-next v4 2/8] bpf: Remove truncation test in bpf_strtol and bpf_strtoul helpers Daniel Borkmann
2024-09-06 21:03 ` Andrii Nakryiko
2024-09-06 13:56 ` [PATCH bpf-next v4 3/8] bpf: Fix helper writes to read-only maps Daniel Borkmann
2024-09-06 21:03 ` Andrii Nakryiko
2024-09-06 22:36 ` Alexei Starovoitov
2024-09-09 11:51 ` Shung-Hsi Yu
2024-09-06 13:56 ` [PATCH bpf-next v4 4/8] bpf: Improve check_raw_mode_ok test for MEM_UNINIT-tagged types Daniel Borkmann
2024-09-06 21:03 ` Andrii Nakryiko
2024-09-09 12:24 ` Shung-Hsi Yu
2024-09-06 13:56 ` [PATCH bpf-next v4 5/8] bpf: Zero former ARG_PTR_TO_{LONG,INT} args in case of error Daniel Borkmann
2024-09-06 21:03 ` Andrii Nakryiko
2024-09-06 22:35 ` Alexei Starovoitov
2024-09-09 12:16 ` Daniel Borkmann
2024-09-12 9:47 ` Daniel Borkmann [this message]
2024-09-12 17:59 ` Andrii Nakryiko
2024-09-12 22:47 ` Daniel Borkmann
2024-09-06 13:56 ` [PATCH bpf-next v4 6/8] selftests/bpf: Fix ARG_PTR_TO_LONG {half-,}uninitialized test Daniel Borkmann
2024-09-06 21:03 ` Andrii Nakryiko
2024-09-06 13:56 ` [PATCH bpf-next v4 7/8] selftests/bpf: Rename ARG_PTR_TO_LONG test description Daniel Borkmann
2024-09-06 13:56 ` [PATCH bpf-next v4 8/8] selftests/bpf: Add a test case to write into .rodata Daniel Borkmann
2024-09-06 21:02 ` Andrii Nakryiko
2024-09-06 21:02 ` [PATCH bpf-next v4 1/8] bpf: Fix bpf_strtol and bpf_strtoul helpers for 32bit Andrii Nakryiko
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=a86eb76d-f52f-dee4-e5d2-87e45de3e16f@iogearbox.net \
--to=daniel@iogearbox.net \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=kongln9170@gmail.com \
--cc=shung-hsi.yu@suse.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