bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>,
	Viktor Malik <vmalik@redhat.com>
Cc: bpf@vger.kernel.org, Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann	 <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau	 <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	Yonghong Song	 <yonghong.song@linux.dev>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh	 <kpsingh@kernel.org>,
	Stanislav Fomichev <sdf@fomichev.me>,
	Hao Luo	 <haoluo@google.com>, Jiri Olsa <jolsa@kernel.org>
Subject: Re: [PATCH bpf-next v4 3/4] bpf: Add kfuncs for read-only string operations
Date: Fri, 09 May 2025 14:37:34 -0700	[thread overview]
Message-ID: <e1bb9c33b8852e1d3575f7cefe50aca266a8ff2b.camel@gmail.com> (raw)
In-Reply-To: <CAEf4BzZBB3rD0gfxq3ZC0_RuBjXHBMqdXxw3DcEyuYhmh7n5HA@mail.gmail.com>

On Fri, 2025-05-09 at 11:20 -0700, Andrii Nakryiko wrote:

[...]

> > +/**
> > + * bpf_strchr - Find the first occurrence of a character in a string
> > + * @s: The string to be searched
> > + * @c: The character to search for
> > + *
> > + * Note that the %NUL-terminator is considered part of the string, and can
> > + * be searched for.
> > + *
> > + * Return:
> > + * * const char * - Pointer to the first occurrence of @c within @s
> > + * * %NULL        - @c not found in @s
> > + * * %-EFAULT     - Cannot read @s
> > + * * %-E2BIG      - @s too large
> > + */
> > +__bpf_kfunc const char *bpf_strchr(const char *s, char c)
> 
> so let's say we found the character, we return a pointer to it, and
> that memory goes away (because we never owned it, so we don't really
> know what and when will happen with it). Question, will verifier allow
> BPF program to dereference this pointer? If yes, that's a problem. But
> if not, then I'm not sure there is much point in returning a pointer.
> 
> 
> I'm just trying to imply that in BPF world integer-based APIs work
> better/safer, overall? For strings, we can switch any
> pointer-returning API to position-returning (or negative error) API
> and it would more or less naturally fit into BPF API surface, no?

Integer based API solves the problem with memory access but is not
really ergonomic. W/o special logic in verifier the returned int would
be unbounded, hence the user would have to compare it with string
length before using.

It looks like some verifier logic is necessary regardless of API being
integer or pointer based. In any case verifier needs additional rules
for each pointer type to adjust bounds on the return value or its refobj_id.


  reply	other threads:[~2025-05-09 21:37 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-07  6:40 [PATCH bpf-next v4 0/4] bpf: Add kfuncs for read-only string operations Viktor Malik
2025-05-07  6:40 ` [PATCH bpf-next v4 1/4] bpf: Teach vefier to handle const ptrs as args to kfuncs Viktor Malik
2025-05-08  9:08   ` Matt Bobrowski
2025-05-09 16:20     ` Alexei Starovoitov
2025-05-13  6:48       ` Matt Bobrowski
2025-05-13  7:54         ` Viktor Malik
2025-05-13 14:58           ` Alexei Starovoitov
2025-05-13  7:58     ` Viktor Malik
2025-05-07  6:40 ` [PATCH bpf-next v4 2/4] uaccess: Define pagefault lock guard Viktor Malik
2025-05-08 10:00   ` Matt Bobrowski
2025-05-09 18:20     ` Andrii Nakryiko
2025-05-13  6:55       ` Matt Bobrowski
2025-05-07  6:40 ` [PATCH bpf-next v4 3/4] bpf: Add kfuncs for read-only string operations Viktor Malik
2025-05-08  9:41   ` Matt Bobrowski
2025-05-09 16:39     ` Alexei Starovoitov
2025-05-28  9:05       ` Viktor Malik
2025-05-15 12:32     ` Viktor Malik
2025-05-09 18:20   ` Andrii Nakryiko
2025-05-09 21:37     ` Eduard Zingerman [this message]
2025-05-09 22:03       ` Andrii Nakryiko
2025-05-15 12:27         ` Viktor Malik
2025-05-15 17:17           ` Andrii Nakryiko
2025-05-16  5:59             ` Viktor Malik
2025-05-16 15:50               ` Andrii Nakryiko
2025-05-07  6:40 ` [PATCH bpf-next v4 4/4] selftests/bpf: Add tests for string kfuncs Viktor Malik

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=e1bb9c33b8852e1d3575f7cefe50aca266a8ff2b.camel@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=andrii.nakryiko@gmail.com \
    --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=martin.lau@linux.dev \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=vmalik@redhat.com \
    --cc=yonghong.song@linux.dev \
    /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).