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