BPF List
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: Daniel Xu <dxu@dxuuu.xyz>
Cc: acme@kernel.org, jolsa@kernel.org, quentin@isovalent.com,
	 alan.maguire@oracle.com, andrii.nakryiko@gmail.com,
	ast@kernel.org,  daniel@iogearbox.net, bpf@vger.kernel.org
Subject: Re: [PATCH dwarves v4 2/2] pahole: Inject kfunc decl tags into BTF
Date: Wed, 28 Feb 2024 23:33:28 +0200	[thread overview]
Message-ID: <773a54ed9f0cfb91c1b7c1978070bb86e42ac568.camel@gmail.com> (raw)
In-Reply-To: <7bpvkfzqfawuzltt7jicekwlbxokdbprom6io3fxef3rbng4ud@hwohkmt662ar>

On Wed, 2024-02-28 at 09:07 -0700, Daniel Xu wrote:
> Hi Eduard,
>
> Apologies for long delay - life has been busy.

Hi Daniel,

No problem, thank you for reaching back.

[...]

> > > +static char *get_func_name(const char *sym)
> > > +{
> > > +	char *func, *end;
> > > +
> > > +	if (strncmp(sym, BTF_ID_FUNC_PFX, sizeof(BTF_ID_FUNC_PFX) - 1))
> > > +		return NULL;
> > > +
> > > +	/* Strip prefix */
> > > +	func = strdup(sym + sizeof(BTF_ID_FUNC_PFX) - 1);
> > > +
> > > +	/* Strip suffix */
> > > +	end = strrchr(func, '_');
> > > +	if (!end || *(end - 1) != '_') {
> > 
> > Nit: this would do out of bounds access on malformed input
> >      "__BTF_ID__func___"
> 
> I think this is actually ok. Reason is we have the strncmp() above
> so we know the prefix is there. Then the strdup() in the malformed cased
> returns empty string. And strrchr() will then return NULL, so we don't
> enter the branch.
> 
> I tested it with: https://pastes.dxuuu.xyz/c3j4kk
> 
>         $ gcc test.c
>         dxu@kashmir~/scratch $ ./a.out
>         name=(null)
> 

The test is for "__BTF_ID__func__", but nitpick is for "__BTF_ID__func___"
(three underscores in the end).

E.g. here is a repro:

--- 8< ---------------------------------------------------------------

$ cat test.c

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define BTF_ID_FUNC_PFX         "__BTF_ID__func__"

static char *get_func_name(const char *sym)
{
        char *func, *end;

        if (strncmp(sym, BTF_ID_FUNC_PFX, sizeof(BTF_ID_FUNC_PFX) - 1))
                return NULL;

        /* Strip prefix */
        func = strdup(sym + sizeof(BTF_ID_FUNC_PFX) - 1);

        /* Strip suffix */
        end = strrchr(func, '_');
        if (!end || *(end - 1) != '_') {
                free(func);
                return NULL;
        }
        *(end - 1) = '\0';

        return func;
}

int main(int argc, char *argv[]) {
	if (argc < 2)
		return -1;

	printf("name='%s;\n", get_func_name(argv[1]));
	return 0;
}

$ gcc -g test.c

$ valgrind ./a.out __BTF_ID__func___

==16856== Memcheck, a memory error detector
==16856== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==16856== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info
==16856== Command: ./a.out __BTF_ID__func___
==16856== 
==16856== Invalid read of size 1
==16856==    at 0x4011EB: get_func_name (test.c:19)
==16856==    by 0x401244: main (test.c:32)
==16856==  Address 0x4a7e03f is 1 bytes before a block of size 2 alloc'd
==16856==    at 0x4845784: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==16856==    by 0x492176D: strdup (in /usr/lib64/libc.so.6)
==16856==    by 0x4011C2: get_func_name (test.c:15)
==16856==    by 0x401244: main (test.c:32)
==16856== 
name='(null);
==16856== 
==16856== HEAP SUMMARY:
==16856==     in use at exit: 0 bytes in 0 blocks
==16856==   total heap usage: 2 allocs, 2 frees, 1,026 bytes allocated
==16856== 
==16856== All heap blocks were freed -- no leaks are possible
==16856== 
==16856== For lists of detected and suppressed errors, rerun with: -s
==16856== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

--------------------------------------------------------------- >8 ---

Thanks,
Eduard

  reply	other threads:[~2024-02-28 21:33 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-04 18:40 [PATCH dwarves v4 0/2] pahole: Inject kfunc decl tags into BTF Daniel Xu
2024-02-04 18:40 ` [PATCH dwarves v4 1/2] pahole: Add --btf_feature=decl_tag_kfuncs feature Daniel Xu
2024-02-06 12:47   ` Jiri Olsa
2024-02-06 14:14     ` Alan Maguire
2024-02-04 18:40 ` [PATCH dwarves v4 2/2] pahole: Inject kfunc decl tags into BTF Daniel Xu
2024-02-05 16:54   ` Eduard Zingerman
2024-02-05 23:31   ` Eduard Zingerman
2024-02-28 16:07     ` Daniel Xu
2024-02-28 21:33       ` Eduard Zingerman [this message]
2024-03-15 19:43         ` Daniel Xu
2024-02-08 10:00   ` Alan Maguire
2024-02-29  0:57     ` Daniel Xu
2024-03-13  3:17       ` Daniel Xu
2024-03-13  9:36         ` Alan Maguire

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=773a54ed9f0cfb91c1b7c1978070bb86e42ac568.camel@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=acme@kernel.org \
    --cc=alan.maguire@oracle.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=dxu@dxuuu.xyz \
    --cc=jolsa@kernel.org \
    --cc=quentin@isovalent.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