8a984d
This is a rewrite of the commit for the pre-64-bit time_t version of
8a984d
the msgctl handling. Similar to semctl we want the RHEL8 handling of
8a984d
the unknown commands to be the same as upstream.
8a984d
8a984d
commit be9b0b9a012780a403a266c90878efffb9a5f3ca
8a984d
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
8a984d
Date:   Tue Sep 29 14:45:09 2020 -0300
8a984d
8a984d
    sysvipc: Return EINVAL for invalid msgctl commands
8a984d
    
8a984d
    It avoids regressions on possible future commands that might require
8a984d
    additional libc support.  The downside is new commands added by newer
8a984d
    kernels will need further glibc support.
8a984d
    
8a984d
    Checked on x86_64-linux-gnu and i686-linux-gnu (Linux v4.15 and v5.4).
8a984d
8a984d
diff --git a/sysdeps/unix/sysv/linux/msgctl.c b/sysdeps/unix/sysv/linux/msgctl.c
8a984d
index 7280cba31a8815a2..6a2c79d188b875b9 100644
8a984d
--- a/sysdeps/unix/sysv/linux/msgctl.c
8a984d
+++ b/sysdeps/unix/sysv/linux/msgctl.c
8a984d
@@ -29,6 +29,20 @@
8a984d
 int
8a984d
 __new_msgctl (int msqid, int cmd, struct msqid_ds *buf)
8a984d
 {
8a984d
+  switch (cmd)
8a984d
+    {
8a984d
+    case IPC_RMID:
8a984d
+    case IPC_SET:
8a984d
+    case IPC_STAT:
8a984d
+    case MSG_STAT:
8a984d
+    case MSG_STAT_ANY:
8a984d
+    case IPC_INFO:
8a984d
+    case MSG_INFO:
8a984d
+      break;
8a984d
+    default:
8a984d
+      __set_errno (EINVAL);
8a984d
+      return -1;
8a984d
+    }
8a984d
 #ifdef __ASSUME_DIRECT_SYSVIPC_SYSCALLS
8a984d
   return INLINE_SYSCALL_CALL (msgctl, msqid, cmd | __IPC_64, buf);
8a984d
 #else
8a984d
diff --git a/sysvipc/test-sysvipc.h b/sysvipc/test-sysvipc.h
8a984d
index ed0057b7871e505c..133fb71c6113a2b5 100644
8a984d
--- a/sysvipc/test-sysvipc.h
8a984d
+++ b/sysvipc/test-sysvipc.h
8a984d
@@ -134,4 +134,29 @@ first_shm_invalid_cmd (void)
8a984d
   return invalid;
8a984d
 }
8a984d
 
8a984d
+/* Return the first invalid command SysV IPC command for message queue.  */
8a984d
+static inline int
8a984d
+first_msg_invalid_cmd (void)
8a984d
+{
8a984d
+  const int msg_cmds[] = {
8a984d
+    MSG_STAT,
8a984d
+    MSG_INFO,
8a984d
+#ifdef MSG_STAT_ANY
8a984d
+    MSG_STAT_ANY,
8a984d
+#endif
8a984d
+  };
8a984d
+
8a984d
+  int invalid = first_common_invalid_cmd ();
8a984d
+  for (int i = 0; i < array_length (msg_cmds); i++)
8a984d
+    {
8a984d
+      if (invalid == msg_cmds[i])
8a984d
+	{
8a984d
+	  invalid++;
8a984d
+	  i = 0;
8a984d
+	}
8a984d
+    }
8a984d
+
8a984d
+  return invalid;
8a984d
+}
8a984d
+
8a984d
 #endif /* _TEST_SYSV_H  */
8a984d
diff --git a/sysvipc/test-sysvmsg.c b/sysvipc/test-sysvmsg.c
8a984d
index 1e0471807cd26da1..74a907ad39ee114e 100644
8a984d
--- a/sysvipc/test-sysvmsg.c
8a984d
+++ b/sysvipc/test-sysvmsg.c
8a984d
@@ -24,6 +24,8 @@
8a984d
 #include <sys/ipc.h>
8a984d
 #include <sys/msg.h>
8a984d
 
8a984d
+#include <test-sysvipc.h>
8a984d
+
8a984d
 #include <support/support.h>
8a984d
 #include <support/check.h>
8a984d
 #include <support/temp_file.h>
8a984d
@@ -86,6 +88,9 @@ do_test (void)
8a984d
       FAIL_EXIT1 ("msgget failed (errno=%d)", errno);
8a984d
     }
8a984d
 
8a984d
+  TEST_COMPARE (msgctl (msqid, first_msg_invalid_cmd (), NULL), -1);
8a984d
+  TEST_COMPARE (errno, EINVAL);
8a984d
+
8a984d
   /* Get message queue kernel information and do some sanity checks.  */
8a984d
   struct msqid_ds msginfo;
8a984d
   if (msgctl (msqid, IPC_STAT, &msginfo) == -1)