glibc/sysdeps/unix/sysv/linux
John David Anglin 6f4527a7dd Fix data race in setting function descriptors during lazy binding on hppa.
This addresses an issue that is present mainly on SMP machines running
threaded code.  In a typical indirect call or PLT import stub, the
target address is loaded first.  Then the global pointer is loaded into
the PIC register in the delay slot of a branch to the target address.
During lazy binding, the target address is a trampoline which transfers
to _dl_runtime_resolve().

_dl_runtime_resolve() uses the relocation offset stored in the global
pointer and the linkage map stored in the trampoline to find the
relocation.  Then, the function descriptor is updated.

In a multi-threaded application, it is possible for the global pointer
to be updated between the load of the target address and the global
pointer.  When this happens, the relocation offset has been replaced
by the new global pointer.  The function pointer has probably been
updated as well but there is no way to find the address of the function
descriptor and to transfer to the target.  So, _dl_runtime_resolve()
typically crashes.

HP-UX addressed this problem by adding an extra pc-relative branch to
the trampoline.  The descriptor is initially setup to point to the
branch.  The branch then transfers to the trampoline.  This allowed
the trampoline code to figure out which descriptor was being used
without any modification to user code.  I didn't use this approach
as it is more complex and changes function pointer canonicalization.

The order of loading the target address and global pointer in
indirect calls was not consistent with the order used in import stubs.
In particular, $$dyncall and some inline versions of it loaded the
global pointer first.  This was inconsistent with the global pointer
being updated first in dl-machine.h.  Assuming the accesses are
ordered, we want elf_machine_fixup_plt() to store the global pointer
first and calls to load it last.  Then, the global pointer will be
correct when the target function is entered.

However, just to make things more fun, HP added support for
out-of-order execution of accesses in PA 2.0.  The accesses used by
calls are weakly ordered. So, it's possibly under some circumstances
that a function might be entered with the wrong global pointer.
However, HP uses weakly ordered accesses in 64-bit HP-UX, so I assume
that loading the global pointer in the delay slot of the branch must
work consistently.

The basic fix for the race is a combination of modifying user code to
preserve the address of the function descriptor in register %r22 and
setting the least-significant bit in the relocation offset.  The
latter was suggested by Carlos as a way to distinguish relocation
offsets from global pointer values.  Conventionally, %r22 is used
as the address of the function descriptor in calls to $$dyncall.
So, it wasn't hard to preserve the address in %r22.

I have updated gcc trunk and gcc-9 branch to not clobber %r22 in
$$dyncall and inline indirect calls.  I have also modified the import
stubs in binutils trunk and the 2.33 branch to preserve %r22.  This
required making the stubs one instruction longer but we save one
relocation.  I also modified binutils to align the .plt section on
a 8-byte boundary.  This allows descriptors to be updated atomically
with a floting-point store.

With these changes, _dl_runtime_resolve() can fallback to an alternate
mechanism to find the relocation offset when it has been clobbered.
There's just one additional instruction in the fast path. I tested
the fallback function, _dl_fix_reloc_arg(), by changing the branch to
always use the fallback.  Old code still runs as it did before.

Fixes bug 23296.

Reviewed-by: Carlos O'Donell <carlos@redhat.com>
(cherry picked from commit 1a044511a3)
2020-05-04 22:00:52 +02:00
..
aarch64 nptl: Add POSIX-proposed pthread_mutex_clocklock 2019-07-12 13:36:25 +00:00
alpha alpha: force old OSF1 syscalls for getegid, geteuid and getppid [BZ #24986] 2019-09-14 20:08:02 +02:00
arm nptl: Add POSIX-proposed pthread_mutex_clocklock 2019-07-12 13:36:25 +00:00
bits Don't use a custom wrapper macro around __has_include (bug 25189). 2019-11-22 13:09:01 +01:00
csky nptl: Add POSIX-proposed pthread_mutex_clocklock 2019-07-12 13:36:25 +00:00
generic
hppa Fix data race in setting function descriptors during lazy binding on hppa. 2020-05-04 22:00:52 +02:00
i386 nptl: Add POSIX-proposed pthread_mutex_clocklock 2019-07-12 13:36:25 +00:00
ia64 nptl: Add POSIX-proposed pthread_mutex_clocklock 2019-07-12 13:36:25 +00:00
include
m68k nptl: Add POSIX-proposed pthread_mutex_clocklock 2019-07-12 13:36:25 +00:00
microblaze microblaze: Avoid clobbering register parameters in syscall 2020-02-16 15:16:10 +01:00
mips mips: Fix argument passing for inlined syscalls on Linux [BZ #25523] 2020-02-16 11:44:25 +01:00
net
netash
netatalk
netax25
neteconet
netinet
netipx
netiucv
netpacket
netrom
netrose
nfs
nios2 nptl: Add POSIX-proposed pthread_mutex_clocklock 2019-07-12 13:36:25 +00:00
powerpc Restore r31 setting in powerpc32 swapcontext. 2019-07-30 14:05:11 +00:00
riscv riscv: Avoid clobbering register parameters in syscall 2020-02-16 15:16:50 +01:00
s390 login: Add nonstring attributes to struct utmp, struct utmpx [BZ #24899] 2020-01-17 13:56:00 +01:00
scsi
sh nptl: Add POSIX-proposed pthread_mutex_clocklock 2019-07-12 13:36:25 +00:00
sparc sparc: Move sigreturn stub to assembly 2020-03-24 08:48:45 -03:00
sys Linux: Deprecate <sys/sysctl.h> and sysctl 2019-06-12 14:32:08 +02:00
wordsize-64
x86 nptl: Rename lll_timedlock to lll_clocklock and add clockid parameter 2019-07-12 13:36:25 +00:00
x86_64 rtld: Check __libc_enable_secure before honoring LD_PREFER_MAP_32BIT_EXEC (CVE-2019-19126) [BZ #25204] 2019-11-22 13:09:23 +01:00
Implies
Makefile Add nocancel version of pread64() 2020-01-17 13:56:43 +01:00
Versions Add nocancel version of pread64() 2020-01-17 13:56:43 +01:00
_G_config.h
_exit.c
a.out.h
accept.c
accept4.c
access.c
adjtime.c
aio_misc.h
aio_sigqueue.c
alphasort64.c
arch-fork.h
bind.c
check_native.c
check_pf.c
clock.c
clock_getcpuclockid.c
clock_getres.c
clock_gettime.c
clock_nanosleep.c
clock_settime.c
close.c
close_nocancel.c
cmsg_nxthdr.c
configure
configure.ac
connect.c
copy_file_range.c io: Remove copy_file_range emulation [BZ #24744] 2019-06-28 09:39:21 +02:00
creat.c
creat64.c
createthread.c
default-sched.h
device-nrs.h
dl-brk.c
dl-execstack.c
dl-fxstatat64.c
dl-getcwd.c
dl-librecon.h
dl-openat64.c
dl-opendir.c
dl-origin.c
dl-osinfo.h
dl-sbrk.c
dl-sysdep.c
dl-sysdep.h
dl-vdso.c
dl-vdso.h dl-vdso: Add LINUX_4 HASH CODE to support nds32 vdso mechanism 2019-07-08 15:57:01 -03:00
dl-writev.h
epoll_pwait.c
epoll_wait.c
errqueue.h
eventfd_read.c
eventfd_write.c
exit-thread.h
faccessat.c
fallocate.c
fallocate64.c
fatal-prepare.h
fchmodat.c
fcntl.c
fcntl64.c
fcntl_nocancel.c
fd_to_filename.h
fdatasync.c
fexecve.c
filter-nr-syscalls.awk
fips-private.h
fpathconf.c
fstatfs64.c
fstatvfs.c
fstatvfs64.c
fsync.c
ftime.c
ftruncate.c
ftruncate64.c
futex-internal.h nptl: Remove futex_supports_exact_relative_timeouts 2019-07-12 13:36:25 +00:00
futimens.c
futimes.c
futimesat.c
fxstat.c
fxstat64.c
fxstatat.c
fxstatat64.c
gai_sigqueue.c
gen-syscall-h.awk
gentempfd.c
getclktck.c
getcpu.c
getcwd.c
getdents.c Linux: Add getdents64 system call 2019-06-07 09:27:01 +02:00
getdents64.c Linux: Adjust gedents64 buffer size to int range [BZ #24740] 2019-06-27 15:08:40 +02:00
getdirentries.c
getdirentries64.c
getdtsz.c
getentropy.c
gethostid.c
getipv4sourcefilter.c
getloadavg.c
getlogin.c
getlogin_r.c
getpagesize.c
getpeername.c
getpriority.c
getpt.c
getrandom.c
getrlimit.c
getrlimit64.c
getsockname.c
getsockopt.c
getsourcefilter.c
getsourcefilter.h
getsysstats.c
gettimeofday.c
glob-lstat-compat.c
glob.c
glob64-lstat-compat.c
glob64.c
globfree.c
globfree64.c
grantpt.c
if_index.c
ifaddrs.c
ifreq.c
internal-signals.h
internal_statvfs.c
internal_statvfs.h
internal_statvfs64.c
ipc_ops.h
ipc_priv.h sysvipc: Consolidate semtimedop s390 2019-05-20 12:25:31 -03:00
kernel-features.h io: Remove copy_file_range emulation [BZ #24744] 2019-06-28 09:39:21 +02:00
kernel-posix-cpu-timers.h
kernel-posix-timers.h
kernel_sigaction.h
kernel_stat.h
kernel_termios.h
ldd-rewrite.sed
lddlibc4.c
ldsodefs.h
libc_fatal.c
linux_fsinfo.h
listen.c
local-setxid.h
lowlevellock-futex.h nptl: Add clockid parameter to futex timed wait calls 2019-07-12 13:36:23 +00:00
lseek.c
lseek64.c
lutimes.c
lxstat.c
lxstat64.c
malloc-sysdep.h
mlock2.c
mmap.c
mmap64.c posix: Fix large mmap64 offset for mips64n32 (BZ#24699) 2019-07-10 16:52:50 -03:00
mmap_internal.h
mq_close.c
mq_getattr.c
mq_notify.c
mq_open.c
mq_receive.c
mq_send.c
mq_timedreceive.c
mq_timedsend.c
mq_unlink.c
msgctl.c sysvipc: Fix compat msgctl (BZ#24570) 2019-05-20 12:25:28 -03:00
msgget.c
msgrcv.c
msgsnd.c
msync.c
nanosleep.c
nanosleep_nocancel.c
netlink_assert_response.c
netlinkaccess.h
nice.c
not-cancel.h Add nocancel version of pread64() 2020-01-17 13:56:43 +01:00
not-errno.h
nscd_setup_thread.c
ntp_gettime.c
ntp_gettimex.c
olddirent.h
oldglob.c
open.c
open64.c
open64_nocancel.c
open_by_handle_at.c
open_nocancel.c
openat.c
openat64.c
openat64_nocancel.c
openat_nocancel.c
opensock.c
pathconf.c
pathconf.h
paths.h
pause.c
pause_nocancel.c
personality.c
pkey_get.c
pkey_mprotect.c
pkey_set.c
poll.c
posix_fadvise.c
posix_fadvise64.c
posix_fallocate.c
posix_fallocate64.c
posix_madvise.c
ppoll.c
pread.c
pread64.c
pread64_nocancel.c Add nocancel version of pread64() 2020-01-17 13:56:43 +01:00
preadv.c
preadv2.c
preadv64.c
preadv64v2.c
prlimit.c
prof-freq.c
profil.c
pselect.c
pt-raise.c
pthread-pids.h
pthread_getaffinity.c
pthread_getcpuclockid.c
pthread_getname.c
pthread_kill.c
pthread_setaffinity.c
pthread_setname.c
pthread_sigmask.c
pthread_sigqueue.c
ptrace.c
ptsname.c
pwrite.c
pwrite64.c
pwritev.c
pwritev2.c
pwritev64.c
pwritev64v2.c
raise.c
read.c
read_nocancel.c
readahead.c
readdir.c
readdir64.c
readdir64_r.c
readdir_r.c
readonly-area.c
readv.c
reboot.c
recv.c
recvfrom.c
recvmmsg.c
recvmsg.c
remove.c
rename.c
renameat.c
renameat2.c
sa_len.c
safe-fatal.h
scandir64.c
sched_getaffinity.c
sched_getcpu.c
sched_setaffinity.c
segfault.c
select.c
semctl.c
semget.c
semop.c
semtimedop.c sysvipc: Consolidate semtimedop s390 2019-05-20 12:25:31 -03:00
send.c
sendmmsg.c
sendmsg.c
sendto.c
setegid.c
seteuid.c
setgid.c
setgroups.c
sethostid.c
setipv4sourcefilter.c
setregid.c
setresgid.c
setresuid.c
setreuid.c
setrlimit.c
setrlimit64.c
setsockopt.c
setsourcefilter.c
setuid.c
shlib-versions
shm-directory.c
shmat.c
shmctl.c
shmdt.c
shmget.c
shutdown.c
sigaction.c
siglist.h
signal.c
signalfd.c
sigpending.c
sigprocmask.c
sigqueue.c
sigreturn.c
sigset-cvt-mask.h
sigsetops.h
sigstack.c
sigsuspend.c
sigtimedwait.c
sigwait.c
sigwaitinfo.c
single-thread.h Add single-thread.h header 2019-05-14 08:41:15 -03:00
sizes.h
socket.c
socketcall.h
socketpair.c
spawni.c
speed.c
splice.c
statfs64.c
statvfs.c
statvfs64.c
statx.c
statx_cp.c
statx_cp.h
sync_file_range.c
syscall-names.list Linux: Update syscall-names.list to Linux 5.2 2019-07-19 08:53:04 +02:00
syscalls.list Linux: Add the tgkill function 2019-05-14 22:55:51 +02:00
sysconf.c linux: Assume clock_getres CLOCK_{PROCESS,THREAD}_CPUTIME_ID 2019-03-22 16:52:29 -03:00
sysctl.c Linux: Deprecate <sys/sysctl.h> and sysctl 2019-06-12 14:32:08 +02:00
sysctl.mk
sysdep-cancel.h Add single-thread.h header 2019-05-14 08:41:15 -03:00
sysdep-vdso.h
sysdep.h
syslog.c
tcdrain.c
tcflow.c
tcflush.c
tcgetattr.c
tcgetpgrp.c
tcsendbrk.c
tcsetattr.c
tcsetpgrp.c
tee.c
termio.h
test-errno-linux.c misc/test-errno-linux: Handle EINVAL from quotactl 2019-12-05 17:30:13 +01:00
time.c
timer_create.c
timer_delete.c
timer_getoverr.c
timer_gettime.c
timer_routines.c
timer_settime.c
times.c
timespec_get.c
truncate.c
truncate64.c
tst-affinity-pid.c
tst-affinity-static.c
tst-affinity.c
tst-align-clone.c
tst-clone.c
tst-clone2.c
tst-clone3.c
tst-fallocate-common.c
tst-fallocate.c
tst-fallocate64.c
tst-fanotify.c
tst-getdents64.c Linux: Use mmap instead of malloc in dirent/tst-getdents64 2019-06-28 14:05:02 +02:00
tst-getpid1.c
tst-gettid-kill.c
tst-gettid.c
tst-memfd_create.c
tst-mlock2.c
tst-mman-consts.py
tst-ofdlocks-compat.c
tst-ofdlocks.c
tst-personality.c
tst-pkey.c
tst-quota.c
tst-readdir64-compat.c
tst-rlimit-infinity.c
tst-setgetname.c
tst-signal-numbers.py
tst-skeleton-affinity.c
tst-skeleton-thread-affinity.c
tst-socket-consts.py Linux: Use in-tree copy of SO_ constants for !__USE_MISC [BZ #24532] 2019-07-24 10:59:34 +02:00
tst-sync_file_range.c
tst-syscall-list.sh
tst-sysconf-iov_max-uapi.c
tst-sysconf-iov_max.c
tst-tgkill.c Linux: Add the tgkill function 2019-05-14 22:55:51 +02:00
tst-thread-affinity-pthread.c
tst-thread-affinity-pthread2.c
tst-thread-affinity-sched.c
tst-ttyname.c
ttyname.c
ttyname.h
ttyname_r.c
ualarm.c
umount.c
umount2.S
unlockpt.c
updwtmp.c
ustat.c
utimensat.c
utimes.c
utmp_file.c
versionsort64.c
vfork.c
vmsplice.c
wait.c
wait3.c
waitid.c
waitpid.c
waitpid_nocancel.c
write.c
write_nocancel.c
writev.c
xmknod.c
xmknodat.c
xstat.c
xstat64.c
xstatconv.c
xstatconv.h