From b4e3fcbc4d2d3e18adce064d71cf3defded2d4a8 Mon Sep 17 00:00:00 2001 From: Ahreum Jeong Date: Wed, 30 Aug 2017 12:31:00 +0900 Subject: [PATCH] Fix svace issues in mtd and testcases for filesystem. fs_main.c - The handle 'dirp' was created at fs_main.c:629 by calling function 'opendir' and lost at fs_main.c:631. - The handle 'ret' was created at fs_main.c:392 by calling function 'dup' and lost at fs_main.c:393. - Handler 'fd' is passed to a function at fs_main.c:489 by calling function 'close' after the handler is closed again at fs_main.c:485 by calling function 'close'. - The handle 'fd1' was created at fs_main.c:447 by calling function 'dup2' and lost at fs_main.c:449. - The handle 'dirp' was created at fs_main.c:818 by calling function 'opendir' and lost at fs_main.c:819. mtd_partition.c - Use of vulerable function 'strcpy' at mtd_partition.c:572. This function is unsafe, use strncpy instead. --- apps/examples/testcase/le_tc/filesystem/fs_main.c | 16 +++++++++------- os/fs/driver/mtd/mtd_partition.c | 8 ++++++-- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/apps/examples/testcase/le_tc/filesystem/fs_main.c b/apps/examples/testcase/le_tc/filesystem/fs_main.c index 93cfe78..2e7645a 100644 --- a/apps/examples/testcase/le_tc/filesystem/fs_main.c +++ b/apps/examples/testcase/le_tc/filesystem/fs_main.c @@ -384,13 +384,13 @@ static void fs_vfs_dup_tc(void) /* Nagative case with invalid argument, invalid fd. It will return ERROR */ #if CONFIG_NFILE_DESCRIPTORS > 0 - ret = dup(CONFIG_NFILE_DESCRIPTORS); - TC_ASSERT_EQ("dup", ret, ERROR); + fd1 = dup(CONFIG_NFILE_DESCRIPTORS); + TC_ASSERT_LT_CLEANUP("dup", fd1, 0, close(fd1)); #endif #if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0 - ret = dup(CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS); - TC_ASSERT_EQ("dup", ret, ERROR); + fd1 = dup(CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS); + TC_ASSERT_LT_CLEANUP("dup", fd1, 0, close(fd1)); #endif TC_SUCCESS_RESULT(); @@ -445,7 +445,8 @@ static void fs_vfs_dup2_tc(void) /* Nagative case with invalid argument, invalid fd. It will return ERROR */ fd1 = -1; ret = dup2(CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS, fd1); - TC_ASSERT_LT_CLEANUP("dup2", fd1, 0, close(fd1)); + close(fd1); + TC_ASSERT_LT("dup2", fd1, 0); TC_ASSERT_EQ("dup2", ret, ERROR); TC_SUCCESS_RESULT(); @@ -486,7 +487,7 @@ static void fs_vfs_fsync_tc(void) /* Nagative case with invalid argument, fd. It will return ERROR */ ret = fsync(CONFIG_NFILE_DESCRIPTORS); - TC_ASSERT_EQ_CLEANUP("fsync", ret, ERROR, close(fd)); + TC_ASSERT_EQ("fsync", ret, ERROR); TC_SUCCESS_RESULT(); } @@ -628,6 +629,7 @@ static void fs_vfs_opendir_tc(void) dirp = opendir(VFS_FOLDER_PATH); TC_ASSERT("opendir", dirp); + closedir(dirp); TC_SUCCESS_RESULT(); } @@ -816,7 +818,7 @@ static void fs_vfs_closedir_tc(void) TC_ASSERT_EQ("closedir", ret, OK); dirp = opendir("nodir"); - TC_ASSERT_EQ("opendir", dirp, NULL); + TC_ASSERT_EQ_CLEANUP("opendir", dirp, NULL, closedir(dirp)); ret = closedir(NULL); TC_ASSERT_EQ("closedir", ret, ERROR); diff --git a/os/fs/driver/mtd/mtd_partition.c b/os/fs/driver/mtd/mtd_partition.c index 9206d91..4d6086b 100644 --- a/os/fs/driver/mtd/mtd_partition.c +++ b/os/fs/driver/mtd/mtd_partition.c @@ -120,6 +120,10 @@ struct part_procfs_file_s { }; #endif +#ifdef CONFIG_MTD_PARTITION_NAMES +#define MTD_PARTNAME_LEN 11 +#endif + /**************************************************************************** * Private Function Prototypes ****************************************************************************/ @@ -519,7 +523,7 @@ static ssize_t part_procfs_read(FAR struct file *filep, FAR char *buffer, size_t ssize_t blkpererase; ssize_t ret; #ifdef CONFIG_MTD_PARTITION_NAMES - char partname[11]; + char partname[MTD_PARTNAME_LEN]; FAR const char *ptr; uint8_t x; #endif @@ -569,7 +573,7 @@ static ssize_t part_procfs_read(FAR struct file *filep, FAR char *buffer, size_t #ifdef CONFIG_MTD_PARTITION_NAMES if (attr->nextpart->name == NULL) { - strcpy(partname, "(noname) "); + strncpy(partname, "(noname) ", MTD_PARTNAME_LEN); } else { ptr = attr->nextpart->name; for (x = 0; x < sizeof(partname) - 1; x++) { -- 2.7.4