2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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
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.
26 #include <sys/types.h>
31 #include <sys/signal.h>
33 #include <common/error.h>
34 #include <common/compat/mman.h>
35 #include <common/compat/clone.h>
39 #define RUNAS_CHILD_STACK_SIZE 10485760
46 /* FreeBSD MAP_STACK always return -ENOMEM */
47 #define LTTNG_MAP_STACK 0
49 #define LTTNG_MAP_STACK MAP_STACK
53 #define MAP_GROWSDOWN 0
57 #define MAP_ANONYMOUS MAP_ANON
61 int (*cmd
)(void *data
);
68 struct run_as_mkdir_data
{
73 struct run_as_open_data
{
80 * Create recursively directory using the FULL path.
83 int _mkdir_recursive(void *_data
)
85 struct run_as_mkdir_data
*data
= _data
;
87 char *p
, tmp
[PATH_MAX
];
96 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
98 PERROR("snprintf mkdir");
103 if (tmp
[len
- 1] == '/') {
107 for (p
= tmp
+ 1; *p
; p
++) {
110 ret
= stat(tmp
, &statbuf
);
112 ret
= mkdir(tmp
, mode
);
114 if (!(errno
== EEXIST
)) {
115 PERROR("mkdir recursive");
125 ret
= mkdir(tmp
, mode
);
127 if (!(errno
== EEXIST
)) {
128 PERROR("mkdir recursive last piece");
140 int _mkdir(void *_data
)
142 struct run_as_mkdir_data
*data
= _data
;
143 return mkdir(data
->path
, data
->mode
);
147 int _open(void *_data
)
149 struct run_as_open_data
*data
= _data
;
150 return open(data
->path
, data
->flags
, data
->mode
);
154 int child_run_as(void *_data
)
157 struct run_as_data
*data
= _data
;
159 size_t writeleft
, index
;
166 * Child: it is safe to drop egid and euid while sharing the
167 * file descriptors with the parent process, since we do not
168 * drop "uid": therefore, the user we are dropping egid/euid to
169 * cannot attach to this process with, e.g. ptrace, nor map this
172 if (data
->gid
!= getegid()) {
173 ret
= setegid(data
->gid
);
179 if (data
->uid
!= geteuid()) {
180 ret
= seteuid(data
->uid
);
187 * Also set umask to 0 for mkdir executable bit.
190 sendret
.i
= (*data
->cmd
)(data
->data
);
191 /* send back return value */
192 writeleft
= sizeof(sendret
);
196 writelen
= write(data
->retval_pipe
, &sendret
.c
[index
],
198 } while (writelen
< 0 && errno
== EINTR
);
203 writeleft
-= writelen
;
205 } while (writeleft
> 0);
210 int run_as_clone(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
212 struct run_as_data run_as_data
;
217 ssize_t readlen
, readleft
, index
;
225 * If we are non-root, we can only deal with our own uid.
227 if (geteuid() != 0) {
228 if (uid
!= geteuid()) {
229 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
235 ret
= pipe(retval_pipe
);
241 run_as_data
.data
= data
;
242 run_as_data
.cmd
= cmd
;
243 run_as_data
.uid
= uid
;
244 run_as_data
.gid
= gid
;
245 run_as_data
.retval_pipe
= retval_pipe
[1]; /* write end */
246 child_stack
= mmap(NULL
, RUNAS_CHILD_STACK_SIZE
,
247 PROT_WRITE
| PROT_READ
,
248 MAP_PRIVATE
| MAP_GROWSDOWN
| MAP_ANONYMOUS
| LTTNG_MAP_STACK
,
250 if (child_stack
== MAP_FAILED
) {
256 * Pointing to the middle of the stack to support architectures
257 * where the stack grows up (HPPA).
259 pid
= lttng_clone_files(child_run_as
, child_stack
+ (RUNAS_CHILD_STACK_SIZE
/ 2),
266 /* receive return value */
267 readleft
= sizeof(retval
);
270 readlen
= read(retval_pipe
[0], &retval
.c
[index
], readleft
);
278 } while (readleft
> 0);
281 * Parent: wait for child to return, in which case the
282 * shared memory map will have been created.
284 pid
= waitpid(pid
, &status
, 0);
285 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
290 ret
= munmap(child_stack
, RUNAS_CHILD_STACK_SIZE
);
296 ret
= close(retval_pipe
[0]);
300 ret
= close(retval_pipe
[1]);
309 * To be used on setups where gdb has issues debugging programs using
310 * clone/rfork. Note that this is for debuging ONLY, and should not be
314 int run_as_noclone(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
327 int run_as(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
329 if (!getenv("LTTNG_DEBUG_NOCLONE")) {
332 DBG("Using run_as_clone");
333 pthread_mutex_lock(<tng_libc_state_lock
);
334 ret
= run_as_clone(cmd
, data
, uid
, gid
);
335 pthread_mutex_unlock(<tng_libc_state_lock
);
338 DBG("Using run_as_noclone");
339 return run_as_noclone(cmd
, data
, uid
, gid
);
343 __attribute__((visibility("hidden")))
344 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
346 struct run_as_mkdir_data data
;
348 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
349 path
, mode
, uid
, gid
);
352 return run_as(_mkdir_recursive
, &data
, uid
, gid
);
355 __attribute__((visibility("hidden")))
356 int run_as_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
358 struct run_as_mkdir_data data
;
360 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
361 path
, mode
, uid
, gid
);
364 return run_as(_mkdir
, &data
, uid
, gid
);
368 * Note: open_run_as is currently not working. We'd need to pass the fd
369 * opened in the child to the parent.
371 __attribute__((visibility("hidden")))
372 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
374 struct run_as_open_data data
;
376 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
377 path
, flags
, mode
, uid
, gid
);
381 return run_as(_open
, &data
, uid
, gid
);
This page took 0.05834 seconds and 4 git commands to generate.