From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS31976 209.132.180.0/23 X-Spam-Status: No, score=-4.0 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id A669220248 for ; Thu, 18 Apr 2019 12:59:53 +0000 (UTC) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:references:date:in-reply-to :message-id:mime-version:content-type; q=dns; s=default; b=KtX3Z IxGxBZx3lP0mcSt/I57x3bcKYim2bWD2UdS9UzNlIAoQo1xN44zXYbf8u30iyEyy 45bcf/1OmVLV6Hs0rDUMk6d1xuXLWA7gffEZaBZeJqleQ6hUeygurbDPbJpx6RFH wfCGhGL2szEFphOzW8zYaygxL1vETV6rVcJOt0= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:references:date:in-reply-to :message-id:mime-version:content-type; s=default; bh=tskgMsaJm2+ lDil9trNVmrtPjbg=; b=DSVWabKPXm8YVnTOhFYWOKCpZ2pvlLr4eT3HdaloBo5 2cCrA0RCNvbxre8htTDyq0mFV+vK6G6Wlj2jvJIfSU0kuRUvR6EQvGZRzazAKpk4 48pm/SIBRyFF3U1zxdrV5Nn1aatfb3H2YTdWdRdmhTpxrhg1z+EvLwTjZbfUGNuU = Received: (qmail 11196 invoked by alias); 18 Apr 2019 12:59:50 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Received: (qmail 11188 invoked by uid 89); 18 Apr 2019 12:59:50 -0000 Authentication-Results: sourceware.org; auth=none X-HELO: mx1.suse.de From: Andreas Schwab To: Florian Weimer Cc: libc-alpha@sourceware.org Subject: Re: [PATCH] Avoid concurrency problem in ldconfig (bug 23973) References: <87bm13y11o.fsf@oldenburg2.str.redhat.com> X-Yow: Hmmm.. a CRIPPLED ACCOUNTANT with a FALAFEL sandwich is HIT by a TROLLEY-CAR.. Date: Thu, 18 Apr 2019 14:59:46 +0200 In-Reply-To: <87bm13y11o.fsf@oldenburg2.str.redhat.com> (Florian Weimer's message of "Thu, 18 Apr 2019 14:01:55 +0200") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain On Apr 18 2019, Florian Weimer wrote: > * Andreas Schwab: > >> Use a unique name for the temporary file when updating the ld.so cache, so >> that two concurrent runs of ldconfig don't write to the same file. >> >> * elf/cache.c (save_cache): Use unique temporary name. >> (save_aux_cache): Likewise. > > The downside of this change is that if ldconfig is interrupted, the > temporary file never goes away. > > Ideally, we would use O_TMPFILE if supported by the (file) system, but > that can get quite involved. That shortens the window, but won't close it, since we need a name to pass to rename in any case. >> diff --git a/elf/cache.c b/elf/cache.c >> index b8e9e6ccc3..ec7d94b0bc 100644 >> --- a/elf/cache.c >> +++ b/elf/cache.c >> @@ -427,12 +427,12 @@ save_cache (const char *cache_name) >> /* Write out the cache. */ >> >> /* Write cache first to a temporary file and rename it later. */ >> - char *temp_name = xmalloc (strlen (cache_name) + 2); >> - sprintf (temp_name, "%s~", cache_name); >> + char *temp_name; >> + if (asprintf (&temp_name, "%s.XXXXXX", cache_name) < 0) >> + error (EXIT_FAILURE, errno, _("Can't allocate temporary name for cache file")); >> >> /* Create file. */ >> - int fd = open (temp_name, O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW, >> - S_IRUSR|S_IWUSR); >> + int fd = mkostemp (temp_name, O_NOFOLLOW); > > I think you can use mkstemp because O_NOFOLLOW is implied by its use of > O_EXCL. Yes, O_NOFOLLOW is useless anyway, since mk[o]stemp is about creating a new file. Andreas. -- Andreas Schwab, SUSE Labs, schwab@suse.de GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE 1748 E4D4 88E3 0EEA B9D7 "And now for something completely different."