From patchwork Wed Dec 22 13:27:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 49190 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2A4253858427 for ; Wed, 22 Dec 2021 13:37:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2A4253858427 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1640180229; bh=VSE3fYmuScnauaTB0aRGycvs0jlt4u6kgrhvDAIKPug=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=IPLP5z/Lhw3NI1p1R5GaR6Rjy52I5T8iVJuvW2IY1mvz/u0m1rjNZEYe1cI/tkli2 KH2XImjSIBe9zhtSdAKeI4HmrOeZOWGgum8BOWsiSi/iqn0tIrmPakOtdHz9R33xUa 9HTR1Hwub/OpcgGhVhZcLuHWpx53nmBb8/Ym2G+c= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qv1-xf30.google.com (mail-qv1-xf30.google.com [IPv6:2607:f8b0:4864:20::f30]) by sourceware.org (Postfix) with ESMTPS id 43C163858403 for ; Wed, 22 Dec 2021 13:27:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 43C163858403 Received: by mail-qv1-xf30.google.com with SMTP id q4so2276575qvh.9 for ; Wed, 22 Dec 2021 05:27:54 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=VSE3fYmuScnauaTB0aRGycvs0jlt4u6kgrhvDAIKPug=; b=xgH2uZB/o1QyUG6ZrQwR79tzcrcgFyDu4hFYmKJ/Vzezkr6yT34EHzLKPT4n+kVjFs gR9IEY+yNiO2KpHXdDs+k6REkAr5q33a4Rqz17akLCrvld5qS8t/nCEclnlotf2cd07i I+cme9BdEwb8V0KJ97Ya5rWE4k/ekETZR3twOnf3UiVbVe3ug4yPosVilW2pJOOlXudJ npLOVtXLrVRB4SmIOe0Whv/gUgSKZG5/HXTwSQ9XWP8lkZMKGqpKDAYxEYe347TPknc/ 3az7uSZU9Z/nHrIywhnT5eJqGtWl1z4lPDPCF4DriKANUubIqUouPwze/yF/wRMX8BFE JniA== X-Gm-Message-State: AOAM5337Gld4sYfeQyOZgnCmk0k0XBjLYEDsmpyCTvBYCZbdfgf0/Bti PWL4ifCCFw3PT15/gI9KKeW1dDUhEE720w== X-Google-Smtp-Source: ABdhPJxYXvuV1qUnZt1dyLf5Tw4mekTUp3f3+ulskTNetotslEtGtQdnLzpyxBZbiMdHQkRE3ga9Vg== X-Received: by 2002:a05:6214:2341:: with SMTP id hu1mr2367924qvb.78.1640179673606; Wed, 22 Dec 2021 05:27:53 -0800 (PST) Received: from birita.. ([2804:431:c7cb:3b1e:1ba8:c11f:6224:efe1]) by smtp.gmail.com with ESMTPSA id x13sm1942849qkp.102.2021.12.22.05.27.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Dec 2021 05:27:53 -0800 (PST) To: libc-alpha@sourceware.org, fweimer@redhat.com Subject: [PATCH v7 13/16] elf: Issue audit la_objopen for vDSO Date: Wed, 22 Dec 2021 10:27:09 -0300 Message-Id: <20211222132712.523295-14-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211222132712.523295-1-adhemerval.zanella@linaro.org> References: <20211222132712.523295-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Reply-To: Adhemerval Zanella Cc: John Mellor-Crummey Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" The vDSO is is listed in the link_map chain, but is never the subject of an la_objopen call. A new internal flag __RTLD_VDSO is added that acts as __RTLD_OPENEXEC to allocate the required 'struct auditstate' extra space for the 'struct link_map'. The return value from the callback is currently ignored, since there is no PLT call involved by glibc when using the vDSO, neither the vDSO are exported directly. Checked on x86_64-linux-gnu, i686-linux-gnu, and aarch64-linux-gnu. Reviewed-by: Florian Weimer --- elf/Makefile | 5 ++ elf/dl-object.c | 17 +++--- elf/rtld.c | 6 +++ elf/setup-vdso.h | 2 +- elf/tst-audit22.c | 124 +++++++++++++++++++++++++++++++++++++++++++ elf/tst-auditmod22.c | 51 ++++++++++++++++++ include/dlfcn.h | 2 + 7 files changed, 199 insertions(+), 8 deletions(-) create mode 100644 elf/tst-audit22.c create mode 100644 elf/tst-auditmod22.c diff --git a/elf/Makefile b/elf/Makefile index 97fa75ff5e..47305d3bde 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -234,6 +234,7 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \ tst-audit19b \ tst-audit20 \ tst-audit21 \ + tst-audit22 \ tst-rtld-run-static \ # reldep9 tests-internal += loadtest unload unload2 circleload1 \ @@ -387,6 +388,7 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \ tst-auditmod20 \ tst-auditmod21a \ tst-auditmod21b \ + tst-auditmod22 \ # Most modules build with _ISOMAC defined, but those filtered out # depend on internal headers. @@ -1607,6 +1609,9 @@ $(objpfx)tst-audit21.out: $(objpfx)tst-auditmod21a.so $(objpfx)tst-auditmod21a.so: $(objpfx)tst-auditmod21b.so tst-audit21-ENV = LD_AUDIT=$(objpfx)tst-auditmod21a.so +$(objpfx)tst-audit22.out: $(objpfx)tst-auditmod22.so +tst-audit22-ARGS = -- $(host-test-program-cmd) + # tst-sonamemove links against an older implementation of the library. LDFLAGS-tst-sonamemove-linkmod1.so = \ -Wl,--version-script=tst-sonamemove-linkmod1.map \ diff --git a/elf/dl-object.c b/elf/dl-object.c index f3d637ac27..cb998640ec 100644 --- a/elf/dl-object.c +++ b/elf/dl-object.c @@ -59,16 +59,19 @@ _dl_new_object (char *realname, const char *libname, int type, { #ifdef SHARED unsigned int naudit; - if (__glibc_unlikely ((mode & __RTLD_OPENEXEC) != 0)) + if (__glibc_unlikely ((mode & (__RTLD_OPENEXEC | __RTLD_VDSO)) != 0)) { - assert (type == lt_executable); - assert (nsid == LM_ID_BASE); + if (mode & __RTLD_OPENEXEC) + { + assert (type == lt_executable); + assert (nsid == LM_ID_BASE); - /* Ignore the specified libname for the main executable. It is - only known with an explicit loader invocation. */ - libname = ""; + /* Ignore the specified libname for the main executable. It is + only known with an explicit loader invocation. */ + libname = ""; + } - /* We create the map for the executable before we know whether + /* We create the map for the executable and vDSO before we know whether we have auditing libraries and if yes, how many. Assume the worst. */ naudit = DL_NNS; diff --git a/elf/rtld.c b/elf/rtld.c index 0a5e5be6e0..97d3d856dc 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -1914,6 +1914,12 @@ dl_main (const ElfW(Phdr) *phdr, assert (i == npreloads); } +#ifdef NEED_DL_SYSINFO_DSO + /* Now that the audit modules are opened, call la_objopen for the vDSO. */ + if (GLRO(dl_sysinfo_map) != NULL) + _dl_audit_objopen (GLRO(dl_sysinfo_map), LM_ID_BASE); +#endif + /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD specified some libraries to load, these are inserted before the actual dependencies in the executable's searchlist for symbol resolution. */ diff --git a/elf/setup-vdso.h b/elf/setup-vdso.h index 3f20578046..2b013d974a 100644 --- a/elf/setup-vdso.h +++ b/elf/setup-vdso.h @@ -30,7 +30,7 @@ setup_vdso (struct link_map *main_map __attribute__ ((unused)), We just want our data structures to describe it as if we had just mapped and relocated it normally. */ struct link_map *l = _dl_new_object ((char *) "", "", lt_library, NULL, - 0, LM_ID_BASE); + __RTLD_VDSO, LM_ID_BASE); if (__glibc_likely (l != NULL)) { l->l_phdr = ((const void *) GLRO(dl_sysinfo_dso) diff --git a/elf/tst-audit22.c b/elf/tst-audit22.c new file mode 100644 index 0000000000..18fd22a760 --- /dev/null +++ b/elf/tst-audit22.c @@ -0,0 +1,124 @@ +/* Check DTAUDIT and vDSO interaction. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static int restart; +#define CMDLINE_OPTIONS \ + { "restart", no_argument, &restart, 1 }, + +static uintptr_t vdso_addr; + +static int +handle_restart (void) +{ + fprintf (stderr, "vdso: %p\n", (void*) vdso_addr); + return 0; +} + +static uintptr_t +parse_address (const char *str) +{ + void *r; + TEST_COMPARE (sscanf (str, "%p\n", &r), 1); + return (uintptr_t) r; +} + +static inline bool +startswith (const char *str, const char *pre) +{ + size_t lenpre = strlen (pre); + size_t lenstr = strlen (str); + return lenstr >= lenpre && memcmp (pre, str, lenpre) == 0; +} + +static int +do_test (int argc, char *argv[]) +{ + vdso_addr = getauxval (AT_SYSINFO_EHDR); + if (vdso_addr == 0) + FAIL_UNSUPPORTED ("getauxval (AT_SYSINFO_EHDR) returned 0"); + + /* We must have either: + - One our fource parameters left if called initially: + + path to ld.so optional + + "--library-path" optional + + the library path optional + + the application name */ + if (restart) + return handle_restart (); + + char *spargv[9]; + int i = 0; + for (; i < argc - 1; i++) + spargv[i] = argv[i + 1]; + spargv[i++] = (char *) "--direct"; + spargv[i++] = (char *) "--restart"; + spargv[i] = NULL; + + setenv ("LD_AUDIT", "tst-auditmod22.so", 0); + struct support_capture_subprocess result + = support_capture_subprogram (spargv[0], spargv); + support_capture_subprocess_check (&result, "tst-audit22", 0, sc_allow_stderr); + + /* The respawned process should always print the vDSO address (otherwise it + will fails as unsupported). However, on some architectures the audit + module might see the vDSO with l_addr being 0, meaning a fixed mapping + (linux-gate.so). In this case we don't check its value against + AT_SYSINFO_EHDR one. */ + uintptr_t vdso_process = 0; + bool vdso_audit_found = false; + uintptr_t vdso_audit = 0; + + FILE *out = fmemopen (result.err.buffer, result.err.length, "r"); + TEST_VERIFY (out != NULL); + char *buffer = NULL; + size_t buffer_length = 0; + while (xgetline (&buffer, &buffer_length, out)) + { + if (startswith (buffer, "vdso: ")) + vdso_process = parse_address (buffer + strlen ("vdso: ")); + else if (startswith (buffer, "vdso found: ")) + { + vdso_audit = parse_address (buffer + strlen ("vdso found: ")); + vdso_audit_found = true; + } + } + + TEST_COMPARE (vdso_audit_found, true); + if (vdso_audit != 0) + TEST_COMPARE (vdso_process, vdso_audit); + + free (buffer); + xfclose (out); + + return 0; +} + +#define TEST_FUNCTION_ARGV do_test +#include diff --git a/elf/tst-auditmod22.c b/elf/tst-auditmod22.c new file mode 100644 index 0000000000..8e05ce8cbb --- /dev/null +++ b/elf/tst-auditmod22.c @@ -0,0 +1,51 @@ +/* Check DTAUDIT and vDSO interaction. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include + +static inline bool +startswith (const char *str, const char *pre) +{ + size_t lenpre = strlen (pre); + size_t lenstr = strlen (str); + return lenstr < lenpre ? false : memcmp (pre, str, lenpre) == 0; +} + +unsigned int +la_version (unsigned int version) +{ + return LAV_CURRENT; +} + +unsigned int +la_objopen (struct link_map *map, Lmid_t lmid, uintptr_t *cookie) +{ + /* The linux-gate.so is placed at a fixed address, thus l_addr being 0, + and it might be the value reported as the AT_SYSINFO_EHDR. */ + if (map->l_addr == 0 && startswith (map->l_name, "linux-gate.so")) + fprintf (stderr, "vdso found: %p\n", NULL); + else if (map->l_addr == getauxval (AT_SYSINFO_EHDR)) + fprintf (stderr, "vdso found: %p\n", (void*) map->l_addr); + + return 0; +} diff --git a/include/dlfcn.h b/include/dlfcn.h index a4c283728f..e73294b0af 100644 --- a/include/dlfcn.h +++ b/include/dlfcn.h @@ -12,6 +12,8 @@ #define __RTLD_AUDIT 0x08000000 #define __RTLD_SECURE 0x04000000 /* Apply additional security checks. */ #define __RTLD_NOIFUNC 0x02000000 /* Suppress calling ifunc functions. */ +#define __RTLD_VDSO 0x01000000 /* Tell _dl_new_object the object is + system-loaded. */ #define __LM_ID_CALLER -2