Path: blob/master/tools/testing/selftests/filesystems/utils.c
26298 views
// SPDX-License-Identifier: GPL-2.01#ifndef _GNU_SOURCE2#define _GNU_SOURCE3#endif4#include <fcntl.h>5#include <sys/types.h>6#include <dirent.h>7#include <grp.h>8#include <linux/limits.h>9#include <sched.h>10#include <stdio.h>11#include <stdlib.h>12#include <sys/eventfd.h>13#include <sys/fsuid.h>14#include <sys/prctl.h>15#include <sys/socket.h>16#include <sys/stat.h>17#include <sys/types.h>18#include <sys/wait.h>19#include <sys/xattr.h>20#include <sys/mount.h>2122#include "../kselftest.h"23#include "wrappers.h"24#include "utils.h"2526#define MAX_USERNS_LEVEL 322728#define syserror(format, ...) \29({ \30fprintf(stderr, "%m - " format "\n", ##__VA_ARGS__); \31(-errno); \32})3334#define syserror_set(__ret__, format, ...) \35({ \36typeof(__ret__) __internal_ret__ = (__ret__); \37errno = labs(__ret__); \38fprintf(stderr, "%m - " format "\n", ##__VA_ARGS__); \39__internal_ret__; \40})4142#define STRLITERALLEN(x) (sizeof(""x"") - 1)4344#define INTTYPE_TO_STRLEN(type) \45(2 + (sizeof(type) <= 1 \46? 3 \47: sizeof(type) <= 2 \48? 5 \49: sizeof(type) <= 4 \50? 10 \51: sizeof(type) <= 8 ? 20 : sizeof(int[-2 * (sizeof(type) > 8)])))5253#define list_for_each(__iterator, __list) \54for (__iterator = (__list)->next; __iterator != __list; __iterator = __iterator->next)5556typedef enum idmap_type_t {57ID_TYPE_UID,58ID_TYPE_GID59} idmap_type_t;6061struct id_map {62idmap_type_t map_type;63__u32 nsid;64__u32 hostid;65__u32 range;66};6768struct list {69void *elem;70struct list *next;71struct list *prev;72};7374struct userns_hierarchy {75int fd_userns;76int fd_event;77unsigned int level;78struct list id_map;79};8081static inline void list_init(struct list *list)82{83list->elem = NULL;84list->next = list->prev = list;85}8687static inline int list_empty(const struct list *list)88{89return list == list->next;90}9192static inline void __list_add(struct list *new, struct list *prev, struct list *next)93{94next->prev = new;95new->next = next;96new->prev = prev;97prev->next = new;98}99100static inline void list_add_tail(struct list *head, struct list *list)101{102__list_add(list, head->prev, head);103}104105static inline void list_del(struct list *list)106{107struct list *next, *prev;108109next = list->next;110prev = list->prev;111next->prev = prev;112prev->next = next;113}114115static ssize_t read_nointr(int fd, void *buf, size_t count)116{117ssize_t ret;118119do {120ret = read(fd, buf, count);121} while (ret < 0 && errno == EINTR);122123return ret;124}125126static ssize_t write_nointr(int fd, const void *buf, size_t count)127{128ssize_t ret;129130do {131ret = write(fd, buf, count);132} while (ret < 0 && errno == EINTR);133134return ret;135}136137#define __STACK_SIZE (8 * 1024 * 1024)138static pid_t do_clone(int (*fn)(void *), void *arg, int flags)139{140void *stack;141142stack = malloc(__STACK_SIZE);143if (!stack)144return -ENOMEM;145146#ifdef __ia64__147return __clone2(fn, stack, __STACK_SIZE, flags | SIGCHLD, arg, NULL);148#else149return clone(fn, stack + __STACK_SIZE, flags | SIGCHLD, arg, NULL);150#endif151}152153static int get_userns_fd_cb(void *data)154{155for (;;)156pause();157_exit(0);158}159160static int wait_for_pid(pid_t pid)161{162int status, ret;163164again:165ret = waitpid(pid, &status, 0);166if (ret == -1) {167if (errno == EINTR)168goto again;169170return -1;171}172173if (!WIFEXITED(status))174return -1;175176return WEXITSTATUS(status);177}178179static int write_id_mapping(idmap_type_t map_type, pid_t pid, const char *buf, size_t buf_size)180{181int fd = -EBADF, setgroups_fd = -EBADF;182int fret = -1;183int ret;184char path[STRLITERALLEN("/proc/") + INTTYPE_TO_STRLEN(pid_t) +185STRLITERALLEN("/setgroups") + 1];186187if (geteuid() != 0 && map_type == ID_TYPE_GID) {188ret = snprintf(path, sizeof(path), "/proc/%d/setgroups", pid);189if (ret < 0 || ret >= sizeof(path))190goto out;191192setgroups_fd = open(path, O_WRONLY | O_CLOEXEC);193if (setgroups_fd < 0 && errno != ENOENT) {194syserror("Failed to open \"%s\"", path);195goto out;196}197198if (setgroups_fd >= 0) {199ret = write_nointr(setgroups_fd, "deny\n", STRLITERALLEN("deny\n"));200if (ret != STRLITERALLEN("deny\n")) {201syserror("Failed to write \"deny\" to \"/proc/%d/setgroups\"", pid);202goto out;203}204}205}206207ret = snprintf(path, sizeof(path), "/proc/%d/%cid_map", pid, map_type == ID_TYPE_UID ? 'u' : 'g');208if (ret < 0 || ret >= sizeof(path))209goto out;210211fd = open(path, O_WRONLY | O_CLOEXEC);212if (fd < 0) {213syserror("Failed to open \"%s\"", path);214goto out;215}216217ret = write_nointr(fd, buf, buf_size);218if (ret != buf_size) {219syserror("Failed to write %cid mapping to \"%s\"",220map_type == ID_TYPE_UID ? 'u' : 'g', path);221goto out;222}223224fret = 0;225out:226close(fd);227close(setgroups_fd);228229return fret;230}231232static int map_ids_from_idmap(struct list *idmap, pid_t pid)233{234int fill, left;235char mapbuf[4096] = {};236bool had_entry = false;237idmap_type_t map_type, u_or_g;238239if (list_empty(idmap))240return 0;241242for (map_type = ID_TYPE_UID, u_or_g = 'u';243map_type <= ID_TYPE_GID; map_type++, u_or_g = 'g') {244char *pos = mapbuf;245int ret;246struct list *iterator;247248249list_for_each(iterator, idmap) {250struct id_map *map = iterator->elem;251if (map->map_type != map_type)252continue;253254had_entry = true;255256left = 4096 - (pos - mapbuf);257fill = snprintf(pos, left, "%u %u %u\n", map->nsid, map->hostid, map->range);258/*259* The kernel only takes <= 4k for writes to260* /proc/<pid>/{g,u}id_map261*/262if (fill <= 0 || fill >= left)263return syserror_set(-E2BIG, "Too many %cid mappings defined", u_or_g);264265pos += fill;266}267if (!had_entry)268continue;269270ret = write_id_mapping(map_type, pid, mapbuf, pos - mapbuf);271if (ret < 0)272return syserror("Failed to write mapping: %s", mapbuf);273274memset(mapbuf, 0, sizeof(mapbuf));275}276277return 0;278}279280static int get_userns_fd_from_idmap(struct list *idmap)281{282int ret;283pid_t pid;284char path_ns[STRLITERALLEN("/proc/") + INTTYPE_TO_STRLEN(pid_t) +285STRLITERALLEN("/ns/user") + 1];286287pid = do_clone(get_userns_fd_cb, NULL, CLONE_NEWUSER | CLONE_NEWNS);288if (pid < 0)289return -errno;290291ret = map_ids_from_idmap(idmap, pid);292if (ret < 0)293return ret;294295ret = snprintf(path_ns, sizeof(path_ns), "/proc/%d/ns/user", pid);296if (ret < 0 || (size_t)ret >= sizeof(path_ns))297ret = -EIO;298else299ret = open(path_ns, O_RDONLY | O_CLOEXEC | O_NOCTTY);300301(void)kill(pid, SIGKILL);302(void)wait_for_pid(pid);303return ret;304}305306int get_userns_fd(unsigned long nsid, unsigned long hostid, unsigned long range)307{308struct list head, uid_mapl, gid_mapl;309struct id_map uid_map = {310.map_type = ID_TYPE_UID,311.nsid = nsid,312.hostid = hostid,313.range = range,314};315struct id_map gid_map = {316.map_type = ID_TYPE_GID,317.nsid = nsid,318.hostid = hostid,319.range = range,320};321322list_init(&head);323uid_mapl.elem = &uid_map;324gid_mapl.elem = &gid_map;325list_add_tail(&head, &uid_mapl);326list_add_tail(&head, &gid_mapl);327328return get_userns_fd_from_idmap(&head);329}330331bool switch_ids(uid_t uid, gid_t gid)332{333if (setgroups(0, NULL))334return syserror("failure: setgroups");335336if (setresgid(gid, gid, gid))337return syserror("failure: setresgid");338339if (setresuid(uid, uid, uid))340return syserror("failure: setresuid");341342/* Ensure we can access proc files from processes we can ptrace. */343if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0))344return syserror("failure: make dumpable");345346return true;347}348349static int create_userns_hierarchy(struct userns_hierarchy *h);350351static int userns_fd_cb(void *data)352{353struct userns_hierarchy *h = data;354char c;355int ret;356357ret = read_nointr(h->fd_event, &c, 1);358if (ret < 0)359return syserror("failure: read from socketpair");360361/* Only switch ids if someone actually wrote a mapping for us. */362if (c == '1') {363if (!switch_ids(0, 0))364return syserror("failure: switch ids to 0");365}366367ret = write_nointr(h->fd_event, "1", 1);368if (ret < 0)369return syserror("failure: write to socketpair");370371ret = create_userns_hierarchy(++h);372if (ret < 0)373return syserror("failure: userns level %d", h->level);374375return 0;376}377378static int create_userns_hierarchy(struct userns_hierarchy *h)379{380int fret = -1;381char c;382int fd_socket[2];383int fd_userns = -EBADF, ret = -1;384ssize_t bytes;385pid_t pid;386char path[256];387388if (h->level == MAX_USERNS_LEVEL)389return 0;390391ret = socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, fd_socket);392if (ret < 0)393return syserror("failure: create socketpair");394395/* Note the CLONE_FILES | CLONE_VM when mucking with fds and memory. */396h->fd_event = fd_socket[1];397pid = do_clone(userns_fd_cb, h, CLONE_NEWUSER | CLONE_FILES | CLONE_VM);398if (pid < 0) {399syserror("failure: userns level %d", h->level);400goto out_close;401}402403ret = map_ids_from_idmap(&h->id_map, pid);404if (ret < 0) {405kill(pid, SIGKILL);406syserror("failure: writing id mapping for userns level %d for %d", h->level, pid);407goto out_wait;408}409410if (!list_empty(&h->id_map))411bytes = write_nointr(fd_socket[0], "1", 1); /* Inform the child we wrote a mapping. */412else413bytes = write_nointr(fd_socket[0], "0", 1); /* Inform the child we didn't write a mapping. */414if (bytes < 0) {415kill(pid, SIGKILL);416syserror("failure: write to socketpair");417goto out_wait;418}419420/* Wait for child to set*id() and become dumpable. */421bytes = read_nointr(fd_socket[0], &c, 1);422if (bytes < 0) {423kill(pid, SIGKILL);424syserror("failure: read from socketpair");425goto out_wait;426}427428snprintf(path, sizeof(path), "/proc/%d/ns/user", pid);429fd_userns = open(path, O_RDONLY | O_CLOEXEC);430if (fd_userns < 0) {431kill(pid, SIGKILL);432syserror("failure: open userns level %d for %d", h->level, pid);433goto out_wait;434}435436fret = 0;437438out_wait:439if (!wait_for_pid(pid) && !fret) {440h->fd_userns = fd_userns;441fd_userns = -EBADF;442}443444out_close:445if (fd_userns >= 0)446close(fd_userns);447close(fd_socket[0]);448close(fd_socket[1]);449return fret;450}451452static int write_file(const char *path, const char *val)453{454int fd = open(path, O_WRONLY);455size_t len = strlen(val);456int ret;457458if (fd == -1) {459ksft_print_msg("opening %s for write: %s\n", path, strerror(errno));460return -1;461}462463ret = write(fd, val, len);464if (ret == -1) {465ksft_print_msg("writing to %s: %s\n", path, strerror(errno));466return -1;467}468if (ret != len) {469ksft_print_msg("short write to %s\n", path);470return -1;471}472473ret = close(fd);474if (ret == -1) {475ksft_print_msg("closing %s\n", path);476return -1;477}478479return 0;480}481482int setup_userns(void)483{484int ret;485char buf[32];486uid_t uid = getuid();487gid_t gid = getgid();488489ret = unshare(CLONE_NEWNS|CLONE_NEWUSER|CLONE_NEWPID);490if (ret) {491ksft_exit_fail_msg("unsharing mountns and userns: %s\n",492strerror(errno));493return ret;494}495496sprintf(buf, "0 %d 1", uid);497ret = write_file("/proc/self/uid_map", buf);498if (ret)499return ret;500ret = write_file("/proc/self/setgroups", "deny");501if (ret)502return ret;503sprintf(buf, "0 %d 1", gid);504ret = write_file("/proc/self/gid_map", buf);505if (ret)506return ret;507508ret = mount("", "/", NULL, MS_REC|MS_PRIVATE, NULL);509if (ret) {510ksft_print_msg("making mount tree private: %s\n", strerror(errno));511return ret;512}513514return 0;515}516517/* caps_down - lower all effective caps */518int caps_down(void)519{520bool fret = false;521cap_t caps = NULL;522int ret = -1;523524caps = cap_get_proc();525if (!caps)526goto out;527528ret = cap_clear_flag(caps, CAP_EFFECTIVE);529if (ret)530goto out;531532ret = cap_set_proc(caps);533if (ret)534goto out;535536fret = true;537538out:539cap_free(caps);540return fret;541}542543/* cap_down - lower an effective cap */544int cap_down(cap_value_t down)545{546bool fret = false;547cap_t caps = NULL;548cap_value_t cap = down;549int ret = -1;550551caps = cap_get_proc();552if (!caps)553goto out;554555ret = cap_set_flag(caps, CAP_EFFECTIVE, 1, &cap, 0);556if (ret)557goto out;558559ret = cap_set_proc(caps);560if (ret)561goto out;562563fret = true;564565out:566cap_free(caps);567return fret;568}569570uint64_t get_unique_mnt_id(const char *path)571{572struct statx sx;573int ret;574575ret = statx(AT_FDCWD, path, 0, STATX_MNT_ID_UNIQUE, &sx);576if (ret == -1) {577ksft_print_msg("retrieving unique mount ID for %s: %s\n", path,578strerror(errno));579return 0;580}581582if (!(sx.stx_mask & STATX_MNT_ID_UNIQUE)) {583ksft_print_msg("no unique mount ID available for %s\n", path);584return 0;585}586587return sx.stx_mnt_id;588}589590591