| 1 | #define _GNU_SOURCE |
| 2 | #include <dlfcn.h> |
| 3 | #include <errno.h> |
| 4 | #include <fcntl.h> |
| 5 | #include <grp.h> |
| 6 | #include <pwd.h> |
| 7 | #include <unistd.h> |
| 8 | #include <stdarg.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | int open64(const char *path, int flags, ...) { |
| 13 | int (*original)(const char *, int, ...) = dlsym(RTLD_NEXT, "open64"); |
| 14 | mode_t mode = 0; |
| 15 | if (flags & O_CREAT) { |
| 16 | va_list args; |
| 17 | va_start(args, flags); |
| 18 | mode = va_arg(args, unsigned int); |
| 19 | va_end(args); |
| 20 | } |
| 21 | if (!strcmp(path, "/etc/passwd")) { |
| 22 | path = getenv("ZIPELO_GROUP_PASSWD"); |
| 23 | } else if (!strcmp(path, "/etc/nsswitch.conf")) { |
| 24 | path = getenv("ZIPELO_GROUP_NSS"); |
| 25 | } |
| 26 | if (!path) { |
| 27 | errno = EACCES; |
| 28 | return -1; |
| 29 | } |
| 30 | return original(path, flags, mode); |
| 31 | } |
| 32 | |
| 33 | int getgrgid_r(gid_t gid, struct group *group, char *buffer, size_t length, |
| 34 | struct group **result) { |
| 35 | (void)buffer; |
| 36 | (void)length; |
| 37 | const char *phase = getenv("ZIPELO_GROUP_CASE"); |
| 38 | *result = NULL; |
| 39 | if (!phase || !strcmp(phase, "group-error")) { |
| 40 | return EIO; |
| 41 | } |
| 42 | if (!strcmp(phase, "group-missing")) { |
| 43 | return 0; |
| 44 | } |
| 45 | static char *empty[] = {NULL}; |
| 46 | static char *shared[] = {"other", NULL}; |
| 47 | *group = (struct group){ |
| 48 | .gr_name = "fixture", |
| 49 | .gr_passwd = "x", |
| 50 | .gr_gid = !strcmp(phase, "group-wrong-gid") ? gid + 1 : gid, |
| 51 | .gr_mem = !strcmp(phase, "group-null-members") ? NULL : |
| 52 | (!strcmp(phase, "supplementary-member") ? shared : empty) |
| 53 | }; |
| 54 | *result = group; |
| 55 | return 0; |
| 56 | } |
| 57 | #include <dirent.h> |
| 58 | #include <limits.h> |
| 59 | #include <stddef.h> |
| 60 | #include <stdio.h> |
| 61 | #include <sys/socket.h> |
| 62 | #include <sys/stat.h> |
| 63 | #include <sys/un.h> |
| 64 | |
| 65 | static const char *redirect(const char *path, char *buffer, size_t length) { |
| 66 | if (!path) { |
| 67 | return path; |
| 68 | } |
| 69 | const char *base = NULL; |
| 70 | const char *tail = NULL; |
| 71 | if (!strncmp(path, "/var/lib/sss", 12) && (!path[12] || path[12] == '/')) { |
| 72 | base = getenv("ZIPELO_GROUP_SSS"); |
| 73 | tail = path + 12; |
| 74 | } else { |
| 75 | const char *paths[] = {"/etc/userdb", "/run/userdb", "/run/host/userdb", |
| 76 | "/usr/local/lib/userdb", "/usr/lib/userdb", "/run/host", "/run/systemd/userdb"}; |
| 77 | const char *names[] = {"/d0", "/d1", "/host/userdb", "/d3", "/d4", "/host", "/s"}; |
| 78 | for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) { |
| 79 | size_t n = strlen(paths[i]); |
| 80 | if (!strncmp(path, paths[i], n) && (!path[n] || path[n] == '/')) { |
| 81 | base = getenv("ZIPELO_GROUP_USERDB"); |
| 82 | int size = snprintf(buffer, length, "%s%s%s", base ? base : "", names[i], path + n); |
| 83 | if (!base || size < 0 || (size_t)size >= length) { |
| 84 | errno = EACCES; |
| 85 | return NULL; |
| 86 | } |
| 87 | return buffer; |
| 88 | } |
| 89 | } |
| 90 | return path; |
| 91 | } |
| 92 | int n = snprintf(buffer, length, "%s%s", base ? base : "", tail); |
| 93 | if (!base || n < 0 || (size_t)n >= length) { |
| 94 | errno = EACCES; |
| 95 | return NULL; |
| 96 | } |
| 97 | return buffer; |
| 98 | } |
| 99 | |
| 100 | int private_group_statx(int fd, const char *path, int flags, unsigned int mask, |
| 101 | struct statx *info) __asm__("statx"); |
| 102 | |
| 103 | int private_group_statx(int fd, const char *path, int flags, unsigned int mask, struct statx *info) { |
| 104 | int (*original)(int, const char *, int, unsigned int, struct statx *) = dlsym(RTLD_NEXT, "statx"); |
| 105 | const char *phase = getenv("ZIPELO_GROUP_CASE"); |
| 106 | if (path && phase && ((!strcmp(phase, "sss-probe-error") && !strncmp(path, "/var/lib/sss", 12)) || |
| 107 | (!strcmp(phase, "userdb-probe-error") && !strcmp(path, "/etc/userdb")))) { |
| 108 | errno = EACCES; |
| 109 | return -1; |
| 110 | } |
| 111 | char buffer[PATH_MAX]; |
| 112 | const char *mapped = redirect(path, buffer, sizeof(buffer)); |
| 113 | return mapped || !path ? original(fd, mapped, flags, mask, info) : -1; |
| 114 | } |
| 115 | |
| 116 | DIR *opendir(const char *path) { |
| 117 | DIR *(*original)(const char *) = dlsym(RTLD_NEXT, "opendir"); |
| 118 | char buffer[PATH_MAX]; |
| 119 | const char *mapped = redirect(path, buffer, sizeof(buffer)); |
| 120 | return mapped ? original(mapped) : NULL; |
| 121 | } |
| 122 | |
| 123 | int connect(int fd, const struct sockaddr *address, socklen_t length) { |
| 124 | int (*original)(int, const struct sockaddr *, socklen_t) = dlsym(RTLD_NEXT, "connect"); |
| 125 | const struct sockaddr_un *un = (const struct sockaddr_un *)address; |
| 126 | if (address->sa_family != AF_UNIX || strncmp(un->sun_path, "/run/systemd/userdb/", 20)) { |
| 127 | return original(fd, address, length); |
| 128 | } |
| 129 | char buffer[PATH_MAX]; |
| 130 | const char *mapped = redirect(un->sun_path, buffer, sizeof(buffer)); |
| 131 | if (!mapped) return -1; |
| 132 | char *slash = strrchr(buffer, '/'); |
| 133 | if (!slash) return -1; |
| 134 | *slash = 0; |
| 135 | int parent = open(buffer, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 136 | if (parent < 0) return -1; |
| 137 | struct sockaddr_un redirected = {.sun_family = AF_UNIX}; |
| 138 | int n = snprintf(redirected.sun_path, sizeof(redirected.sun_path), "/proc/self/fd/%d/%s", parent, slash + 1); |
| 139 | int result = -1; |
| 140 | if (n >= 0 && (size_t)n < sizeof(redirected.sun_path)) { |
| 141 | result = original(fd, (const struct sockaddr *)&redirected, offsetof(struct sockaddr_un, sun_path) + n + 1); |
| 142 | } |
| 143 | close(parent); |
| 144 | return result; |
| 145 | } |
| 146 | |
| 147 | #include <sys/xattr.h> |
| 148 | |
| 149 | ssize_t fgetxattr(int fd, const char *name, void *value, size_t size) { |
| 150 | ssize_t (*original)(int, const char *, void *, size_t) = dlsym(RTLD_NEXT, "fgetxattr"); |
| 151 | const char *phase = getenv("ZIPELO_GROUP_CASE"); |
| 152 | if (!phase || strcmp(name, "system.posix_acl_access")) { |
| 153 | return original(fd, name, value, size); |
| 154 | } |
| 155 | const char *errors[] = {"acl-enodata", "acl-enotsup", "acl-eacces", "acl-eio", "acl-erange"}; |
| 156 | const int codes[] = {ENODATA, ENOTSUP, EACCES, EIO, ERANGE}; |
| 157 | for (size_t i = 0; i < sizeof(codes) / sizeof(codes[0]); i++) { |
| 158 | if (!strcmp(phase, errors[i])) { |
| 159 | errno = codes[i]; |
| 160 | return -1; |
| 161 | } |
| 162 | } |
| 163 | if (!strncmp(phase, "acl-invalid-", 12) || !strcmp(phase, "acl-truncated") || !strcmp(phase, "acl-unknown-tag")) { |
| 164 | unsigned char bytes[] = {2, 0, 0, 0, |
| 165 | 1, 0, 7, 0, 255, 255, 255, 255, |
| 166 | 4, 0, 7, 0, 255, 255, 255, 255, |
| 167 | 32, 0, 5, 0, 255, 255, 255, 255}; |
| 168 | if (!strcmp(phase, "acl-invalid-version")) bytes[0] = 3; |
| 169 | if (!strcmp(phase, "acl-invalid-permissions")) bytes[6] = 8; |
| 170 | if (!strcmp(phase, "acl-invalid-id")) bytes[8] = 0; |
| 171 | if (!strcmp(phase, "acl-unknown-tag")) bytes[4] = 64; |
| 172 | size_t length = !strcmp(phase, "acl-truncated") ? sizeof(bytes) - 1 : sizeof(bytes); |
| 173 | if (size < length) { errno = ERANGE; return -1; } |
| 174 | memcpy(value, bytes, length); |
| 175 | return (ssize_t)length; |
| 176 | } |
| 177 | return original(fd, name, value, size); |
| 178 | } |