samples/landlock: fix path_list memory leak
Subsystems (2)
PATCH 1/1 samples/landlock: fix path_list memory leak
From: Tom Rix <[email protected]>
Clang static analysis reports this error
sandboxer.c:134:8: warning: Potential leak of memory
pointed to by 'path_list'
ret = 0;
^path_list is allocated in parse_path() but never freed.
--- samples/landlock/sandboxer.c | 2 ++ 1 file changed, 2 insertions(+)
samples/landlock/sandboxer.c +2 -0
@@ -134,6 +134,8 @@ static int populate_ruleset( ret =0 ; out_free_name: +if (path_list) +free (path_list);free (env_path_name);return ret; }
Signature · 1 line
2.26.3
Quoted 21 lines · On Tue, Apr 27, 2021 at 11:38 AM <[email protected]> wrote:
From: Tom Rix <[email protected]>
Clang static analysis reports this error
sandboxer.c:134:8: warning: Potential leak of memory
pointed to by 'path_list'ret = 0; ^
path_listis allocated inparse_path()but never freed.Signed-off-by Tom Rix <[email protected]>--- samples/landlock/sandboxer.c | 2 ++ 1 file changed, 2 insertions(+)samples/landlock/sandboxer.c +0
-0
samples/landlock/sandboxer.c static int populate_ruleset( · line 138
Quoted 5 lines
samples/landlock/sandboxer.c +2
-0@@ -134,6 +134,8 @@ static int populate_ruleset( ret =0 ; out_free_name: +if (path_list) +free (path_list);
I don't think the conditional is even necessary? By our first goto out_free_name;, parse_path has already been called/memory for path_list has already been allocated. parse_path doesn't check whether malloc has failed.
Quoted 6 lines
samples/landlock/sandboxer.c +0
-1free (env_path_name);return ret; } --2.26.3
Signature · 2 lines
Thanks, ~Nick Desaulniers
Quoted 24 lines · Nick Desaulniers
Quoted 23 lines · On Tue, Apr 27, 2021 at 11:38 AM <[email protected]> wrote:
From: Tom Rix <[email protected]>
Clang static analysis reports this error
sandboxer.c:134:8: warning: Potential leak of memory
pointed to by 'path_list'ret = 0; ^
path_listis allocated inparse_path()but never freed.Signed-off-by Tom Rix <[email protected]>--- samples/landlock/sandboxer.c | 2 ++ 1 file changed, 2 insertions(+)samples/landlock/sandboxer.c +0
-0@@ -134,6 +134,8 @@ static int populate_ruleset( ret =0 ;
Quoted 8 lines
samples/landlock/sandboxer.c static int populate_ruleset( · line 138
Quoted 3 lines
samples/landlock/sandboxer.c +2
-0out_free_name: +if (path_list) +free (path_list);I don't think the conditional is even necessary? By our first
goto out_free_name;,parse_pathhas already been called/memory forpath_listhas already been allocated.parse_pathdoesn't check whethermallochas failed.
Indeed, no need for the path_list check. In practice, this memory leak doesn't stay long because of the execve, but I missed this free anyway. Thanks!
Quoted 9 lines
Quoted 7 lines
samples/landlock/sandboxer.c +0
-1free (env_path_name);return ret; } --2.26.3
Quoted 31 lines · Mickaël Salaün
Quoted 23 lines · Nick Desaulniers
Quoted 22 lines · On Tue, Apr 27, 2021 at 11:38 AM <[email protected]> wrote:
From: Tom Rix <[email protected]>
Clang static analysis reports this error
sandboxer.c:134:8: warning: Potential leak of memory
pointed to by 'path_list'ret = 0; ^
path_listis allocated inparse_path()but never freed.Signed-off-by Tom Rix <[email protected]>--- samples/landlock/sandboxer.c | 2 ++ 1 file changed, 2 insertions(+)samples/landlock/sandboxer.c +0
-0@@ -134,6 +134,8 @@ static int populate_ruleset( ret =0 ;Quoted 7 lines
samples/landlock/sandboxer.c static int populate_ruleset( · line 138
Quoted 3 lines
samples/landlock/sandboxer.c +2
-0out_free_name: +if (path_list) +free (path_list);I don't think the conditional is even necessary? By our first
goto out_free_name;,parse_pathhas already been called/memory forpath_listhas already been allocated.parse_pathdoesn't check whethermallochas failed.
Quoted 3 lines
Indeed, no need for the
path_listcheck. In practice, this memory leak doesn't stay long because of the execve, but I missed this free anyway. Thanks!
Ok, the general problem of not checking if malloc and friends succeeds is a different problem.
So remove the check and keep the free ?
Tom
Quoted 9 lines
Reviewed-by Mickaël Salaün <[email protected]>Quoted 7 lines
Quoted 6 lines
samples/landlock/sandboxer.c +0
-1free (env_path_name);return ret; } --2.26.3
Mickaël Salaün
maintainerQuoted 34 lines · Tom Rix
Quoted 32 lines · Mickaël Salaün
Quoted 31 lines · Nick Desaulniers
Quoted 23 lines · On Tue, Apr 27, 2021 at 11:38 AM <[email protected]> wrote:
From: Tom Rix <[email protected]>
Clang static analysis reports this error
sandboxer.c:134:8: warning: Potential leak of memory pointed to by '
path_list'
ret = 0;
^path_listis allocated inparse_path()but never freed.Signed-off-by Tom Rix <[email protected]>---
samples/landlock/sandboxer.c| 2 ++1 file changed, 2 insertions(+)samples/landlock/sandboxer.c +0
-0@@ -134,6 +134,8 @@ static int populate_ruleset( ret = 0;Quoted 3 lines
out_free_name:
+ if (path_list)
+free(path_list);I don't think the conditional is even necessary? By our first
goto out_free_name;,parse_pathhas already been called/memory forpath_listhas already been allocated.parse_pathdoesn't check whethermallochas failed.
Quoted 8 lines
Quoted 3 lines
Indeed, no need for the
path_listcheck. In practice, this memory leak doesn't stay long because of the execve, but I missed this free anyway. Thanks!Ok, the general problem of not checking if malloc and friends succeeds is a different problem.
So remove the check and keep the free ?
Yes please.
Quoted 13 lines
Tom
Quoted 9 lines
Reviewed-by Mickaël Salaün <[email protected]>Quoted 7 lines
Quoted 6 lines
free(env_path_name);
return ret;
}Signature · 1 line
2.26.3