Add extra hardening checks to FS
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/0c6d37a8 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/0c6d37a8 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/0c6d37a8 Branch: refs/heads/sensors_branch Commit: 0c6d37a89e4b8104f3fe8b74cb9629e86426bf70 Parents: 408caf5 Author: Fabio Utzig <[email protected]> Authored: Wed Feb 15 09:56:05 2017 -0800 Committer: Fabio Utzig <[email protected]> Committed: Wed Feb 15 09:56:05 2017 -0800 ---------------------------------------------------------------------- fs/fs/src/fs_dirent.c | 3 +-- fs/fs/src/fs_file.c | 2 +- fs/fs/src/fs_mount.c | 5 +++++ fs/fs/src/fsutil.c | 4 ++-- fs/nffs/src/nffs.c | 3 +++ 5 files changed, 12 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0c6d37a8/fs/fs/src/fs_dirent.c ---------------------------------------------------------------------- diff --git a/fs/fs/src/fs_dirent.c b/fs/fs/src/fs_dirent.c index 0216f3b..c9e4636 100644 --- a/fs/fs/src/fs_dirent.c +++ b/fs/fs/src/fs_dirent.c @@ -25,8 +25,7 @@ struct fs_ops *fops_from_filename(const char *); static struct fs_ops * fops_from_dir(const struct fs_dir *dir) { - /* NOTE: fs_ops must always be the first field for any fs_file */ - return (struct fs_ops *) *((uint32_t *)dir); + return fs_ops_from_container((struct fops_container *) dir); } static inline struct fs_ops * http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0c6d37a8/fs/fs/src/fs_file.c ---------------------------------------------------------------------- diff --git a/fs/fs/src/fs_file.c b/fs/fs/src/fs_file.c index 305a3c4..cb8d1e6 100644 --- a/fs/fs/src/fs_file.c +++ b/fs/fs/src/fs_file.c @@ -117,7 +117,7 @@ fake_dirent_is_dir(const struct fs_dirent *dirent) return FS_EUNINIT; } -static struct fs_ops not_initialized_ops = { +struct fs_ops not_initialized_ops = { .f_open = &fake_open, .f_close = &fake_close, .f_read = &fake_read, http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0c6d37a8/fs/fs/src/fs_mount.c ---------------------------------------------------------------------- diff --git a/fs/fs/src/fs_mount.c b/fs/fs/src/fs_mount.c index 022ea29..2e998c4 100644 --- a/fs/fs/src/fs_mount.c +++ b/fs/fs/src/fs_mount.c @@ -91,8 +91,13 @@ fs_ops_for(const char *fs_name) return fops; } +struct fs_ops not_initialized_ops; + struct fs_ops * fs_ops_from_container(struct fops_container *container) { + if (!container) { + return ¬_initialized_ops; + } return container->fops; } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0c6d37a8/fs/fs/src/fsutil.c ---------------------------------------------------------------------- diff --git a/fs/fs/src/fsutil.c b/fs/fs/src/fsutil.c index 7fb5360..7fc97d1 100644 --- a/fs/fs/src/fsutil.c +++ b/fs/fs/src/fsutil.c @@ -28,7 +28,7 @@ fsutil_read_file(const char *path, uint32_t offset, uint32_t len, void *dst, rc = fs_open(path, FS_ACCESS_READ, &file); if (rc != 0) { - goto done; + return rc; } rc = fs_read(file, len, dst, out_len); @@ -51,7 +51,7 @@ fsutil_write_file(const char *path, const void *data, uint32_t len) rc = fs_open(path, FS_ACCESS_WRITE, &file); if (rc != 0) { - goto done; + return rc; } rc = fs_write(file, data, len); http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0c6d37a8/fs/nffs/src/nffs.c ---------------------------------------------------------------------- diff --git a/fs/nffs/src/nffs.c b/fs/nffs/src/nffs.c index 7b5c128..5105ca1 100644 --- a/fs/nffs/src/nffs.c +++ b/fs/nffs/src/nffs.c @@ -516,6 +516,9 @@ done: free(filepath); } nffs_unlock(); + if (rc != 0) { + *out_dir = NULL; + } return rc; }
