Skip to content

Commit b40f07b

Browse files
FooBarriortstellar
authored andcommitted
[msan] Add stat-family interceptors on Linux
Add following interceptors on Linux: stat, lstat, fstat, fstatat. This fixes use-of-uninitialized value on platforms with GLIBC 2.33+. In particular: Arch Linux, Ubuntu hirsute/impish. The tests should have also been failing during the release on the mentioned platforms, but I cannot find any related discussion. Most likely, the regression was introduced by glibc commit [[ bminor/glibc@8ed005d | 8ed005daf0ab03e14250032 ]]: all stat-family functions are now exported as shared functions. Before, some of them (namely stat, lstat, fstat, fstatat) were provided as a part of libc_noshared.a and called their __xstat dopplegangers. This is still true for Debian Sid and earlier Ubuntu's. stat interceptors may be safely provided for them, no problem with that. Closes google/sanitizers#1452. See also https://jira.mariadb.org/browse/MDEV-24841 Reviewed By: eugenis Differential Revision: https://reviews.llvm.org/D111984 (cherry picked from commit 4e1a6c0)
1 parent 59289a8 commit b40f07b

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

compiler-rt/lib/msan/msan_interceptors.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -656,15 +656,16 @@ INTERCEPTOR(int, putenv, char *string) {
656656
return res;
657657
}
658658

659-
#if SANITIZER_FREEBSD || SANITIZER_NETBSD
659+
#define SANITIZER_STAT_LINUX (SANITIZER_LINUX && __GLIBC_PREREQ(2, 33))
660+
#if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_STAT_LINUX
660661
INTERCEPTOR(int, fstat, int fd, void *buf) {
661662
ENSURE_MSAN_INITED();
662663
int res = REAL(fstat)(fd, buf);
663664
if (!res)
664665
__msan_unpoison(buf, __sanitizer::struct_stat_sz);
665666
return res;
666667
}
667-
#define MSAN_MAYBE_INTERCEPT_FSTAT INTERCEPT_FUNCTION(fstat)
668+
# define MSAN_MAYBE_INTERCEPT_FSTAT MSAN_INTERCEPT_FUNC(fstat)
668669
#else
669670
#define MSAN_MAYBE_INTERCEPT_FSTAT
670671
#endif
@@ -677,7 +678,7 @@ INTERCEPTOR(int, __fxstat, int magic, int fd, void *buf) {
677678
__msan_unpoison(buf, __sanitizer::struct_stat_sz);
678679
return res;
679680
}
680-
#define MSAN_MAYBE_INTERCEPT___FXSTAT INTERCEPT_FUNCTION(__fxstat)
681+
# define MSAN_MAYBE_INTERCEPT___FXSTAT MSAN_INTERCEPT_FUNC(__fxstat)
681682
#else
682683
#define MSAN_MAYBE_INTERCEPT___FXSTAT
683684
#endif
@@ -690,28 +691,34 @@ INTERCEPTOR(int, __fxstat64, int magic, int fd, void *buf) {
690691
__msan_unpoison(buf, __sanitizer::struct_stat64_sz);
691692
return res;
692693
}
693-
#define MSAN_MAYBE_INTERCEPT___FXSTAT64 INTERCEPT_FUNCTION(__fxstat64)
694+
# define MSAN_MAYBE_INTERCEPT___FXSTAT64 MSAN_INTERCEPT_FUNC(__fxstat64)
694695
#else
695-
#define MSAN_MAYBE_INTERCEPT___FXSTAT64
696+
# define MSAN_MAYBE_INTERCEPT___FXSTAT64
696697
#endif
697698

698-
#if SANITIZER_FREEBSD || SANITIZER_NETBSD
699+
#if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_STAT_LINUX
699700
INTERCEPTOR(int, fstatat, int fd, char *pathname, void *buf, int flags) {
700701
ENSURE_MSAN_INITED();
701702
int res = REAL(fstatat)(fd, pathname, buf, flags);
702703
if (!res) __msan_unpoison(buf, __sanitizer::struct_stat_sz);
703704
return res;
704705
}
705-
# define MSAN_INTERCEPT_FSTATAT INTERCEPT_FUNCTION(fstatat)
706+
# define MSAN_MAYBE_INTERCEPT_FSTATAT MSAN_INTERCEPT_FUNC(fstatat)
706707
#else
708+
# define MSAN_MAYBE_INTERCEPT_FSTATAT
709+
#endif
710+
711+
#if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
707712
INTERCEPTOR(int, __fxstatat, int magic, int fd, char *pathname, void *buf,
708713
int flags) {
709714
ENSURE_MSAN_INITED();
710715
int res = REAL(__fxstatat)(magic, fd, pathname, buf, flags);
711716
if (!res) __msan_unpoison(buf, __sanitizer::struct_stat_sz);
712717
return res;
713718
}
714-
# define MSAN_INTERCEPT_FSTATAT INTERCEPT_FUNCTION(__fxstatat)
719+
# define MSAN_MAYBE_INTERCEPT___FXSTATAT MSAN_INTERCEPT_FUNC(__fxstatat)
720+
#else
721+
# define MSAN_MAYBE_INTERCEPT___FXSTATAT
715722
#endif
716723

717724
#if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
@@ -722,9 +729,9 @@ INTERCEPTOR(int, __fxstatat64, int magic, int fd, char *pathname, void *buf,
722729
if (!res) __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
723730
return res;
724731
}
725-
#define MSAN_MAYBE_INTERCEPT___FXSTATAT64 INTERCEPT_FUNCTION(__fxstatat64)
732+
# define MSAN_MAYBE_INTERCEPT___FXSTATAT64 MSAN_INTERCEPT_FUNC(__fxstatat64)
726733
#else
727-
#define MSAN_MAYBE_INTERCEPT___FXSTATAT64
734+
# define MSAN_MAYBE_INTERCEPT___FXSTATAT64
728735
#endif
729736

730737
INTERCEPTOR(int, pipe, int pipefd[2]) {
@@ -1686,7 +1693,8 @@ void InitializeInterceptors() {
16861693
MSAN_MAYBE_INTERCEPT_FCVT;
16871694
MSAN_MAYBE_INTERCEPT_FSTAT;
16881695
MSAN_MAYBE_INTERCEPT___FXSTAT;
1689-
MSAN_INTERCEPT_FSTATAT;
1696+
MSAN_MAYBE_INTERCEPT_FSTATAT;
1697+
MSAN_MAYBE_INTERCEPT___FXSTATAT;
16901698
MSAN_MAYBE_INTERCEPT___FXSTAT64;
16911699
MSAN_MAYBE_INTERCEPT___FXSTATAT64;
16921700
INTERCEPT_FUNCTION(pipe);

compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,13 @@
457457
#define SANITIZER_INTERCEPT_SEND_SENDTO SI_POSIX
458458
#define SANITIZER_INTERCEPT_EVENTFD_READ_WRITE SI_LINUX
459459

460-
#define SANITIZER_INTERCEPT_STAT \
461-
(SI_FREEBSD || SI_MAC || SI_ANDROID || SI_NETBSD || SI_SOLARIS)
460+
#define SI_STAT_LINUX (SI_LINUX && __GLIBC_PREREQ(2, 33))
461+
#define SANITIZER_INTERCEPT_STAT \
462+
(SI_FREEBSD || SI_MAC || SI_ANDROID || SI_NETBSD || SI_SOLARIS || \
463+
SI_STAT_LINUX)
462464
#define SANITIZER_INTERCEPT_LSTAT (SI_NETBSD || SI_FREEBSD)
463-
#define SANITIZER_INTERCEPT___XSTAT (!SANITIZER_INTERCEPT_STAT && SI_POSIX)
465+
#define SANITIZER_INTERCEPT___XSTAT \
466+
(!SANITIZER_INTERCEPT_STAT && SI_POSIX) || SI_STAT_LINUX
464467
#define SANITIZER_INTERCEPT___XSTAT64 SI_LINUX_NOT_ANDROID
465468
#define SANITIZER_INTERCEPT___LXSTAT SANITIZER_INTERCEPT___XSTAT
466469
#define SANITIZER_INTERCEPT___LXSTAT64 SI_LINUX_NOT_ANDROID

0 commit comments

Comments
 (0)