Message ID | 20240717223409.1788719-3-amerey@redhat.com |
---|---|
State | Superseded |
Headers |
Return-Path: <elfutils-devel-bounces~patchwork=sourceware.org@sourceware.org> 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 EF57E3860C39 for <patchwork@sourceware.org>; Wed, 17 Jul 2024 22:34:58 +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 0A16B385E011 for <elfutils-devel@sourceware.org>; Wed, 17 Jul 2024 22:34:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0A16B385E011 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 0A16B385E011 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=1721255686; cv=none; b=ru8t3v6hgcc++oN/XWryQ3TsV/jLF280gOp6UIcx3KKYLpxNmaYm0ng30T2LOLXmrqHLp/jQsfXSrZT+F4dYQl/4lJ+fI/yPm0a+eSQTDVc1b4yRVj6JsJjr2txppaeZzkWbh6cGhmMJDOOWDtlaZak3Ymx/UUEzaV9LiNrG1Ck= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721255686; c=relaxed/simple; bh=HTfrTO/pr04gPTGLH2IZHM1G9DTr31tOio2B1ldGsLQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=XeDPNva5rnJ9L4nkaZTFiZMdk2AyhTonOmRnZHhuWwlFKB6YVrHmQwQPDxgm7csw5q4xfvjDpIDcZHZAG/lKOvgLzm+QXdCUnKfKgwlpNK0F3Okm//JoAku8+bnr/Gd/Q6tvNDgfn2ywDmSDxnmybsRV5VdQp+ANyytzA69x5sg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721255684; 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=Z3ZhBOPcCHHurMQRNqdfRJP5LXeZa0Wux5LZE0fH8t8=; b=eKKujDkdIUdmPeuewHsSy17QnQCP3U8wqMJvW7RWyzHqfsMz20cVTjxCx/e2xSuRtVVDV3 nPP5yEMhrK6pcA/JTk60JOLLwTU0Z5lVIBFboDC7BZaRi2CzxZSLnRvYAnMiuuDba9Vweq fbLKZMbMKWyAul1p4WJ+27iUqQlgHRY= Received: from mx-prod-mc-03.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-53-6UMGwGdJO1SPWbpsZlj-ig-1; Wed, 17 Jul 2024 18:34:43 -0400 X-MC-Unique: 6UMGwGdJO1SPWbpsZlj-ig-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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5C89D19560B4; Wed, 17 Jul 2024 22:34:42 +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 88E1B19560AA; Wed, 17 Jul 2024 22:34:40 +0000 (UTC) From: Aaron Merey <amerey@redhat.com> To: elfutils-devel@sourceware.org Cc: hsm2@rice.edu, johnmc@rice.edu, Aaron Merey <amerey@redhat.com>, Mark Wielaard <mark@klomp.org> Subject: [PATCH 2/9 v2] libelf: Fix deadlock in elf_cntl Date: Wed, 17 Jul 2024 18:34:01 -0400 Message-ID: <20240717223409.1788719-3-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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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 <elfutils-devel.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/elfutils-devel>, <mailto:elfutils-devel-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/elfutils-devel/> List-Post: <mailto:elfutils-devel@sourceware.org> List-Help: <mailto:elfutils-devel-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/elfutils-devel>, <mailto:elfutils-devel-request@sourceware.org?subject=subscribe> Errors-To: elfutils-devel-bounces~patchwork=sourceware.org@sourceware.org |
Series |
Fix thread-safety for elfutils
|
|
Commit Message
Aaron Merey
July 17, 2024, 10:34 p.m. UTC
From: Heather McIntyre <hsm2@rice.edu> * libelf/elf_cntl.c (elf_cntl): Move rwlock_wrlock, rwlock_unlock, inside case switch statements. Signed-off-by: Heather S. McIntyre <hsm2@rice.edu> Signed-off-by: Aaron Merey <amerey@redhat.com> Signed-off-by: Mark Wielaard <mark@klomp.org> --- v2 changes: Remove unnecessary locking and checking of elf->map_address libelf/elf_cntl.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
Comments
Hi, On Wed, 2024-07-17 at 18:34 -0400, Aaron Merey wrote: > From: Heather McIntyre <hsm2@rice.edu> > > * libelf/elf_cntl.c (elf_cntl): Move rwlock_wrlock, rwlock_unlock, > inside case switch statements. > > Signed-off-by: Heather S. McIntyre <hsm2@rice.edu> > Signed-off-by: Aaron Merey <amerey@redhat.com> > Signed-off-by: Mark Wielaard <mark@klomp.org> > > --- > v2 changes: > Remove unnecessary locking and checking of elf->map_address > > libelf/elf_cntl.c | 7 +++---- > 1 file changed, 3 insertions(+), 4 deletions(-) > > diff --git a/libelf/elf_cntl.c b/libelf/elf_cntl.c > index 04aa9132..3fbc7d97 100644 > --- a/libelf/elf_cntl.c > +++ b/libelf/elf_cntl.c > @@ -48,13 +48,12 @@ elf_cntl (Elf *elf, Elf_Cmd cmd) > return -1; > } > Out of view of this patch, but in the line before this code does: if (elf->fildes == -1) { __libelf_seterrno (ELF_E_INVALID_HANDLE); return -1; } Below we are careful to take a lock (in __libelf_readall or explicitly in the case of ELF_C_FDDONE, before reading or writing fildes. So we either have to add a lock around this check too. Or maybe better just remove this check. In the case of ELF_C_FDREAD __libelf_readall will return NULL and so already produce an error. In the case of ELF_C_FDDONE I would make the point that it doesn't matter, if the fildes was already -1, then it still is. No need for error in that case? > > - rwlock_wrlock (elf->lock); > > switch (cmd) > { > case ELF_C_FDREAD: > /* If not all of the file is in the memory read it now. */ > - if (elf->map_address == NULL && __libelf_readall (elf) == NULL) > + if (__libelf_readall (elf) == NULL) This looks good, __libelf_readall (elf) already does that check (after locking). > { > /* We were not able to read everything. */ > result = -1; > @@ -64,7 +63,9 @@ elf_cntl (Elf *elf, Elf_Cmd cmd) > > case ELF_C_FDDONE: > /* Mark the file descriptor as not usable. */ > + rwlock_wrlock (elf->lock); > elf->fildes = -1; > + rwlock_unlock (elf->lock); > break; > > default: This also looks correct. Cheers, Mark
diff --git a/libelf/elf_cntl.c b/libelf/elf_cntl.c index 04aa9132..3fbc7d97 100644 --- a/libelf/elf_cntl.c +++ b/libelf/elf_cntl.c @@ -48,13 +48,12 @@ elf_cntl (Elf *elf, Elf_Cmd cmd) return -1; } - rwlock_wrlock (elf->lock); switch (cmd) { case ELF_C_FDREAD: /* If not all of the file is in the memory read it now. */ - if (elf->map_address == NULL && __libelf_readall (elf) == NULL) + if (__libelf_readall (elf) == NULL) { /* We were not able to read everything. */ result = -1; @@ -64,7 +63,9 @@ elf_cntl (Elf *elf, Elf_Cmd cmd) case ELF_C_FDDONE: /* Mark the file descriptor as not usable. */ + rwlock_wrlock (elf->lock); elf->fildes = -1; + rwlock_unlock (elf->lock); break; default: @@ -73,7 +74,5 @@ elf_cntl (Elf *elf, Elf_Cmd cmd) break; } - rwlock_unlock (elf->lock); - return result; }