Message ID | 20231010134300.53830-4-mark@klomp.org |
---|---|
State | Superseded |
Delegated to: | Aaron Merey |
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 B65F2385DC11 for <patchwork@sourceware.org>; Tue, 10 Oct 2023 13:43:45 +0000 (GMT) X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org Received: from gnu.wildebeest.org (gnu.wildebeest.org [45.83.234.184]) by sourceware.org (Postfix) with ESMTPS id B513A3858404 for <elfutils-devel@sourceware.org>; Tue, 10 Oct 2023 13:43:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B513A3858404 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from r6.localdomain (82-217-174-174.cable.dynamic.v4.ziggo.nl [82.217.174.174]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 5F187301FE87; Tue, 10 Oct 2023 15:43:34 +0200 (CEST) Received: by r6.localdomain (Postfix, from userid 1000) id 1B31E3403A4; Tue, 10 Oct 2023 15:43:34 +0200 (CEST) From: Mark Wielaard <mark@klomp.org> To: elfutils-devel@sourceware.org Cc: hsm2@rice.edu, Mark Wielaard <mark@klomp.org> Subject: [PATCH 04/16] libelf: Fix deadlock in elf_cntl Date: Tue, 10 Oct 2023 15:42:48 +0200 Message-ID: <20231010134300.53830-4-mark@klomp.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231010134300.53830-1-mark@klomp.org> References: <301fac87e83ebbbd677750579ae9a3429b461bdf.camel@klomp.org> <20231010134300.53830-1-mark@klomp.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3033.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, SPF_HELO_NONE, SPF_PASS, 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 |
[01/16] lib: Add new once_define and once macros to eu-config.h
|
|
Commit Message
Mark Wielaard
Oct. 10, 2023, 1:42 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: Mark Wielaard <mark@klomp.org> --- libelf/elf_cntl.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
Comments
Hi Heather, On Tue, 2023-10-10 at 15:42 +0200, Mark Wielaard 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: Mark Wielaard <mark@klomp.org> > --- > libelf/elf_cntl.c | 11 +++++++---- > 1 file changed, 7 insertions(+), 4 deletions(-) > > diff --git a/libelf/elf_cntl.c b/libelf/elf_cntl.c > index 04aa9132..64087c7d 100644 > --- a/libelf/elf_cntl.c > +++ b/libelf/elf_cntl.c > @@ -48,13 +48,16 @@ elf_cntl (Elf *elf, Elf_Cmd cmd) > return -1; > } > > - rwlock_wrlock (elf->lock); > + > > switch (cmd) > { > case ELF_C_FDREAD: > + rwlock_rdlock (elf->lock); > + int addr_isnull = elf->map_address == NULL; > + rwlock_unlock(elf->lock); > /* If not all of the file is in the memory read it now. */ > - if (elf->map_address == NULL && __libelf_readall (elf) == NULL) > + if (addr_isnull && __libelf_readall (elf) == NULL) > { > /* We were not able to read everything. */ > result = -1; Can't we just rely on if (__libelf_readall (elf) == NULL)? __libelf_readall already does locking and will return non-NULL if elf- >map_address is already set. So it looks like the extra check (and locking) to check addr_isnull is redundant and just make the code more complex. > @@ -64,7 +67,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 looks correct. All other accesses to elf->fildes seem to be done under the elf->lock too. > @@ -73,7 +78,5 @@ elf_cntl (Elf *elf, Elf_Cmd cmd) > break; > } > > - rwlock_unlock (elf->lock); > - > return result; > }
You are right. I changed the code to just rely on if (__libelf_readall (elf) == NULL) and this seems to work just fine. On Tue, Oct 10, 2023 at 10:23 AM Mark Wielaard <mark@klomp.org> wrote: > Hi Heather, > > On Tue, 2023-10-10 at 15:42 +0200, Mark Wielaard 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: Mark Wielaard <mark@klomp.org> > > --- > > libelf/elf_cntl.c | 11 +++++++---- > > 1 file changed, 7 insertions(+), 4 deletions(-) > > > > diff --git a/libelf/elf_cntl.c b/libelf/elf_cntl.c > > index 04aa9132..64087c7d 100644 > > --- a/libelf/elf_cntl.c > > +++ b/libelf/elf_cntl.c > > @@ -48,13 +48,16 @@ elf_cntl (Elf *elf, Elf_Cmd cmd) > > return -1; > > } > > > > - rwlock_wrlock (elf->lock); > > + > > > > switch (cmd) > > { > > case ELF_C_FDREAD: > > + rwlock_rdlock (elf->lock); > > + int addr_isnull = elf->map_address == NULL; > > + rwlock_unlock(elf->lock); > > /* If not all of the file is in the memory read it now. */ > > - if (elf->map_address == NULL && __libelf_readall (elf) == NULL) > > + if (addr_isnull && __libelf_readall (elf) == NULL) > > { > > /* We were not able to read everything. */ > > result = -1; > > Can't we just rely on if (__libelf_readall (elf) == NULL)? > > __libelf_readall already does locking and will return non-NULL if elf- > >map_address is already set. So it looks like the extra check (and > locking) to check addr_isnull is redundant and just make the code more > complex. > > > @@ -64,7 +67,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 looks correct. All other accesses to elf->fildes seem to be done > under the elf->lock too. > > > @@ -73,7 +78,5 @@ elf_cntl (Elf *elf, Elf_Cmd cmd) > > break; > > } > > > > - rwlock_unlock (elf->lock); > > - > > return result; > > } > >
diff --git a/libelf/elf_cntl.c b/libelf/elf_cntl.c index 04aa9132..64087c7d 100644 --- a/libelf/elf_cntl.c +++ b/libelf/elf_cntl.c @@ -48,13 +48,16 @@ elf_cntl (Elf *elf, Elf_Cmd cmd) return -1; } - rwlock_wrlock (elf->lock); + switch (cmd) { case ELF_C_FDREAD: + rwlock_rdlock (elf->lock); + int addr_isnull = elf->map_address == NULL; + rwlock_unlock(elf->lock); /* If not all of the file is in the memory read it now. */ - if (elf->map_address == NULL && __libelf_readall (elf) == NULL) + if (addr_isnull && __libelf_readall (elf) == NULL) { /* We were not able to read everything. */ result = -1; @@ -64,7 +67,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 +78,5 @@ elf_cntl (Elf *elf, Elf_Cmd cmd) break; } - rwlock_unlock (elf->lock); - return result; }