From nobody Sun Jan 02 19:08:14 2022 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 079CD191F4B5; Sun, 2 Jan 2022 19:08:16 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4JRpLQ523Xz4Xhh; Sun, 2 Jan 2022 19:08:14 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 58C841763D; Sun, 2 Jan 2022 19:08:14 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 202J8E2Z057790; Sun, 2 Jan 2022 19:08:14 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 202J8EO1057789; Sun, 2 Jan 2022 19:08:14 GMT (envelope-from git) Date: Sun, 2 Jan 2022 19:08:14 GMT Message-Id: <202201021908.202J8EO1057789@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: 1a28d9343c5b - stable/13 - rtld: load preloaded vdso List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 1a28d9343c5b70d5404133d599e7e01c3d021d0f Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1641150495; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ny5ZmvcQ4kEPdODp5Klgxp0G2Pu52ZpdaCaH74Pfxaw=; b=tH/6g4A8KM590iBo4DcM7QhjhZCuWhltuKoY8wauH1S6P8q7llEzWuAl8f4qDDi9Q9DlOW bJPp0A3Gc6CAWhKdbOx4hGHskXxVWCbr7E8pakWwuuiy8+2pLugf3CT62cPE8FYFfV74er Jdzu4msh7XulS3cVcafSB9GRq9ATYKlwTFnk7kKN0zO//eB0IEs2abRpivXbxcD/t9GQbC hcQfqsvNrPOe0rTjxF5KhzJt13yqMfzqF/2yFy2HMSXFsq7wxN0NrwnIo3+0t25cvF+Iyc mxR7Ap3FOHRGhAYvlC3Fxq4Yb51WhHJ4Cj9Ae1M8ry8EoR1EG8bXL4SYo0eG4w== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1641150495; a=rsa-sha256; cv=none; b=P42KznJrzuQO00DEInlKtOipdJUY9jPGeojjCf/n6dlaDpbki7V2vJi40D9+75x9b2oyhD hiEm4Rx/LNuNO2EOMh94jTiNwldfg28lUGqsdY9fiMk77+8G54MwW0tlgGqoRAkfFjumID f+r3AoKGYbz88psUGv8+BjuyAVfsWDEeIvlyKbI+IbxQqD7t5Gv9x6lQ8IXyxwf2xq3nXA +HL9R09Nt0RwVx+G+BcZnHfZ+3jX90kEmAbEXaEb0zfFoKibt0tLHl2WUKNxn8MNzuWwYx bpHAa2duXSsyBHAATP1I1cPgw3Xsjecs0kusHFLn6LTTKlQi8qbnwToPnIX+sw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=1a28d9343c5b70d5404133d599e7e01c3d021d0f commit 1a28d9343c5b70d5404133d599e7e01c3d021d0f Author: Konstantin Belousov AuthorDate: 2021-11-11 18:07:44 +0000 Commit: Konstantin Belousov CommitDate: 2022-01-02 16:43:01 +0000 rtld: load preloaded vdso (cherry picked from commit c210ef1347929cb3a0f46cdfdb3d4ad738e3af74) --- libexec/rtld-elf/rtld.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c index af29d3ead390..5819bf18e4b5 100644 --- a/libexec/rtld-elf/rtld.c +++ b/libexec/rtld-elf/rtld.c @@ -125,6 +125,7 @@ static void load_filtees(Obj_Entry *, int flags, RtldLockState *); static void unload_filtees(Obj_Entry *, RtldLockState *); static int load_needed_objects(Obj_Entry *, int); static int load_preload_objects(const char *, bool); +static int load_kpreload(const void *addr); static Obj_Entry *load_object(const char *, int fd, const Obj_Entry *, int); static void map_stacks_exec(RtldLockState *); static int obj_disable_relro(Obj_Entry *); @@ -828,6 +829,13 @@ _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp) if (!libmap_disable) libmap_disable = (bool)lm_init(libmap_override); + if (aux_info[AT_KPRELOAD] != NULL && + aux_info[AT_KPRELOAD]->a_un.a_ptr != NULL) { + dbg("loading kernel vdso"); + if (load_kpreload(aux_info[AT_KPRELOAD]->a_un.a_ptr) == -1) + rtld_die(); + } + dbg("loading LD_PRELOAD_FDS libraries"); if (load_preload_objects(ld_preload_fds, true) == -1) rtld_die(); @@ -2842,6 +2850,77 @@ errp: return (NULL); } +static int +load_kpreload(const void *addr) +{ + Obj_Entry *obj; + const Elf_Ehdr *ehdr; + const Elf_Phdr *phdr, *phlimit, *phdyn, *seg0, *segn; + static const char kname[] = "[vdso]"; + + ehdr = addr; + if (!check_elf_headers(ehdr, "kpreload")) + return (-1); + obj = obj_new(); + phdr = (const Elf_Phdr *)((const char *)addr + ehdr->e_phoff); + obj->phdr = phdr; + obj->phsize = ehdr->e_phnum * sizeof(*phdr); + phlimit = phdr + ehdr->e_phnum; + seg0 = segn = NULL; + + for (; phdr < phlimit; phdr++) { + switch (phdr->p_type) { + case PT_DYNAMIC: + phdyn = phdr; + break; + case PT_GNU_STACK: + /* Absense of PT_GNU_STACK implies stack_flags == 0. */ + obj->stack_flags = phdr->p_flags; + break; + case PT_LOAD: + if (seg0 == NULL || seg0->p_vaddr > phdr->p_vaddr) + seg0 = phdr; + if (segn == NULL || segn->p_vaddr + segn->p_memsz < + phdr->p_vaddr + phdr->p_memsz) + segn = phdr; + break; + } + } + + obj->mapbase = __DECONST(caddr_t, addr); + obj->mapsize = segn->p_vaddr + segn->p_memsz - (Elf_Addr)addr; + obj->vaddrbase = 0; + obj->relocbase = obj->mapbase; + + object_add_name(obj, kname); + obj->path = xstrdup(kname); + obj->dynamic = (const Elf_Dyn *)(obj->relocbase + phdyn->p_vaddr); + + if (!digest_dynamic(obj, 0)) { + obj_free(obj); + return (-1); + } + + /* + * We assume that kernel-preloaded object does not need + * relocation. It is currently written into read-only page, + * handling relocations would mean we need to allocate at + * least one additional page per AS. + */ + dbg("%s mapbase %p phdrs %p PT_LOAD phdr %p vaddr %p dynamic %p", + obj->path, obj->mapbase, obj->phdr, seg0, + obj->relocbase + seg0->p_vaddr, obj->dynamic); + + TAILQ_INSERT_TAIL(&obj_list, obj, next); + obj_count++; + obj_loads++; + linkmap_add(obj); /* for GDB & dlinfo() */ + max_stack_flags |= obj->stack_flags; + + LD_UTRACE(UTRACE_LOAD_OBJECT, obj, obj->mapbase, 0, 0, obj->path); + return (0); +} + Obj_Entry * obj_from_addr(const void *addr) {