erofs-utils: fsck: fix unsigned integer overflow in symlink extraction
PATCH 1/3 erofs-utils: fsck: fix unsigned integer overflow in symlink extraction
erofs_extract_symlink() allocates a buffer using malloc(i_size + 1) where i_size is a 64-bit value read directly from the on-disk inode with no upper bound validation. When i_size equals UINT64_MAX, the addition wraps to zero, and malloc(0) returns a minimal allocation. The subsequent erofs_pread() writes i_size bytes into this tiny buffer.
--- fsck/main.c | 5 +++-- include/erofs/defs.h | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-)
fsck/main.c +3 -2
@@ -784,6 +784,7 @@ static inline int erofs_extract_symlink(struct erofs_inode *inode) {struct erofs_vfile vf;bool tryagain = true; +erofs_off_t bufsz;int ret;char *buf =NULL ;
@@ -794,8 +795,8 @@ static inline int erofs_extract_symlink(struct erofs_inode *inode)if (ret)return ret; - buf =malloc (inode->i_size +1 ); -if (!buf) { +if (check_add_overflow (inode->i_size, (erofs_off_t )1 , &bufsz) || + (buf =malloc (bufsz))) { ret = -ENOMEM ; goto out; }
include/erofs/defs.h +9 -0
@@ -392,6 +392,15 @@ unsigned long __roundup_pow_of_two(unsigned long n) #define __erofs_stringify_1(x...) #x #define __erofs_stringify(x...) __erofs_stringify_1(x) +#define check_add_overflow (a, b, d) ({ \ + typeof(a) __a = (a); \ + typeof(b) __b = (b); \ + typeof(d) __d = (d); \ + (void) (&__a == &__b); \ + (void) (&__a == __d); \ + __builtin_add_overflow(__a, __b, __d); \ +}) +#define check_sub_overflow (a, b, d) ({ \ typeof(a) __a = (a); \ typeof(b) __b = (b); \
Signature · 1 line
2.43.5
PATCH 2/3 erofs-utils: fsck: account '/' separator in path construction
--- fsck/main.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
fsck/main.c +4 -4
@@ -191,7 +191,7 @@ static int erofsfsck_parse_options_cfg(int argc, char **argv)return -ENAMETOOLONG ; } - fsckcfg.extract_path =malloc (PATH_MAX ); + fsckcfg.extract_path =malloc (PATH_MAX +1 );if (!fsckcfg.extract_path)return -ENOMEM ;strncpy (fsckcfg.extract_path, optarg, len);
@@ -902,9 +902,9 @@ static int erofsfsck_dirent_iter(struct erofs_dir_context *ctx) prev_pos = fsckcfg.extract_pos; curr_pos = prev_pos; -if (prev_pos + ctx->de_namelen >=PATH_MAX ) { +if (prev_pos + ctx->de_namelen +1 >=PATH_MAX ) {erofs_err ("unable to fsck since the path is too long (%llu)" , - (curr_pos + ctx->de_namelen) |0ULL ); + (curr_pos + ctx->de_namelen +1 ) |0ULL );return -EOPNOTSUPP ; }
@@ -915,7 +915,7 @@ static int erofsfsck_dirent_iter(struct erofs_dir_context *ctx) curr_pos += ctx->de_namelen; fsckcfg.extract_path[curr_pos] ='\0' ; }else { - curr_pos += ctx->de_namelen; + curr_pos += ctx->de_namelen +1 ; } fsckcfg.extract_pos = curr_pos; ret =erofsfsck_check_inode (ctx->dir->nid, ctx->de_nid);
Signature · 1 line
2.43.5
PATCH 3/3 erofs-utils: fsck: fix potential overflow due to u64-to-u32 truncation
In reply to Gao XiangIt seems possible but no reproducible way made into public.
--- fsck/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fsck/main.c +1 -1
@@ -513,7 +513,7 @@ static int erofs_verify_inode_data(struct erofs_inode *inode, int outfd)bool compressed;erofs_off_t pos =0 ;u64 pchunk_len =0 ; -unsigned int raw_size =0 , buffer_size =0 ; +u64 raw_size =0 , buffer_size =0 ;char *raw =NULL , *buffer =NULL ; erofs_dbg("verify data chunk of nid(%llu): type(%d)",
Signature · 1 line
2.43.5
PATCH v2 1/3 erofs-utils: fsck: fix unsigned integer overflow in symlink extraction
In reply to Gao Xiangerofs_extract_symlink() allocates a buffer using malloc(i_size + 1) where i_size is a 64-bit value read directly from the on-disk inode with no upper bound validation. When i_size equals UINT64_MAX, the addition wraps to zero, and malloc(0) returns a minimal allocation. The subsequent erofs_pread() writes i_size bytes into this tiny buffer.
--- fsck/main.c | 5 +++-- include/erofs/defs.h | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-)
fsck/main.c +3 -2
@@ -784,6 +784,7 @@ static inline int erofs_extract_symlink(struct erofs_inode *inode) {struct erofs_vfile vf;bool tryagain = true; +erofs_off_t bufsz;int ret;char *buf =NULL ;
@@ -794,8 +795,8 @@ static inline int erofs_extract_symlink(struct erofs_inode *inode)if (ret)return ret; - buf =malloc (inode->i_size +1 ); -if (!buf) { +if (check_add_overflow (inode->i_size, (erofs_off_t )1 , &bufsz) || + !(buf =malloc (bufsz))) { ret = -ENOMEM ; goto out; }
include/erofs/defs.h +9 -0
@@ -392,6 +392,15 @@ unsigned long __roundup_pow_of_two(unsigned long n) #define __erofs_stringify_1(x...) #x #define __erofs_stringify(x...) __erofs_stringify_1(x) +#define check_add_overflow (a, b, d) ({ \ + typeof(a) __a = (a); \ + typeof(b) __b = (b); \ + typeof(d) __d = (d); \ + (void) (&__a == &__b); \ + (void) (&__a == __d); \ + __builtin_add_overflow(__a, __b, __d); \ +}) +#define check_sub_overflow (a, b, d) ({ \ typeof(a) __a = (a); \ typeof(b) __b = (b); \
Signature · 1 line
2.43.5