threads / patch / 64643

patchopen: check fd_flags value before calling fcntl

Subject: [PATCH] open: check fd_flags value before calling fcntl

## tl;dr

2 messages between Dec 18, 2025 and Dec 18, 2025. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

AZero13 via GitGitGadget· Dec 18, 2025, 03:14 UTC · lore
From: AZero13 <gfunni234@gmail.com>
Otherwise, fcntl can be called with invalid fd_flags.
Signed-off-by: Greg Funni <gfunni234@gmail.com>
---
    open: check fd_flags value before calling fcntl
    
    Otherwise, fcntl can be called with invalid fd_flags.
    
    Signed-off-by:Greg Funni gfunni234@gmail.com
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2131%2FAZero13%2Ffixopen-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2131/AZero13/fixopen-v1
Pull-Request: https://github.com/git/git/pull/2131
 compat/open.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Show changes to compat/open.c +2 −2
diff --git a/compat/open.c b/compat/open.c
index 37ae2b1aeb..b313bcd364 100644
--- a/compat/open.c
+++ b/compat/open.c
@@ -44,8 +44,8 @@ int git_open_cloexec(const char *name, int flags)
 
 		if (!o_cloexec && 0 <= fd && fd_cloexec) {
 			/* Opened w/o O_CLOEXEC?  try with fcntl(2) to add it */
-			int flags = fcntl(fd, F_GETFD);
-			if (fcntl(fd, F_SETFD, flags | fd_cloexec))
+			int fd_flags = fcntl(fd, F_GETFD);
+			if (fd_flags < 0 || fcntl(fd, F_SETFD, fd_flags | fd_cloexec))
 				fd_cloexec = 0;
 		}
 	}

base-commit: c4a0c8845e2426375ad257b6c221a3a7d92ecfda
-- 
gitgitgadget
Junio C Hamano· Dec 18, 2025, 03:45 UTC · re: AZero13 via GitGitGadget · lore

Re: [PATCH] open: check fd_flags value before calling fcntl

"AZero13 via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: AZero13 <gfunni234@gmail.com>
>
> Otherwise, fcntl can be called with invalid fd_flags.

This somehow feels mischaracterised. The intent of the code is already to check the current flags value using getfd and or-in the cloexec bit to call setfd. What this patch fixes is to handle a failed getfd case correctly.

    Subject: git_open_cloexec(): handle failing F_GETFD
    Before using F_SETFD to add in fd_cloexec bit, the code uses
    F_GETFD to see the current set of flags.  As it does not pay
    attention to potential failures, F_SETFD may be called with a
    set of invalid fcntl bits.
    Continue without calling F_SETFD and behave the same way as the
    case where F_SETFD failed, when the initial F_GETFD failed.
or something like that, perhaps?
> Signed-off-by: Greg Funni <gfunni234@gmail.com>

The in-body "From: AZer..." line we see above should say "From: Greg..." instead. Set "git config set user.name 'Greg Funni'" in the repository you use to contribute to this project, amend the commit so that it will record "Greg..." instead of "AZer..." as its author, and force push to GGG and tell GGG to send out the email, perhaps?

Show 31 quoted lines
> ---
>     open: check fd_flags value before calling fcntl
>     
>     Otherwise, fcntl can be called with invalid fd_flags.
>     
>     Signed-off-by:Greg Funni gfunni234@gmail.com
>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2131%2FAZero13%2Ffixopen-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2131/AZero13/fixopen-v1
> Pull-Request: https://github.com/git/git/pull/2131
>
>  compat/open.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/compat/open.c b/compat/open.c
> index 37ae2b1aeb..b313bcd364 100644
> --- a/compat/open.c
> +++ b/compat/open.c
> @@ -44,8 +44,8 @@ int git_open_cloexec(const char *name, int flags)
>  
>  		if (!o_cloexec && 0 <= fd && fd_cloexec) {
>  			/* Opened w/o O_CLOEXEC?  try with fcntl(2) to add it */
> -			int flags = fcntl(fd, F_GETFD);
> -			if (fcntl(fd, F_SETFD, flags | fd_cloexec))
> +			int fd_flags = fcntl(fd, F_GETFD);
> +			if (fd_flags < 0 || fcntl(fd, F_SETFD, fd_flags | fd_cloexec))
>  				fd_cloexec = 0;
>  		}
>  	}
>
> base-commit: c4a0c8845e2426375ad257b6c221a3a7d92ecfda

← back to recent threads