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