diff options
author | NIIBE Yutaka <[email protected]> | 2024-10-16 01:04:18 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 2024-10-29 11:27:30 +0000 |
commit | 2ae017a25fe800c9bc6fc1090179ec987bb8ba8d (patch) | |
tree | 51b94df8c8035c9837e096a87f66c4bb655bf9c4 | |
parent | po: Update German translation (diff) | |
download | gnupg-2ae017a25fe800c9bc6fc1090179ec987bb8ba8d.tar.gz gnupg-2ae017a25fe800c9bc6fc1090179ec987bb8ba8d.zip |
common: Fix a race condition in creating socketdir.
* common/homedir.c (_gnupg_socketdir_internal): Check return code
of gnupg_mkdir and handle the case of GPG_ERR_EEXIST.
--
GnuPG-bug-id: 7332
Signed-off-by: NIIBE Yutaka <[email protected]>
(cherry picked from commit 71840b57f48680b7555451a29026d9c6de4fe2bc)
-rw-r--r-- | common/homedir.c | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/common/homedir.c b/common/homedir.c index 6f99f3eab..9fcb90be8 100644 --- a/common/homedir.c +++ b/common/homedir.c @@ -971,6 +971,7 @@ _gnupg_socketdir_internal (int skip_checks, unsigned *r_info) { #if defined(HAVE_W32_SYSTEM) char *name; + gpg_err_code_t ec; (void)skip_checks; @@ -1048,7 +1049,8 @@ _gnupg_socketdir_internal (int skip_checks, unsigned *r_info) else if (!skip_checks) { /* Try to create the directory and check again. */ - if (gnupg_mkdir (name, "-rwx")) + ec = gnupg_mkdir (name, "-rwx"); + if (ec && ec != GPG_ERR_EEXIST) *r_info |= 16; /* mkdir failed. */ else if (gnupg_stat (name, &sb)) { @@ -1105,6 +1107,7 @@ _gnupg_socketdir_internal (int skip_checks, unsigned *r_info) const char *prefix; const char *s; char *name = NULL; + gpg_err_code_t ec; *r_info = 0; @@ -1161,8 +1164,14 @@ _gnupg_socketdir_internal (int skip_checks, unsigned *r_info) goto leave; } - /* Try to create the directory and check again. */ - if (gnupg_mkdir (prefix, "-rwx")) + /* Try to create the directory and check again. + * Here comes a possible race condition: + * stat(2) above failed by ENOENT, but another process does + * mkdir(2) before we do mkdir(2) + * So, an error with EEXIST should be handled. + */ + ec = gnupg_mkdir (prefix, "-rwx"); + if (ec && ec != GPG_ERR_EEXIST) { *r_info |= 16; /* mkdir failed. */ goto leave; @@ -1221,7 +1230,8 @@ _gnupg_socketdir_internal (int skip_checks, unsigned *r_info) else if (!skip_checks) { /* Try to create the directory and check again. */ - if (gnupg_mkdir (name, "-rwx")) + ec = gnupg_mkdir (name, "-rwx"); + if (ec && ec != GPG_ERR_EEXIST) *r_info |= 16; /* mkdir failed. */ else if (stat (prefix, &sb)) { |