diff mbox series

[bpf-next,v3,1/1] samples/bpf: Add -fsanitize=bounds to userspace programs

Message ID 20230927045030.224548-2-ruowenq2@illinois.edu (mailing list archive)
State Accepted
Commit 9e09b75079e229b08f12a732712100fdb9af8cab
Delegated to: BPF
Headers show
Series samples/bpf: syscall_tp_user: Refactor and fix array index out-of-bounds bug | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 9 this patch: 9
netdev/cc_maintainers warning 12 maintainers not CCed: martin.lau@linux.dev trix@redhat.com jolsa@kernel.org ndesaulniers@google.com haoluo@google.com sdf@google.com john.fastabend@gmail.com yonghong.song@linux.dev llvm@lists.linux.dev kpsingh@kernel.org song@kernel.org nathan@kernel.org
netdev/build_clang success Errors and warnings before: 9 this patch: 9
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 9 this patch: 9
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 9 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-29 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-30 success Logs for veristat
bpf/vmtest-bpf-next-VM_Test-0 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-5 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-1 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for veristat
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-16 fail Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on s390x with gcc

Commit Message

ruowenq2@illinois.edu Sept. 27, 2023, 4:50 a.m. UTC
From: Ruowen Qin <ruowenq2@illinois.edu>

The sanitizer flag, which is supported by both clang and gcc, would make
it easier to debug array index out-of-bounds problems in these programs.

Make the Makfile smarter to detect ubsan support from the compiler and
add the '-fsanitize=bounds' accordingly.

Suggested-by: Mimi Zohar <zohar@linux.ibm.com>
Signed-off-by: Jinghao Jia <jinghao@linux.ibm.com>
Signed-off-by: Jinghao Jia <jinghao7@illinois.edu>
Signed-off-by: Ruowen Qin <ruowenq2@illinois.edu>
---
 samples/bpf/Makefile | 3 +++
 1 file changed, 3 insertions(+)

Comments

Jiri Olsa Sept. 27, 2023, 11:03 a.m. UTC | #1
On Tue, Sep 26, 2023 at 11:50:30PM -0500, ruowenq2@illinois.edu wrote:
> From: Ruowen Qin <ruowenq2@illinois.edu>
> 
> The sanitizer flag, which is supported by both clang and gcc, would make
> it easier to debug array index out-of-bounds problems in these programs.
> 
> Make the Makfile smarter to detect ubsan support from the compiler and
> add the '-fsanitize=bounds' accordingly.
> 
> Suggested-by: Mimi Zohar <zohar@linux.ibm.com>
> Signed-off-by: Jinghao Jia <jinghao@linux.ibm.com>
> Signed-off-by: Jinghao Jia <jinghao7@illinois.edu>
> Signed-off-by: Ruowen Qin <ruowenq2@illinois.edu>
> ---
>  samples/bpf/Makefile | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
> index 6c707ebcebb9..90af76fa9dd8 100644
> --- a/samples/bpf/Makefile
> +++ b/samples/bpf/Makefile
> @@ -169,6 +169,9 @@ endif
>  TPROGS_CFLAGS += -Wall -O2
>  TPROGS_CFLAGS += -Wmissing-prototypes
>  TPROGS_CFLAGS += -Wstrict-prototypes
> +TPROGS_CFLAGS += $(call try-run,\
> +	printf "int main() { return 0; }" |\
> +	$(CC) -Werror -fsanitize=bounds -x c - -o "$$TMP",-fsanitize=bounds,)

I haven't checked deeply, but could we use just cc-option? looks simpler

TPROGS_CFLAGS += $(call cc-option, -fsanitize=bounds)

jirka

>  
>  TPROGS_CFLAGS += -I$(objtree)/usr/include
>  TPROGS_CFLAGS += -I$(srctree)/tools/testing/selftests/bpf/
> -- 
> 2.42.0
> 
>
ruowenq2@illinois.edu Sept. 27, 2023, 11:19 p.m. UTC | #2
On 9/27/23 6:03 AM, Jiri Olsa <olsajiri@gmail.com> wrote:
> On Tue, Sep 26, 2023 at 11:50:30PM -0500, ruowenq2@illinois.edu wrote:
> > From: Ruowen Qin <ruowenq2@illinois.edu>
> >
> > The sanitizer flag, which is supported by both clang and gcc, would make
> > it easier to debug array index out-of-bounds problems in these programs.
> >
> > Make the Makfile smarter to detect ubsan support from the compiler and
> > add the '-fsanitize=bounds' accordingly.
> >
> > Suggested-by: Mimi Zohar <zohar@linux.ibm.com>
> > Signed-off-by: Jinghao Jia <jinghao@linux.ibm.com>
> > Signed-off-by: Jinghao Jia <jinghao7@illinois.edu>
> > Signed-off-by: Ruowen Qin <ruowenq2@illinois.edu>
> > ---
> >   samples/bpf/Makefile | 3 +++
> >   1 file changed, 3 insertions(+)
> >
> > diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
> > index 6c707ebcebb9..90af76fa9dd8 100644
> > --- a/samples/bpf/Makefile
> > +++ b/samples/bpf/Makefile
> > @@ -169,6 +169,9 @@ endif
> >   TPROGS_CFLAGS += -Wall -O2
> >   TPROGS_CFLAGS += -Wmissing-prototypes
> >   TPROGS_CFLAGS += -Wstrict-prototypes
> > +TPROGS_CFLAGS += $(call try-run,\
> > +	printf "int main() { return 0; }" |\
> > +	$(CC) -Werror -fsanitize=bounds -x c - -o "$$TMP",-fsanitize=bounds,)
> 
> I haven't checked deeply, but could we use just cc-option? looks simpler
> 
> TPROGS_CFLAGS += $(call cc-option, -fsanitize=bounds)
> 
> jirka

Hi, thanks for your quick reply! When checking for flags, cc-option does not execute the linker, but on Fedora, an error appears and stating that "/usr/lib64/libubsan.so.1.0.0" cannot be found during linking. So I try this seemingly cumbersome way.

Ruowen

> >   
> >   TPROGS_CFLAGS += -I$(objtree)/usr/include
> >   TPROGS_CFLAGS += -I$(srctree)/tools/testing/selftests/bpf/
> > -- 
> > 2.42.0
> >
> >
>
Jiri Olsa Sept. 28, 2023, 8:15 a.m. UTC | #3
On Wed, Sep 27, 2023 at 06:19:10PM -0500, ruowenq2@illinois.edu wrote:
> 
> 
> On 9/27/23 6:03 AM, Jiri Olsa <olsajiri@gmail.com> wrote:
> > On Tue, Sep 26, 2023 at 11:50:30PM -0500, ruowenq2@illinois.edu wrote:
> > > From: Ruowen Qin <ruowenq2@illinois.edu>
> > >
> > > The sanitizer flag, which is supported by both clang and gcc, would make
> > > it easier to debug array index out-of-bounds problems in these programs.
> > >
> > > Make the Makfile smarter to detect ubsan support from the compiler and
> > > add the '-fsanitize=bounds' accordingly.
> > >
> > > Suggested-by: Mimi Zohar <zohar@linux.ibm.com>
> > > Signed-off-by: Jinghao Jia <jinghao@linux.ibm.com>
> > > Signed-off-by: Jinghao Jia <jinghao7@illinois.edu>
> > > Signed-off-by: Ruowen Qin <ruowenq2@illinois.edu>
> > > ---
> > >   samples/bpf/Makefile | 3 +++
> > >   1 file changed, 3 insertions(+)
> > >
> > > diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
> > > index 6c707ebcebb9..90af76fa9dd8 100644
> > > --- a/samples/bpf/Makefile
> > > +++ b/samples/bpf/Makefile
> > > @@ -169,6 +169,9 @@ endif
> > >   TPROGS_CFLAGS += -Wall -O2
> > >   TPROGS_CFLAGS += -Wmissing-prototypes
> > >   TPROGS_CFLAGS += -Wstrict-prototypes
> > > +TPROGS_CFLAGS += $(call try-run,\
> > > +	printf "int main() { return 0; }" |\
> > > +	$(CC) -Werror -fsanitize=bounds -x c - -o "$$TMP",-fsanitize=bounds,)
> > 
> > I haven't checked deeply, but could we use just cc-option? looks simpler
> > 
> > TPROGS_CFLAGS += $(call cc-option, -fsanitize=bounds)
> > 
> > jirka
> 
> Hi, thanks for your quick reply! When checking for flags, cc-option does not execute the linker, but on Fedora, an error appears and stating that "/usr/lib64/libubsan.so.1.0.0" cannot be found during linking. So I try this seemingly cumbersome way.

I see, there's also ld-option, would that work?

jirka

> 
> Ruowen
> 
> > >   >   TPROGS_CFLAGS += -I$(objtree)/usr/include
> > >   TPROGS_CFLAGS += -I$(srctree)/tools/testing/selftests/bpf/
> > > -- > 2.42.0
> > >
> > >
> >
Jinghao Jia Sept. 28, 2023, 9:19 a.m. UTC | #4
On 9/28/23 3:15 AM, Jiri Olsa wrote:
> On Wed, Sep 27, 2023 at 06:19:10PM -0500, ruowenq2@illinois.edu wrote:
>>
>>
>> On 9/27/23 6:03 AM, Jiri Olsa <olsajiri@gmail.com> wrote:
>>> On Tue, Sep 26, 2023 at 11:50:30PM -0500, ruowenq2@illinois.edu wrote:
>>>> From: Ruowen Qin <ruowenq2@illinois.edu>
>>>>
>>>> The sanitizer flag, which is supported by both clang and gcc, would make
>>>> it easier to debug array index out-of-bounds problems in these programs.
>>>>
>>>> Make the Makfile smarter to detect ubsan support from the compiler and
>>>> add the '-fsanitize=bounds' accordingly.
>>>>
>>>> Suggested-by: Mimi Zohar <zohar@linux.ibm.com>
>>>> Signed-off-by: Jinghao Jia <jinghao@linux.ibm.com>
>>>> Signed-off-by: Jinghao Jia <jinghao7@illinois.edu>
>>>> Signed-off-by: Ruowen Qin <ruowenq2@illinois.edu>
>>>> ---
>>>>   samples/bpf/Makefile | 3 +++
>>>>   1 file changed, 3 insertions(+)
>>>>
>>>> diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
>>>> index 6c707ebcebb9..90af76fa9dd8 100644
>>>> --- a/samples/bpf/Makefile
>>>> +++ b/samples/bpf/Makefile
>>>> @@ -169,6 +169,9 @@ endif
>>>>   TPROGS_CFLAGS += -Wall -O2
>>>>   TPROGS_CFLAGS += -Wmissing-prototypes
>>>>   TPROGS_CFLAGS += -Wstrict-prototypes
>>>> +TPROGS_CFLAGS += $(call try-run,\
>>>> +	printf "int main() { return 0; }" |\
>>>> +	$(CC) -Werror -fsanitize=bounds -x c - -o "$$TMP",-fsanitize=bounds,)
>>>
>>> I haven't checked deeply, but could we use just cc-option? looks simpler
>>>
>>> TPROGS_CFLAGS += $(call cc-option, -fsanitize=bounds)
>>>
>>> jirka
>>
>> Hi, thanks for your quick reply! When checking for flags, cc-option does not execute the linker, but on Fedora, an error appears and stating that "/usr/lib64/libubsan.so.1.0.0" cannot be found during linking. So I try this seemingly cumbersome way.
> 
> I see, there's also ld-option, would that work?
> 
> jirka
> 

IMHO I don't think ld-option would solve the problem. It directly sends the
flag to the linker but -fsanitize=bounds is a compiler flag, not a linker
flag.

Basically, what's special about this case is that the feature we want to
probe is behind a gcc/clang flag but we do not know whether it is supported
until link time (e.g. the sanitizer library is missing on Fedora so we get
a link error).

--Jinghao

>>
>> Ruowen
>>
>>>>   >   TPROGS_CFLAGS += -I$(objtree)/usr/include
>>>>   TPROGS_CFLAGS += -I$(srctree)/tools/testing/selftests/bpf/
>>>> -- > 2.42.0
>>>>
>>>>
>>>
Jiri Olsa Sept. 28, 2023, 2:03 p.m. UTC | #5
On Thu, Sep 28, 2023 at 04:19:02AM -0500, Jinghao Jia wrote:
> 
> 
> On 9/28/23 3:15 AM, Jiri Olsa wrote:
> > On Wed, Sep 27, 2023 at 06:19:10PM -0500, ruowenq2@illinois.edu wrote:
> >>
> >>
> >> On 9/27/23 6:03 AM, Jiri Olsa <olsajiri@gmail.com> wrote:
> >>> On Tue, Sep 26, 2023 at 11:50:30PM -0500, ruowenq2@illinois.edu wrote:
> >>>> From: Ruowen Qin <ruowenq2@illinois.edu>
> >>>>
> >>>> The sanitizer flag, which is supported by both clang and gcc, would make
> >>>> it easier to debug array index out-of-bounds problems in these programs.
> >>>>
> >>>> Make the Makfile smarter to detect ubsan support from the compiler and
> >>>> add the '-fsanitize=bounds' accordingly.
> >>>>
> >>>> Suggested-by: Mimi Zohar <zohar@linux.ibm.com>
> >>>> Signed-off-by: Jinghao Jia <jinghao@linux.ibm.com>
> >>>> Signed-off-by: Jinghao Jia <jinghao7@illinois.edu>
> >>>> Signed-off-by: Ruowen Qin <ruowenq2@illinois.edu>
> >>>> ---
> >>>>   samples/bpf/Makefile | 3 +++
> >>>>   1 file changed, 3 insertions(+)
> >>>>
> >>>> diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
> >>>> index 6c707ebcebb9..90af76fa9dd8 100644
> >>>> --- a/samples/bpf/Makefile
> >>>> +++ b/samples/bpf/Makefile
> >>>> @@ -169,6 +169,9 @@ endif
> >>>>   TPROGS_CFLAGS += -Wall -O2
> >>>>   TPROGS_CFLAGS += -Wmissing-prototypes
> >>>>   TPROGS_CFLAGS += -Wstrict-prototypes
> >>>> +TPROGS_CFLAGS += $(call try-run,\
> >>>> +	printf "int main() { return 0; }" |\
> >>>> +	$(CC) -Werror -fsanitize=bounds -x c - -o "$$TMP",-fsanitize=bounds,)
> >>>
> >>> I haven't checked deeply, but could we use just cc-option? looks simpler
> >>>
> >>> TPROGS_CFLAGS += $(call cc-option, -fsanitize=bounds)
> >>>
> >>> jirka
> >>
> >> Hi, thanks for your quick reply! When checking for flags, cc-option does not execute the linker, but on Fedora, an error appears and stating that "/usr/lib64/libubsan.so.1.0.0" cannot be found during linking. So I try this seemingly cumbersome way.
> > 
> > I see, there's also ld-option, would that work?
> > 
> > jirka
> > 
> 
> IMHO I don't think ld-option would solve the problem. It directly sends the
> flag to the linker but -fsanitize=bounds is a compiler flag, not a linker
> flag.
> 
> Basically, what's special about this case is that the feature we want to
> probe is behind a gcc/clang flag but we do not know whether it is supported
> until link time (e.g. the sanitizer library is missing on Fedora so we get
> a link error).

ok, I tested on fedora, looks good

Acked-by: Jiri Olsa <jolsa@kernel.org>
Tested-by: Jiri Olsa <jolsa@kernel.org>

thanks,
jirka

> 
> --Jinghao
> 
> >>
> >> Ruowen
> >>
> >>>>   >   TPROGS_CFLAGS += -I$(objtree)/usr/include
> >>>>   TPROGS_CFLAGS += -I$(srctree)/tools/testing/selftests/bpf/
> >>>> -- > 2.42.0
> >>>>
> >>>>
> >>>
diff mbox series

Patch

diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
index 6c707ebcebb9..90af76fa9dd8 100644
--- a/samples/bpf/Makefile
+++ b/samples/bpf/Makefile
@@ -169,6 +169,9 @@  endif
 TPROGS_CFLAGS += -Wall -O2
 TPROGS_CFLAGS += -Wmissing-prototypes
 TPROGS_CFLAGS += -Wstrict-prototypes
+TPROGS_CFLAGS += $(call try-run,\
+	printf "int main() { return 0; }" |\
+	$(CC) -Werror -fsanitize=bounds -x c - -o "$$TMP",-fsanitize=bounds,)
 
 TPROGS_CFLAGS += -I$(objtree)/usr/include
 TPROGS_CFLAGS += -I$(srctree)/tools/testing/selftests/bpf/