Accept uid and gid parameters in utils_mkdir()/utils_mkdir_recursive()
[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 *
d14d33bf
AM
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
60b6c79c
MD
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
d14d33bf 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
60b6c79c
MD
12 * more details.
13 *
d14d33bf
AM
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
60b6c79c
MD
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
90e535ef 33#include <common/common.h>
3fd15a74 34#include <common/utils.h>
730389d9
DG
35#include <common/compat/mman.h>
36#include <common/compat/clone.h>
60b6c79c 37
0857097f
DG
38#include "runas.h"
39
e11d277b 40#define RUNAS_CHILD_STACK_SIZE 10485760
c2b75c49 41
1e4035fc
AS
42#ifndef MAP_STACK
43#define MAP_STACK 0
44#endif
45
0756d557
MD
46#ifdef __FreeBSD__
47/* FreeBSD MAP_STACK always return -ENOMEM */
48#define LTTNG_MAP_STACK 0
49#else
50#define LTTNG_MAP_STACK MAP_STACK
059e1d3d
MD
51#endif
52
a5ae566a 53#ifndef MAP_GROWSDOWN
edb025e8 54#define MAP_GROWSDOWN 0
a5ae566a
MD
55#endif
56
57#ifndef MAP_ANONYMOUS
58#define MAP_ANONYMOUS MAP_ANON
59#endif
60
c2b75c49
MD
61struct run_as_data {
62 int (*cmd)(void *data);
63 void *data;
64 uid_t uid;
65 gid_t gid;
66 int retval_pipe;
67};
68
e11d277b 69struct run_as_mkdir_data {
60b6c79c
MD
70 const char *path;
71 mode_t mode;
72};
73
e11d277b 74struct run_as_open_data {
60b6c79c
MD
75 const char *path;
76 int flags;
77 mode_t mode;
78};
79
8f0044bf
MD
80#ifdef VALGRIND
81static
82int use_clone(void)
83{
84 return 0;
85}
86#else
87static
88int use_clone(void)
89{
90 return !getenv("LTTNG_DEBUG_NOCLONE");
91}
92#endif
93
0d083fb8
JG
94LTTNG_HIDDEN
95int _utils_mkdir_recursive_unsafe(const char *path, mode_t mode);
96
60b6c79c
MD
97/*
98 * Create recursively directory using the FULL path.
99 */
100static
101int _mkdir_recursive(void *_data)
102{
e11d277b 103 struct run_as_mkdir_data *data = _data;
60b6c79c 104 const char *path;
60b6c79c 105 mode_t mode;
60b6c79c
MD
106
107 path = data->path;
108 mode = data->mode;
109
0d083fb8
JG
110 /* Safe to call as we have transitioned to the requested uid/gid. */
111 return _utils_mkdir_recursive_unsafe(path, mode);
60b6c79c
MD
112}
113
114static
115int _mkdir(void *_data)
116{
7ce36756 117 int ret;
e11d277b 118 struct run_as_mkdir_data *data = _data;
7ce36756
DG
119
120 ret = mkdir(data->path, data->mode);
121 if (ret < 0) {
122 ret = -errno;
123 }
124
125 return ret;
60b6c79c
MD
126}
127
128static
129int _open(void *_data)
130{
e11d277b 131 struct run_as_open_data *data = _data;
60b6c79c
MD
132 return open(data->path, data->flags, data->mode);
133}
134
c2b75c49
MD
135static
136int child_run_as(void *_data)
137{
6775595e 138 int ret;
c2b75c49 139 struct run_as_data *data = _data;
6775595e 140 ssize_t writelen;
6cd525e8 141 int sendret;
c2b75c49
MD
142
143 /*
144 * Child: it is safe to drop egid and euid while sharing the
145 * file descriptors with the parent process, since we do not
146 * drop "uid": therefore, the user we are dropping egid/euid to
147 * cannot attach to this process with, e.g. ptrace, nor map this
148 * process memory.
149 */
1576d582
MD
150 if (data->gid != getegid()) {
151 ret = setegid(data->gid);
152 if (ret < 0) {
4c462e79 153 PERROR("setegid");
6cd525e8 154 sendret = -1;
6d73c4ef 155 goto write_return;
1576d582 156 }
c2b75c49 157 }
1576d582
MD
158 if (data->uid != geteuid()) {
159 ret = seteuid(data->uid);
160 if (ret < 0) {
4c462e79 161 PERROR("seteuid");
6cd525e8 162 sendret = -1;
6d73c4ef 163 goto write_return;
1576d582 164 }
c2b75c49
MD
165 }
166 /*
167 * Also set umask to 0 for mkdir executable bit.
168 */
169 umask(0);
6cd525e8 170 sendret = (*data->cmd)(data->data);
6d73c4ef
MD
171
172write_return:
c2b75c49 173 /* send back return value */
6cd525e8
MD
174 writelen = lttng_write(data->retval_pipe, &sendret, sizeof(sendret));
175 if (writelen < sizeof(sendret)) {
176 PERROR("lttng_write error");
177 return EXIT_FAILURE;
178 } else {
179 return EXIT_SUCCESS;
180 }
c2b75c49
MD
181}
182
60b6c79c 183static
2d85a600 184int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
60b6c79c 185{
c2b75c49 186 struct run_as_data run_as_data;
60b6c79c 187 int ret = 0;
6cd525e8 188 ssize_t readlen;
c2b75c49 189 int status;
60b6c79c 190 pid_t pid;
c2b75c49 191 int retval_pipe[2];
d5bd7573 192 void *child_stack;
6cd525e8 193 int retval;
60b6c79c
MD
194
195 /*
196 * If we are non-root, we can only deal with our own uid.
197 */
198 if (geteuid() != 0) {
199 if (uid != geteuid()) {
200 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
201 uid, geteuid());
202 return -EPERM;
203 }
60b6c79c
MD
204 }
205
c2b75c49
MD
206 ret = pipe(retval_pipe);
207 if (ret < 0) {
4c462e79 208 PERROR("pipe");
6cd525e8 209 retval = ret;
60b6c79c 210 goto end;
c2b75c49
MD
211 }
212 run_as_data.data = data;
213 run_as_data.cmd = cmd;
214 run_as_data.uid = uid;
215 run_as_data.gid = gid;
216 run_as_data.retval_pipe = retval_pipe[1]; /* write end */
e11d277b 217 child_stack = mmap(NULL, RUNAS_CHILD_STACK_SIZE,
d5bd7573 218 PROT_WRITE | PROT_READ,
0756d557 219 MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | LTTNG_MAP_STACK,
d5bd7573
MD
220 -1, 0);
221 if (child_stack == MAP_FAILED) {
4c462e79 222 PERROR("mmap");
6cd525e8 223 retval = -ENOMEM;
d5bd7573
MD
224 goto close_pipe;
225 }
226 /*
227 * Pointing to the middle of the stack to support architectures
228 * where the stack grows up (HPPA).
229 */
89831a74
MD
230 pid = lttng_clone_files(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2),
231 &run_as_data);
c2b75c49 232 if (pid < 0) {
4c462e79 233 PERROR("clone");
6cd525e8 234 retval = pid;
d5bd7573 235 goto unmap_stack;
c2b75c49
MD
236 }
237 /* receive return value */
6cd525e8
MD
238 readlen = lttng_read(retval_pipe[0], &retval, sizeof(retval));
239 if (readlen < sizeof(retval)) {
240 ret = -1;
241 }
c2b75c49
MD
242
243 /*
244 * Parent: wait for child to return, in which case the
245 * shared memory map will have been created.
246 */
47fb7563 247 pid = waitpid(pid, &status, 0);
c2b75c49 248 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
4c462e79 249 PERROR("wait");
6cd525e8 250 retval = -1;
60b6c79c 251 }
d5bd7573 252unmap_stack:
e11d277b 253 ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE);
d5bd7573 254 if (ret < 0) {
4c462e79 255 PERROR("munmap");
6cd525e8 256 retval = ret;
d5bd7573 257 }
c2b75c49 258close_pipe:
4c462e79
MD
259 ret = close(retval_pipe[0]);
260 if (ret) {
261 PERROR("close");
262 }
263 ret = close(retval_pipe[1]);
264 if (ret) {
265 PERROR("close");
266 }
60b6c79c 267end:
6cd525e8 268 return retval;
60b6c79c
MD
269}
270
2d85a600
MD
271/*
272 * To be used on setups where gdb has issues debugging programs using
273 * clone/rfork. Note that this is for debuging ONLY, and should not be
274 * considered secure.
275 */
276static
277int run_as_noclone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
278{
5b73926f
DG
279 int ret;
280 mode_t old_mask;
281
282 old_mask = umask(0);
283 ret = cmd(data);
284 umask(old_mask);
285
286 return ret;
2d85a600
MD
287}
288
289static
290int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
291{
8f0044bf 292 if (use_clone()) {
9ef70f87
MD
293 int ret;
294
2d85a600 295 DBG("Using run_as_clone");
9ef70f87
MD
296 pthread_mutex_lock(&lttng_libc_state_lock);
297 ret = run_as_clone(cmd, data, uid, gid);
298 pthread_mutex_unlock(&lttng_libc_state_lock);
299 return ret;
2d85a600
MD
300 } else {
301 DBG("Using run_as_noclone");
302 return run_as_noclone(cmd, data, uid, gid);
303 }
304}
305
90e535ef 306LTTNG_HIDDEN
e11d277b 307int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
60b6c79c 308{
e11d277b 309 struct run_as_mkdir_data data;
60b6c79c
MD
310
311 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
312 path, mode, uid, gid);
313 data.path = path;
314 data.mode = mode;
315 return run_as(_mkdir_recursive, &data, uid, gid);
316}
317
90e535ef 318LTTNG_HIDDEN
e11d277b 319int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
60b6c79c 320{
e11d277b 321 struct run_as_mkdir_data data;
60b6c79c
MD
322
323 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
324 path, mode, uid, gid);
325 data.path = path;
326 data.mode = mode;
327 return run_as(_mkdir, &data, uid, gid);
328}
329
330/*
331 * Note: open_run_as is currently not working. We'd need to pass the fd
332 * opened in the child to the parent.
333 */
90e535ef 334LTTNG_HIDDEN
e11d277b 335int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
60b6c79c 336{
e11d277b 337 struct run_as_open_data data;
c2b75c49 338
47fb7563
MD
339 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
340 path, flags, mode, uid, gid);
60b6c79c
MD
341 data.path = path;
342 data.flags = flags;
343 data.mode = mode;
344 return run_as(_open, &data, uid, gid);
60b6c79c 345}
This page took 0.075696 seconds and 4 git commands to generate.