bee107951d8664e6f111ed5ff260574407f0c3a3
[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 = clone(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2),
241 CLONE_FILES | SIGCHLD,
242 &run_as_data, NULL);
243 if (pid < 0) {
244 perror("clone");
245 ret = pid;
246 goto unmap_stack;
247 }
248 /* receive return value */
249 readleft = sizeof(retval);
250 index = 0;
251 do {
252 readlen = read(retval_pipe[0], &retval.c[index], readleft);
253 if (readlen < 0) {
254 perror("read");
255 ret = -1;
256 break;
257 }
258 readleft -= readlen;
259 index += readlen;
260 } while (readleft > 0);
261
262 /*
263 * Parent: wait for child to return, in which case the
264 * shared memory map will have been created.
265 */
266 pid = waitpid(pid, &status, 0);
267 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
268 perror("wait");
269 ret = -1;
270 }
271 unmap_stack:
272 ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE);
273 if (ret < 0) {
274 perror("munmap");
275 }
276 close_pipe:
277 close(retval_pipe[0]);
278 close(retval_pipe[1]);
279 end:
280 return retval.i;
281 }
282
283 int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
284 {
285 struct run_as_mkdir_data data;
286
287 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
288 path, mode, uid, gid);
289 data.path = path;
290 data.mode = mode;
291 return run_as(_mkdir_recursive, &data, uid, gid);
292 }
293
294 int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
295 {
296 struct run_as_mkdir_data data;
297
298 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
299 path, mode, uid, gid);
300 data.path = path;
301 data.mode = mode;
302 return run_as(_mkdir, &data, uid, gid);
303 }
304
305 /*
306 * Note: open_run_as is currently not working. We'd need to pass the fd
307 * opened in the child to the parent.
308 */
309 int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
310 {
311 struct run_as_open_data data;
312
313 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
314 path, flags, mode, uid, gid);
315 data.path = path;
316 data.flags = flags;
317 data.mode = mode;
318 return run_as(_open, &data, uid, gid);
319 }
This page took 0.054937 seconds and 3 git commands to generate.