[PATCH rtems6 - v2 16/16] flashdev: restrict flash writes

berndmoessner80 at gmail.com berndmoessner80 at gmail.com
Sun Jan 7 18:18:50 UTC 2024


From: Bernd Moessner <berndmoessner80 at gmail.com>

Disallow writes that do not match alignment / req. length

This feature applies if the min write block size is != 0.

Closes #4981
---
 cpukit/dev/flash/flashdev.c           | 11 +++++++++++
 testsuites/libtests/flashdev01/init.c | 24 +++++++++++++++++++++++-
 2 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/cpukit/dev/flash/flashdev.c b/cpukit/dev/flash/flashdev.c
index 8aa4380ab7..9e319321be 100644
--- a/cpukit/dev/flash/flashdev.c
+++ b/cpukit/dev/flash/flashdev.c
@@ -323,6 +323,7 @@ static int rtems_flashdev_read_write(
   rtems_flashdev *flash = IMFS_generic_get_context_by_iop( iop );
   off_t addr;
   int status;
+  size_t min_write_block_size = 0;
 
   if ( read_buff == NULL && write_buff == NULL ) {
     rtems_set_errno_and_return_minus_one( EINVAL );
@@ -339,6 +340,16 @@ static int rtems_flashdev_read_write(
   if ( read_buff != NULL ) {
     status = ( *flash->read )( flash, addr, count, read_buff );
   } else if ( write_buff != NULL ) {
+    /* Make sure we have aligned writes in min. write block size is set */
+    ( *flash->get_min_write_block_size )( flash, &min_write_block_size );
+    if (min_write_block_size)
+    {
+      if((addr % min_write_block_size) || (count % min_write_block_size) )
+      {
+        rtems_flashdev_release( flash );
+        rtems_set_errno_and_return_minus_one( EINVAL );
+      }
+    }
     status = ( *flash->write )( flash, addr, count, write_buff );
   }
   rtems_flashdev_release( flash );
diff --git a/testsuites/libtests/flashdev01/init.c b/testsuites/libtests/flashdev01/init.c
index 5755708525..bbbbebbbfe 100644
--- a/testsuites/libtests/flashdev01/init.c
+++ b/testsuites/libtests/flashdev01/init.c
@@ -215,11 +215,33 @@ static void run_test(void) {
   /* Open the flashdev */
   file = fopen(flash_path, "r+");
 
+  fd = fileno(file);
+
+  /* Write the test name to the flash - actually to newlib s buffer */
+  status = fwrite(test_string, 1, sizeof(test_string)-1, file);
+  /* False positive, rtems_flashdev_read_write was not called. String is still
+   * in buffer.
+   */
+  rtems_test_assert(status == sizeof(test_string)-1);
+  /* Flush will call rtems_flashdev_read_write and we see that things fail */
+  status = fflush(file);
+  rtems_test_assert(status);
+
   /* Adjust the file buffering */
   status = setvbuf(file, NULL, _IOFBF, min_write_write_block_size_in[1]);
   rtems_test_assert(!status);
 
-  fd = fileno(file);
+  fseek(file, 0x0, SEEK_SET);
+
+  /* Write the test name to the flash - actually to newlib s buffer */
+  status = fwrite(test_string, 1, sizeof(test_string)-1, file);
+  /* False positive, rtems_flashdev_read_write was not called. String is still
+   * in buffer.
+   */
+  rtems_test_assert(status == sizeof(test_string)-1);
+  /* Flush will call rtems_flashdev_read_write and we see that things fail */
+  status = fflush(file);
+  rtems_test_assert(status);
 
   /* Test Regions - this one must fail */
   region.offset = 0x401;
-- 
2.34.1



More information about the devel mailing list