0002-CVE-2021-38604.patch 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. From 4cc79c217744743077bf7a0ec5e0a4318f1e6641 Mon Sep 17 00:00:00 2001
  2. From: Nikita Popov <npv1310@gmail.com>
  3. Date: Thu, 12 Aug 2021 16:09:50 +0530
  4. Subject: [PATCH] librt: add test (bug 28213)
  5. This test implements following logic:
  6. 1) Create POSIX message queue.
  7. Register a notification with mq_notify (using NULL attributes).
  8. Then immediately unregister the notification with mq_notify.
  9. Helper thread in a vulnerable version of glibc
  10. should cause NULL pointer dereference after these steps.
  11. 2) Once again, register the same notification.
  12. Try to send a dummy message.
  13. Test is considered successfulif the dummy message
  14. is successfully received by the callback function.
  15. Upstream-Status: Backport [https://sourceware.org/git/?p=glibc.git;a=commit;h=4cc79c217744743077bf7a0ec5e0a4318f1e6641]
  16. CVE: CVE-2021-38604
  17. Signed-off-by: Nikita Popov <npv1310@gmail.com>
  18. Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
  19. Signed-off-by: Vinay Kumar <vinay.m.engg@gmail.com>
  20. ---
  21. rt/Makefile | 1 +
  22. rt/tst-bz28213.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++
  23. 2 files changed, 102 insertions(+)
  24. create mode 100644 rt/tst-bz28213.c
  25. diff --git a/rt/Makefile b/rt/Makefile
  26. index 113cea03a5..910e775995 100644
  27. --- a/rt/Makefile
  28. +++ b/rt/Makefile
  29. @@ -74,6 +74,7 @@ tests := tst-shm tst-timer tst-timer2 \
  30. tst-aio7 tst-aio8 tst-aio9 tst-aio10 \
  31. tst-mqueue1 tst-mqueue2 tst-mqueue3 tst-mqueue4 \
  32. tst-mqueue5 tst-mqueue6 tst-mqueue7 tst-mqueue8 tst-mqueue9 \
  33. + tst-bz28213 \
  34. tst-timer3 tst-timer4 tst-timer5 \
  35. tst-cpuclock2 tst-cputimer1 tst-cputimer2 tst-cputimer3 \
  36. tst-shm-cancel \
  37. diff --git a/rt/tst-bz28213.c b/rt/tst-bz28213.c
  38. new file mode 100644
  39. index 0000000000..0c096b5a0a
  40. --- /dev/null
  41. +++ b/rt/tst-bz28213.c
  42. @@ -0,0 +1,101 @@
  43. +/* Bug 28213: test for NULL pointer dereference in mq_notify.
  44. + Copyright (C) The GNU Toolchain Authors.
  45. + This file is part of the GNU C Library.
  46. +
  47. + The GNU C Library is free software; you can redistribute it and/or
  48. + modify it under the terms of the GNU Lesser General Public
  49. + License as published by the Free Software Foundation; either
  50. + version 2.1 of the License, or (at your option) any later version.
  51. +
  52. + The GNU C Library is distributed in the hope that it will be useful,
  53. + but WITHOUT ANY WARRANTY; without even the implied warranty of
  54. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  55. + Lesser General Public License for more details.
  56. +
  57. + You should have received a copy of the GNU Lesser General Public
  58. + License along with the GNU C Library; if not, see
  59. + <https://www.gnu.org/licenses/>. */
  60. +
  61. +#include <errno.h>
  62. +#include <sys/types.h>
  63. +#include <sys/stat.h>
  64. +#include <fcntl.h>
  65. +#include <unistd.h>
  66. +#include <mqueue.h>
  67. +#include <signal.h>
  68. +#include <stdlib.h>
  69. +#include <string.h>
  70. +#include <support/check.h>
  71. +
  72. +static mqd_t m = -1;
  73. +static const char msg[] = "hello";
  74. +
  75. +static void
  76. +check_bz28213_cb (union sigval sv)
  77. +{
  78. + char buf[sizeof (msg)];
  79. +
  80. + (void) sv;
  81. +
  82. + TEST_VERIFY_EXIT ((size_t) mq_receive (m, buf, sizeof (buf), NULL)
  83. + == sizeof (buf));
  84. + TEST_VERIFY_EXIT (memcmp (buf, msg, sizeof (buf)) == 0);
  85. +
  86. + exit (0);
  87. +}
  88. +
  89. +static void
  90. +check_bz28213 (void)
  91. +{
  92. + struct sigevent sev;
  93. +
  94. + memset (&sev, '\0', sizeof (sev));
  95. + sev.sigev_notify = SIGEV_THREAD;
  96. + sev.sigev_notify_function = check_bz28213_cb;
  97. +
  98. + /* Step 1: Register & unregister notifier.
  99. + Helper thread should receive NOTIFY_REMOVED notification.
  100. + In a vulnerable version of glibc, NULL pointer dereference follows. */
  101. + TEST_VERIFY_EXIT (mq_notify (m, &sev) == 0);
  102. + TEST_VERIFY_EXIT (mq_notify (m, NULL) == 0);
  103. +
  104. + /* Step 2: Once again, register notification.
  105. + Try to send one message.
  106. + Test is considered successful, if the callback does exit (0). */
  107. + TEST_VERIFY_EXIT (mq_notify (m, &sev) == 0);
  108. + TEST_VERIFY_EXIT (mq_send (m, msg, sizeof (msg), 1) == 0);
  109. +
  110. + /* Wait... */
  111. + pause ();
  112. +}
  113. +
  114. +static int
  115. +do_test (void)
  116. +{
  117. + static const char m_name[] = "/bz28213_queue";
  118. + struct mq_attr m_attr;
  119. +
  120. + memset (&m_attr, '\0', sizeof (m_attr));
  121. + m_attr.mq_maxmsg = 1;
  122. + m_attr.mq_msgsize = sizeof (msg);
  123. +
  124. + m = mq_open (m_name,
  125. + O_RDWR | O_CREAT | O_EXCL,
  126. + 0600,
  127. + &m_attr);
  128. +
  129. + if (m < 0)
  130. + {
  131. + if (errno == ENOSYS)
  132. + FAIL_UNSUPPORTED ("POSIX message queues are not implemented\n");
  133. + FAIL_EXIT1 ("Failed to create POSIX message queue: %m\n");
  134. + }
  135. +
  136. + TEST_VERIFY_EXIT (mq_unlink (m_name) == 0);
  137. +
  138. + check_bz28213 ();
  139. +
  140. + return 0;
  141. +}
  142. +
  143. +#include <support/test-driver.c>
  144. --
  145. 2.31.1