Skip to content

Commit aca602f

Browse files
OmegaRelaykartben
authored andcommitted
fs: zms: add input validation of fs pointer for api
Add checks on the fs pointer passed through the api before using to avoid causing an exception Signed-off-by: Theis Mejnertsen <[email protected]>
1 parent 4e44822 commit aca602f

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

include/zephyr/fs/zms.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ struct zms_fs {
8585
* @retval 0 on success.
8686
* @retval -ENOTSUP if the detected file system is not ZMS.
8787
* @retval -EPROTONOSUPPORT if the ZMS version is not supported.
88-
* @retval -EINVAL if any of the flash parameters or the sector layout is invalid.
88+
* @retval -EINVAL if `fs` is NULL or any of the flash parameters or the sector layout is invalid.
8989
* @retval -ENXIO if there is a device error.
9090
* @retval -EIO if there is a memory read/write error.
9191
*/
@@ -101,6 +101,7 @@ int zms_mount(struct zms_fs *fs);
101101
* @retval -EACCES if `fs` is not mounted.
102102
* @retval -ENXIO if there is a device error.
103103
* @retval -EIO if there is a memory read/write error.
104+
* @retval -EINVAL if `fs` is NULL.
104105
*/
105106
int zms_clear(struct zms_fs *fs);
106107

@@ -124,7 +125,7 @@ int zms_clear(struct zms_fs *fs);
124125
* @retval -EACCES if ZMS is still not initialized.
125126
* @retval -ENXIO if there is a device error.
126127
* @retval -EIO if there is a memory read/write error.
127-
* @retval -EINVAL if `len` is invalid.
128+
* @retval -EINVAL if `fs` is NULL or `len` is invalid.
128129
* @retval -ENOSPC if no space is left on the device.
129130
*/
130131
ssize_t zms_write(struct zms_fs *fs, uint32_t id, const void *data, size_t len);
@@ -139,6 +140,7 @@ ssize_t zms_write(struct zms_fs *fs, uint32_t id, const void *data, size_t len);
139140
* @retval -EACCES if ZMS is still not initialized.
140141
* @retval -ENXIO if there is a device error.
141142
* @retval -EIO if there is a memory read/write error.
143+
* @retval -EINVAL if `fs` is NULL.
142144
*/
143145
int zms_delete(struct zms_fs *fs, uint32_t id);
144146

@@ -157,6 +159,7 @@ int zms_delete(struct zms_fs *fs, uint32_t id);
157159
* @retval -EACCES if ZMS is still not initialized.
158160
* @retval -EIO if there is a memory read/write error.
159161
* @retval -ENOENT if there is no entry with the given `id`.
162+
* @retval -EINVAL if `fs` is NULL.
160163
*/
161164
ssize_t zms_read(struct zms_fs *fs, uint32_t id, void *data, size_t len);
162165

@@ -177,6 +180,7 @@ ssize_t zms_read(struct zms_fs *fs, uint32_t id, void *data, size_t len);
177180
* @retval -EACCES if ZMS is still not initialized.
178181
* @retval -EIO if there is a memory read/write error.
179182
* @retval -ENOENT if there is no entry with the given `id` and history counter.
183+
* @retval -EINVAL if `fs` is NULL.
180184
*/
181185
ssize_t zms_read_hist(struct zms_fs *fs, uint32_t id, void *data, size_t len, uint32_t cnt);
182186

@@ -192,6 +196,7 @@ ssize_t zms_read_hist(struct zms_fs *fs, uint32_t id, void *data, size_t len, ui
192196
* @retval -EACCES if ZMS is still not initialized.
193197
* @retval -EIO if there is a memory read/write error.
194198
* @retval -ENOENT if there is no entry with the given id.
199+
* @retval -EINVAL if `fs` is NULL.
195200
*/
196201
ssize_t zms_get_data_length(struct zms_fs *fs, uint32_t id);
197202

@@ -207,6 +212,7 @@ ssize_t zms_get_data_length(struct zms_fs *fs, uint32_t id);
207212
* @retval Number of free bytes (>= 0) on success.
208213
* @retval -EACCES if ZMS is still not initialized.
209214
* @retval -EIO if there is a memory read/write error.
215+
* @retval -EINVAL if `fs` is NULL.
210216
*/
211217
ssize_t zms_calc_free_space(struct zms_fs *fs);
212218

@@ -217,6 +223,7 @@ ssize_t zms_calc_free_space(struct zms_fs *fs);
217223
*
218224
* @retval >=0 Number of free bytes in the currently active sector
219225
* @retval -EACCES if ZMS is still not initialized.
226+
* @retval -EINVAL if `fs` is NULL.
220227
*/
221228
ssize_t zms_active_sector_free_space(struct zms_fs *fs);
222229

@@ -234,6 +241,7 @@ ssize_t zms_active_sector_free_space(struct zms_fs *fs);
234241
* @retval 0 on success.
235242
* @retval -EACCES if ZMS is still not initialized.
236243
* @retval -EIO if there is a memory read/write error.
244+
* @retval -EINVAL if `fs` is NULL.
237245
*/
238246
int zms_sector_use_next(struct zms_fs *fs);
239247

subsys/fs/zms/zms.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,11 @@ int zms_clear(struct zms_fs *fs)
11011101
int rc;
11021102
uint64_t addr;
11031103

1104+
if (!fs) {
1105+
LOG_ERR("Invalid fs");
1106+
return -EINVAL;
1107+
}
1108+
11041109
if (!fs->ready) {
11051110
LOG_ERR("zms not initialized");
11061111
return -EACCES;
@@ -1395,6 +1400,11 @@ int zms_mount(struct zms_fs *fs)
13951400
struct flash_pages_info info;
13961401
size_t write_block_size;
13971402

1403+
if (!fs) {
1404+
LOG_ERR("Invalid fs");
1405+
return -EINVAL;
1406+
}
1407+
13981408
k_mutex_init(&fs->zms_lock);
13991409

14001410
fs->flash_parameters = flash_get_parameters(fs->flash_device);
@@ -1465,6 +1475,11 @@ ssize_t zms_write(struct zms_fs *fs, uint32_t id, const void *data, size_t len)
14651475
uint32_t gc_count;
14661476
uint32_t required_space = 0U; /* no space, appropriate for delete ate */
14671477

1478+
if (!fs) {
1479+
LOG_ERR("Invalid fs");
1480+
return -EINVAL;
1481+
}
1482+
14681483
if (!fs->ready) {
14691484
LOG_ERR("zms not initialized");
14701485
return -EACCES;
@@ -1615,6 +1630,10 @@ ssize_t zms_read_hist(struct zms_fs *fs, uint32_t id, void *data, size_t len, ui
16151630
#ifdef CONFIG_ZMS_DATA_CRC
16161631
uint32_t computed_data_crc;
16171632
#endif
1633+
if (!fs) {
1634+
LOG_ERR("Invalid fs");
1635+
return -EINVAL;
1636+
}
16181637

16191638
if (!fs->ready) {
16201639
LOG_ERR("zms not initialized");
@@ -1739,6 +1758,12 @@ ssize_t zms_calc_free_space(struct zms_fs *fs)
17391758
uint64_t data_wra = 0U;
17401759
uint8_t current_cycle;
17411760
ssize_t free_space = 0;
1761+
1762+
if (!fs) {
1763+
LOG_ERR("Invalid fs");
1764+
return -EINVAL;
1765+
}
1766+
17421767
const uint32_t second_to_last_offset = (2 * fs->ate_size);
17431768

17441769
if (!fs->ready) {
@@ -1839,6 +1864,11 @@ ssize_t zms_calc_free_space(struct zms_fs *fs)
18391864

18401865
ssize_t zms_active_sector_free_space(struct zms_fs *fs)
18411866
{
1867+
if (!fs) {
1868+
LOG_ERR("Invalid fs");
1869+
return -EINVAL;
1870+
}
1871+
18421872
if (!fs->ready) {
18431873
LOG_ERR("ZMS not initialized");
18441874
return -EACCES;
@@ -1851,6 +1881,11 @@ int zms_sector_use_next(struct zms_fs *fs)
18511881
{
18521882
int ret;
18531883

1884+
if (!fs) {
1885+
LOG_ERR("Invalid fs");
1886+
return -EINVAL;
1887+
}
1888+
18541889
if (!fs->ready) {
18551890
LOG_ERR("ZMS not initialized");
18561891
return -EACCES;

0 commit comments

Comments
 (0)