File: clickpreload.c

package info (click to toggle)
click 0.5.2-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,372 kB
  • sloc: python: 7,135; ansic: 857; makefile: 441; sh: 236; perl: 26; xml: 11
file content (567 lines) | stat: -rw-r--r-- 17,272 bytes parent folder | download
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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/* Copyright (C) 2013 Canonical Ltd.
 * Author: Colin Watson <cjwatson@ubuntu.com>
 *
 * 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; version 3 of the License.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

/* Stub out a few syscalls that are unhelpful when installing Click
 * packages.  This is roughly akin to the effect of using all of fakechroot,
 * fakeroot, and eatmydata, but a few orders of magnitude simpler.
 */

#define _GNU_SOURCE

/* ensure we see *64 variants and they aren't transparently used */
#undef _TIME_BITS
#undef _FILE_OFFSET_BITS
#define _FILE_OFFSET_BITS 32
#define _LARGEFILE64_SOURCE

#include <assert.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <grp.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif

static int (*libc_chmod) (const char *, mode_t) = (void *) 0;
static int (*libc_chown) (const char *, uid_t, gid_t) = (void *) 0;
static int (*libc_execvp) (const char *, char * const []) = (void *) 0;
static int (*libc_fchmod) (int, mode_t) = (void *) 0;
static int (*libc_fchown) (int, uid_t, gid_t) = (void *) 0;
static FILE *(*libc_fopen) (const char *, const char *) = (void *) 0;
#ifdef __GLIBC__
static FILE *(*libc_fopen64) (const char *, const char *) = (void *) 0;
#endif
static struct group *(*libc_getgrnam) (const char *) = (void *) 0;
static struct passwd *(*libc_getpwnam) (const char *) = (void *) 0;
static int (*libc_lchown) (const char *, uid_t, gid_t) = (void *) 0;
static int (*libc_link) (const char *, const char *) = (void *) 0;
static int (*libc_mkdir) (const char *, mode_t) = (void *) 0;
static int (*libc_mkfifo) (const char *, mode_t) = (void *) 0;
static int (*libc_open) (const char *, int, ...) = (void *) 0;
#ifdef __GLIBC__
static int (*libc_open64) (const char *, int, ...) = (void *) 0;
#endif
static int (*libc_symlink) (const char *, const char *) = (void *) 0;
#ifdef HAVE___XMKNOD
static int (*libc___xmknod) (int, const char *, mode_t, dev_t *) = (void *) 0;
#endif
static int (*libc_mknod) (const char *, mode_t, dev_t) = (void *) 0;
#ifdef HAVE___XSTAT
static int (*libc___xstat) (int, const char *, struct stat *) = (void *) 0;
#endif
#ifdef HAVE___XSTAT64
static int (*libc___xstat64) (int, const char *, struct stat64 *) = (void *) 0;
#endif
static int (*libc_stat) (const char *, struct stat *) = (void *) 0;
#ifdef __GLIBC__
static int (*libc_stat64) (const char *, struct stat64 *) = (void *) 0;

/*
 * Note: The actual argument of __stat64_time64() is actually struct stat64
 * defined to use time64_t. The type is never exposed externally unless
 * stat{,64}() is going to redirect to that variant. But since we aren't going
 * to dereference it, a forward declaration is fine.
 */
struct stat64_t64;
static int (*libc___stat64_time64) (const char *, struct stat64_t64 *) = (void *) 0;
#endif

uid_t euid;
struct passwd root_pwd;
struct group root_grp;
const char *base_path;
size_t base_path_len;
const char *package_path;
int package_fd;

#define GET_NEXT_SYMBOL(name) \
    do { \
        char *err; \
        libc_##name = dlsym (RTLD_NEXT, #name); \
        if ((err = dlerror ()) != NULL) { \
            fprintf (stderr, \
                     "Error getting address of symbol '" #name "': %s\n", \
                     err); \
            fflush (stderr); \
            _exit (1); \
        } \
    } while (0)

static void __attribute__ ((constructor)) clickpreload_init (void)
{
    const char *package_fd_str;

    /* Clear any old error conditions, albeit unlikely, as per dlsym(2) */
    dlerror ();

    GET_NEXT_SYMBOL (chmod);
    GET_NEXT_SYMBOL (chown);
    GET_NEXT_SYMBOL (execvp);
    GET_NEXT_SYMBOL (fchmod);
    GET_NEXT_SYMBOL (fchown);
    GET_NEXT_SYMBOL (fopen);
#ifdef __GLIBC__
    GET_NEXT_SYMBOL (fopen64);
#endif
    GET_NEXT_SYMBOL (getgrnam);
    GET_NEXT_SYMBOL (getpwnam);
    GET_NEXT_SYMBOL (lchown);
    GET_NEXT_SYMBOL (link);
    GET_NEXT_SYMBOL (mkdir);
    GET_NEXT_SYMBOL (mkfifo);
    GET_NEXT_SYMBOL (open);
#ifdef __GLIBC__
    GET_NEXT_SYMBOL (open64);
#endif
    GET_NEXT_SYMBOL (symlink);
#ifdef HAVE___XMKNOD
    GET_NEXT_SYMBOL (__xmknod);
#endif
    GET_NEXT_SYMBOL (mknod);
#ifdef HAVE___XSTAT
    GET_NEXT_SYMBOL (__xstat);
#endif
#ifdef HAVE___XSTAT64
    GET_NEXT_SYMBOL (__xstat64);
#endif
    GET_NEXT_SYMBOL (stat);
#ifdef __GLIBC__
    GET_NEXT_SYMBOL (stat64);
#if __TIMESIZE == 32
    GET_NEXT_SYMBOL (__stat64_time64);
#endif
#endif

    euid = geteuid ();
    /* dpkg only cares about these fields. */
    root_pwd.pw_uid = 0;
    root_pwd.pw_name = "root";
    root_grp.gr_gid = 0;

    base_path = getenv ("CLICK_BASE_DIR");
    base_path_len = base_path ? strlen (base_path) : 0;

    package_path = getenv ("CLICK_PACKAGE_PATH");
    package_fd_str = getenv ("CLICK_PACKAGE_FD");
    package_fd = atoi (package_fd_str);
}

/* dpkg calls chown/fchown/lchown to set permissions of extracted files.  If
 * we aren't running as root, we don't care.
 */
int chown (const char *path, uid_t owner, gid_t group)
{
    if (euid != 0)
        return 0;

    if (!libc_chown)
        clickpreload_init ();
    return (*libc_chown) (path, owner, group);
}

int fchown (int fd, uid_t owner, gid_t group)
{
    if (euid != 0)
        return 0;

    if (!libc_fchown)
        clickpreload_init ();
    return (*libc_fchown) (fd, owner, group);
}

int lchown (const char *path, uid_t owner, gid_t group)
{
    if (euid != 0)
        return 0;

    if (!libc_lchown)
        clickpreload_init ();
    return (*libc_lchown) (path, owner, group);
}

/* Similarly, we don't much care about passwd/group lookups when we aren't
 * root.  (This could be more sanely replaced by having dpkg cache those
 * lookups itself.)
 */
struct passwd *getpwnam (const char *name)
{
    if (!libc_getpwnam)
        clickpreload_init ();  /* also needed for root_pwd */

    if (euid != 0)
        return &root_pwd;
    return (*libc_getpwnam) (name);
}

struct group *getgrnam (const char *name)
{
    if (!libc_getgrnam)
        clickpreload_init ();  /* also needed for root_grp */

    if (euid != 0)
        return &root_grp;
    return (*libc_getgrnam) (name);
}

/* dpkg calls chroot to run maintainer scripts when --instdir is used (which
 * we use so that we can have independently-rooted filesystem tarballs).
 * However, there is exactly one maintainer script ever used by Click
 * packages, and that's a static preinst which doesn't touch the filesystem
 * except to be executed with /bin/sh.  Chrooting for this causes more
 * problems than it solves.
 */
int chroot (const char *path)
{
    return 0;
}

/* dpkg executes the static preinst.  We don't want it. */
int execvp (const char *file, char * const argv[])
{
    if (strcmp (file, "/.click/tmp.ci/preinst") == 0)
        _exit (0);

    if (!libc_execvp)
        clickpreload_init ();
    return (*libc_execvp) (file, argv);
}

/* dpkg calls fsync/sync_file_range quite a lot.  However, Click packages
 * never correspond to essential system facilities, so it's OK to compromise
 * perfect write reliability in the face of hostile filesystem
 * implementations for performance.
 *
 * (Note that dpkg only started using fsync/sync_file_range relatively
 * recently, and on many reasonable filesystem configurations using those
 * functions buys us nothing; most of dpkg's reliability comes from other
 * strategies, such as careful unpack and renaming into place.)
 */
int fsync (int fd)
{
    return 0;
}

int sync_file_range(int fd, off64_t offset, off64_t nbytes, unsigned int flags)
{
    return 0;
}

/* Sandboxing:
 *
 * We try to insulate against dpkg getting confused enough by malformed
 * archives to write outside the instdir.  This is not full confinement, and
 * generally for system security it should be sufficient to run "click
 * install" as a specialised user; as such we don't necessarily wrap all
 * possible relevant functions here.  The main purpose of this is just to
 * provide a useful error message if dpkg gets confused.
 */

static void clickpreload_assert_path_in_instdir (const char *verb,
                                                 const char *pathname)
{
    if (strncmp (pathname, base_path, base_path_len) == 0 &&
        (pathname[base_path_len] == '\0' || pathname[base_path_len] == '/'))
        return;

    /* When building click in a chroot with pkgbinarymangler, dpkg-deb is in
     * fact a wrapper shell script, and bash checks at startup whether it
     * can open /dev/tty for writing.  This is harmless, so allow it.
     */
    if (strcmp (verb, "write-open") == 0 && strcmp (pathname, "/dev/tty") == 0)
        return;

    fprintf (stderr,
             "Sandbox failure: 'click install' not permitted to %s '%s'\n",
             verb, pathname);
    fflush (stderr);
    exit (1);
}

int link (const char *oldpath, const char *newpath)
{
    if (!libc_link)
        clickpreload_init ();  /* also needed for base_path, base_path_len */

    clickpreload_assert_path_in_instdir ("make hard link", newpath);
    return (*libc_link) (oldpath, newpath);
}

int mkdir (const char *pathname, mode_t mode)
{
    if (!libc_mkdir)
        clickpreload_init ();  /* also needed for base_path, base_path_len */

    clickpreload_assert_path_in_instdir ("mkdir", pathname);
    return (*libc_mkdir) (pathname, mode);
}

int mkfifo (const char *pathname, mode_t mode)
{
    if (!libc_mkfifo)
        clickpreload_init ();  /* also needed for base_path, base_path_len */

    clickpreload_assert_path_in_instdir ("mkfifo", pathname);
    return (*libc_mkfifo) (pathname, mode);
}

int symlink (const char *oldpath, const char *newpath)
{
    if (!libc_symlink)
        clickpreload_init ();  /* also needed for base_path, base_path_len */

    clickpreload_assert_path_in_instdir ("make symbolic link", newpath);
    return (*libc_symlink) (oldpath, newpath);
}

/* As well as write sandboxing, our versions of fopen, open, and stat also
 * trap accesses to the package path and turn them into accesses to a fixed
 * file descriptor instead.  With some cooperation from click.install, this
 * allows dpkg to read packages in paths not readable by the clickpkg user.
 *
 * We cannot do this entirely perfectly.  In particular, we have to seek to
 * the start of the file on open, but the file offset is shared among all
 * duplicates of a file descriptor.  Let's hope that dpkg doesn't open the
 * .deb multiple times and expect to have independent file offsets ...
 */

FILE *fopen (const char *pathname, const char *mode)
{
    int for_reading =
        (strncmp (mode, "r", 1) == 0 && strncmp (mode, "r+", 2) != 0);

    if (!libc_fopen)
        clickpreload_init ();  /* also needed for package_path */

    if (for_reading && package_path && strcmp (pathname, package_path) == 0) {
        int dup_fd = dup (package_fd);
        lseek (dup_fd, 0, SEEK_SET);  /* also changes offset of package_fd */
        return fdopen (dup_fd, mode);
    }

    if (!for_reading)
        clickpreload_assert_path_in_instdir ("write-fdopen", pathname);

    return (*libc_fopen) (pathname, mode);
}

#ifdef __GLIBC__
FILE *fopen64 (const char *pathname, const char *mode)
{
    int for_reading =
        (strncmp (mode, "r", 1) == 0 && strncmp (mode, "r+", 2) != 0);

    if (!libc_fopen64)
        clickpreload_init ();  /* also needed for package_path */

    if (for_reading && package_path && strcmp (pathname, package_path) == 0) {
        int dup_fd = dup (package_fd);
        lseek (dup_fd, 0, SEEK_SET);  /* also changes offset of package_fd */
        return fdopen (dup_fd, mode);
    }

    if (!for_reading)
        clickpreload_assert_path_in_instdir ("write-fdopen", pathname);

    return (*libc_fopen64) (pathname, mode);
}
#endif

int open (const char *pathname, int flags, ...)
{
    int for_writing = ((flags & O_WRONLY) || (flags & O_RDWR));
    mode_t mode = 0;
    int ret;

    if (!libc_open)
        clickpreload_init ();  /* also needed for package_path */

    if (!for_writing && package_path && strcmp (pathname, package_path) == 0) {
        int dup_fd = dup (package_fd);
        lseek (dup_fd, 0, SEEK_SET);  /* also changes offset of package_fd */
        return dup_fd;
    }

    if (for_writing)
        clickpreload_assert_path_in_instdir ("write-open", pathname);

    if (flags & O_CREAT) {
        va_list argv;
        va_start (argv, flags);
        mode = va_arg (argv, mode_t);
        va_end (argv);
    }

    ret = (*libc_open) (pathname, flags, mode);
    return ret;
}

#ifdef __GLIBC__
int open64 (const char *pathname, int flags, ...)
{
    int for_writing = ((flags & O_WRONLY) || (flags & O_RDWR));
    mode_t mode = 0;
    int ret;

    if (!libc_open64)
        clickpreload_init ();  /* also needed for package_path */

    if (!for_writing && package_path && strcmp (pathname, package_path) == 0) {
        int dup_fd = dup (package_fd);
        lseek (dup_fd, 0, SEEK_SET);  /* also changes offset of package_fd */
        return dup_fd;
    }

    if (for_writing)
        clickpreload_assert_path_in_instdir ("write-open", pathname);

    if (flags & O_CREAT) {
        va_list argv;
        va_start (argv, flags);
        mode = va_arg (argv, mode_t);
        va_end (argv);
    }

    ret = (*libc_open64) (pathname, flags, mode);
    return ret;
}
#endif

#ifdef HAVE___XMKNOD
int __xmknod (int ver, const char *pathname, mode_t mode, dev_t *dev)
{
    if (!libc___xmknod)
        clickpreload_init ();  /* also needed for base_path, base_path_len */

    clickpreload_assert_path_in_instdir ("mknod", pathname);
    return (*libc___xmknod) (ver, pathname, mode, dev);
}
#endif

int mknod (const char *pathname, mode_t mode, dev_t dev)
{
    if (!libc_mknod)
        clickpreload_init ();  /* also needed for base_path, base_path_len */

    clickpreload_assert_path_in_instdir ("mknod", pathname);
    return (*libc_mknod) (pathname, mode, dev);
}

#ifdef HAVE___XSTAT
extern int __fxstat (int __ver, int __fildes, struct stat *__stat_buf);

int __xstat (int ver, const char *pathname, struct stat *buf)
{
    if (!libc___xstat)
        clickpreload_init ();  /* also needed for package_path */

    if (package_path && strcmp (pathname, package_path) == 0)
        return __fxstat (ver, package_fd, buf);

    return (*libc___xstat) (ver, pathname, buf);
}
#endif

#ifdef HAVE___XSTAT64
extern int __fxstat64 (int ver, int __fildes, struct stat64 *__stat_buf);

int __xstat64 (int ver, const char *pathname, struct stat64 *buf)
{
    if (!libc___xstat64)
        clickpreload_init ();  /* also needed for package_path */

    if (package_path && strcmp (pathname, package_path) == 0)
        return __fxstat64 (ver, package_fd, buf);

    return (*libc___xstat64) (ver, pathname, buf);
}
#endif

int stat(const char * pathname, struct stat * buf)
{
    if (!libc_stat)
        clickpreload_init ();  /* also needed for package_path */

    if (package_path && strcmp (pathname, package_path) == 0)
        return fstat (package_fd, buf);

    return (*libc_stat) (pathname, buf);
}

#ifdef __GLIBC__
int stat64(const char * pathname, struct stat64 * buf)
{
    if (!libc_stat64)
        clickpreload_init ();  /* also needed for package_path */

    if (package_path && strcmp (pathname, package_path) == 0)
        return fstat64 (package_fd, buf);

    return (*libc_stat64) (pathname, buf);
}

#if __TIMESIZE == 32

extern int __fstat64_time64(int fd, struct stat64_t64 * buf);

int __stat64_time64(const char * pathname, struct stat64_t64 * buf)
{
    if (!libc___stat64_time64)
        clickpreload_init ();  /* also needed for package_path */

    if (package_path && strcmp (pathname, package_path) == 0)
        return __fstat64_time64 (package_fd, buf);

    return (*libc___stat64_time64) (pathname, buf);
}
#endif // __TIMESIZE == 32
#endif // __GLIBC__

/* As well as write sandboxing, our versions of chmod and fchmod also
 * prevent the 0200 (u+w) permission bit from being removed from unpacked
 * files.  dpkg normally expects to be run as root which can override DAC
 * write permissions, so a mode 04xx file is not normally a problem for it,
 * but it is a problem when running dpkg as non-root.  Since unpacked
 * packages are non-writeable from the point of view of the package's code,
 * forcing u+w is safe.
 */

int chmod (const char *path, mode_t mode)
{
    if (!libc_chmod)
        clickpreload_init ();  /* also needed for package_path */

    clickpreload_assert_path_in_instdir ("chmod", path);
    mode |= S_IWUSR;
    return (*libc_chmod) (path, mode);
}

int fchmod (int fd, mode_t mode)
{
    if (!libc_fchmod)
        clickpreload_init ();

    mode |= S_IWUSR;
    return (*libc_fchmod) (fd, mode);
}