Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uOwC0-00DyGW-Kq for pgsql-hackers@arkaria.postgresql.org; Tue, 10 Jun 2025 10:26:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uOwBy-0034qC-Fg for pgsql-hackers@arkaria.postgresql.org; Tue, 10 Jun 2025 10:26:55 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uOwBy-0034oz-5N for pgsql-hackers@lists.postgresql.org; Tue, 10 Jun 2025 10:26:54 +0000 Received: from mail-wr1-x434.google.com ([2a00:1450:4864:20::434]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uOwBw-001DkX-07 for pgsql-hackers@lists.postgresql.org; Tue, 10 Jun 2025 10:26:54 +0000 Received: by mail-wr1-x434.google.com with SMTP id ffacd0b85a97d-3a548a73ff2so1956978f8f.0 for ; Tue, 10 Jun 2025 03:26:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749551210; x=1750156010; darn=lists.postgresql.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :subject:cc:to:from:date:sender:message-id:from:to:cc:subject:date :message-id:reply-to; bh=oZLLw61zyXtc3k0+P3a15EjNZCKHbR5C5Q797wTQYfA=; b=YBdj9Ott8WPv5W0iQupdIdaVhoeb3uLH/Pt93WETq2pFyq5ZyKwc29kSZixv9f/t3M E+o89Cs0j4dQjnUHKlWzGvlsvc3TKIYcqpRIcJmkdrs4j5/znQq7bR1aby6M5RwA12t3 sqZWZkR2GJ8VF+fkYtEcs72X6CVzYwQm1oK1/25LzHRSJNcSxZMCWtRblT4l6GC6dYDK P7m7qs7iNin+SpxHhuVBWUm98HPR8Kq7OUsISDUwtW7259wk4YFU0EgaRY3hQNelc9vU D0UGLPfu4PTTmYg9Fz1dnDxxY4+esle60n07MWoK46Ffarm1XL2Ftgv7vHs30no151oH 5xgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749551210; x=1750156010; h=user-agent:in-reply-to:content-disposition:mime-version:references :subject:cc:to:from:date:sender:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=oZLLw61zyXtc3k0+P3a15EjNZCKHbR5C5Q797wTQYfA=; b=tsIRvwhOW2cYAl8JjYiK4C3SoHLah2zNw+lG+9Jk+EoJ+tJ3m/78lI/ClYYMv5lbu3 /ozYA/VGSZRE91ESN0U+V8D8oY44G046JY7smLGUOdJ1rKYpBayzfl1v5oGDm6hen4UB qXTTT060w0GgfdO6jeKufxXQrEQouPbHwMMMiWTmC9qiJxXvCEb4HdfDgKbzJLxJqY6i 7qHR5jWgGmF9WlQ6Gn2bEl4IJAj/0aiVEXRwMmx96QI01cL3SclImIaU8DAHqoMejZfE bxitOiMu/c546fkk6CIBmivOXXYYTwWBZcTc60S4gDOG9S2KMlv/z6yMEuDw55vMSYbu n7ZA== X-Gm-Message-State: AOJu0YxEpa3BJosFOg5iFy2SDuEUduOXGrch7kdP5bob/MKsQsznahOs 0KnSEx86sVyBFbExgMhNb3Gw9TCyj9D4oPZG+AIP4JJEh6UHwmZFXGsG0zVDnA== X-Gm-Gg: ASbGncuxMZzIZfXssALyinkHtro3NUGxrFimR1O5pbFJxwzoUiuuB2hmAtb40iOPRU2 w6KHY70KZbz48TeyMId8DiFUWA9EZ1tXKxxFFI53CaFuUE7XnaaHUtXtRy+qZM3wS+3hHIqWClU d0EA7zQ5qvF1xzH7gpJGKGZpxr1GAxDxQ4FDqu/XEGBgE89FDmsvuDmvZJw3IyE1sdhjESnwJyI 28s2PbRAvnRhFD04V0X9qEotmahB7ClMj+GtP5lMr+59OB2dKI8G36pH2NhoKEPyEIy/B+t0D8e LNydkPV0QATAOwuIjOzfv7buUW2bcTJphyPnR4nrWooCT4p488Q4XoczTQCGvpAPjzG5PlbEBX+ FyGktLks5TlM476TAWOs= X-Google-Smtp-Source: AGHT+IEf5xrfmEnae0RxwK2lP3DZfM9VIsLZNcLlr4QzBiJz6iFZg2arNeXaxyq8M+RJb/SLXcBEbA== X-Received: by 2002:a05:6000:26ca:b0:3a5:2949:6c38 with SMTP id ffacd0b85a97d-3a5522ddd75mr1641497f8f.52.1749551209674; Tue, 10 Jun 2025 03:26:49 -0700 (PDT) Received: from lightning.caipicrew.dd-dns.de ([2001:a61:aa3:d01:75c4:e686:37fd:e458]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a53244d10asm12095680f8f.67.2025.06.10.03.26.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 03:26:48 -0700 (PDT) Message-ID: <68480868.5d0a0220.1e214d.68a6@mx.google.com> X-Google-Original-Message-ID: <20250610102647.GA22343@caipicrew.dd-dns.de;lightning.caipicrew.dd-dns.de> Sender: Michael Banck Received: from mbanck by lightning.caipicrew.dd-dns.de with local (Exim 4.92) (envelope-from ) id 1uOwBs-0005y8-An; Tue, 10 Jun 2025 12:26:48 +0200 Date: Tue, 10 Jun 2025 12:26:48 +0200 From: Michael Banck To: Michael Paquier Cc: Postgres hackers Subject: Re: Safeguards against incorrect fd flags for fsync() References: <20191009062640.GB21379@paquier.xyz> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="ZPt4rx8FFjLCG7dd" Content-Disposition: inline In-Reply-To: <20191009062640.GB21379@paquier.xyz> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --ZPt4rx8FFjLCG7dd Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi, On Wed, Oct 09, 2019 at 03:26:40PM +0900, Michael Paquier wrote: > After the set of issues discussed here, it seems to me that it would > be a good thing to have some safeguards against incorrect flags when > opening a fd which would be used for fsync(): > https://www.postgresql.org/message-id/16039-196fc97cc05e141c@postgresql.org > > Attached is a patch aimed at doing that. Historically O_RDONLY is 0, > so when looking at a directory we just need to make sure that no write > flags are used. For files, that's the contrary, a write flag has to > be used. > > Thoughts or better ideas? Well O_RDONLY might historically be 0 almost everywhere, but it is defined to 1 on the GNU system [1]: |#define O_RDONLY 0x0001 /* Open read-only. */ So there, the comparison with 0 does not work and initdb (at least) fails on assert-enabled builds: |running bootstrap script ... TRAP: FailedAssertion("(desc_flags & (O_RDWR | O_WRONLY)) == 0", File: "fd.c", Line: 395, PID: 4560) TTBOMK, POSIX does not mandate that O_RDONLY be 0, so I think this check is overly zealous. The better way might be to mask the flags with O_ACCMODE and then just check what you want, like in the attached. Thoughts? Michael [1] https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/mach/hurd/bits/fcntl.h;hb=HEAD#l39 --ZPt4rx8FFjLCG7dd Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-Make-safeguard-against-incorrect-fd-flags-for-fsync-.patch" From 16431e7b1aac9600fac285e77f39bb628a0dea14 Mon Sep 17 00:00:00 2001 From: Michael Banck Date: Tue, 10 Jun 2025 12:23:17 +0200 Subject: [PATCH] Make safeguard against incorrect fd flags for fsync more portable. The current code assumed that O_RDONLY is defined as 0, but this is not universally the case. To fix this, mask the flags with O_ACCMODE and assert that they are O_RDONLY for directories or not O_RDONLY for files. --- src/backend/storage/file/fd.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c index 0e8299dd556..cd1e661974a 100644 --- a/src/backend/storage/file/fd.c +++ b/src/backend/storage/file/fd.c @@ -411,14 +411,12 @@ pg_fsync(int fd) { int desc_flags = fcntl(fd, F_GETFL); - /* - * O_RDONLY is historically 0, so just make sure that for directories - * no write flags are used. - */ + desc_flags &= O_ACCMODE; + if (S_ISDIR(st.st_mode)) - Assert((desc_flags & (O_RDWR | O_WRONLY)) == 0); + Assert(desc_flags == O_RDONLY); else - Assert((desc_flags & (O_RDWR | O_WRONLY)) != 0); + Assert(desc_flags != O_RDONLY); } errno = 0; #endif -- 2.39.5 --ZPt4rx8FFjLCG7dd--