From patchwork Sun Sep 20 23:37:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Buka X-Patchwork-Id: 40464 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 CA20D398305E; Sun, 20 Sep 2020 23:38:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CA20D398305E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1600645083; bh=o7eL34P4InYOLuxfLIGLCLH8qUVP9ca8K5znV8u/INQ=; h=Date:In-Reply-To:References:Subject:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=JoSCqUSvWocF8D3SHUZ8RHGc5szMH0g7ZPh/uFb2Zezzb6Gu2rx/KI09AziO+6Wh1 kqhxdlJeIy206PFyhbXFgxSIxS2dhDh6MrX0P1cAoqwJ7UIRrYVXp0aQgw2U+zv+Ky Nb05Re5y9NYwgjDnb7P5lrMPzuG++CmDa/ZBUlsw= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qv1-xf4a.google.com (mail-qv1-xf4a.google.com [IPv6:2607:f8b0:4864:20::f4a]) by sourceware.org (Postfix) with ESMTPS id CB86E3857C61 for ; Sun, 20 Sep 2020 23:38:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org CB86E3857C61 Received: by mail-qv1-xf4a.google.com with SMTP id z12so7838263qvp.11 for ; Sun, 20 Sep 2020 16:38:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=o7eL34P4InYOLuxfLIGLCLH8qUVP9ca8K5znV8u/INQ=; b=QGNyC6x9Dlr7QjyQ28/ajeGXw296vlx7A/Kua0/tZoC4tQnokPqLYlw8lgsxik8+j6 x7CPGO2gHRu4Jzg2u8EvUFo9hrMpWfnXEsBVyQhPMq+EgeomeaMpNcaDn4mGrkQyrmzp 1ljpLu+s8s3u3ROSygPF03u/xLm5jWMRJ3ahRx4MI8/3iJHQS+QFyb3ZbEJyUHVZlz8F mwT2hVmmzmAJE+xMKDb/plvoDSShZieMmuIhV0h3LTV49lvHVN22sNMlLUGcv2mK/B44 /XnV3JaPPaNlAXc69SOSZTkldfoszv3ryDkR+mgNspi0ulPaBcVFuEwEsuwZAyWs0zdV uv1w== X-Gm-Message-State: AOAM531HsiqdVXobo+BcaZhNSwj/3ddmryXlWQh/LBlVRRPJNwyS5AdH BejZu7mMoc2AL+YpOWxdtSUATJmYKok/DU9TbLBWg7+UQMDLEjQJU00UbIfdmKRb6WdPQSn6g/Q cQMI9U3QtUcSwk7+6CrQB5GU4dFldAC9A66ys+ghsvzOGam7RO7Ppq/nocOHPwBFT9Y/lXI1xKj Mc X-Google-Smtp-Source: ABdhPJxyx59tcM+oNr720s2W2Y2lkyYaf/pYyN8yHcPdmSl/qLXZylUsnyNCzqdOhkRWf7kYvPrQXe0gt9R33cME X-Received: from vbl.svl.corp.google.com ([2620:15c:2ce:0:a28c:fdff:fee0:ac24]) (user=vitalybuka job=sendgmr) by 2002:ad4:42a5:: with SMTP id e5mr27311343qvr.58.1600645080273; Sun, 20 Sep 2020 16:38:00 -0700 (PDT) Date: Sun, 20 Sep 2020 16:37:56 -0700 In-Reply-To: Message-Id: <20200920233756.3886676-1-vitalybuka@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.28.0.681.g6f77f65b4e-goog Subject: [PATCH] stdlib: Fix data race in __run_exit_handlers To: libc-alpha@sourceware.org X-Spam-Status: No, score=-18.9 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Vitaly Buka via Libc-alpha From: Vitaly Buka Reply-To: Vitaly Buka Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Read f->func.cxa under the lock. There is a clear data race: thread 0: __run_exit_handlers unlock __exit_funcs_lock thread 1: __internal_atexit locks __exit_funcs_lock thread 0: f->flavor = ef_free; thread 1: sees ef_free and use it as new thread 1: new->func.cxa.fn = (void (*) (void *, int)) func; thread 1: new->func.cxa.arg = arg; thread 1: new->flavor = ef_cxa; thread 0: cxafct = f->func.cxa.fn; // it's wrong fn! thread 0: cxafct (f->func.cxa.arg, status); // it's wrong arg! thread 0: goto restart; thread 0: call the same exit_function again as it's ef_cxa It's incredibly rare so I can't create reasonable test. --- stdlib/exit.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/stdlib/exit.c b/stdlib/exit.c index 7bca1cdc14..9c79aa4a62 100644 --- a/stdlib/exit.c +++ b/stdlib/exit.c @@ -72,44 +72,52 @@ __run_exit_handlers (int status, struct exit_function_list **listp, struct exit_function *const f = &cur->fns[--cur->idx]; const uint64_t new_exitfn_called = __new_exitfn_called; - /* Unlock the list while we call a foreign function. */ - __libc_lock_unlock (__exit_funcs_lock); switch (f->flavor) { void (*atfct) (void); void (*onfct) (int status, void *arg); void (*cxafct) (void *arg, int status); + void *arg; case ef_free: case ef_us: break; case ef_on: onfct = f->func.on.fn; + arg = f->func.on.arg; #ifdef PTR_DEMANGLE PTR_DEMANGLE (onfct); #endif - onfct (status, f->func.on.arg); + /* Unlock the list while we call a foreign function. */ + __libc_lock_unlock (__exit_funcs_lock); + onfct (status, arg); + __libc_lock_lock (__exit_funcs_lock); break; case ef_at: atfct = f->func.at; #ifdef PTR_DEMANGLE PTR_DEMANGLE (atfct); #endif + /* Unlock the list while we call a foreign function. */ + __libc_lock_unlock (__exit_funcs_lock); atfct (); + __libc_lock_lock (__exit_funcs_lock); break; case ef_cxa: /* To avoid dlclose/exit race calling cxafct twice (BZ 22180), we must mark this function as ef_free. */ f->flavor = ef_free; cxafct = f->func.cxa.fn; + arg = f->func.cxa.arg; #ifdef PTR_DEMANGLE PTR_DEMANGLE (cxafct); #endif - cxafct (f->func.cxa.arg, status); + /* Unlock the list while we call a foreign function. */ + __libc_lock_unlock (__exit_funcs_lock); + cxafct (arg, status); + __libc_lock_lock (__exit_funcs_lock); break; } - /* Re-lock again before looking at global state. */ - __libc_lock_lock (__exit_funcs_lock); if (__glibc_unlikely (new_exitfn_called != __new_exitfn_called)) /* The last exit function, or another thread, has registered