From: Darrick J. Wong <djwong@xxxxxxxxxx> If the fuse2fs object that libfuse hands to us has the wrong magic number, something is clearly wrong. In that case, we don't really want to rely on the global_fs variable actually pointing to a valid ext2_filsys object because something could be wrong there too. Instead, try to emit an error on stderr and just bail out. Signed-off-by: "Darrick J. Wong" <djwong@xxxxxxxxxx> --- misc/fuse2fs.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/misc/fuse2fs.c b/misc/fuse2fs.c index 2368c87ef0b614..138e3292b2de09 100644 --- a/misc/fuse2fs.c +++ b/misc/fuse2fs.c @@ -175,9 +175,23 @@ struct fuse2fs { return translate_error((fs), 0, EXT2_ET_FILESYSTEM_CORRUPTED); \ } while (0) -#define FUSE2FS_CHECK_CONTEXT(ptr) do {if ((ptr)->magic != FUSE2FS_MAGIC) \ - return translate_error(global_fs, 0, EXT2_ET_FILESYSTEM_CORRUPTED); \ -} while (0) +#define __FUSE2FS_CHECK_CONTEXT(ff, retcode) \ + do { \ + if ((ff)->magic != FUSE2FS_MAGIC) { \ + fprintf(stderr, \ + "FUSE2FS: Corrupt in-memory data at %s:%d!\n", \ + __func__, __LINE__); \ + fflush(stderr); \ + retcode; \ + } \ + } while (0) + +#define FUSE2FS_CHECK_CONTEXT(ff) \ + __FUSE2FS_CHECK_CONTEXT((ff), return -EUCLEAN) +#define FUSE2FS_CHECK_CONTEXT_NORET(ff) \ + __FUSE2FS_CHECK_CONTEXT((ff), return) +#define FUSE2FS_CHECK_CONTEXT_NULL(ff) \ + __FUSE2FS_CHECK_CONTEXT((ff), return NULL) static int __translate_error(ext2_filsys fs, ext2_ino_t ino, errcode_t err, const char *file, int line); @@ -670,10 +684,8 @@ static void op_destroy(void *p EXT2FS_ATTR((unused))) ext2_filsys fs; errcode_t err; - if (ff->magic != FUSE2FS_MAGIC) { - translate_error(global_fs, 0, EXT2_ET_BAD_MAGIC); - return; - } + + FUSE2FS_CHECK_CONTEXT_NORET(ff); fs = ff->fs; dbg_printf(ff, "%s: dev=%s\n", __func__, fs->device_name); if (fs->flags & EXT2_FLAG_RW) { @@ -722,10 +734,7 @@ static void *op_init(struct fuse_conn_info *conn ext2_filsys fs; errcode_t err; - if (ff->magic != FUSE2FS_MAGIC) { - translate_error(global_fs, 0, EXT2_ET_BAD_MAGIC); - return NULL; - } + FUSE2FS_CHECK_CONTEXT_NULL(ff); fs = ff->fs; dbg_printf(ff, "%s: dev=%s\n", __func__, fs->device_name); #ifdef FUSE_CAP_IOCTL_DIR @@ -4785,13 +4794,10 @@ static int __translate_error(ext2_filsys fs, ext2_ino_t ino, errcode_t err, ret = -ENOENT; #endif break; - /* Sometimes fuse returns a garbage file handle pointer to us... */ - case EXT2_ET_MAGIC_EXT2_FILE: - ret = -EFAULT; - break; case EXT2_ET_UNIMPLEMENTED: ret = -EOPNOTSUPP; break; + case EXT2_ET_MAGIC_EXT2_FILE: case EXT2_ET_MAGIC_EXT2FS_FILSYS: case EXT2_ET_MAGIC_BADBLOCKS_LIST: case EXT2_ET_MAGIC_BADBLOCKS_ITERATE: