| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include "rsync.h" |
| 8 |
|
| 9 |
#include <sys/stat.h> |
| 10 |
|
| 11 |
int dry_run = 0; |
| 12 |
int am_root = 0; |
| 13 |
int am_sender = 0; |
| 14 |
int read_only = 0; |
| 15 |
int list_only = 0; |
| 16 |
int copy_links = 0; |
| 17 |
int copy_unsafe_links = 0; |
| 18 |
extern int am_daemon, am_chrooted; |
| 19 |
|
| 20 |
short info_levels[COUNT_INFO], debug_levels[COUNT_DEBUG]; |
| 21 |
|
| 22 |
static int errs = 0; |
| 23 |
|
| 24 |
#ifdef AT_FDCWD |
| 25 |
|
| 26 |
|
| 27 |
static int vulnerable_mixed_rename_at(const char *old_path, const char *new_path) |
| 28 |
{ |
| 29 |
const char *old_slash, *new_slash; |
| 30 |
|
| 31 |
if (!old_path || !*old_path || *old_path == '/' |
| 32 |
|| !new_path || !*new_path || *new_path == '/') |
| 33 |
return do_rename(old_path, new_path); |
| 34 |
|
| 35 |
old_slash = strrchr(old_path, '/'); |
| 36 |
new_slash = strrchr(new_path, '/'); |
| 37 |
if (!old_slash || !new_slash) |
| 38 |
return do_rename(old_path, new_path); |
| 39 |
|
| 40 |
return do_rename_at(old_path, new_path); |
| 41 |
} |
| 42 |
#endif |
| 43 |
|
| 44 |
static void check_exists(const char *label, const char *path, int expect_exists) |
| 45 |
{ |
| 46 |
int exists = access(path, F_OK) == 0; |
| 47 |
|
| 48 |
if (exists != expect_exists) { |
| 49 |
fprintf(stderr, "FAIL [%s]: %s %s, expected %s\n", |
| 50 |
label, path, exists ? "exists" : "does not exist", |
| 51 |
expect_exists ? "exists" : "does not exist"); |
| 52 |
errs++; |
| 53 |
return; |
| 54 |
} |
| 55 |
fprintf(stderr, "OK [%s]: %s %s\n", |
| 56 |
label, path, exists ? "exists" : "does not exist"); |
| 57 |
} |
| 58 |
|
| 59 |
static void check_rename(const char *label, const char *old_path, |
| 60 |
const char *new_path, int expect_ok) |
| 61 |
{ |
| 62 |
int rc; |
| 63 |
int got_ok; |
| 64 |
int saved_errno; |
| 65 |
|
| 66 |
errno = 0; |
| 67 |
rc = do_rename_at(old_path, new_path); |
| 68 |
saved_errno = errno; |
| 69 |
got_ok = rc == 0; |
| 70 |
|
| 71 |
if (got_ok != expect_ok) { |
| 72 |
fprintf(stderr, "FAIL [%s]: rename %s -> %s rc=%d errno=%d (%s), expected %s\n", |
| 73 |
label, old_path, new_path, rc, saved_errno, |
| 74 |
strerror(saved_errno), expect_ok ? "success" : "rejection"); |
| 75 |
errs++; |
| 76 |
return; |
| 77 |
} |
| 78 |
fprintf(stderr, "OK [%s]: rename %s -> %s %s\n", |
| 79 |
label, old_path, new_path, expect_ok ? "succeeded" : "rejected"); |
| 80 |
} |
| 81 |
|
| 82 |
static void check_vulnerable_rename(const char *label, const char *old_path, |
| 83 |
const char *new_path) |
| 84 |
{ |
| 85 |
#ifdef AT_FDCWD |
| 86 |
int rc; |
| 87 |
int saved_errno; |
| 88 |
|
| 89 |
errno = 0; |
| 90 |
rc = vulnerable_mixed_rename_at(old_path, new_path); |
| 91 |
saved_errno = errno; |
| 92 |
|
| 93 |
if (rc != 0) { |
| 94 |
fprintf(stderr, "FAIL [%s]: vulnerable rename %s -> %s rc=%d errno=%d (%s), expected escape\n", |
| 95 |
label, old_path, new_path, rc, saved_errno, |
| 96 |
strerror(saved_errno)); |
| 97 |
errs++; |
| 98 |
return; |
| 99 |
} |
| 100 |
fprintf(stderr, "OK [%s]: vulnerable rename %s -> %s escaped\n", |
| 101 |
label, old_path, new_path); |
| 102 |
#else |
| 103 |
fprintf(stderr, "SKIP [%s]: AT_FDCWD not available\n", label); |
| 104 |
#endif |
| 105 |
} |
| 106 |
|
| 107 |
static int run_escape_poc(const char *module_dir) |
| 108 |
{ |
| 109 |
#ifndef AT_FDCWD |
| 110 |
fprintf(stderr, "SKIP: AT_FDCWD not available\n"); |
| 111 |
return 77; |
| 112 |
#else |
| 113 |
if (chdir(module_dir) < 0) { |
| 114 |
perror("chdir"); |
| 115 |
return 2; |
| 116 |
} |
| 117 |
|
| 118 |
am_daemon = 1; |
| 119 |
am_chrooted = 0; |
| 120 |
|
| 121 |
check_vulnerable_rename("P1: 3.4.3-style top-level source to escaping destination parent", |
| 122 |
"poc-top-to-escape", "escape_link/vuln-created"); |
| 123 |
check_exists("P1 source consumed", "poc-top-to-escape", 0); |
| 124 |
check_exists("P1 outside destination created", "../trap/vuln-created", 1); |
| 125 |
|
| 126 |
check_vulnerable_rename("P2: 3.4.3-style escaping source parent to top-level destination", |
| 127 |
"escape_link/poc-outside-source", "vuln-stolen"); |
| 128 |
check_exists("P2 outside source consumed", "../trap/poc-outside-source", 0); |
| 129 |
check_exists("P2 destination created in module", "vuln-stolen", 1); |
| 130 |
|
| 131 |
check_rename("P3: fixed top-level source to escaping destination parent", |
| 132 |
"fixed-top-to-escape", "escape_link/fixed-created", 0); |
| 133 |
check_exists("P3 source preserved", "fixed-top-to-escape", 1); |
| 134 |
check_exists("P3 outside destination absent", "../trap/fixed-created", 0); |
| 135 |
|
| 136 |
check_rename("P4: fixed escaping source parent to top-level destination", |
| 137 |
"escape_link/fixed-outside-source", "fixed-stolen", 0); |
| 138 |
check_exists("P4 outside source preserved", "../trap/fixed-outside-source", 1); |
| 139 |
check_exists("P4 destination absent", "fixed-stolen", 0); |
| 140 |
|
| 141 |
if (errs) |
| 142 |
fprintf(stderr, "%d failure(s)\n", errs); |
| 143 |
return errs ? 1 : 0; |
| 144 |
#endif |
| 145 |
} |
| 146 |
|
| 147 |
int main(int argc, char **argv) |
| 148 |
{ |
| 149 |
if (argc == 3 && strcmp(argv[1], "--poc") == 0) |
| 150 |
return run_escape_poc(argv[2]); |
| 151 |
|
| 152 |
if (argc != 2) { |
| 153 |
fprintf(stderr, "usage: %s [--poc] <module-dir>\n", argv[0]); |
| 154 |
return 2; |
| 155 |
} |
| 156 |
|
| 157 |
#ifndef AT_FDCWD |
| 158 |
fprintf(stderr, "SKIP: AT_FDCWD not available\n"); |
| 159 |
return 77; |
| 160 |
#else |
| 161 |
if (chdir(argv[1]) < 0) { |
| 162 |
perror("chdir"); |
| 163 |
return 2; |
| 164 |
} |
| 165 |
|
| 166 |
am_daemon = 1; |
| 167 |
am_chrooted = 0; |
| 168 |
|
| 169 |
|
| 170 |
check_rename("A: top-level source to slashed destination", |
| 171 |
"top-to-dir", "realdir/top-to-dir", 1); |
| 172 |
check_exists("A source consumed", "top-to-dir", 0); |
| 173 |
check_exists("A destination created", "realdir/top-to-dir", 1); |
| 174 |
|
| 175 |
check_rename("B: slashed source to top-level destination", |
| 176 |
"realdir/dir-to-top", "dir-to-top", 1); |
| 177 |
check_exists("B source consumed", "realdir/dir-to-top", 0); |
| 178 |
check_exists("B destination created", "dir-to-top", 1); |
| 179 |
|
| 180 |
|
| 181 |
check_rename("C: top-level source to escaping destination parent", |
| 182 |
"top-to-escape", "escape_link/new-outside", 0); |
| 183 |
check_exists("C source preserved", "top-to-escape", 1); |
| 184 |
check_exists("C outside destination absent", "../trap/new-outside", 0); |
| 185 |
|
| 186 |
|
| 187 |
check_rename("D: escaping source parent to top-level destination", |
| 188 |
"escape_link/outside-source", "stolen-from-outside", 0); |
| 189 |
check_exists("D outside source preserved", "../trap/outside-source", 1); |
| 190 |
check_exists("D destination absent", "stolen-from-outside", 0); |
| 191 |
|
| 192 |
check_rename("E: shared slashed parent", |
| 193 |
"realdir/same-old", "realdir/same-new", 1); |
| 194 |
check_exists("E source consumed", "realdir/same-old", 0); |
| 195 |
check_exists("E destination created", "realdir/same-new", 1); |
| 196 |
|
| 197 |
check_rename("F: top-level source to top-level destination", |
| 198 |
"top-old", "top-new", 1); |
| 199 |
check_exists("F source consumed", "top-old", 0); |
| 200 |
check_exists("F destination created", "top-new", 1); |
| 201 |
|
| 202 |
if (errs) |
| 203 |
fprintf(stderr, "%d failure(s)\n", errs); |
| 204 |
return errs ? 1 : 0; |
| 205 |
#endif |
| 206 |
} |