| 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 | |
| 58 | void *dlopen(const char *name, int flags) { |
| 59 | void *(*original)(const char *, int) = dlsym(RTLD_NEXT, "dlopen"); |
| 60 | const char *phase = getenv("ZIPELO_GROUP_CASE"); |
| 61 | if (name && !strcmp(name, "libnss_systemd.so.2")) { |
| 62 | if (phase && !strcmp(phase, "systemd-load-error")) { |
| 63 | return NULL; |
| 64 | } |
| 65 | if (phase && !strcmp(phase, "systemd-symbol-error")) { |
| 66 | return original("libm.so.6", flags); |
| 67 | } |
| 68 | name = getenv("LD_PRELOAD"); |
| 69 | } |
| 70 | return original(name, flags); |
| 71 | } |
| 72 | |
| 73 | static int systemd_record; |
| 74 | |
| 75 | int _nss_systemd_setpwent(int stayopen) { |
| 76 | (void)stayopen; |
| 77 | systemd_record = 0; |
| 78 | const char *phase = getenv("ZIPELO_GROUP_CASE"); |
| 79 | return phase && !strcmp(phase, "systemd-set-error") ? -1 : 1; |
| 80 | } |
| 81 | |
| 82 | int _nss_systemd_endpwent(void) { |
| 83 | return 1; |
| 84 | } |
| 85 | |
| 86 | int _nss_systemd_getpwent_r(struct passwd *account, char *buffer, size_t length, |
| 87 | int *error) { |
| 88 | (void)buffer; |
| 89 | const char *phase = getenv("ZIPELO_GROUP_CASE"); |
| 90 | if (!phase || !strcmp(phase, "systemd-enumeration-error") || |
| 91 | (systemd_record && !strcmp(phase, "systemd-partial-error"))) { |
| 92 | *error = EIO; |
| 93 | return -1; |
| 94 | } |
| 95 | if (!systemd_record && |
| 96 | ((!strcmp(phase, "systemd-large-record") && length < 32768) || |
| 97 | !strcmp(phase, "systemd-large-shared"))) { |
| 98 | /* 실제 모듈처럼 버퍼 검사 전에 계정을 소비함. */ |
| 99 | systemd_record = 1; |
| 100 | *error = ERANGE; |
| 101 | return -2; |
| 102 | } |
| 103 | if (!strcmp(phase, "systemd-buffer-limit")) { |
| 104 | *error = ERANGE; |
| 105 | return -2; |
| 106 | } |
| 107 | if (systemd_record || !strcmp(phase, "systemd-personal")) { |
| 108 | return 0; |
| 109 | } |
| 110 | systemd_record = 1; |
| 111 | *account = (struct passwd){ |
| 112 | .pw_name = "systemd-fixture", |
| 113 | .pw_passwd = "x", |
| 114 | .pw_uid = geteuid() + 1, |
| 115 | .pw_gid = getegid() + |
| 116 | ((strcmp(phase, "systemd-dynamic-shared") && |
| 117 | strcmp(phase, "systemd-homed-shared")) ? 1 : 0), |
| 118 | .pw_gecos = "", |
| 119 | .pw_dir = "/", |
| 120 | .pw_shell = "/bin/false" |
| 121 | }; |
| 122 | return 1; |
| 123 | } |
| 124 | |
| 125 | #include <limits.h> |
| 126 | #include <stdio.h> |
| 127 | #include <sys/stat.h> |
| 128 | |
| 129 | int statx(int fd, const char *path, int flags, unsigned int mask, |
| 130 | struct statx *info) { |
| 131 | int (*original)(int, const char *, int, unsigned int, struct statx *) = |
| 132 | dlsym(RTLD_NEXT, "statx"); |
| 133 | const char *prefix = "/var/lib/sss"; |
| 134 | const size_t size = strlen(prefix); |
| 135 | if (!strncmp(path, prefix, size) && (!path[size] || path[size] == '/')) { |
| 136 | const char *base = getenv("ZIPELO_GROUP_SSS"); |
| 137 | const char *phase = getenv("ZIPELO_GROUP_CASE"); |
| 138 | if (!base || (phase && !strcmp(phase, "sss-probe-error"))) { |
| 139 | errno = EACCES; |
| 140 | return -1; |
| 141 | } |
| 142 | char redirected[PATH_MAX]; |
| 143 | int n = snprintf(redirected, sizeof(redirected), "%s%s", base, path + size); |
| 144 | if (n < 0 || (size_t)n >= sizeof(redirected)) { |
| 145 | errno = ENAMETOOLONG; |
| 146 | return -1; |
| 147 | } |
| 148 | return original(fd, redirected, flags, mask, info); |
| 149 | } |
| 150 | return original(fd, path, flags, mask, info); |
| 151 | } |