MCPcopy Index your code
hub / github.com/RsyncProject/rsync / do_open_at

Function do_open_at

syscall.c:707–758  ·  view source on GitHub ↗

Symlink-race-safe variant of do_open() for receiver-side use. See the comment on do_chmod_at() for the threat model. open() resolves parent components, so a parent-symlink swap can redirect the open to a file outside the module. This wrapper is defence-in-depth for bare-path do_open() sites that callers know are otherwise protected by secure parent-syscalls (e.g. generator.c's in-place

Source from the content-addressed store, hash-verified

705 pre-planted symlink, which is what we want for O_CREAT|O_EXCL).
706*/
707int do_open_at(const char *pathname, int flags, mode_t mode)
708{
709#ifdef AT_FDCWD
710 extern int am_daemon, am_chrooted;
711 char dirpath[MAXPATHLEN];
712 const char *bname;
713 const char *slash;
714 int dfd, ret, e;
715 size_t dlen;
716
717 if (flags != O_RDONLY) {
718 RETURN_ERROR_IF(dry_run, 0);
719 RETURN_ERROR_IF_RO_OR_LO;
720 }
721
722 if (!am_daemon || am_chrooted)
723 return do_open(pathname, flags, mode);
724
725 if (!pathname || !*pathname || *pathname == '/')
726 return do_open(pathname, flags, mode);
727
728 slash = strrchr(pathname, '/');
729 if (!slash)
730 return do_open(pathname, flags, mode);
731
732 dlen = slash - pathname;
733 if (dlen >= sizeof dirpath) {
734 errno = ENAMETOOLONG;
735 return -1;
736 }
737 memcpy(dirpath, pathname, dlen);
738 dirpath[dlen] = '\0';
739 bname = slash + 1;
740
741 dfd = secure_relative_open(NULL, dirpath, O_RDONLY | O_DIRECTORY, 0);
742 if (dfd < 0)
743 return -1;
744
745#ifdef O_NOATIME
746 if (open_noatime)
747 flags |= O_NOATIME;
748#endif
749
750 ret = openat(dfd, bname, flags | O_NOFOLLOW | O_BINARY, mode);
751 e = errno;
752 close(dfd);
753 errno = e;
754 return ret;
755#else
756 return do_open(pathname, flags, mode);
757#endif
758}
759
760#ifdef HAVE_CHMOD
761int do_chmod(const char *path, mode_t mode)

Callers 2

unlink_and_reopenFunction · 0.85
recv_generatorFunction · 0.85

Calls 2

do_openFunction · 0.85
secure_relative_openFunction · 0.85

Tested by

no test coverage detected