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