Skip to contentSkip to conversation

samples/landlock: fix path_list memory leak

PATCH Superseded [email protected] 3 participants 5 messages Apr 27, 2021 – Apr 28, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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
index 7a15910d2171..4629d011ed61 100644
@@ -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
#1 lore raw
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_list is allocated in parse_path() but never freed.

---
 samples/landlock/sandboxer.c | 2 ++
 1 file changed, 2 insertions(+)
samples/landlock/sandboxer.c +0 -0
index 7a15910d2171..4629d011ed61 100644

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 -1
        free(env_path_name);
        return ret;
 }
--

2.26.3

Signature · 2 lines
Thanks,
~Nick Desaulniers
maintainer Reviewed-by
#2 lore raw
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_list is allocated in parse_path() but never freed.

---
 samples/landlock/sandboxer.c | 2 ++
 1 file changed, 2 insertions(+)
samples/landlock/sandboxer.c +0 -0
index 7a15910d2171..4629d011ed61 100644
@@ -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 -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.

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 -1
        free(env_path_name);
        return ret;
 }
--

2.26.3

author
#3 lore raw
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_list is allocated in parse_path() but never freed.

---
  samples/landlock/sandboxer.c | 2 ++
  1 file changed, 2 insertions(+)
samples/landlock/sandboxer.c +0 -0
index 7a15910d2171..4629d011ed61 100644
@@ -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 -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 3 lines

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!

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
Quoted 7 lines
Quoted 6 lines
samples/landlock/sandboxer.c +0 -1
         free(env_path_name);
         return ret;
  }
--

2.26.3

maintainer
#4 lore raw
Quoted 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_list is allocated in parse_path() but never freed.

---
  samples/landlock/sandboxer.c | 2 ++

  1 file changed, 2 insertions(+)
samples/landlock/sandboxer.c +0 -0
index 7a15910d2171..4629d011ed61 100644
@@ -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_path has already been called/memory for path_list has already been allocated. parse_path doesn't check whether malloc has failed.

Quoted 8 lines
Quoted 3 lines

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!

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
Quoted 7 lines
Quoted 6 lines

         free(env_path_name);
         return ret;
  }

Signature · 1 line
2.26.3