From nobody Mon Dec 06 18:48:22 2021 X-Original-To: dev-commits-src-all@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 EF91A18B5867; Mon, 6 Dec 2021 18:48:23 +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 4J7C9y3NmMz4SZ4; Mon, 6 Dec 2021 18:48:22 +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 47D5311415; Mon, 6 Dec 2021 18:48:22 +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 1B6ImMrZ065164; Mon, 6 Dec 2021 18:48:22 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1B6ImM87065163; Mon, 6 Dec 2021 18:48:22 GMT (envelope-from git) Date: Mon, 6 Dec 2021 18:48:22 GMT Message-Id: <202112061848.1B6ImM87065163@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: c210ef134792 - main - rtld: load preloaded vdso List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@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/main X-Git-Reftype: branch X-Git-Commit: c210ef1347929cb3a0f46cdfdb3d4ad738e3af74 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1638816503; 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=H2fthnEFYqm43RmMaiDWFrUhztt2QuC45ALZZrrN8oA=; b=RxGkRZ02nx0IQ8rLq8boFenCXUKO8/Ni6ViUkTRiw5lOkvA5OGGgJtEvyivThhUYAqF1T5 UIOejUYBsDCBOFZFBAXet6P1dyGeiDpXr2j7IK61hNqgUf4vYyaAos78At1ZkDddrJ/uts VDPYR2zJ5UNrnHNCMQrzx+dnZvYMM3FkAK96G3DWFMede7n5s+XjB0csldM+7QzXJ6BLu/ Ex7V0dGwgoSwNfdDA2cZPKf4TwLEjR2GZET4bWuXKzddkeZIN7wnuuB0kjtcc5hJdtHt3/ TkF6dQU4aywoYMAk3vmf0Ej8kxb2qb+r2WxfHsJIRt1TMv7A3MuSm0pIg0L6tA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1638816503; a=rsa-sha256; cv=none; b=W4j9Yb8Vn1KLOO6+FjmvDb6Uc9nB/v/M6qgd3qNd8yidsHXtoiaBE+5cJr6N3HW/uX7m3j 75LdNPnctRSO1qXVqMLj1BHa5rkFg9snCZndaa5QcM4SuTDS2OSYz9w9AEhiqLgze1XfUG 35bXTSNb+uszsGKQ08oWBsf2aXycmZjj4B+RrdpY8XzdFsterdOVez0tOPPLXYmfnIpgnu p7ENcJ5wLKxooSrmpExg02aOWuSuWWZvrjL3FdrXRrg5r31dlcB2ixmAtHBbgDpfMCXShs Oe24sswMWbul3XUJlY5ODSAQSP0wbFd/W/2K/2raxbe2Cbo7BH/8x9cqlp7RoA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=c210ef1347929cb3a0f46cdfdb3d4ad738e3af74 commit c210ef1347929cb3a0f46cdfdb3d4ad738e3af74 Author: Konstantin Belousov AuthorDate: 2021-11-11 18:07:44 +0000 Commit: Konstantin Belousov CommitDate: 2021-12-06 18:46:49 +0000 rtld: load preloaded vdso Reviewed by: emaste Discussed with: jrtc27 Tested by: pho Sponsored by: The FreeBSD Foundation MFC after: 1 month Differential revision: https://reviews.freebsd.org/D32960 --- 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) {