Fix: run_as error handling
[lttng-tools.git] / src / common / runas.c
CommitLineData
60b6c79c
MD
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>
c2b75c49 30#include <sched.h>
730389d9 31#include <sys/signal.h>
60b6c79c 32
db758600 33#include <common/error.h>
730389d9
DG
34#include <common/compat/mman.h>
35#include <common/compat/clone.h>
60b6c79c 36
0857097f
DG
37#include "runas.h"
38
e11d277b 39#define RUNAS_CHILD_STACK_SIZE 10485760
c2b75c49 40
059e1d3d
MD
41#ifndef MAP_STACK
42#define MAP_STACK 0
43#endif
44
c2b75c49
MD
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
e11d277b 53struct run_as_mkdir_data {
60b6c79c
MD
54 const char *path;
55 mode_t mode;
56};
57
e11d277b 58struct run_as_open_data {
60b6c79c
MD
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{
e11d277b 70 struct run_as_mkdir_data *data = _data;
60b6c79c
MD
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{
e11d277b 127 struct run_as_mkdir_data *data = _data;
60b6c79c
MD
128 return mkdir(data->path, data->mode);
129}
130
131static
132int _open(void *_data)
133{
e11d277b 134 struct run_as_open_data *data = _data;
60b6c79c
MD
135 return open(data->path, data->flags, data->mode);
136}
137
c2b75c49
MD
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 */
1576d582
MD
156 if (data->gid != getegid()) {
157 ret = setegid(data->gid);
158 if (ret < 0) {
159 perror("setegid");
6da9f915 160 return EXIT_FAILURE;
1576d582 161 }
c2b75c49 162 }
1576d582
MD
163 if (data->uid != geteuid()) {
164 ret = seteuid(data->uid);
165 if (ret < 0) {
166 perror("seteuid");
6da9f915 167 return EXIT_FAILURE;
1576d582 168 }
c2b75c49
MD
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");
6da9f915 183 return EXIT_FAILURE;
c2b75c49
MD
184 }
185 writeleft -= writelen;
186 index += writelen;
187 } while (writeleft > 0);
6da9f915 188 return EXIT_SUCCESS;
c2b75c49
MD
189}
190
60b6c79c
MD
191static
192int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
193{
c2b75c49 194 struct run_as_data run_as_data;
60b6c79c 195 int ret = 0;
c2b75c49 196 int status;
60b6c79c 197 pid_t pid;
c2b75c49
MD
198 int retval_pipe[2];
199 ssize_t readlen, readleft, index;
d5bd7573 200 void *child_stack;
c2b75c49
MD
201 union {
202 int i;
203 char c[sizeof(int)];
204 } retval;
60b6c79c
MD
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 }
60b6c79c
MD
215 }
216
c2b75c49
MD
217 ret = pipe(retval_pipe);
218 if (ret < 0) {
219 perror("pipe");
def5e0e8 220 retval.i = ret;
60b6c79c 221 goto end;
c2b75c49
MD
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 */
e11d277b 228 child_stack = mmap(NULL, RUNAS_CHILD_STACK_SIZE,
d5bd7573
MD
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");
def5e0e8 234 retval.i = -ENOMEM;
d5bd7573
MD
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 */
89831a74
MD
241 pid = lttng_clone_files(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2),
242 &run_as_data);
c2b75c49
MD
243 if (pid < 0) {
244 perror("clone");
def5e0e8 245 retval.i = pid;
d5bd7573 246 goto unmap_stack;
c2b75c49
MD
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;
60b6c79c 257 }
c2b75c49
MD
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 */
47fb7563 266 pid = waitpid(pid, &status, 0);
c2b75c49
MD
267 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
268 perror("wait");
def5e0e8 269 retval.i = -1;
60b6c79c 270 }
d5bd7573 271unmap_stack:
e11d277b 272 ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE);
d5bd7573
MD
273 if (ret < 0) {
274 perror("munmap");
def5e0e8 275 retval.i = ret;
d5bd7573 276 }
c2b75c49
MD
277close_pipe:
278 close(retval_pipe[0]);
279 close(retval_pipe[1]);
60b6c79c 280end:
c2b75c49 281 return retval.i;
60b6c79c
MD
282}
283
e11d277b 284int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
60b6c79c 285{
e11d277b 286 struct run_as_mkdir_data data;
60b6c79c
MD
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
e11d277b 295int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
60b6c79c 296{
e11d277b 297 struct run_as_mkdir_data data;
60b6c79c
MD
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 */
e11d277b 310int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
60b6c79c 311{
e11d277b 312 struct run_as_open_data data;
c2b75c49 313
47fb7563
MD
314 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
315 path, flags, mode, uid, gid);
60b6c79c
MD
316 data.path = path;
317 data.flags = flags;
318 data.mode = mode;
319 return run_as(_open, &data, uid, gid);
60b6c79c 320}
This page took 0.06187 seconds and 4 git commands to generate.