rtld: set osrel when in the direct exec mode.

Rtld itself is a shared object which does not have vendor note, so
after the direct exec of ld-elf.so.1 process has p_osrel set to zero.
This affects the ABI of syscalls.

Set osrel to the __FreeBSD_version value at compile time right after
rtld identified direct exec mode.  Then, switch to the osrel read from
the binary note or zero if no note, right before starting calling
ifunc resolvers, which is the first byte of the user code.

Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
This commit is contained in:
Konstantin Belousov 2020-06-12 22:10:03 +00:00
parent 3e6e3de0aa
commit 510b525fa5
2 changed files with 32 additions and 3 deletions

View File

@ -46,7 +46,7 @@ _libc_string_objects= bcmp bcopy bzero memset memchr memcmp memcpy memmove \
# Also use all the syscall .o files from libc_nossp_pic:
_libc_other_objects= sigsetjmp lstat stat fstat fstatat fstatfs syscall \
cerror geteuid getegid sigfastblock munmap mprotect \
sysarch __sysctl issetugid __getcwd utrace \
sysarch __sysctl issetugid __getcwd utrace getpid \
thr_self thr_kill pread mmap lseek _exit _fstat _fstatat _fstatfs \
getdirentries _getdirentries _close _fcntl _open _openat _read \
_sigprocmask _write readlink __realpathat _setjmp setjmp setjmperr

View File

@ -384,11 +384,12 @@ _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp)
const char *argv0, *binpath;
caddr_t imgentry;
char buf[MAXPATHLEN];
int argc, fd, i, phnum, rtld_argc;
int argc, fd, i, mib[4], old_osrel, osrel, phnum, rtld_argc;
size_t sz;
#ifdef __powerpc__
int old_auxv_format = 1;
#endif
bool dir_enable, explicit_fd, search_in_path;
bool dir_enable, direct_exec, explicit_fd, search_in_path;
/*
* On entry, the dynamic linker itself has not been relocated yet.
@ -451,6 +452,7 @@ _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp)
ld_fast_sigblock = true;
trust = !issetugid();
direct_exec = false;
md_abi_variant_hook(aux_info);
@ -466,6 +468,21 @@ _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp)
argv0);
rtld_die();
}
direct_exec = true;
/*
* Set osrel for us, it is later reset to the binary'
* value before first instruction of code from the binary
* is executed.
*/
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_OSREL;
mib[3] = getpid();
osrel = __FreeBSD_version;
sz = sizeof(old_osrel);
(void)sysctl(mib, 4, &old_osrel, &sz, &osrel, sizeof(osrel));
dbg("opening main program in direct exec mode");
if (argc >= 2) {
rtld_argc = parse_args(argv, argc, &search_in_path, &fd, &argv0);
@ -804,6 +821,18 @@ _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp)
*/
pre_init();
if (direct_exec) {
/* Set osrel for direct-execed binary */
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_OSREL;
mib[3] = getpid();
osrel = obj_main->osrel;
sz = sizeof(old_osrel);
dbg("setting osrel to %d", osrel);
(void)sysctl(mib, 4, &old_osrel, &sz, &osrel, sizeof(osrel));
}
wlock_acquire(rtld_bind_lock, &lockstate);
dbg("resolving ifuncs");