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 1iZ4VU-0006Le-Oq for pgsql-hackers@arkaria.postgresql.org; Mon, 25 Nov 2019 02:53:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iZ4VT-0001ky-9N for pgsql-hackers@arkaria.postgresql.org; Mon, 25 Nov 2019 02:53:43 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iZ4VT-0001kq-0a for pgsql-hackers@lists.postgresql.org; Mon, 25 Nov 2019 02:53:43 +0000 Received: from mail-pl1-x644.google.com ([2607:f8b0:4864:20::644]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iZ4VQ-00051W-8Q for pgsql-hackers@lists.postgresql.org; Mon, 25 Nov 2019 02:53:42 +0000 Received: by mail-pl1-x644.google.com with SMTP id f9so5847815plr.7 for ; Sun, 24 Nov 2019 18:53:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=ftpu87/8+iqxPBraF+Cbfv8VKdWNTaJOiTcDEKgbCbw=; b=BSTawrHawnGrHDcnqsjcKInKQ719D6DPw/gI+8Kc12BX7MCR7jhVk++j+t0VGTc4S+ HhBZKcotS9Nf7MF3HKA8btl6igtFxcHpZ1OzQ6Xj7xDByXKTkuWRxr9u3mVTnplgSvTl pCkRUsdyO81U2I28JWV/5JNV3y555H8l2HX2K4nJc7Fz2sIOVAu0W6ZZH/knwdwrtsAl 6EFgfJ5m6jotG6tVInKMHz8w3p/1i2cyZwkPM4GKxjrbhJO8Wb6XKKYi4ugJmquavD34 kdILnhtsBNek9V9o+uf6BM3rNdA1bTH1gtFb3UTr8jAY/cn2RPKCmZhDrfSJjRAvKRgS QQug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=ftpu87/8+iqxPBraF+Cbfv8VKdWNTaJOiTcDEKgbCbw=; b=eBdBvZ0JCNVfZOV8ayPjItG3RcHUsrydBFi5b2blxeuXH8uXgDjRx8C8gXfxalD9dp UzU/etCY/8lxfLtXfngsTHqtqW7znKosCBZ53sFRflMUcLf4q3oaxeEcCfLOc7G751yH C8q1IQyuvaJNIMWghsSpFrDnLbVOywTNRGvq7jzd+Ll7MC58VtPF9gponrDX/q5DysaP x83V29A9EjpR2dRjPx+CEd5WQYX90bJgm4A8r0aFyOo0cxtHiLLPdAemZgavuOvD+95d aaEbjHGLlNpvAzQlGe+vmGtaj7khIqo+wpmT1hThP4PrAA4BTsWsKOvd2zJWW1WUvFXk yW2A== X-Gm-Message-State: APjAAAVN8CjgNYq0w4K6JE2sw4+Zl7qXAA7WMjdY7l2VoedhZhd3Ezl8 UYdnGOOX1tMbw/f+Yt6G1eMW7wQ8 X-Google-Smtp-Source: APXvYqwygUAoWo5V9ZJsZGjjlgrm3lSdXz28XcXu6q6Oe93FdpSkhmx4B7zAUhZVD7Ox8+4Bk9IYyQ== X-Received: by 2002:a17:90a:8083:: with SMTP id c3mr36090083pjn.92.1574650417733; Sun, 24 Nov 2019 18:53:37 -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 w4sm5991946pjt.21.2019.11.24.18.53.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 24 Nov 2019 18:53:37 -0800 (PST) Subject: Re: Safeguards against incorrect fd flags for fsync() To: Michael Paquier Cc: Postgres hackers References: <20191009062640.GB21379@paquier.xyz> <82db297a-74f7-ca23-545b-3c531d692e4d@gmail.com> <20191125022858.GE37821@paquier.xyz> From: Mark Dilger Message-ID: <107de5d3-6972-7566-04d1-4776b677b462@gmail.com> Date: Sun, 24 Nov 2019 18:53:35 -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: <20191125022858.GE37821@paquier.xyz> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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.... -- Mark Dilger