Commit | Line | Data |
---|---|---|
60b6c79c MD |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
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. | |
8 | * | |
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 | |
12 | * more details. | |
13 | * | |
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. | |
17 | */ | |
18 | ||
19 | #define _GNU_SOURCE | |
20 | #include <errno.h> | |
21 | #include <limits.h> | |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <sys/wait.h> | |
26 | #include <sys/types.h> | |
27 | #include <sys/stat.h> | |
28 | #include <unistd.h> | |
29 | #include <fcntl.h> | |
c2b75c49 | 30 | #include <sched.h> |
d5bd7573 | 31 | #include <sys/mman.h> |
60b6c79c | 32 | |
db758600 | 33 | #include <common/error.h> |
60b6c79c | 34 | |
0857097f DG |
35 | #include "runas.h" |
36 | ||
e11d277b | 37 | #define RUNAS_CHILD_STACK_SIZE 10485760 |
c2b75c49 | 38 | |
059e1d3d MD |
39 | #ifndef MAP_STACK |
40 | #define MAP_STACK 0 | |
41 | #endif | |
42 | ||
c2b75c49 MD |
43 | struct run_as_data { |
44 | int (*cmd)(void *data); | |
45 | void *data; | |
46 | uid_t uid; | |
47 | gid_t gid; | |
48 | int retval_pipe; | |
49 | }; | |
50 | ||
e11d277b | 51 | struct run_as_mkdir_data { |
60b6c79c MD |
52 | const char *path; |
53 | mode_t mode; | |
54 | }; | |
55 | ||
e11d277b | 56 | struct run_as_open_data { |
60b6c79c MD |
57 | const char *path; |
58 | int flags; | |
59 | mode_t mode; | |
60 | }; | |
61 | ||
62 | /* | |
63 | * Create recursively directory using the FULL path. | |
64 | */ | |
65 | static | |
66 | int _mkdir_recursive(void *_data) | |
67 | { | |
e11d277b | 68 | struct run_as_mkdir_data *data = _data; |
60b6c79c MD |
69 | const char *path; |
70 | char *p, tmp[PATH_MAX]; | |
71 | struct stat statbuf; | |
72 | mode_t mode; | |
73 | size_t len; | |
74 | int ret; | |
75 | ||
76 | path = data->path; | |
77 | mode = data->mode; | |
78 | ||
79 | ret = snprintf(tmp, sizeof(tmp), "%s", path); | |
80 | if (ret < 0) { | |
81 | PERROR("snprintf mkdir"); | |
82 | goto error; | |
83 | } | |
84 | ||
85 | len = ret; | |
86 | if (tmp[len - 1] == '/') { | |
87 | tmp[len - 1] = 0; | |
88 | } | |
89 | ||
90 | for (p = tmp + 1; *p; p++) { | |
91 | if (*p == '/') { | |
92 | *p = 0; | |
93 | ret = stat(tmp, &statbuf); | |
94 | if (ret < 0) { | |
95 | ret = mkdir(tmp, mode); | |
96 | if (ret < 0) { | |
97 | if (!(errno == EEXIST)) { | |
98 | PERROR("mkdir recursive"); | |
99 | ret = -errno; | |
100 | goto error; | |
101 | } | |
102 | } | |
103 | } | |
104 | *p = '/'; | |
105 | } | |
106 | } | |
107 | ||
108 | ret = mkdir(tmp, mode); | |
109 | if (ret < 0) { | |
110 | if (!(errno == EEXIST)) { | |
111 | PERROR("mkdir recursive last piece"); | |
112 | ret = -errno; | |
113 | } else { | |
114 | ret = 0; | |
115 | } | |
116 | } | |
117 | ||
118 | error: | |
119 | return ret; | |
120 | } | |
121 | ||
122 | static | |
123 | int _mkdir(void *_data) | |
124 | { | |
e11d277b | 125 | struct run_as_mkdir_data *data = _data; |
60b6c79c MD |
126 | return mkdir(data->path, data->mode); |
127 | } | |
128 | ||
129 | static | |
130 | int _open(void *_data) | |
131 | { | |
e11d277b | 132 | struct run_as_open_data *data = _data; |
60b6c79c MD |
133 | return open(data->path, data->flags, data->mode); |
134 | } | |
135 | ||
c2b75c49 MD |
136 | static |
137 | int child_run_as(void *_data) | |
138 | { | |
139 | struct run_as_data *data = _data; | |
140 | size_t writelen, writeleft, index; | |
141 | union { | |
142 | int i; | |
143 | char c[sizeof(int)]; | |
144 | } sendret; | |
145 | int ret; | |
146 | ||
147 | /* | |
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 | |
152 | * process memory. | |
153 | */ | |
1576d582 MD |
154 | if (data->gid != getegid()) { |
155 | ret = setegid(data->gid); | |
156 | if (ret < 0) { | |
157 | perror("setegid"); | |
6da9f915 | 158 | return EXIT_FAILURE; |
1576d582 | 159 | } |
c2b75c49 | 160 | } |
1576d582 MD |
161 | if (data->uid != geteuid()) { |
162 | ret = seteuid(data->uid); | |
163 | if (ret < 0) { | |
164 | perror("seteuid"); | |
6da9f915 | 165 | return EXIT_FAILURE; |
1576d582 | 166 | } |
c2b75c49 MD |
167 | } |
168 | /* | |
169 | * Also set umask to 0 for mkdir executable bit. | |
170 | */ | |
171 | umask(0); | |
172 | sendret.i = (*data->cmd)(data->data); | |
173 | /* send back return value */ | |
174 | writeleft = sizeof(sendret); | |
175 | index = 0; | |
176 | do { | |
177 | writelen = write(data->retval_pipe, &sendret.c[index], | |
178 | writeleft); | |
179 | if (writelen < 0) { | |
180 | perror("write"); | |
6da9f915 | 181 | return EXIT_FAILURE; |
c2b75c49 MD |
182 | } |
183 | writeleft -= writelen; | |
184 | index += writelen; | |
185 | } while (writeleft > 0); | |
6da9f915 | 186 | return EXIT_SUCCESS; |
c2b75c49 MD |
187 | } |
188 | ||
60b6c79c MD |
189 | static |
190 | int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) | |
191 | { | |
c2b75c49 | 192 | struct run_as_data run_as_data; |
60b6c79c | 193 | int ret = 0; |
c2b75c49 | 194 | int status; |
60b6c79c | 195 | pid_t pid; |
c2b75c49 MD |
196 | int retval_pipe[2]; |
197 | ssize_t readlen, readleft, index; | |
d5bd7573 | 198 | void *child_stack; |
c2b75c49 MD |
199 | union { |
200 | int i; | |
201 | char c[sizeof(int)]; | |
202 | } retval; | |
60b6c79c MD |
203 | |
204 | /* | |
205 | * If we are non-root, we can only deal with our own uid. | |
206 | */ | |
207 | if (geteuid() != 0) { | |
208 | if (uid != geteuid()) { | |
209 | ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)", | |
210 | uid, geteuid()); | |
211 | return -EPERM; | |
212 | } | |
60b6c79c MD |
213 | } |
214 | ||
c2b75c49 MD |
215 | ret = pipe(retval_pipe); |
216 | if (ret < 0) { | |
217 | perror("pipe"); | |
60b6c79c | 218 | goto end; |
c2b75c49 MD |
219 | } |
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 */ | |
e11d277b | 225 | child_stack = mmap(NULL, RUNAS_CHILD_STACK_SIZE, |
d5bd7573 MD |
226 | PROT_WRITE | PROT_READ, |
227 | MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | MAP_STACK, | |
228 | -1, 0); | |
229 | if (child_stack == MAP_FAILED) { | |
230 | perror("mmap"); | |
231 | ret = -ENOMEM; | |
232 | goto close_pipe; | |
233 | } | |
234 | /* | |
235 | * Pointing to the middle of the stack to support architectures | |
236 | * where the stack grows up (HPPA). | |
237 | */ | |
e11d277b | 238 | pid = clone(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2), |
9beed4cb | 239 | CLONE_FILES | SIGCHLD, |
d5bd7573 | 240 | &run_as_data, NULL); |
c2b75c49 MD |
241 | if (pid < 0) { |
242 | perror("clone"); | |
243 | ret = pid; | |
d5bd7573 | 244 | goto unmap_stack; |
c2b75c49 MD |
245 | } |
246 | /* receive return value */ | |
247 | readleft = sizeof(retval); | |
248 | index = 0; | |
249 | do { | |
250 | readlen = read(retval_pipe[0], &retval.c[index], readleft); | |
251 | if (readlen < 0) { | |
252 | perror("read"); | |
253 | ret = -1; | |
254 | break; | |
60b6c79c | 255 | } |
c2b75c49 MD |
256 | readleft -= readlen; |
257 | index += readlen; | |
258 | } while (readleft > 0); | |
259 | ||
260 | /* | |
261 | * Parent: wait for child to return, in which case the | |
262 | * shared memory map will have been created. | |
263 | */ | |
47fb7563 | 264 | pid = waitpid(pid, &status, 0); |
c2b75c49 MD |
265 | if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
266 | perror("wait"); | |
267 | ret = -1; | |
60b6c79c | 268 | } |
d5bd7573 | 269 | unmap_stack: |
e11d277b | 270 | ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE); |
d5bd7573 MD |
271 | if (ret < 0) { |
272 | perror("munmap"); | |
273 | } | |
c2b75c49 MD |
274 | close_pipe: |
275 | close(retval_pipe[0]); | |
276 | close(retval_pipe[1]); | |
60b6c79c | 277 | end: |
c2b75c49 | 278 | return retval.i; |
60b6c79c MD |
279 | } |
280 | ||
e11d277b | 281 | int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid) |
60b6c79c | 282 | { |
e11d277b | 283 | struct run_as_mkdir_data data; |
60b6c79c MD |
284 | |
285 | DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d", | |
286 | path, mode, uid, gid); | |
287 | data.path = path; | |
288 | data.mode = mode; | |
289 | return run_as(_mkdir_recursive, &data, uid, gid); | |
290 | } | |
291 | ||
e11d277b | 292 | int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) |
60b6c79c | 293 | { |
e11d277b | 294 | struct run_as_mkdir_data data; |
60b6c79c MD |
295 | |
296 | DBG3("mkdir() %s with mode %d for uid %d and gid %d", | |
297 | path, mode, uid, gid); | |
298 | data.path = path; | |
299 | data.mode = mode; | |
300 | return run_as(_mkdir, &data, uid, gid); | |
301 | } | |
302 | ||
303 | /* | |
304 | * Note: open_run_as is currently not working. We'd need to pass the fd | |
305 | * opened in the child to the parent. | |
306 | */ | |
e11d277b | 307 | int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid) |
60b6c79c | 308 | { |
e11d277b | 309 | struct run_as_open_data data; |
c2b75c49 | 310 | |
47fb7563 MD |
311 | DBG3("open() %s with flags %X mode %d for uid %d and gid %d", |
312 | path, flags, mode, uid, gid); | |
60b6c79c MD |
313 | data.path = path; |
314 | data.flags = flags; | |
315 | data.mode = mode; | |
316 | return run_as(_open, &data, uid, gid); | |
60b6c79c | 317 | } |