| 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 |
#include "rsync.h" |
| 28 |
|
| 29 |
#include <sys/stat.h> |
| 30 |
|
| 31 |
int dry_run = 0; |
| 32 |
int am_root = 0; |
| 33 |
int am_sender = 0; |
| 34 |
int read_only = 0; |
| 35 |
int list_only = 0; |
| 36 |
int copy_links = 0; |
| 37 |
int copy_unsafe_links = 0; |
| 38 |
extern int am_daemon, am_chrooted; |
| 39 |
|
| 40 |
short info_levels[COUNT_INFO], debug_levels[COUNT_DEBUG]; |
| 41 |
|
| 42 |
static int errs = 0; |
| 43 |
|
| 44 |
static void check_relpath(const char *relpath) |
| 45 |
{ |
| 46 |
int fd; |
| 47 |
int saved_errno; |
| 48 |
|
| 49 |
errno = 0; |
| 50 |
fd = secure_relative_open(NULL, relpath, O_RDONLY | O_DIRECTORY, 0); |
| 51 |
saved_errno = errno; |
| 52 |
|
| 53 |
if (fd >= 0) { |
| 54 |
fprintf(stderr, |
| 55 |
"FAIL [relpath=%-12s]: returned valid fd %d (escape) -- expected -1 EINVAL\n", |
| 56 |
relpath, fd); |
| 57 |
close(fd); |
| 58 |
errs++; |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
if (saved_errno != EINVAL) { |
| 63 |
fprintf(stderr, |
| 64 |
"FAIL [relpath=%-12s]: rejected but errno=%d (%s), expected EINVAL\n", |
| 65 |
relpath, saved_errno, strerror(saved_errno)); |
| 66 |
errs++; |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
fprintf(stderr, "OK [relpath=%-12s]: rejected with EINVAL\n", relpath); |
| 71 |
} |
| 72 |
|
| 73 |
static void check_basedir(const char *basedir) |
| 74 |
{ |
| 75 |
int fd; |
| 76 |
int saved_errno; |
| 77 |
|
| 78 |
errno = 0; |
| 79 |
fd = secure_relative_open(basedir, "ok", O_RDONLY | O_DIRECTORY, 0); |
| 80 |
saved_errno = errno; |
| 81 |
|
| 82 |
if (fd >= 0) { |
| 83 |
fprintf(stderr, |
| 84 |
"FAIL [basedir=%-12s]: returned valid fd %d -- expected -1 EINVAL\n", |
| 85 |
basedir, fd); |
| 86 |
close(fd); |
| 87 |
errs++; |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
if (saved_errno != EINVAL) { |
| 92 |
fprintf(stderr, |
| 93 |
"FAIL [basedir=%-12s]: rejected but errno=%d (%s), expected EINVAL\n", |
| 94 |
basedir, saved_errno, strerror(saved_errno)); |
| 95 |
errs++; |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
fprintf(stderr, "OK [basedir=%-12s]: rejected with EINVAL\n", basedir); |
| 100 |
} |
| 101 |
|
| 102 |
static void check_beneath_dotdot(void) |
| 103 |
{ |
| 104 |
STRUCT_STAT ast, fst; |
| 105 |
int anchor, fd; |
| 106 |
|
| 107 |
anchor = open(".", O_RDONLY | O_DIRECTORY); |
| 108 |
if (anchor < 0 || fstat(anchor, &ast) < 0) { |
| 109 |
perror("open/fstat anchor"); |
| 110 |
errs++; |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
fd = secure_relative_open_at_beneath(anchor, "alias/../subdir", |
| 115 |
O_RDONLY | O_DIRECTORY, 0); |
| 116 |
if (fd < 0 || fstat(fd, &fst) < 0 || fst.st_dev != ast.st_dev |
| 117 |
|| fst.st_ino == ast.st_ino) { |
| 118 |
fprintf(stderr, "FAIL [beneath safe]: in-anchor '..' was not resolved\n"); |
| 119 |
errs++; |
| 120 |
} else |
| 121 |
fprintf(stderr, "OK [beneath safe]: in-anchor '..' resolved\n"); |
| 122 |
if (fd >= 0) |
| 123 |
close(fd); |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
{ |
| 132 |
static const struct { const char *label; int flags; } dotdot_cases[] = { |
| 133 |
{ "O_DIRECTORY", O_RDONLY | O_DIRECTORY }, |
| 134 |
{ "O_DIRECTORY|O_NOFOLLOW", O_RDONLY | O_DIRECTORY | O_NOFOLLOW }, |
| 135 |
{ "no O_DIRECTORY", O_RDONLY }, |
| 136 |
}; |
| 137 |
unsigned ci; |
| 138 |
for (ci = 0; ci < sizeof dotdot_cases / sizeof *dotdot_cases; ci++) { |
| 139 |
int dfd; |
| 140 |
errno = 0; |
| 141 |
dfd = secure_relative_open_at_beneath(anchor, "..", |
| 142 |
dotdot_cases[ci].flags, 0); |
| 143 |
if (dfd >= 0) { |
| 144 |
STRUCT_STAT dst; |
| 145 |
int above = fstat(dfd, &dst) == 0 && dst.st_ino != ast.st_ino; |
| 146 |
fprintf(stderr, "FAIL [beneath bare-dotdot %s]: rc=%d%s\n", |
| 147 |
dotdot_cases[ci].label, dfd, |
| 148 |
above ? " -- resolved ABOVE the anchor" : ""); |
| 149 |
errs++; |
| 150 |
close(dfd); |
| 151 |
} else if (errno != ELOOP) { |
| 152 |
fprintf(stderr, "FAIL [beneath bare-dotdot %s]: errno=%d, expected ELOOP\n", |
| 153 |
dotdot_cases[ci].label, errno); |
| 154 |
errs++; |
| 155 |
} else |
| 156 |
fprintf(stderr, "OK [beneath bare-dotdot %s]: refused with ELOOP\n", |
| 157 |
dotdot_cases[ci].label); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
errno = 0; |
| 162 |
fd = secure_relative_open_at_beneath(anchor, "../outside", |
| 163 |
O_RDONLY | O_DIRECTORY, 0); |
| 164 |
if (fd >= 0 || errno != ELOOP) { |
| 165 |
fprintf(stderr, "FAIL [beneath escape]: rc=%d errno=%d, expected -1/ELOOP\n", |
| 166 |
fd, errno); |
| 167 |
if (fd >= 0) |
| 168 |
close(fd); |
| 169 |
errs++; |
| 170 |
} else |
| 171 |
fprintf(stderr, "OK [beneath escape]: climb above anchor refused\n"); |
| 172 |
|
| 173 |
close(anchor); |
| 174 |
} |
| 175 |
|
| 176 |
int main(int argc, char **argv) |
| 177 |
{ |
| 178 |
if (argc != 2) { |
| 179 |
fprintf(stderr, "usage: %s <test-dir>\n", argv[0]); |
| 180 |
return 2; |
| 181 |
} |
| 182 |
if (chdir(argv[1]) < 0) { |
| 183 |
perror("chdir"); |
| 184 |
return 2; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
am_daemon = 1; |
| 193 |
am_chrooted = 0; |
| 194 |
|
| 195 |
mkdir("subdir", 0755); |
| 196 |
symlink("subdir", "alias"); |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
check_relpath(".."); |
| 207 |
check_relpath("../foo"); |
| 208 |
check_relpath("subdir/.."); |
| 209 |
check_relpath("subdir/../subdir"); |
| 210 |
check_relpath("foo/../bar"); |
| 211 |
check_relpath("/foo"); |
| 212 |
check_relpath("/"); |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
check_basedir(".."); |
| 219 |
check_basedir("../subdir"); |
| 220 |
check_basedir("subdir/.."); |
| 221 |
check_basedir("foo/../bar"); |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
check_beneath_dotdot(); |
| 227 |
|
| 228 |
if (errs) |
| 229 |
fprintf(stderr, "\n%d failure(s)\n", errs); |
| 230 |
return errs ? 1 : 0; |
| 231 |
} |