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