|
| 1 | +#define _GNU_SOURCE |
| 2 | +#include <unistd.h> |
| 3 | +#include <stdio.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <stdbool.h> |
| 6 | +#include <string.h> |
| 7 | +#include <limits.h> |
| 8 | +#include <fcntl.h> |
| 9 | + |
| 10 | +#include <sys/types.h> |
| 11 | +#include <sys/stat.h> |
| 12 | +#include <sys/vfs.h> |
| 13 | +#include <sys/mman.h> |
| 14 | +#include <sys/sendfile.h> |
| 15 | +#include <sys/syscall.h> |
| 16 | + |
| 17 | +#include <linux/magic.h> |
| 18 | +#include <linux/memfd.h> |
| 19 | + |
| 20 | +#define MEMFD_COMMENT "runc_cloned:/proc/self/exe" |
| 21 | +#define MEMFD_LNKNAME "/memfd:" MEMFD_COMMENT " (deleted)" |
| 22 | + |
| 23 | +/* Use our own wrapper for memfd_create. */ |
| 24 | +#if !defined(SYS_memfd_create) && defined(__NR_memfd_create) |
| 25 | +# define SYS_memfd_create __NR_memfd_create |
| 26 | +#endif |
| 27 | +#ifndef SYS_memfd_create |
| 28 | +# error "memfd_create(2) syscall not supported by this glibc version" |
| 29 | +#endif |
| 30 | +int memfd_create(const char *name, unsigned int flags) |
| 31 | +{ |
| 32 | + return syscall(SYS_memfd_create, name, flags); |
| 33 | +} |
| 34 | + |
| 35 | +/* This comes directly from <linux/fcntl.h>. */ |
| 36 | +#ifndef F_LINUX_SPECIFIC_BASE |
| 37 | +# define F_LINUX_SPECIFIC_BASE 1024 |
| 38 | +#endif |
| 39 | +#ifndef F_ADD_SEALS |
| 40 | +# define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9) |
| 41 | +# define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10) |
| 42 | +#endif |
| 43 | +#ifndef F_SEAL_SEAL |
| 44 | +# define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */ |
| 45 | +# define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */ |
| 46 | +# define F_SEAL_GROW 0x0004 /* prevent file from growing */ |
| 47 | +# define F_SEAL_WRITE 0x0008 /* prevent writes */ |
| 48 | +#endif |
| 49 | + |
| 50 | +/* |
| 51 | + * Verify whether we are currently in a self-cloned program. It's not really |
| 52 | + * possible to trivially identify a memfd compared to a regular tmpfs file, so |
| 53 | + * the best we can do is to check whether the readlink(2) looks okay and that |
| 54 | + * it is on a tmpfs. |
| 55 | + */ |
| 56 | +static int is_self_cloned(void) |
| 57 | +{ |
| 58 | + struct statfs statfsbuf = {0}; |
| 59 | + char linkname[PATH_MAX + 1] = {0}; |
| 60 | + |
| 61 | + if (statfs("/proc/self/exe", &statfsbuf) < 0) |
| 62 | + return -1; |
| 63 | + if (readlink("/proc/self/exe", linkname, PATH_MAX) < 0) |
| 64 | + return -1; |
| 65 | + |
| 66 | + return statfsbuf.f_type == TMPFS_MAGIC && |
| 67 | + !strncmp(linkname, MEMFD_LNKNAME, PATH_MAX); |
| 68 | +} |
| 69 | + |
| 70 | +/* |
| 71 | + * Basic wrapper around mmap(2) that gives you the file length so you can |
| 72 | + * safely treat it as an ordinary buffer. Only gives you read access. |
| 73 | + */ |
| 74 | +static char *read_file(char *path, size_t *length) |
| 75 | +{ |
| 76 | + int fd; |
| 77 | + char buf[4096], *copy = NULL; |
| 78 | + |
| 79 | + if (!length) |
| 80 | + goto err; |
| 81 | + *length = 0; |
| 82 | + |
| 83 | + fd = open(path, O_RDONLY|O_CLOEXEC); |
| 84 | + if (fd < 0) |
| 85 | + goto err_free; |
| 86 | + |
| 87 | + for (;;) { |
| 88 | + int n; |
| 89 | + char *old = copy; |
| 90 | + |
| 91 | + n = read(fd, buf, sizeof(buf)); |
| 92 | + if (n < 0) |
| 93 | + goto err_fd; |
| 94 | + if (!n) |
| 95 | + break; |
| 96 | + |
| 97 | + do { |
| 98 | + copy = realloc(old, (*length + n) * sizeof(*old)); |
| 99 | + } while(!copy); |
| 100 | + |
| 101 | + memcpy(copy + *length, buf, n); |
| 102 | + *length += n; |
| 103 | + } |
| 104 | + close(fd); |
| 105 | + return copy; |
| 106 | + |
| 107 | +err_fd: |
| 108 | + close(fd); |
| 109 | +err_free: |
| 110 | + free(copy); |
| 111 | +err: |
| 112 | + return NULL; |
| 113 | +} |
| 114 | + |
| 115 | +/* |
| 116 | + * A poor-man's version of "xargs -0". Basically parses a given block of |
| 117 | + * NUL-delimited data, within the given length and adds a pointer to each entry |
| 118 | + * to the array of pointers. |
| 119 | + */ |
| 120 | +static int parse_xargs(char *data, int data_length, char ***output) |
| 121 | +{ |
| 122 | + int num = 0; |
| 123 | + char *cur = data; |
| 124 | + |
| 125 | + if (!data || *output) |
| 126 | + return -1; |
| 127 | + |
| 128 | + do { |
| 129 | + *output = malloc(sizeof(**output)); |
| 130 | + } while (!*output); |
| 131 | + |
| 132 | + while (cur < data + data_length) { |
| 133 | + char **old = *output; |
| 134 | + |
| 135 | + num++; |
| 136 | + do { |
| 137 | + *output = realloc(old, (num + 1) * sizeof(*old)); |
| 138 | + } while (!*output); |
| 139 | + |
| 140 | + (*output)[num - 1] = cur; |
| 141 | + cur += strlen(cur) + 1; |
| 142 | + } |
| 143 | + (*output)[num] = NULL; |
| 144 | + return num; |
| 145 | +} |
| 146 | + |
| 147 | +/* |
| 148 | + * "Parse" out argv and envp from /proc/self/cmdline and /proc/self/environ. |
| 149 | + * This is necessary because we are running in a context where we don't have a |
| 150 | + * main() that we can just get the arguments from. |
| 151 | + */ |
| 152 | +static int fetchve(char ***argv, char ***envp) |
| 153 | +{ |
| 154 | + char *cmdline, *environ; |
| 155 | + size_t cmdline_size, environ_size; |
| 156 | + |
| 157 | + cmdline = read_file("/proc/self/cmdline", &cmdline_size); |
| 158 | + if (!cmdline) |
| 159 | + goto err; |
| 160 | + environ = read_file("/proc/self/environ", &environ_size); |
| 161 | + if (!environ) |
| 162 | + goto err_free; |
| 163 | + |
| 164 | + if (parse_xargs(cmdline, cmdline_size, argv) <= 0) |
| 165 | + goto err_free_both; |
| 166 | + if (parse_xargs(environ, environ_size, envp) <= 0) |
| 167 | + goto err_free_both; |
| 168 | + |
| 169 | + return 0; |
| 170 | + |
| 171 | +err_free_both: |
| 172 | + free(environ); |
| 173 | +err_free: |
| 174 | + free(cmdline); |
| 175 | +err: |
| 176 | + return -1; |
| 177 | +} |
| 178 | + |
| 179 | +static int clone_binary(void) |
| 180 | +{ |
| 181 | + int binfd, memfd, err; |
| 182 | + ssize_t sent = 0; |
| 183 | + struct stat statbuf = {0}; |
| 184 | + |
| 185 | + binfd = open("/proc/self/exe", O_RDONLY|O_CLOEXEC); |
| 186 | + if (binfd < 0) |
| 187 | + goto err; |
| 188 | + if (fstat(binfd, &statbuf) < 0) |
| 189 | + goto err_binfd; |
| 190 | + |
| 191 | + memfd = memfd_create(MEMFD_COMMENT, MFD_CLOEXEC|MFD_ALLOW_SEALING); |
| 192 | + if (memfd < 0) |
| 193 | + goto err_binfd; |
| 194 | + |
| 195 | + while (sent < statbuf.st_size) { |
| 196 | + ssize_t n = sendfile(memfd, binfd, NULL, statbuf.st_size - sent); |
| 197 | + if (n < 0) |
| 198 | + goto err_memfd; |
| 199 | + sent += n; |
| 200 | + } |
| 201 | + |
| 202 | + err = fcntl(memfd, F_ADD_SEALS, F_SEAL_SHRINK|F_SEAL_GROW|F_SEAL_WRITE|F_SEAL_SEAL); |
| 203 | + if (err < 0) |
| 204 | + goto err_memfd; |
| 205 | + |
| 206 | + close(binfd); |
| 207 | + return memfd; |
| 208 | + |
| 209 | +err_memfd: |
| 210 | + close(memfd); |
| 211 | +err_binfd: |
| 212 | + close(binfd); |
| 213 | +err: |
| 214 | + return -1; |
| 215 | +} |
| 216 | + |
| 217 | +int ensure_cloned_binary(void) |
| 218 | +{ |
| 219 | + int execfd; |
| 220 | + char **argv = NULL, **envp = NULL; |
| 221 | + |
| 222 | + /* Check that we're not self-cloned, and if we are then bail. */ |
| 223 | + int cloned = is_self_cloned(); |
| 224 | + if (cloned != 0) |
| 225 | + return cloned; |
| 226 | + |
| 227 | + if (fetchve(&argv, &envp) < 0) |
| 228 | + return -1; |
| 229 | + |
| 230 | + execfd = clone_binary(); |
| 231 | + if (execfd < 0) |
| 232 | + return -1; |
| 233 | + |
| 234 | + fexecve(execfd, argv, envp); |
| 235 | + return -1; |
| 236 | +} |
0 commit comments