@@ -107,6 +107,19 @@ static int lfs_cache_crc(lfs_t *lfs, lfs_cache_t *rcache,
107
107
return 0 ;
108
108
}
109
109
110
+ static inline void lfs_cache_drop (lfs_t * lfs , lfs_cache_t * rcache ) {
111
+ // do not zero, cheaper if cache is readonly or only going to be
112
+ // written with identical data (during relocates)
113
+ (void )lfs ;
114
+ rcache -> block = 0xffffffff ;
115
+ }
116
+
117
+ static inline void lfs_cache_zero (lfs_t * lfs , lfs_cache_t * pcache ) {
118
+ // zero to avoid information leak
119
+ memset (pcache -> buffer , 0xff , lfs -> cfg -> prog_size );
120
+ pcache -> block = 0xffffffff ;
121
+ }
122
+
110
123
static int lfs_cache_flush (lfs_t * lfs ,
111
124
lfs_cache_t * pcache , lfs_cache_t * rcache ) {
112
125
if (pcache -> block != 0xffffffff ) {
@@ -128,7 +141,7 @@ static int lfs_cache_flush(lfs_t *lfs,
128
141
}
129
142
}
130
143
131
- pcache -> block = 0xffffffff ;
144
+ lfs_cache_zero ( lfs , pcache ) ;
132
145
}
133
146
134
147
return 0 ;
@@ -233,7 +246,7 @@ static int lfs_bd_erase(lfs_t *lfs, lfs_block_t block) {
233
246
}
234
247
235
248
static int lfs_bd_sync (lfs_t * lfs ) {
236
- lfs -> rcache . block = 0xffffffff ;
249
+ lfs_cache_drop ( lfs , & lfs -> rcache ) ;
237
250
238
251
int err = lfs_cache_flush (lfs , & lfs -> pcache , NULL );
239
252
if (err ) {
@@ -592,7 +605,7 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_dir_t *dir,
592
605
593
606
// drop caches and prepare to relocate block
594
607
relocated = true;
595
- lfs -> pcache . block = 0xffffffff ;
608
+ lfs_cache_drop ( lfs , & lfs -> pcache ) ;
596
609
597
610
// can't relocate superblock, filesystem is now frozen
598
611
if (lfs_paircmp (oldpair , (const lfs_block_t [2 ]){0 , 1 }) == 0 ) {
@@ -1217,7 +1230,7 @@ static int lfs_ctz_extend(lfs_t *lfs,
1217
1230
LFS_DEBUG ("Bad block at %d" , nblock );
1218
1231
1219
1232
// just clear cache and try a new block
1220
- pcache -> block = 0xffffffff ;
1233
+ lfs_cache_drop ( lfs , & lfs -> pcache ) ;
1221
1234
}
1222
1235
}
1223
1236
@@ -1322,7 +1335,6 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
1322
1335
}
1323
1336
1324
1337
// allocate buffer if needed
1325
- file -> cache .block = 0xffffffff ;
1326
1338
if (lfs -> cfg -> file_buffer ) {
1327
1339
if (lfs -> files ) {
1328
1340
// already in use
@@ -1341,6 +1353,9 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
1341
1353
}
1342
1354
}
1343
1355
1356
+ // zero to avoid information leak
1357
+ lfs_cache_zero (lfs , & file -> cache );
1358
+
1344
1359
// add to list of files
1345
1360
file -> next = lfs -> files ;
1346
1361
lfs -> files = file ;
@@ -1409,7 +1424,7 @@ static int lfs_file_relocate(lfs_t *lfs, lfs_file_t *file) {
1409
1424
memcpy (file -> cache .buffer , lfs -> pcache .buffer , lfs -> cfg -> prog_size );
1410
1425
file -> cache .block = lfs -> pcache .block ;
1411
1426
file -> cache .off = lfs -> pcache .off ;
1412
- lfs -> pcache . block = 0xffffffff ;
1427
+ lfs_cache_zero ( lfs , & lfs -> pcache ) ;
1413
1428
1414
1429
file -> block = nblock ;
1415
1430
return 0 ;
@@ -1418,7 +1433,7 @@ static int lfs_file_relocate(lfs_t *lfs, lfs_file_t *file) {
1418
1433
static int lfs_file_flush (lfs_t * lfs , lfs_file_t * file ) {
1419
1434
if (file -> flags & LFS_F_READING ) {
1420
1435
// just drop read cache
1421
- file -> cache . block = 0xffffffff ;
1436
+ lfs_cache_drop ( lfs , & file -> cache ) ;
1422
1437
file -> flags &= ~LFS_F_READING ;
1423
1438
}
1424
1439
@@ -1433,7 +1448,7 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) {
1433
1448
.pos = file -> pos ,
1434
1449
.cache = lfs -> rcache ,
1435
1450
};
1436
- lfs -> rcache . block = 0xffffffff ;
1451
+ lfs_cache_drop ( lfs , & lfs -> rcache ) ;
1437
1452
1438
1453
while (file -> pos < file -> size ) {
1439
1454
// copy over a byte at a time, leave it up to caching
@@ -1451,8 +1466,8 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) {
1451
1466
1452
1467
// keep our reference to the rcache in sync
1453
1468
if (lfs -> rcache .block != 0xffffffff ) {
1454
- orig .cache . block = 0xffffffff ;
1455
- lfs -> rcache . block = 0xffffffff ;
1469
+ lfs_cache_drop ( lfs , & orig .cache ) ;
1470
+ lfs_cache_drop ( lfs , & lfs -> rcache ) ;
1456
1471
}
1457
1472
}
1458
1473
@@ -1630,7 +1645,7 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
1630
1645
}
1631
1646
1632
1647
// mark cache as dirty since we may have read data into it
1633
- file -> cache . block = 0xffffffff ;
1648
+ lfs_cache_zero ( lfs , & file -> cache ) ;
1634
1649
}
1635
1650
1636
1651
// extend file with new blocks
@@ -1981,7 +1996,6 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
1981
1996
lfs -> cfg = cfg ;
1982
1997
1983
1998
// setup read cache
1984
- lfs -> rcache .block = 0xffffffff ;
1985
1999
if (lfs -> cfg -> read_buffer ) {
1986
2000
lfs -> rcache .buffer = lfs -> cfg -> read_buffer ;
1987
2001
} else {
@@ -1992,7 +2006,6 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
1992
2006
}
1993
2007
1994
2008
// setup program cache
1995
- lfs -> pcache .block = 0xffffffff ;
1996
2009
if (lfs -> cfg -> prog_buffer ) {
1997
2010
lfs -> pcache .buffer = lfs -> cfg -> prog_buffer ;
1998
2011
} else {
@@ -2002,6 +2015,10 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
2002
2015
}
2003
2016
}
2004
2017
2018
+ // zero to avoid information leaks
2019
+ lfs_cache_zero (lfs , & lfs -> rcache );
2020
+ lfs_cache_zero (lfs , & lfs -> pcache );
2021
+
2005
2022
// setup lookahead, round down to nearest 32-bits
2006
2023
LFS_ASSERT (lfs -> cfg -> lookahead % 32 == 0 );
2007
2024
LFS_ASSERT (lfs -> cfg -> lookahead > 0 );
0 commit comments