Merge branch 'bugfix/vfs_zero_length_base_path' into 'master'

vfs: fix invalid dereference for zero-length base_path

Closes IDFGH-3567

See merge request espressif/esp-idf!9561
This commit is contained in:
Ivan Grokhotkov
2020-07-23 04:06:48 +08:00
2 changed files with 9 additions and 3 deletions

View File

@@ -259,12 +259,16 @@ typedef struct
* Register a virtual filesystem for given path prefix. * Register a virtual filesystem for given path prefix.
* *
* @param base_path file path prefix associated with the filesystem. * @param base_path file path prefix associated with the filesystem.
* Must be a zero-terminated C string, up to ESP_VFS_PATH_MAX * Must be a zero-terminated C string, may be empty.
* If not empty, must be up to ESP_VFS_PATH_MAX
* characters long, and at least 2 characters long. * characters long, and at least 2 characters long.
* Name must start with a "/" and must not end with "/". * Name must start with a "/" and must not end with "/".
* For example, "/data" or "/dev/spi" are valid. * For example, "/data" or "/dev/spi" are valid.
* These VFSes would then be called to handle file paths such as * These VFSes would then be called to handle file paths such as
* "/data/myfile.txt" or "/dev/spi/0". * "/data/myfile.txt" or "/dev/spi/0".
* In the special case of an empty base_path, a "fallback"
* VFS is registered. Such VFS will handle paths which are not
* matched by any other registered VFS.
* @param vfs Pointer to esp_vfs_t, a structure which maps syscalls to * @param vfs Pointer to esp_vfs_t, a structure which maps syscalls to
* the filesystem driver functions. VFS component doesn't * the filesystem driver functions. VFS component doesn't
* assume ownership of this pointer. * assume ownership of this pointer.

View File

@@ -75,10 +75,12 @@ static _lock_t s_fd_table_lock;
static esp_err_t esp_vfs_register_common(const char* base_path, size_t len, const esp_vfs_t* vfs, void* ctx, int *vfs_index) static esp_err_t esp_vfs_register_common(const char* base_path, size_t len, const esp_vfs_t* vfs, void* ctx, int *vfs_index)
{ {
if (len != LEN_PATH_PREFIX_IGNORED) { if (len != LEN_PATH_PREFIX_IGNORED) {
if ((len != 0 && len < 2) || (len > ESP_VFS_PATH_MAX)) { /* empty prefix is allowed, "/" is not allowed */
if ((len == 1) || (len > ESP_VFS_PATH_MAX)) {
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
} }
if ((len > 0 && base_path[0] != '/') || base_path[len - 1] == '/') { /* prefix has to start with "/" and not end with "/" */
if (len >= 2 && ((base_path[0] != '/') || (base_path[len - 1] == '/'))) {
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
} }
} }