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