| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include "rsync.h" |
| 11 |
|
| 12 |
#include <sys/stat.h> |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#if defined SUPPORT_LINKS && (defined NO_SYMLINK_XATTRS || defined NO_SYMLINK_USER_XATTRS) |
| 18 |
#define TEST_SYMLINK_PLACEHOLDER 1 |
| 19 |
#endif |
| 20 |
|
| 21 |
int dry_run = 0; |
| 22 |
int am_root = -1; |
| 23 |
int am_sender = 0; |
| 24 |
int read_only = 0; |
| 25 |
int list_only = 0; |
| 26 |
int copy_links = 0; |
| 27 |
int copy_unsafe_links = 0; |
| 28 |
extern int am_daemon, am_chrooted; |
| 29 |
|
| 30 |
short info_levels[COUNT_INFO], debug_levels[COUNT_DEBUG]; |
| 31 |
|
| 32 |
static int errs = 0; |
| 33 |
|
| 34 |
static void check_preserved(const char *label, const char *victim, const char *want) |
| 35 |
{ |
| 36 |
char buf[256]; |
| 37 |
int fd = open(victim, O_RDONLY); |
| 38 |
ssize_t n = fd >= 0 ? read(fd, buf, sizeof buf - 1) : -1; |
| 39 |
|
| 40 |
if (fd >= 0) |
| 41 |
close(fd); |
| 42 |
if (n < 0) |
| 43 |
n = 0; |
| 44 |
buf[n] = '\0'; |
| 45 |
if (n > 0 && buf[n-1] == '\n') |
| 46 |
buf[n-1] = '\0'; |
| 47 |
|
| 48 |
if (strcmp(buf, want) != 0) { |
| 49 |
fprintf(stderr, "FAIL [%s]: victim %s = \"%s\", expected \"%s\" " |
| 50 |
"(basename symlink was followed -> module escape)\n", |
| 51 |
label, victim, buf, want); |
| 52 |
errs++; |
| 53 |
return; |
| 54 |
} |
| 55 |
fprintf(stderr, "OK [%s]: victim %s preserved\n", label, victim); |
| 56 |
} |
| 57 |
|
| 58 |
static void check_clobbered(const char *label, const char *victim, const char *unwanted) |
| 59 |
{ |
| 60 |
char buf[256]; |
| 61 |
int fd = open(victim, O_RDONLY); |
| 62 |
ssize_t n = fd >= 0 ? read(fd, buf, sizeof buf - 1) : -1; |
| 63 |
|
| 64 |
if (fd >= 0) |
| 65 |
close(fd); |
| 66 |
if (n < 0) |
| 67 |
n = 0; |
| 68 |
buf[n] = '\0'; |
| 69 |
if (n > 0 && buf[n-1] == '\n') |
| 70 |
buf[n-1] = '\0'; |
| 71 |
|
| 72 |
if (strcmp(buf, unwanted) != 0) { |
| 73 |
fprintf(stderr, "FAIL [%s]: victim %s = \"%s\", expected the escape to write \"%s\"\n", |
| 74 |
label, victim, buf, unwanted); |
| 75 |
errs++; |
| 76 |
return; |
| 77 |
} |
| 78 |
fprintf(stderr, "OK [%s]: victim %s clobbered as expected (escape demonstrated)\n", |
| 79 |
label, victim); |
| 80 |
} |
| 81 |
|
| 82 |
int main(int argc, char **argv) |
| 83 |
{ |
| 84 |
#ifndef AT_FDCWD |
| 85 |
fprintf(stderr, "SKIP: AT_FDCWD not available\n"); |
| 86 |
return 77; |
| 87 |
#else |
| 88 |
int poc = 0; |
| 89 |
const char *moddir; |
| 90 |
|
| 91 |
# if !defined(HAVE_MKNODAT) && !defined(TEST_SYMLINK_PLACEHOLDER) |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
(void)argc; (void)argv; |
| 96 |
fprintf(stderr, "SKIP: no mknodat() and no symlink placeholders -- " |
| 97 |
"nothing this helper asserts applies to this build\n"); |
| 98 |
return 77; |
| 99 |
# endif |
| 100 |
|
| 101 |
if (argc == 3 && strcmp(argv[1], "--poc") == 0) { |
| 102 |
poc = 1; |
| 103 |
moddir = argv[2]; |
| 104 |
} else if (argc == 2) { |
| 105 |
moddir = argv[1]; |
| 106 |
} else { |
| 107 |
fprintf(stderr, "usage: %s [--poc] <module-dir>\n", argv[0]); |
| 108 |
return 2; |
| 109 |
} |
| 110 |
|
| 111 |
if (chdir(moddir) < 0) { |
| 112 |
perror("chdir"); |
| 113 |
return 2; |
| 114 |
} |
| 115 |
|
| 116 |
am_daemon = 1; |
| 117 |
am_chrooted = 0; |
| 118 |
am_root = -1; |
| 119 |
|
| 120 |
if (poc) { |
| 121 |
|
| 122 |
|
| 123 |
#ifdef TEST_SYMLINK_PLACEHOLDER |
| 124 |
do_symlink("VULN_SYM_PAYLOAD", "sympath"); |
| 125 |
check_clobbered("poc do_symlink bare", "../outside/secret_sym", |
| 126 |
"VULN_SYM_PAYLOAD"); |
| 127 |
#endif |
| 128 |
do_mknod("nodpath", S_IFCHR | 0600, 0); |
| 129 |
check_clobbered("poc do_mknod bare", "../outside/secret_nod", ""); |
| 130 |
return errs ? 1 : 0; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
#ifdef TEST_SYMLINK_PLACEHOLDER |
| 136 |
do_symlink_at("FIXED_SYM_PAYLOAD", "sympath"); |
| 137 |
check_preserved("do_symlink_at bare", "../outside/secret_sym", "VICTIM_SYM"); |
| 138 |
|
| 139 |
|
| 140 |
do_symlink_at("FIXED_SYM_PAYLOAD", "sub/sympath2"); |
| 141 |
check_preserved("do_symlink_at slashed", "../outside/secret_sym2", "VICTIM_SYM2"); |
| 142 |
#endif |
| 143 |
|
| 144 |
# ifdef HAVE_MKNODAT |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
do_mknod_at("nodpath", S_IFCHR | 0600, 0); |
| 150 |
check_preserved("do_mknod_at bare", "../outside/secret_nod", "VICTIM_NOD"); |
| 151 |
|
| 152 |
do_mknod_at("sub/nodpath2", S_IFCHR | 0600, 0); |
| 153 |
check_preserved("do_mknod_at slashed", "../outside/secret_nod2", "VICTIM_NOD2"); |
| 154 |
# endif |
| 155 |
|
| 156 |
if (errs) |
| 157 |
fprintf(stderr, "%d failure(s)\n", errs); |
| 158 |
return errs ? 1 : 0; |
| 159 |
#endif |
| 160 |
} |