Fix: run_as error handling
[lttng-tools.git] / src / common / runas.c
... / ...
CommitLineData
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
45struct 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
53struct run_as_mkdir_data {
54 const char *path;
55 mode_t mode;
56};
57
58struct 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 */
67static
68int _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
120error:
121 return ret;
122}
123
124static
125int _mkdir(void *_data)
126{
127 struct run_as_mkdir_data *data = _data;
128 return mkdir(data->path, data->mode);
129}
130
131static
132int _open(void *_data)
133{
134 struct run_as_open_data *data = _data;
135 return open(data->path, data->flags, data->mode);
136}
137
138static
139int 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
191static
192int 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 retval.i = ret;
221 goto end;
222 }
223 run_as_data.data = data;
224 run_as_data.cmd = cmd;
225 run_as_data.uid = uid;
226 run_as_data.gid = gid;
227 run_as_data.retval_pipe = retval_pipe[1]; /* write end */
228 child_stack = mmap(NULL, RUNAS_CHILD_STACK_SIZE,
229 PROT_WRITE | PROT_READ,
230 MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | MAP_STACK,
231 -1, 0);
232 if (child_stack == MAP_FAILED) {
233 perror("mmap");
234 retval.i = -ENOMEM;
235 goto close_pipe;
236 }
237 /*
238 * Pointing to the middle of the stack to support architectures
239 * where the stack grows up (HPPA).
240 */
241 pid = lttng_clone_files(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2),
242 &run_as_data);
243 if (pid < 0) {
244 perror("clone");
245 retval.i = 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 retval.i = -1;
270 }
271unmap_stack:
272 ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE);
273 if (ret < 0) {
274 perror("munmap");
275 retval.i = ret;
276 }
277close_pipe:
278 close(retval_pipe[0]);
279 close(retval_pipe[1]);
280end:
281 return retval.i;
282}
283
284int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
285{
286 struct run_as_mkdir_data data;
287
288 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
289 path, mode, uid, gid);
290 data.path = path;
291 data.mode = mode;
292 return run_as(_mkdir_recursive, &data, uid, gid);
293}
294
295int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
296{
297 struct run_as_mkdir_data data;
298
299 DBG3("mkdir() %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, &data, uid, gid);
304}
305
306/*
307 * Note: open_run_as is currently not working. We'd need to pass the fd
308 * opened in the child to the parent.
309 */
310int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
311{
312 struct run_as_open_data data;
313
314 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
315 path, flags, mode, uid, gid);
316 data.path = path;
317 data.flags = flags;
318 data.mode = mode;
319 return run_as(_open, &data, uid, gid);
320}
This page took 0.023015 seconds and 4 git commands to generate.