Commit | Line | Data |
---|---|---|
60b6c79c MD |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
d14d33bf AM |
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. | |
60b6c79c MD |
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 | |
d14d33bf | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
60b6c79c MD |
12 | * more details. |
13 | * | |
d14d33bf AM |
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. | |
60b6c79c MD |
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> | |
c2b75c49 | 30 | #include <sched.h> |
730389d9 | 31 | #include <sys/signal.h> |
60b6c79c | 32 | |
90e535ef | 33 | #include <common/common.h> |
3fd15a74 | 34 | #include <common/utils.h> |
730389d9 DG |
35 | #include <common/compat/mman.h> |
36 | #include <common/compat/clone.h> | |
60b6c79c | 37 | |
0857097f DG |
38 | #include "runas.h" |
39 | ||
e11d277b | 40 | #define RUNAS_CHILD_STACK_SIZE 10485760 |
c2b75c49 | 41 | |
1e4035fc AS |
42 | #ifndef MAP_STACK |
43 | #define MAP_STACK 0 | |
44 | #endif | |
45 | ||
0756d557 MD |
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 | |
059e1d3d MD |
51 | #endif |
52 | ||
a5ae566a | 53 | #ifndef MAP_GROWSDOWN |
edb025e8 | 54 | #define MAP_GROWSDOWN 0 |
a5ae566a MD |
55 | #endif |
56 | ||
57 | #ifndef MAP_ANONYMOUS | |
58 | #define MAP_ANONYMOUS MAP_ANON | |
59 | #endif | |
60 | ||
c2b75c49 MD |
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 | ||
e11d277b | 69 | struct run_as_mkdir_data { |
60b6c79c MD |
70 | const char *path; |
71 | mode_t mode; | |
72 | }; | |
73 | ||
e11d277b | 74 | struct run_as_open_data { |
60b6c79c MD |
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 | { | |
e11d277b | 86 | struct run_as_mkdir_data *data = _data; |
60b6c79c | 87 | const char *path; |
60b6c79c | 88 | mode_t mode; |
60b6c79c MD |
89 | |
90 | path = data->path; | |
91 | mode = data->mode; | |
92 | ||
3fd15a74 | 93 | return utils_mkdir_recursive(path, mode); |
60b6c79c MD |
94 | } |
95 | ||
96 | static | |
97 | int _mkdir(void *_data) | |
98 | { | |
7ce36756 | 99 | int ret; |
e11d277b | 100 | struct run_as_mkdir_data *data = _data; |
7ce36756 DG |
101 | |
102 | ret = mkdir(data->path, data->mode); | |
103 | if (ret < 0) { | |
104 | ret = -errno; | |
105 | } | |
106 | ||
107 | return ret; | |
60b6c79c MD |
108 | } |
109 | ||
110 | static | |
111 | int _open(void *_data) | |
112 | { | |
e11d277b | 113 | struct run_as_open_data *data = _data; |
60b6c79c MD |
114 | return open(data->path, data->flags, data->mode); |
115 | } | |
116 | ||
c2b75c49 MD |
117 | static |
118 | int child_run_as(void *_data) | |
119 | { | |
6775595e | 120 | int ret; |
c2b75c49 | 121 | struct run_as_data *data = _data; |
6775595e DG |
122 | ssize_t writelen; |
123 | size_t writeleft, index; | |
c2b75c49 MD |
124 | union { |
125 | int i; | |
126 | char c[sizeof(int)]; | |
127 | } sendret; | |
c2b75c49 MD |
128 | |
129 | /* | |
130 | * Child: it is safe to drop egid and euid while sharing the | |
131 | * file descriptors with the parent process, since we do not | |
132 | * drop "uid": therefore, the user we are dropping egid/euid to | |
133 | * cannot attach to this process with, e.g. ptrace, nor map this | |
134 | * process memory. | |
135 | */ | |
1576d582 MD |
136 | if (data->gid != getegid()) { |
137 | ret = setegid(data->gid); | |
138 | if (ret < 0) { | |
4c462e79 | 139 | PERROR("setegid"); |
6d73c4ef MD |
140 | sendret.i = -1; |
141 | goto write_return; | |
1576d582 | 142 | } |
c2b75c49 | 143 | } |
1576d582 MD |
144 | if (data->uid != geteuid()) { |
145 | ret = seteuid(data->uid); | |
146 | if (ret < 0) { | |
4c462e79 | 147 | PERROR("seteuid"); |
6d73c4ef MD |
148 | sendret.i = -1; |
149 | goto write_return; | |
1576d582 | 150 | } |
c2b75c49 MD |
151 | } |
152 | /* | |
153 | * Also set umask to 0 for mkdir executable bit. | |
154 | */ | |
155 | umask(0); | |
156 | sendret.i = (*data->cmd)(data->data); | |
6d73c4ef MD |
157 | |
158 | write_return: | |
c2b75c49 MD |
159 | /* send back return value */ |
160 | writeleft = sizeof(sendret); | |
161 | index = 0; | |
162 | do { | |
6f94560a MD |
163 | do { |
164 | writelen = write(data->retval_pipe, &sendret.c[index], | |
165 | writeleft); | |
166 | } while (writelen < 0 && errno == EINTR); | |
c2b75c49 | 167 | if (writelen < 0) { |
4c462e79 | 168 | PERROR("write"); |
6da9f915 | 169 | return EXIT_FAILURE; |
c2b75c49 MD |
170 | } |
171 | writeleft -= writelen; | |
172 | index += writelen; | |
173 | } while (writeleft > 0); | |
6da9f915 | 174 | return EXIT_SUCCESS; |
c2b75c49 MD |
175 | } |
176 | ||
60b6c79c | 177 | static |
2d85a600 | 178 | int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) |
60b6c79c | 179 | { |
c2b75c49 | 180 | struct run_as_data run_as_data; |
60b6c79c | 181 | int ret = 0; |
c2b75c49 | 182 | int status; |
60b6c79c | 183 | pid_t pid; |
c2b75c49 MD |
184 | int retval_pipe[2]; |
185 | ssize_t readlen, readleft, index; | |
d5bd7573 | 186 | void *child_stack; |
c2b75c49 MD |
187 | union { |
188 | int i; | |
189 | char c[sizeof(int)]; | |
190 | } retval; | |
60b6c79c MD |
191 | |
192 | /* | |
193 | * If we are non-root, we can only deal with our own uid. | |
194 | */ | |
195 | if (geteuid() != 0) { | |
196 | if (uid != geteuid()) { | |
197 | ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)", | |
198 | uid, geteuid()); | |
199 | return -EPERM; | |
200 | } | |
60b6c79c MD |
201 | } |
202 | ||
c2b75c49 MD |
203 | ret = pipe(retval_pipe); |
204 | if (ret < 0) { | |
4c462e79 | 205 | PERROR("pipe"); |
def5e0e8 | 206 | retval.i = ret; |
60b6c79c | 207 | goto end; |
c2b75c49 MD |
208 | } |
209 | run_as_data.data = data; | |
210 | run_as_data.cmd = cmd; | |
211 | run_as_data.uid = uid; | |
212 | run_as_data.gid = gid; | |
213 | run_as_data.retval_pipe = retval_pipe[1]; /* write end */ | |
e11d277b | 214 | child_stack = mmap(NULL, RUNAS_CHILD_STACK_SIZE, |
d5bd7573 | 215 | PROT_WRITE | PROT_READ, |
0756d557 | 216 | MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | LTTNG_MAP_STACK, |
d5bd7573 MD |
217 | -1, 0); |
218 | if (child_stack == MAP_FAILED) { | |
4c462e79 | 219 | PERROR("mmap"); |
def5e0e8 | 220 | retval.i = -ENOMEM; |
d5bd7573 MD |
221 | goto close_pipe; |
222 | } | |
223 | /* | |
224 | * Pointing to the middle of the stack to support architectures | |
225 | * where the stack grows up (HPPA). | |
226 | */ | |
89831a74 MD |
227 | pid = lttng_clone_files(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2), |
228 | &run_as_data); | |
c2b75c49 | 229 | if (pid < 0) { |
4c462e79 | 230 | PERROR("clone"); |
def5e0e8 | 231 | retval.i = pid; |
d5bd7573 | 232 | goto unmap_stack; |
c2b75c49 MD |
233 | } |
234 | /* receive return value */ | |
235 | readleft = sizeof(retval); | |
236 | index = 0; | |
237 | do { | |
238 | readlen = read(retval_pipe[0], &retval.c[index], readleft); | |
239 | if (readlen < 0) { | |
4c462e79 | 240 | PERROR("read"); |
c2b75c49 MD |
241 | ret = -1; |
242 | break; | |
60b6c79c | 243 | } |
c2b75c49 MD |
244 | readleft -= readlen; |
245 | index += readlen; | |
246 | } while (readleft > 0); | |
247 | ||
248 | /* | |
249 | * Parent: wait for child to return, in which case the | |
250 | * shared memory map will have been created. | |
251 | */ | |
47fb7563 | 252 | pid = waitpid(pid, &status, 0); |
c2b75c49 | 253 | if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
4c462e79 | 254 | PERROR("wait"); |
def5e0e8 | 255 | retval.i = -1; |
60b6c79c | 256 | } |
d5bd7573 | 257 | unmap_stack: |
e11d277b | 258 | ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE); |
d5bd7573 | 259 | if (ret < 0) { |
4c462e79 | 260 | PERROR("munmap"); |
def5e0e8 | 261 | retval.i = ret; |
d5bd7573 | 262 | } |
c2b75c49 | 263 | close_pipe: |
4c462e79 MD |
264 | ret = close(retval_pipe[0]); |
265 | if (ret) { | |
266 | PERROR("close"); | |
267 | } | |
268 | ret = close(retval_pipe[1]); | |
269 | if (ret) { | |
270 | PERROR("close"); | |
271 | } | |
60b6c79c | 272 | end: |
c2b75c49 | 273 | return retval.i; |
60b6c79c MD |
274 | } |
275 | ||
2d85a600 MD |
276 | /* |
277 | * To be used on setups where gdb has issues debugging programs using | |
278 | * clone/rfork. Note that this is for debuging ONLY, and should not be | |
279 | * considered secure. | |
280 | */ | |
281 | static | |
282 | int run_as_noclone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) | |
283 | { | |
5b73926f DG |
284 | int ret; |
285 | mode_t old_mask; | |
286 | ||
287 | old_mask = umask(0); | |
288 | ret = cmd(data); | |
289 | umask(old_mask); | |
290 | ||
291 | return ret; | |
2d85a600 MD |
292 | } |
293 | ||
294 | static | |
295 | int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) | |
296 | { | |
297 | if (!getenv("LTTNG_DEBUG_NOCLONE")) { | |
9ef70f87 MD |
298 | int ret; |
299 | ||
2d85a600 | 300 | DBG("Using run_as_clone"); |
9ef70f87 MD |
301 | pthread_mutex_lock(<tng_libc_state_lock); |
302 | ret = run_as_clone(cmd, data, uid, gid); | |
303 | pthread_mutex_unlock(<tng_libc_state_lock); | |
304 | return ret; | |
2d85a600 MD |
305 | } else { |
306 | DBG("Using run_as_noclone"); | |
307 | return run_as_noclone(cmd, data, uid, gid); | |
308 | } | |
309 | } | |
310 | ||
90e535ef | 311 | LTTNG_HIDDEN |
e11d277b | 312 | int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid) |
60b6c79c | 313 | { |
e11d277b | 314 | struct run_as_mkdir_data data; |
60b6c79c MD |
315 | |
316 | DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d", | |
317 | path, mode, uid, gid); | |
318 | data.path = path; | |
319 | data.mode = mode; | |
320 | return run_as(_mkdir_recursive, &data, uid, gid); | |
321 | } | |
322 | ||
90e535ef | 323 | LTTNG_HIDDEN |
e11d277b | 324 | int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) |
60b6c79c | 325 | { |
e11d277b | 326 | struct run_as_mkdir_data data; |
60b6c79c MD |
327 | |
328 | DBG3("mkdir() %s with mode %d for uid %d and gid %d", | |
329 | path, mode, uid, gid); | |
330 | data.path = path; | |
331 | data.mode = mode; | |
332 | return run_as(_mkdir, &data, uid, gid); | |
333 | } | |
334 | ||
335 | /* | |
336 | * Note: open_run_as is currently not working. We'd need to pass the fd | |
337 | * opened in the child to the parent. | |
338 | */ | |
90e535ef | 339 | LTTNG_HIDDEN |
e11d277b | 340 | int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid) |
60b6c79c | 341 | { |
e11d277b | 342 | struct run_as_open_data data; |
c2b75c49 | 343 | |
47fb7563 MD |
344 | DBG3("open() %s with flags %X mode %d for uid %d and gid %d", |
345 | path, flags, mode, uid, gid); | |
60b6c79c MD |
346 | data.path = path; |
347 | data.flags = flags; | |
348 | data.mode = mode; | |
349 | return run_as(_open, &data, uid, gid); | |
60b6c79c | 350 | } |