Commit | Line | Data |
---|---|---|
60b6c79c MD |
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 it | |
6 | * under the terms of the GNU General Public License as published by the Free | |
7 | * Software Foundation; only version 2 of the License. | |
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 with | |
15 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple | |
16 | * Place - Suite 330, Boston, MA 02111-1307, 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> | |
c2b75c49 | 30 | #include <sched.h> |
730389d9 | 31 | #include <sys/signal.h> |
60b6c79c | 32 | |
db758600 | 33 | #include <common/error.h> |
730389d9 DG |
34 | #include <common/compat/mman.h> |
35 | #include <common/compat/clone.h> | |
60b6c79c | 36 | |
0857097f DG |
37 | #include "runas.h" |
38 | ||
e11d277b | 39 | #define RUNAS_CHILD_STACK_SIZE 10485760 |
c2b75c49 | 40 | |
edb025e8 MD |
41 | #ifndef __FreeBSD__ |
42 | # ifndef MAP_STACK | |
43 | # define MAP_STACK 0 | |
44 | # endif | |
45 | #else /* FreeBSD MAP_STACK always return -ENOMEM */ | |
46 | # define MAP_STACK 0 | |
059e1d3d MD |
47 | #endif |
48 | ||
a5ae566a | 49 | #ifndef MAP_GROWSDOWN |
edb025e8 | 50 | #define MAP_GROWSDOWN 0 |
a5ae566a MD |
51 | #endif |
52 | ||
53 | #ifndef MAP_ANONYMOUS | |
54 | #define MAP_ANONYMOUS MAP_ANON | |
55 | #endif | |
56 | ||
c2b75c49 MD |
57 | struct run_as_data { |
58 | int (*cmd)(void *data); | |
59 | void *data; | |
60 | uid_t uid; | |
61 | gid_t gid; | |
62 | int retval_pipe; | |
63 | }; | |
64 | ||
e11d277b | 65 | struct run_as_mkdir_data { |
60b6c79c MD |
66 | const char *path; |
67 | mode_t mode; | |
68 | }; | |
69 | ||
e11d277b | 70 | struct run_as_open_data { |
60b6c79c MD |
71 | const char *path; |
72 | int flags; | |
73 | mode_t mode; | |
74 | }; | |
75 | ||
76 | /* | |
77 | * Create recursively directory using the FULL path. | |
78 | */ | |
79 | static | |
80 | int _mkdir_recursive(void *_data) | |
81 | { | |
e11d277b | 82 | struct run_as_mkdir_data *data = _data; |
60b6c79c MD |
83 | const char *path; |
84 | char *p, tmp[PATH_MAX]; | |
85 | struct stat statbuf; | |
86 | mode_t mode; | |
87 | size_t len; | |
88 | int ret; | |
89 | ||
90 | path = data->path; | |
91 | mode = data->mode; | |
92 | ||
93 | ret = snprintf(tmp, sizeof(tmp), "%s", path); | |
94 | if (ret < 0) { | |
95 | PERROR("snprintf mkdir"); | |
96 | goto error; | |
97 | } | |
98 | ||
99 | len = ret; | |
100 | if (tmp[len - 1] == '/') { | |
101 | tmp[len - 1] = 0; | |
102 | } | |
103 | ||
104 | for (p = tmp + 1; *p; p++) { | |
105 | if (*p == '/') { | |
106 | *p = 0; | |
107 | ret = stat(tmp, &statbuf); | |
108 | if (ret < 0) { | |
109 | ret = mkdir(tmp, mode); | |
110 | if (ret < 0) { | |
111 | if (!(errno == EEXIST)) { | |
112 | PERROR("mkdir recursive"); | |
113 | ret = -errno; | |
114 | goto error; | |
115 | } | |
116 | } | |
117 | } | |
118 | *p = '/'; | |
119 | } | |
120 | } | |
121 | ||
122 | ret = mkdir(tmp, mode); | |
123 | if (ret < 0) { | |
124 | if (!(errno == EEXIST)) { | |
125 | PERROR("mkdir recursive last piece"); | |
126 | ret = -errno; | |
127 | } else { | |
128 | ret = 0; | |
129 | } | |
130 | } | |
131 | ||
132 | error: | |
133 | return ret; | |
134 | } | |
135 | ||
136 | static | |
137 | int _mkdir(void *_data) | |
138 | { | |
e11d277b | 139 | struct run_as_mkdir_data *data = _data; |
60b6c79c MD |
140 | return mkdir(data->path, data->mode); |
141 | } | |
142 | ||
143 | static | |
144 | int _open(void *_data) | |
145 | { | |
e11d277b | 146 | struct run_as_open_data *data = _data; |
60b6c79c MD |
147 | return open(data->path, data->flags, data->mode); |
148 | } | |
149 | ||
c2b75c49 MD |
150 | static |
151 | int child_run_as(void *_data) | |
152 | { | |
153 | struct run_as_data *data = _data; | |
154 | size_t writelen, writeleft, index; | |
155 | union { | |
156 | int i; | |
157 | char c[sizeof(int)]; | |
158 | } sendret; | |
159 | int ret; | |
160 | ||
161 | /* | |
162 | * Child: it is safe to drop egid and euid while sharing the | |
163 | * file descriptors with the parent process, since we do not | |
164 | * drop "uid": therefore, the user we are dropping egid/euid to | |
165 | * cannot attach to this process with, e.g. ptrace, nor map this | |
166 | * process memory. | |
167 | */ | |
1576d582 MD |
168 | if (data->gid != getegid()) { |
169 | ret = setegid(data->gid); | |
170 | if (ret < 0) { | |
171 | perror("setegid"); | |
6da9f915 | 172 | return EXIT_FAILURE; |
1576d582 | 173 | } |
c2b75c49 | 174 | } |
1576d582 MD |
175 | if (data->uid != geteuid()) { |
176 | ret = seteuid(data->uid); | |
177 | if (ret < 0) { | |
178 | perror("seteuid"); | |
6da9f915 | 179 | return EXIT_FAILURE; |
1576d582 | 180 | } |
c2b75c49 MD |
181 | } |
182 | /* | |
183 | * Also set umask to 0 for mkdir executable bit. | |
184 | */ | |
185 | umask(0); | |
186 | sendret.i = (*data->cmd)(data->data); | |
187 | /* send back return value */ | |
188 | writeleft = sizeof(sendret); | |
189 | index = 0; | |
190 | do { | |
191 | writelen = write(data->retval_pipe, &sendret.c[index], | |
192 | writeleft); | |
193 | if (writelen < 0) { | |
194 | perror("write"); | |
6da9f915 | 195 | return EXIT_FAILURE; |
c2b75c49 MD |
196 | } |
197 | writeleft -= writelen; | |
198 | index += writelen; | |
199 | } while (writeleft > 0); | |
6da9f915 | 200 | return EXIT_SUCCESS; |
c2b75c49 MD |
201 | } |
202 | ||
60b6c79c MD |
203 | static |
204 | int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) | |
205 | { | |
c2b75c49 | 206 | struct run_as_data run_as_data; |
60b6c79c | 207 | int ret = 0; |
c2b75c49 | 208 | int status; |
60b6c79c | 209 | pid_t pid; |
c2b75c49 MD |
210 | int retval_pipe[2]; |
211 | ssize_t readlen, readleft, index; | |
d5bd7573 | 212 | void *child_stack; |
c2b75c49 MD |
213 | union { |
214 | int i; | |
215 | char c[sizeof(int)]; | |
216 | } retval; | |
60b6c79c MD |
217 | |
218 | /* | |
219 | * If we are non-root, we can only deal with our own uid. | |
220 | */ | |
221 | if (geteuid() != 0) { | |
222 | if (uid != geteuid()) { | |
223 | ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)", | |
224 | uid, geteuid()); | |
225 | return -EPERM; | |
226 | } | |
60b6c79c MD |
227 | } |
228 | ||
c2b75c49 MD |
229 | ret = pipe(retval_pipe); |
230 | if (ret < 0) { | |
231 | perror("pipe"); | |
def5e0e8 | 232 | retval.i = ret; |
60b6c79c | 233 | goto end; |
c2b75c49 MD |
234 | } |
235 | run_as_data.data = data; | |
236 | run_as_data.cmd = cmd; | |
237 | run_as_data.uid = uid; | |
238 | run_as_data.gid = gid; | |
239 | run_as_data.retval_pipe = retval_pipe[1]; /* write end */ | |
e11d277b | 240 | child_stack = mmap(NULL, RUNAS_CHILD_STACK_SIZE, |
d5bd7573 MD |
241 | PROT_WRITE | PROT_READ, |
242 | MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | MAP_STACK, | |
243 | -1, 0); | |
244 | if (child_stack == MAP_FAILED) { | |
245 | perror("mmap"); | |
def5e0e8 | 246 | retval.i = -ENOMEM; |
d5bd7573 MD |
247 | goto close_pipe; |
248 | } | |
249 | /* | |
250 | * Pointing to the middle of the stack to support architectures | |
251 | * where the stack grows up (HPPA). | |
252 | */ | |
89831a74 MD |
253 | pid = lttng_clone_files(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2), |
254 | &run_as_data); | |
c2b75c49 MD |
255 | if (pid < 0) { |
256 | perror("clone"); | |
def5e0e8 | 257 | retval.i = pid; |
d5bd7573 | 258 | goto unmap_stack; |
c2b75c49 MD |
259 | } |
260 | /* receive return value */ | |
261 | readleft = sizeof(retval); | |
262 | index = 0; | |
263 | do { | |
264 | readlen = read(retval_pipe[0], &retval.c[index], readleft); | |
265 | if (readlen < 0) { | |
266 | perror("read"); | |
267 | ret = -1; | |
268 | break; | |
60b6c79c | 269 | } |
c2b75c49 MD |
270 | readleft -= readlen; |
271 | index += readlen; | |
272 | } while (readleft > 0); | |
273 | ||
274 | /* | |
275 | * Parent: wait for child to return, in which case the | |
276 | * shared memory map will have been created. | |
277 | */ | |
47fb7563 | 278 | pid = waitpid(pid, &status, 0); |
c2b75c49 MD |
279 | if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
280 | perror("wait"); | |
def5e0e8 | 281 | retval.i = -1; |
60b6c79c | 282 | } |
d5bd7573 | 283 | unmap_stack: |
e11d277b | 284 | ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE); |
d5bd7573 MD |
285 | if (ret < 0) { |
286 | perror("munmap"); | |
def5e0e8 | 287 | retval.i = ret; |
d5bd7573 | 288 | } |
c2b75c49 MD |
289 | close_pipe: |
290 | close(retval_pipe[0]); | |
291 | close(retval_pipe[1]); | |
60b6c79c | 292 | end: |
c2b75c49 | 293 | return retval.i; |
60b6c79c MD |
294 | } |
295 | ||
e11d277b | 296 | int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid) |
60b6c79c | 297 | { |
e11d277b | 298 | struct run_as_mkdir_data data; |
60b6c79c MD |
299 | |
300 | DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d", | |
301 | path, mode, uid, gid); | |
302 | data.path = path; | |
303 | data.mode = mode; | |
304 | return run_as(_mkdir_recursive, &data, uid, gid); | |
305 | } | |
306 | ||
e11d277b | 307 | int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) |
60b6c79c | 308 | { |
e11d277b | 309 | struct run_as_mkdir_data data; |
60b6c79c MD |
310 | |
311 | DBG3("mkdir() %s with mode %d for uid %d and gid %d", | |
312 | path, mode, uid, gid); | |
313 | data.path = path; | |
314 | data.mode = mode; | |
315 | return run_as(_mkdir, &data, uid, gid); | |
316 | } | |
317 | ||
318 | /* | |
319 | * Note: open_run_as is currently not working. We'd need to pass the fd | |
320 | * opened in the child to the parent. | |
321 | */ | |
e11d277b | 322 | int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid) |
60b6c79c | 323 | { |
e11d277b | 324 | struct run_as_open_data data; |
c2b75c49 | 325 | |
47fb7563 MD |
326 | DBG3("open() %s with flags %X mode %d for uid %d and gid %d", |
327 | path, flags, mode, uid, gid); | |
60b6c79c MD |
328 | data.path = path; |
329 | data.flags = flags; | |
330 | data.mode = mode; | |
331 | return run_as(_open, &data, uid, gid); | |
60b6c79c | 332 | } |