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 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.
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 with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307, 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
42 /* FreeBSD MAP_STACK always return -ENOMEM */
43 #define LTTNG_MAP_STACK 0
45 #define LTTNG_MAP_STACK MAP_STACK
49 #define MAP_GROWSDOWN 0
53 #define MAP_ANONYMOUS MAP_ANON
57 int (*cmd
)(void *data
);
64 struct run_as_mkdir_data
{
69 struct run_as_open_data
{
76 * Create recursively directory using the FULL path.
79 int _mkdir_recursive(void *_data
)
81 struct run_as_mkdir_data
*data
= _data
;
83 char *p
, tmp
[PATH_MAX
];
92 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
94 PERROR("snprintf mkdir");
99 if (tmp
[len
- 1] == '/') {
103 for (p
= tmp
+ 1; *p
; p
++) {
106 ret
= stat(tmp
, &statbuf
);
108 ret
= mkdir(tmp
, mode
);
110 if (!(errno
== EEXIST
)) {
111 PERROR("mkdir recursive");
121 ret
= mkdir(tmp
, mode
);
123 if (!(errno
== EEXIST
)) {
124 PERROR("mkdir recursive last piece");
136 int _mkdir(void *_data
)
138 struct run_as_mkdir_data
*data
= _data
;
139 return mkdir(data
->path
, data
->mode
);
143 int _open(void *_data
)
145 struct run_as_open_data
*data
= _data
;
146 return open(data
->path
, data
->flags
, data
->mode
);
150 int child_run_as(void *_data
)
152 struct run_as_data
*data
= _data
;
153 size_t writelen
, writeleft
, index
;
161 * Child: it is safe to drop egid and euid while sharing the
162 * file descriptors with the parent process, since we do not
163 * drop "uid": therefore, the user we are dropping egid/euid to
164 * cannot attach to this process with, e.g. ptrace, nor map this
167 if (data
->gid
!= getegid()) {
168 ret
= setegid(data
->gid
);
174 if (data
->uid
!= geteuid()) {
175 ret
= seteuid(data
->uid
);
182 * Also set umask to 0 for mkdir executable bit.
185 sendret
.i
= (*data
->cmd
)(data
->data
);
186 /* send back return value */
187 writeleft
= sizeof(sendret
);
190 writelen
= write(data
->retval_pipe
, &sendret
.c
[index
],
196 writeleft
-= writelen
;
198 } while (writeleft
> 0);
203 int run_as_clone(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
205 struct run_as_data run_as_data
;
210 ssize_t readlen
, readleft
, index
;
218 * If we are non-root, we can only deal with our own uid.
220 if (geteuid() != 0) {
221 if (uid
!= geteuid()) {
222 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
228 ret
= pipe(retval_pipe
);
234 run_as_data
.data
= data
;
235 run_as_data
.cmd
= cmd
;
236 run_as_data
.uid
= uid
;
237 run_as_data
.gid
= gid
;
238 run_as_data
.retval_pipe
= retval_pipe
[1]; /* write end */
239 child_stack
= mmap(NULL
, RUNAS_CHILD_STACK_SIZE
,
240 PROT_WRITE
| PROT_READ
,
241 MAP_PRIVATE
| MAP_GROWSDOWN
| MAP_ANONYMOUS
| LTTNG_MAP_STACK
,
243 if (child_stack
== MAP_FAILED
) {
249 * Pointing to the middle of the stack to support architectures
250 * where the stack grows up (HPPA).
252 pid
= lttng_clone_files(child_run_as
, child_stack
+ (RUNAS_CHILD_STACK_SIZE
/ 2),
259 /* receive return value */
260 readleft
= sizeof(retval
);
263 readlen
= read(retval_pipe
[0], &retval
.c
[index
], readleft
);
271 } while (readleft
> 0);
274 * Parent: wait for child to return, in which case the
275 * shared memory map will have been created.
277 pid
= waitpid(pid
, &status
, 0);
278 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
283 ret
= munmap(child_stack
, RUNAS_CHILD_STACK_SIZE
);
289 ret
= close(retval_pipe
[0]);
293 ret
= close(retval_pipe
[1]);
302 * To be used on setups where gdb has issues debugging programs using
303 * clone/rfork. Note that this is for debuging ONLY, and should not be
307 int run_as_noclone(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
313 int run_as(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
315 if (!getenv("LTTNG_DEBUG_NOCLONE")) {
316 DBG("Using run_as_clone");
317 return run_as_clone(cmd
, data
, uid
, gid
);
319 DBG("Using run_as_noclone");
320 return run_as_noclone(cmd
, data
, uid
, gid
);
324 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
326 struct run_as_mkdir_data data
;
328 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
329 path
, mode
, uid
, gid
);
332 return run_as(_mkdir_recursive
, &data
, uid
, gid
);
335 int run_as_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
337 struct run_as_mkdir_data data
;
339 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
340 path
, mode
, uid
, gid
);
343 return run_as(_mkdir
, &data
, uid
, gid
);
347 * Note: open_run_as is currently not working. We'd need to pass the fd
348 * opened in the child to the parent.
350 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
352 struct run_as_open_data data
;
354 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
355 path
, flags
, mode
, uid
, gid
);
359 return run_as(_open
, &data
, uid
, gid
);
This page took 0.047966 seconds and 4 git commands to generate.