Skip to contentSkip to conversation

samples/bpf: Don't use uninitialized cg2 variable

PATCH In review Daniel Müller 1 participant 3 messages Jul 20, 2022 BPF

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 samples/bpf: Don't use uninitialized cg2 variable

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.

---
 samples/bpf/test_current_task_under_cgroup_user.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
samples/bpf/test_current_task_under_cgroup_user.c +2 -1
index ac251a..04a37f 100644
@@ -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:
Signature · 1 line
2.30.2
#1 lore raw
Quoted 35 lines · Daniel Müller

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.

---
 samples/bpf/test_current_task_under_cgroup_user.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
samples/bpf/test_current_task_under_cgroup_user.c +2 -1
index ac251a..04a37f 100644
@@ -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:
Signature · 1 line
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

#2 lore raw
Quoted 32 lines · Daniel Müller
Quoted 31 lines · Daniel Müller

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.

---
 samples/bpf/test_current_task_under_cgroup_user.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
samples/bpf/test_current_task_under_cgroup_user.c +2 -1
index ac251a..04a37f 100644
@@ -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();
 
Quoted 7 lines
Quoted 4 lines
samples/bpf/test_current_task_under_cgroup_user.c +0 -0
 cleanup:
Signature · 1 line
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