Skip to content

Commit 6fdca3c

Browse files
committed
Merge tag 'erofs-for-7.1-rc1-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs
Pull erofs fixes from Gao Xiang: - Fix dirent nameoff handling to avoid out-of-bound reads out of crafted images - Fix two type truncation issues on 32-bit platforms * tag 'erofs-for-7.1-rc1-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs: erofs: unify lcn as u64 for 32-bit platforms erofs: fix offset truncation when shifting pgoff on 32-bit platforms erofs: fix the out-of-bounds nameoff handling for trailing dirents
2 parents 4ee6420 + 2d8c7ed commit 6fdca3c

4 files changed

Lines changed: 26 additions & 25 deletions

File tree

fs/erofs/data.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset, bool need_kmap)
3939
* However, the data access range must be verified here in advance.
4040
*/
4141
if (buf->file) {
42-
fpos = index << PAGE_SHIFT;
42+
fpos = (loff_t)index << PAGE_SHIFT;
4343
err = rw_verify_area(READ, buf->file, &fpos, PAGE_SIZE);
4444
if (err < 0)
4545
return ERR_PTR(err);

fs/erofs/dir.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,18 @@ static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
1919
const char *de_name = (char *)dentry_blk + nameoff;
2020
unsigned int de_namelen;
2121

22-
/* the last dirent in the block? */
23-
if (de + 1 >= end)
24-
de_namelen = strnlen(de_name, maxsize - nameoff);
25-
else
22+
/* non-trailing dirent in the directory block? */
23+
if (de + 1 < end)
2624
de_namelen = le16_to_cpu(de[1].nameoff) - nameoff;
25+
else if (maxsize <= nameoff)
26+
goto err_bogus;
27+
else
28+
de_namelen = strnlen(de_name, maxsize - nameoff);
2729

28-
/* a corrupted entry is found */
29-
if (nameoff + de_namelen > maxsize ||
30-
de_namelen > EROFS_NAME_LEN) {
31-
erofs_err(dir->i_sb, "bogus dirent @ nid %llu",
32-
EROFS_I(dir)->nid);
33-
DBG_BUGON(1);
34-
return -EFSCORRUPTED;
35-
}
30+
/* a corrupted entry is found (including negative namelen) */
31+
if (!in_range32(de_namelen, 1, EROFS_NAME_LEN) ||
32+
nameoff + de_namelen > maxsize)
33+
goto err_bogus;
3634

3735
if (!dir_emit(ctx, de_name, de_namelen,
3836
erofs_nid_to_ino64(EROFS_SB(dir->i_sb),
@@ -42,6 +40,10 @@ static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
4240
ctx->pos += sizeof(struct erofs_dirent);
4341
}
4442
return 0;
43+
err_bogus:
44+
erofs_err(dir->i_sb, "bogus dirent @ nid %llu", EROFS_I(dir)->nid);
45+
DBG_BUGON(1);
46+
return -EFSCORRUPTED;
4547
}
4648

4749
static int erofs_readdir(struct file *f, struct dir_context *ctx)
@@ -88,7 +90,7 @@ static int erofs_readdir(struct file *f, struct dir_context *ctx)
8890
}
8991

9092
nameoff = le16_to_cpu(de->nameoff);
91-
if (nameoff < sizeof(struct erofs_dirent) || nameoff >= bsz) {
93+
if (!nameoff || nameoff >= bsz || (nameoff % sizeof(*de))) {
9294
erofs_err(sb, "invalid de[0].nameoff %u @ nid %llu",
9395
nameoff, EROFS_I(dir)->nid);
9496
err = -EFSCORRUPTED;

fs/erofs/zdata.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1872,7 +1872,7 @@ static void z_erofs_pcluster_readmore(struct z_erofs_frontend *f,
18721872

18731873
if (cur < PAGE_SIZE)
18741874
break;
1875-
cur = (index << PAGE_SHIFT) - 1;
1875+
cur = ((loff_t)index << PAGE_SHIFT) - 1;
18761876
}
18771877
}
18781878

fs/erofs/zmap.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
struct z_erofs_maprecorder {
1111
struct inode *inode;
1212
struct erofs_map_blocks *map;
13-
unsigned long lcn;
13+
u64 lcn;
1414
/* compression extent information gathered */
1515
u8 type, headtype;
1616
u16 clusterofs;
@@ -20,8 +20,7 @@ struct z_erofs_maprecorder {
2020
bool partialref, in_mbox;
2121
};
2222

23-
static int z_erofs_load_full_lcluster(struct z_erofs_maprecorder *m,
24-
unsigned long lcn)
23+
static int z_erofs_load_full_lcluster(struct z_erofs_maprecorder *m, u64 lcn)
2524
{
2625
struct inode *const inode = m->inode;
2726
struct erofs_inode *const vi = EROFS_I(inode);
@@ -94,7 +93,7 @@ static int get_compacted_la_distance(unsigned int lobits,
9493
}
9594

9695
static int z_erofs_load_compact_lcluster(struct z_erofs_maprecorder *m,
97-
unsigned long lcn, bool lookahead)
96+
u64 lcn, bool lookahead)
9897
{
9998
struct inode *const inode = m->inode;
10099
struct erofs_inode *const vi = EROFS_I(inode);
@@ -234,7 +233,7 @@ static int z_erofs_load_compact_lcluster(struct z_erofs_maprecorder *m,
234233
}
235234

236235
static int z_erofs_load_lcluster_from_disk(struct z_erofs_maprecorder *m,
237-
unsigned int lcn, bool lookahead)
236+
u64 lcn, bool lookahead)
238237
{
239238
struct erofs_inode *vi = EROFS_I(m->inode);
240239
int err;
@@ -249,7 +248,7 @@ static int z_erofs_load_lcluster_from_disk(struct z_erofs_maprecorder *m,
249248
return err;
250249

251250
if (m->type >= Z_EROFS_LCLUSTER_TYPE_MAX) {
252-
erofs_err(m->inode->i_sb, "unknown type %u @ lcn %u of nid %llu",
251+
erofs_err(m->inode->i_sb, "unknown type %u @ lcn %llu of nid %llu",
253252
m->type, lcn, EROFS_I(m->inode)->nid);
254253
DBG_BUGON(1);
255254
return -EOPNOTSUPP;
@@ -269,7 +268,7 @@ static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m,
269268
const unsigned int lclusterbits = vi->z_lclusterbits;
270269

271270
while (m->lcn >= lookback_distance) {
272-
unsigned long lcn = m->lcn - lookback_distance;
271+
u64 lcn = m->lcn - lookback_distance;
273272
int err;
274273

275274
if (!lookback_distance)
@@ -286,7 +285,7 @@ static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m,
286285
m->map->m_la = (lcn << lclusterbits) | m->clusterofs;
287286
return 0;
288287
}
289-
erofs_err(sb, "bogus lookback distance %u @ lcn %lu of nid %llu",
288+
erofs_err(sb, "bogus lookback distance %u @ lcn %llu of nid %llu",
290289
lookback_distance, m->lcn, vi->nid);
291290
DBG_BUGON(1);
292291
return -EFSCORRUPTED;
@@ -300,7 +299,7 @@ static int z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder *m,
300299
struct erofs_inode *vi = EROFS_I(inode);
301300
bool bigpcl1 = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1;
302301
bool bigpcl2 = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2;
303-
unsigned long lcn = m->lcn + 1;
302+
u64 lcn = m->lcn + 1;
304303
int err;
305304

306305
DBG_BUGON(m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD);
@@ -331,7 +330,7 @@ static int z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder *m,
331330
m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD);
332331

333332
if (m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD && m->delta[0] != 1) {
334-
erofs_err(sb, "bogus CBLKCNT @ lcn %lu of nid %llu", lcn, vi->nid);
333+
erofs_err(sb, "bogus CBLKCNT @ lcn %llu of nid %llu", lcn, vi->nid);
335334
DBG_BUGON(1);
336335
return -EFSCORRUPTED;
337336
}

0 commit comments

Comments
 (0)