diff mbox series

[bpf-next] samples/bpf: Don't use uninitialized cg2 variable

Message ID 20220720205336.3628755-1-deso@posteo.net (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series [bpf-next] samples/bpf: Don't use uninitialized cg2 variable | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 8 maintainers not CCed: haoluo@google.com song@kernel.org yhs@fb.com martin.lau@linux.dev john.fastabend@gmail.com jolsa@kernel.org sdf@google.com kpsingh@kernel.org
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
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: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 15 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-VM_Test-3 success Logs for Kernel LATEST on z15 with gcc
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for Kernel LATEST on ubuntu-latest with gcc
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Kernel LATEST on ubuntu-latest with llvm-15

Commit Message

Daniel Müller July 20, 2022, 8:53 p.m. UTC
When the setup_cgroup_environment function returns false, we enter a
cleanup path that uses the cg2 variable, which, at this point, is not
initialized.
This change fixes the issue by introducing a new error label that does
not perform the close operation which uses said variable on this path.

Signed-off-by: Daniel Müller <deso@posteo.net>
---
 samples/bpf/test_current_task_under_cgroup_user.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Daniel Müller July 20, 2022, 9:03 p.m. UTC | #1
On Wed, Jul 20, 2022 at 08:53:36PM +0000, Daniel Müller wrote:
> When the setup_cgroup_environment function returns false, we enter a
> cleanup path that uses the cg2 variable, which, at this point, is not
> initialized.
> This change fixes the issue by introducing a new error label that does
> not perform the close operation which uses said variable on this path.
> 
> Signed-off-by: Daniel Müller <deso@posteo.net>
> ---
>  samples/bpf/test_current_task_under_cgroup_user.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/samples/bpf/test_current_task_under_cgroup_user.c b/samples/bpf/test_current_task_under_cgroup_user.c
> index ac251a..04a37f 100644
> --- a/samples/bpf/test_current_task_under_cgroup_user.c
> +++ b/samples/bpf/test_current_task_under_cgroup_user.c
> @@ -55,7 +55,7 @@ int main(int argc, char **argv)
>  	}
>  
>  	if (setup_cgroup_environment())
> -		goto err;
> +		goto err_cgroup;
>  
>  	cg2 = create_and_get_cgroup(CGROUP_PATH);
>  
> @@ -104,6 +104,7 @@ int main(int argc, char **argv)
>  
>  err:
>  	close(cg2);
> +err_cgroup:
>  	cleanup_cgroup_environment();
>  
>  cleanup:
> -- 
> 2.30.2
> 

I see that I may have jumped the gun here. We probably shouldn't be cleaning up
the cgroup environment when the setup failed. Will update the patch.

Thanks,
Daniel
Daniel Müller July 20, 2022, 9:28 p.m. UTC | #2
On Wed, Jul 20, 2022 at 09:03:16PM +0000, Daniel Müller wrote:
> On Wed, Jul 20, 2022 at 08:53:36PM +0000, Daniel Müller wrote:
> > When the setup_cgroup_environment function returns false, we enter a
> > cleanup path that uses the cg2 variable, which, at this point, is not
> > initialized.
> > This change fixes the issue by introducing a new error label that does
> > not perform the close operation which uses said variable on this path.
> > 
> > Signed-off-by: Daniel Müller <deso@posteo.net>
> > ---
> >  samples/bpf/test_current_task_under_cgroup_user.c | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> > 
> > diff --git a/samples/bpf/test_current_task_under_cgroup_user.c b/samples/bpf/test_current_task_under_cgroup_user.c
> > index ac251a..04a37f 100644
> > --- a/samples/bpf/test_current_task_under_cgroup_user.c
> > +++ b/samples/bpf/test_current_task_under_cgroup_user.c
> > @@ -55,7 +55,7 @@ int main(int argc, char **argv)
> >  	}
> >  
> >  	if (setup_cgroup_environment())
> > -		goto err;
> > +		goto err_cgroup;
> >  
> >  	cg2 = create_and_get_cgroup(CGROUP_PATH);
> >  
> > @@ -104,6 +104,7 @@ int main(int argc, char **argv)
> >  
> >  err:
> >  	close(cg2);
> > +err_cgroup:
> >  	cleanup_cgroup_environment();
> >  
> >  cleanup:
> > -- 
> > 2.30.2
> > 
> 
> I see that I may have jumped the gun here. We probably shouldn't be cleaning up
> the cgroup environment when the setup failed. Will update the patch.

...but the semantics of setup_cgroup_environment are actually that it may leave
half initialized state around on failure and cleanup_cgroup_environment is
designed to deal with that. So we are good after all. Sorry for the noise.

Thanks,
Daniel
diff mbox series

Patch

diff --git a/samples/bpf/test_current_task_under_cgroup_user.c b/samples/bpf/test_current_task_under_cgroup_user.c
index ac251a..04a37f 100644
--- a/samples/bpf/test_current_task_under_cgroup_user.c
+++ b/samples/bpf/test_current_task_under_cgroup_user.c
@@ -55,7 +55,7 @@  int main(int argc, char **argv)
 	}
 
 	if (setup_cgroup_environment())
-		goto err;
+		goto err_cgroup;
 
 	cg2 = create_and_get_cgroup(CGROUP_PATH);
 
@@ -104,6 +104,7 @@  int main(int argc, char **argv)
 
 err:
 	close(cg2);
+err_cgroup:
 	cleanup_cgroup_environment();
 
 cleanup: