From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 0E5B73858438 for ; Tue, 18 Jan 2022 13:02:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0E5B73858438 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-640-_c0FCXBENYKafDa2DebsiQ-1; Tue, 18 Jan 2022 08:02:15 -0500 X-MC-Unique: _c0FCXBENYKafDa2DebsiQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8D11981A1D0; Tue, 18 Jan 2022 13:02:14 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.198]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 12F832B4DE; Tue, 18 Jan 2022 13:02:12 +0000 (UTC) From: Florian Weimer To: Adhemerval Zanella Cc: Adhemerval Zanella via Libc-alpha , jma14 , Carlos O'Donell , John Mellor-Crummey Subject: Re: [PATCH v10 2/4] elf: Fix initial-exec TLS access on audit modules (BZ #28096) References: <20220117213136.1327053-1-adhemerval.zanella@linaro.org> <20220117213136.1327053-3-adhemerval.zanella@linaro.org> <87r195jn1p.fsf@oldenburg.str.redhat.com> Date: Tue, 18 Jan 2022 14:02:11 +0100 In-Reply-To: (Adhemerval Zanella's message of "Tue, 18 Jan 2022 09:58:43 -0300") Message-ID: <875yqhjiy4.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, URIBL_BLACK 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-List-Received-Date: Tue, 18 Jan 2022 13:02:19 -0000 * Adhemerval Zanella: > On 18/01/2022 08:33, Florian Weimer wrote: >> * Adhemerval Zanella via Libc-alpha: >>=20 >>> diff --git a/elf/dl-tls.c b/elf/dl-tls.c >>> index 8ba70c9a9d..8ed91ff599 100644 >>> --- a/elf/dl-tls.c >>> +++ b/elf/dl-tls.c >>> @@ -520,7 +520,7 @@ _dl_resize_dtv (dtv_t *dtv, size_t max_modid) >>> =20 >>> =20 >>> void * >>> -_dl_allocate_tls_init (void *result) >>> +_dl_allocate_tls_init (void *result, bool init_tls) >>> { >>> if (result =3D=3D NULL) >>> /* The memory allocation failed. */ >>> @@ -593,7 +593,14 @@ _dl_allocate_tls_init (void *result) >>> =09 some platforms use in static programs requires it. */ >>> =09 dtv[map->l_tls_modid].pointer.val =3D dest; >>> =20 >>> -=09 /* Copy the initialization image and clear the BSS part. */ >>> +=09 /* Copy the initialization image and clear the BSS part. For >>> +=09 audit modules or depedencies with initial-exec TLS, we can not >>> +=09 set the initial TLS image on default loader initialization >>> +=09 because it would already be set by the audit setup. However, >>> +=09 subsequent thread creation would need to follow the default >>> +=09 behaviour. */ >>> +=09 if (__glibc_unlikely (map->l_auditing && !init_tls)) >>> +=09 continue; >>> =09 memset (__mempcpy (dest, map->l_tls_initimage, >>> =09=09=09 map->l_tls_initimage_size), '\0', >>> =09=09 map->l_tls_blocksize - map->l_tls_initimage_size); >>=20 >> I don't understand why the map->l_auditing condition is correct. >> Shouldn't it be >>=20 >> map->l_ns =3D=3D LM_ID_BASE && !init_tls Not sure if this is what I meant, the =E2=80=9Ccontinue=E2=80=9D inverts th= e condition. The initialization should happen for pthread_create or l_ns =3D=3D LM_ID_BA= SE. >> ? Everything else has been loaded via dlopen or dlmopen, so TLS >> initialization for the main thread has already happened. > > This is not suffice (I tried before in fact) because we need to skip > solely for the audit modules and dependencies at the startup. For > instance with tst-audit21 we have with some _dl_debug_printf to=20 > show what is happening > > 1533769: [rtld.c:966] load_audit_module > 1533769: [rtld.c:973] load_audit_module > 1533769: [rtld.c:2465] dl_main > 1533769: [../elf/dl-tls.c:603] map->l_name=3D map->l_ns=3D0 map->l= _auditing=3D0 init_tls=3D0 [INIT] > 1533769: [../elf/dl-tls.c:603] map->l_name=3D[...]/elf/tst-auditmo= d21a.so map->l_ns=3D1 map->l_auditing=3D1 init_tls=3D0 > 1533769: [../elf/dl-tls.c:603] map->l_name=3D[...]/libc.so.6 map->= l_ns=3D1 map->l_auditing=3D1 init_tls=3D0 > 1533769: [../elf/dl-tls.c:603] map->l_name=3D[...]/libc.so.6 map->= l_ns=3D0 map->l_auditing=3D0 init_tls=3D0 [INIT] > 1533769: [../elf/dl-tls.c:621] > 1533769: [rtld.c:2467] dl_main > [allocatestack.c:435] allocate_stack > 1533769: [../elf/dl-tls.c:633] _dl_allocate_tls > 1533769: [../elf/dl-tls.c:603] map->l_name=3D map->l_ns=3D0 map->l= _auditing=3D0 init_tls=3D1 [INIT] > 1533769: [../elf/dl-tls.c:603] [...]/elf/tst-auditmod21a.so map->l= _ns=3D1 map->l_auditing=3D1 init_tls=3D1 [INIT] > 1533769: [../elf/dl-tls.c:603] [...]/libc.so.6 map->l_ns=3D1 map->= l_auditing=3D1 init_tls=3D1 [INIT] > 1533769: [../elf/dl-tls.c:603] [...]/libc.so.6 map->l_ns=3D0 map->= l_auditing=3D0 init_tls=3D1 [INIT] > 1533769: [../elf/dl-tls.c:621] > 1533769: [../elf/dl-tls.c:642] _dl_allocate_tls > [allocatestack.c:446] allocate_stack > > > So we need to avoid initialization only for the libc.so loading by > audit libraries, the default one should still be initialized (marked > as [INIT]). Later when new threads are created we need to initialized > everything. That's what I tried to express with the LM_ID_BASE check. Thanks, Florian