Debian Codegraph @

debian/patches/testsuite_interpose_lfs_open_symbols.patch

raw
1 From: Samuel Henrique <[email protected]>
2 Date: Mon, 17 Aug 2026 22:00:00 -0700
3 Subject: testsuite: interpose the large-file spellings of open/openat/fstatat
4
5 Which symbol names this test's LD_PRELOAD hook exports is decided by the
6 compiler that builds it, and which names rsync imports is decided by
7 configure -- and the two do not have to agree.
8
9 Where off_t is not already 64 bits, configure's AC_SYS_LARGEFILE adds
10 -D_FILE_OFFSET_BITS=64 (i386 and alpha in Debian), so glibc redirects every
11 open()/openat()/fstatat() call in rsync to open64(), openat64() and
12 fstatat64(). The hook is compiled by a bare "cc", so on those architectures
13 it defines only the unsuffixed names: the receiver's opens never reach it, no
14 EACCES is injected, the marker the positive control looks for is never
15 written, and the test fails with
16
17 positive control failed: receiver did not open the existing partial file
18 with O_CREAT (rc=0, output='')
19
20 It happens to work on Debian's 64-bit time_t ports (armhf, hppa, powerpc,
21 ...) only by luck: their gcc predefines -D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64,
22 so glibc's __REDIRECT renames the hook's own DEFINITIONS as well and it ends
23 up exporting exactly the *64 names rsync imports.
24
25 Define both spellings explicitly so the hook interposes whichever set the
26 rsync under test was linked against, and #undef the two macros at the top of
27 the hook so that renaming cannot happen -- otherwise, on precisely those ports
28 where the compiler predefines them, open() would be emitted as open64() and
29 collide with the explicit wrapper ("symbol `open64' is already defined"),
30 which would leave the hook unbuildable and the test skipped.
31
32 The new pointers are resolved through hook_resolve(), so the existing
33 nested-dlsym recursion guard covers them as well.
34
35 Forwarded: https://github.com/RsyncProject/rsync/pull/1063
36 ---
37 --- a/testsuite/partial-protected-regular-retry-linux_test.py
38 +++ b/testsuite/partial-protected-regular-retry-linux_test.py
39 @@ -37,6 +37,17 @@ if _proto is not None and _proto < 30:
40
41 hook_code = r'''
42 #define _GNU_SOURCE
43 +/* Build the hook itself WITHOUT large-file redirection, whatever the compiler
44 + * defaults to. Debian's armhf/hppa/powerpc gcc predefines
45 + * -D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64 (check with "gcc -v -E -"), and under
46 + * those macros glibc's __REDIRECT renames the DEFINITIONS below -- open()
47 + * becomes open64(), fstatat() becomes __fstatat64_time64() -- which then
48 + * collide with the explicit large-file wrappers further down ("symbol `open64'
49 + * is already defined"). Undefining them here keeps each name declared exactly
50 + * once, so the hook always exports both spellings and interposes whichever set
51 + * the rsync under test was linked against. */
52 +#undef _FILE_OFFSET_BITS
53 +#undef _TIME_BITS
54 #include <sys/stat.h>
55 #include <sys/syscall.h>
56 #include <sys/types.h>
57 @@ -56,6 +67,9 @@ static int (*real_open)(const char *, in
58 static int (*real_openat)(int, const char *, int, ...);
59 static int (*real_fstatat)(int, const char *, struct stat *, int);
60 static int (*real_fxstatat)(int, int, const char *, struct stat *, int);
61 +static int (*real_fstatat64)(int, const char *, struct stat64 *, int);
62 +static int (*real_fxstatat64)(int, int, const char *, struct stat64 *, int);
63 +static int (*real_fstatat64_time64)(int, const char *, struct stat64 *, int);
64
65 /* Resolve on demand rather than trusting our constructor to have run. A
66 * preloaded open() interposes for the whole process the moment the loader maps
67 @@ -77,6 +91,10 @@ static void hook_resolve(void)
68 if (!real_openat) real_openat = dlsym(RTLD_NEXT, "openat");
69 if (!real_fstatat) real_fstatat = dlsym(RTLD_NEXT, "fstatat");
70 if (!real_fxstatat) real_fxstatat = dlsym(RTLD_NEXT, "__fxstatat");
71 + if (!real_fstatat64) real_fstatat64 = dlsym(RTLD_NEXT, "fstatat64");
72 + if (!real_fxstatat64) real_fxstatat64 = dlsym(RTLD_NEXT, "__fxstatat64");
73 + if (!real_fstatat64_time64)
74 + real_fstatat64_time64 = dlsym(RTLD_NEXT, "__fstatat64_time64");
75 hook_resolving = 0;
76 }
77
78 @@ -151,6 +169,10 @@ static int swap_and_deny(void)
79 # define HOOK_TAKES_MODE(f) ((f) & O_CREAT)
80 #endif
81
82 +#ifndef O_LARGEFILE
83 +# define O_LARGEFILE 0
84 +#endif
85 +
86 static int is_victim_write(const char *path, int flags)
87 {
88 return path && strcmp(path, "victim") == 0
89 @@ -231,6 +253,49 @@ int open(const char *path, int flags, ..
90 return fd;
91 }
92
93 +/* --- large-file spellings -------------------------------------------------
94 + * Which names the RECEIVER calls is settled by its own build: where off_t is
95 + * not already 64 bits, configure's AC_SYS_LARGEFILE adds -D_FILE_OFFSET_BITS=64
96 + * (i386, alpha, ...), and distro CPPFLAGS add it -- along with -D_TIME_BITS=64
97 + * -- on the 64-bit time_t ports, so glibc redirects each open()/openat()/
98 + * fstatat() call to open64()/openat64()/fstatat64()/__fstatat64_time64().
99 + * "objdump -T rsync | grep UND" says which set a given build imports.
100 + *
101 + * Which names THIS HOOK exports is a different question with a different
102 + * answer, settled by whatever the "cc" below it defaults to -- see the #undef
103 + * at the top. Nothing keeps the two in step, so define every spelling and let
104 + * the loader match them up. With only the unsuffixed ones the receiver's opens
105 + * sail straight past the hook, no EACCES is ever injected, and the test reports
106 + * "positive control failed" having exercised nothing at all.
107 + *
108 + * O_LARGEFILE is the only thing open64() adds over open(), so the wrappers
109 + * below can hand the call to the unsuffixed interposer above. */
110 +int open64(const char *path, int flags, ...)
111 +{
112 + mode_t mode = 0;
113 +
114 + if (HOOK_TAKES_MODE(flags)) {
115 + va_list ap;
116 + va_start(ap, flags);
117 + mode = (mode_t)va_arg(ap, int);
118 + va_end(ap);
119 + }
120 + return open(path, flags | O_LARGEFILE, mode);
121 +}
122 +
123 +int openat64(int dfd, const char *path, int flags, ...)
124 +{
125 + mode_t mode = 0;
126 +
127 + if (HOOK_TAKES_MODE(flags)) {
128 + va_list ap;
129 + va_start(ap, flags);
130 + mode = (mode_t)va_arg(ap, int);
131 + va_end(ap);
132 + }
133 + return openat(dfd, path, flags | O_LARGEFILE, mode);
134 +}
135 +
136 /* ona_open() decides via fstatat(..., AT_SYMLINK_NOFOLLOW) and refuses a
137 * component owned by neither root nor the euid. Model the attacker as a
138 * different uid so a retry that kept the ownership walk refuses the swap. */
139 @@ -275,6 +340,68 @@ int __fxstatat(int ver, int dfd, const c
140 errno = saved_errno;
141 return rc;
142 }
143 +
144 +/* The stat family's large-file spellings. st_mode and st_uid sit ahead of the
145 + * timestamps in every glibc struct stat layout, so the time32/time64 variants
146 + * of the buffer are interchangeable for the two fields touched here. */
147 +static void model_foreign_owner64(int rc, const char *path, struct stat64 *st)
148 +{
149 + if (rc == 0 && swapped && path && strcmp(path, "pdir") == 0
150 + && S_ISLNK(st->st_mode)) {
151 + st->st_uid = geteuid() + 1;
152 + mark(getenv("RSYNC_PARTIAL_RETRY_FOREIGN_MARKER"));
153 + }
154 +}
155 +
156 +int fstatat64(int dfd, const char *path, struct stat64 *st, int flags)
157 +{
158 + int rc, saved_errno;
159 +
160 + hook_resolve();
161 + if (!real_fstatat64) {
162 + errno = ENOSYS;
163 + return -1;
164 + }
165 + rc = real_fstatat64(dfd, path, st, flags);
166 + saved_errno = errno;
167 + model_foreign_owner64(rc, path, st);
168 + errno = saved_errno;
169 + return rc;
170 +}
171 +
172 +int __fxstatat64(int ver, int dfd, const char *path, struct stat64 *st, int flags)
173 +{
174 + int rc, saved_errno;
175 +
176 + hook_resolve();
177 + if (!real_fxstatat64) {
178 + errno = ENOSYS;
179 + return -1;
180 + }
181 + rc = real_fxstatat64(ver, dfd, path, st, flags);
182 + saved_errno = errno;
183 + model_foreign_owner64(rc, path, st);
184 + errno = saved_errno;
185 + return rc;
186 +}
187 +
188 +/* A 32-bit port built with -D_TIME_BITS=64 (Debian's armhf/armel/hppa/powerpc,
189 + * ...) reaches fstatat() under this third name. */
190 +int __fstatat64_time64(int dfd, const char *path, struct stat64 *st, int flags)
191 +{
192 + int rc, saved_errno;
193 +
194 + hook_resolve();
195 + if (!real_fstatat64_time64) {
196 + errno = ENOSYS;
197 + return -1;
198 + }
199 + rc = real_fstatat64_time64(dfd, path, st, flags);
200 + saved_errno = errno;
201 + model_foreign_owner64(rc, path, st);
202 + errno = saved_errno;
203 + return rc;
204 +}
205
206 __attribute__((constructor)) static void hook_loaded(void)
207 {