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
46 int (*cmd
)(void *data
);
53 struct run_as_mkdir_data
{
58 struct run_as_open_data
{
65 * Create recursively directory using the FULL path.
68 int _mkdir_recursive(void *_data
)
70 struct run_as_mkdir_data
*data
= _data
;
72 char *p
, tmp
[PATH_MAX
];
81 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
83 PERROR("snprintf mkdir");
88 if (tmp
[len
- 1] == '/') {
92 for (p
= tmp
+ 1; *p
; p
++) {
95 ret
= stat(tmp
, &statbuf
);
97 ret
= mkdir(tmp
, mode
);
99 if (!(errno
== EEXIST
)) {
100 PERROR("mkdir recursive");
110 ret
= mkdir(tmp
, mode
);
112 if (!(errno
== EEXIST
)) {
113 PERROR("mkdir recursive last piece");
125 int _mkdir(void *_data
)
127 struct run_as_mkdir_data
*data
= _data
;
128 return mkdir(data
->path
, data
->mode
);
132 int _open(void *_data
)
134 struct run_as_open_data
*data
= _data
;
135 return open(data
->path
, data
->flags
, data
->mode
);
139 int child_run_as(void *_data
)
141 struct run_as_data
*data
= _data
;
142 size_t writelen
, writeleft
, index
;
150 * Child: it is safe to drop egid and euid while sharing the
151 * file descriptors with the parent process, since we do not
152 * drop "uid": therefore, the user we are dropping egid/euid to
153 * cannot attach to this process with, e.g. ptrace, nor map this
156 if (data
->gid
!= getegid()) {
157 ret
= setegid(data
->gid
);
163 if (data
->uid
!= geteuid()) {
164 ret
= seteuid(data
->uid
);
171 * Also set umask to 0 for mkdir executable bit.
174 sendret
.i
= (*data
->cmd
)(data
->data
);
175 /* send back return value */
176 writeleft
= sizeof(sendret
);
179 writelen
= write(data
->retval_pipe
, &sendret
.c
[index
],
185 writeleft
-= writelen
;
187 } while (writeleft
> 0);
192 int run_as(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
194 struct run_as_data run_as_data
;
199 ssize_t readlen
, readleft
, index
;
207 * If we are non-root, we can only deal with our own uid.
209 if (geteuid() != 0) {
210 if (uid
!= geteuid()) {
211 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
217 ret
= pipe(retval_pipe
);
222 run_as_data
.data
= data
;
223 run_as_data
.cmd
= cmd
;
224 run_as_data
.uid
= uid
;
225 run_as_data
.gid
= gid
;
226 run_as_data
.retval_pipe
= retval_pipe
[1]; /* write end */
227 child_stack
= mmap(NULL
, RUNAS_CHILD_STACK_SIZE
,
228 PROT_WRITE
| PROT_READ
,
229 MAP_PRIVATE
| MAP_GROWSDOWN
| MAP_ANONYMOUS
| MAP_STACK
,
231 if (child_stack
== MAP_FAILED
) {
237 * Pointing to the middle of the stack to support architectures
238 * where the stack grows up (HPPA).
240 pid
= clone(child_run_as
, child_stack
+ (RUNAS_CHILD_STACK_SIZE
/ 2),
241 CLONE_FILES
| SIGCHLD
,
248 /* receive return value */
249 readleft
= sizeof(retval
);
252 readlen
= read(retval_pipe
[0], &retval
.c
[index
], readleft
);
260 } while (readleft
> 0);
263 * Parent: wait for child to return, in which case the
264 * shared memory map will have been created.
266 pid
= waitpid(pid
, &status
, 0);
267 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
272 ret
= munmap(child_stack
, RUNAS_CHILD_STACK_SIZE
);
277 close(retval_pipe
[0]);
278 close(retval_pipe
[1]);
283 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
285 struct run_as_mkdir_data data
;
287 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
288 path
, mode
, uid
, gid
);
291 return run_as(_mkdir_recursive
, &data
, uid
, gid
);
294 int run_as_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
296 struct run_as_mkdir_data data
;
298 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
299 path
, mode
, uid
, gid
);
302 return run_as(_mkdir
, &data
, uid
, gid
);
306 * Note: open_run_as is currently not working. We'd need to pass the fd
307 * opened in the child to the parent.
309 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
311 struct run_as_open_data data
;
313 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
314 path
, flags
, mode
, uid
, gid
);
318 return run_as(_open
, &data
, uid
, gid
);
This page took 0.058562 seconds and 5 git commands to generate.