yaffsfs.c: Fix NULL dereference in yaffs_unmount2_reldev() master
authorCharles Manning <cdhmanning@gmail.com>
Thu, 8 Dec 2022 21:27:12 +0000 (10:27 +1300)
committerCharles Manning <cdhmanning@gmail.com>
Thu, 8 Dec 2022 21:27:12 +0000 (10:27 +1300)
Signed-off-by: Charles Manning <cdhmanning@gmail.com>
14 files changed:
direct/test-framework/basic-tests/Makefile
direct/test-framework/basic-tests/reldevtest.c [new file with mode: 0644]
direct/test-framework/unit_tests/64_and_32_bit_time/32_bit/Makefile
direct/test-framework/unit_tests/64_and_32_bit_time/32_bit/create_32_bit.c [new symlink]
direct/test-framework/unit_tests/64_and_32_bit_time/32_bit/shared.c [new symlink]
direct/test-framework/unit_tests/64_and_32_bit_time/32_bit/shared.h [new symlink]
direct/test-framework/unit_tests/64_and_32_bit_time/32_bit/validate_32_bit.c [new symlink]
direct/test-framework/unit_tests/64_and_32_bit_time/64_bit/create_64_bit.c [changed from file to symlink]
direct/test-framework/unit_tests/64_and_32_bit_time/64_bit/validate_64_bit.c [changed from file to symlink]
direct/test-framework/unit_tests/64_and_32_bit_time/shared/create_file.c [new file with mode: 0644]
direct/test-framework/unit_tests/64_and_32_bit_time/shared/shared.c
direct/test-framework/unit_tests/64_and_32_bit_time/shared/validate_file.c [new file with mode: 0644]
direct/test-framework/unit_tests/64_and_32_bit_time/time_test_runner.sh [new file with mode: 0755]
direct/yaffsfs.c

index 35353811a4a61583caddc4b0ffbf40400a722e7e..7d219ef5a319c53ab385baf26906b1e75f3c697a 100644 (file)
 YDI_DIR = ../../
 YDI_FRAMEWORK_DIR = ../
 
-TARGETS = directtest2k
+TARGETS = directtest2k reldevtest
 
 all: $(TARGETS)
 
 DIRECTTESTOBJS = $(COMMONTESTOBJS) dtest.o
+RELDEVOBJS =  $(COMMONTESTOBJS) reldevtest.o
 
-ALL_UNSORTED_OBJS += $(DIRECTTESTOBJS)
+ALL_UNSORTED_OBJS += $(DIRECTTESTOBJS) $(RELDEVOBJS)
 
 include ../FrameworkRules.mk
 
-directtest2k: $(FRAMEWORK_SOURCES) $(DIRECTTESTOBJS)
+directtest2k: $(FRAMEWORK_SOURCES) $(DIRECTTESTOBJS) Makefile
        gcc -o $@ $(DIRECTTESTOBJS) -lpthread
 
+reldevtest: $(FRAMEWORK_SOURCES) $(RELDEVOBJS) Makefile
+       gcc -o $@ $(RELDEVOBJS) -lpthread
+
diff --git a/direct/test-framework/basic-tests/reldevtest.c b/direct/test-framework/basic-tests/reldevtest.c
new file mode 100644 (file)
index 0000000..0661be6
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ * YAFFS: Yet another FFS. A NAND-flash specific file system.
+ *
+ * Copyright (C) 2002-2018 Aleph One Ltd.
+ *
+ * Created by Charles Manning <charles@aleph1.co.uk>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <time.h>
+#include <ctype.h>
+
+
+#include "yaffsfs.h"
+
+#include "yaffs_guts.h" /* Only for dumping device innards */
+#include "yaffs_endian.h" /*For testing the swap_u64 macro */
+
+extern int yaffs_trace_mask;
+
+
+
+int call_all_reldev(struct yaffs_dev *dev)
+{
+       struct yaffs_stat buf;
+       struct yaffs_utimbuf utime;
+       unsigned char xbuffer[20];
+       char cbuffer[20];
+
+       yaffs_mount_reldev(dev);
+       yaffs_open_sharing_reldev(dev, "foo", 0, 0, 0);
+       yaffs_open_reldev(dev, "foo", 0, 0);
+       yaffs_truncate_reldev(dev, "foo", 99);
+       yaffs_unlink_reldev(dev, "foo");
+       yaffs_rename_reldev(dev, "foo", "foo_new");
+       yaffs_stat_reldev(dev, "foo", &buf);
+       yaffs_lstat_reldev(dev, "foo", &buf);
+       yaffs_utime_reldev(dev, "foo", &utime);
+       yaffs_setxattr_reldev(dev, "foo", "name", xbuffer, 20, 0);
+       yaffs_lsetxattr_reldev(dev, "foo", "name", xbuffer, 20, 0);
+       yaffs_getxattr_reldev(dev, "foo", "name", xbuffer, 20);
+       yaffs_lgetxattr_reldev(dev, "foo", "name", xbuffer, 20);
+
+       yaffs_listxattr_reldev(dev, "foo", cbuffer, 20);
+       yaffs_llistxattr_reldev(dev, "foo", cbuffer, 20);
+       yaffs_removexattr_reldev(dev, "foo", "name");
+       yaffs_lremovexattr_reldev(dev, "foo", "name");
+
+       yaffs_access_reldev(dev, "foo", 0);
+       yaffs_chmod_reldev(dev, "foo", 0);
+       yaffs_mkdir_reldev(dev, "foo", 0);
+       yaffs_rmdir_reldev(dev, "foo");
+
+
+       yaffs_opendir_reldev(dev, "foo");
+
+       //yaffs_symlink_reldev(dev, "foo", "foolink");
+       //yaffs_readlink_reldev(dev, "foo", cbuffer, 20);
+       //yaffs_link_reldev(dev, "foo", "foo_new");
+
+       yaffs_mknod_reldev(dev, "foo", 0, 0);
+       yaffs_freespace_reldev(dev);
+       yaffs_totalspace_reldev(dev);
+
+       yaffs_sync_reldev(dev);
+       yaffs_sync_files_reldev(dev);
+
+       yaffs_unmount_reldev(dev);
+       yaffs_unmount2_reldev(dev, 1);
+       yaffs_remount_reldev(dev, 1, 1);
+
+       return 0;
+}
+
+
+int random_seed;
+int simulate_power_failure;
+
+int main(int argc, char *argv[])
+{
+       (void) argc;
+       (void) argv;
+
+       call_all_reldev(NULL);
+
+       return 0;
+}
index 7c12c25bb3a3392bc5c8308aaea54de80a09db18..8842f4b1d7b472e3644ce16afa7a72d1586eb0ff 100644 (file)
@@ -1,4 +1,4 @@
-# Makefile for 32 bit time test.
+# Makefile for 64 bit time test.
 #
 #
 # YAFFS: Yet another Flash File System. A NAND-flash specific file system.
 YDI_DIR = ../../../../
 YDI_FRAMEWORK_DIR = ../../../
 
-CLEAN_OBJS = time_32_tests emfile-2k-0 emfile-nand  emfile-nand128MB 
-TESTFILES =    time_32_tests.o
+MAINFILES = time_32_tests create_32_bit validate_32_bit
+MAIN_OBJS = $(addsuffix .o,$(MAINFILES))
 
-CFLAGS = -DCONFIG_YAFFS_USE_32_BIT_TIME_T
-all: time_32_tests
+EXTRA_OBJS = shared.o
 
-YAFFS_TEST_OBJS = $(COMMONTESTOBJS) $(TESTFILES)
+CLEAN_OBJS = $(MAINFILES) 
+CFLAGS = -DCONFIG_YAFFS_USE_32_BIT_TIME_T
 
+YAFFS_TEST_OBJS = $(COMMONTESTOBJS) $(EXTRA_OBJS)
+ALL_UNSORTED_OBJS += $(YAFFS_TEST_OBJS) $(MAIN_OBJS)
 
-ALL_UNSORTED_OBJS += $(YAFFS_TEST_OBJS) $(FUZZER_OBJS)
+all: $(MAINFILES) 
 
 include $(YDI_FRAMEWORK_DIR)/FrameworkRules.mk
 
 phony. test: time_32_tests
        ./time_32_tests
 
-time_32_tests: $(FRAMEWORK_SOURCES) $(YAFFS_TEST_OBJS)
-       gcc $(CFLAGS) -o $@ $(YAFFS_TEST_OBJS) -lpthread
+$(MAINFILES): $(FRAMEWORK_SOURCES) $(YAFFS_TEST_OBJS) $(MAIN_OBJS) 
+       gcc $(CFLAGS) -o $@ $(YAFFS_TEST_OBJS) $@.o -lpthread -DCONFIG_YAFFS_USE_32_BIT_TIME_T
 
diff --git a/direct/test-framework/unit_tests/64_and_32_bit_time/32_bit/create_32_bit.c b/direct/test-framework/unit_tests/64_and_32_bit_time/32_bit/create_32_bit.c
new file mode 120000 (symlink)
index 0000000..d73d45a
--- /dev/null
@@ -0,0 +1 @@
+../shared/create_file.c
\ No newline at end of file
diff --git a/direct/test-framework/unit_tests/64_and_32_bit_time/32_bit/shared.c b/direct/test-framework/unit_tests/64_and_32_bit_time/32_bit/shared.c
new file mode 120000 (symlink)
index 0000000..e4b04bb
--- /dev/null
@@ -0,0 +1 @@
+../shared/shared.c
\ No newline at end of file
diff --git a/direct/test-framework/unit_tests/64_and_32_bit_time/32_bit/shared.h b/direct/test-framework/unit_tests/64_and_32_bit_time/32_bit/shared.h
new file mode 120000 (symlink)
index 0000000..ae87f9e
--- /dev/null
@@ -0,0 +1 @@
+../shared/shared.h
\ No newline at end of file
diff --git a/direct/test-framework/unit_tests/64_and_32_bit_time/32_bit/validate_32_bit.c b/direct/test-framework/unit_tests/64_and_32_bit_time/32_bit/validate_32_bit.c
new file mode 120000 (symlink)
index 0000000..c08acf3
--- /dev/null
@@ -0,0 +1 @@
+../shared/validate_file.c
\ No newline at end of file
deleted file mode 100644 (file)
index 85c29bd2acef6699519147122784f02171377bea..0000000000000000000000000000000000000000
+++ /dev/null
@@ -1,10 +0,0 @@
-#include "shared.h"
-
-int random_seed;
-int simulate_power_failure = 0;
-
-int main(int argc, char *argv[] ){
-       int ret = shared_create(argc, argv);
-       ret = ret && shared_validate_file(argc, argv);
-       return ret;
-}
new file mode 120000 (symlink)
index 0000000000000000000000000000000000000000..d73d45a2a63ccf9aa040bb40c26c33130aa9ae48
--- /dev/null
@@ -0,0 +1 @@
+../shared/create_file.c
\ No newline at end of file
deleted file mode 100644 (file)
index df1fcdb79371d3de1cd6bedaf53e2d9c446e5c3f..0000000000000000000000000000000000000000
+++ /dev/null
@@ -1,8 +0,0 @@
-#include "shared.h"
-
-int random_seed;
-int simulate_power_failure = 0;
-
-int main(int argc, char *argv[] ){
-       return shared_validate_file(argc, argv);
-}
new file mode 120000 (symlink)
index 0000000000000000000000000000000000000000..c08acf3d36af363920cd52c08eea2450797d80c5
--- /dev/null
@@ -0,0 +1 @@
+../shared/validate_file.c
\ No newline at end of file
diff --git a/direct/test-framework/unit_tests/64_and_32_bit_time/shared/create_file.c b/direct/test-framework/unit_tests/64_and_32_bit_time/shared/create_file.c
new file mode 100644 (file)
index 0000000..85c29bd
--- /dev/null
@@ -0,0 +1,10 @@
+#include "shared.h"
+
+int random_seed;
+int simulate_power_failure = 0;
+
+int main(int argc, char *argv[] ){
+       int ret = shared_create(argc, argv);
+       ret = ret && shared_validate_file(argc, argv);
+       return ret;
+}
index 0e0dd8b91b48a490d5cbbe72ab0783e3f2f6d4e0..31672f2403395216e2f04db36594f24ce85a9585 100644 (file)
@@ -21,7 +21,12 @@ void setup_yaffs() {
 
 
 int shared_create(int argc, char *argv[]){
-       
+       printf("YTIME_T size is %lu bits\n", sizeof(YTIME_T) *8);
+       #ifdef CONFIG_YAFFS_USE_32_BIT_TIME_T
+               if (sizeof(YTIME_T)*8 != 32) {
+                       printf("YTIME_T size is incorrect. it is %lu and should be 32\n", sizeof(YTIME_T));
+               } 
+       #endif
        if (argc != 3) {
                printf("wrong number of arguments\n");
                printf("requires $ create file_name time\n");
@@ -45,7 +50,7 @@ int shared_create(int argc, char *argv[]){
        assert_exit_yaffs("yaffs_utime", ret);
 
        assert_exit_yaffs("yaffs_unmount", yaffs_unmount(YAFFS_MOUNT_POINT));
-       return TEST_FAIL;
+       return 0;
 }
 
 int shared_validate_file(int argc, char *argv[]){
@@ -71,5 +76,5 @@ int shared_validate_file(int argc, char *argv[]){
                exit(0);
        }
        assert_exit_yaffs("yaffs_unmount", yaffs_unmount(YAFFS_MOUNT_POINT));
-       return TEST_FAIL;
+       return 0;
 }
diff --git a/direct/test-framework/unit_tests/64_and_32_bit_time/shared/validate_file.c b/direct/test-framework/unit_tests/64_and_32_bit_time/shared/validate_file.c
new file mode 100644 (file)
index 0000000..df1fcdb
--- /dev/null
@@ -0,0 +1,8 @@
+#include "shared.h"
+
+int random_seed;
+int simulate_power_failure = 0;
+
+int main(int argc, char *argv[] ){
+       return shared_validate_file(argc, argv);
+}
diff --git a/direct/test-framework/unit_tests/64_and_32_bit_time/time_test_runner.sh b/direct/test-framework/unit_tests/64_and_32_bit_time/time_test_runner.sh
new file mode 100755 (executable)
index 0000000..74d1cec
--- /dev/null
@@ -0,0 +1,14 @@
+make -C  32_bit/ && make -C 64_bit/ &&  
+       rm emfile-* && 
+       ./32_bit/create_32_bit /yflash2/foo 100 && 
+       echo created 32 bit file &&
+       ./64_bit/validate_64_bit /yflash2/foo 100 &&
+       echo 32 bit file opened with 64 bit mode correctly
+
+       rm emfile-* && 
+       ./64_bit/create_64_bit /yflash2/foo 100 && 
+       echo created 64 bit file &&
+       ./32_bit/validate_32_bit /yflash2/foo 100 &&
+       echo 64 bit file opened with 32 bit mode correctly &&
+
+       echo All tests ran properly
index 2c1ac42eba1c8cefad544e6c8f7d79d37d96674e..823f7edcfc21c9dd7e2247e1da8781e8a74eb7e1 100644 (file)
@@ -3110,14 +3110,17 @@ int yaffs_unmount2_common(struct yaffs_dev *dev, const YCHAR *path, int force)
 {
        int retVal = -1;
 
-       if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
-               yaffsfs_SetError(-EFAULT);
-               return -1;
-       }
 
-       if (yaffsfs_CheckPath(path) < 0) {
-               yaffsfs_SetError(-ENAMETOOLONG);
-               return -1;
+       if (!dev) {
+               if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
+                       yaffsfs_SetError(-EFAULT);
+                       return -1;
+               }
+
+               if (yaffsfs_CheckPath(path) < 0) {
+                       yaffsfs_SetError(-ENAMETOOLONG);
+                       return -1;
+               }
        }
 
        yaffsfs_Lock();