From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x32e.google.com (mail-ot1-x32e.google.com [IPv6:2607:f8b0:4864:20::32e]) by sourceware.org (Postfix) with ESMTPS id 7758B3858C01 for ; Wed, 23 Aug 2023 13:46:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7758B3858C01 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-ot1-x32e.google.com with SMTP id 46e09a7af769-6bca3588edbso3705216a34.0 for ; Wed, 23 Aug 2023 06:46:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1692798369; x=1693403169; h=content-transfer-encoding:in-reply-to:organization:from:references :to:content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=7z+A9FN4m2zIrHfvb+m9OV1+vkf97NPKugWXy39iF5Y=; b=A6xMP69ceL2awkcIFn7Ljk/TmlU0Kfko8s4PP7xgxQczfP9mQoeWedh4GOl3ec3AvB mTC5GP0rny292Rg6sygK0esSt/TjtGzAXA6VUJ16WUQg0yXgG4xzaLwhEQczO7JP+dX+ tD+DG2t6XCH3Q+8AjhGlqVZBes8ldi82R+2OQ4Te3L/06OBns7l3P4VB3vsiWKy2O+1T q67ULqSC+053bvKbkLQWW2amvFgtHwVYzboNqc8yYtlqchzngbmaifNBJ/RdKvPmgHls UYFxJS/kLwRpMH93pu6IPa4w/QyTyOStdLZBBBunP/XynHj8HBpnv7uGZY8QgnWo9uBC JSGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692798369; x=1693403169; h=content-transfer-encoding:in-reply-to:organization:from:references :to:content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7z+A9FN4m2zIrHfvb+m9OV1+vkf97NPKugWXy39iF5Y=; b=iqislJPDNST9HGgP/O3GJzhzm0rna3k3T3y5ynSk4tWme+uZXOoJfQGE+1sqLlo1L+ tDGkUU3igIi5l00TGCUK/mAjvmRbI5xNQdCOqw6Y0Dp9zXDEnAhFkAE71dzvrQi6jago Lo5SRx1rXLyXPIuS15Gp7UDTBcmctSf6yUfEKBq9aDQmBUpcrwHEJBRrKSuL0f1rTWpf UavS5CWlwEfH8zwHzjHk8BNKFCviCpWC1XJyhFYZ780IivzJtdf4jhuoZWJOEs8Vq8WR zMWelHQvdEJeM/QWDWfst52PDqJ8RCHqLpeKYkis5wkEct76AUxwQ9nMdwURIXriMZeC 2Kvg== X-Gm-Message-State: AOJu0YzqaKQrvGZTT0DtV4yADM5hTR0XGAUNuhHLQp3FacnWkbEHnOwt MwNuBzuU/OnSs0+tcei+V6ZYcj9wEARiv8uZHqbxHQ== X-Google-Smtp-Source: AGHT+IHUVMW0AIi1dr1K7wu8EP8gYexx6ARBkFhGLn4g5L7YZTC2a5WdWjJ7opYwFBxb/14/JUU0JA== X-Received: by 2002:a05:6830:8e:b0:6bc:9799:a00c with SMTP id a14-20020a056830008e00b006bc9799a00cmr12119337oto.26.1692798368722; Wed, 23 Aug 2023 06:46:08 -0700 (PDT) Received: from ?IPV6:2804:1b3:a7c2:c275:d8f:2562:4517:f8f5? ([2804:1b3:a7c2:c275:d8f:2562:4517:f8f5]) by smtp.gmail.com with ESMTPSA id u17-20020a056830119100b006b94fb2762asm5662650otq.23.2023.08.23.06.46.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 23 Aug 2023 06:46:08 -0700 (PDT) Message-ID: <4755cc77-54d0-00e6-62d5-2a90d0c35af2@linaro.org> Date: Wed, 23 Aug 2023 10:46:05 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.14.0 Subject: Re: [PATCH] sysdeps: Clear O_CREAT|O_ACCMODE when trying again on sem_open Content-Language: en-US To: Sergio Durigan Junior , libc-alpha@sourceware.org References: <20230823042129.3955131-1-sergiodj@sergiodj.net> From: Adhemerval Zanella Netto Organization: Linaro In-Reply-To: <20230823042129.3955131-1-sergiodj@sergiodj.net> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-14.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,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 List-Id: On 23/08/23 01:21, Sergio Durigan Junior via Libc-alpha wrote: > When invoking sem_open with O_CREAT as one of its flags, we'll end up > in the second part of sem_open's "if ((oflag & O_CREAT) == 0 || (oflag > & O_EXCL) == 0)", which means that we don't expect the semaphore file > to exist. > > In that part, open_flags is initialized as "O_RDWR | O_CREAT | O_EXCL > | O_CLOEXEC" and there's an attempt to open(2) the file, which will > likely fail because it won't exist. After that first (expected) > failure, some cleanup is done and we go back to the label "try_again", > which lives in the first part of the aforementioned "if". > > The problem is that, in that part of the code, we expect the semaphore > file to exist, and as such O_CREAT (this time the flag we pass to > open(2)) needs to be cleaned from open_flags, otherwise we'll see > another failure (this time unexpected) when trying to open the file, > which will lead the call to sem_open to fail as well. > > This can cause very strange bugs, especially with OpenMPI, which makes > extensive use of semaphores. > > The fix here is to actually make sure that the O_CREAT|O_ACCMODE flags > are clear after we enter "try_again". > > See also: https://bugs.launchpad.net/ubuntu/+source/h5py/+bug/2031912 This need needs a bug report and, if possible, a regression check (I give you that it might be tricky due it is a racy condition). > --- > sysdeps/pthread/sem_open.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/sysdeps/pthread/sem_open.c b/sysdeps/pthread/sem_open.c > index e5db929d20..ba91f89d57 100644 > --- a/sysdeps/pthread/sem_open.c > +++ b/sysdeps/pthread/sem_open.c > @@ -66,8 +66,8 @@ __sem_open (const char *name, int oflag, ...) > if ((oflag & O_CREAT) == 0 || (oflag & O_EXCL) == 0) > { > open_flags = O_RDWR | O_NOFOLLOW | O_CLOEXEC; > - open_flags |= (oflag & ~(O_CREAT|O_ACCMODE)); > try_again: > + open_flags |= (oflag & ~(O_CREAT|O_ACCMODE)); > fd = __open (dirname.name, open_flags); > > if (fd == -1) I still think this is not fully correct, because on second try it would not use O_NOFOLLOW. Also, O_RDWR will be always set since now it always clear the O_ACCMODE. So I think it does not actually need to keep the open_flags over the iteration, we can simplify with something like that: diff --git a/sysdeps/pthread/sem_open.c b/sysdeps/pthread/sem_open.c index e5db929d20..7c189afbcf 100644 --- a/sysdeps/pthread/sem_open.c +++ b/sysdeps/pthread/sem_open.c @@ -32,11 +32,12 @@ # define __unlink unlink #endif +#define SEM_OPEN_FLAGS (O_RDWR | O_NOFOLLOW | O_CLOEXEC) + sem_t * __sem_open (const char *name, int oflag, ...) { int fd; - int open_flags; sem_t *result; /* Check that shared futexes are supported. */ @@ -65,10 +66,8 @@ __sem_open (const char *name, int oflag, ...) /* If the semaphore object has to exist simply open it. */ if ((oflag & O_CREAT) == 0 || (oflag & O_EXCL) == 0) { - open_flags = O_RDWR | O_NOFOLLOW | O_CLOEXEC; - open_flags |= (oflag & ~(O_CREAT|O_ACCMODE)); try_again: - fd = __open (dirname.name, open_flags); + fd = __open (dirname.name, (oflag & O_EXCL) | SEM_OPEN_FLAGS); if (fd == -1) { @@ -135,8 +134,7 @@ __sem_open (const char *name, int oflag, ...) } /* Open the file. Make sure we do not overwrite anything. */ - open_flags = O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC; - fd = __open (tmpfname, open_flags, mode); + fd = __open (tmpfname, O_CREAT | O_EXCL | SEM_OPEN_FLAGS, mode); if (fd == -1) { if (errno == EEXIST)