Skip to content

Commit

Permalink
samples/landlock: Fix incorrect free in populate_ruleset_net
Browse files Browse the repository at this point in the history
BugLink: https://bugs.launchpad.net/bugs/2071621

[ Upstream commit 4221293 ]

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]>
Signed-off-by: Sasha Levin <[email protected]>
Signed-off-by: Manuel Diewald <[email protected]>
Signed-off-by: Stefan Bader <[email protected]>
  • Loading branch information
sm1ling-knight authored and smb49 committed Jul 5, 2024
1 parent a3d1b5d commit f1097c9
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions samples/landlock/sandboxer.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ static int populate_ruleset_net(const char *const env_var, const int ruleset_fd,
const __u64 allowed_access)
{
int ret = 1;
char *env_port_name, *strport;
char *env_port_name, *env_port_name_next, *strport;
struct landlock_net_port_attr net_port = {
.allowed_access = allowed_access,
.port = 0,
Expand All @@ -163,7 +163,8 @@ static int populate_ruleset_net(const char *const env_var, const int ruleset_fd,
env_port_name = strdup(env_port_name);
unsetenv(env_var);

while ((strport = strsep(&env_port_name, ENV_DELIMITER))) {
env_port_name_next = env_port_name;
while ((strport = strsep(&env_port_name_next, ENV_DELIMITER))) {
net_port.port = atoi(strport);
if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
&net_port, 0)) {
Expand Down

0 comments on commit f1097c9

Please sign in to comment.