public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: abushwang <abushwangs@gmail.com>
To: libc-alpha@sourceware.org, adhemerval.zanella@linaro.org
Cc: abushwang <abushwangs@gmail.com>
Subject: [PATCH v4] rt: fix shm_open not set ENAMETOOLONG when name exceeds {_POSIX_PATH_MAX}
Date: Tue,  7 Mar 2023 20:16:20 +0800	[thread overview]
Message-ID: <20230307121620.793600-1-abushwangs@gmail.com> (raw)

according to man-pages-posix-2017, shm_open() function may fail if the length
of the name argument exceeds {_POSIX_PATH_MAX} and set ENAMETOOLONG

Signed-off-by: abushwang <abushwangs@gmail.com>
---
 posix/shm-directory.c      | 12 +++++++++---
 rt/shm_open.c              |  5 +++--
 sysdeps/pthread/sem_open.c |  5 +++--
 3 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/posix/shm-directory.c b/posix/shm-directory.c
index 86d9fd8e4f..f130bab3c2 100644
--- a/posix/shm-directory.c
+++ b/posix/shm-directory.c
@@ -25,6 +25,7 @@
 #include <string.h>
 #include <sys/mman.h>
 #include <fcntl.h>
+#include <errno.h>
 
 int
 __shm_get_name (struct shmdir_name *result, const char *name, bool sem_prefix)
@@ -54,9 +55,14 @@ __shm_get_name (struct shmdir_name *result, const char *name, bool sem_prefix)
   if (sem_prefix)
     alloc_buffer_copy_bytes (&buffer, "sem.", strlen ("sem."));
   alloc_buffer_copy_bytes (&buffer, name, namelen + 1);
-  if (namelen == 0 || memchr (name, '/', namelen) != NULL
-      || alloc_buffer_has_failed (&buffer))
-    return -1;
+  if (namelen == 0 || memchr (name, '/', namelen) != NULL)
+    return EINVAL;
+  if (alloc_buffer_has_failed (&buffer))
+    {
+      if (namelen > NAME_MAX)
+        return ENAMETOOLONG;
+      return EINVAL;
+    }
   return 0;
 }
 libc_hidden_def (__shm_get_name)
diff --git a/rt/shm_open.c b/rt/shm_open.c
index 6c1f4d604f..fc1dc96bb4 100644
--- a/rt/shm_open.c
+++ b/rt/shm_open.c
@@ -30,9 +30,10 @@ int
 __shm_open (const char *name, int oflag, mode_t mode)
 {
   struct shmdir_name dirname;
-  if (__shm_get_name (&dirname, name, false) != 0)
+  int ret =__shm_get_name (&dirname, name, false);
+  if (ret != 0)
     {
-      __set_errno (EINVAL);
+      __set_errno (ret);
       return -1;
     }
 
diff --git a/sysdeps/pthread/sem_open.c b/sysdeps/pthread/sem_open.c
index 2551b035e1..2d32a13557 100644
--- a/sysdeps/pthread/sem_open.c
+++ b/sysdeps/pthread/sem_open.c
@@ -47,9 +47,10 @@ __sem_open (const char *name, int oflag, ...)
     }
 
   struct shmdir_name dirname;
-  if (__shm_get_name (&dirname, name, true) != 0)
+  int ret = __shm_get_name (&dirname, name, true);
+  if (ret != 0)
     {
-      __set_errno (EINVAL);
+      __set_errno (ret);
       return SEM_FAILED;
     }
 
-- 
2.36.1


             reply	other threads:[~2023-03-07 12:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-07 12:16 abushwang [this message]
2023-03-08 13:11 ` Adhemerval Zanella Netto

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230307121620.793600-1-abushwangs@gmail.com \
    --to=abushwangs@gmail.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).