public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] rt: fix shm_open not set ENAMETOOLONG when name exceeds {_POSIX_PATH_MAX}
@ 2023-03-07  9:45 abushwang
  2023-03-07 10:02 ` Xi Ruoyao
  0 siblings, 1 reply; 6+ messages in thread
From: abushwang @ 2023-03-07  9:45 UTC (permalink / raw)
  To: libc-alpha, carlos; +Cc: abushwang

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 | 5 +++--
 rt/shm_open.c         | 5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/posix/shm-directory.c b/posix/shm-directory.c
index 86d9fd8e4f..ca9d9f2f77 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,9 @@ __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
+  if (namelen == 0 || namelen > NAME_MAX || memchr (name, '/', namelen) != NULL
       || alloc_buffer_has_failed (&buffer))
-    return -1;
+    return namelen ? ENAMETOOLONG : 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;
     }
 
-- 
2.36.1


^ permalink raw reply	[flat|nested] 6+ messages in thread
* [PATCH] rt: fix shm_open not set ENAMETOOLONG when name exceeds {_POSIX_PATH_MAX}
@ 2023-03-07  8:32 abushwang
  2023-03-07  8:54 ` Xi Ruoyao
  0 siblings, 1 reply; 6+ messages in thread
From: abushwang @ 2023-03-07  8:32 UTC (permalink / raw)
  To: libc-alpha, carlos; +Cc: abushwang

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 | 5 ++++-
 rt/shm_open.c         | 5 +++--
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/posix/shm-directory.c b/posix/shm-directory.c
index 86d9fd8e4f..93aaeb60a0 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)
@@ -50,13 +51,15 @@ __shm_get_name (struct shmdir_name *result, const char *name, bool sem_prefix)
   while (name[0] == '/')
     ++name;
   namelen = strlen (name);
+  if (namelen > NAME_MAX)
+    return ENAMETOOLONG;
 
   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;
+    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;
     }
 
-- 
2.36.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-03-07 10:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-07  9:45 [PATCH] rt: fix shm_open not set ENAMETOOLONG when name exceeds {_POSIX_PATH_MAX} abushwang
2023-03-07 10:02 ` Xi Ruoyao
  -- strict thread matches above, loose matches on Subject: below --
2023-03-07  8:32 abushwang
2023-03-07  8:54 ` Xi Ruoyao
2023-03-07  9:46   ` abush wang
2023-03-07  9:46     ` abush wang

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).