Fix buffer overrun and memory leak on smartfs and improvement to cxd56xx

Author: Alan Carvalho de Assis <acassis@gmail.com>

    Check all .c and .h against nxstyle and fix it.

Author: Alin Jerpelea <alin.jerpelea@sony.com>

    fs: smartfs: Fix over capacity write

    When the remaining capacity of flash is one sector, if a new root
    directory is created by file open, then the root directory's chain is
    broken and it causes to SmartFS filesystem crash. Once this fatal
    problem occurs, it's impossible to recover even if the system reboot.
    Fix it by finally update link of root directory.

    fs: smartfs: Fix buffer overrun

    fs: smartfs: Fix uninitialized variable warnings

    fs: smartfs: Memory leak fix

    boards: cxd56xx: Update spresense board.h

    - Fix PMIC assignment
    - Add specific pin configurations for spresense
    - Remove unnecessary definitions

    arch: cxd56xx: Add ITM syslog init at startup

    arch: cxd56xx: Enable DMA settings dynamically
This commit is contained in:
Alin Jerpelea
2020-01-08 07:51:11 -03:00
committed by Alan Carvalho de Assis
parent 3abdc352a8
commit 3e45517599
13 changed files with 443 additions and 224 deletions
+3
View File
@@ -383,6 +383,9 @@ errout_with_buffer:
sf->entry.name = NULL;
}
#ifdef CONFIG_SMARTFS_USE_SECTOR_BUFFER
kmm_free(sf->buffer);
#endif /* CONFIG_SMARTFS_USE_SECTOR_BUFFER */
kmm_free(sf);
errout_with_semaphore:
+47 -16
View File
@@ -56,6 +56,12 @@
#include "smartfs.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define WORKBUFFER_SIZE 256
/****************************************************************************
* Private Data
****************************************************************************/
@@ -269,7 +275,7 @@ int smartfs_mount(struct smartfs_mountpt_s *fs, bool writeable)
if (nextfs == NULL)
{
fs->fs_rwbuffer = (char *) kmm_malloc(fs->fs_llformat.availbytes);
fs->fs_workbuffer = (char *) kmm_malloc(256);
fs->fs_workbuffer = (char *) kmm_malloc(WORKBUFFER_SIZE);
}
/* Now add ourselves to the linked list of SMART mounts */
@@ -292,12 +298,12 @@ int smartfs_mount(struct smartfs_mountpt_s *fs, bool writeable)
g_mounthead = fs;
#endif
#endif /* CONFIG_SMARTFS_MULTI_ROOT_DIRS */
fs->fs_rwbuffer = (char *) kmm_malloc(fs->fs_llformat.availbytes);
fs->fs_workbuffer = (char *) kmm_malloc(256);
fs->fs_workbuffer = (char *) kmm_malloc(WORKBUFFER_SIZE);
fs->fs_rootsector = SMARTFS_ROOT_DIR_SECTOR;
#endif /* CONFIG_SMARTFS_MULTI_ROOT_DIRS */
/* We did it! */
fs->fs_mounted = TRUE;
@@ -486,6 +492,11 @@ int smartfs_finddirentry(struct smartfs_mountpt_s *fs,
struct smart_read_write_s readwrite;
struct smartfs_entry_header_s *entry;
/* Set the initial value of the output */
*parentdirsector = 0xffff;
*filename = NULL;
/* Initialize directory level zero as the root sector */
dirstack[0] = fs->fs_rootsector;
@@ -523,6 +534,14 @@ int smartfs_finddirentry(struct smartfs_mountpt_s *fs,
ptr++;
}
/* Check to avoid buffer overflow */
if (seglen >= WORKBUFFER_SIZE)
{
ret = -ENAMETOOLONG;
goto errout;
}
strncpy(fs->fs_workbuffer, segment, seglen);
fs->fs_workbuffer[seglen] = '\0';
@@ -832,6 +851,9 @@ int smartfs_createentry(FAR struct smartfs_mountpt_s *fs,
uint16_t entrysize;
struct smartfs_entry_header_s *entry;
struct smartfs_chain_header_s *chainheader;
int update_chain = 0;
struct smart_read_write_s update_readwrite;
struct smartfs_chain_header_s update_header;
/* Start at the 1st sector in the parent directory */
@@ -924,19 +946,15 @@ int smartfs_createentry(FAR struct smartfs_mountpt_s *fs,
nextsector = (uint16_t) ret;
/* Chain the next sector into this sector sector */
/* Chain the next sector into this sector. */
*((FAR uint16_t *)chainheader->nextsector) = nextsector;
readwrite.offset = offsetof(struct smartfs_chain_header_s,
nextsector);
readwrite.count = sizeof(uint16_t);
readwrite.buffer = chainheader->nextsector;
ret = FS_IOCTL(fs, BIOC_WRITESECT, (unsigned long) &readwrite);
if (ret < 0)
{
ferr("ERROR: Error chaining sector %d\n", nextsector);
goto errout;
}
*((uint16_t *)update_header.nextsector) = nextsector;
update_readwrite.logsector = psector;
update_readwrite.offset = offsetof(struct smartfs_chain_header_s,
nextsector);
update_readwrite.count = sizeof(uint16_t);
update_readwrite.buffer = update_header.nextsector;
update_chain = 1;
}
/* Now update to the next sector */
@@ -1048,6 +1066,19 @@ int smartfs_createentry(FAR struct smartfs_mountpt_s *fs,
goto errout;
}
if (update_chain)
{
/* Update chain header after the next sector was written */
ret = FS_IOCTL(fs, BIOC_WRITESECT, (unsigned long) &update_readwrite);
if (ret < 0)
{
ferr("ERROR: Error chaining sector %d\n",
update_readwrite.logsector);
goto errout;
}
}
/* Now fill in the entry */
direntry->firstsector = nextsector;