ba361d6d63156a3361816a83bc1c0ae1b3a692da
[lttng-tools.git] / librunas / runas.c
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>
30 #include <sched.h>
31 #include <sys/mman.h>
32
33 #include <lttngerr.h>
34
35 #define CHILD_STACK_SIZE 10485760
36
37 struct run_as_data {
38 int (*cmd)(void *data);
39 void *data;
40 uid_t uid;
41 gid_t gid;
42 int retval_pipe;
43 };
44
45 struct mkdir_data {
46 const char *path;
47 mode_t mode;
48 };
49
50 struct open_data {
51 const char *path;
52 int flags;
53 mode_t mode;
54 };
55
56 /*
57 * Create recursively directory using the FULL path.
58 */
59 static
60 int _mkdir_recursive(void *_data)
61 {
62 struct mkdir_data *data = _data;
63 const char *path;
64 char *p, tmp[PATH_MAX];
65 struct stat statbuf;
66 mode_t mode;
67 size_t len;
68 int ret;
69
70 path = data->path;
71 mode = data->mode;
72
73 ret = snprintf(tmp, sizeof(tmp), "%s", path);
74 if (ret < 0) {
75 PERROR("snprintf mkdir");
76 goto error;
77 }
78
79 len = ret;
80 if (tmp[len - 1] == '/') {
81 tmp[len - 1] = 0;
82 }
83
84 for (p = tmp + 1; *p; p++) {
85 if (*p == '/') {
86 *p = 0;
87 ret = stat(tmp, &statbuf);
88 if (ret < 0) {
89 ret = mkdir(tmp, mode);
90 if (ret < 0) {
91 if (!(errno == EEXIST)) {
92 PERROR("mkdir recursive");
93 ret = -errno;
94 goto error;
95 }
96 }
97 }
98 *p = '/';
99 }
100 }
101
102 ret = mkdir(tmp, mode);
103 if (ret < 0) {
104 if (!(errno == EEXIST)) {
105 PERROR("mkdir recursive last piece");
106 ret = -errno;
107 } else {
108 ret = 0;
109 }
110 }
111
112 error:
113 return ret;
114 }
115
116 static
117 int _mkdir(void *_data)
118 {
119 struct mkdir_data *data = _data;
120 return mkdir(data->path, data->mode);
121 }
122
123 static
124 int _open(void *_data)
125 {
126 struct open_data *data = _data;
127 return open(data->path, data->flags, data->mode);
128 }
129
130 static
131 int child_run_as(void *_data)
132 {
133 struct run_as_data *data = _data;
134 size_t writelen, writeleft, index;
135 union {
136 int i;
137 char c[sizeof(int)];
138 } sendret;
139 int ret;
140
141 /*
142 * Child: it is safe to drop egid and euid while sharing the
143 * file descriptors with the parent process, since we do not
144 * drop "uid": therefore, the user we are dropping egid/euid to
145 * cannot attach to this process with, e.g. ptrace, nor map this
146 * process memory.
147 */
148 if (data->gid != getegid()) {
149 ret = setegid(data->gid);
150 if (ret < 0) {
151 perror("setegid");
152 exit(EXIT_FAILURE);
153 }
154 }
155 if (data->uid != geteuid()) {
156 ret = seteuid(data->uid);
157 if (ret < 0) {
158 perror("seteuid");
159 exit(EXIT_FAILURE);
160 }
161 }
162 /*
163 * Also set umask to 0 for mkdir executable bit.
164 */
165 umask(0);
166 sendret.i = (*data->cmd)(data->data);
167 /* send back return value */
168 writeleft = sizeof(sendret);
169 index = 0;
170 do {
171 writelen = write(data->retval_pipe, &sendret.c[index],
172 writeleft);
173 if (writelen < 0) {
174 perror("write");
175 exit(EXIT_FAILURE);
176 }
177 writeleft -= writelen;
178 index += writelen;
179 } while (writeleft > 0);
180
181 exit(EXIT_SUCCESS);
182 }
183
184 static
185 int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
186 {
187 struct run_as_data run_as_data;
188 int ret = 0;
189 int status;
190 pid_t pid;
191 int retval_pipe[2];
192 ssize_t readlen, readleft, index;
193 void *child_stack;
194 union {
195 int i;
196 char c[sizeof(int)];
197 } retval;
198
199 /*
200 * If we are non-root, we can only deal with our own uid.
201 */
202 if (geteuid() != 0) {
203 if (uid != geteuid()) {
204 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
205 uid, geteuid());
206 return -EPERM;
207 }
208 }
209
210 ret = pipe(retval_pipe);
211 if (ret < 0) {
212 perror("pipe");
213 goto end;
214 }
215 run_as_data.data = data;
216 run_as_data.cmd = cmd;
217 run_as_data.uid = uid;
218 run_as_data.gid = gid;
219 run_as_data.retval_pipe = retval_pipe[1]; /* write end */
220 child_stack = mmap(NULL, CHILD_STACK_SIZE,
221 PROT_WRITE | PROT_READ,
222 MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | MAP_STACK,
223 -1, 0);
224 if (child_stack == MAP_FAILED) {
225 perror("mmap");
226 ret = -ENOMEM;
227 goto close_pipe;
228 }
229 /*
230 * Pointing to the middle of the stack to support architectures
231 * where the stack grows up (HPPA).
232 */
233 pid = clone(child_run_as, child_stack + (CHILD_STACK_SIZE / 2),
234 CLONE_FILES | SIGCHLD,
235 &run_as_data, NULL);
236 if (pid < 0) {
237 perror("clone");
238 ret = pid;
239 goto unmap_stack;
240 }
241 /* receive return value */
242 readleft = sizeof(retval);
243 index = 0;
244 do {
245 readlen = read(retval_pipe[0], &retval.c[index], readleft);
246 if (readlen < 0) {
247 perror("read");
248 ret = -1;
249 break;
250 }
251 readleft -= readlen;
252 index += readlen;
253 } while (readleft > 0);
254
255 /*
256 * Parent: wait for child to return, in which case the
257 * shared memory map will have been created.
258 */
259 pid = wait(&status);
260 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
261 perror("wait");
262 ret = -1;
263 }
264 unmap_stack:
265 ret = munmap(child_stack, CHILD_STACK_SIZE);
266 if (ret < 0) {
267 perror("munmap");
268 }
269 close_pipe:
270 close(retval_pipe[0]);
271 close(retval_pipe[1]);
272 end:
273 return retval.i;
274 }
275
276 int mkdir_recursive_run_as(const char *path, mode_t mode, uid_t uid, gid_t gid)
277 {
278 struct mkdir_data data;
279
280 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
281 path, mode, uid, gid);
282 data.path = path;
283 data.mode = mode;
284 return run_as(_mkdir_recursive, &data, uid, gid);
285 }
286
287 int mkdir_run_as(const char *path, mode_t mode, uid_t uid, gid_t gid)
288 {
289 struct mkdir_data data;
290
291 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
292 path, mode, uid, gid);
293 data.path = path;
294 data.mode = mode;
295 return run_as(_mkdir, &data, uid, gid);
296 }
297
298 /*
299 * Note: open_run_as is currently not working. We'd need to pass the fd
300 * opened in the child to the parent.
301 */
302 int open_run_as(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
303 {
304 struct open_data data;
305
306 data.path = path;
307 data.flags = flags;
308 data.mode = mode;
309 return run_as(_open, &data, uid, gid);
310 }
This page took 0.037038 seconds and 4 git commands to generate.