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: 5e990dcef1 ("samples/landlock: Support TCP restrictions")
Signed-off-by: Ivanov Mikhail <ivanov.mikhail1@huawei-partners.com>
Reviewed-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20240326095625.3576164-1-ivanov.mikhail1@huawei-partners.com
Signed-off-by: Mickaël Salaün <mic@digikod.net>
This commit is contained in:
Ivanov Mikhail 2024-03-26 17:56:25 +08:00 committed by Mickaël Salaün
parent a38297e3fb
commit 42212936d9
No known key found for this signature in database
GPG Key ID: E5E3D0E88C82F6D2

View File

@ -153,7 +153,7 @@ static int populate_ruleset_net(const char *const env_var, const int ruleset_fd,
const __u64 allowed_access) const __u64 allowed_access)
{ {
int ret = 1; int ret = 1;
char *env_port_name, *strport; char *env_port_name, *env_port_name_next, *strport;
struct landlock_net_port_attr net_port = { struct landlock_net_port_attr net_port = {
.allowed_access = allowed_access, .allowed_access = allowed_access,
.port = 0, .port = 0,
@ -165,7 +165,8 @@ static int populate_ruleset_net(const char *const env_var, const int ruleset_fd,
env_port_name = strdup(env_port_name); env_port_name = strdup(env_port_name);
unsetenv(env_var); 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); net_port.port = atoi(strport);
if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT, if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
&net_port, 0)) { &net_port, 0)) {