From patchwork Wed Jul 17 22:34:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Merey X-Patchwork-Id: 94096 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 662B9385F027 for ; Wed, 17 Jul 2024 22:35:01 +0000 (GMT) X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 67A8E385E458 for ; Wed, 17 Jul 2024 22:34:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 67A8E385E458 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 67A8E385E458 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721255683; cv=none; b=FRvQJR5uixIziEqEEkb7Vn3wOA/CGhB8krgnGPaoBn2xtQ5WQCrEnIugIqChzORyBodUUNZZjakB2DrE9XHgoHySA7K3Q02xslvTeAVy9J8q1oPXI4i6diWnycnzFu46AHuFPIf09OKw/QrOpbyZu1/dTCciWoaOUSGIq8BL+9c= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721255683; c=relaxed/simple; bh=pIOg+YNcKt5Pe1EF7MB2LKH5jdGXEPeUG3+4Pq/n6mI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=G3lnpOMPDnptejHDYDNghcDjpfqBny3ue4JqXqqxpJgDsaTZ3oxg5ebneEsycrfRbJ/9CIqEhQhrRqbl49zWB6/0BuRmRQsjCYIUudCl/AnLH+cPgGL8YiU9I4u0fgYIu3MFwrzIktXOKOwYSukqmfvnmb49WbSPSjtQ1IX9rMk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721255681; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1e2FY0wp5/rBLHWOoEPFZ2StGZ72rQYJdOGidEk5lrE=; b=YY2QJGaskFeJFzTLDug30J9t/aEpcPWH8EX1mWxpL9tfxeFrliBFrbMAo/5Q+pXtE9Rar5 zJseFAQ+qPU9MG84+epvxn25mexviC1aZ/Ryx51xcRXSRYosv/7RO6DbOLTIeMqvD9h560 Nkoc47gc1i1AV5LQS+5/SRHti0w0ylc= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-471-9XHINjfuNDCCej28mf6ZwQ-1; Wed, 17 Jul 2024 18:34:39 -0400 X-MC-Unique: 9XHINjfuNDCCej28mf6ZwQ-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8FAE819560B0; Wed, 17 Jul 2024 22:34:38 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.22.16.62]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 1FFEB19560AA; Wed, 17 Jul 2024 22:34:36 +0000 (UTC) From: Aaron Merey To: elfutils-devel@sourceware.org Cc: hsm2@rice.edu, johnmc@rice.edu, Aaron Merey , Mark Wielaard Subject: [PATCH 1/9 v2] libelf: Fix deadlock in __libelf_readall Date: Wed, 17 Jul 2024 18:34:00 -0400 Message-ID: <20240717223409.1788719-2-amerey@redhat.com> In-Reply-To: <20240717223409.1788719-1-amerey@redhat.com> References: <20240717223409.1788719-1-amerey@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.2 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_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: elfutils-devel-bounces~patchwork=sourceware.org@sourceware.org From: Heather McIntyre Apply locking during __libelf_readall. Signed-off-by: Heather S. McIntyre Signed-off-by: Aaron Merey Signed-off-by: Mark Wielaard --- v2 changes: Add locking for all early returns in __libelf_readall. libelf_{acquire,release}_all have been renamed to libelf_{acquire,release}_all_children. These functions also no longer acquire/release the parent's lock. This is done in order to simplify lock management in __libelf_readall. libelf/common.h | 16 ++++++++-------- libelf/elf_readall.c | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/libelf/common.h b/libelf/common.h index 9b2a856d..b7226ee8 100644 --- a/libelf/common.h +++ b/libelf/common.h @@ -92,10 +92,8 @@ allocate_elf (int fildes, void *map_address, int64_t offset, size_t maxsize, /* Acquire lock for the descriptor and all children. */ static void __attribute__ ((unused)) -libelf_acquire_all (Elf *elf) +libelf_acquire_all_children (Elf *elf) { - rwlock_wrlock (elf->lock); - if (elf->kind == ELF_K_AR) { Elf *child = elf->state.ar.children; @@ -103,7 +101,9 @@ libelf_acquire_all (Elf *elf) while (child != NULL) { if (child->ref_count != 0) - libelf_acquire_all (child); + libelf_acquire_all_children (child); + + rwlock_wrlock(child->lock); child = child->next; } } @@ -112,7 +112,7 @@ libelf_acquire_all (Elf *elf) /* Release own lock and those of the children. */ static void __attribute__ ((unused)) -libelf_release_all (Elf *elf) +libelf_release_all_children (Elf *elf) { if (elf->kind == ELF_K_AR) { @@ -121,12 +121,12 @@ libelf_release_all (Elf *elf) while (child != NULL) { if (child->ref_count != 0) - libelf_release_all (child); + libelf_release_all_children (child); + + rwlock_unlock (child->lock); child = child->next; } } - - rwlock_unlock (elf->lock); } diff --git a/libelf/elf_readall.c b/libelf/elf_readall.c index d0f9a28c..4ef8fe97 100644 --- a/libelf/elf_readall.c +++ b/libelf/elf_readall.c @@ -84,7 +84,7 @@ __libelf_readall (Elf *elf) /* If this is an archive and we have derived descriptors get the locks for all of them. */ - libelf_acquire_all (elf); + libelf_acquire_all_children (elf); if (elf->maximum_size == ~((size_t) 0)) { @@ -141,7 +141,7 @@ __libelf_readall (Elf *elf) __libelf_seterrno (ELF_E_NOMEM); /* Free the locks on the children. */ - libelf_release_all (elf); + libelf_release_all_children (elf); } rwlock_unlock (elf->lock);