@@ -992,6 +992,175 @@ if test x"$rsync_cv_HAVE_DIRFD" = x"yes"; then
992992 AC_DEFINE ( [ HAVE_DIRFD] , 1 , [ Define to 1 if you have a working dirfd() (function or macro).] )
993993fi
994994
995+ dnl The confined resolver needs a directory descriptor which can be opened with
996+ dnl search permission alone and which still rechecks search permission when it
997+ dnl is used. A defined O_SEARCH is not sufficient: NetBSD 11 discards it, and
998+ dnl POSIX permits an O_SEARCH descriptor to bypass later permission checks.
999+ AC_CACHE_CHECK ( [ for race-safe search-only directory descriptors] ,
1000+ [ rsync_cv_search_only_dirfd_flag] , [
1001+ rm -f conftest.search-only
1002+ AC_RUN_IFELSE ( [ AC_LANG_SOURCE ( [ [
1003+ #define _GNU_SOURCE 1
1004+ #include <sys/types.h>
1005+ #include <sys/stat.h>
1006+ #include <errno.h>
1007+ #include <fcntl.h>
1008+ #include <stdio.h>
1009+ #include <stdlib.h>
1010+ #include <unistd.h>
1011+
1012+ #ifndef O_CLOEXEC
1013+ #define O_CLOEXEC 0
1014+ #endif
1015+
1016+ static int check_flag(int flag)
1017+ {
1018+ struct stat expected, observed;
1019+ char base[ ] = "/tmp/rsync-search-only.XXXXXX";
1020+ int root = -1, control = -1, cwd = -1, fd = -1, child = -1;
1021+ int dropped = 0, result = 1;
1022+ gid_t saved_gid = getegid();
1023+
1024+ if (!mkdtemp(base))
1025+ goto out;
1026+ root = open(base, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
1027+ cwd = open(".", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
1028+ if (root < 0 || cwd < 0 || mkdirat(root, "dir", 0700) < 0
1029+ || symlinkat("dir", root, "link") < 0)
1030+ goto out;
1031+ control = openat(root, "dir", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
1032+ if (control < 0 || fstat(control, &expected) < 0)
1033+ goto out;
1034+ child = openat(control, "known", O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0600);
1035+ if (child < 0 || write(child, "x", 1) != 1)
1036+ goto out;
1037+ if (geteuid() == 0) {
1038+ if (fchown(root, 65534, 65534) < 0
1039+ || fchown(control, 65534, 65534) < 0
1040+ || fchown(child, 65534, 65534) < 0)
1041+ goto out;
1042+ }
1043+ if (close(child) < 0)
1044+ goto out;
1045+ child = -1;
1046+ if (geteuid() == 0) {
1047+ if (setegid(65534) < 0)
1048+ goto out;
1049+ if (seteuid(65534) < 0) {
1050+ setegid(saved_gid);
1051+ goto out;
1052+ }
1053+ dropped = 1;
1054+ }
1055+ if (fchmod(control, 0100) < 0)
1056+ goto out;
1057+ fd = openat(root, "dir", flag | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
1058+ if (fd < 0 || fstat(fd, &observed) < 0
1059+ || expected.st_dev != observed.st_dev || expected.st_ino != observed.st_ino)
1060+ goto out;
1061+ child = openat(fd, "known", O_RDONLY | O_CLOEXEC);
1062+ if (child < 0 || close(child) < 0)
1063+ goto out;
1064+ child = -1;
1065+ if (fchmod(control, 0300) < 0)
1066+ goto out;
1067+ child = openat(fd, "new", O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0600);
1068+ if (child < 0 || close(child) < 0 || unlinkat(fd, "new", 0) < 0)
1069+ goto out;
1070+ child = -1;
1071+ if (fchmod(control, 0000) < 0)
1072+ goto out;
1073+ child = openat(fd, "known", O_RDONLY | O_CLOEXEC);
1074+ if (child >= 0 || (errno != EACCES && errno != EPERM))
1075+ goto out;
1076+ if (fchdir(fd) == 0) {
1077+ fchdir(cwd);
1078+ goto out;
1079+ }
1080+ if (errno != EACCES && errno != EPERM)
1081+ goto out;
1082+ if (fchmod(control, 0700) < 0)
1083+ goto out;
1084+ child = openat(root, "link", flag | O_DIRECTORY | O_CLOEXEC);
1085+ if (child < 0 || close(child) < 0)
1086+ goto out;
1087+ child = -1;
1088+ child = openat(root, "link", flag | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
1089+ if (child >= 0)
1090+ goto out;
1091+ result = 0;
1092+ out:
1093+ if (dropped && (seteuid(0) < 0 || setegid(saved_gid) < 0))
1094+ result = 1;
1095+ if (child >= 0 && close(child) < 0)
1096+ result = 1;
1097+ if (fd >= 0 && close(fd) < 0)
1098+ result = 1;
1099+ if (control >= 0) {
1100+ if (fchmod(control, 0700) < 0)
1101+ result = 1;
1102+ if (unlinkat(control, "new", 0) < 0 && errno != ENOENT)
1103+ result = 1;
1104+ if (unlinkat(control, "known", 0) < 0 && errno != ENOENT)
1105+ result = 1;
1106+ if (close(control) < 0)
1107+ result = 1;
1108+ }
1109+ if (root >= 0) {
1110+ if (unlinkat(root, "link", 0) < 0 && errno != ENOENT)
1111+ result = 1;
1112+ if (unlinkat(root, "dir", AT_REMOVEDIR) < 0 && errno != ENOENT)
1113+ result = 1;
1114+ if (close(root) < 0)
1115+ result = 1;
1116+ }
1117+ if (cwd >= 0 && close(cwd) < 0)
1118+ result = 1;
1119+ if (rmdir(base) < 0 && errno != ENOENT)
1120+ result = 1;
1121+ return result;
1122+ }
1123+
1124+ int main(void)
1125+ {
1126+ const char *name = NULL;
1127+ #ifdef O_PATH
1128+ if (check_flag(O_PATH) == 0)
1129+ name = "O_PATH";
1130+ #endif
1131+ #ifdef O_SEARCH
1132+ if (!name && check_flag(O_SEARCH) == 0)
1133+ name = "O_SEARCH";
1134+ #endif
1135+ if (name) {
1136+ FILE *out = fopen("conftest.search-only", "w");
1137+ if (!out || fprintf(out, "%s\n", name) < 0 || fclose(out) < 0)
1138+ return 1;
1139+ return 0;
1140+ }
1141+ return 1;
1142+ }
1143+ ] ] ) ] ,
1144+ [ rsync_cv_search_only_dirfd_flag=`cat conftest.search-only`] ,
1145+ [ rsync_cv_search_only_dirfd_flag=no] ,
1146+ [ case "$host_os" in
1147+ linux*|android*)
1148+ AC_LINK_IFELSE ( [ AC_LANG_PROGRAM ( [ [
1149+ #define _GNU_SOURCE 1
1150+ #include <fcntl.h>
1151+ ] ] , [ [ return openat(AT_FDCWD, ".", O_PATH | O_DIRECTORY | O_NOFOLLOW) < -1;] ] ) ] ,
1152+ [ rsync_cv_search_only_dirfd_flag=O_PATH] ,
1153+ [ rsync_cv_search_only_dirfd_flag=no] )
1154+ ;;
1155+ *) rsync_cv_search_only_dirfd_flag=no ;;
1156+ esac] )
1157+ rm -f conftest.search-only
1158+ ] )
1159+ if test x"$rsync_cv_search_only_dirfd_flag" != x"no"; then
1160+ AC_DEFINE_UNQUOTED ( [ SEARCH_ONLY_DIRFD_FLAG] , [ $rsync_cv_search_only_dirfd_flag] ,
1161+ [ Open flag for race-safe search-only directory descriptors.] )
1162+ fi
1163+
9951164dnl cygwin iconv.h defines iconv_open as libiconv_open
9961165if test x"$ac_cv_func_iconv_open" != x"yes"; then
9971166 AC_CHECK_FUNC ( libiconv_open , [ ac_cv_func_iconv_open=yes; AC_DEFINE ( HAVE_ICONV_OPEN , 1 ) ] )
0 commit comments