| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License, version 2 only, |
| 7 | * as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | #define _GNU_SOURCE |
| 20 | #include <errno.h> |
| 21 | #include <limits.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/wait.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <unistd.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <sched.h> |
| 31 | #include <sys/signal.h> |
| 32 | |
| 33 | #include <common/common.h> |
| 34 | #include <common/utils.h> |
| 35 | #include <common/compat/mman.h> |
| 36 | #include <common/compat/clone.h> |
| 37 | |
| 38 | #include "runas.h" |
| 39 | |
| 40 | #define RUNAS_CHILD_STACK_SIZE 10485760 |
| 41 | |
| 42 | #ifndef MAP_STACK |
| 43 | #define MAP_STACK 0 |
| 44 | #endif |
| 45 | |
| 46 | #ifdef __FreeBSD__ |
| 47 | /* FreeBSD MAP_STACK always return -ENOMEM */ |
| 48 | #define LTTNG_MAP_STACK 0 |
| 49 | #else |
| 50 | #define LTTNG_MAP_STACK MAP_STACK |
| 51 | #endif |
| 52 | |
| 53 | #ifndef MAP_GROWSDOWN |
| 54 | #define MAP_GROWSDOWN 0 |
| 55 | #endif |
| 56 | |
| 57 | #ifndef MAP_ANONYMOUS |
| 58 | #define MAP_ANONYMOUS MAP_ANON |
| 59 | #endif |
| 60 | |
| 61 | struct run_as_data { |
| 62 | int (*cmd)(void *data); |
| 63 | void *data; |
| 64 | uid_t uid; |
| 65 | gid_t gid; |
| 66 | int retval_pipe; |
| 67 | }; |
| 68 | |
| 69 | struct run_as_mkdir_data { |
| 70 | const char *path; |
| 71 | mode_t mode; |
| 72 | }; |
| 73 | |
| 74 | struct run_as_open_data { |
| 75 | const char *path; |
| 76 | int flags; |
| 77 | mode_t mode; |
| 78 | }; |
| 79 | |
| 80 | /* |
| 81 | * Create recursively directory using the FULL path. |
| 82 | */ |
| 83 | static |
| 84 | int _mkdir_recursive(void *_data) |
| 85 | { |
| 86 | struct run_as_mkdir_data *data = _data; |
| 87 | const char *path; |
| 88 | mode_t mode; |
| 89 | |
| 90 | path = data->path; |
| 91 | mode = data->mode; |
| 92 | |
| 93 | return utils_mkdir_recursive(path, mode); |
| 94 | } |
| 95 | |
| 96 | static |
| 97 | int _mkdir(void *_data) |
| 98 | { |
| 99 | int ret; |
| 100 | struct run_as_mkdir_data *data = _data; |
| 101 | |
| 102 | ret = mkdir(data->path, data->mode); |
| 103 | if (ret < 0) { |
| 104 | ret = -errno; |
| 105 | } |
| 106 | |
| 107 | return ret; |
| 108 | } |
| 109 | |
| 110 | static |
| 111 | int _open(void *_data) |
| 112 | { |
| 113 | struct run_as_open_data *data = _data; |
| 114 | return open(data->path, data->flags, data->mode); |
| 115 | } |
| 116 | |
| 117 | static |
| 118 | int child_run_as(void *_data) |
| 119 | { |
| 120 | int ret; |
| 121 | struct run_as_data *data = _data; |
| 122 | ssize_t writelen; |
| 123 | int sendret; |
| 124 | |
| 125 | /* |
| 126 | * Child: it is safe to drop egid and euid while sharing the |
| 127 | * file descriptors with the parent process, since we do not |
| 128 | * drop "uid": therefore, the user we are dropping egid/euid to |
| 129 | * cannot attach to this process with, e.g. ptrace, nor map this |
| 130 | * process memory. |
| 131 | */ |
| 132 | if (data->gid != getegid()) { |
| 133 | ret = setegid(data->gid); |
| 134 | if (ret < 0) { |
| 135 | PERROR("setegid"); |
| 136 | sendret = -1; |
| 137 | goto write_return; |
| 138 | } |
| 139 | } |
| 140 | if (data->uid != geteuid()) { |
| 141 | ret = seteuid(data->uid); |
| 142 | if (ret < 0) { |
| 143 | PERROR("seteuid"); |
| 144 | sendret = -1; |
| 145 | goto write_return; |
| 146 | } |
| 147 | } |
| 148 | /* |
| 149 | * Also set umask to 0 for mkdir executable bit. |
| 150 | */ |
| 151 | umask(0); |
| 152 | sendret = (*data->cmd)(data->data); |
| 153 | |
| 154 | write_return: |
| 155 | /* send back return value */ |
| 156 | writelen = lttng_write(data->retval_pipe, &sendret, sizeof(sendret)); |
| 157 | if (writelen < sizeof(sendret)) { |
| 158 | PERROR("lttng_write error"); |
| 159 | return EXIT_FAILURE; |
| 160 | } else { |
| 161 | return EXIT_SUCCESS; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static |
| 166 | int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) |
| 167 | { |
| 168 | struct run_as_data run_as_data; |
| 169 | int ret = 0; |
| 170 | ssize_t readlen; |
| 171 | int status; |
| 172 | pid_t pid; |
| 173 | int retval_pipe[2]; |
| 174 | void *child_stack; |
| 175 | int retval; |
| 176 | |
| 177 | /* |
| 178 | * If we are non-root, we can only deal with our own uid. |
| 179 | */ |
| 180 | if (geteuid() != 0) { |
| 181 | if (uid != geteuid()) { |
| 182 | ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)", |
| 183 | uid, geteuid()); |
| 184 | return -EPERM; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | ret = pipe(retval_pipe); |
| 189 | if (ret < 0) { |
| 190 | PERROR("pipe"); |
| 191 | retval = ret; |
| 192 | goto end; |
| 193 | } |
| 194 | run_as_data.data = data; |
| 195 | run_as_data.cmd = cmd; |
| 196 | run_as_data.uid = uid; |
| 197 | run_as_data.gid = gid; |
| 198 | run_as_data.retval_pipe = retval_pipe[1]; /* write end */ |
| 199 | child_stack = mmap(NULL, RUNAS_CHILD_STACK_SIZE, |
| 200 | PROT_WRITE | PROT_READ, |
| 201 | MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | LTTNG_MAP_STACK, |
| 202 | -1, 0); |
| 203 | if (child_stack == MAP_FAILED) { |
| 204 | PERROR("mmap"); |
| 205 | retval = -ENOMEM; |
| 206 | goto close_pipe; |
| 207 | } |
| 208 | /* |
| 209 | * Pointing to the middle of the stack to support architectures |
| 210 | * where the stack grows up (HPPA). |
| 211 | */ |
| 212 | pid = lttng_clone_files(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2), |
| 213 | &run_as_data); |
| 214 | if (pid < 0) { |
| 215 | PERROR("clone"); |
| 216 | retval = pid; |
| 217 | goto unmap_stack; |
| 218 | } |
| 219 | /* receive return value */ |
| 220 | readlen = lttng_read(retval_pipe[0], &retval, sizeof(retval)); |
| 221 | if (readlen < sizeof(retval)) { |
| 222 | ret = -1; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * Parent: wait for child to return, in which case the |
| 227 | * shared memory map will have been created. |
| 228 | */ |
| 229 | pid = waitpid(pid, &status, 0); |
| 230 | if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 231 | PERROR("wait"); |
| 232 | retval = -1; |
| 233 | } |
| 234 | unmap_stack: |
| 235 | ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE); |
| 236 | if (ret < 0) { |
| 237 | PERROR("munmap"); |
| 238 | retval = ret; |
| 239 | } |
| 240 | close_pipe: |
| 241 | ret = close(retval_pipe[0]); |
| 242 | if (ret) { |
| 243 | PERROR("close"); |
| 244 | } |
| 245 | ret = close(retval_pipe[1]); |
| 246 | if (ret) { |
| 247 | PERROR("close"); |
| 248 | } |
| 249 | end: |
| 250 | return retval; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * To be used on setups where gdb has issues debugging programs using |
| 255 | * clone/rfork. Note that this is for debuging ONLY, and should not be |
| 256 | * considered secure. |
| 257 | */ |
| 258 | static |
| 259 | int run_as_noclone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) |
| 260 | { |
| 261 | int ret; |
| 262 | mode_t old_mask; |
| 263 | |
| 264 | old_mask = umask(0); |
| 265 | ret = cmd(data); |
| 266 | umask(old_mask); |
| 267 | |
| 268 | return ret; |
| 269 | } |
| 270 | |
| 271 | static |
| 272 | int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) |
| 273 | { |
| 274 | if (!getenv("LTTNG_DEBUG_NOCLONE")) { |
| 275 | int ret; |
| 276 | |
| 277 | DBG("Using run_as_clone"); |
| 278 | pthread_mutex_lock(<tng_libc_state_lock); |
| 279 | ret = run_as_clone(cmd, data, uid, gid); |
| 280 | pthread_mutex_unlock(<tng_libc_state_lock); |
| 281 | return ret; |
| 282 | } else { |
| 283 | DBG("Using run_as_noclone"); |
| 284 | return run_as_noclone(cmd, data, uid, gid); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | LTTNG_HIDDEN |
| 289 | int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid) |
| 290 | { |
| 291 | struct run_as_mkdir_data data; |
| 292 | |
| 293 | DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d", |
| 294 | path, mode, uid, gid); |
| 295 | data.path = path; |
| 296 | data.mode = mode; |
| 297 | return run_as(_mkdir_recursive, &data, uid, gid); |
| 298 | } |
| 299 | |
| 300 | LTTNG_HIDDEN |
| 301 | int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) |
| 302 | { |
| 303 | struct run_as_mkdir_data data; |
| 304 | |
| 305 | DBG3("mkdir() %s with mode %d for uid %d and gid %d", |
| 306 | path, mode, uid, gid); |
| 307 | data.path = path; |
| 308 | data.mode = mode; |
| 309 | return run_as(_mkdir, &data, uid, gid); |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * Note: open_run_as is currently not working. We'd need to pass the fd |
| 314 | * opened in the child to the parent. |
| 315 | */ |
| 316 | LTTNG_HIDDEN |
| 317 | int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid) |
| 318 | { |
| 319 | struct run_as_open_data data; |
| 320 | |
| 321 | DBG3("open() %s with flags %X mode %d for uid %d and gid %d", |
| 322 | path, flags, mode, uid, gid); |
| 323 | data.path = path; |
| 324 | data.flags = flags; |
| 325 | data.mode = mode; |
| 326 | return run_as(_open, &data, uid, gid); |
| 327 | } |