Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/utest/configs/components/dfs.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ CONFIG_RT_UTEST_TC_USING_POSIX_API=y
CONFIG_RT_UTEST_DFS_MNT_PATH=""
CONFIG_RT_UTEST_DFS_FS_TYPE="elm"
CONFIG_RT_UTEST_DFS_BLOCK_DEV="sd"
CONFIG_RT_UTEST_TC_USING_DEVTMPFS_PATH=y
58 changes: 50 additions & 8 deletions components/dfs/dfs_v2/filesystems/devfs/devtmpfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,23 @@ struct devtmpfs_sb

static struct dfs_file_ops _default_fops = { 0 };

static int _path_separate(const char *path, char *parent_path, char *file_name)
static int _path_separate(const char *path, char *parent_path, rt_size_t parent_size,
char *file_name, rt_size_t file_size)
{
const char *path_p, *path_q;
rt_size_t parent_len, file_len;

/* validate the arguments before the path is dereferenced below */
if (path == RT_NULL || parent_path == RT_NULL || file_name == RT_NULL ||
parent_size < 2 || file_size == 0)
{
return -EINVAL;
}

RT_ASSERT(path[0] == '/');

file_name[0] = '\0';
parent_path[0] = '\0';
path_p = path_q = &path[1];
__next_dir:
while (*path_q != '/' && *path_q != '\0')
Expand All @@ -77,10 +87,17 @@ static int _path_separate(const char *path, char *parent_path, char *file_name)
}
else /* Last level dir */
{
rt_memcpy(parent_path, path, path_p - path - 1);
parent_path[path_p - path - 1] = '\0';
rt_memcpy(file_name, path_p, path_q - path_p);
file_name[path_q - path_p] = '\0';
parent_len = path_p - path - 1;
file_len = path_q - path_p;
if (parent_len >= parent_size || file_len >= file_size)
{
return -ENAMETOOLONG;
}

rt_memcpy(parent_path, path, parent_len);
parent_path[parent_len] = '\0';
rt_memcpy(file_name, path_p, file_len);
file_name[file_len] = '\0';
}
}
if (parent_path[0] == 0)
Expand All @@ -94,17 +111,32 @@ static int _path_separate(const char *path, char *parent_path, char *file_name)
return 0;
}

static int _get_subdir(const char *path, char *name)
static int _get_subdir(const char *path, char *name, rt_size_t name_size)
{
const char *subpath = path;
char *name_start = name;
rt_size_t name_len = 0;

if (path == RT_NULL || name == RT_NULL || name_size == 0)
{
return -EINVAL;
}

while (*subpath == '/' && *subpath)
subpath ++;
while (*subpath != '/' && *subpath)
{
if (name_len + 1 >= name_size)
{
*name_start = '\0';
return -ENAMETOOLONG;
}
*name = *subpath;
name ++;
subpath ++;
name_len++;
}
*name = '\0';
return 0;
}

Expand Down Expand Up @@ -213,7 +245,10 @@ static struct devtmpfs_file *devtmpfs_file_lookup(struct devtmpfs_sb *superblock
subpath ++; /* skip '/' */

memset(subdir_name, 0, DIRENT_NAME_MAX);
_get_subdir(curpath, subdir_name);
if (_get_subdir(curpath, subdir_name, sizeof(subdir_name)) != 0)
{
return NULL;
}

rt_spin_lock(&superblock->lock);

Expand Down Expand Up @@ -490,6 +525,7 @@ static struct dfs_vnode *devtmpfs_create_vnode(struct dfs_dentry *dentry, int ty
struct devtmpfs_sb *superblock;
struct devtmpfs_file *d_file, *p_file;
char parent_path[DFS_PATH_MAX], file_name[DIRENT_NAME_MAX];
int ret;

if (dentry == NULL || dentry->mnt == NULL || dentry->mnt->data == NULL)
{
Expand All @@ -503,7 +539,13 @@ static struct dfs_vnode *devtmpfs_create_vnode(struct dfs_dentry *dentry, int ty
if (vnode)
{
/* find parent file */
_path_separate(dentry->pathname, parent_path, file_name);
ret = _path_separate(dentry->pathname, parent_path, sizeof(parent_path),
file_name, sizeof(file_name));
if (ret != RT_EOK)
{
dfs_vnode_destroy(vnode);
return NULL;
}
if (file_name[0] == '\0') /* it's root dir */
{
dfs_vnode_destroy(vnode);
Expand Down
13 changes: 13 additions & 0 deletions components/dfs/utest/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ if RT_USING_DFS

endif

config RT_UTEST_TC_USING_DEVTMPFS_PATH
bool "devtmpfs path component boundary Test"
default n
depends on RT_USING_DFS_V2
depends on RT_USING_DFS_DEVFS
depends on RT_USING_UTESTCASES
help
Enable boundary regression tests for devtmpfs path components.
Verifies that a path component of DIRENT_NAME_MAX bytes or more is
rejected with -ENAMETOOLONG instead of overrunning the stack buffers
used by devtmpfs_create_vnode() and devtmpfs_file_lookup(), and that
shorter components keep working. Requires devtmpfs mounted on /dev.

rsource "v2/af_unix/Kconfig"

endmenu
Expand Down
4 changes: 4 additions & 0 deletions components/dfs/utest/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ if GetDepend('RT_UTEST_TC_USING_DFS_API'):
if GetDepend('RT_UTEST_TC_USING_POSIX_API'):
src += ['tc_posix_api.c']

# Add devtmpfs path component boundary test if enabled (requires DFS v2)
if GetDepend('RT_UTEST_TC_USING_DEVTMPFS_PATH'):
src += ['tc_devtmpfs_path.c']

# Define the test group with proper dependencies
objs = DefineGroup('utestcases', src, depend = ['RT_USING_UTESTCASES', 'RT_USING_DFS'], CPPPATH = CPPPATH)
objs = objs + SConscript(os.path.join('v2', 'af_unix', 'SConscript'))
Expand Down
Loading
Loading