From patchwork Sun Sep 20 09:57:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Buka X-Patchwork-Id: 40458 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 3688F398B17A; Sun, 20 Sep 2020 09:58:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3688F398B17A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1600595886; bh=qlyA/S8ZlzONtq5/6Dcz0LO4DMdv3w+APovYi/Anmlw=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=eM8qkDMx4xocdmQjI7caM+4VrXKpRG7Wsm7Dy+LMkbTytESqvso26UBb7My5JvVV1 33UiiumGNGSHQzm7EW/UfN2kCDb0NLfOavWxk1XY6pjGTaW/uTXAuOmA1YZegGkXC4 MGMJYOgDuqrpIHE9Er1i+5pKpM1+6PmRy14tZ4sQ= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by sourceware.org (Postfix) with ESMTPS id 737B8393C87E for ; Sun, 20 Sep 2020 09:58:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 737B8393C87E Received: by mail-yb1-xb4a.google.com with SMTP id f185so10340515ybf.17 for ; Sun, 20 Sep 2020 02:58:04 -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:message-id:mime-version:subject:from :to:cc; bh=qlyA/S8ZlzONtq5/6Dcz0LO4DMdv3w+APovYi/Anmlw=; b=bip+jJnO+hlXyjiztIwcFMbOf5TfbVIF1tgKHogTJMges6pAuFsWsKy1LeFvDq0EQu +YyzOsrMHSe+K+wwMkFV32PbADI+bw+cHkw8QlHCfh87IBmZI2tXIBuAZrWKHdstmsQt fO8UbE2hoPm90mnkOVCVn6yxS9v8H1/WDZt4DwuAsY2dw/a9Izg3/yJn+pllzr+q561a SLgp/XJ5Ys19FOpYSPSfqQqkDqW/Vyky1ooXemoM9nT0TlSwaEFyVMg0Kzuh4ENN1lDg Ebm9SRnHCXuweG8esm2fMiqrl80tBonidwGKI93RyvD/ietFU5VoZ5a0gEFxERfI+KXZ zWFA== X-Gm-Message-State: AOAM533rmPlWGgAe+d2+mU1eF/vHW5D3NCZdGWxQTma2Vj0hxyaArpUl GLC9OX6ZlcjKxCmD270k7t+eZ7ofJgX3D/WPuqo6ORBNQcWIUcyMcTNUhhXveoP/zU0ShdzV5I8 pozmI1kV0klZI66z44odV8+BpfnJpVLUqNxGndbo6qo4MDyrC8AJZLCnKd0tn4rTWqXpaWpOK4j fr X-Google-Smtp-Source: ABdhPJxa/lPnTItd6prXffCSkjFJ7kETelNnDv+Q1HnXdTJvQ1Kroe5Ld69JjalOHyptWz9XOqImLuW76nINN5Fs X-Received: from vbl.svl.corp.google.com ([2620:15c:2ce:0:a28c:fdff:fee0:ac24]) (user=vitalybuka job=sendgmr) by 2002:a25:8746:: with SMTP id e6mr50957050ybn.89.1600595883826; Sun, 20 Sep 2020 02:58:03 -0700 (PDT) Date: Sun, 20 Sep 2020 02:57:50 -0700 Message-Id: <20200920095750.396908-1-vitalybuka@google.com> Mime-Version: 1.0 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=-20.2 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 Cc: Vitaly Buka Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Read f->func.cxa under the lock. It's incredible rare so I failed to create a test. However we have binary which according to logs add __cxa_atexit with unique arg and then callback is called twice for the same arg. 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 --- stdlib/exit.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/stdlib/exit.c b/stdlib/exit.c index 7bca1cdc14..93cdec82f0 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; + /* Unlock the list while we call a foreign function. */ + __libc_lock_unlock (__exit_funcs_lock); #ifdef PTR_DEMANGLE PTR_DEMANGLE (onfct); #endif - onfct (status, f->func.on.arg); + onfct (status, arg); + __libc_lock_lock (__exit_funcs_lock); break; case ef_at: atfct = f->func.at; + /* Unlock the list while we call a foreign function. */ + __libc_lock_unlock (__exit_funcs_lock); #ifdef PTR_DEMANGLE PTR_DEMANGLE (atfct); #endif 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; + /* Unlock the list while we call a foreign function. */ + __libc_lock_unlock (__exit_funcs_lock); #ifdef PTR_DEMANGLE PTR_DEMANGLE (cxafct); #endif - cxafct (f->func.cxa.arg, status); + 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