94084c
commit 7c987a5ccb31df80456d53a094e47f81310f549b
94084c
Author: Nikita Popov <npv1310@gmail.com>
94084c
Date:   Thu Aug 12 16:09:50 2021 +0530
94084c
94084c
    librt: add test (bug 28213)
94084c
    
94084c
    This test implements following logic:
94084c
    1) Create POSIX message queue.
94084c
       Register a notification with mq_notify (using NULL attributes).
94084c
       Then immediately unregister the notification with mq_notify.
94084c
       Helper thread in a vulnerable version of glibc
94084c
       should cause NULL pointer dereference after these steps.
94084c
    2) Once again, register the same notification.
94084c
       Try to send a dummy message.
94084c
       Test is considered successfulif the dummy message
94084c
       is successfully received by the callback function.
94084c
    
94084c
    Signed-off-by: Nikita Popov <npv1310@gmail.com>
94084c
    Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
94084c
    (cherry picked from commit 4cc79c217744743077bf7a0ec5e0a4318f1e6641)
94084c
94084c
diff --git a/rt/Makefile b/rt/Makefile
94084c
index 113cea03a5b75613..910e7759956d7ae9 100644
94084c
--- a/rt/Makefile
94084c
+++ b/rt/Makefile
94084c
@@ -74,6 +74,7 @@ tests := tst-shm tst-timer tst-timer2 \
94084c
 	 tst-aio7 tst-aio8 tst-aio9 tst-aio10 \
94084c
 	 tst-mqueue1 tst-mqueue2 tst-mqueue3 tst-mqueue4 \
94084c
 	 tst-mqueue5 tst-mqueue6 tst-mqueue7 tst-mqueue8 tst-mqueue9 \
94084c
+	 tst-bz28213 \
94084c
 	 tst-timer3 tst-timer4 tst-timer5 \
94084c
 	 tst-cpuclock2 tst-cputimer1 tst-cputimer2 tst-cputimer3 \
94084c
 	 tst-shm-cancel \
94084c
diff --git a/rt/tst-bz28213.c b/rt/tst-bz28213.c
94084c
new file mode 100644
94084c
index 0000000000000000..0c096b5a0ad4170a
94084c
--- /dev/null
94084c
+++ b/rt/tst-bz28213.c
94084c
@@ -0,0 +1,101 @@
94084c
+/* Bug 28213: test for NULL pointer dereference in mq_notify.
94084c
+   Copyright (C) The GNU Toolchain Authors.
94084c
+   This file is part of the GNU C Library.
94084c
+
94084c
+   The GNU C Library is free software; you can redistribute it and/or
94084c
+   modify it under the terms of the GNU Lesser General Public
94084c
+   License as published by the Free Software Foundation; either
94084c
+   version 2.1 of the License, or (at your option) any later version.
94084c
+
94084c
+   The GNU C Library is distributed in the hope that it will be useful,
94084c
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
94084c
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
94084c
+   Lesser General Public License for more details.
94084c
+
94084c
+   You should have received a copy of the GNU Lesser General Public
94084c
+   License along with the GNU C Library; if not, see
94084c
+   <https://www.gnu.org/licenses/>.  */
94084c
+
94084c
+#include <errno.h>
94084c
+#include <sys/types.h>
94084c
+#include <sys/stat.h>
94084c
+#include <fcntl.h>
94084c
+#include <unistd.h>
94084c
+#include <mqueue.h>
94084c
+#include <signal.h>
94084c
+#include <stdlib.h>
94084c
+#include <string.h>
94084c
+#include <support/check.h>
94084c
+
94084c
+static mqd_t m = -1;
94084c
+static const char msg[] = "hello";
94084c
+
94084c
+static void
94084c
+check_bz28213_cb (union sigval sv)
94084c
+{
94084c
+  char buf[sizeof (msg)];
94084c
+
94084c
+  (void) sv;
94084c
+
94084c
+  TEST_VERIFY_EXIT ((size_t) mq_receive (m, buf, sizeof (buf), NULL)
94084c
+		    == sizeof (buf));
94084c
+  TEST_VERIFY_EXIT (memcmp (buf, msg, sizeof (buf)) == 0);
94084c
+
94084c
+  exit (0);
94084c
+}
94084c
+
94084c
+static void
94084c
+check_bz28213 (void)
94084c
+{
94084c
+  struct sigevent sev;
94084c
+
94084c
+  memset (&sev, '\0', sizeof (sev));
94084c
+  sev.sigev_notify = SIGEV_THREAD;
94084c
+  sev.sigev_notify_function = check_bz28213_cb;
94084c
+
94084c
+  /* Step 1: Register & unregister notifier.
94084c
+     Helper thread should receive NOTIFY_REMOVED notification.
94084c
+     In a vulnerable version of glibc, NULL pointer dereference follows. */
94084c
+  TEST_VERIFY_EXIT (mq_notify (m, &sev) == 0);
94084c
+  TEST_VERIFY_EXIT (mq_notify (m, NULL) == 0);
94084c
+
94084c
+  /* Step 2: Once again, register notification.
94084c
+     Try to send one message.
94084c
+     Test is considered successful, if the callback does exit (0). */
94084c
+  TEST_VERIFY_EXIT (mq_notify (m, &sev) == 0);
94084c
+  TEST_VERIFY_EXIT (mq_send (m, msg, sizeof (msg), 1) == 0);
94084c
+
94084c
+  /* Wait... */
94084c
+  pause ();
94084c
+}
94084c
+
94084c
+static int
94084c
+do_test (void)
94084c
+{
94084c
+  static const char m_name[] = "/bz28213_queue";
94084c
+  struct mq_attr m_attr;
94084c
+
94084c
+  memset (&m_attr, '\0', sizeof (m_attr));
94084c
+  m_attr.mq_maxmsg = 1;
94084c
+  m_attr.mq_msgsize = sizeof (msg);
94084c
+
94084c
+  m = mq_open (m_name,
94084c
+               O_RDWR | O_CREAT | O_EXCL,
94084c
+               0600,
94084c
+               &m_attr);
94084c
+
94084c
+  if (m < 0)
94084c
+    {
94084c
+      if (errno == ENOSYS)
94084c
+        FAIL_UNSUPPORTED ("POSIX message queues are not implemented\n");
94084c
+      FAIL_EXIT1 ("Failed to create POSIX message queue: %m\n");
94084c
+    }
94084c
+
94084c
+  TEST_VERIFY_EXIT (mq_unlink (m_name) == 0);
94084c
+
94084c
+  check_bz28213 ();
94084c
+
94084c
+  return 0;
94084c
+}
94084c
+
94084c
+#include <support/test-driver.c>