misc: clean up undo file setup
PATCH 1/1 misc: clean up undo file setup
Fix Coverity bugs 1297094-1297101 by fixing all the mutations in the *_setup_tdb() functions, fixing buffer overflows, and checking return values.
Signed-off-by Darrick J. Wong <[email protected]>
--- debugfs/debugfs.c | 29 ++++++++++++------------ e2fsck/unix.c | 16 +++++++++++-- misc/e2undo.c | 64 +++++++++++++++++++++++++++++++++-------------------- misc/mke2fs.c | 12 +++++++--- misc/tune2fs.c | 63 +++++++++++++++++++++++++++++----------------------- resize/main.c | 29 ++++++++++++------------ 6 files changed, 125 insertions(+), 88 deletions(-)
debugfs/debugfs.c +14 -15
@@ -55,11 +55,12 @@ static int debugfs_setup_tdb(const char *device_name, char *undo_file,errcode_t retval =ENOMEM ;char *tdb_dir =NULL , *tdb_file =NULL ;char *dev_name, *tmp_name; -int free_tdb_dir =0 ;/* (re)open a specific undo file */ if (undo_file && undo_file[0 ] !=0 ) { -set_undo_io_backing_manager (*io_ptr); + retval =set_undo_io_backing_manager (*io_ptr); +if (retval) + goto err; *io_ptr = undo_io_manager; retval =set_undo_io_backup_file (undo_file);if (retval)
@@ -68,7 +69,7 @@ static int debugfs_setup_tdb(const char *device_name, char *undo_file,"using the command:\n" " e2undo %s %s\n\n" , undo_file, device_name); -return 0 ; +return retval; }/*
@@ -76,19 +77,18 @@ static int debugfs_setup_tdb(const char *device_name, char *undo_file,* nice */ tdb_dir =getenv ("E2FSPROGS_UNDO_DIR" ); +if (!tdb_dir) + tdb_dir ="/var/lib/e2fsprogs" ; -if (tdb_dir ==NULL || !strcmp (tdb_dir,"none" ) || (tdb_dir[0 ] ==0 ) || -access (tdb_dir,W_OK )) { -if (free_tdb_dir) -free (tdb_dir); +if (!strcmp (tdb_dir,"none" ) || (tdb_dir[0 ] ==0 ) || +access (tdb_dir,W_OK ))return 0 ; - } tmp_name =strdup (device_name);if (!tmp_name) goto errout; dev_name =basename (tmp_name); - tdb_file =malloc (strlen (tdb_dir) +8 +strlen (dev_name) +7 +1 ); + tdb_file =malloc (strlen (tdb_dir) +9 +strlen (dev_name) +7 +1 );if (!tdb_file) {free (tmp_name); goto errout;
@@ -98,10 +98,14 @@ static int debugfs_setup_tdb(const char *device_name, char *undo_file,if ((unlink (tdb_file) <0 ) && (errno !=ENOENT )) { retval = errno; +com_err ("debugfs" , retval, +"while trying to delete %s" , tdb_file); goto errout; } -set_undo_io_backing_manager (*io_ptr); + retval =set_undo_io_backing_manager (*io_ptr); +if (retval) + goto errout; *io_ptr = undo_io_manager; retval =set_undo_io_backup_file (tdb_file);if (retval)
@@ -110,14 +114,9 @@ static int debugfs_setup_tdb(const char *device_name, char *undo_file,"using the command:\n" " e2undo %s %s\n\n", tdb_file, device_name); -if (free_tdb_dir) -free (tdb_dir);free (tdb_file);return 0 ; - errout: -if (free_tdb_dir) -free (tdb_dir);free (tdb_file); err:com_err ("debugfs" , retval,"while trying to setup undo file\n" );
e2fsck/unix.c +13 -3
@@ -682,6 +682,10 @@ static void parse_extended_opts(e2fsck_t ctx, const char *opts) } ctx->ext_attr_ver = ea_ver; }else if (strcmp (token,"readahead_kb" ) ==0 ) { +if (!arg) { + extended_usage++; +continue ; + } reada_kb =strtoull (arg, &p,0 );if (*p) { fprintf(stderr, "%s",
@@ -1238,7 +1242,9 @@ static int e2fsck_setup_tdb(e2fsck_t ctx, io_manager *io_ptr)/* (re)open a specific undo file */ if (ctx->undo_file && ctx->undo_file[0 ] !=0 ) { -set_undo_io_backing_manager (*io_ptr); + retval =set_undo_io_backing_manager (*io_ptr); +if (retval) + goto err; *io_ptr = undo_io_manager; retval =set_undo_io_backup_file (ctx->undo_file);if (retval)
@@ -1247,7 +1253,7 @@ static int e2fsck_setup_tdb(e2fsck_t ctx, io_manager *io_ptr)"using the command:\n" " e2undo %s %s\n\n"), ctx->undo_file, ctx->filesystem_name); -return 0 ; +return retval; }/*
@@ -1283,10 +1289,14 @@ static int e2fsck_setup_tdb(e2fsck_t ctx, io_manager *io_ptr)if ((unlink (tdb_file) <0 ) && (errno !=ENOENT )) { retval = errno; +com_err (ctx->program_name, retval, +_ ("while trying to delete %s" ), tdb_file); goto errout; } -set_undo_io_backing_manager (*io_ptr); + retval =set_undo_io_backing_manager (*io_ptr); +if (retval) + goto errout; *io_ptr = undo_io_manager; retval =set_undo_io_backup_file (tdb_file);if (retval)
misc/e2undo.c +40 -24
96 lines not on this page · raw message
misc/mke2fs.c +9 -3
@@ -2500,7 +2500,9 @@ static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)/* (re)open a specific undo file */ if (undo_file && undo_file[0 ] !=0 ) { -set_undo_io_backing_manager (*io_ptr); + retval =set_undo_io_backing_manager (*io_ptr); +if (retval) + goto err; *io_ptr = undo_io_manager; retval =set_undo_io_backup_file (undo_file);if (retval)
@@ -2508,7 +2510,7 @@ static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)printf (_ ("Overwriting existing filesystem; this can be undone " "using the command:\n" " e2undo %s %s\n\n" ), undo_file, name); -return 0 ; +return retval; }/*
@@ -2544,10 +2546,14 @@ static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)if ((unlink (tdb_file) <0 ) && (errno !=ENOENT )) { retval = errno; +com_err (program_name, retval, +_ ("while trying to delete %s" ), tdb_file); goto errout; } -set_undo_io_backing_manager (*io_ptr); + retval =set_undo_io_backing_manager (*io_ptr); +if (retval) + goto errout; *io_ptr = undo_io_manager; retval =set_undo_io_backup_file (tdb_file);if (retval)
misc/tune2fs.c +35 -28
100 lines not on this page · raw message
resize/main.c +14 -12
@@ -170,11 +170,12 @@ static int resize2fs_setup_tdb(const char *device_name, char *undo_file,errcode_t retval =ENOMEM ;char *tdb_dir =NULL , *tdb_file =NULL ;char *dev_name, *tmp_name; -int free_tdb_dir =0 ;/* (re)open a specific undo file */ if (undo_file && undo_file[0 ] !=0 ) { -set_undo_io_backing_manager (*io_ptr); + retval =set_undo_io_backing_manager (*io_ptr); +if (retval) + goto err; *io_ptr = undo_io_manager; retval =set_undo_io_backup_file (undo_file);if (retval)
@@ -183,7 +184,7 @@ static int resize2fs_setup_tdb(const char *device_name, char *undo_file,"using the command:\n" " e2undo %s %s\n\n"), undo_file, device_name); -return 0 ; +return retval; }/*
@@ -191,19 +192,18 @@ static int resize2fs_setup_tdb(const char *device_name, char *undo_file,* nice */ tdb_dir =getenv ("E2FSPROGS_UNDO_DIR" ); +if (!tdb_dir) + tdb_dir ="/var/lib/e2fsprogs" ; -if (tdb_dir ==NULL || !strcmp (tdb_dir,"none" ) || (tdb_dir[0 ] ==0 ) || -access (tdb_dir,W_OK )) { -if (free_tdb_dir) -free (tdb_dir); +if (!strcmp (tdb_dir,"none" ) || (tdb_dir[0 ] ==0 ) || +access (tdb_dir,W_OK ))return 0 ; - } tmp_name =strdup (device_name);if (!tmp_name) goto errout; dev_name =basename (tmp_name); - tdb_file =malloc (strlen (tdb_dir) +8 +strlen (dev_name) +7 +1 ); + tdb_file =malloc (strlen (tdb_dir) +11 +strlen (dev_name) +7 +1 );if (!tdb_file) {free (tmp_name); goto errout;
@@ -213,10 +213,14 @@ static int resize2fs_setup_tdb(const char *device_name, char *undo_file,if ((unlink (tdb_file) <0 ) && (errno !=ENOENT )) { retval = errno; +com_err (program_name, retval, +_ ("while trying to delete %s" ), tdb_file); goto errout; } -set_undo_io_backing_manager (*io_ptr); + retval =set_undo_io_backing_manager (*io_ptr); +if (retval) + goto errout; *io_ptr = undo_io_manager; retval =set_undo_io_backup_file (tdb_file);if (retval)
@@ -225,14 +229,9 @@ static int resize2fs_setup_tdb(const char *device_name, char *undo_file,"using the command:\n" " e2undo %s %s\n\n"), tdb_file, device_name); -if (free_tdb_dir) -free (tdb_dir);free (tdb_file);return 0 ;