freebsd: do not use MAP_STACK
[lttng-tools.git] / src / common / runas.c
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>
30 #include <sched.h>
31 #include <sys/signal.h>
32
33 #include <common/error.h>
34 #include <common/compat/mman.h>
35 #include <common/compat/clone.h>
36
37 #include "runas.h"
38
39 #define RUNAS_CHILD_STACK_SIZE 10485760
40
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
47 #endif
48
49 #ifndef MAP_GROWSDOWN
50 #define MAP_GROWSDOWN 0
51 #endif
52
53 #ifndef MAP_ANONYMOUS
54 #define MAP_ANONYMOUS MAP_ANON
55 #endif
56
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
65 struct run_as_mkdir_data {
66 const char *path;
67 mode_t mode;
68 };
69
70 struct run_as_open_data {
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 {
82 struct run_as_mkdir_data *data = _data;
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 {
139 struct run_as_mkdir_data *data = _data;
140 return mkdir(data->path, data->mode);
141 }
142
143 static
144 int _open(void *_data)
145 {
146 struct run_as_open_data *data = _data;
147 return open(data->path, data->flags, data->mode);
148 }
149
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 */
168 if (data->gid != getegid()) {
169 ret = setegid(data->gid);
170 if (ret < 0) {
171 perror("setegid");
172 return EXIT_FAILURE;
173 }
174 }
175 if (data->uid != geteuid()) {
176 ret = seteuid(data->uid);
177 if (ret < 0) {
178 perror("seteuid");
179 return EXIT_FAILURE;
180 }
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");
195 return EXIT_FAILURE;
196 }
197 writeleft -= writelen;
198 index += writelen;
199 } while (writeleft > 0);
200 return EXIT_SUCCESS;
201 }
202
203 static
204 int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
205 {
206 struct run_as_data run_as_data;
207 int ret = 0;
208 int status;
209 pid_t pid;
210 int retval_pipe[2];
211 ssize_t readlen, readleft, index;
212 void *child_stack;
213 union {
214 int i;
215 char c[sizeof(int)];
216 } retval;
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 }
227 }
228
229 ret = pipe(retval_pipe);
230 if (ret < 0) {
231 perror("pipe");
232 retval.i = ret;
233 goto end;
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 */
240 child_stack = mmap(NULL, RUNAS_CHILD_STACK_SIZE,
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");
246 retval.i = -ENOMEM;
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 */
253 pid = lttng_clone_files(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2),
254 &run_as_data);
255 if (pid < 0) {
256 perror("clone");
257 retval.i = pid;
258 goto unmap_stack;
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;
269 }
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 */
278 pid = waitpid(pid, &status, 0);
279 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
280 perror("wait");
281 retval.i = -1;
282 }
283 unmap_stack:
284 ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE);
285 if (ret < 0) {
286 perror("munmap");
287 retval.i = ret;
288 }
289 close_pipe:
290 close(retval_pipe[0]);
291 close(retval_pipe[1]);
292 end:
293 return retval.i;
294 }
295
296 int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
297 {
298 struct run_as_mkdir_data data;
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
307 int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
308 {
309 struct run_as_mkdir_data data;
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 */
322 int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
323 {
324 struct run_as_open_data data;
325
326 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
327 path, flags, mode, uid, gid);
328 data.path = path;
329 data.flags = flags;
330 data.mode = mode;
331 return run_as(_open, &data, uid, gid);
332 }
This page took 0.036429 seconds and 5 git commands to generate.