Report on failing tests with thread stack protection and their resolution.

Utkarsh Rai utkarsh.rai60 at gmail.com
Thu Dec 3 00:53:27 UTC 2020


Hello,
As discussed in this
<https://lists.rtems.org/pipermail/devel/2020-November/063341.html> thread,
I have compiled a list of the tests that deal with inter stack
communication and fail with the thread stack protection option. Most of
these tests pass when, as Sebastian suggested and had provided a
wonderful example, I disable memory protection at places where contents of
different thread stacks are accessed by the current thread. There are a few
tests that still fail due to inter-stack access in the application code
itself.

The changes I have made are -

diff --git a/bsps/arm/realview-pbx-a9/mmu/bsp-set-mmu-attr.c
b/bsps/arm/realview-pbx-a9/mmu/bsp-set-mmu-attr.c
index c176d4b8c5..a45b175395 100644
--- a/bsps/arm/realview-pbx-a9/mmu/bsp-set-mmu-attr.c
+++ b/bsps/arm/realview-pbx-a9/mmu/bsp-set-mmu-attr.c
@@ -1,15 +1,49 @@
 #include <bsp/arm-cp15-start.h>
 #include <rtems/score/memoryprotection.h>
+#include <rtems/score/threadimpl.h>
 #include <libcpu/arm-cp15.h>

+bool set_memory_flags(Thread_Control* thread, void* arg)
+{
+  uintptr_t begin;
+  uintptr_t end;
+  uint32_t flags;
+  rtems_interrupt_level irq_level;
+  Thread_Control *executing;
+
+  executing = _Thread_Executing;
+
+  if(thread !=  executing) {
+
+    flags = *( uint32_t *)( arg );
+    begin = thread->Start.Initial_stack.area;
+    end = begin + thread->Start.Initial_stack.size;
+
+    rtems_interrupt_disable(irq_level);
+    arm_cp15_set_translation_table_entries(begin, end, flags);
+    rtems_interrupt_enable(irq_level);
+  }
+
+  return false;
+}
+
+rtems_status_code _Memory_protection_Enable( void )
+{
+  uint32_t access_flags;
+
+  access_flags = translate_flags(  RTEMS_NO_ACCESS );
+
+  _Thread_Iterate( set_memory_flags, &access_flags );
+
+  return RTEMS_SUCCESSFUL; // check the return values for iterating
function and current method.
+}
+
+rtems_status_code _Memory_protection_Disable( void )
+{
+  uint32_t access_flags;
+
+  access_flags = translate_flags(  RTEMS_READ_WRITE );
+
+  _Thread_Iterate( set_memory_flags, &access_flags );
+
+  return RTEMS_SUCCESSFUL;
 }
\ No newline at end of file
diff --git a/cpukit/include/rtems/score/coremsgimpl.h
b/cpukit/include/rtems/score/coremsgimpl.h
index e598dce96a..3719a3d3c8 100644
--- a/cpukit/include/rtems/score/coremsgimpl.h
+++ b/cpukit/include/rtems/score/coremsgimpl.h
@@ -27,6 +27,10 @@
 #include <rtems/score/threaddispatch.h>
 #include <rtems/score/threadqimpl.h>

+#if defined RTEMS_THREAD_STACK_PROTECTION
+ #include <rtems/score/memoryprotection.h>
+#endif
+
 #include <limits.h>
 #include <string.h>

@@ -586,7 +590,9 @@ RTEMS_INLINE_ROUTINE Thread_Control
*_CORE_message_queue_Dequeue_receiver(
   if ( the_thread == NULL ) {
     return NULL;
   }
-
+#if defined RTEMS_THREAD_STACK_PROTECTION
+  _Memory_protection_Disable();
+#endif
    *(size_t *) the_thread->Wait.return_argument = size;
    the_thread->Wait.count = (uint32_t) submit_type;

@@ -595,6 +601,9 @@ RTEMS_INLINE_ROUTINE Thread_Control
*_CORE_message_queue_Dequeue_receiver(
     the_thread->Wait.return_argument_second.mutable_object,
     size
   );
+#if defined RTEMS_THREAD_STACK_PROTECTION
+  _Memory_protection_Enable();
+#endif

   _Thread_queue_Extract_critical(
     &the_message_queue->Wait_queue.Queue,

diff --git a/cpukit/posix/src/psignalunblockthread.c
b/cpukit/posix/src/psignalunblockthread.c
index 80a0f33a09..e0f8468de6 100644
--- a/cpukit/posix/src/psignalunblockthread.c
+++ b/cpukit/posix/src/psignalunblockthread.c
@@ -24,6 +24,9 @@
 #include <signal.h>

 #include <rtems/score/isr.h>
+#if defined RTEMS_THREAD_STACK_PROTECTION
+#include <rtems/score/memoryprotection.h>
+#endif
 #include <rtems/score/threadimpl.h>
 #include <rtems/score/threadqimpl.h>
 #include <rtems/score/watchdogimpl.h>
@@ -205,6 +208,10 @@ bool _POSIX_signals_Unblock_thread(

       the_info = (siginfo_t *) the_thread->Wait.return_argument;

+#if defined RTEMS_THREAD_STACK_PROTECTION
+_Memory_protection_Disable();
+#endif
+
       if ( !info ) {
         the_info->si_signo = signo;
         the_info->si_code = SI_USER;
@@ -212,6 +219,9 @@ bool _POSIX_signals_Unblock_thread(
       } else {
         *the_info = *info;
       }
+#if defined RTEMS_THREAD_STACK_PROTECTION
+_Memory_protection_Enable();
+#endif

       _Thread_queue_Extract_with_proxy( the_thread );
       return _POSIX_signals_Unblock_thread_done( the_thread, api, true );
diff --git a/cpukit/rtems/src/eventsurrender.c
b/cpukit/rtems/src/eventsurrender.c
index 49f77d2663..5de62ec292 100644
--- a/cpukit/rtems/src/eventsurrender.c
+++ b/cpukit/rtems/src/eventsurrender.c
@@ -23,6 +23,10 @@
 #include <rtems/score/threadimpl.h>
 #include <rtems/score/watchdogimpl.h>

+#if defined RTEMS_THREAD_STACK_PROTECTION
+  #include <rtems/score/memoryprotection.h>
+#endif
+
 static void _Event_Satisfy(
   Thread_Control  *the_thread,
   Event_Control   *event,
@@ -31,7 +35,13 @@ static void _Event_Satisfy(
 )
 {
   event->pending_events = _Event_sets_Clear( pending_events, seized_events
);
+#if defined RTEMS_THREAD_STACK_PROTECTION
+    _Memory_protection_Disable();
+#endif
   *(rtems_event_set *) the_thread->Wait.return_argument = seized_events;
+#if defined RTEMS_THREAD_STACK_PROTECTION
+    _Memory_protection_Enable();
+#endif
 }

 static bool _Event_Is_blocking_on_event(
diff --git a/cpukit/rtems/src/regionprocessqueue.c
b/cpukit/rtems/src/regionprocessqueue.c
index 4adaf66674..29b078a38c 100644
--- a/cpukit/rtems/src/regionprocessqueue.c
+++ b/cpukit/rtems/src/regionprocessqueue.c
@@ -22,6 +22,10 @@
 #include <rtems/score/status.h>
 #include <rtems/score/threadqimpl.h>

+#if defined RTEMS_THREAD_STACK_PROTECTION
+ #include <rtems/score/memoryprotection.h>
+#endif
+
 void _Region_Process_queue(
   Region_Control *the_region
 )
@@ -63,8 +67,13 @@ void _Region_Process_queue(

     if ( the_segment == NULL )
       break;
-
+#if defined RTEMS_THREAD_STACK_PROTECTION
+    _Memory_protection_Disable();
+#endif
     *(void **)the_thread->Wait.return_argument = the_segment;
+#if defined RTEMS_THREAD_STACK_PROTECTION
+    _Memory_protection_Enable();
+#endif
     _Thread_queue_Extract( the_thread );
     the_thread->Wait.return_code = STATUS_SUCCESSFUL;
   }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/devel/attachments/20201203/3ebd6e74/attachment-0001.html>
-------------- next part --------------
+------------------+---------+----------------------------------------------------+
| TESTS            | STATUS  | METHOD                                             |
+------------------+---------+----------------------------------------------------+
|                  |         |                                                    |
+------------------+---------+----------------------------------------------------+
| SPTESTS          |         |                                                    |
+------------------+---------+----------------------------------------------------+
| spthreadsq01     | Passes  | Disable memory protection                          |
+------------------+---------+----------------------------------------------------+
| spmsgq_error02   | PASSES  | Disable mempry protection                          |
+------------------+---------+----------------------------------------------------+
| sp13             | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| spintrcitical10  | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| spmrsp           | Failing | Access to inter stack variable                     |
|                  |         | in application code                                |
+------------------+---------+----------------------------------------------------+
| spcontext1       | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| spextension01    | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| spthreadlife01   | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| spintrcitical21  | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| sptimerserver01  | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| spmutex1         | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| spintrcitical18  | Failing | Access to inter stack variable in application code |
+------------------+---------+----------------------------------------------------+
| spthread01       | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| sp37             | Failing | DMP                                                |
+------------------+---------+----------------------------------------------------+
| speventtransient | Failing | DMP                                                |
+------------------+---------+----------------------------------------------------+
| sp42             | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| sp04             | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| spintrcitical12  | Failing | Access to inter stack variable in application code |
+------------------+---------+----------------------------------------------------+
| spintrcritical11 | Failing | Access to inter stack variable in application code |
+------------------+---------+----------------------------------------------------+
| sptls01          | Failing | Access to inter stack variable in application code |
+------------------+---------+----------------------------------------------------+
| sptls02          | Failing | Access to inter stack variable in application code |
+------------------+---------+----------------------------------------------------+
| spsyslock01      | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
|                  |         |                                                    |
+------------------+---------+----------------------------------------------------+
| TMTESTS          |         |                                                    |
+------------------+---------+----------------------------------------------------+
| tm14             | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| tm11             | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| tm13             | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| tm15             | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| tm12             | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| tm16             | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| tm09             | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| tm21             | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| tm22             | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| tmonetoone       | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
|                  |         |                                                    |
+------------------+---------+----------------------------------------------------+
| LIBTESTS         |         |                                                    |
+------------------+---------+----------------------------------------------------+
| ttest01          | Failing | Access to inter stack variable in application code |
+------------------+---------+----------------------------------------------------+
| newlib01         | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| block06          | Failing | Access to inter stack variable in application code |
+------------------+---------+----------------------------------------------------+
|                  |         |                                                    |
+------------------+---------+----------------------------------------------------+
| RHEALSTONE       |         |                                                    |
+------------------+---------+----------------------------------------------------+
| rhmlatency       | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
|                  |         |                                                    |
+------------------+---------+----------------------------------------------------+
| PSXTESTS         |         |                                                    |
+------------------+---------+----------------------------------------------------+
| psxcleanup01     | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| psxonce01        | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
| psxkey6          | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
|                  |         |                                                    |
+------------------+---------+----------------------------------------------------+
| FSTESTS          |         |                                                    |
+------------------+---------+----------------------------------------------------+
| fsclose01        | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
|                  |         |                                                    |
+------------------+---------+----------------------------------------------------+
| SAMPLES          |         |                                                    |
+------------------+---------+----------------------------------------------------+
| Unlimited        | PASSES  | DMP                                                |
+------------------+---------+----------------------------------------------------+
|                  |         |                                                    |
+------------------+---------+----------------------------------------------------+
|                  |         |                                                    |
+------------------+---------+----------------------------------------------------+
|                  |         |                                                    |
+------------------+---------+----------------------------------------------------+
|                  |         |                                                    |
+------------------+---------+----------------------------------------------------+


More information about the devel mailing list