From: Charles Manning Date: Thu, 8 Dec 2022 21:27:12 +0000 (+1300) Subject: yaffsfs.c: Fix NULL dereference in yaffs_unmount2_reldev() X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs2.git;a=commitdiff_plain;h=HEAD;hp=5758b7a95a391ac8e4d631fbea9fd409d1130a62 yaffsfs.c: Fix NULL dereference in yaffs_unmount2_reldev() Signed-off-by: Charles Manning --- diff --git a/direct/test-framework/basic-tests/Makefile b/direct/test-framework/basic-tests/Makefile index 3535381..7d219ef 100644 --- a/direct/test-framework/basic-tests/Makefile +++ b/direct/test-framework/basic-tests/Makefile @@ -21,16 +21,20 @@ 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 index 0000000..0661be6 --- /dev/null +++ b/direct/test-framework/basic-tests/reldevtest.c @@ -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 + * + * 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 +#include +#include +#include +#include +#include + + +#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; +} diff --git a/direct/test-framework/unit_tests/64_and_32_bit_time/32_bit/Makefile b/direct/test-framework/unit_tests/64_and_32_bit_time/32_bit/Makefile index 7c12c25..8842f4b 100644 --- a/direct/test-framework/unit_tests/64_and_32_bit_time/32_bit/Makefile +++ b/direct/test-framework/unit_tests/64_and_32_bit_time/32_bit/Makefile @@ -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. @@ -18,22 +18,24 @@ 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 index 0000000..d73d45a --- /dev/null +++ b/direct/test-framework/unit_tests/64_and_32_bit_time/32_bit/create_32_bit.c @@ -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 index 0000000..e4b04bb --- /dev/null +++ b/direct/test-framework/unit_tests/64_and_32_bit_time/32_bit/shared.c @@ -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 index 0000000..ae87f9e --- /dev/null +++ b/direct/test-framework/unit_tests/64_and_32_bit_time/32_bit/shared.h @@ -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 index 0000000..c08acf3 --- /dev/null +++ b/direct/test-framework/unit_tests/64_and_32_bit_time/32_bit/validate_32_bit.c @@ -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/64_bit/create_64_bit.c b/direct/test-framework/unit_tests/64_and_32_bit_time/64_bit/create_64_bit.c deleted file mode 100644 index 85c29bd..0000000 --- a/direct/test-framework/unit_tests/64_and_32_bit_time/64_bit/create_64_bit.c +++ /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; -} diff --git a/direct/test-framework/unit_tests/64_and_32_bit_time/64_bit/create_64_bit.c b/direct/test-framework/unit_tests/64_and_32_bit_time/64_bit/create_64_bit.c new file mode 120000 index 0000000..d73d45a --- /dev/null +++ b/direct/test-framework/unit_tests/64_and_32_bit_time/64_bit/create_64_bit.c @@ -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/64_bit/validate_64_bit.c b/direct/test-framework/unit_tests/64_and_32_bit_time/64_bit/validate_64_bit.c deleted file mode 100644 index df1fcdb..0000000 --- a/direct/test-framework/unit_tests/64_and_32_bit_time/64_bit/validate_64_bit.c +++ /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); -} diff --git a/direct/test-framework/unit_tests/64_and_32_bit_time/64_bit/validate_64_bit.c b/direct/test-framework/unit_tests/64_and_32_bit_time/64_bit/validate_64_bit.c new file mode 120000 index 0000000..c08acf3 --- /dev/null +++ b/direct/test-framework/unit_tests/64_and_32_bit_time/64_bit/validate_64_bit.c @@ -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 index 0000000..85c29bd --- /dev/null +++ b/direct/test-framework/unit_tests/64_and_32_bit_time/shared/create_file.c @@ -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; +} diff --git a/direct/test-framework/unit_tests/64_and_32_bit_time/shared/shared.c b/direct/test-framework/unit_tests/64_and_32_bit_time/shared/shared.c index 0e0dd8b..31672f2 100644 --- a/direct/test-framework/unit_tests/64_and_32_bit_time/shared/shared.c +++ b/direct/test-framework/unit_tests/64_and_32_bit_time/shared/shared.c @@ -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 index 0000000..df1fcdb --- /dev/null +++ b/direct/test-framework/unit_tests/64_and_32_bit_time/shared/validate_file.c @@ -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 index 0000000..74d1cec --- /dev/null +++ b/direct/test-framework/unit_tests/64_and_32_bit_time/time_test_runner.sh @@ -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 diff --git a/direct/yaffsfs.c b/direct/yaffsfs.c index 2c1ac42..823f7ed 100644 --- a/direct/yaffsfs.c +++ b/direct/yaffsfs.c @@ -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();