| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429 |
- // SPDX-License-Identifier: GPL-2.0
- #define _GNU_SOURCE
- #include <errno.h>
- #include <fcntl.h>
- #include <grp.h>
- #include <limits.h>
- #include <sched.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/mount.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <linux/unistd.h>
- #include "kselftest_harness.h"
- #ifndef FD_NSFS_ROOT
- #define FD_NSFS_ROOT -10003 /* Root of the nsfs filesystem */
- #endif
- TEST(nsfs_net_handle)
- {
- struct file_handle *handle;
- int mount_id;
- int ret;
- int fd;
- int ns_fd;
- struct stat st1, st2;
- /* Drop to unprivileged uid/gid */
- ASSERT_EQ(setresgid(65534, 65534, 65534), 0); /* nogroup */
- ASSERT_EQ(setresuid(65534, 65534, 65534), 0); /* nobody */
- handle = malloc(sizeof(*handle) + MAX_HANDLE_SZ);
- ASSERT_NE(handle, NULL);
- /* Open a namespace file descriptor */
- ns_fd = open("/proc/self/ns/net", O_RDONLY);
- ASSERT_GE(ns_fd, 0);
- /* Get handle for the namespace */
- handle->handle_bytes = MAX_HANDLE_SZ;
- ret = name_to_handle_at(ns_fd, "", handle, &mount_id, AT_EMPTY_PATH);
- if (ret < 0 && errno == EOPNOTSUPP) {
- SKIP(free(handle); close(ns_fd);
- return, "nsfs doesn't support file handles");
- }
- ASSERT_EQ(ret, 0);
- ASSERT_GT(handle->handle_bytes, 0);
- /* Try to open using FD_NSFS_ROOT as unprivileged user */
- fd = open_by_handle_at(FD_NSFS_ROOT, handle, O_RDONLY);
- if (fd < 0 && (errno == EINVAL || errno == EOPNOTSUPP)) {
- SKIP(free(handle); close(ns_fd);
- return,
- "open_by_handle_at with FD_NSFS_ROOT not supported");
- }
- if (fd < 0 && errno == EPERM) {
- SKIP(free(handle); close(ns_fd);
- return,
- "Permission denied for unprivileged user (expected)");
- }
- ASSERT_GE(fd, 0);
- /* Verify we opened the correct namespace */
- ASSERT_EQ(fstat(ns_fd, &st1), 0);
- ASSERT_EQ(fstat(fd, &st2), 0);
- ASSERT_EQ(st1.st_ino, st2.st_ino);
- ASSERT_EQ(st1.st_dev, st2.st_dev);
- close(fd);
- close(ns_fd);
- free(handle);
- }
- TEST(nsfs_uts_handle)
- {
- struct file_handle *handle;
- int mount_id;
- int ret;
- int fd;
- int ns_fd;
- struct stat st1, st2;
- /* Drop to unprivileged uid/gid */
- ASSERT_EQ(setresgid(65534, 65534, 65534), 0); /* nogroup */
- ASSERT_EQ(setresuid(65534, 65534, 65534), 0); /* nobody */
- handle = malloc(sizeof(*handle) + MAX_HANDLE_SZ);
- ASSERT_NE(handle, NULL);
- /* Open UTS namespace file descriptor */
- ns_fd = open("/proc/self/ns/uts", O_RDONLY);
- ASSERT_GE(ns_fd, 0);
- /* Get handle for the namespace */
- handle->handle_bytes = MAX_HANDLE_SZ;
- ret = name_to_handle_at(ns_fd, "", handle, &mount_id, AT_EMPTY_PATH);
- if (ret < 0 && errno == EOPNOTSUPP) {
- SKIP(free(handle); close(ns_fd);
- return, "nsfs doesn't support file handles");
- }
- ASSERT_EQ(ret, 0);
- ASSERT_GT(handle->handle_bytes, 0);
- /* Try to open using FD_NSFS_ROOT */
- fd = open_by_handle_at(FD_NSFS_ROOT, handle, O_RDONLY);
- if (fd < 0 && (errno == EINVAL || errno == EOPNOTSUPP)) {
- SKIP(free(handle); close(ns_fd);
- return,
- "open_by_handle_at with FD_NSFS_ROOT not supported");
- }
- ASSERT_GE(fd, 0);
- /* Verify we opened the correct namespace */
- ASSERT_EQ(fstat(ns_fd, &st1), 0);
- ASSERT_EQ(fstat(fd, &st2), 0);
- ASSERT_EQ(st1.st_ino, st2.st_ino);
- ASSERT_EQ(st1.st_dev, st2.st_dev);
- close(fd);
- close(ns_fd);
- free(handle);
- }
- TEST(nsfs_ipc_handle)
- {
- struct file_handle *handle;
- int mount_id;
- int ret;
- int fd;
- int ns_fd;
- struct stat st1, st2;
- /* Drop to unprivileged uid/gid */
- ASSERT_EQ(setresgid(65534, 65534, 65534), 0); /* nogroup */
- ASSERT_EQ(setresuid(65534, 65534, 65534), 0); /* nobody */
- handle = malloc(sizeof(*handle) + MAX_HANDLE_SZ);
- ASSERT_NE(handle, NULL);
- /* Open IPC namespace file descriptor */
- ns_fd = open("/proc/self/ns/ipc", O_RDONLY);
- ASSERT_GE(ns_fd, 0);
- /* Get handle for the namespace */
- handle->handle_bytes = MAX_HANDLE_SZ;
- ret = name_to_handle_at(ns_fd, "", handle, &mount_id, AT_EMPTY_PATH);
- if (ret < 0 && errno == EOPNOTSUPP) {
- SKIP(free(handle); close(ns_fd);
- return, "nsfs doesn't support file handles");
- }
- ASSERT_EQ(ret, 0);
- ASSERT_GT(handle->handle_bytes, 0);
- /* Try to open using FD_NSFS_ROOT */
- fd = open_by_handle_at(FD_NSFS_ROOT, handle, O_RDONLY);
- if (fd < 0 && (errno == EINVAL || errno == EOPNOTSUPP)) {
- SKIP(free(handle); close(ns_fd);
- return,
- "open_by_handle_at with FD_NSFS_ROOT not supported");
- }
- ASSERT_GE(fd, 0);
- /* Verify we opened the correct namespace */
- ASSERT_EQ(fstat(ns_fd, &st1), 0);
- ASSERT_EQ(fstat(fd, &st2), 0);
- ASSERT_EQ(st1.st_ino, st2.st_ino);
- ASSERT_EQ(st1.st_dev, st2.st_dev);
- close(fd);
- close(ns_fd);
- free(handle);
- }
- TEST(nsfs_pid_handle)
- {
- struct file_handle *handle;
- int mount_id;
- int ret;
- int fd;
- int ns_fd;
- struct stat st1, st2;
- /* Drop to unprivileged uid/gid */
- ASSERT_EQ(setresgid(65534, 65534, 65534), 0); /* nogroup */
- ASSERT_EQ(setresuid(65534, 65534, 65534), 0); /* nobody */
- handle = malloc(sizeof(*handle) + MAX_HANDLE_SZ);
- ASSERT_NE(handle, NULL);
- /* Open PID namespace file descriptor */
- ns_fd = open("/proc/self/ns/pid", O_RDONLY);
- ASSERT_GE(ns_fd, 0);
- /* Get handle for the namespace */
- handle->handle_bytes = MAX_HANDLE_SZ;
- ret = name_to_handle_at(ns_fd, "", handle, &mount_id, AT_EMPTY_PATH);
- if (ret < 0 && errno == EOPNOTSUPP) {
- SKIP(free(handle); close(ns_fd);
- return, "nsfs doesn't support file handles");
- }
- ASSERT_EQ(ret, 0);
- ASSERT_GT(handle->handle_bytes, 0);
- /* Try to open using FD_NSFS_ROOT */
- fd = open_by_handle_at(FD_NSFS_ROOT, handle, O_RDONLY);
- if (fd < 0 && (errno == EINVAL || errno == EOPNOTSUPP)) {
- SKIP(free(handle); close(ns_fd);
- return,
- "open_by_handle_at with FD_NSFS_ROOT not supported");
- }
- ASSERT_GE(fd, 0);
- /* Verify we opened the correct namespace */
- ASSERT_EQ(fstat(ns_fd, &st1), 0);
- ASSERT_EQ(fstat(fd, &st2), 0);
- ASSERT_EQ(st1.st_ino, st2.st_ino);
- ASSERT_EQ(st1.st_dev, st2.st_dev);
- close(fd);
- close(ns_fd);
- free(handle);
- }
- TEST(nsfs_mnt_handle)
- {
- struct file_handle *handle;
- int mount_id;
- int ret;
- int fd;
- int ns_fd;
- struct stat st1, st2;
- /* Drop to unprivileged uid/gid */
- ASSERT_EQ(setresgid(65534, 65534, 65534), 0); /* nogroup */
- ASSERT_EQ(setresuid(65534, 65534, 65534), 0); /* nobody */
- handle = malloc(sizeof(*handle) + MAX_HANDLE_SZ);
- ASSERT_NE(handle, NULL);
- /* Open mount namespace file descriptor */
- ns_fd = open("/proc/self/ns/mnt", O_RDONLY);
- ASSERT_GE(ns_fd, 0);
- /* Get handle for the namespace */
- handle->handle_bytes = MAX_HANDLE_SZ;
- ret = name_to_handle_at(ns_fd, "", handle, &mount_id, AT_EMPTY_PATH);
- if (ret < 0 && errno == EOPNOTSUPP) {
- SKIP(free(handle); close(ns_fd);
- return, "nsfs doesn't support file handles");
- }
- ASSERT_EQ(ret, 0);
- ASSERT_GT(handle->handle_bytes, 0);
- /* Try to open using FD_NSFS_ROOT */
- fd = open_by_handle_at(FD_NSFS_ROOT, handle, O_RDONLY);
- if (fd < 0 && (errno == EINVAL || errno == EOPNOTSUPP)) {
- SKIP(free(handle); close(ns_fd);
- return,
- "open_by_handle_at with FD_NSFS_ROOT not supported");
- }
- ASSERT_GE(fd, 0);
- /* Verify we opened the correct namespace */
- ASSERT_EQ(fstat(ns_fd, &st1), 0);
- ASSERT_EQ(fstat(fd, &st2), 0);
- ASSERT_EQ(st1.st_ino, st2.st_ino);
- ASSERT_EQ(st1.st_dev, st2.st_dev);
- close(fd);
- close(ns_fd);
- free(handle);
- }
- TEST(nsfs_user_handle)
- {
- struct file_handle *handle;
- int mount_id;
- int ret;
- int fd;
- int ns_fd;
- struct stat st1, st2;
- /* Drop to unprivileged uid/gid */
- ASSERT_EQ(setresgid(65534, 65534, 65534), 0); /* nogroup */
- ASSERT_EQ(setresuid(65534, 65534, 65534), 0); /* nobody */
- handle = malloc(sizeof(*handle) + MAX_HANDLE_SZ);
- ASSERT_NE(handle, NULL);
- /* Open user namespace file descriptor */
- ns_fd = open("/proc/self/ns/user", O_RDONLY);
- ASSERT_GE(ns_fd, 0);
- /* Get handle for the namespace */
- handle->handle_bytes = MAX_HANDLE_SZ;
- ret = name_to_handle_at(ns_fd, "", handle, &mount_id, AT_EMPTY_PATH);
- if (ret < 0 && errno == EOPNOTSUPP) {
- SKIP(free(handle); close(ns_fd);
- return, "nsfs doesn't support file handles");
- }
- ASSERT_EQ(ret, 0);
- ASSERT_GT(handle->handle_bytes, 0);
- /* Try to open using FD_NSFS_ROOT */
- fd = open_by_handle_at(FD_NSFS_ROOT, handle, O_RDONLY);
- if (fd < 0 && (errno == EINVAL || errno == EOPNOTSUPP)) {
- SKIP(free(handle); close(ns_fd);
- return,
- "open_by_handle_at with FD_NSFS_ROOT not supported");
- }
- ASSERT_GE(fd, 0);
- /* Verify we opened the correct namespace */
- ASSERT_EQ(fstat(ns_fd, &st1), 0);
- ASSERT_EQ(fstat(fd, &st2), 0);
- ASSERT_EQ(st1.st_ino, st2.st_ino);
- ASSERT_EQ(st1.st_dev, st2.st_dev);
- close(fd);
- close(ns_fd);
- free(handle);
- }
- TEST(nsfs_cgroup_handle)
- {
- struct file_handle *handle;
- int mount_id;
- int ret;
- int fd;
- int ns_fd;
- struct stat st1, st2;
- /* Drop to unprivileged uid/gid */
- ASSERT_EQ(setresgid(65534, 65534, 65534), 0); /* nogroup */
- ASSERT_EQ(setresuid(65534, 65534, 65534), 0); /* nobody */
- handle = malloc(sizeof(*handle) + MAX_HANDLE_SZ);
- ASSERT_NE(handle, NULL);
- /* Open cgroup namespace file descriptor */
- ns_fd = open("/proc/self/ns/cgroup", O_RDONLY);
- if (ns_fd < 0) {
- SKIP(free(handle); return, "cgroup namespace not available");
- }
- /* Get handle for the namespace */
- handle->handle_bytes = MAX_HANDLE_SZ;
- ret = name_to_handle_at(ns_fd, "", handle, &mount_id, AT_EMPTY_PATH);
- if (ret < 0 && errno == EOPNOTSUPP) {
- SKIP(free(handle); close(ns_fd);
- return, "nsfs doesn't support file handles");
- }
- ASSERT_EQ(ret, 0);
- ASSERT_GT(handle->handle_bytes, 0);
- /* Try to open using FD_NSFS_ROOT */
- fd = open_by_handle_at(FD_NSFS_ROOT, handle, O_RDONLY);
- if (fd < 0 && (errno == EINVAL || errno == EOPNOTSUPP)) {
- SKIP(free(handle); close(ns_fd);
- return,
- "open_by_handle_at with FD_NSFS_ROOT not supported");
- }
- ASSERT_GE(fd, 0);
- /* Verify we opened the correct namespace */
- ASSERT_EQ(fstat(ns_fd, &st1), 0);
- ASSERT_EQ(fstat(fd, &st2), 0);
- ASSERT_EQ(st1.st_ino, st2.st_ino);
- ASSERT_EQ(st1.st_dev, st2.st_dev);
- close(fd);
- close(ns_fd);
- free(handle);
- }
- TEST(nsfs_time_handle)
- {
- struct file_handle *handle;
- int mount_id;
- int ret;
- int fd;
- int ns_fd;
- struct stat st1, st2;
- /* Drop to unprivileged uid/gid */
- ASSERT_EQ(setresgid(65534, 65534, 65534), 0); /* nogroup */
- ASSERT_EQ(setresuid(65534, 65534, 65534), 0); /* nobody */
- handle = malloc(sizeof(*handle) + MAX_HANDLE_SZ);
- ASSERT_NE(handle, NULL);
- /* Open time namespace file descriptor */
- ns_fd = open("/proc/self/ns/time", O_RDONLY);
- if (ns_fd < 0) {
- SKIP(free(handle); return, "time namespace not available");
- }
- /* Get handle for the namespace */
- handle->handle_bytes = MAX_HANDLE_SZ;
- ret = name_to_handle_at(ns_fd, "", handle, &mount_id, AT_EMPTY_PATH);
- if (ret < 0 && errno == EOPNOTSUPP) {
- SKIP(free(handle); close(ns_fd);
- return, "nsfs doesn't support file handles");
- }
- ASSERT_EQ(ret, 0);
- ASSERT_GT(handle->handle_bytes, 0);
- /* Try to open using FD_NSFS_ROOT */
- fd = open_by_handle_at(FD_NSFS_ROOT, handle, O_RDONLY);
- if (fd < 0 && (errno == EINVAL || errno == EOPNOTSUPP)) {
- SKIP(free(handle); close(ns_fd);
- return,
- "open_by_handle_at with FD_NSFS_ROOT not supported");
- }
- ASSERT_GE(fd, 0);
- /* Verify we opened the correct namespace */
- ASSERT_EQ(fstat(ns_fd, &st1), 0);
- ASSERT_EQ(fstat(fd, &st2), 0);
- ASSERT_EQ(st1.st_ino, st2.st_ino);
- ASSERT_EQ(st1.st_dev, st2.st_dev);
- close(fd);
- close(ns_fd);
- free(handle);
- }
- TEST(nsfs_user_net_namespace_isolation)
- {
- struct file_handle *handle;
- int mount_id;
- int ret;
- int fd;
- int ns_fd;
- pid_t pid;
- int status;
- int pipefd[2];
- char result;
- handle = malloc(sizeof(*handle) + MAX_HANDLE_SZ);
- ASSERT_NE(handle, NULL);
- /* Create pipe for communication */
- ASSERT_EQ(pipe(pipefd), 0);
- /* Get handle for current network namespace */
- ns_fd = open("/proc/self/ns/net", O_RDONLY);
- ASSERT_GE(ns_fd, 0);
- handle->handle_bytes = MAX_HANDLE_SZ;
- ret = name_to_handle_at(ns_fd, "", handle, &mount_id, AT_EMPTY_PATH);
- if (ret < 0 && errno == EOPNOTSUPP) {
- SKIP(free(handle); close(ns_fd); close(pipefd[0]);
- close(pipefd[1]);
- return, "nsfs doesn't support file handles");
- }
- ASSERT_EQ(ret, 0);
- close(ns_fd);
- pid = fork();
- ASSERT_GE(pid, 0);
- if (pid == 0) {
- /* Child process */
- close(pipefd[0]);
- /* First create new user namespace to drop privileges */
- ret = unshare(CLONE_NEWUSER);
- if (ret < 0) {
- write(pipefd[1], "U",
- 1); /* Unable to create user namespace */
- close(pipefd[1]);
- exit(0);
- }
- /* Write uid/gid mappings to maintain some capabilities */
- int uid_map_fd = open("/proc/self/uid_map", O_WRONLY);
- int gid_map_fd = open("/proc/self/gid_map", O_WRONLY);
- int setgroups_fd = open("/proc/self/setgroups", O_WRONLY);
- if (uid_map_fd < 0 || gid_map_fd < 0 || setgroups_fd < 0) {
- write(pipefd[1], "M", 1); /* Unable to set mappings */
- close(pipefd[1]);
- exit(0);
- }
- /* Disable setgroups to allow gid mapping */
- write(setgroups_fd, "deny", 4);
- close(setgroups_fd);
- /* Map current uid/gid to root in the new namespace */
- char mapping[64];
- snprintf(mapping, sizeof(mapping), "0 %d 1", getuid());
- write(uid_map_fd, mapping, strlen(mapping));
- close(uid_map_fd);
- snprintf(mapping, sizeof(mapping), "0 %d 1", getgid());
- write(gid_map_fd, mapping, strlen(mapping));
- close(gid_map_fd);
- /* Now create new network namespace */
- ret = unshare(CLONE_NEWNET);
- if (ret < 0) {
- write(pipefd[1], "N",
- 1); /* Unable to create network namespace */
- close(pipefd[1]);
- exit(0);
- }
- /* Try to open parent's network namespace handle from new user+net namespace */
- fd = open_by_handle_at(FD_NSFS_ROOT, handle, O_RDONLY);
- if (fd >= 0) {
- /* Should NOT succeed - we're in a different user namespace */
- write(pipefd[1], "S", 1); /* Unexpected success */
- close(fd);
- } else if (errno == ESTALE) {
- /* Expected: Stale file handle */
- write(pipefd[1], "P", 1);
- } else {
- /* Other error */
- write(pipefd[1], "F", 1);
- }
- close(pipefd[1]);
- exit(0);
- }
- /* Parent process */
- close(pipefd[1]);
- ASSERT_EQ(read(pipefd[0], &result, 1), 1);
- waitpid(pid, &status, 0);
- ASSERT_TRUE(WIFEXITED(status));
- ASSERT_EQ(WEXITSTATUS(status), 0);
- if (result == 'U') {
- SKIP(free(handle); close(pipefd[0]);
- return, "Cannot create new user namespace");
- }
- if (result == 'M') {
- SKIP(free(handle); close(pipefd[0]);
- return, "Cannot set uid/gid mappings");
- }
- if (result == 'N') {
- SKIP(free(handle); close(pipefd[0]);
- return, "Cannot create new network namespace");
- }
- /* Should fail with permission denied since we're in a different user namespace */
- ASSERT_EQ(result, 'P');
- close(pipefd[0]);
- free(handle);
- }
- TEST(nsfs_user_uts_namespace_isolation)
- {
- struct file_handle *handle;
- int mount_id;
- int ret;
- int fd;
- int ns_fd;
- pid_t pid;
- int status;
- int pipefd[2];
- char result;
- handle = malloc(sizeof(*handle) + MAX_HANDLE_SZ);
- ASSERT_NE(handle, NULL);
- /* Create pipe for communication */
- ASSERT_EQ(pipe(pipefd), 0);
- /* Get handle for current UTS namespace */
- ns_fd = open("/proc/self/ns/uts", O_RDONLY);
- ASSERT_GE(ns_fd, 0);
- handle->handle_bytes = MAX_HANDLE_SZ;
- ret = name_to_handle_at(ns_fd, "", handle, &mount_id, AT_EMPTY_PATH);
- if (ret < 0 && errno == EOPNOTSUPP) {
- SKIP(free(handle); close(ns_fd); close(pipefd[0]);
- close(pipefd[1]);
- return, "nsfs doesn't support file handles");
- }
- ASSERT_EQ(ret, 0);
- close(ns_fd);
- pid = fork();
- ASSERT_GE(pid, 0);
- if (pid == 0) {
- /* Child process */
- close(pipefd[0]);
- /* First create new user namespace to drop privileges */
- ret = unshare(CLONE_NEWUSER);
- if (ret < 0) {
- write(pipefd[1], "U",
- 1); /* Unable to create user namespace */
- close(pipefd[1]);
- exit(0);
- }
- /* Write uid/gid mappings to maintain some capabilities */
- int uid_map_fd = open("/proc/self/uid_map", O_WRONLY);
- int gid_map_fd = open("/proc/self/gid_map", O_WRONLY);
- int setgroups_fd = open("/proc/self/setgroups", O_WRONLY);
- if (uid_map_fd < 0 || gid_map_fd < 0 || setgroups_fd < 0) {
- write(pipefd[1], "M", 1); /* Unable to set mappings */
- close(pipefd[1]);
- exit(0);
- }
- /* Disable setgroups to allow gid mapping */
- write(setgroups_fd, "deny", 4);
- close(setgroups_fd);
- /* Map current uid/gid to root in the new namespace */
- char mapping[64];
- snprintf(mapping, sizeof(mapping), "0 %d 1", getuid());
- write(uid_map_fd, mapping, strlen(mapping));
- close(uid_map_fd);
- snprintf(mapping, sizeof(mapping), "0 %d 1", getgid());
- write(gid_map_fd, mapping, strlen(mapping));
- close(gid_map_fd);
- /* Now create new UTS namespace */
- ret = unshare(CLONE_NEWUTS);
- if (ret < 0) {
- write(pipefd[1], "N",
- 1); /* Unable to create UTS namespace */
- close(pipefd[1]);
- exit(0);
- }
- /* Try to open parent's UTS namespace handle from new user+uts namespace */
- fd = open_by_handle_at(FD_NSFS_ROOT, handle, O_RDONLY);
- if (fd >= 0) {
- /* Should NOT succeed - we're in a different user namespace */
- write(pipefd[1], "S", 1); /* Unexpected success */
- close(fd);
- } else if (errno == ESTALE) {
- /* Expected: Stale file handle */
- write(pipefd[1], "P", 1);
- } else {
- /* Other error */
- write(pipefd[1], "F", 1);
- }
- close(pipefd[1]);
- exit(0);
- }
- /* Parent process */
- close(pipefd[1]);
- ASSERT_EQ(read(pipefd[0], &result, 1), 1);
- waitpid(pid, &status, 0);
- ASSERT_TRUE(WIFEXITED(status));
- ASSERT_EQ(WEXITSTATUS(status), 0);
- if (result == 'U') {
- SKIP(free(handle); close(pipefd[0]);
- return, "Cannot create new user namespace");
- }
- if (result == 'M') {
- SKIP(free(handle); close(pipefd[0]);
- return, "Cannot set uid/gid mappings");
- }
- if (result == 'N') {
- SKIP(free(handle); close(pipefd[0]);
- return, "Cannot create new UTS namespace");
- }
- /* Should fail with ESTALE since we're in a different user namespace */
- ASSERT_EQ(result, 'P');
- close(pipefd[0]);
- free(handle);
- }
- TEST(nsfs_user_ipc_namespace_isolation)
- {
- struct file_handle *handle;
- int mount_id;
- int ret;
- int fd;
- int ns_fd;
- pid_t pid;
- int status;
- int pipefd[2];
- char result;
- handle = malloc(sizeof(*handle) + MAX_HANDLE_SZ);
- ASSERT_NE(handle, NULL);
- /* Create pipe for communication */
- ASSERT_EQ(pipe(pipefd), 0);
- /* Get handle for current IPC namespace */
- ns_fd = open("/proc/self/ns/ipc", O_RDONLY);
- ASSERT_GE(ns_fd, 0);
- handle->handle_bytes = MAX_HANDLE_SZ;
- ret = name_to_handle_at(ns_fd, "", handle, &mount_id, AT_EMPTY_PATH);
- if (ret < 0 && errno == EOPNOTSUPP) {
- SKIP(free(handle); close(ns_fd); close(pipefd[0]);
- close(pipefd[1]);
- return, "nsfs doesn't support file handles");
- }
- ASSERT_EQ(ret, 0);
- close(ns_fd);
- pid = fork();
- ASSERT_GE(pid, 0);
- if (pid == 0) {
- /* Child process */
- close(pipefd[0]);
- /* First create new user namespace to drop privileges */
- ret = unshare(CLONE_NEWUSER);
- if (ret < 0) {
- write(pipefd[1], "U",
- 1); /* Unable to create user namespace */
- close(pipefd[1]);
- exit(0);
- }
- /* Write uid/gid mappings to maintain some capabilities */
- int uid_map_fd = open("/proc/self/uid_map", O_WRONLY);
- int gid_map_fd = open("/proc/self/gid_map", O_WRONLY);
- int setgroups_fd = open("/proc/self/setgroups", O_WRONLY);
- if (uid_map_fd < 0 || gid_map_fd < 0 || setgroups_fd < 0) {
- write(pipefd[1], "M", 1); /* Unable to set mappings */
- close(pipefd[1]);
- exit(0);
- }
- /* Disable setgroups to allow gid mapping */
- write(setgroups_fd, "deny", 4);
- close(setgroups_fd);
- /* Map current uid/gid to root in the new namespace */
- char mapping[64];
- snprintf(mapping, sizeof(mapping), "0 %d 1", getuid());
- write(uid_map_fd, mapping, strlen(mapping));
- close(uid_map_fd);
- snprintf(mapping, sizeof(mapping), "0 %d 1", getgid());
- write(gid_map_fd, mapping, strlen(mapping));
- close(gid_map_fd);
- /* Now create new IPC namespace */
- ret = unshare(CLONE_NEWIPC);
- if (ret < 0) {
- write(pipefd[1], "N",
- 1); /* Unable to create IPC namespace */
- close(pipefd[1]);
- exit(0);
- }
- /* Try to open parent's IPC namespace handle from new user+ipc namespace */
- fd = open_by_handle_at(FD_NSFS_ROOT, handle, O_RDONLY);
- if (fd >= 0) {
- /* Should NOT succeed - we're in a different user namespace */
- write(pipefd[1], "S", 1); /* Unexpected success */
- close(fd);
- } else if (errno == ESTALE) {
- /* Expected: Stale file handle */
- write(pipefd[1], "P", 1);
- } else {
- /* Other error */
- write(pipefd[1], "F", 1);
- }
- close(pipefd[1]);
- exit(0);
- }
- /* Parent process */
- close(pipefd[1]);
- ASSERT_EQ(read(pipefd[0], &result, 1), 1);
- waitpid(pid, &status, 0);
- ASSERT_TRUE(WIFEXITED(status));
- ASSERT_EQ(WEXITSTATUS(status), 0);
- if (result == 'U') {
- SKIP(free(handle); close(pipefd[0]);
- return, "Cannot create new user namespace");
- }
- if (result == 'M') {
- SKIP(free(handle); close(pipefd[0]);
- return, "Cannot set uid/gid mappings");
- }
- if (result == 'N') {
- SKIP(free(handle); close(pipefd[0]);
- return, "Cannot create new IPC namespace");
- }
- /* Should fail with ESTALE since we're in a different user namespace */
- ASSERT_EQ(result, 'P');
- close(pipefd[0]);
- free(handle);
- }
- TEST(nsfs_user_mnt_namespace_isolation)
- {
- struct file_handle *handle;
- int mount_id;
- int ret;
- int fd;
- int ns_fd;
- pid_t pid;
- int status;
- int pipefd[2];
- char result;
- handle = malloc(sizeof(*handle) + MAX_HANDLE_SZ);
- ASSERT_NE(handle, NULL);
- /* Create pipe for communication */
- ASSERT_EQ(pipe(pipefd), 0);
- /* Get handle for current mount namespace */
- ns_fd = open("/proc/self/ns/mnt", O_RDONLY);
- ASSERT_GE(ns_fd, 0);
- handle->handle_bytes = MAX_HANDLE_SZ;
- ret = name_to_handle_at(ns_fd, "", handle, &mount_id, AT_EMPTY_PATH);
- if (ret < 0 && errno == EOPNOTSUPP) {
- SKIP(free(handle); close(ns_fd); close(pipefd[0]);
- close(pipefd[1]);
- return, "nsfs doesn't support file handles");
- }
- ASSERT_EQ(ret, 0);
- close(ns_fd);
- pid = fork();
- ASSERT_GE(pid, 0);
- if (pid == 0) {
- /* Child process */
- close(pipefd[0]);
- /* First create new user namespace to drop privileges */
- ret = unshare(CLONE_NEWUSER);
- if (ret < 0) {
- write(pipefd[1], "U",
- 1); /* Unable to create user namespace */
- close(pipefd[1]);
- exit(0);
- }
- /* Write uid/gid mappings to maintain some capabilities */
- int uid_map_fd = open("/proc/self/uid_map", O_WRONLY);
- int gid_map_fd = open("/proc/self/gid_map", O_WRONLY);
- int setgroups_fd = open("/proc/self/setgroups", O_WRONLY);
- if (uid_map_fd < 0 || gid_map_fd < 0 || setgroups_fd < 0) {
- write(pipefd[1], "M", 1); /* Unable to set mappings */
- close(pipefd[1]);
- exit(0);
- }
- /* Disable setgroups to allow gid mapping */
- write(setgroups_fd, "deny", 4);
- close(setgroups_fd);
- /* Map current uid/gid to root in the new namespace */
- char mapping[64];
- snprintf(mapping, sizeof(mapping), "0 %d 1", getuid());
- write(uid_map_fd, mapping, strlen(mapping));
- close(uid_map_fd);
- snprintf(mapping, sizeof(mapping), "0 %d 1", getgid());
- write(gid_map_fd, mapping, strlen(mapping));
- close(gid_map_fd);
- /* Now create new mount namespace */
- ret = unshare(CLONE_NEWNS);
- if (ret < 0) {
- write(pipefd[1], "N",
- 1); /* Unable to create mount namespace */
- close(pipefd[1]);
- exit(0);
- }
- /* Try to open parent's mount namespace handle from new user+mnt namespace */
- fd = open_by_handle_at(FD_NSFS_ROOT, handle, O_RDONLY);
- if (fd >= 0) {
- /* Should NOT succeed - we're in a different user namespace */
- write(pipefd[1], "S", 1); /* Unexpected success */
- close(fd);
- } else if (errno == ESTALE) {
- /* Expected: Stale file handle */
- write(pipefd[1], "P", 1);
- } else {
- /* Other error */
- write(pipefd[1], "F", 1);
- }
- close(pipefd[1]);
- exit(0);
- }
- /* Parent process */
- close(pipefd[1]);
- ASSERT_EQ(read(pipefd[0], &result, 1), 1);
- waitpid(pid, &status, 0);
- ASSERT_TRUE(WIFEXITED(status));
- ASSERT_EQ(WEXITSTATUS(status), 0);
- if (result == 'U') {
- SKIP(free(handle); close(pipefd[0]);
- return, "Cannot create new user namespace");
- }
- if (result == 'M') {
- SKIP(free(handle); close(pipefd[0]);
- return, "Cannot set uid/gid mappings");
- }
- if (result == 'N') {
- SKIP(free(handle); close(pipefd[0]);
- return, "Cannot create new mount namespace");
- }
- /* Should fail with ESTALE since we're in a different user namespace */
- ASSERT_EQ(result, 'P');
- close(pipefd[0]);
- free(handle);
- }
- TEST(nsfs_user_cgroup_namespace_isolation)
- {
- struct file_handle *handle;
- int mount_id;
- int ret;
- int fd;
- int ns_fd;
- pid_t pid;
- int status;
- int pipefd[2];
- char result;
- handle = malloc(sizeof(*handle) + MAX_HANDLE_SZ);
- ASSERT_NE(handle, NULL);
- /* Create pipe for communication */
- ASSERT_EQ(pipe(pipefd), 0);
- /* Get handle for current cgroup namespace */
- ns_fd = open("/proc/self/ns/cgroup", O_RDONLY);
- if (ns_fd < 0) {
- SKIP(free(handle); close(pipefd[0]); close(pipefd[1]);
- return, "cgroup namespace not available");
- }
- handle->handle_bytes = MAX_HANDLE_SZ;
- ret = name_to_handle_at(ns_fd, "", handle, &mount_id, AT_EMPTY_PATH);
- if (ret < 0 && errno == EOPNOTSUPP) {
- SKIP(free(handle); close(ns_fd); close(pipefd[0]);
- close(pipefd[1]);
- return, "nsfs doesn't support file handles");
- }
- ASSERT_EQ(ret, 0);
- close(ns_fd);
- pid = fork();
- ASSERT_GE(pid, 0);
- if (pid == 0) {
- /* Child process */
- close(pipefd[0]);
- /* First create new user namespace to drop privileges */
- ret = unshare(CLONE_NEWUSER);
- if (ret < 0) {
- write(pipefd[1], "U",
- 1); /* Unable to create user namespace */
- close(pipefd[1]);
- exit(0);
- }
- /* Write uid/gid mappings to maintain some capabilities */
- int uid_map_fd = open("/proc/self/uid_map", O_WRONLY);
- int gid_map_fd = open("/proc/self/gid_map", O_WRONLY);
- int setgroups_fd = open("/proc/self/setgroups", O_WRONLY);
- if (uid_map_fd < 0 || gid_map_fd < 0 || setgroups_fd < 0) {
- write(pipefd[1], "M", 1); /* Unable to set mappings */
- close(pipefd[1]);
- exit(0);
- }
- /* Disable setgroups to allow gid mapping */
- write(setgroups_fd, "deny", 4);
- close(setgroups_fd);
- /* Map current uid/gid to root in the new namespace */
- char mapping[64];
- snprintf(mapping, sizeof(mapping), "0 %d 1", getuid());
- write(uid_map_fd, mapping, strlen(mapping));
- close(uid_map_fd);
- snprintf(mapping, sizeof(mapping), "0 %d 1", getgid());
- write(gid_map_fd, mapping, strlen(mapping));
- close(gid_map_fd);
- /* Now create new cgroup namespace */
- ret = unshare(CLONE_NEWCGROUP);
- if (ret < 0) {
- write(pipefd[1], "N",
- 1); /* Unable to create cgroup namespace */
- close(pipefd[1]);
- exit(0);
- }
- /* Try to open parent's cgroup namespace handle from new user+cgroup namespace */
- fd = open_by_handle_at(FD_NSFS_ROOT, handle, O_RDONLY);
- if (fd >= 0) {
- /* Should NOT succeed - we're in a different user namespace */
- write(pipefd[1], "S", 1); /* Unexpected success */
- close(fd);
- } else if (errno == ESTALE) {
- /* Expected: Stale file handle */
- write(pipefd[1], "P", 1);
- } else {
- /* Other error */
- write(pipefd[1], "F", 1);
- }
- close(pipefd[1]);
- exit(0);
- }
- /* Parent process */
- close(pipefd[1]);
- ASSERT_EQ(read(pipefd[0], &result, 1), 1);
- waitpid(pid, &status, 0);
- ASSERT_TRUE(WIFEXITED(status));
- ASSERT_EQ(WEXITSTATUS(status), 0);
- if (result == 'U') {
- SKIP(free(handle); close(pipefd[0]);
- return, "Cannot create new user namespace");
- }
- if (result == 'M') {
- SKIP(free(handle); close(pipefd[0]);
- return, "Cannot set uid/gid mappings");
- }
- if (result == 'N') {
- SKIP(free(handle); close(pipefd[0]);
- return, "Cannot create new cgroup namespace");
- }
- /* Should fail with ESTALE since we're in a different user namespace */
- ASSERT_EQ(result, 'P');
- close(pipefd[0]);
- free(handle);
- }
- TEST(nsfs_user_pid_namespace_isolation)
- {
- struct file_handle *handle;
- int mount_id;
- int ret;
- int fd;
- int ns_fd;
- pid_t pid;
- int status;
- int pipefd[2];
- char result;
- handle = malloc(sizeof(*handle) + MAX_HANDLE_SZ);
- ASSERT_NE(handle, NULL);
- /* Create pipe for communication */
- ASSERT_EQ(pipe(pipefd), 0);
- /* Get handle for current PID namespace */
- ns_fd = open("/proc/self/ns/pid", O_RDONLY);
- ASSERT_GE(ns_fd, 0);
- handle->handle_bytes = MAX_HANDLE_SZ;
- ret = name_to_handle_at(ns_fd, "", handle, &mount_id, AT_EMPTY_PATH);
- if (ret < 0 && errno == EOPNOTSUPP) {
- SKIP(free(handle); close(ns_fd); close(pipefd[0]);
- close(pipefd[1]);
- return, "nsfs doesn't support file handles");
- }
- ASSERT_EQ(ret, 0);
- close(ns_fd);
- pid = fork();
- ASSERT_GE(pid, 0);
- if (pid == 0) {
- /* Child process */
- close(pipefd[0]);
- /* First create new user namespace to drop privileges */
- ret = unshare(CLONE_NEWUSER);
- if (ret < 0) {
- write(pipefd[1], "U",
- 1); /* Unable to create user namespace */
- close(pipefd[1]);
- exit(0);
- }
- /* Write uid/gid mappings to maintain some capabilities */
- int uid_map_fd = open("/proc/self/uid_map", O_WRONLY);
- int gid_map_fd = open("/proc/self/gid_map", O_WRONLY);
- int setgroups_fd = open("/proc/self/setgroups", O_WRONLY);
- if (uid_map_fd < 0 || gid_map_fd < 0 || setgroups_fd < 0) {
- write(pipefd[1], "M", 1); /* Unable to set mappings */
- close(pipefd[1]);
- exit(0);
- }
- /* Disable setgroups to allow gid mapping */
- write(setgroups_fd, "deny", 4);
- close(setgroups_fd);
- /* Map current uid/gid to root in the new namespace */
- char mapping[64];
- snprintf(mapping, sizeof(mapping), "0 %d 1", getuid());
- write(uid_map_fd, mapping, strlen(mapping));
- close(uid_map_fd);
- snprintf(mapping, sizeof(mapping), "0 %d 1", getgid());
- write(gid_map_fd, mapping, strlen(mapping));
- close(gid_map_fd);
- /* Now create new PID namespace - requires fork to take effect */
- ret = unshare(CLONE_NEWPID);
- if (ret < 0) {
- write(pipefd[1], "N",
- 1); /* Unable to create PID namespace */
- close(pipefd[1]);
- exit(0);
- }
- /* Fork again for PID namespace to take effect */
- pid_t child_pid = fork();
- if (child_pid < 0) {
- write(pipefd[1], "N",
- 1); /* Unable to fork in PID namespace */
- close(pipefd[1]);
- exit(0);
- }
- if (child_pid == 0) {
- /* Grandchild in new PID namespace */
- /* Try to open parent's PID namespace handle from new user+pid namespace */
- fd = open_by_handle_at(FD_NSFS_ROOT, handle, O_RDONLY);
- if (fd >= 0) {
- /* Should NOT succeed - we're in a different user namespace */
- write(pipefd[1], "S",
- 1); /* Unexpected success */
- close(fd);
- } else if (errno == ESTALE) {
- /* Expected: Stale file handle */
- write(pipefd[1], "P", 1);
- } else {
- /* Other error */
- write(pipefd[1], "F", 1);
- }
- close(pipefd[1]);
- exit(0);
- }
- /* Wait for grandchild */
- waitpid(child_pid, NULL, 0);
- exit(0);
- }
- /* Parent process */
- close(pipefd[1]);
- ASSERT_EQ(read(pipefd[0], &result, 1), 1);
- waitpid(pid, &status, 0);
- ASSERT_TRUE(WIFEXITED(status));
- ASSERT_EQ(WEXITSTATUS(status), 0);
- if (result == 'U') {
- SKIP(free(handle); close(pipefd[0]);
- return, "Cannot create new user namespace");
- }
- if (result == 'M') {
- SKIP(free(handle); close(pipefd[0]);
- return, "Cannot set uid/gid mappings");
- }
- if (result == 'N') {
- SKIP(free(handle); close(pipefd[0]);
- return, "Cannot create new PID namespace");
- }
- /* Should fail with ESTALE since we're in a different user namespace */
- ASSERT_EQ(result, 'P');
- close(pipefd[0]);
- free(handle);
- }
- TEST(nsfs_user_time_namespace_isolation)
- {
- struct file_handle *handle;
- int mount_id;
- int ret;
- int fd;
- int ns_fd;
- pid_t pid;
- int status;
- int pipefd[2];
- char result;
- handle = malloc(sizeof(*handle) + MAX_HANDLE_SZ);
- ASSERT_NE(handle, NULL);
- /* Create pipe for communication */
- ASSERT_EQ(pipe(pipefd), 0);
- /* Get handle for current time namespace */
- ns_fd = open("/proc/self/ns/time", O_RDONLY);
- if (ns_fd < 0) {
- SKIP(free(handle); close(pipefd[0]); close(pipefd[1]);
- return, "time namespace not available");
- }
- handle->handle_bytes = MAX_HANDLE_SZ;
- ret = name_to_handle_at(ns_fd, "", handle, &mount_id, AT_EMPTY_PATH);
- if (ret < 0 && errno == EOPNOTSUPP) {
- SKIP(free(handle); close(ns_fd); close(pipefd[0]);
- close(pipefd[1]);
- return, "nsfs doesn't support file handles");
- }
- ASSERT_EQ(ret, 0);
- close(ns_fd);
- pid = fork();
- ASSERT_GE(pid, 0);
- if (pid == 0) {
- /* Child process */
- close(pipefd[0]);
- /* First create new user namespace to drop privileges */
- ret = unshare(CLONE_NEWUSER);
- if (ret < 0) {
- write(pipefd[1], "U",
- 1); /* Unable to create user namespace */
- close(pipefd[1]);
- exit(0);
- }
- /* Write uid/gid mappings to maintain some capabilities */
- int uid_map_fd = open("/proc/self/uid_map", O_WRONLY);
- int gid_map_fd = open("/proc/self/gid_map", O_WRONLY);
- int setgroups_fd = open("/proc/self/setgroups", O_WRONLY);
- if (uid_map_fd < 0 || gid_map_fd < 0 || setgroups_fd < 0) {
- write(pipefd[1], "M", 1); /* Unable to set mappings */
- close(pipefd[1]);
- exit(0);
- }
- /* Disable setgroups to allow gid mapping */
- write(setgroups_fd, "deny", 4);
- close(setgroups_fd);
- /* Map current uid/gid to root in the new namespace */
- char mapping[64];
- snprintf(mapping, sizeof(mapping), "0 %d 1", getuid());
- write(uid_map_fd, mapping, strlen(mapping));
- close(uid_map_fd);
- snprintf(mapping, sizeof(mapping), "0 %d 1", getgid());
- write(gid_map_fd, mapping, strlen(mapping));
- close(gid_map_fd);
- /* Now create new time namespace - requires fork to take effect */
- ret = unshare(CLONE_NEWTIME);
- if (ret < 0) {
- write(pipefd[1], "N",
- 1); /* Unable to create time namespace */
- close(pipefd[1]);
- exit(0);
- }
- /* Fork again for time namespace to take effect */
- pid_t child_pid = fork();
- if (child_pid < 0) {
- write(pipefd[1], "N",
- 1); /* Unable to fork in time namespace */
- close(pipefd[1]);
- exit(0);
- }
- if (child_pid == 0) {
- /* Grandchild in new time namespace */
- /* Try to open parent's time namespace handle from new user+time namespace */
- fd = open_by_handle_at(FD_NSFS_ROOT, handle, O_RDONLY);
- if (fd >= 0) {
- /* Should NOT succeed - we're in a different user namespace */
- write(pipefd[1], "S",
- 1); /* Unexpected success */
- close(fd);
- } else if (errno == ESTALE) {
- /* Expected: Stale file handle */
- write(pipefd[1], "P", 1);
- } else {
- /* Other error */
- write(pipefd[1], "F", 1);
- }
- close(pipefd[1]);
- exit(0);
- }
- /* Wait for grandchild */
- waitpid(child_pid, NULL, 0);
- exit(0);
- }
- /* Parent process */
- close(pipefd[1]);
- ASSERT_EQ(read(pipefd[0], &result, 1), 1);
- waitpid(pid, &status, 0);
- ASSERT_TRUE(WIFEXITED(status));
- ASSERT_EQ(WEXITSTATUS(status), 0);
- if (result == 'U') {
- SKIP(free(handle); close(pipefd[0]);
- return, "Cannot create new user namespace");
- }
- if (result == 'M') {
- SKIP(free(handle); close(pipefd[0]);
- return, "Cannot set uid/gid mappings");
- }
- if (result == 'N') {
- SKIP(free(handle); close(pipefd[0]);
- return, "Cannot create new time namespace");
- }
- /* Should fail with ESTALE since we're in a different user namespace */
- ASSERT_EQ(result, 'P');
- close(pipefd[0]);
- free(handle);
- }
- TEST(nsfs_open_flags)
- {
- struct file_handle *handle;
- int mount_id;
- int ret;
- int fd;
- int ns_fd;
- handle = malloc(sizeof(*handle) + MAX_HANDLE_SZ);
- ASSERT_NE(handle, NULL);
- /* Open a namespace file descriptor */
- ns_fd = open("/proc/self/ns/net", O_RDONLY);
- ASSERT_GE(ns_fd, 0);
- /* Get handle for the namespace */
- handle->handle_bytes = MAX_HANDLE_SZ;
- ret = name_to_handle_at(ns_fd, "", handle, &mount_id, AT_EMPTY_PATH);
- if (ret < 0 && errno == EOPNOTSUPP) {
- SKIP(free(handle); close(ns_fd);
- return, "nsfs doesn't support file handles");
- }
- ASSERT_EQ(ret, 0);
- ASSERT_GT(handle->handle_bytes, 0);
- /* Test invalid flags that should fail */
- fd = open_by_handle_at(FD_NSFS_ROOT, handle, O_WRONLY);
- ASSERT_LT(fd, 0);
- ASSERT_EQ(errno, EPERM);
- fd = open_by_handle_at(FD_NSFS_ROOT, handle, O_RDWR);
- ASSERT_LT(fd, 0);
- ASSERT_EQ(errno, EPERM);
- fd = open_by_handle_at(FD_NSFS_ROOT, handle, O_TRUNC);
- ASSERT_LT(fd, 0);
- ASSERT_EQ(errno, EPERM);
- fd = open_by_handle_at(FD_NSFS_ROOT, handle, O_DIRECT);
- ASSERT_LT(fd, 0);
- ASSERT_EQ(errno, EINVAL);
- fd = open_by_handle_at(FD_NSFS_ROOT, handle, O_TMPFILE);
- ASSERT_LT(fd, 0);
- ASSERT_EQ(errno, EINVAL);
- fd = open_by_handle_at(FD_NSFS_ROOT, handle, O_DIRECTORY);
- ASSERT_LT(fd, 0);
- ASSERT_EQ(errno, ENOTDIR);
- close(ns_fd);
- free(handle);
- }
- TEST_HARNESS_MAIN
|