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
45 #else /* FreeBSD MAP_STACK always return -ENOMEM */
50 #define MAP_GROWSDOWN 0
54 #define MAP_ANONYMOUS MAP_ANON
58 int (*cmd
)(void *data
);
65 struct run_as_mkdir_data
{
70 struct run_as_open_data
{
77 * Create recursively directory using the FULL path.
80 int _mkdir_recursive(void *_data
)
82 struct run_as_mkdir_data
*data
= _data
;
84 char *p
, tmp
[PATH_MAX
];
93 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
95 PERROR("snprintf mkdir");
100 if (tmp
[len
- 1] == '/') {
104 for (p
= tmp
+ 1; *p
; p
++) {
107 ret
= stat(tmp
, &statbuf
);
109 ret
= mkdir(tmp
, mode
);
111 if (!(errno
== EEXIST
)) {
112 PERROR("mkdir recursive");
122 ret
= mkdir(tmp
, mode
);
124 if (!(errno
== EEXIST
)) {
125 PERROR("mkdir recursive last piece");
137 int _mkdir(void *_data
)
139 struct run_as_mkdir_data
*data
= _data
;
140 return mkdir(data
->path
, data
->mode
);
144 int _open(void *_data
)
146 struct run_as_open_data
*data
= _data
;
147 return open(data
->path
, data
->flags
, data
->mode
);
151 int child_run_as(void *_data
)
153 struct run_as_data
*data
= _data
;
154 size_t writelen
, writeleft
, index
;
162 * Child: it is safe to drop egid and euid while sharing the
163 * file descriptors with the parent process, since we do not
164 * drop "uid": therefore, the user we are dropping egid/euid to
165 * cannot attach to this process with, e.g. ptrace, nor map this
168 if (data
->gid
!= getegid()) {
169 ret
= setegid(data
->gid
);
175 if (data
->uid
!= geteuid()) {
176 ret
= seteuid(data
->uid
);
183 * Also set umask to 0 for mkdir executable bit.
186 sendret
.i
= (*data
->cmd
)(data
->data
);
187 /* send back return value */
188 writeleft
= sizeof(sendret
);
191 writelen
= write(data
->retval_pipe
, &sendret
.c
[index
],
197 writeleft
-= writelen
;
199 } while (writeleft
> 0);
204 int run_as(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
206 struct run_as_data run_as_data
;
211 ssize_t readlen
, readleft
, index
;
219 * If we are non-root, we can only deal with our own uid.
221 if (geteuid() != 0) {
222 if (uid
!= geteuid()) {
223 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
229 ret
= pipe(retval_pipe
);
235 run_as_data
.data
= data
;
236 run_as_data
.cmd
= cmd
;
237 run_as_data
.uid
= uid
;
238 run_as_data
.gid
= gid
;
239 run_as_data
.retval_pipe
= retval_pipe
[1]; /* write end */
240 child_stack
= mmap(NULL
, RUNAS_CHILD_STACK_SIZE
,
241 PROT_WRITE
| PROT_READ
,
242 MAP_PRIVATE
| MAP_GROWSDOWN
| MAP_ANONYMOUS
| MAP_STACK
,
244 if (child_stack
== MAP_FAILED
) {
250 * Pointing to the middle of the stack to support architectures
251 * where the stack grows up (HPPA).
253 pid
= lttng_clone_files(child_run_as
, child_stack
+ (RUNAS_CHILD_STACK_SIZE
/ 2),
260 /* receive return value */
261 readleft
= sizeof(retval
);
264 readlen
= read(retval_pipe
[0], &retval
.c
[index
], readleft
);
272 } while (readleft
> 0);
275 * Parent: wait for child to return, in which case the
276 * shared memory map will have been created.
278 pid
= waitpid(pid
, &status
, 0);
279 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
284 ret
= munmap(child_stack
, RUNAS_CHILD_STACK_SIZE
);
290 close(retval_pipe
[0]);
291 close(retval_pipe
[1]);
296 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
298 struct run_as_mkdir_data data
;
300 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
301 path
, mode
, uid
, gid
);
304 return run_as(_mkdir_recursive
, &data
, uid
, gid
);
307 int run_as_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
309 struct run_as_mkdir_data data
;
311 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
312 path
, mode
, uid
, gid
);
315 return run_as(_mkdir
, &data
, uid
, gid
);
319 * Note: open_run_as is currently not working. We'd need to pass the fd
320 * opened in the child to the parent.
322 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
324 struct run_as_open_data data
;
326 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
327 path
, flags
, mode
, uid
, gid
);
331 return run_as(_open
, &data
, uid
, gid
);
This page took 0.042795 seconds and 5 git commands to generate.