erofs-utils: fix fragmentoff overflow for large packed inode
PATCH 1/1 erofs-utils: fix fragmentoff overflow for large packed inode
From: Yue Hu <[email protected]>
The return value of ftell() is a long int, use ftello{64} instead. Also, use lseek64 for large file position to avoid this error if we have it. And need to return at once if they fail.
--- configure.ac | 2 ++ lib/fragments.c | 34 +++++++++++++++++++++++++++++----- lib/inode.c | 9 +++++++-- 3 files changed, 38 insertions(+), 7 deletions(-)
configure.ac +2 -0
@@ -190,6 +190,8 @@ AC_CHECK_FUNCS(m4_flatten([ llistxattr memset realpath + lseek64 + ftello64 pread64 pwrite64 strdup
lib/fragments.c +29 -5
@@ -3,7 +3,18 @@* Copyright (C), 2022, Coolpad Group Limited. * Created by Yue Hu <[email protected]> */ +#ifndef _LARGEFILE_SOURCE +#define _LARGEFILE_SOURCE +#endif +#ifndef _LARGEFILE64_SOURCE +#define _LARGEFILE64_SOURCE +#endif +#ifndef _FILE_OFFSET_BITS +#define _FILE_OFFSET_BITS 64 +#endif +#ifndef _GNU_SOURCE#define _GNU_SOURCE +#endif #include <stdlib.h> #include <unistd.h> #include "erofs/err.h"
@@ -30,6 +41,13 @@ static struct list_head dupli_frags[FRAGMENT_HASHSIZE];static FILE *packedfile;const char *frags_packedname ="packed_file" ; +#ifndef HAVE_LSEEK64 +static inline off_t lseek64 (int fd,u64 offset,int set) +{ +return lseek (fd, offset, set); +} +#endif +static int z_erofs_fragments_dedupe_find (struct erofs_inode *inode,int fd,u32 crc) {
@@ -53,8 +71,7 @@ static int z_erofs_fragments_dedupe_find(struct erofs_inode *inode, int fd,if (!data)return -ENOMEM ; - ret =lseek (fd, inode->i_size - length,SEEK_SET ); -if (ret <0 ) { +if (lseek64 (fd, inode->i_size - length,SEEK_SET ) <0 ) { ret = -errno; goto out; }
@@ -113,8 +130,7 @@ int z_erofs_fragments_dedupe(struct erofs_inode *inode, int fd, u32 *tofcrc)if (inode->i_size <=EROFS_TOF_HASHLEN )return 0 ; - ret =lseek (fd, inode->i_size -EROFS_TOF_HASHLEN ,SEEK_SET ); -if (ret <0 ) +if (lseek64 (fd, inode->i_size -EROFS_TOF_HASHLEN ,SEEK_SET ) <0 )return -errno; ret =read (fd, data_to_hash,EROFS_TOF_HASHLEN );
@@ -192,9 +208,17 @@ void z_erofs_fragments_commit(struct erofs_inode *inode)int z_erofs_pack_fragments (struct erofs_inode *inode,void *data,unsigned int len,u32 tofcrc) { +#ifdef HAVE_FTELLO64 +off64_t offset =ftello64 (packedfile); +#else +off_t offset =ftello (packedfile); +#endif int ret; - inode->fragmentoff =ftell (packedfile); +if (offset <0 ) +return -errno; + + inode->fragmentoff = (erofs_off_t )offset; inode->fragment_size = len;if (fwrite (data, len,1 , packedfile) !=1 )
lib/inode.c +7 -2
@@ -1196,7 +1196,10 @@ struct erofs_inode *erofs_mkfs_build_special_from_fd(int fd, const char *name)struct erofs_inode *inode;int ret; -lseek (fd,0 ,SEEK_SET ); + ret =lseek (fd,0 ,SEEK_SET ); +if (ret <0 ) +return ERR_PTR (-errno); + ret =fstat64 (fd, &st);if (ret)return ERR_PTR (-errno);
@@ -1223,7 +1226,9 @@ struct erofs_inode *erofs_mkfs_build_special_from_fd(int fd, const char *name) ret =erofs_write_compressed_file (inode, fd);if (ret == -ENOSPC ) { -lseek (fd,0 ,SEEK_SET ); + ret =lseek (fd,0 ,SEEK_SET ); +if (ret <0 ) +return ERR_PTR (-errno); ret =write_uncompressed_file_from_fd (inode, fd); }
Signature · 1 line
2.17.1
(cc Khem Raj <[email protected]>... Since I'd like to apply this first
and then enable _FILE_OFFSET_BITS=64 unconditionally so that we could clean up all hard-coded _FILE_OFFSET_BITS in the source code.)
Hi Yue,
Quoted 50 lines · Yue Hu
From: Yue Hu <[email protected]>
The return value of
ftell()is a long int, use ftello{64} instead. Also, use lseek64 for large file position to avoid this error if we have it. And need to return at once if they fail.Signed-off-by Yue Hu <[email protected]>--- configure.ac | 2 ++ lib/fragments.c | 34 +++++++++++++++++++++++++++++----- lib/inode.c | 9 +++++++-- 3 files changed, 38 insertions(+), 7 deletions(-)configure.ac +2
-0@@ -190,6 +190,8 @@ AC_CHECK_FUNCS(m4_flatten([ llistxattr memset realpath + lseek64 + ftello64 pread64 pwrite64 strduplib/fragments.c +11
-0@@ -3,7 +3,18 @@* Copyright (C), 2022, Coolpad Group Limited. * Created by Yue Hu <[email protected]> */ +#ifndef _LARGEFILE_SOURCE +#define _LARGEFILE_SOURCE +#endif +#ifndef _LARGEFILE64_SOURCE +#define _LARGEFILE64_SOURCE +#endif +#ifndef _FILE_OFFSET_BITS +#define _FILE_OFFSET_BITS 64 +#endif +#ifndef _GNU_SOURCE#define _GNU_SOURCE +#endif #include <stdlib.h> #include <unistd.h> #include "erofs/err.h"
lib/fragments.c static struct list_head dupli_frags[FRAGMENT_HASHSIZE]; · line 49
Quoted 9 lines
lib/fragments.c +6
-0@@ -30,6 +41,13 @@ static struct list_head dupli_frags[FRAGMENT_HASHSIZE];static FILE *packedfile;const char *frags_packedname ="packed_file" ; +#ifndef HAVE_LSEEK64 +static inline off_t lseek64 (int fd,u64 offset,int set) +{ +return lseek (fd, offset, set); +} +#endif
Could we use another function name other than native lseek64?
Quoted 48 lines
lib/fragments.c +12
-5+static int z_erofs_fragments_dedupe_find (struct erofs_inode *inode,int fd,u32 crc) {@@ -53,8 +71,7 @@ static int z_erofs_fragments_dedupe_find(struct erofs_inode *inode, int fd,if (!data)return -ENOMEM ; - ret =lseek (fd, inode->i_size - length,SEEK_SET ); -if (ret <0 ) { +if (lseek64 (fd, inode->i_size - length,SEEK_SET ) <0 ) { ret = -errno; goto out; }@@ -113,8 +130,7 @@ int z_erofs_fragments_dedupe(struct erofs_inode *inode, int fd, u32 *tofcrc)if (inode->i_size <=EROFS_TOF_HASHLEN )return 0 ; - ret =lseek (fd, inode->i_size -EROFS_TOF_HASHLEN ,SEEK_SET ); -if (ret <0 ) +if (lseek64 (fd, inode->i_size -EROFS_TOF_HASHLEN ,SEEK_SET ) <0 )return -errno; ret =read (fd, data_to_hash,EROFS_TOF_HASHLEN );@@ -192,9 +208,17 @@ void z_erofs_fragments_commit(struct erofs_inode *inode)int z_erofs_pack_fragments (struct erofs_inode *inode,void *data,unsigned int len,u32 tofcrc) { +#ifdef HAVE_FTELLO64 +off64_t offset =ftello64 (packedfile); +#else +off_t offset =ftello (packedfile); +#endif int ret; - inode->fragmentoff =ftell (packedfile); +if (offset <0 ) +return -errno; + + inode->fragmentoff = (erofs_off_t )offset; inode->fragment_size = len;if (fwrite (data, len,1 , packedfile) !=1 )lib/inode.c +0
-0
lib/inode.c struct erofs_inode *erofs_mkfs_build_special_from_fd(int fd, const char *name) · line 1201
Quoted 7 lines
lib/inode.c +3
-1@@ -1196,7 +1196,10 @@ struct erofs_inode *erofs_mkfs_build_special_from_fd(int fd, const char *name)struct erofs_inode *inode;int ret; -lseek (fd,0 ,SEEK_SET ); + ret =lseek (fd,0 ,SEEK_SET ); +if (ret <0 ) +return ERR_PTR (-errno);
This should be in a seperate patch, please don't fix it here.
lib/inode.c struct erofs_inode *erofs_mkfs_build_special_from_fd(int fd, const char *name) · line 1230
Quoted 11 lines
lib/inode.c +3
-1+ ret =fstat64 (fd, &st);if (ret)return ERR_PTR (-errno);@@ -1223,7 +1226,9 @@ struct erofs_inode *erofs_mkfs_build_special_from_fd(int fd, const char *name) ret =erofs_write_compressed_file (inode, fd);if (ret == -ENOSPC ) { -lseek (fd,0 ,SEEK_SET ); + ret =lseek (fd,0 ,SEEK_SET ); +if (ret <0 )
Same here.
Thanks,
Gao Xiang
Quoted 6 lines
lib/inode.c +1
-0+return ERR_PTR (-errno); ret =write_uncompressed_file_from_fd (inode, fd); }Signature · 1 line
2.17.1
Quoted 133 lines · On Tue, 13 Dec 2022 16:27:37 +0800 Gao Xiang <[email protected]>…
(cc Khem Raj <[email protected]>... Since I'd like to apply this first
and then enable _FILE_OFFSET_BITS=64 unconditionally so that we could clean up all hard-coded_FILE_OFFSET_BITSin the source code.)Hi Yue,
Quoted 50 lines · Yue Hu
From: Yue Hu <[email protected]>
The return value of
ftell()is a long int, use ftello{64} instead. Also, use lseek64 for large file position to avoid this error if we have it. And need to return at once if they fail.Signed-off-by Yue Hu <[email protected]>--- configure.ac | 2 ++ lib/fragments.c | 34 +++++++++++++++++++++++++++++----- lib/inode.c | 9 +++++++-- 3 files changed, 38 insertions(+), 7 deletions(-)configure.ac +2
-0@@ -190,6 +190,8 @@ AC_CHECK_FUNCS(m4_flatten([ llistxattr memset realpath + lseek64 + ftello64 pread64 pwrite64 strduplib/fragments.c +11
-0@@ -3,7 +3,18 @@* Copyright (C), 2022, Coolpad Group Limited. * Created by Yue Hu <[email protected]> */ +#ifndef _LARGEFILE_SOURCE +#define _LARGEFILE_SOURCE +#endif +#ifndef _LARGEFILE64_SOURCE +#define _LARGEFILE64_SOURCE +#endif +#ifndef _FILE_OFFSET_BITS +#define _FILE_OFFSET_BITS 64 +#endif +#ifndef _GNU_SOURCE#define _GNU_SOURCE +#endif #include <stdlib.h> #include <unistd.h> #include "erofs/err.h" lib/fragments.c static struct list_head dupli_frags[FRAGMENT_HASHSIZE]; · line 49
Quoted 9 lines
lib/fragments.c +6
-0@@ -30,6 +41,13 @@ static struct list_head dupli_frags[FRAGMENT_HASHSIZE];static FILE *packedfile;const char *frags_packedname ="packed_file" ; +#ifndef HAVE_LSEEK64 +static inline off_t lseek64 (int fd,u64 offset,int set) +{ +return lseek (fd, offset, set); +} +#endif Could we use another function name other than native lseek64?
Quoted 48 lines
lib/fragments.c +12
-5+static int z_erofs_fragments_dedupe_find (struct erofs_inode *inode,int fd,u32 crc) {@@ -53,8 +71,7 @@ static int z_erofs_fragments_dedupe_find(struct erofs_inode *inode, int fd,if (!data)return -ENOMEM ; - ret =lseek (fd, inode->i_size - length,SEEK_SET ); -if (ret <0 ) { +if (lseek64 (fd, inode->i_size - length,SEEK_SET ) <0 ) { ret = -errno; goto out; }@@ -113,8 +130,7 @@ int z_erofs_fragments_dedupe(struct erofs_inode *inode, int fd, u32 *tofcrc)if (inode->i_size <=EROFS_TOF_HASHLEN )return 0 ; - ret =lseek (fd, inode->i_size -EROFS_TOF_HASHLEN ,SEEK_SET ); -if (ret <0 ) +if (lseek64 (fd, inode->i_size -EROFS_TOF_HASHLEN ,SEEK_SET ) <0 )return -errno; ret =read (fd, data_to_hash,EROFS_TOF_HASHLEN );@@ -192,9 +208,17 @@ void z_erofs_fragments_commit(struct erofs_inode *inode)int z_erofs_pack_fragments (struct erofs_inode *inode,void *data,unsigned int len,u32 tofcrc) { +#ifdef HAVE_FTELLO64 +off64_t offset =ftello64 (packedfile); +#else +off_t offset =ftello (packedfile); +#endif int ret; - inode->fragmentoff =ftell (packedfile); +if (offset <0 ) +return -errno; + + inode->fragmentoff = (erofs_off_t )offset; inode->fragment_size = len;if (fwrite (data, len,1 , packedfile) !=1 )lib/inode.c +0
-0lib/inode.c struct erofs_inode *erofs_mkfs_build_special_from_fd(int fd, const char *name) · line 1201
Quoted 7 lines
lib/inode.c +3
-1@@ -1196,7 +1196,10 @@ struct erofs_inode *erofs_mkfs_build_special_from_fd(int fd, const char *name)struct erofs_inode *inode;int ret; -lseek (fd,0 ,SEEK_SET ); + ret =lseek (fd,0 ,SEEK_SET ); +if (ret <0 ) +return ERR_PTR (-errno);This should be in a seperate patch, please don't fix it here.
Quoted 6 lines
lib/inode.c +1
-0+ ret =fstat64 (fd, &st);if (ret)return ERR_PTR (-errno);@@ -1223,7 +1226,9 @@ struct erofs_inode *erofs_mkfs_build_special_from_fd(int fd, const char *name)
Quoted 7 lines
lib/inode.c struct erofs_inode *erofs_mkfs_build_special_from_fd(int fd, const char *name) · line 1230
Quoted 5 lines
lib/inode.c +2
-1ret =erofs_write_compressed_file (inode, fd);if (ret == -ENOSPC ) { -lseek (fd,0 ,SEEK_SET ); + ret =lseek (fd,0 ,SEEK_SET ); +if (ret <0 )Same here.
Okay, will update for all in v2.
Quoted 10 lines
Thanks,
Gao XiangQuoted 6 lines
lib/inode.c +1
-0+return ERR_PTR (-errno); ret =write_uncompressed_file_from_fd (inode, fd); }Signature · 1 line
2.17.1