ddb2f18bf62c34f3508dfbae8c0a1d080dec4c69
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
50 #define MAP_ANONYMOUS MAP_ANON
54 int (*cmd
)(void *data
);
61 struct run_as_mkdir_data
{
66 struct run_as_open_data
{
73 * Create recursively directory using the FULL path.
76 int _mkdir_recursive(void *_data
)
78 struct run_as_mkdir_data
*data
= _data
;
80 char *p
, tmp
[PATH_MAX
];
89 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
91 PERROR("snprintf mkdir");
96 if (tmp
[len
- 1] == '/') {
100 for (p
= tmp
+ 1; *p
; p
++) {
103 ret
= stat(tmp
, &statbuf
);
105 ret
= mkdir(tmp
, mode
);
107 if (!(errno
== EEXIST
)) {
108 PERROR("mkdir recursive");
118 ret
= mkdir(tmp
, mode
);
120 if (!(errno
== EEXIST
)) {
121 PERROR("mkdir recursive last piece");
133 int _mkdir(void *_data
)
135 struct run_as_mkdir_data
*data
= _data
;
136 return mkdir(data
->path
, data
->mode
);
140 int _open(void *_data
)
142 struct run_as_open_data
*data
= _data
;
143 return open(data
->path
, data
->flags
, data
->mode
);
147 int child_run_as(void *_data
)
149 struct run_as_data
*data
= _data
;
150 size_t writelen
, writeleft
, index
;
158 * Child: it is safe to drop egid and euid while sharing the
159 * file descriptors with the parent process, since we do not
160 * drop "uid": therefore, the user we are dropping egid/euid to
161 * cannot attach to this process with, e.g. ptrace, nor map this
164 if (data
->gid
!= getegid()) {
165 ret
= setegid(data
->gid
);
171 if (data
->uid
!= geteuid()) {
172 ret
= seteuid(data
->uid
);
179 * Also set umask to 0 for mkdir executable bit.
182 sendret
.i
= (*data
->cmd
)(data
->data
);
183 /* send back return value */
184 writeleft
= sizeof(sendret
);
187 writelen
= write(data
->retval_pipe
, &sendret
.c
[index
],
193 writeleft
-= writelen
;
195 } while (writeleft
> 0);
200 int run_as(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
202 struct run_as_data run_as_data
;
207 ssize_t readlen
, readleft
, index
;
215 * If we are non-root, we can only deal with our own uid.
217 if (geteuid() != 0) {
218 if (uid
!= geteuid()) {
219 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
225 ret
= pipe(retval_pipe
);
231 run_as_data
.data
= data
;
232 run_as_data
.cmd
= cmd
;
233 run_as_data
.uid
= uid
;
234 run_as_data
.gid
= gid
;
235 run_as_data
.retval_pipe
= retval_pipe
[1]; /* write end */
236 child_stack
= mmap(NULL
, RUNAS_CHILD_STACK_SIZE
,
237 PROT_WRITE
| PROT_READ
,
238 MAP_PRIVATE
| MAP_GROWSDOWN
| MAP_ANONYMOUS
| MAP_STACK
,
240 if (child_stack
== MAP_FAILED
) {
246 * Pointing to the middle of the stack to support architectures
247 * where the stack grows up (HPPA).
249 pid
= lttng_clone_files(child_run_as
, child_stack
+ (RUNAS_CHILD_STACK_SIZE
/ 2),
256 /* receive return value */
257 readleft
= sizeof(retval
);
260 readlen
= read(retval_pipe
[0], &retval
.c
[index
], readleft
);
268 } while (readleft
> 0);
271 * Parent: wait for child to return, in which case the
272 * shared memory map will have been created.
274 pid
= waitpid(pid
, &status
, 0);
275 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
280 ret
= munmap(child_stack
, RUNAS_CHILD_STACK_SIZE
);
286 close(retval_pipe
[0]);
287 close(retval_pipe
[1]);
292 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
294 struct run_as_mkdir_data data
;
296 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
297 path
, mode
, uid
, gid
);
300 return run_as(_mkdir_recursive
, &data
, uid
, gid
);
303 int run_as_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
305 struct run_as_mkdir_data data
;
307 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
308 path
, mode
, uid
, gid
);
311 return run_as(_mkdir
, &data
, uid
, gid
);
315 * Note: open_run_as is currently not working. We'd need to pass the fd
316 * opened in the child to the parent.
318 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
320 struct run_as_open_data data
;
322 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
323 path
, flags
, mode
, uid
, gid
);
327 return run_as(_open
, &data
, uid
, gid
);
This page took 0.04194 seconds and 4 git commands to generate.