| 1 | #define _GNU_SOURCE |
| 2 | #include <dlfcn.h> |
| 3 | #include <errno.h> |
| 4 | #include <fcntl.h> |
| 5 | #include <stdarg.h> |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | #include <sys/statvfs.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | static int (*original_openat)(int, const char *, int, ...); |
| 13 | static int (*original_open64)(const char *, int, ...); |
| 14 | static ssize_t (*original_write)(int, const void *, size_t); |
| 15 | static int written[3]; |
| 16 | static int snapshot_fd = -1; |
| 17 | |
| 18 | __attribute__((constructor)) static void initialize(void) { |
| 19 | original_openat = dlsym(RTLD_NEXT, "openat"); |
| 20 | original_open64 = dlsym(RTLD_NEXT, "open64"); |
| 21 | original_write = dlsym(RTLD_NEXT, "write"); |
| 22 | } |
| 23 | static int location(const char *path) { |
| 24 | if (strstr(path, "/cache/")) return 0; |
| 25 | if (strstr(path, "/archive/")) return 1; |
| 26 | if (strstr(path, "/system/")) return 2; |
| 27 | return -1; |
| 28 | } |
| 29 | static int descriptor(int fd, char *path, size_t size) { |
| 30 | char link[64]; |
| 31 | path[0] = 0; |
| 32 | snprintf(link, sizeof(link), "/proc/self/fd/%d", fd); |
| 33 | ssize_t length = readlink(link, path, size - 1); |
| 34 | if (length < 0) return -1; |
| 35 | path[length] = 0; |
| 36 | return location(path); |
| 37 | } |
| 38 | static int affected(int at) { |
| 39 | const char *mask = getenv("ZIPELO_SPOOL_MASK"); |
| 40 | return at >= 0 && mask && (atoi(mask) & (1 << at)); |
| 41 | } |
| 42 | static void trace(int at) { |
| 43 | const char *path = getenv("ZIPELO_SPOOL_TRACE"); |
| 44 | if (!path || at < 0) return; |
| 45 | int fd = original_openat(AT_FDCWD, path, O_CREAT | O_WRONLY | O_APPEND, 0600); |
| 46 | if (fd >= 0) { |
| 47 | char value = '0' + at; |
| 48 | original_write(fd, &value, 1); |
| 49 | close(fd); |
| 50 | } |
| 51 | } |
| 52 | static int reject(int at, int flags) { |
| 53 | if ((flags & O_TMPFILE) != O_TMPFILE && !(flags & O_CREAT)) return 0; |
| 54 | trace(at); |
| 55 | const char *phase = getenv("ZIPELO_SPOOL_PHASE"); |
| 56 | if (affected(at) && phase && (!strcmp(phase, "create") || !strcmp(phase, "denied"))) { |
| 57 | errno = !strcmp(phase, "denied") ? EACCES : EDQUOT; |
| 58 | return 1; |
| 59 | } |
| 60 | if (getenv("ZIPELO_SPOOL_NAMED") && (flags & O_TMPFILE) == O_TMPFILE) { |
| 61 | errno = EOPNOTSUPP; |
| 62 | return 1; |
| 63 | } |
| 64 | return 0; |
| 65 | } |
| 66 | int openat(int fd, const char *path, int flags, ...) { |
| 67 | mode_t mode = 0; |
| 68 | if ((flags & O_CREAT) || (flags & O_TMPFILE) == O_TMPFILE) { |
| 69 | va_list args; |
| 70 | va_start(args, flags); |
| 71 | mode = va_arg(args, unsigned int); |
| 72 | va_end(args); |
| 73 | } |
| 74 | char parent[4096], full[8192]; |
| 75 | descriptor(fd, parent, sizeof(parent)); |
| 76 | snprintf(full, sizeof(full), "%s/%s", parent, path); |
| 77 | if (reject(location(full), flags)) return -1; |
| 78 | int result = original_openat(fd, path, flags, mode); |
| 79 | if (result >= 0 && snapshot_fd < 0 && location(full) >= 0 |
| 80 | && ((flags & O_TMPFILE) == O_TMPFILE || (flags & O_CREAT))) { |
| 81 | snapshot_fd = result; |
| 82 | } |
| 83 | return result; |
| 84 | } |
| 85 | int open64(const char *path, int flags, ...) { |
| 86 | mode_t mode = 0; |
| 87 | if ((flags & O_CREAT) || (flags & O_TMPFILE) == O_TMPFILE) { |
| 88 | va_list args; |
| 89 | va_start(args, flags); |
| 90 | mode = va_arg(args, unsigned int); |
| 91 | va_end(args); |
| 92 | } |
| 93 | char full[8192]; |
| 94 | snprintf(full, sizeof(full), "%s/", path); |
| 95 | if (reject(location(full), flags)) return -1; |
| 96 | int result = original_open64(path, flags, mode); |
| 97 | if (result >= 0 && snapshot_fd < 0 && location(full) >= 0 |
| 98 | && ((flags & O_TMPFILE) == O_TMPFILE || (flags & O_CREAT))) { |
| 99 | snapshot_fd = result; |
| 100 | } |
| 101 | return result; |
| 102 | } |
| 103 | ssize_t write(int fd, const void *bytes, size_t count) { |
| 104 | char path[4096]; |
| 105 | int at = descriptor(fd, path, sizeof(path)); |
| 106 | const char *phase = getenv("ZIPELO_SPOOL_PHASE"); |
| 107 | int stage = phase && !strcmp(phase, "stage-write") && fd != snapshot_fd; |
| 108 | if (affected(at) && phase && (stage || !strcmp(phase, "write") |
| 109 | || !strcmp(phase, "changed") || !strcmp(phase, "io"))) { |
| 110 | if (!strcmp(phase, "io")) { errno = EIO; return -1; } |
| 111 | if (!written[at]++) return original_write(fd, bytes, count < 7 ? count : 7); |
| 112 | if (!strcmp(phase, "changed")) { |
| 113 | int source = original_openat(AT_FDCWD, getenv("ZIPELO_SPOOL_SOURCE"), O_WRONLY | O_APPEND); |
| 114 | if (source >= 0) { |
| 115 | original_write(source, "x", 1); |
| 116 | close(source); |
| 117 | } |
| 118 | } |
| 119 | errno = EDQUOT; |
| 120 | return -1; |
| 121 | } |
| 122 | return original_write(fd, bytes, count); |
| 123 | } |
| 124 | int fstatvfs(int fd, struct statvfs *value) { |
| 125 | int (*original)(int, struct statvfs *) = dlsym(RTLD_NEXT, "fstatvfs"); |
| 126 | int result = original(fd, value); |
| 127 | char path[4096]; |
| 128 | int at = descriptor(fd, path, sizeof(path)); |
| 129 | const char *phase = getenv("ZIPELO_SPOOL_PHASE"); |
| 130 | int stage = phase && !strcmp(phase, "stage-space") && fd != snapshot_fd; |
| 131 | if (!result && affected(at) && phase && (stage || !strcmp(phase, "space"))) { |
| 132 | value->f_frsize = 1; |
| 133 | value->f_bavail = stage ? 0 : 1024; |
| 134 | } |
| 135 | return result; |
| 136 | } |