@@ -110,7 +110,9 @@ class LoggingManager {
110110 self .assertEqual (result .returncode , 0 , f"Compilation failed: { result .stderr } " )
111111
112112 result = subprocess .run ([exe_path ], check = False , capture_output = True , text = True , timeout = 10 )
113- self .assertEqual (result .returncode , 0 , f"Program crashed with exit code { result .returncode } : { result .stderr } " )
113+ self .assertEqual (
114+ result .returncode , 0 , f"Program crashed with exit code { result .returncode } : { result .stderr } "
115+ )
114116 self .assertIn ("PASS" , result .stdout )
115117 finally :
116118 os .unlink (src_path )
@@ -202,9 +204,9 @@ def test_sysconf_fallback(self):
202204 def test_sysfs_hide_with_ld_preload (self ):
203205 """Verify LD_PRELOAD shim can hide sysfs files.
204206
205- This compiles a small shim that intercepts fopen to return ENOENT
206- for /sys/devices/system/cpu/{possible,present}, then runs a test program
207- that tries to read those files.
207+ This compiles a small shim that intercepts open-family calls to return
208+ ENOENT for /sys/devices/system/cpu/{possible,present}, then runs a test
209+ program that opens those files.
208210 """
209211 _require_linux ()
210212 _require_gcc ()
@@ -213,8 +215,15 @@ def test_sysfs_hide_with_ld_preload(self):
213215 #define _GNU_SOURCE
214216 #include <dlfcn.h>
215217 #include <errno.h>
216- #include <string.h>
218+ #include <fcntl.h>
219+ #include <stdarg.h>
217220 #include <stdio.h>
221+ #include <string.h>
222+ #include <sys/types.h>
223+
224+ #ifndef O_TMPFILE
225+ #define O_TMPFILE 0
226+ #endif
218227
219228 static const char *CPU_POSSIBLE = "/sys/devices/system/cpu/possible";
220229 static const char *CPU_PRESENT = "/sys/devices/system/cpu/present";
@@ -223,6 +232,86 @@ def test_sysfs_hide_with_ld_preload(self):
223232 return (strcmp(path, CPU_POSSIBLE) == 0 || strcmp(path, CPU_PRESENT) == 0);
224233 }
225234
235+ static mode_t get_mode_if_needed(int flags, va_list args) {
236+ return ((flags & O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE)) ? va_arg(args, mode_t) : 0;
237+ }
238+
239+ int open(const char *path, int flags, ...) {
240+ static int (*real_open)(const char *, int, ...) = NULL;
241+ va_list args;
242+ mode_t mode = 0;
243+
244+ if (!real_open) real_open = dlsym(RTLD_NEXT, "open");
245+ if (is_blocked(path)) {
246+ errno = ENOENT;
247+ return -1;
248+ }
249+
250+ va_start(args, flags);
251+ mode = get_mode_if_needed(flags, args);
252+ va_end(args);
253+ return ((flags & O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE))
254+ ? real_open(path, flags, mode)
255+ : real_open(path, flags);
256+ }
257+
258+ int open64(const char *path, int flags, ...) {
259+ static int (*real_open64)(const char *, int, ...) = NULL;
260+ va_list args;
261+ mode_t mode = 0;
262+
263+ if (!real_open64) real_open64 = dlsym(RTLD_NEXT, "open64");
264+ if (is_blocked(path)) {
265+ errno = ENOENT;
266+ return -1;
267+ }
268+
269+ va_start(args, flags);
270+ mode = get_mode_if_needed(flags, args);
271+ va_end(args);
272+ return ((flags & O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE))
273+ ? real_open64(path, flags, mode)
274+ : real_open64(path, flags);
275+ }
276+
277+ int openat(int dirfd, const char *path, int flags, ...) {
278+ static int (*real_openat)(int, const char *, int, ...) = NULL;
279+ va_list args;
280+ mode_t mode = 0;
281+
282+ if (!real_openat) real_openat = dlsym(RTLD_NEXT, "openat");
283+ if (path && is_blocked(path)) {
284+ errno = ENOENT;
285+ return -1;
286+ }
287+
288+ va_start(args, flags);
289+ mode = get_mode_if_needed(flags, args);
290+ va_end(args);
291+ return ((flags & O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE))
292+ ? real_openat(dirfd, path, flags, mode)
293+ : real_openat(dirfd, path, flags);
294+ }
295+
296+ int openat64(int dirfd, const char *path, int flags, ...) {
297+ static int (*real_openat64)(int, const char *, int, ...) = NULL;
298+ va_list args;
299+ mode_t mode = 0;
300+
301+ if (!real_openat64) real_openat64 = dlsym(RTLD_NEXT, "openat64");
302+ if (path && is_blocked(path)) {
303+ errno = ENOENT;
304+ return -1;
305+ }
306+
307+ va_start(args, flags);
308+ mode = get_mode_if_needed(flags, args);
309+ va_end(args);
310+ return ((flags & O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE))
311+ ? real_openat64(dirfd, path, flags, mode)
312+ : real_openat64(dirfd, path, flags);
313+ }
314+
226315 FILE *fopen(const char *restrict path, const char *restrict mode) {
227316 static FILE *(*real_fopen)(const char *, const char *) = NULL;
228317 if (!real_fopen) real_fopen = dlsym(RTLD_NEXT, "fopen");
@@ -236,41 +325,48 @@ def test_sysfs_hide_with_ld_preload(self):
236325 """ )
237326
238327 test_source = textwrap .dedent (r"""
239- #include <stdio.h>
240328 #include <errno.h>
329+ #include <fcntl.h>
330+ #include <stdio.h>
241331 #include <string.h>
332+ #include <unistd.h>
333+
334+ static int try_open(const char *path) {
335+ int fd = open(path, O_RDONLY);
336+ if (fd >= 0) {
337+ close(fd);
338+ }
339+ return fd;
340+ }
242341
243342 int main() {
244- FILE *f ;
343+ int fd ;
245344 int pass = 1;
246345
247- f = fopen ("/sys/devices/system/cpu/possible", "r ");
248- if (f != NULL ) {
346+ fd = try_open ("/sys/devices/system/cpu/possible");
347+ if (fd >= 0 ) {
249348 printf("FAIL: /sys/devices/system/cpu/possible should be blocked\n");
250- fclose(f);
251349 pass = 0;
252350 } else {
253351 printf("OK: /sys/devices/system/cpu/possible blocked (errno=%d: %s)\n",
254352 errno, strerror(errno));
255353 }
256354
257- f = fopen ("/sys/devices/system/cpu/present", "r ");
258- if (f != NULL ) {
355+ fd = try_open ("/sys/devices/system/cpu/present");
356+ if (fd >= 0 ) {
259357 printf("FAIL: /sys/devices/system/cpu/present should be blocked\n");
260- fclose(f);
261358 pass = 0;
262359 } else {
263360 printf("OK: /sys/devices/system/cpu/present blocked (errno=%d: %s)\n",
264361 errno, strerror(errno));
265362 }
266363
267364 // Verify other files still work
268- f = fopen ("/proc/cpuinfo", "r ");
269- if (f == NULL ) {
365+ fd = try_open ("/proc/cpuinfo");
366+ if (fd < 0 ) {
270367 printf("WARN: /proc/cpuinfo not accessible (may be OK in some envs)\n");
271368 } else {
272369 printf("OK: /proc/cpuinfo still accessible\n");
273- fclose(f);
274370 }
275371
276372 if (pass) {
@@ -334,8 +430,15 @@ def test_ort_import_with_hidden_sysfs(self):
334430 #define _GNU_SOURCE
335431 #include <dlfcn.h>
336432 #include <errno.h>
337- #include <string.h>
433+ #include <fcntl.h>
434+ #include <stdarg.h>
338435 #include <stdio.h>
436+ #include <string.h>
437+ #include <sys/types.h>
438+
439+ #ifndef O_TMPFILE
440+ #define O_TMPFILE 0
441+ #endif
339442
340443 static const char *CPU_POSSIBLE = "/sys/devices/system/cpu/possible";
341444 static const char *CPU_PRESENT = "/sys/devices/system/cpu/present";
@@ -344,6 +447,74 @@ def test_ort_import_with_hidden_sysfs(self):
344447 return (strcmp(path, CPU_POSSIBLE) == 0 || strcmp(path, CPU_PRESENT) == 0);
345448 }
346449
450+ static mode_t get_mode_if_needed(int flags, va_list args) {
451+ return ((flags & O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE)) ? va_arg(args, mode_t) : 0;
452+ }
453+
454+ int open(const char *path, int flags, ...) {
455+ static int (*real_open)(const char *, int, ...) = NULL;
456+ va_list args;
457+ mode_t mode = 0;
458+
459+ if (!real_open) real_open = dlsym(RTLD_NEXT, "open");
460+ if (is_blocked(path)) { errno = ENOENT; return -1; }
461+
462+ va_start(args, flags);
463+ mode = get_mode_if_needed(flags, args);
464+ va_end(args);
465+ return ((flags & O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE))
466+ ? real_open(path, flags, mode)
467+ : real_open(path, flags);
468+ }
469+
470+ int open64(const char *path, int flags, ...) {
471+ static int (*real_open64)(const char *, int, ...) = NULL;
472+ va_list args;
473+ mode_t mode = 0;
474+
475+ if (!real_open64) real_open64 = dlsym(RTLD_NEXT, "open64");
476+ if (is_blocked(path)) { errno = ENOENT; return -1; }
477+
478+ va_start(args, flags);
479+ mode = get_mode_if_needed(flags, args);
480+ va_end(args);
481+ return ((flags & O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE))
482+ ? real_open64(path, flags, mode)
483+ : real_open64(path, flags);
484+ }
485+
486+ int openat(int dirfd, const char *path, int flags, ...) {
487+ static int (*real_openat)(int, const char *, int, ...) = NULL;
488+ va_list args;
489+ mode_t mode = 0;
490+
491+ if (!real_openat) real_openat = dlsym(RTLD_NEXT, "openat");
492+ if (path && is_blocked(path)) { errno = ENOENT; return -1; }
493+
494+ va_start(args, flags);
495+ mode = get_mode_if_needed(flags, args);
496+ va_end(args);
497+ return ((flags & O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE))
498+ ? real_openat(dirfd, path, flags, mode)
499+ : real_openat(dirfd, path, flags);
500+ }
501+
502+ int openat64(int dirfd, const char *path, int flags, ...) {
503+ static int (*real_openat64)(int, const char *, int, ...) = NULL;
504+ va_list args;
505+ mode_t mode = 0;
506+
507+ if (!real_openat64) real_openat64 = dlsym(RTLD_NEXT, "openat64");
508+ if (path && is_blocked(path)) { errno = ENOENT; return -1; }
509+
510+ va_start(args, flags);
511+ mode = get_mode_if_needed(flags, args);
512+ va_end(args);
513+ return ((flags & O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE))
514+ ? real_openat64(dirfd, path, flags, mode)
515+ : real_openat64(dirfd, path, flags);
516+ }
517+
347518 FILE *fopen(const char *restrict path, const char *restrict mode) {
348519 static FILE *(*real_fopen)(const char *, const char *) = NULL;
349520 if (!real_fopen) real_fopen = dlsym(RTLD_NEXT, "fopen");
0 commit comments