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