Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iZ5pm-0000kY-17 for pgsql-hackers@arkaria.postgresql.org; Mon, 25 Nov 2019 04:18:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iZ5pk-0004ac-Ql for pgsql-hackers@arkaria.postgresql.org; Mon, 25 Nov 2019 04:18:44 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iZ5pk-0004aU-Bx for pgsql-hackers@lists.postgresql.org; Mon, 25 Nov 2019 04:18:44 +0000 Received: from mail-pg1-x541.google.com ([2607:f8b0:4864:20::541]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iZ5pi-0001xr-3L for pgsql-hackers@lists.postgresql.org; Mon, 25 Nov 2019 04:18:43 +0000 Received: by mail-pg1-x541.google.com with SMTP id k13so6543067pgh.3 for ; Sun, 24 Nov 2019 20:18:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:references:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=+SHlNT4QhLe86jYxuiVcEV7ASLP3obe9AVGQP7rC48c=; b=RzltNFCw3A+qznz79Yzg9s2n2ujPQi30pM4NS5RoLMssJ8wWmusBEji+dEn3NEwYQa 76FFuNpG3XbtUDoyy/VmyHH8zjnPLLSfRHdqQQl0OzhB2J00exE9lwgMzH8xmYNWQFam Pd6mbWl48pTk21/G+sGJipcqSytb+ddQ86HylsMW/fM3UDrYWNjaQ3PsNhfqIMVi2/9N 8Xt0kqBBvBbicnW0/MQvLmy6rLWXIue0NwRfUapTLG+JHWR50e/H9DJiCnyAOZ6ZPks1 IpkLUXq9b4kU1rGseUadFiQyxOzALu+q+22keG6n9N9X4SlaBw1Mc4S0S9qjdFsgLJN3 xY4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=+SHlNT4QhLe86jYxuiVcEV7ASLP3obe9AVGQP7rC48c=; b=PLShpBoURHi0hquIE7outm2WX2JD58h/toyVQkGCn0NsivlCxRV02jm6w8DaEIcxtA M4OoT7QsJH8dtmmqSCMGcAD6U8sY14/+WuM3ZzzpMcdqWaP8CEpue4v9DibDQgSjp6KB bF79xHSpXwwUVuTgusgaDRfrxfOLUGTGUDT7mAcnyuBuFf8q5VyVUBRn+amkQtld7c7k 6SY7wxBoGVi2zDUL5F09hucme7X2E3lUK4okRye+/ZTTSKDraWIa3D2A10ss/FjEuhMx UjIUgRtIlJRn9Gqt7Vce9T+ihvtJgWe++P/+BCmwzfI2tqS5a2DH031Z+DOja8Nug+68 uvcw== X-Gm-Message-State: APjAAAVrXTUuU5Hhv6/xFrN7ZZsHBnvOFVB8AejCI/9N4529uazUKbnp SReGuxztETjm2a7Fp0pSrBL7UAPM X-Google-Smtp-Source: APXvYqxvRkcovm6+hz1tfnVF2Nyl+qsADlG4UgvxAuuedWrfwZ1o7TpA0Aet6JTo31mcr3Va5pwIwg== X-Received: by 2002:a62:ed02:: with SMTP id u2mr10443141pfh.240.1574655520580; Sun, 24 Nov 2019 20:18:40 -0800 (PST) Received: from [192.168.0.17] (24-113-193-150.wavecable.com. [24.113.193.150]) by smtp.gmail.com with ESMTPSA id s13sm6171854pfc.110.2019.11.24.20.18.39 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 24 Nov 2019 20:18:39 -0800 (PST) Subject: Re: Safeguards against incorrect fd flags for fsync() From: Mark Dilger To: Michael Paquier Cc: Postgres hackers References: <20191009062640.GB21379@paquier.xyz> <82db297a-74f7-ca23-545b-3c531d692e4d@gmail.com> <20191125022858.GE37821@paquier.xyz> <107de5d3-6972-7566-04d1-4776b677b462@gmail.com> Message-ID: <724f2e0a-9fd5-6e8a-5188-0fad8eb91bf7@gmail.com> Date: Sun, 24 Nov 2019 20:18:38 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.1 MIME-Version: 1.0 In-Reply-To: <107de5d3-6972-7566-04d1-4776b677b462@gmail.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 11/24/19 6:53 PM, Mark Dilger wrote: > > > On 11/24/19 6:28 PM, Michael Paquier wrote: >> On Thu, Nov 07, 2019 at 01:57:57PM -0800, Mark Dilger wrote: >>> The code and comments don't clearly indicate what you have said in the >>> email, that you are verifying directories are opened read-only and >>> files are >>> opened either read-write or write-only.  I'd recommend changing the >>> comments >>> a bit to make that clearer. >> >> Thanks for the suggestions, sounds fine to me. >> >>> I would also rearrange the code a little, as it is slightly clearer >>> to read: >>> >>>     if (x) >>>         /* directory stuff */ >>>     else >>>         /* file stuff */ >>> >>> than as you have it: >>> >>>     if (!x) >>>         /* file stuff */ >>>     else >>>         /* directory stuff */ >> >> The check order in the former patch is consistent with what's done at >> the top of fsync_fname_ext(), still I can see your point.  So let's do >> as you suggest. >> >>> I'm a little uncertain about ignoring fstat errors as you do, but >>> left that >>> part of the logic alone.  I understand that any fstat error will >>> likely be >>> immediately followed by another error when the fsync is attempted, but >>> relying on that seems vaguely similar to the security vulnerability of >>> checking permissions and then opening a file as two separate operations. >>> Not sure the analogy actually holds for fstat before fsync, though. >> >> The only possible error which could be expected here would be a ENOENT >> so we could filter after that, but fsync() would most likely complain >> about that so it sounds better to let it do its work with its own >> logging, which would be more helpful for the user, if of course we >> have fsync=on in postgresql.conf. >> >>> Attached is a revised version of the patch.  Perhaps you can check >>> what I've >>> done and tell me if I've broken it. >> >> Thanks for the review.  I was wondering why I did not do that as well >> for file_utils.c, just to find out that fsync_fname() is the only >> entry point in file_utils.c.  Anyway, the patch had a problem >> regarding fcntl() which is not available on Windows (see for example >> pg_set_noblock in noblock.c).  Performing the sanity check will allow >> to catch any problems for all platforms we support, so let's just skip >> it for Windows.  For this reason it is better as well to update errno >> to 0 after the fstat() call.  Who knows...  Attached is an updated >> version, with your changes included.  How does that look? > > That looks great, thank you, but I have not tested it yet.  I'll go do > that now.... Ok, it passes all regression tests, and I played around with intentionally breaking the code to open file descriptors in the wrong mode. The assertion appears to work as intended. I'd say this is ready for commit. -- Mark Dilger