Skip to content

Commit 013f76d

Browse files
authored
syscall: preserve Cygwin UNC roots in ownership walks (#1113)
* syscall: preserve Cygwin UNC roots in ownership walks * syscall: remove duplicate anchor state update
1 parent f884cfe commit 013f76d

2 files changed

Lines changed: 99 additions & 1 deletion

File tree

‎syscall.c‎

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,65 @@ static int abspath_step(char *abspath, size_t cap, const char *comp, size_t comp
346346
return 0;
347347
}
348348

349+
/* Return the length of a //server/share root, or 0 when path is not a
350+
* complete UNC path. Kept platform-independent so the parser is testable on
351+
* every build host; only Cygwin uses the result for path resolution. */
352+
size_t unc_root_len(const char *path)
353+
{
354+
const char *server_end, *share_end;
355+
356+
if (!path || path[0] != '/' || path[1] != '/' || path[2] == '/')
357+
return 0;
358+
server_end = strchr(path + 2, '/');
359+
if (!server_end || server_end == path + 2 || server_end[1] == '\0'
360+
|| server_end[1] == '/')
361+
return 0;
362+
share_end = strchr(server_end + 1, '/');
363+
return share_end ? (size_t)(share_end - path) : strlen(path);
364+
}
365+
366+
#if defined __CYGWIN__ && defined AT_FDCWD && defined O_NOFOLLOW && defined O_DIRECTORY
367+
/* Pin an absolute path's root and remove it from remaining. Cygwin's UNC
368+
* namespace has a //server/share root which must not be collapsed into /. */
369+
static int ona_open_absolute_root(char *remaining, char *abspath, size_t abspath_cap,
370+
int dir_traverse_flags)
371+
{
372+
size_t root_len = unc_root_len(remaining);
373+
int dfd;
374+
375+
if (root_len) {
376+
char root[MAXPATHLEN];
377+
char *tail;
378+
379+
if (root_len >= sizeof root) {
380+
errno = ENAMETOOLONG;
381+
return -1;
382+
}
383+
memcpy(root, remaining, root_len);
384+
root[root_len] = '\0';
385+
dfd = open(root, dir_traverse_flags);
386+
if (dfd < 0)
387+
return -1;
388+
strlcpy(abspath, root, abspath_cap);
389+
tail = remaining + root_len;
390+
while (*tail == '/')
391+
tail++;
392+
memmove(remaining, tail, strlen(tail) + 1);
393+
return dfd;
394+
}
395+
396+
dfd = open("/", dir_traverse_flags);
397+
if (dfd >= 0) {
398+
char *tail = remaining;
399+
abspath[0] = '\0';
400+
while (*tail == '/')
401+
tail++;
402+
memmove(remaining, tail, strlen(tail) + 1);
403+
}
404+
return dfd;
405+
}
406+
#endif
407+
349408
/* Open an operator-supplied path, refusing to traverse any symlink (parent or
350409
* leaf) not owned by uid 0 or our euid. A trusted-owned symlink (e.g. root's
351410
* /var/log -> /data/log) is still followed; an untrusted one fails ELOOP.
@@ -432,15 +491,22 @@ static int ona_open(const char *path, int flags, mode_t mode, char *out_abs, siz
432491

433492
/* Absolute path: pin "/" as the starting dfd. */
434493
if (remaining[0] == '/') {
494+
#ifdef __CYGWIN__
495+
dfd = ona_open_absolute_root(remaining, abspath, sizeof abspath,
496+
dir_traverse_flags);
497+
#else
435498
dfd = open("/", dir_traverse_flags);
499+
#endif
436500
if (dfd < 0)
437501
return -1;
438502
dfd_owns = 1;
503+
#ifndef __CYGWIN__
439504
abspath[0] = '\0'; /* now resolving from "/" */
440-
is_anchored = 1;
441505
char *p = remaining;
442506
while (*p == '/') p++;
443507
memmove(remaining, p, strlen(p) + 1);
508+
#endif
509+
is_anchored = 1;
444510
}
445511

446512
int loops = 40; /* SYMLOOP_MAX-ish; breaks symlink cycles. Counts symlink
@@ -567,13 +633,20 @@ static int ona_open(const char *path, int flags, mode_t mode, char *out_abs, siz
567633

568634
if (target[0] == '/') {
569635
if (dfd_owns) close(dfd);
636+
#ifdef __CYGWIN__
637+
strlcpy(remaining, rebuilt, sizeof remaining);
638+
dfd = ona_open_absolute_root(remaining, abspath, sizeof abspath,
639+
dir_traverse_flags);
640+
#else
570641
dfd = open("/", dir_traverse_flags);
642+
#endif
571643
if (dfd < 0) {
572644
saved_errno = errno;
573645
dfd_owns = 0;
574646
goto out;
575647
}
576648
dfd_owns = 1;
649+
#ifndef __CYGWIN__
577650
abspath[0] = '\0'; /* followed an absolute target: restart from "/" */
578651
/* "self" resolves to "<pid>", still inside the pin;
579652
* the magic link itself lands elsewhere and ends the
@@ -583,6 +656,7 @@ static int ona_open(const char *path, int flags, mode_t mode, char *out_abs, siz
583656
char *p = rebuilt;
584657
while (*p == '/') p++;
585658
strlcpy(remaining, p, sizeof remaining);
659+
#endif
586660
} else {
587661
strlcpy(remaining, rebuilt, sizeof remaining);
588662
}

‎t_secure_relpath.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ short info_levels[COUNT_INFO], debug_levels[COUNT_DEBUG];
4141

4242
static int errs = 0;
4343

44+
static void check_unc_root(const char *path, size_t expected)
45+
{
46+
size_t got = unc_root_len(path);
47+
48+
if (got != expected) {
49+
fprintf(stderr, "FAIL [UNC root=%s]: got %lu, expected %lu\n",
50+
path ? path : "(null)", (unsigned long)got,
51+
(unsigned long)expected);
52+
errs++;
53+
} else
54+
fprintf(stderr, "OK [UNC root=%s]: %lu\n",
55+
path ? path : "(null)", (unsigned long)got);
56+
}
57+
4458
static void check_relpath(const char *relpath)
4559
{
4660
int fd;
@@ -192,6 +206,16 @@ int main(int argc, char **argv)
192206
am_daemon = 1;
193207
am_chrooted = 0;
194208

209+
check_unc_root("//server/share", strlen("//server/share"));
210+
check_unc_root("//server/share/path", strlen("//server/share"));
211+
check_unc_root("//server/share//path", strlen("//server/share"));
212+
check_unc_root("/server/share", 0);
213+
check_unc_root("///server/share", 0);
214+
check_unc_root("//server", 0);
215+
check_unc_root("//server/", 0);
216+
check_unc_root("//server//path", 0);
217+
check_unc_root(NULL, 0);
218+
195219
mkdir("subdir", 0755);
196220
symlink("subdir", "alias");
197221

0 commit comments

Comments
 (0)