[rtems commit] Filesystem: Read-only file system checks

Joel Sherrill joel at rtems.org
Thu Mar 29 14:14:28 UTC 2012


Module:    rtems
Branch:    master
Commit:    246b4f4e17a232f2d247cd6e4d7ed97395d7366b
Changeset: http://git.rtems.org/rtems/commit/?id=246b4f4e17a232f2d247cd6e4d7ed97395d7366b

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Fri Mar  2 10:18:10 2012 +0100

Filesystem: Read-only file system checks

 o Make sure EROFS is indicated for write operations on a read-only file
   system.
 o Add error indication for read-only file systems in fchmod() and
   fchown() according to POSIX.

---

 cpukit/libcsupport/src/fchmod.c           |   11 ++-
 cpukit/libcsupport/src/fchown.c           |   13 ++-
 cpukit/libcsupport/src/sup_fs_eval_path.c |   42 ++++---
 testsuites/fstests/fsrofs01/Makefile.am   |   20 +++
 testsuites/fstests/fsrofs01/fsrofs01.doc  |   11 ++
 testsuites/fstests/fsrofs01/fsrofs01.scn  |    2 +
 testsuites/fstests/fsrofs01/init.c        |  188 +++++++++++++++++++++++++++++
 testsuites/psxtests/psxfchx01/init.c      |    5 +-
 testsuites/psxtests/psxmount/test.c       |   33 +++++-
 9 files changed, 291 insertions(+), 34 deletions(-)

diff --git a/cpukit/libcsupport/src/fchmod.c b/cpukit/libcsupport/src/fchmod.c
index e651066..14fb416 100644
--- a/cpukit/libcsupport/src/fchmod.c
+++ b/cpukit/libcsupport/src/fchmod.c
@@ -28,9 +28,14 @@ int fchmod( int fd, mode_t mode )
   iop = rtems_libio_iop( fd );
   rtems_libio_check_is_open(iop);
 
-  rtems_filesystem_instance_lock( &iop->pathinfo );
-  rv = (*iop->pathinfo.ops->fchmod_h)( &iop->pathinfo, mode );
-  rtems_filesystem_instance_unlock( &iop->pathinfo );
+  if (iop->pathinfo.mt_entry->writeable) {
+    rtems_filesystem_instance_lock( &iop->pathinfo );
+    rv = (*iop->pathinfo.ops->fchmod_h)( &iop->pathinfo, mode );
+    rtems_filesystem_instance_unlock( &iop->pathinfo );
+  } else {
+    errno = EROFS;
+    rv = -1;
+  }
 
   return rv;
 }
diff --git a/cpukit/libcsupport/src/fchown.c b/cpukit/libcsupport/src/fchown.c
index b5891bf..d0d0400 100644
--- a/cpukit/libcsupport/src/fchown.c
+++ b/cpukit/libcsupport/src/fchown.c
@@ -28,11 +28,14 @@ int fchown( int fd, uid_t owner, gid_t group )
   iop = rtems_libio_iop( fd );
   rtems_libio_check_is_open(iop);
 
-  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
-
-  rtems_filesystem_instance_lock( &iop->pathinfo );
-  rv = (*iop->pathinfo.ops->chown_h)( &iop->pathinfo, owner, group );
-  rtems_filesystem_instance_unlock( &iop->pathinfo );
+  if (iop->pathinfo.mt_entry->writeable) {
+    rtems_filesystem_instance_lock( &iop->pathinfo );
+    rv = (*iop->pathinfo.ops->chown_h)( &iop->pathinfo, owner, group );
+    rtems_filesystem_instance_unlock( &iop->pathinfo );
+  } else {
+    errno = EROFS;
+    rv = -1;
+  }
 
   return rv;
 }
diff --git a/cpukit/libcsupport/src/sup_fs_eval_path.c b/cpukit/libcsupport/src/sup_fs_eval_path.c
index 1940cce..c827ba7 100644
--- a/cpukit/libcsupport/src/sup_fs_eval_path.c
+++ b/cpukit/libcsupport/src/sup_fs_eval_path.c
@@ -68,26 +68,32 @@ static void check_access(
 )
 {
   const rtems_filesystem_location_info_t *currentloc = &ctx->currentloc;
-  struct stat st;
-  int rv;
-
-  st.st_mode = 0;
-  st.st_uid = 0;
-  st.st_gid = 0;
-  rv = (*currentloc->handlers->fstat_h)(currentloc, &st);
-  if (rv == 0) {
-    bool access_ok = rtems_filesystem_check_access(
-      eval_flags,
-      st.st_mode,
-      st.st_uid,
-      st.st_gid
-    );
-
-    if (!access_ok) {
-      rtems_filesystem_eval_path_error(ctx, EACCES);
+  const rtems_filesystem_mount_table_entry_t *mt_entry = currentloc->mt_entry;
+
+  if ((eval_flags & RTEMS_LIBIO_PERMS_WRITE) == 0 || mt_entry->writeable) {
+    struct stat st;
+    int rv;
+
+    st.st_mode = 0;
+    st.st_uid = 0;
+    st.st_gid = 0;
+    rv = (*currentloc->handlers->fstat_h)(currentloc, &st);
+    if (rv == 0) {
+      bool access_ok = rtems_filesystem_check_access(
+        eval_flags,
+        st.st_mode,
+        st.st_uid,
+        st.st_gid
+      );
+
+      if (!access_ok) {
+        rtems_filesystem_eval_path_error(ctx, EACCES);
+      }
+    } else {
+      rtems_filesystem_eval_path_error(ctx, 0);
     }
   } else {
-    rtems_filesystem_eval_path_error(ctx, 0);
+    rtems_filesystem_eval_path_error(ctx, EROFS);
   }
 }
 
diff --git a/testsuites/fstests/fsrofs01/Makefile.am b/testsuites/fstests/fsrofs01/Makefile.am
new file mode 100644
index 0000000..729a629
--- /dev/null
+++ b/testsuites/fstests/fsrofs01/Makefile.am
@@ -0,0 +1,20 @@
+rtems_tests_PROGRAMS = fsrofs01
+fsrofs01_SOURCES = init.c
+
+dist_rtems_tests_DATA = fsrofs01.scn fsrofs01.doc
+
+include $(RTEMS_ROOT)/make/custom/@RTEMS_BSP at .cfg
+include $(top_srcdir)/../automake/compile.am
+include $(top_srcdir)/../automake/leaf.am
+
+
+AM_CPPFLAGS += -I$(top_srcdir)/../support/include
+
+LINK_OBJS = $(fsrofs01_OBJECTS)
+LINK_LIBS = $(fsrofs01_LDLIBS)
+
+fsrofs01$(EXEEXT): $(fsrofs01_OBJECTS) $(fsrofs01_DEPENDENCIES)
+	@rm -f fsrofs01$(EXEEXT)
+	$(make-exe)
+
+include $(top_srcdir)/../automake/local.am
diff --git a/testsuites/fstests/fsrofs01/fsrofs01.doc b/testsuites/fstests/fsrofs01/fsrofs01.doc
new file mode 100644
index 0000000..9a600ae
--- /dev/null
+++ b/testsuites/fstests/fsrofs01/fsrofs01.doc
@@ -0,0 +1,11 @@
+This file describes the directives and concepts tested by this test set.
+
+test set name: fsrofs01
+
+directives:
+
+  TBD
+
+concepts:
+
+  Make sure EROFS is indicated for write operations on a read-only file system.
diff --git a/testsuites/fstests/fsrofs01/fsrofs01.scn b/testsuites/fstests/fsrofs01/fsrofs01.scn
new file mode 100644
index 0000000..e19d9e5
--- /dev/null
+++ b/testsuites/fstests/fsrofs01/fsrofs01.scn
@@ -0,0 +1,2 @@
+*** TEST FSROFS 1 ***
+*** END OF TEST FSROFS 1 ***
diff --git a/testsuites/fstests/fsrofs01/init.c b/testsuites/fstests/fsrofs01/init.c
new file mode 100644
index 0000000..9bea115
--- /dev/null
+++ b/testsuites/fstests/fsrofs01/init.c
@@ -0,0 +1,188 @@
+/*
+ * Copyright (c) 2012 embedded brains GmbH.  All rights reserved.
+ *
+ *  embedded brains GmbH
+ *  Obere Lagerstr. 30
+ *  82178 Puchheim
+ *  Germany
+ *  <rtems at embedded-brains.de>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ */
+
+#ifdef HAVE_CONFIG_H
+  #include "config.h"
+#endif
+
+#include "tmacros.h"
+
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdio.h>
+
+#include <rtems/libio.h>
+#include <rtems/rtems-rfs-format.h>
+#include <rtems/ramdisk.h>
+
+static const rtems_rfs_format_config rfs_config;
+
+static const char rda [] = "/dev/rda";
+
+static const char mnt [] = "/mnt";
+
+static const char file [] = "/mnt/file";
+
+static const char not_exist [] = "/mnt/not_exist";
+
+static void test_mount(bool writeable)
+{
+  int rv;
+  const void *data = NULL;
+
+  rv = mount(
+    rda,
+    mnt,
+    RTEMS_FILESYSTEM_TYPE_RFS,
+    writeable ? RTEMS_FILESYSTEM_READ_WRITE : 0,
+    data
+  );
+  rtems_test_assert(rv == 0);
+}
+
+static void test_create_file_system(void)
+{
+  int rv;
+
+  rv = mkdir(mnt, S_IRWXU | S_IRWXG | S_IRWXO);
+  rtems_test_assert(rv == 0);
+
+  rv = rtems_rfs_format(rda, &rfs_config);
+  rtems_test_assert(rv == 0);
+
+  test_mount(true);
+
+  rv = mknod(file, S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO, 0);
+  rtems_test_assert(rv == 0);
+
+  rv = unmount(mnt);
+  rtems_test_assert(rv == 0);
+}
+
+static void test_rofs(void)
+{
+  int rv;
+  int fd;
+  char buf [1];
+  ssize_t n;
+
+  test_mount(false);
+
+  fd = open(file, O_RDONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
+  rtems_test_assert(fd >= 0);
+
+  n = read(fd, buf, sizeof(buf));
+  rtems_test_assert(n == 0);
+
+  errno = 0;
+  n = write(fd, buf, sizeof(buf));
+  rtems_test_assert(n == -1);
+  rtems_test_assert(errno == EBADF);
+
+  errno = 0;
+  rv = ftruncate(fd, 0);
+  rtems_test_assert(rv == -1);
+  rtems_test_assert(errno == EINVAL);
+
+  errno = 0;
+  rv = fchmod(fd, 0);
+  rtems_test_assert(rv == -1);
+  rtems_test_assert(errno == EROFS);
+
+  errno = 0;
+  rv = fchown(fd, 0, 0);
+  rtems_test_assert(rv == -1);
+  rtems_test_assert(errno == EROFS);
+
+  rv = close(fd);
+  rtems_test_assert(rv == 0);
+
+  errno = 0;
+  fd = open(not_exist, O_RDONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
+  rtems_test_assert(fd == -1);
+  rtems_test_assert(errno == EROFS);
+
+  errno = 0;
+  rv = mknod(not_exist, S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO, 0);
+  rtems_test_assert(rv == -1);
+  rtems_test_assert(errno == EROFS);
+
+  errno = 0;
+  rv = mkdir(not_exist, S_IRWXU | S_IRWXG | S_IRWXO);
+  rtems_test_assert(rv == -1);
+  rtems_test_assert(errno == EROFS);
+
+  errno = 0;
+  rv = rename(file, not_exist);
+  rtems_test_assert(rv == -1);
+  rtems_test_assert(errno == EROFS);
+
+  errno = 0;
+  rv = link(file, not_exist);
+  rtems_test_assert(rv == -1);
+  rtems_test_assert(errno == EROFS);
+
+  errno = 0;
+  rv = unlink(file);
+  rtems_test_assert(rv == -1);
+  rtems_test_assert(errno == EROFS);
+
+  errno = 0;
+  rv = symlink(file, not_exist);
+  rtems_test_assert(rv == -1);
+  rtems_test_assert(errno == EROFS);
+
+  rv = unmount(mnt);
+  rtems_test_assert(rv == 0);
+}
+
+static void Init(rtems_task_argument arg)
+{
+  printf("\n\n*** TEST FSROFS 1 ***\n");
+
+  test_create_file_system();
+  test_rofs();
+
+  printf("*** END OF TEST FSROFS 1 ***\n");
+
+  rtems_test_exit(0);
+}
+
+rtems_ramdisk_config rtems_ramdisk_configuration [] = {
+  { .block_size = 512, .block_num = 1024 }
+};
+
+size_t rtems_ramdisk_configuration_size = 1;
+
+#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
+#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
+#define CONFIGURE_APPLICATION_EXTRA_DRIVERS RAMDISK_DRIVER_TABLE_ENTRY
+#define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
+
+#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 5
+
+#define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM
+#define CONFIGURE_FILESYSTEM_RFS
+
+#define CONFIGURE_MAXIMUM_TASKS 2
+
+#define CONFIGURE_EXTRA_TASK_STACKS (8 * 1024)
+
+#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
+
+#define CONFIGURE_INIT
+
+#include <rtems/confdefs.h>
diff --git a/testsuites/psxtests/psxfchx01/init.c b/testsuites/psxtests/psxfchx01/init.c
index 7ef030e..56ff3c5 100644
--- a/testsuites/psxtests/psxfchx01/init.c
+++ b/testsuites/psxtests/psxfchx01/init.c
@@ -184,10 +184,9 @@ rtems_task Init(
   fd = open( "/newfile", O_RDONLY | O_CREAT, S_IRWXU );
   rtems_test_assert( fd != -1 );
   
-  puts( "Init - fchown, with the opened file descriptor - expect EINVAL" );
+  puts( "Init - fchown, with the opened file descriptor - OK" );
   status = fchown( fd, 0, 0 );
-  rtems_test_assert( status == -1 );
-  rtems_test_assert( errno == EINVAL );
+  rtems_test_assert( status == 0 );
 
   puts( "Init - close and remove /newfile" );
   status = close( fd );
diff --git a/testsuites/psxtests/psxmount/test.c b/testsuites/psxtests/psxmount/test.c
index 1e80376..6a113c7 100644
--- a/testsuites/psxtests/psxmount/test.c
+++ b/testsuites/psxtests/psxmount/test.c
@@ -242,18 +242,41 @@ int main(
    * Create a directory that passes through the read only file system.
    */
 
-  printf("create c/y/my_mount_point/../../y/my_mount_point/new_dir\n");
-  status = mkdir("c/y/my_mount_point/../../y/my_mount_point/new_dir",S_IRWXU );
+  printf("create c/y/my_mount_point/../../y/new_dir\n");
+  status = mkdir("c/y/my_mount_point/../../y/new_dir",S_IRWXU );
   rtems_test_assert( status == 0 );
-  status = stat("c/y/my_mount_point/../../y/my_mount_point/new_dir",&statbuf );
+  status = stat("c/y/my_mount_point/../../y/new_dir",&statbuf );
   rtems_test_assert( status == 0 );
-  status = stat("c/y/my_mount_point/new_dir/..", &statbuf );
+  status = stat("c/y/new_dir", &statbuf );
   rtems_test_assert( status == 0 );
 
   /*
    * Attempt to mount a second file system at a used mount point.
    */
 
+  printf("Verify a mount point returns EROFS for another mount\n");
+  status = mount(
+    "null",
+    "/c/y/my_mount_point",
+    "imfs",
+     RTEMS_FILESYSTEM_READ_ONLY,
+     NULL );
+  rtems_test_assert( status == -1 );
+  rtems_test_assert( errno == EROFS);
+
+  printf("Unmount /c/y/my_mount_point\n");
+  status = unmount( "/c/y/my_mount_point" );
+  rtems_test_assert( status == 0 );
+
+  printf("Mount a read-write file system at /c/y/my_mount_point\n");
+  status = mount(
+    "null",
+    "/c/y/my_mount_point",
+    "imfs",
+    RTEMS_FILESYSTEM_READ_WRITE,
+    NULL );
+  rtems_test_assert( status == 0 );
+
   printf("Verify a mount point returns EBUSY for another mount\n");
   status = mount(
     "null",
@@ -330,7 +353,7 @@ int main(
     "null",
     "/c/y/my_mount_point",
     "imfs",
-    RTEMS_FILESYSTEM_READ_ONLY,
+    RTEMS_FILESYSTEM_READ_WRITE,
     NULL );
   rtems_test_assert( status == 0 );
 




More information about the vc mailing list