|
| 1 | +/* |
| 2 | + * Copyright (C) 2019 Aleksa Sarai <[email protected]> |
| 3 | + * Copyright (C) 2019 SUSE LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#define _GNU_SOURCE |
| 19 | +#include <unistd.h> |
| 20 | +#include <stdio.h> |
| 21 | +#include <stdlib.h> |
| 22 | +#include <stdbool.h> |
| 23 | +#include <string.h> |
| 24 | +#include <limits.h> |
| 25 | +#include <fcntl.h> |
| 26 | +#include <errno.h> |
| 27 | + |
| 28 | +#include <sys/types.h> |
| 29 | +#include <sys/stat.h> |
| 30 | +#include <sys/vfs.h> |
| 31 | +#include <sys/mman.h> |
| 32 | +#include <sys/sendfile.h> |
| 33 | +#include <sys/syscall.h> |
| 34 | + |
| 35 | +/* Use our own wrapper for memfd_create. */ |
| 36 | +#if !defined(SYS_memfd_create) && defined(__NR_memfd_create) |
| 37 | +# define SYS_memfd_create __NR_memfd_create |
| 38 | +#endif |
| 39 | +#ifdef SYS_memfd_create |
| 40 | +# define HAVE_MEMFD_CREATE |
| 41 | +/* memfd_create(2) flags -- copied from <linux/memfd.h>. */ |
| 42 | +# ifndef MFD_CLOEXEC |
| 43 | +# define MFD_CLOEXEC 0x0001U |
| 44 | +# define MFD_ALLOW_SEALING 0x0002U |
| 45 | +# endif |
| 46 | +int memfd_create(const char *name, unsigned int flags) |
| 47 | +{ |
| 48 | + return syscall(SYS_memfd_create, name, flags); |
| 49 | +} |
| 50 | +#endif |
| 51 | + |
| 52 | +/* This comes directly from <linux/fcntl.h>. */ |
| 53 | +#ifndef F_LINUX_SPECIFIC_BASE |
| 54 | +# define F_LINUX_SPECIFIC_BASE 1024 |
| 55 | +#endif |
| 56 | +#ifndef F_ADD_SEALS |
| 57 | +# define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9) |
| 58 | +# define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10) |
| 59 | +#endif |
| 60 | +#ifndef F_SEAL_SEAL |
| 61 | +# define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */ |
| 62 | +# define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */ |
| 63 | +# define F_SEAL_GROW 0x0004 /* prevent file from growing */ |
| 64 | +# define F_SEAL_WRITE 0x0008 /* prevent writes */ |
| 65 | +#endif |
| 66 | + |
| 67 | +#define RUNC_SENDFILE_MAX 0x7FFFF000 /* sendfile(2) is limited to 2GB. */ |
| 68 | +#ifdef HAVE_MEMFD_CREATE |
| 69 | +# define RUNC_MEMFD_COMMENT "runc_cloned:/proc/self/exe" |
| 70 | +# define RUNC_MEMFD_SEALS \ |
| 71 | + (F_SEAL_SEAL | F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE) |
| 72 | +#endif |
| 73 | + |
| 74 | +static void *must_realloc(void *ptr, size_t size) |
| 75 | +{ |
| 76 | + void *old = ptr; |
| 77 | + do { |
| 78 | + ptr = realloc(old, size); |
| 79 | + } while(!ptr); |
| 80 | + return ptr; |
| 81 | +} |
| 82 | + |
| 83 | +/* |
| 84 | + * Verify whether we are currently in a self-cloned program (namely, is |
| 85 | + * /proc/self/exe a memfd). F_GET_SEALS will only succeed for memfds (or rather |
| 86 | + * for shmem files), and we want to be sure it's actually sealed. |
| 87 | + */ |
| 88 | +static int is_self_cloned(void) |
| 89 | +{ |
| 90 | + int fd, ret, is_cloned = 0; |
| 91 | + |
| 92 | + fd = open("/proc/self/exe", O_RDONLY|O_CLOEXEC); |
| 93 | + if (fd < 0) |
| 94 | + return -ENOTRECOVERABLE; |
| 95 | + |
| 96 | +#ifdef HAVE_MEMFD_CREATE |
| 97 | + ret = fcntl(fd, F_GET_SEALS); |
| 98 | + is_cloned = (ret == RUNC_MEMFD_SEALS); |
| 99 | +#else |
| 100 | + struct stat statbuf = {0}; |
| 101 | + ret = fstat(fd, &statbuf); |
| 102 | + if (ret >= 0) |
| 103 | + is_cloned = (statbuf.st_nlink == 0); |
| 104 | +#endif |
| 105 | + close(fd); |
| 106 | + return is_cloned; |
| 107 | +} |
| 108 | + |
| 109 | +/* |
| 110 | + * Basic wrapper around mmap(2) that gives you the file length so you can |
| 111 | + * safely treat it as an ordinary buffer. Only gives you read access. |
| 112 | + */ |
| 113 | +static char *read_file(char *path, size_t *length) |
| 114 | +{ |
| 115 | + int fd; |
| 116 | + char buf[4096], *copy = NULL; |
| 117 | + |
| 118 | + if (!length) |
| 119 | + return NULL; |
| 120 | + |
| 121 | + fd = open(path, O_RDONLY | O_CLOEXEC); |
| 122 | + if (fd < 0) |
| 123 | + return NULL; |
| 124 | + |
| 125 | + *length = 0; |
| 126 | + for (;;) { |
| 127 | + int n; |
| 128 | + |
| 129 | + n = read(fd, buf, sizeof(buf)); |
| 130 | + if (n < 0) |
| 131 | + goto error; |
| 132 | + if (!n) |
| 133 | + break; |
| 134 | + |
| 135 | + copy = must_realloc(copy, (*length + n) * sizeof(*copy)); |
| 136 | + memcpy(copy + *length, buf, n); |
| 137 | + *length += n; |
| 138 | + } |
| 139 | + close(fd); |
| 140 | + return copy; |
| 141 | + |
| 142 | +error: |
| 143 | + close(fd); |
| 144 | + free(copy); |
| 145 | + return NULL; |
| 146 | +} |
| 147 | + |
| 148 | +/* |
| 149 | + * A poor-man's version of "xargs -0". Basically parses a given block of |
| 150 | + * NUL-delimited data, within the given length and adds a pointer to each entry |
| 151 | + * to the array of pointers. |
| 152 | + */ |
| 153 | +static int parse_xargs(char *data, int data_length, char ***output) |
| 154 | +{ |
| 155 | + int num = 0; |
| 156 | + char *cur = data; |
| 157 | + |
| 158 | + if (!data || *output != NULL) |
| 159 | + return -1; |
| 160 | + |
| 161 | + while (cur < data + data_length) { |
| 162 | + num++; |
| 163 | + *output = must_realloc(*output, (num + 1) * sizeof(**output)); |
| 164 | + (*output)[num - 1] = cur; |
| 165 | + cur += strlen(cur) + 1; |
| 166 | + } |
| 167 | + (*output)[num] = NULL; |
| 168 | + return num; |
| 169 | +} |
| 170 | + |
| 171 | +/* |
| 172 | + * "Parse" out argv and envp from /proc/self/cmdline and /proc/self/environ. |
| 173 | + * This is necessary because we are running in a context where we don't have a |
| 174 | + * main() that we can just get the arguments from. |
| 175 | + */ |
| 176 | +static int fetchve(char ***argv, char ***envp) |
| 177 | +{ |
| 178 | + char *cmdline = NULL, *environ = NULL; |
| 179 | + size_t cmdline_size, environ_size; |
| 180 | + |
| 181 | + cmdline = read_file("/proc/self/cmdline", &cmdline_size); |
| 182 | + if (!cmdline) |
| 183 | + goto error; |
| 184 | + environ = read_file("/proc/self/environ", &environ_size); |
| 185 | + if (!environ) |
| 186 | + goto error; |
| 187 | + |
| 188 | + if (parse_xargs(cmdline, cmdline_size, argv) <= 0) |
| 189 | + goto error; |
| 190 | + if (parse_xargs(environ, environ_size, envp) <= 0) |
| 191 | + goto error; |
| 192 | + |
| 193 | + return 0; |
| 194 | + |
| 195 | +error: |
| 196 | + free(environ); |
| 197 | + free(cmdline); |
| 198 | + return -EINVAL; |
| 199 | +} |
| 200 | + |
| 201 | +static int clone_binary(void) |
| 202 | +{ |
| 203 | + int binfd, memfd; |
| 204 | + ssize_t sent = 0; |
| 205 | + |
| 206 | +#ifdef HAVE_MEMFD_CREATE |
| 207 | + memfd = memfd_create(RUNC_MEMFD_COMMENT, MFD_CLOEXEC | MFD_ALLOW_SEALING); |
| 208 | +#else |
| 209 | + memfd = open("/tmp", O_TMPFILE | O_EXCL | O_RDWR | O_CLOEXEC, 0711); |
| 210 | +#endif |
| 211 | + if (memfd < 0) |
| 212 | + return -ENOTRECOVERABLE; |
| 213 | + |
| 214 | + binfd = open("/proc/self/exe", O_RDONLY | O_CLOEXEC); |
| 215 | + if (binfd < 0) |
| 216 | + goto error; |
| 217 | + |
| 218 | + sent = sendfile(memfd, binfd, NULL, RUNC_SENDFILE_MAX); |
| 219 | + close(binfd); |
| 220 | + if (sent < 0) |
| 221 | + goto error; |
| 222 | + |
| 223 | +#ifdef HAVE_MEMFD_CREATE |
| 224 | + int err = fcntl(memfd, F_ADD_SEALS, RUNC_MEMFD_SEALS); |
| 225 | + if (err < 0) |
| 226 | + goto error; |
| 227 | +#else |
| 228 | + /* Need to re-open "memfd" as read-only to avoid execve(2) giving -EXTBUSY. */ |
| 229 | + int newfd; |
| 230 | + char *fdpath = NULL; |
| 231 | + |
| 232 | + if (asprintf(&fdpath, "/proc/self/fd/%d", memfd) < 0) |
| 233 | + goto error; |
| 234 | + newfd = open(fdpath, O_RDONLY | O_CLOEXEC); |
| 235 | + free(fdpath); |
| 236 | + if (newfd < 0) |
| 237 | + goto error; |
| 238 | + |
| 239 | + close(memfd); |
| 240 | + memfd = newfd; |
| 241 | +#endif |
| 242 | + return memfd; |
| 243 | + |
| 244 | +error: |
| 245 | + close(memfd); |
| 246 | + return -EIO; |
| 247 | +} |
| 248 | + |
| 249 | +int ensure_cloned_binary(void) |
| 250 | +{ |
| 251 | + int execfd; |
| 252 | + char **argv = NULL, **envp = NULL; |
| 253 | + |
| 254 | + /* Check that we're not self-cloned, and if we are then bail. */ |
| 255 | + int cloned = is_self_cloned(); |
| 256 | + if (cloned > 0 || cloned == -ENOTRECOVERABLE) |
| 257 | + return cloned; |
| 258 | + |
| 259 | + if (fetchve(&argv, &envp) < 0) |
| 260 | + return -EINVAL; |
| 261 | + |
| 262 | + execfd = clone_binary(); |
| 263 | + if (execfd < 0) |
| 264 | + return -EIO; |
| 265 | + |
| 266 | + fexecve(execfd, argv, envp); |
| 267 | + return -ENOEXEC; |
| 268 | +} |
0 commit comments