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>
33 #include <common/error.h>
37 #define RUNAS_CHILD_STACK_SIZE 10485760
44 int (*cmd
)(void *data
);
51 struct run_as_mkdir_data
{
56 struct run_as_open_data
{
63 * Create recursively directory using the FULL path.
66 int _mkdir_recursive(void *_data
)
68 struct run_as_mkdir_data
*data
= _data
;
70 char *p
, tmp
[PATH_MAX
];
79 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
81 PERROR("snprintf mkdir");
86 if (tmp
[len
- 1] == '/') {
90 for (p
= tmp
+ 1; *p
; p
++) {
93 ret
= stat(tmp
, &statbuf
);
95 ret
= mkdir(tmp
, mode
);
97 if (!(errno
== EEXIST
)) {
98 PERROR("mkdir recursive");
108 ret
= mkdir(tmp
, mode
);
110 if (!(errno
== EEXIST
)) {
111 PERROR("mkdir recursive last piece");
123 int _mkdir(void *_data
)
125 struct run_as_mkdir_data
*data
= _data
;
126 return mkdir(data
->path
, data
->mode
);
130 int _open(void *_data
)
132 struct run_as_open_data
*data
= _data
;
133 return open(data
->path
, data
->flags
, data
->mode
);
137 int child_run_as(void *_data
)
139 struct run_as_data
*data
= _data
;
140 size_t writelen
, writeleft
, index
;
148 * Child: it is safe to drop egid and euid while sharing the
149 * file descriptors with the parent process, since we do not
150 * drop "uid": therefore, the user we are dropping egid/euid to
151 * cannot attach to this process with, e.g. ptrace, nor map this
154 if (data
->gid
!= getegid()) {
155 ret
= setegid(data
->gid
);
161 if (data
->uid
!= geteuid()) {
162 ret
= seteuid(data
->uid
);
169 * Also set umask to 0 for mkdir executable bit.
172 sendret
.i
= (*data
->cmd
)(data
->data
);
173 /* send back return value */
174 writeleft
= sizeof(sendret
);
177 writelen
= write(data
->retval_pipe
, &sendret
.c
[index
],
183 writeleft
-= writelen
;
185 } while (writeleft
> 0);
190 int run_as(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
192 struct run_as_data run_as_data
;
197 ssize_t readlen
, readleft
, index
;
205 * If we are non-root, we can only deal with our own uid.
207 if (geteuid() != 0) {
208 if (uid
!= geteuid()) {
209 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
215 ret
= pipe(retval_pipe
);
220 run_as_data
.data
= data
;
221 run_as_data
.cmd
= cmd
;
222 run_as_data
.uid
= uid
;
223 run_as_data
.gid
= gid
;
224 run_as_data
.retval_pipe
= retval_pipe
[1]; /* write end */
225 child_stack
= mmap(NULL
, RUNAS_CHILD_STACK_SIZE
,
226 PROT_WRITE
| PROT_READ
,
227 MAP_PRIVATE
| MAP_GROWSDOWN
| MAP_ANONYMOUS
| MAP_STACK
,
229 if (child_stack
== MAP_FAILED
) {
235 * Pointing to the middle of the stack to support architectures
236 * where the stack grows up (HPPA).
238 pid
= clone(child_run_as
, child_stack
+ (RUNAS_CHILD_STACK_SIZE
/ 2),
239 CLONE_FILES
| SIGCHLD
,
246 /* receive return value */
247 readleft
= sizeof(retval
);
250 readlen
= read(retval_pipe
[0], &retval
.c
[index
], readleft
);
258 } while (readleft
> 0);
261 * Parent: wait for child to return, in which case the
262 * shared memory map will have been created.
264 pid
= waitpid(pid
, &status
, 0);
265 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
270 ret
= munmap(child_stack
, RUNAS_CHILD_STACK_SIZE
);
275 close(retval_pipe
[0]);
276 close(retval_pipe
[1]);
281 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
283 struct run_as_mkdir_data data
;
285 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
286 path
, mode
, uid
, gid
);
289 return run_as(_mkdir_recursive
, &data
, uid
, gid
);
292 int run_as_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
294 struct run_as_mkdir_data data
;
296 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
297 path
, mode
, uid
, gid
);
300 return run_as(_mkdir
, &data
, uid
, gid
);
304 * Note: open_run_as is currently not working. We'd need to pass the fd
305 * opened in the child to the parent.
307 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
309 struct run_as_open_data data
;
311 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
312 path
, flags
, mode
, uid
, gid
);
316 return run_as(_open
, &data
, uid
, gid
);
This page took 0.036251 seconds and 4 git commands to generate.