Skip to content

Commit 4221293

Browse files
sm1ling-knightl0kod
authored andcommitted
samples/landlock: Fix incorrect free in populate_ruleset_net
Pointer env_port_name changes after strsep(). Memory allocated via strdup() will not be freed if landlock_add_rule() returns non-zero value. Fixes: 5e990dc ("samples/landlock: Support TCP restrictions") Signed-off-by: Ivanov Mikhail <[email protected]> Reviewed-by: Konstantin Meskhidze <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mickaël Salaün <[email protected]>
1 parent a38297e commit 4221293

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

samples/landlock/sandboxer.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ static int populate_ruleset_net(const char *const env_var, const int ruleset_fd,
153153
const __u64 allowed_access)
154154
{
155155
int ret = 1;
156-
char *env_port_name, *strport;
156+
char *env_port_name, *env_port_name_next, *strport;
157157
struct landlock_net_port_attr net_port = {
158158
.allowed_access = allowed_access,
159159
.port = 0,
@@ -165,7 +165,8 @@ static int populate_ruleset_net(const char *const env_var, const int ruleset_fd,
165165
env_port_name = strdup(env_port_name);
166166
unsetenv(env_var);
167167

168-
while ((strport = strsep(&env_port_name, ENV_DELIMITER))) {
168+
env_port_name_next = env_port_name;
169+
while ((strport = strsep(&env_port_name_next, ENV_DELIMITER))) {
169170
net_port.port = atoi(strport);
170171
if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
171172
&net_port, 0)) {

0 commit comments

Comments
 (0)