From 101a55d1199530d621f809976f0024aa8295cce8 Mon Sep 17 00:00:00 2001 From: chenwei Date: Mon, 7 Jun 2021 14:12:47 +0800 Subject: [PATCH] fix: codex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1,VFS代码中不修改参数增加const修饰 2,fs_file_mapping.c: 增加安全函数的判空 3,path_cache.c: sizeof改为使用类型 4,fs_syscall.c: 对NULL解引用 5,VnodeLookup:冗余的判空,及不正确的判空 close: I3UMWD Signed-off-by: yansira --- fs/fat/os_adapt/fatfs.c | 4 ++-- fs/fat/os_adapt/fatfs.h | 4 ++-- fs/include/fs/vnode.h | 4 ++-- fs/jffs2/src/vfs_jffs2.c | 4 ++-- fs/vfs/operation/fs_file_mapping.c | 22 +++++++++++++++++++--- fs/vfs/path_cache.c | 2 +- fs/vfs/vnode.c | 9 +++------ syscall/fs_syscall.c | 19 +++++++++---------- 8 files changed, 40 insertions(+), 28 deletions(-) diff --git a/fs/fat/os_adapt/fatfs.c b/fs/fat/os_adapt/fatfs.c index abb31580..e5d9df2d 100644 --- a/fs/fat/os_adapt/fatfs.c +++ b/fs/fat/os_adapt/fatfs.c @@ -1836,7 +1836,7 @@ ERROR_OUT: return -fatfs_2_vfs(result); } -int fatfs_rmdir(struct Vnode *parent, struct Vnode *vp, char *name) +int fatfs_rmdir(struct Vnode *parent, struct Vnode *vp, const char *name) { FATFS *fs = (FATFS *)vp->originMount->data; DIR_FILE *dfp = (DIR_FILE *)vp->data; @@ -1898,7 +1898,7 @@ int fatfs_reclaim(struct Vnode *vp) return 0; } -int fatfs_unlink(struct Vnode *parent, struct Vnode *vp, char *name) +int fatfs_unlink(struct Vnode *parent, struct Vnode *vp, const char *name) { FATFS *fs = (FATFS *)vp->originMount->data; DIR_FILE *dfp = (DIR_FILE *)vp->data; diff --git a/fs/fat/os_adapt/fatfs.h b/fs/fat/os_adapt/fatfs.h index 5d329700..4c1f7821 100644 --- a/fs/fat/os_adapt/fatfs.h +++ b/fs/fat/os_adapt/fatfs.h @@ -131,8 +131,8 @@ int fatfs_closedir(struct Vnode *vnode, struct fs_dirent_s *dir); int fatfs_rename(struct Vnode *oldvnode, struct Vnode *newparent, const char *oldname, const char *newname); int fatfs_mkfs (struct Vnode *device, int sectors, int option); int fatfs_mkdir(struct Vnode *parent, const char *name, mode_t mode, struct Vnode **vpp); -int fatfs_rmdir(struct Vnode *parent, struct Vnode *vp, char *name); -int fatfs_unlink(struct Vnode *parent, struct Vnode *vp, char *name); +int fatfs_rmdir(struct Vnode *parent, struct Vnode *vp, const char *name); +int fatfs_unlink(struct Vnode *parent, struct Vnode *vp, const char *name); int fatfs_ioctl(struct file *filep, int req, unsigned long arg); int fatfs_fscheck(struct Vnode* vnode, struct fs_dirent_s *dir); diff --git a/fs/include/fs/vnode.h b/fs/include/fs/vnode.h index 0312b5ea..677d61cd 100644 --- a/fs/include/fs/vnode.h +++ b/fs/include/fs/vnode.h @@ -85,8 +85,8 @@ struct VnodeOps { int (*Open)(struct Vnode *vnode, int fd, int mode, int flags); int (*Close)(struct Vnode *vnode); int (*Reclaim)(struct Vnode *vnode); - int (*Unlink)(struct Vnode *parent, struct Vnode *vnode, char *fileName); - int (*Rmdir)(struct Vnode *parent, struct Vnode *vnode, char *dirName); + int (*Unlink)(struct Vnode *parent, struct Vnode *vnode, const char *fileName); + int (*Rmdir)(struct Vnode *parent, struct Vnode *vnode, const char *dirName); int (*Mkdir)(struct Vnode *parent, const char *dirName, mode_t mode, struct Vnode **vnode); int (*Readdir)(struct Vnode *vnode, struct fs_dirent_s *dir); int (*Opendir)(struct Vnode *vnode, struct fs_dirent_s *dir); diff --git a/fs/jffs2/src/vfs_jffs2.c b/fs/jffs2/src/vfs_jffs2.c index 00602238..cb651a2b 100644 --- a/fs/jffs2/src/vfs_jffs2.c +++ b/fs/jffs2/src/vfs_jffs2.c @@ -603,7 +603,7 @@ int VfsJffs2Chattr(struct Vnode *pVnode, struct IATTR *attr) return ret; } -int VfsJffs2Rmdir(struct Vnode *parentVnode, struct Vnode *targetVnode, char *path) +int VfsJffs2Rmdir(struct Vnode *parentVnode, struct Vnode *targetVnode, const char *path) { int ret; @@ -620,7 +620,7 @@ int VfsJffs2Rmdir(struct Vnode *parentVnode, struct Vnode *targetVnode, char *pa return ret; } -int VfsJffs2Unlink(struct Vnode *parentVnode, struct Vnode *targetVnode, char *path) +int VfsJffs2Unlink(struct Vnode *parentVnode, struct Vnode *targetVnode, const char *path) { int ret; diff --git a/fs/vfs/operation/fs_file_mapping.c b/fs/vfs/operation/fs_file_mapping.c index c7377644..496b22ae 100644 --- a/fs/vfs/operation/fs_file_mapping.c +++ b/fs/vfs/operation/fs_file_mapping.c @@ -270,11 +270,27 @@ int update_file_path(const char *old_path, const char *new_path) continue; } int len = strlen(new_path) + 1; - filp->f_path = zalloc(len); - strncpy_s(filp->f_path, strlen(new_path) + 1, new_path, len); + char *tmp_path = LOS_MemAlloc(m_aucSysMem0, len); + if (tmp_path == NULL) { + PRINT_ERR("%s-%d: Mem alloc failed, path length(%d)\n", __FUNCTION__, __LINE__, len); + ret = VFS_ERROR; + goto out; + } + ret = strncpy_s(tmp_path, strlen(new_path) + 1, new_path, len); + if (ret != 0) { + (VOID)LOS_MemFree(m_aucSysMem0, tmp_path); + PRINT_ERR("%s-%d: strcpy failed.\n", __FUNCTION__, __LINE__); + ret = VFS_ERROR; + goto out; + } + free(filp->f_path); + filp->f_path = tmp_path; } + ret = LOS_OK; + +out: (VOID)LOS_MuxUnlock(&g_file_mapping.lock); (void)sem_post(&f_list->fl_sem); - return LOS_OK; + return ret; } #endif diff --git a/fs/vfs/path_cache.c b/fs/vfs/path_cache.c index 2d607e2c..c6d71644 100644 --- a/fs/vfs/path_cache.c +++ b/fs/vfs/path_cache.c @@ -82,7 +82,7 @@ static uint32_t NameHash(const char *name, int len, struct Vnode *dvp) { uint32_t hash; hash = fnv_32_buf(name, len, FNV1_32_INIT); - hash = fnv_32_buf(&dvp, sizeof(dvp), hash); + hash = fnv_32_buf(&dvp, sizeof(struct Vnode *), hash); return hash; } diff --git a/fs/vfs/vnode.c b/fs/vfs/vnode.c index b206cdf6..5bc90dcb 100644 --- a/fs/vfs/vnode.c +++ b/fs/vfs/vnode.c @@ -245,9 +245,6 @@ int VnodeDrop() static char *NextName(char *pos, uint8_t *len) { char *name = NULL; - if (*pos == '\0') { - return NULL; - } while (*pos != 0 && *pos == '/') { pos++; } @@ -319,6 +316,7 @@ static int Step(char **currentDir, struct Vnode **currentVnode, uint32_t flags) } nextDir = NextName(*currentDir, &len); if (nextDir == NULL) { + // there is '/' at the end of the *currentDir. *currentDir = NULL; return LOS_OK; } @@ -361,7 +359,6 @@ int VnodeLookup(const char *path, struct Vnode **result, uint32_t flags) struct Vnode *startVnode = NULL; char *normalizedPath = NULL; - int ret = PreProcess(path, &startVnode, &normalizedPath); if (ret != LOS_OK) { PRINT_ERR("[VFS]lookup failed, invalid path=%s err = %d\n", path, ret); @@ -377,9 +374,9 @@ int VnodeLookup(const char *path, struct Vnode **result, uint32_t flags) char *currentDir = normalizedPath; struct Vnode *currentVnode = startVnode; - while (currentDir && *currentDir != '\0') { + while (*currentDir != '\0') { ret = Step(¤tDir, ¤tVnode, flags); - if (*currentDir == '\0') { + if (currentDir == NULL || *currentDir == '\0') { // return target or parent vnode as result *result = currentVnode; } else if (VfsVnodePermissionCheck(currentVnode, EXEC_OP)) { diff --git a/syscall/fs_syscall.c b/syscall/fs_syscall.c index 2df23ff4..b869ee2a 100644 --- a/syscall/fs_syscall.c +++ b/syscall/fs_syscall.c @@ -270,13 +270,11 @@ int SysOpen(const char *path, int oflags, ...) mode_t mode = DEFAULT_FILE_MODE; /* 0666: File read-write properties. */ char *pathRet = NULL; - if (path == NULL && *path == 0) { - return -EINVAL; - } - - ret = UserPathCopy(path, &pathRet); - if (ret != 0) { - return ret; + if (path != NULL) { + ret = UserPathCopy(path, &pathRet); + if (ret != 0) { + goto ERROUT_PATH_FREE; + } } procFd = AllocProcessFd(); @@ -310,15 +308,16 @@ int SysOpen(const char *path, int oflags, ...) return procFd; ERROUT: - if (pathRet != NULL) { - LOS_MemFree(OS_SYS_MEM_ADDR, pathRet); - } if (ret >= 0) { AssociateSystemFd(procFd, ret); ret = procFd; } else { FreeProcessFd(procFd); } +ERROUT_PATH_FREE: + if (pathRet != NULL) { + LOS_MemFree(OS_SYS_MEM_ADDR, pathRet); + } return ret; }