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