|
| 1 | +/* liblxcapi |
| 2 | + * |
| 3 | + * Copyright © 2019 Christian Brauner <christian.brauner@ubuntu.com>. |
| 4 | + * Copyright © 2019 Canonical Ltd. |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License version 2, as |
| 8 | + * published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License along |
| 16 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + */ |
| 19 | + |
| 20 | +#ifndef _GNU_SOURCE |
| 21 | +#define _GNU_SOURCE 1 |
| 22 | +#endif |
| 23 | +#include <errno.h> |
| 24 | +#include <stdio.h> |
| 25 | +#include <stdlib.h> |
| 26 | +#include <string.h> |
| 27 | + |
| 28 | +#include "config.h" |
| 29 | +#include "file_utils.h" |
| 30 | +#include "raw_syscalls.h" |
| 31 | +#include "string_utils.h" |
| 32 | +#include "syscall_wrappers.h" |
| 33 | + |
| 34 | +#define LXC_MEMFD_REXEC_SEALS \ |
| 35 | + (F_SEAL_SEAL | F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE) |
| 36 | + |
| 37 | +static int push_vargs(char *data, int data_length, char ***output) |
| 38 | +{ |
| 39 | + int num = 0; |
| 40 | + char *cur = data; |
| 41 | + |
| 42 | + if (!data || *output) |
| 43 | + return -1; |
| 44 | + |
| 45 | + *output = must_realloc(NULL, sizeof(**output)); |
| 46 | + |
| 47 | + while (cur < data + data_length) { |
| 48 | + num++; |
| 49 | + *output = must_realloc(*output, (num + 1) * sizeof(**output)); |
| 50 | + |
| 51 | + (*output)[num - 1] = cur; |
| 52 | + cur += strlen(cur) + 1; |
| 53 | + } |
| 54 | + (*output)[num] = NULL; |
| 55 | + return num; |
| 56 | +} |
| 57 | + |
| 58 | +static int parse_exec_params(char ***argv, char ***envp) |
| 59 | +{ |
| 60 | + int ret; |
| 61 | + char *cmdline = NULL, *env = NULL; |
| 62 | + size_t cmdline_size, env_size; |
| 63 | + |
| 64 | + cmdline = file_to_buf("/proc/self/cmdline", &cmdline_size); |
| 65 | + if (!cmdline) |
| 66 | + goto on_error; |
| 67 | + |
| 68 | + env = file_to_buf("/proc/self/environ", &env_size); |
| 69 | + if (!env) |
| 70 | + goto on_error; |
| 71 | + |
| 72 | + ret = push_vargs(cmdline, cmdline_size, argv); |
| 73 | + if (ret <= 0) |
| 74 | + goto on_error; |
| 75 | + |
| 76 | + ret = push_vargs(env, env_size, envp); |
| 77 | + if (ret <= 0) |
| 78 | + goto on_error; |
| 79 | + |
| 80 | + return 0; |
| 81 | + |
| 82 | +on_error: |
| 83 | + free(env); |
| 84 | + free(cmdline); |
| 85 | + |
| 86 | + return -1; |
| 87 | +} |
| 88 | + |
| 89 | +static int is_memfd(void) |
| 90 | +{ |
| 91 | + int fd, saved_errno, seals; |
| 92 | + |
| 93 | + fd = open("/proc/self/exe", O_RDONLY | O_CLOEXEC); |
| 94 | + if (fd < 0) |
| 95 | + return -ENOTRECOVERABLE; |
| 96 | + |
| 97 | + seals = fcntl(fd, F_GET_SEALS); |
| 98 | + saved_errno = errno; |
| 99 | + close(fd); |
| 100 | + errno = saved_errno; |
| 101 | + if (seals < 0) |
| 102 | + return -EINVAL; |
| 103 | + |
| 104 | + return seals == LXC_MEMFD_REXEC_SEALS; |
| 105 | +} |
| 106 | + |
| 107 | +static void lxc_rexec_as_memfd(char **argv, char **envp, const char *memfd_name) |
| 108 | +{ |
| 109 | + int saved_errno; |
| 110 | + ssize_t bytes_sent; |
| 111 | + int fd = -1, memfd = -1; |
| 112 | + |
| 113 | + memfd = memfd_create(memfd_name, MFD_ALLOW_SEALING | MFD_CLOEXEC); |
| 114 | + if (memfd < 0) |
| 115 | + return; |
| 116 | + |
| 117 | + fd = open("/proc/self/exe", O_RDONLY | O_CLOEXEC); |
| 118 | + if (fd < 0) |
| 119 | + goto on_error; |
| 120 | + |
| 121 | + /* sendfile() handles up to 2GB. */ |
| 122 | + bytes_sent = lxc_sendfile_nointr(memfd, fd, NULL, LXC_SENDFILE_MAX); |
| 123 | + saved_errno = errno; |
| 124 | + close(fd); |
| 125 | + errno = saved_errno; |
| 126 | + if (bytes_sent < 0) |
| 127 | + goto on_error; |
| 128 | + |
| 129 | + if (fcntl(memfd, F_ADD_SEALS, LXC_MEMFD_REXEC_SEALS)) |
| 130 | + goto on_error; |
| 131 | + |
| 132 | + fexecve(memfd, argv, envp); |
| 133 | + |
| 134 | +on_error: |
| 135 | + saved_errno = errno; |
| 136 | + close(memfd); |
| 137 | + errno = saved_errno; |
| 138 | +} |
| 139 | + |
| 140 | +static int lxc_rexec(const char *memfd_name) |
| 141 | +{ |
| 142 | + int ret; |
| 143 | + char **argv = NULL, **envp = NULL; |
| 144 | + |
| 145 | + ret = is_memfd(); |
| 146 | + if (ret < 0 && ret == -ENOTRECOVERABLE) { |
| 147 | + fprintf(stderr, |
| 148 | + "%s - Failed to determine whether this is a memfd\n", |
| 149 | + strerror(errno)); |
| 150 | + return -1; |
| 151 | + } else if (ret > 0) { |
| 152 | + return 0; |
| 153 | + } |
| 154 | + |
| 155 | + ret = parse_exec_params(&argv, &envp); |
| 156 | + if (ret < 0) { |
| 157 | + fprintf(stderr, |
| 158 | + "%s - Failed to parse command line parameters\n", |
| 159 | + strerror(errno)); |
| 160 | + return -1; |
| 161 | + } |
| 162 | + |
| 163 | + lxc_rexec_as_memfd(argv, envp, memfd_name); |
| 164 | + fprintf(stderr, "%s - Failed to rexec as memfd\n", strerror(errno)); |
| 165 | + return -1; |
| 166 | +} |
| 167 | + |
| 168 | +/** |
| 169 | + * This function will copy any binary that calls liblxc into a memory file and |
| 170 | + * will use the memfd to rexecute the binary. This is done to prevent attacks |
| 171 | + * through the /proc/self/exe symlink to corrupt the host binary when host and |
| 172 | + * container are in the same user namespace or have set up an identity id |
| 173 | + * mapping: CVE-2019-5736. |
| 174 | + */ |
| 175 | +__attribute__((constructor)) static void liblxc_rexec(void) |
| 176 | +{ |
| 177 | + if (lxc_rexec("liblxc")) { |
| 178 | + fprintf(stderr, "Failed to re-execute liblxc via memory file descriptor\n"); |
| 179 | + _exit(EXIT_FAILURE); |
| 180 | + } |
| 181 | +} |
0 commit comments