public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: Martin KaFai Lau <martin.lau@linux.dev>
To: Stanislav Fomichev <sdf@google.com>
Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
	song@kernel.org, yhs@fb.com, john.fastabend@gmail.com,
	kpsingh@kernel.org, haoluo@google.com, jolsa@kernel.org,
	bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next v2 2/4] selftests/bpf: Update EFAULT {g,s}etsockopt selftests
Date: Fri, 28 Apr 2023 17:44:27 -0700	[thread overview]
Message-ID: <b87d7403-a64e-3678-19a0-1b0072ee4198@linux.dev> (raw)
In-Reply-To: <CAKH8qBt-+GDxcfoQP6rmodQzRbZ-Lz11wUpVmP98zDm4qxJKAw@mail.gmail.com>

On 4/28/23 5:32 PM, Stanislav Fomichev wrote:
> On Fri, Apr 28, 2023 at 4:59 PM Stanislav Fomichev <sdf@google.com> wrote:
>>
>> On Fri, Apr 28, 2023 at 4:57 PM Martin KaFai Lau <martin.lau@linux.dev> wrote:
>>>
>>> On 4/27/23 1:04 PM, Stanislav Fomichev wrote:
>>>> Instead of assuming EFAULT, let's assume the BPF program's
>>>> output is ignored.
>>>>
>>>> Remove "getsockopt: deny arbitrary ctx->retval" because it
>>>> was actually testing optlen. We have separate set of tests
>>>> for retval.
>>>>
>>>> Signed-off-by: Stanislav Fomichev <sdf@google.com>
>>>> ---
>>>>    .../selftests/bpf/prog_tests/sockopt.c        | 80 +++++++++++++++++--
>>>>    1 file changed, 74 insertions(+), 6 deletions(-)
>>>>
>>>> diff --git a/tools/testing/selftests/bpf/prog_tests/sockopt.c b/tools/testing/selftests/bpf/prog_tests/sockopt.c
>>>> index aa4debf62fc6..8dad30ce910e 100644
>>>> --- a/tools/testing/selftests/bpf/prog_tests/sockopt.c
>>>> +++ b/tools/testing/selftests/bpf/prog_tests/sockopt.c
>>>> @@ -273,10 +273,30 @@ static struct sockopt_test {
>>>>                .error = EFAULT_GETSOCKOPT,
>>>>        },
>>>>        {
>>>> -             .descr = "getsockopt: deny arbitrary ctx->retval",
>>>> +             .descr = "getsockopt: ignore >PAGE_SIZE optlen",
>>>>                .insns = {
>>>> -                     /* ctx->retval = 123 */
>>>> -                     BPF_MOV64_IMM(BPF_REG_0, 123),
>>>> +                     /* write 0xFF to the first optval byte */
>>>> +
>>>> +                     /* r6 = ctx->optval */
>>>> +                     BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1,
>>>> +                                 offsetof(struct bpf_sockopt, optval)),
>>>> +                     /* r2 = ctx->optval */
>>>> +                     BPF_MOV64_REG(BPF_REG_2, BPF_REG_6),
>>>> +                     /* r6 = ctx->optval + 1 */
>>>> +                     BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, 1),
>>>> +
>>>> +                     /* r7 = ctx->optval_end */
>>>> +                     BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_1,
>>>> +                                 offsetof(struct bpf_sockopt, optval_end)),
>>>> +
>>>> +                     /* if (ctx->optval + 1 <= ctx->optval_end) { */
>>>> +                     BPF_JMP_REG(BPF_JGT, BPF_REG_6, BPF_REG_7, 1),
>>>> +                     /* ctx->optval[0] = 0xF0 */
>>>> +                     BPF_ST_MEM(BPF_B, BPF_REG_2, 0, 0xFF),
>>>> +                     /* } */
>>>> +
>>>> +                     /* ctx->retval = 0 */
>>>> +                     BPF_MOV64_IMM(BPF_REG_0, 0),
>>>>                        BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0,
>>>>                                    offsetof(struct bpf_sockopt, retval)),
>>>>
>>>> @@ -287,9 +307,10 @@ static struct sockopt_test {
>>>>                .attach_type = BPF_CGROUP_GETSOCKOPT,
>>>>                .expected_attach_type = BPF_CGROUP_GETSOCKOPT,
>>>>
>>>> -             .get_optlen = 64,
>>>> -
>>>> -             .error = EFAULT_GETSOCKOPT,
>>>> +             .get_level = 1234,
>>>> +             .get_optname = 5678,
>>>> +             .get_optval = {}, /* the changes are ignored */
>>>> +             .get_optlen = 4096 + 1,
>>>
>>> The patchset looks good. Thanks for taking care of it.
>>>
>>> One question, is it safe to the assume 4096 page size for all platforms in the
>>> selftests?
>>
>> Good question; let me respin with sysconf() just to be safe..
> 
> Argh, the compiler yells at me:
> error: initializer element is not a compile-time constant
> 
> I guess I'm just gonna do #define PAGE_SIZE 4096 and if we do hit some
> problems on the other archs, I'll ifdef it in one place.

or run_test() can reinit optlen to sysconf_page_size + 1 if optlen == 4097.

  reply	other threads:[~2023-04-29  0:44 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-27 20:04 [PATCH bpf-next v2 0/4] bpf: Don't EFAULT for {g,s}setsockopt with wrong optlen Stanislav Fomichev
2023-04-27 20:04 ` [PATCH bpf-next v2 1/4] " Stanislav Fomichev
2023-05-01  5:51   ` Martin KaFai Lau
2023-05-01 16:55     ` Stanislav Fomichev
2023-05-01 18:58       ` Martin KaFai Lau
2023-05-01 19:33         ` Stanislav Fomichev
2023-04-27 20:04 ` [PATCH bpf-next v2 2/4] selftests/bpf: Update EFAULT {g,s}etsockopt selftests Stanislav Fomichev
2023-04-28 23:57   ` Martin KaFai Lau
2023-04-28 23:59     ` Stanislav Fomichev
2023-04-29  0:32       ` Stanislav Fomichev
2023-04-29  0:44         ` Martin KaFai Lau [this message]
2023-05-01 17:22           ` Stanislav Fomichev
2023-05-01 19:04             ` Martin KaFai Lau
2023-04-27 20:04 ` [PATCH bpf-next v2 3/4] selftests/bpf: Correctly handle optlen > 4096 Stanislav Fomichev
2023-04-27 20:04 ` [PATCH bpf-next v2 4/4] bpf: Document EFAULT changes for sockopt Stanislav Fomichev

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=b87d7403-a64e-3678-19a0-1b0072ee4198@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=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=sdf@google.com \
    --cc=song@kernel.org \
    --cc=yhs@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