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