BPF List
 help / color / mirror / Atom feed
From: Martin KaFai Lau <martin.lau@linux.dev>
To: Stanislav Fomichev <sdf@google.com>
Cc: bpf <bpf@vger.kernel.org>
Subject: Re: [Question]: BPF_CGROUP_{GET,SET}SOCKOPT handling when optlen > PAGE_SIZE
Date: Wed, 26 Oct 2022 23:15:48 -0700	[thread overview]
Message-ID: <41284964-123d-704b-2802-24a857a7a989@linux.dev> (raw)
In-Reply-To: <CAKH8qBu7OXptKF46SQSEfueKXRUkBxix3K0qmucgREP4h_rQJQ@mail.gmail.com>

On 10/26/22 7:03 PM, Stanislav Fomichev wrote:
> On Wed, Oct 26, 2022 at 6:14 PM Martin KaFai Lau <martin.lau@linux.dev> wrote:
>>
>> The cgroup-bpf {get,set}sockopt prog is useful to change the optname behavior.
>> The bpf prog usually just handles a few specific optnames and ignores most
>> others.  For the optnames that it ignores, it usually does not need to change
>> the optlen.  The exception is when optlen > PAGE_SIZE (or optval_end - optval).
>> The bpf prog needs to set the optlen to 0 for this case or else the kernel will
>> return -EFAULT to the userspace.  It is usually not what the bpf prog wants
>> because the bpf prog only expects error returning to userspace when it has
>> explicitly 'return 0;' or used bpf_set_retval().  If a bpf prog always changes
>> optlen for optnames that it does not care to 0,  it may risk if the latter bpf
>> prog in the same cgroup may want to change/look-at it.
>>
>> Would like to explore if there is an easier way for the bpf prog to handle it.
>> eg. does it make sense to track if the bpf prog has changed the ctx->optlen
>> before returning -EFAULT to the user space when ctx.optlen > max_optlen?
> 
> Good point on chaining being broken because of this requirement :-/
> 
> With tracking, we need to be careful, because the following situation
> might be problematic:
> Suppose setsockopt is larger than 4k, the program can rewrite some
> byte in the first 4k, not touch optlen and expect this to work.

If the bpf prog rewrites the first 4k, it must change the ctx.optlen to get it 
work.  Otherwise, the kernel will return -EFAULT because the ctx.optlen is 
larger than the max_optlen (or optval_end - optval).

> Currently, optlen=0 explicitly means "ignore whatever is in the bpf
> buffer and use the original one" > If we can have a tracking that catches situations like this - we
> should be able to drop that optlen=0 requirement.
> IIRC, that's the only tricky part.

Ah, I meant, in __cgroup_bpf_run_filter_setsockopt, use "!ctx.optlen_changed && 
ctx.optlen > max_optlen" test to imply "ignore whatever is in the bpf
buffer and use the original one".  Add 'bool optlen_changed' to 'struct 
bpf_sockopt_kern' and set ctx.optlen_changed to true in 
cg_sockopt_convert_ctx_access() whenever there is BPF_WRITE to ctx.optlen. 
Would it work or may be I am still missing something in the writing first 4k 
case above?



  reply	other threads:[~2022-10-27  6:15 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-27  1:14 [Question]: BPF_CGROUP_{GET,SET}SOCKOPT handling when optlen > PAGE_SIZE Martin KaFai Lau
2022-10-27  2:03 ` Stanislav Fomichev
2022-10-27  6:15   ` Martin KaFai Lau [this message]
2022-10-27 16:23     ` Stanislav Fomichev
2022-10-27 17:28       ` Martin KaFai Lau
2022-10-27 17:40         ` Stanislav Fomichev
2022-10-27 18:48           ` Martin KaFai Lau
2022-10-27 19:11             ` Stanislav Fomichev
2022-10-27 20:04               ` Martin KaFai Lau
2022-10-27 20:14                 ` Stanislav Fomichev
2022-10-27 20:46 ` Andrii Nakryiko
2022-10-27 23:46   ` Martin KaFai Lau

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=41284964-123d-704b-2802-24a857a7a989@linux.dev \
    --to=martin.lau@linux.dev \
    --cc=bpf@vger.kernel.org \
    --cc=sdf@google.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