Skip to contentSkip to conversation

xfsprogs random fixes found by Coverity scan

PATCH Superseded Andrey Albershteyn 3 participants 8 messages Apr 17, 2024

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover xfsprogs random fixes found by Coverity scan

Hi all,

This is bunch of random fixes found by Coverity scan, there's memory leak, truncation of time_t to int, access overflow, and freeing of uninitialized struct.

v3:
- better error message for too long runtime
v2:
- remove parentheses
- drop count initialization patch as this code goes away with parent pointers
- rename unload: label
- howlong limit

--
Andrey

Andrey Albershteyn (4):
xfs_db: fix leak in flist_find_ftyp()
xfs_repair: make duration take time_t
xfs_scrub: don't call phase_end if phase_rusage was not initialized
xfs_fsr: convert fsrallfs to use time_t instead of int

 db/flist.c          |  4 +++-
 fsr/xfs_fsr.c       | 10 ++++++++--
 repair/globals.c    |  2 +-
 repair/globals.h    |  2 +-
 repair/progress.c   |  7 ++++---
 repair/progress.h   |  2 +-
 repair/xfs_repair.c |  2 +-
 scrub/xfs_scrub.c   |  3 ++-
 8 files changed, 21 insertions(+), 11 deletions(-)
Signature · 1 line
2.42.0
#1 lore raw

PATCH v3 1/4 xfs_db: fix leak in flist_find_ftyp()

When count is zero fl reference is lost. Fix it by freeing the list.

Fixes a0d79cb37a36 ("xfs_db: make flist_find_ftyp() to check for field existance on disk")
---
 db/flist.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
db/flist.c +3 -1
index c81d229ab99c..0a6cc5fcee43 100644
@@ -424,8 +424,10 @@ flist_find_ftyp(
 		if (f->ftyp == type)
 			return fl;
 		count = fcount(f, obj, startoff);
-		if (!count)
+		if (!count) {
+			flist_free(fl);
 			continue;
+		}
 		fa = &ftattrtab[f->ftyp];
 		if (fa->subfld) {
 			flist_t *nfl;
Signature · 1 line
2.42.0
#3 lore raw

PATCH v3 2/4 xfs_repair: make duration take time_t

In reply to Andrey Albershteyn

In most of the uses of duration() takes time_t instead of int. Convert the rest to use time_t and make duration() take time_t to not truncate it to int.

While at it remove unnecessary parentheses around 'elapsed'.

---
 repair/globals.c    | 2 +-
 repair/globals.h    | 2 +-
 repair/progress.c   | 7 ++++---
 repair/progress.h   | 2 +-
 repair/xfs_repair.c | 2 +-
 5 files changed, 8 insertions(+), 7 deletions(-)
repair/globals.c +1 -1
index c40849853b8f..7c819d70a0ab 100644
@@ -116,7 +116,7 @@ uint32_t	sb_width;
 struct aglock	*ag_locks;
 struct aglock	rt_lock;
 
-int		report_interval;
+time_t		report_interval;
 uint64_t	*prog_rpt_done;
 
 int		ag_stride;
repair/globals.h +1 -1
index 89f1b0e078f3..2d05c8b2c00f 100644
@@ -160,7 +160,7 @@ struct aglock {
 extern struct aglock	*ag_locks;
 extern struct aglock	rt_lock;
 
-extern int		report_interval;
+extern time_t		report_interval;
 extern uint64_t		*prog_rpt_done;
 
 extern int		ag_stride;
repair/progress.c +4 -3
index f6c4d988444e..5f80fb68ddfd 100644
@@ -268,12 +268,13 @@ progress_rpt_thread (void *p)
 				_("\t- %02d:%02d:%02d: Phase %d: elapsed time %s - processed %d %s per minute\n"),
 				tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
 				current_phase, duration(elapsed, msgbuf),
-				(int) (60*sum/(elapsed)), *msgp->format->type);
+				(int) (60*sum/elapsed), *msgp->format->type);
 			do_log(
 	_("\t- %02d:%02d:%02d: Phase %d: %" PRIu64 "%% done - estimated remaining time %s\n"),
 				tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
 				current_phase, percent,
-				duration((int) ((*msgp->total - sum) * (elapsed)/sum), msgbuf));
+				duration((*msgp->total - sum) * elapsed/sum,
+					msgbuf));
 		}
 
 		if (pthread_mutex_unlock(&msgp->mutex) != 0) {
@@ -420,7 +421,7 @@ timestamp(int end, int phase, char *buf)
 }
 
 char *
-duration(int length, char *buf)
+duration(time_t length, char *buf)
 {
 	int sum;
 	int weeks;
repair/progress.h +1 -1
index 2c1690db1b17..9575df164aa0 100644
@@ -38,7 +38,7 @@ extern void summary_report(void);
 extern int  set_progress_msg(int report, uint64_t total);
 extern uint64_t print_final_rpt(void);
 extern char *timestamp(int end, int phase, char *buf);
-extern char *duration(int val, char *buf);
+extern char *duration(time_t val, char *buf);
 extern int do_parallel;
 
 #define	PROG_RPT_INC(a,b) if (ag_stride && prog_rpt_done) (a) += (b)
repair/xfs_repair.c +1 -1
index ba9d28330d82..2ceea87dc57d 100644
@@ -377,7 +377,7 @@ process_args(int argc, char **argv)
 			do_prefetch = 0;
 			break;
 		case 't':
-			report_interval = (int)strtol(optarg, NULL, 0);
+			report_interval = strtol(optarg, NULL, 0);
 			break;
 		case 'e':
 			report_corrected = true;
Signature · 1 line
2.42.0
#4 lore raw

Re: [PATCH v3 2/4] xfs_repair: make duration take time_t

repair/progress.c progress_rpt_thread (void *p) · line 271

Quoted 2 lines
repair/progress.c +1 -1
-				(int) (60*sum/(elapsed)), *msgp->format->type);
+				(int) (60*sum/elapsed), *msgp->format->type);

The case here is still superflous, and the space around the operators are still missing.

#5 lore raw

PATCH v3 3/4 xfs_scrub: don't call phase_end if phase_rusage was not initialized

In reply to Andrey Albershteyn

If unicrash_load() fails, all_pi can be used uninitialized in phase_end(). Fix it by going to the unload: section if unicrash_load fails and just go with unicrash_unload() (the is_service won't be initialized here).

---
 scrub/xfs_scrub.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
scrub/xfs_scrub.c +2 -1
index 752180d646ba..50565857ddd8 100644
@@ -631,7 +631,7 @@ main(
 		fprintf(stderr,
 	_("%s: couldn't initialize Unicode library.\n"),
 				progname);
-		goto out;
+		goto out_unicrash;
 	}
 
 	pthread_mutex_init(&ctx.lock, NULL);
@@ -828,6 +828,7 @@ out:
 	phase_end(&all_pi, 0);
 	if (progress_fp)
 		fclose(progress_fp);
+out_unicrash:
 	unicrash_unload();
 
 	/*
Signature · 1 line
2.42.0
#6 lore raw

PATCH v3 4/4 xfs_fsr: convert fsrallfs to use time_t instead of int

In reply to Andrey Albershteyn

Convert howlong argument to a time_t as it's truncated to int, but in practice this is not an issue as duration will never be this big.

Add check for howlong to fit into int (printf can use int format specifier). Even longer interval doesn't make much sense.

---
 fsr/xfs_fsr.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fsr/xfs_fsr.c +8 -2
index 3077d8f4ef46..02d61ef9399a 100644
@@ -72,7 +72,7 @@ static int  packfile(char *fname, char *tname, int fd,
 static void fsrdir(char *dirname);
 static int  fsrfs(char *mntdir, xfs_ino_t ino, int targetrange);
 static void initallfs(char *mtab);
-static void fsrallfs(char *mtab, int howlong, char *leftofffile);
+static void fsrallfs(char *mtab, time_t howlong, char *leftofffile);
 static void fsrall_cleanup(int timeout);
 static int  getnextents(int);
 int xfsrtextsize(int fd);
@@ -165,6 +165,12 @@ main(int argc, char **argv)
 			break;
 		case 't':
 			howlong = atoi(optarg);
+			if (howlong > INT_MAX) {
+				fprintf(stderr,
+				_("%s: the maximum runtime is %d seconds.\n"),
+					optarg, INT_MAX);
+				exit(1);
+			}
 			break;
 		case 'f':
 			leftofffile = optarg;
@@ -387,7 +393,7 @@ initallfs(char *mtab)
 }
 
 static void
-fsrallfs(char *mtab, int howlong, char *leftofffile)
+fsrallfs(char *mtab, time_t howlong, char *leftofffile)
 {
 	int fd;
 	int error;
Signature · 1 line
2.42.0
Reviewed-by on v3 4/4
#7 lore raw

Re: [PATCH v3 4/4] xfs_fsr: convert fsrallfs to use time_t instead of int

Quoted 7 lines · Andrey Albershteyn

Convert howlong argument to a time_t as it's truncated to int, but in practice this is not an issue as duration will never be this big.

Add check for howlong to fit into int (printf can use int format specifier). Even longer interval doesn't make much sense.

Looks good,

--D

Quoted 43 lines
---
 fsr/xfs_fsr.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fsr/xfs_fsr.c +8 -2
index 3077d8f4ef46..02d61ef9399a 100644
@@ -72,7 +72,7 @@ static int  packfile(char *fname, char *tname, int fd,
 static void fsrdir(char *dirname);
 static int  fsrfs(char *mntdir, xfs_ino_t ino, int targetrange);
 static void initallfs(char *mtab);
-static void fsrallfs(char *mtab, int howlong, char *leftofffile);
+static void fsrallfs(char *mtab, time_t howlong, char *leftofffile);
 static void fsrall_cleanup(int timeout);
 static int  getnextents(int);
 int xfsrtextsize(int fd);
@@ -165,6 +165,12 @@ main(int argc, char **argv)
 			break;
 		case 't':
 			howlong = atoi(optarg);
+			if (howlong > INT_MAX) {
+				fprintf(stderr,
+				_("%s: the maximum runtime is %d seconds.\n"),
+					optarg, INT_MAX);
+				exit(1);
+			}
 			break;
 		case 'f':
 			leftofffile = optarg;
@@ -387,7 +393,7 @@ initallfs(char *mtab)
 }
 
 static void
-fsrallfs(char *mtab, int howlong, char *leftofffile)
+fsrallfs(char *mtab, time_t howlong, char *leftofffile)
 {
 	int fd;
 	int error;
Signature · 1 line
2.42.0