1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
/* $Id: chroot.c 1302 2006-08-31 19:52:01Z lennart $ */
/***
This file is part of avahi.
avahi is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
avahi is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with avahi; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <inttypes.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/un.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <avahi-core/log.h>
#include <libdaemon/dfork.h>
#include "chroot.h"
#include "caps.h"
#include "setproctitle.h"
enum {
AVAHI_CHROOT_SUCCESS = 0,
AVAHI_CHROOT_FAILURE,
AVAHI_CHROOT_GET_RESOLV_CONF,
#ifdef HAVE_DBUS
AVAHI_CHROOT_GET_SERVER_INTROSPECT,
AVAHI_CHROOT_GET_ENTRY_GROUP_INTROSPECT,
AVAHI_CHROOT_GET_ADDRESS_RESOLVER_INTROSPECT,
AVAHI_CHROOT_GET_DOMAIN_BROWSER_INTROSPECT,
AVAHI_CHROOT_GET_HOST_NAME_RESOLVER_INTROSPECT,
AVAHI_CHROOT_GET_SERVICE_BROWSER_INTROSPECT,
AVAHI_CHROOT_GET_SERVICE_RESOLVER_INTROSPECT,
AVAHI_CHROOT_GET_SERVICE_TYPE_BROWSER_INTROSPECT,
AVAHI_CHROOT_GET_RECORD_BROWSER_INTROSPECT,
#endif
AVAHI_CHROOT_UNLINK_PID,
AVAHI_CHROOT_UNLINK_SOCKET,
AVAHI_CHROOT_MAX
};
static const char* const get_file_name_table[AVAHI_CHROOT_MAX] = {
NULL,
NULL,
"/etc/resolv.conf",
#ifdef HAVE_DBUS
AVAHI_DBUS_INTROSPECTION_DIR"/Server.introspect",
AVAHI_DBUS_INTROSPECTION_DIR"/EntryGroup.introspect",
AVAHI_DBUS_INTROSPECTION_DIR"/AddressResolver.introspect",
AVAHI_DBUS_INTROSPECTION_DIR"/DomainBrowser.introspect",
AVAHI_DBUS_INTROSPECTION_DIR"/HostNameResolver.introspect",
AVAHI_DBUS_INTROSPECTION_DIR"/ServiceBrowser.introspect",
AVAHI_DBUS_INTROSPECTION_DIR"/ServiceResolver.introspect",
AVAHI_DBUS_INTROSPECTION_DIR"/ServiceTypeBrowser.introspect",
AVAHI_DBUS_INTROSPECTION_DIR"/RecordBrowser.introspect",
#endif
NULL,
NULL
};
static const char *const unlink_file_name_table[AVAHI_CHROOT_MAX] = {
NULL,
NULL,
NULL,
#ifdef HAVE_DBUS
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
#endif
AVAHI_DAEMON_RUNTIME_DIR"/pid",
AVAHI_SOCKET
};
static int helper_fd = -1;
static int send_fd(int fd, int payload_fd) {
uint8_t dummy = AVAHI_CHROOT_SUCCESS;
struct iovec iov;
struct msghdr msg;
union {
struct cmsghdr hdr;
char buf[CMSG_SPACE(sizeof(int))];
} cmsg;
/* Send a file descriptor over the socket */
memset(&iov, 0, sizeof(iov));
memset(&msg, 0, sizeof(msg));
memset(&cmsg, 0, sizeof(cmsg));
iov.iov_base = &dummy;
iov.iov_len = sizeof(dummy);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_control = &cmsg;
msg.msg_controllen = sizeof(cmsg);
msg.msg_flags = 0;
cmsg.hdr.cmsg_len = CMSG_LEN(sizeof(int));
cmsg.hdr.cmsg_level = SOL_SOCKET;
cmsg.hdr.cmsg_type = SCM_RIGHTS;
*((int*) CMSG_DATA(&cmsg.hdr)) = payload_fd;
if (sendmsg(fd, &msg, 0) < 0) {
avahi_log_error("sendmsg() failed: %s", strerror(errno));
return -1;
}
return 0;
}
static int recv_fd(int fd) {
uint8_t dummy;
struct iovec iov;
struct msghdr msg;
union {
struct cmsghdr hdr;
char buf[CMSG_SPACE(sizeof(int))];
} cmsg;
/* Receive a file descriptor from a socket */
memset(&iov, 0, sizeof(iov));
memset(&msg, 0, sizeof(msg));
memset(&cmsg, 0, sizeof(cmsg));
iov.iov_base = &dummy;
iov.iov_len = sizeof(dummy);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_control = cmsg.buf;
msg.msg_controllen = sizeof(cmsg);
msg.msg_flags = 0;
cmsg.hdr.cmsg_len = CMSG_LEN(sizeof(int));
cmsg.hdr.cmsg_level = SOL_SOCKET;
cmsg.hdr.cmsg_type = SCM_RIGHTS;
*((int*) CMSG_DATA(&cmsg.hdr)) = -1;
if (recvmsg(fd, &msg, 0) <= 0) {
avahi_log_error("recvmsg() failed: %s", strerror(errno));
return -1;
} else {
struct cmsghdr* h;
if (dummy != AVAHI_CHROOT_SUCCESS) {
errno = EINVAL;
return -1;
}
if (!(h = CMSG_FIRSTHDR(&msg))) {
avahi_log_error("recvmsg() sent no fd.");
errno = EINVAL;
return -1;
}
assert(h->cmsg_len = CMSG_LEN(sizeof(int)));
assert(h->cmsg_level = SOL_SOCKET);
assert(h->cmsg_type == SCM_RIGHTS);
return *((int*)CMSG_DATA(h));
}
}
static int helper_main(int fd) {
int ret = 1;
assert(fd >= 0);
/* This is the main function of our helper process which is forked
* off to access files outside the chroot environment. Keep in
* mind that this code is security sensitive! */
avahi_log_debug(__FILE__": chroot() helper started");
for (;;) {
uint8_t command;
ssize_t r;
if ((r = read(fd, &command, sizeof(command))) <= 0) {
/* EOF? */
if (r == 0)
break;
avahi_log_error(__FILE__": read() failed: %s", strerror(errno));
goto fail;
}
assert(r == sizeof(command));
avahi_log_debug(__FILE__": chroot() helper got command %02x", command);
switch (command) {
#ifdef HAVE_DBUS
case AVAHI_CHROOT_GET_SERVER_INTROSPECT:
case AVAHI_CHROOT_GET_ENTRY_GROUP_INTROSPECT:
case AVAHI_CHROOT_GET_ADDRESS_RESOLVER_INTROSPECT:
case AVAHI_CHROOT_GET_DOMAIN_BROWSER_INTROSPECT:
case AVAHI_CHROOT_GET_HOST_NAME_RESOLVER_INTROSPECT:
case AVAHI_CHROOT_GET_SERVICE_BROWSER_INTROSPECT:
case AVAHI_CHROOT_GET_SERVICE_RESOLVER_INTROSPECT:
case AVAHI_CHROOT_GET_SERVICE_TYPE_BROWSER_INTROSPECT:
case AVAHI_CHROOT_GET_RECORD_BROWSER_INTROSPECT:
#endif
case AVAHI_CHROOT_GET_RESOLV_CONF: {
int payload;
if ((payload = open(get_file_name_table[(int) command], O_RDONLY)) < 0) {
uint8_t c = AVAHI_CHROOT_FAILURE;
avahi_log_error(__FILE__": open() failed: %s", strerror(errno));
if (write(fd, &c, sizeof(c)) != sizeof(c)) {
avahi_log_error(__FILE__": write() failed: %s\n", strerror(errno));
goto fail;
}
break;
}
if (send_fd(fd, payload) < 0)
goto fail;
close(payload);
break;
}
case AVAHI_CHROOT_UNLINK_SOCKET:
case AVAHI_CHROOT_UNLINK_PID: {
uint8_t c = AVAHI_CHROOT_SUCCESS;
unlink(unlink_file_name_table[(int) command]);
if (write(fd, &c, sizeof(c)) != sizeof(c)) {
avahi_log_error(__FILE__": write() failed: %s\n", strerror(errno));
goto fail;
}
break;
}
default:
avahi_log_error(__FILE__": Unknown command %02x.", command);
break;
}
}
ret = 0;
fail:
avahi_log_debug(__FILE__": chroot() helper exiting with return value %i", ret);
return ret;
}
int avahi_chroot_helper_start(const char *argv0) {
int sock[2];
pid_t pid;
assert(helper_fd < 0);
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sock) < 0) {
avahi_log_error("socketpair() failed: %s", strerror(errno));
return -1;
}
if ((pid = fork()) < 0) {
close(sock[0]);
close(sock[1]);
avahi_log_error(__FILE__": fork() failed: %s", strerror(errno));
return -1;
} else if (pid == 0) {
setsid();
/* Drop all remaining capabilities */
avahi_caps_drop_all();
avahi_set_proc_title(argv0, "%s: chroot helper", argv0);
daemon_retval_done();
close(sock[0]);
helper_main(sock[1]);
_exit(0);
}
close(sock[1]);
helper_fd = sock[0];
return 0;
}
void avahi_chroot_helper_shutdown(void) {
if (helper_fd <= 0)
return;
close(helper_fd);
helper_fd = -1;
}
int avahi_chroot_helper_get_fd(const char *fname) {
if (helper_fd >= 0) {
uint8_t command;
for (command = 2; command < AVAHI_CHROOT_MAX; command++)
if (get_file_name_table[(int) command] &&
strcmp(fname, get_file_name_table[(int) command]) == 0)
break;
if (command >= AVAHI_CHROOT_MAX) {
avahi_log_error("chroot() helper accessed for invalid file name");
errno = EACCES;
return -1;
}
assert(get_file_name_table[(int) command]);
if (write(helper_fd, &command, sizeof(command)) < 0) {
avahi_log_error("write() failed: %s\n", strerror(errno));
return -1;
}
return recv_fd(helper_fd);
} else
return open(fname, O_RDONLY);
}
FILE *avahi_chroot_helper_get_file(const char *fname) {
FILE *f;
int fd;
if ((fd = avahi_chroot_helper_get_fd(fname)) < 0)
return NULL;
f = fdopen(fd, "r");
assert(f);
return f;
}
int avahi_chroot_helper_unlink(const char *fname) {
if (helper_fd >= 0) {
uint8_t c, command;
ssize_t r;
for (command = 2; command < AVAHI_CHROOT_MAX; command++)
if (unlink_file_name_table[(int) command] &&
strcmp(fname, unlink_file_name_table[(int) command]) == 0)
break;
if (command >= AVAHI_CHROOT_MAX) {
avahi_log_error("chroot() helper accessed for invalid file name");
errno = EACCES;
return -1;
}
if (write(helper_fd, &command, sizeof(command)) < 0) {
avahi_log_error("write() failed: %s\n", strerror(errno));
return -1;
}
if ((r = read(helper_fd, &c, sizeof(c))) < 0) {
avahi_log_error("read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
return -1;
}
return 0;
} else
return unlink(fname);
}
|