rootwrap.c 10.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*****************************************************************************
 * rootwrap.c
 *****************************************************************************
 * Copyright © 2005 Rémi Denis-Courmont
 * $Id$
 *
 * Author: Rémi Denis-Courmont <rem # videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
23
24
25
26
27
 *****************************************************************************/

#if HAVE_CONFIG_H
# include <config.h>
#endif

28
#if defined (HAVE_GETEUID) && !defined (SYS_BEOS)
29
30
31
32
33
34
35
# define ENABLE_ROOTWRAP 1
#endif

#ifdef ENABLE_ROOTWRAP

#include <stdlib.h> /* exit() */
#include <stdio.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
36
#include <string.h>
37
38
39
40
41

#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
42
43
44
45
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/uio.h>
46
47
48
49
50
51
52
53
54
#include <sys/resource.h> /* getrlimit() */
#include <sys/wait.h>
#include <sys/un.h>
#include <pwd.h> /* getpwnam(), getpwuid() */
#include <grp.h> /* setgroups() */
#include <errno.h>
#include <netinet/in.h>
#include <pthread.h>

55
56
57
58
59
#if defined (AF_INET6) && !defined (IPV6_V6ONLY)
# warning Uho, your IPv6 support is broken and has been disabled. Fix your C library.
# undef AF_INET6
#endif

60
61
62
63
#ifndef AF_LOCAL
# define AF_LOCAL AF_UNIX
#endif

64
65
66
67
68
69
70
71
72
73
74
/*#ifndef HAVE_CLEARENV
extern char **environ;

static int clearenv (void)
{
    environ = NULL;
    return 0;
}
#endif*/

/**
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
75
 * Tries to find a real non-root user to use
76
 */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
77
static struct passwd *guess_user (void)
78
79
{
    const char *name;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
80
    struct passwd *pw;
81
82
83
84
85
    uid_t uid;

    /* Try real UID */
    uid = getuid ();
    if (uid)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
86
87
        if ((pw = getpwuid (uid)) != NULL)
            return pw;
88
89
90
91

    /* Try sudo */
    name = getenv ("SUDO_USER");
    if (name != NULL)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
92
93
        if ((pw = getpwnam (name)) != NULL)
            return pw;
94
95
96
97

    /* Try VLC_USER */
    name = getenv ("VLC_USER");
    if (name != NULL)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
98
99
        if ((pw = getpwnam (name)) != NULL)
            return pw;
100
101

    /* Try vlc */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
102
103
    if ((pw = getpwnam ("vlc")) != NULL)
        return pw;
104

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
105
    return getpwuid (0);
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
}


static int is_allowed_port (uint16_t port)
{
    port = ntohs (port);

    return (port == 80) || (port == 443) || (port == 554);
}


static int send_err (int fd, int err)
{
    return send (fd, &err, sizeof (err), 0) == sizeof (err) ? 0 : -1;
}

/**
 * Ugly POSIX(?) code to pass a file descriptor to another process
 */
static int send_fd (int p, int fd)
{
    struct msghdr hdr;
    struct iovec iov;
    struct cmsghdr *cmsg;
    char buf[CMSG_SPACE (sizeof (fd))];
    int val = 0;

    hdr.msg_name = NULL;
    hdr.msg_namelen = 0;
    hdr.msg_iov = &iov;
    hdr.msg_iovlen = 1;
    hdr.msg_control = buf;
    hdr.msg_controllen = sizeof (buf);

    iov.iov_base = &val;
    iov.iov_len = sizeof (val);

    cmsg = CMSG_FIRSTHDR (&hdr);
    cmsg->cmsg_level = SOL_SOCKET;
    cmsg->cmsg_type = SCM_RIGHTS;
    cmsg->cmsg_len = CMSG_LEN (sizeof (fd));
    memcpy (CMSG_DATA (cmsg), &fd, sizeof (fd));
    hdr.msg_controllen = cmsg->cmsg_len;

    return sendmsg (p, &hdr, 0) == sizeof (val) ? 0 : -1;
}


/**
 * Background process run as root to open privileged TCP ports.
 */
static void rootprocess (int fd)
{
    struct sockaddr_storage ss;

    /* TODO:
     *  - use libcap if available,
     *  - call chroot
     */
    while (recv (fd, &ss, sizeof (ss), 0) == sizeof (ss))
    {
        unsigned len;
        int sock;

        switch (ss.ss_family)
        {
            case AF_INET:
                if (!is_allowed_port (((struct sockaddr_in *)&ss)->sin_port))
                {
                    if (send_err (fd, EACCES))
                        return;
                    continue;
                }
                len = sizeof (struct sockaddr_in);
                break;

#ifdef AF_INET6
            case AF_INET6:
                if (!is_allowed_port (((struct sockaddr_in6 *)&ss)->sin6_port))
                {
                    if (send_err (fd, EACCES))
                        return;
                    continue;
                }
                len = sizeof (struct sockaddr_in6);
                break;
#endif

            default:
                if (send_err (fd, EAFNOSUPPORT))
                    return;
                continue;
        }

        sock = socket (ss.ss_family, SOCK_STREAM, IPPROTO_TCP);
        if (sock != -1)
        {
            const int val = 1;

            setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof (val));
206
#ifdef AF_INET6
207
            if (ss.ss_family == AF_INET6)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
208
                setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof (val));
209
#endif
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
            if (bind (sock, (struct sockaddr *)&ss, len) == 0)
            {
                send_fd (fd, sock);
                close (sock);
                continue;
            }
        }
        send_err (fd, errno);
    }
}

static int rootwrap_sock = -1;
static pid_t rootwrap_pid = -1;

static void close_rootwrap (void)
{
    close (rootwrap_sock);
    waitpid (rootwrap_pid, NULL, 0);
}

void rootwrap (void)
{
    struct rlimit lim;
    int fd, pair[2];
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
234
    struct passwd *pw;
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
    uid_t u;

    u = geteuid ();
    /* Are we running with root privileges? */
    if (u != 0)
    {
        setuid (u);
        return;
    }

    /* Make sure 0, 1 and 2 are opened, and only these. */
    if (getrlimit (RLIMIT_NOFILE, &lim))
        exit (1);

    for (fd = 3; ((unsigned)fd) < lim.rlim_cur; fd++)
        close (fd);

    fd = dup (2);
    if (fd <= 2)
        exit (1);
    close (fd);

257
    fputs ("starting VLC root wrapper...", stderr);
258

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
259
260
261
    pw = guess_user ();
    if (pw == NULL)
        return; /* Should we rather print an error and exit ? */
262

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
263
264
    u = pw->pw_uid,
    fprintf (stderr, " using UID %u (%s)\n", (unsigned)u, pw->pw_name);
265
266
267
268
269
270
271
    if (u == 0)
    {
        fputs ("***************************************\n"
               "* Running VLC as root is discouraged. *\n"
               "***************************************\n"
               "\n"
               " It is potentially dangerous, "
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
272
                "and might not even work properly.\n", stderr);
273
274
275
        return;
    }

276
    /* GID */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
277
278
    initgroups (pw->pw_name, pw->pw_gid);
    setgid (pw->pw_gid);
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444

    if (socketpair (AF_LOCAL, SOCK_STREAM, 0, pair))
    {
        perror ("socketpair");
        goto nofork;
    }

    switch (rootwrap_pid = fork ())
    {
        case -1:
            perror ("fork");
            close (pair[0]);
            close (pair[1]);
            break;

        case 0:
            close (0);
            close (1);
            close (2);
            close (pair[0]);
            rootprocess (pair[1]);
            exit (0);

        default:
            close (pair[1]);
            rootwrap_sock = pair[0];
            break;
    }

nofork:
    /* UID */
    setuid (u);

    atexit (close_rootwrap);
}


/**
 * Ugly POSIX(?) code to receive a file descriptor from another process
 */
static int recv_fd (int p)
{
    struct msghdr hdr;
    struct iovec iov;
    struct cmsghdr *cmsg;
    int val, fd;
    char buf[CMSG_SPACE (sizeof (fd))];

    hdr.msg_name = NULL;
    hdr.msg_namelen = 0;
    hdr.msg_iov = &iov;
    hdr.msg_iovlen = 1;
    hdr.msg_control = buf;
    hdr.msg_controllen = sizeof (buf);

    iov.iov_base = &val;
    iov.iov_len = sizeof (val);

    if (recvmsg (p, &hdr, 0) != sizeof (val))
        return -1;

    for (cmsg = CMSG_FIRSTHDR (&hdr); cmsg != NULL;
         cmsg = CMSG_NXTHDR (&hdr, cmsg))
    {
        if ((cmsg->cmsg_level == SOL_SOCKET)
         && (cmsg->cmsg_type = SCM_RIGHTS)
         && (cmsg->cmsg_len >= CMSG_LEN (sizeof (fd))))
        {
            memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd));
            return fd;
        }
    }

    return -1;
}

/**
 * Tries to obtain a bound TCP socket from the root process
 */
int rootwrap_bind (int family, int socktype, int protocol,
                   const struct sockaddr *addr, size_t alen)
{
    /* can't use libvlc */
    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

    struct sockaddr_storage ss;
    int fd;

    if (rootwrap_sock == -1)
    {
        errno = EACCES;
        return -1;
    }

    switch (family)
    {
        case AF_INET:
            if (alen < sizeof (struct sockaddr_in))
            {
                errno = EINVAL;
                return -1;
            }
            break;

#ifdef AF_INET6
        case AF_INET6:
            if (alen < sizeof (struct sockaddr_in6))
            {
                errno = EINVAL;
                return -1;
            }
            break;
#endif

        default:
            errno = EAFNOSUPPORT;
            return -1;
    }

    if (family != addr->sa_family)
    {
        errno = EAFNOSUPPORT;
        return -1;
    }

    /* Only TCP is implemented at the moment */
    if ((socktype != SOCK_STREAM)
     || (protocol && (protocol != IPPROTO_TCP)))
    {
        errno = EACCES;
        return -1;
    }

    memset (&ss, 0, sizeof (ss));
    memcpy (&ss, addr, alen > sizeof (ss) ? sizeof (ss) : alen);

    pthread_mutex_lock (&mutex);
    if (send (rootwrap_sock, &ss, sizeof (ss), 0) != sizeof (ss))
        return -1;

    fd = recv_fd (rootwrap_sock);
    pthread_mutex_unlock (&mutex);

    if (fd != -1)
    {
        int val;

        val = fcntl (fd, F_GETFL, 0);
        fcntl (fd, F_SETFL, ((val != -1) ? val : 0) | O_NONBLOCK);
    }

    return fd;
}

#else
# include <stddef.h>

struct sockaddr;

void rootwrap (void)
{
}

int rootwrap_bind (int family, int socktype, int protocol,
                   const struct sockaddr *addr, size_t alen)
{
445
446
447
448
449
    (void)family;
    (void)socktype;
    (void)protocol;
    (void)addr;
    (void)alen;
450
451
452
453
    return -1;
}

#endif /* ENABLE_ROOTWRAP */