selftests: cgroup: Test open-time cgroup namespace usage for migration checks
commit bf35a7879f1dfb0d050fe779168bcf25c7de66f5 upstream. When a task is writing to an fd opened by a different task, the perm check should use the cgroup namespace of the latter task. Add a test for it. Tested-by: Michal Koutný <mkoutny@suse.com> Signed-off-by: Tejun Heo <tj@kernel.org> [OP: backport to v5.4: adjust context, add wait.h and fcntl.h includes] Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
parent
a3f6c5949f
commit
598a22a077
@ -1,8 +1,13 @@
|
|||||||
/* SPDX-License-Identifier: GPL-2.0 */
|
/* SPDX-License-Identifier: GPL-2.0 */
|
||||||
|
|
||||||
|
#define _GNU_SOURCE
|
||||||
#include <linux/limits.h>
|
#include <linux/limits.h>
|
||||||
|
#include <linux/sched.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sched.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
@ -421,6 +426,99 @@ static int test_cgcore_lesser_euid_open(const char *root)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct lesser_ns_open_thread_arg {
|
||||||
|
const char *path;
|
||||||
|
int fd;
|
||||||
|
int err;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int lesser_ns_open_thread_fn(void *arg)
|
||||||
|
{
|
||||||
|
struct lesser_ns_open_thread_arg *targ = arg;
|
||||||
|
|
||||||
|
targ->fd = open(targ->path, O_RDWR);
|
||||||
|
targ->err = errno;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* cgroup migration permission check should be performed based on the cgroup
|
||||||
|
* namespace at the time of open instead of write.
|
||||||
|
*/
|
||||||
|
static int test_cgcore_lesser_ns_open(const char *root)
|
||||||
|
{
|
||||||
|
static char stack[65536];
|
||||||
|
const uid_t test_euid = 65534; /* usually nobody, any !root is fine */
|
||||||
|
int ret = KSFT_FAIL;
|
||||||
|
char *cg_test_a = NULL, *cg_test_b = NULL;
|
||||||
|
char *cg_test_a_procs = NULL, *cg_test_b_procs = NULL;
|
||||||
|
int cg_test_b_procs_fd = -1;
|
||||||
|
struct lesser_ns_open_thread_arg targ = { .fd = -1 };
|
||||||
|
pid_t pid;
|
||||||
|
int status;
|
||||||
|
|
||||||
|
cg_test_a = cg_name(root, "cg_test_a");
|
||||||
|
cg_test_b = cg_name(root, "cg_test_b");
|
||||||
|
|
||||||
|
if (!cg_test_a || !cg_test_b)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
cg_test_a_procs = cg_name(cg_test_a, "cgroup.procs");
|
||||||
|
cg_test_b_procs = cg_name(cg_test_b, "cgroup.procs");
|
||||||
|
|
||||||
|
if (!cg_test_a_procs || !cg_test_b_procs)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
if (cg_create(cg_test_a) || cg_create(cg_test_b))
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
if (cg_enter_current(cg_test_b))
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
if (chown(cg_test_a_procs, test_euid, -1) ||
|
||||||
|
chown(cg_test_b_procs, test_euid, -1))
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
targ.path = cg_test_b_procs;
|
||||||
|
pid = clone(lesser_ns_open_thread_fn, stack + sizeof(stack),
|
||||||
|
CLONE_NEWCGROUP | CLONE_FILES | CLONE_VM | SIGCHLD,
|
||||||
|
&targ);
|
||||||
|
if (pid < 0)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
if (waitpid(pid, &status, 0) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
if (!WIFEXITED(status))
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
cg_test_b_procs_fd = targ.fd;
|
||||||
|
if (cg_test_b_procs_fd < 0)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
if (cg_enter_current(cg_test_a))
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
if ((status = write(cg_test_b_procs_fd, "0", 1)) >= 0 || errno != ENOENT)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
ret = KSFT_PASS;
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
cg_enter_current(root);
|
||||||
|
if (cg_test_b_procs_fd >= 0)
|
||||||
|
close(cg_test_b_procs_fd);
|
||||||
|
if (cg_test_b)
|
||||||
|
cg_destroy(cg_test_b);
|
||||||
|
if (cg_test_a)
|
||||||
|
cg_destroy(cg_test_a);
|
||||||
|
free(cg_test_b_procs);
|
||||||
|
free(cg_test_a_procs);
|
||||||
|
free(cg_test_b);
|
||||||
|
free(cg_test_a);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
#define T(x) { x, #x }
|
#define T(x) { x, #x }
|
||||||
struct corecg_test {
|
struct corecg_test {
|
||||||
int (*fn)(const char *root);
|
int (*fn)(const char *root);
|
||||||
@ -434,6 +532,7 @@ struct corecg_test {
|
|||||||
T(test_cgcore_invalid_domain),
|
T(test_cgcore_invalid_domain),
|
||||||
T(test_cgcore_populated),
|
T(test_cgcore_populated),
|
||||||
T(test_cgcore_lesser_euid_open),
|
T(test_cgcore_lesser_euid_open),
|
||||||
|
T(test_cgcore_lesser_ns_open),
|
||||||
};
|
};
|
||||||
#undef T
|
#undef T
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user