Skip to contentSkip to conversation

xfs_repair: detect null buf passed to duration

PATCH Superseded Darrick J. Wong 3 participants 8 messages May 31, 2024 – Jun 5, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs_repair: detect null buf passed to duration

From: Darrick J. Wong <[email protected]>

gcc 12.2 with ubsan and fortify turned on complains about this:

In file included from /usr/include/stdio.h:906,

                 from ../include/platform_defs.h:9,
                 from ../include/libxfs.h:16,
                 from progress.c:3:
In function ‘sprintf’,
    inlined from ‘duration’ at progress.c:443:4:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:30:10: error: null destination pointer [-Werror=format-overflow=]
   30 |   return __builtin___sprintf_chk (__s, __USE_FORTIFY_LEVEL - 1,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   31 |                                   __glibc_objsize (__s), __fmt,
      |                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   32 |                                   __va_arg_pack ());
      |                                   ~~~~~~~~~~~~~~~~~

I think this is a false negative since all callers are careful not to pass in a null pointer. Unfortunately the compiler cannot detect that since this isn't a static function and complains. Fix this by adding an explicit null check.

---
 repair/progress.c |    3 +++
 1 file changed, 3 insertions(+)
repair/progress.c +3 -0
index 084afa63c121..e13494e0ed23 100644
@@ -435,6 +435,9 @@ duration(time_t length, char *buf)
 	int seconds;
 	char temp[128];
 
+	if (!buf)
+		return NULL;
+
 	*buf = '\0';
 	weeks = days = hours = minutes = seconds = sum = 0;
 	if (length >= ONEWEEK) {
#1 lore raw
Quoted 2 lines · Darrick J. Wong

I think this is a false negative since all callers are careful not to pass in a null pointer.

Yes.

Quoted 3 lines

Unfortunately the compiler cannot detect that
since this isn't a static function and complains. Fix this by adding an explicit null check.

Can you try adding a __attribute__((nonnull(2))) to the declaration like this?

repair/progress.h +1 -1
index 0b06b2c4f..c09aa6941 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(struct xfs_mount *mp, int end, int phase, char *buf);
-extern char *duration(time_t val, char *buf);
+char *duration(time_t val, char *buf) __attribute__((nonnull(2)));
 extern int do_parallel;
 
 #define	PROG_RPT_INC(a,b) if (ag_stride && prog_rpt_done) (a) += (b)
#2 lore raw
Quoted 4 lines · Christoph Hellwig
Quoted 2 lines · Darrick J. Wong

I think this is a false negative since all callers are careful not to pass in a null pointer.

Quoted 8 lines

Yes.

Quoted 3 lines

Unfortunately the compiler cannot detect that
since this isn't a static function and complains. Fix this by adding an explicit null check.

Can you try adding a __attribute__((nonnull(2))) to the declaration like this?

Seems to work, I'll send in a v2.

--D

Quoted 14 lines
repair/progress.h +1 -1
index 0b06b2c4f..c09aa6941 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(struct xfs_mount *mp, int end, int phase, char *buf);
-extern char *duration(time_t val, char *buf);
+char *duration(time_t val, char *buf) __attribute__((nonnull(2)));
 extern int do_parallel;
 
 #define	PROG_RPT_INC(a,b) if (ag_stride && prog_rpt_done) (a) += (b)
#3 lore raw

PATCH v2 1/1 xfs_repair: detect null buf passed to duration

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

gcc 12.2 with ubsan and fortify turned on complains about this:

In file included from /usr/include/stdio.h:906,

                 from ../include/platform_defs.h:9,
                 from ../include/libxfs.h:16,
                 from progress.c:3:
In function ‘sprintf’,
    inlined from ‘duration’ at progress.c:443:4:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:30:10: error: null destination pointer [-Werror=format-overflow=]
   30 |   return __builtin___sprintf_chk (__s, __USE_FORTIFY_LEVEL - 1,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   31 |                                   __glibc_objsize (__s), __fmt,
      |                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   32 |                                   __va_arg_pack ());
      |                                   ~~~~~~~~~~~~~~~~~

I think this is a false negative since all callers are careful not to pass in a null pointer. Unfortunately the compiler cannot detect that since this isn't a static function and complains. Fix this by adding an explicit declaration that buf isn't null.

---
 repair/progress.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
repair/progress.h +1 -1
index 0b06b2c4f43f..c09aa69413ac 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(struct xfs_mount *mp, int end, int phase, char *buf);
-extern char *duration(time_t val, char *buf);
+char *duration(time_t val, char *buf) __attribute__((nonnull(2)));
 extern int do_parallel;
 
 #define	PROG_RPT_INC(a,b) if (ag_stride && prog_rpt_done) (a) += (b)
#4 lore raw

repair/progress.h extern void summary_report(void); · line 41

Quoted 11 lines
repair/progress.h +1 -1
index 0b06b2c4f43f..c09aa69413ac 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(struct xfs_mount *mp, int end, int phase, char *buf);
-extern char *duration(time_t val, char *buf);
+char *duration(time_t val, char *buf) __attribute__((nonnull(2)));

Once nonnull() is used here, shouldn't we also set -Wnonnull to CFLAGS?

Please don't take it as a review, it's just a question that came to my mind as I don't fully understand the implications of using nonnull here.

Carlos

#5 lore raw
Quoted 13 lines · Carlos Maiolino

repair/progress.h extern void summary_report(void); · line 41

Quoted 11 lines
repair/progress.h +1 -1
index 0b06b2c4f43f..c09aa69413ac 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(struct xfs_mount *mp, int end, int phase, char *buf);
-extern char *duration(time_t val, char *buf);
+char *duration(time_t val, char *buf) __attribute__((nonnull(2)));

Once nonnull() is used here, shouldn't we also set -Wnonnull to CFLAGS?

Already set via -Wall, at least if you're using gcc 12.2:

-Wnonnull
Warn about passing a null pointer for arguments marked as requiring a non-null value by the "nonnull" function attribute.

-Wnonnull is included in -Wall and -Wformat. It can be disabled with the -Wno-nonnull option.

https://git.kernel.org/pub/scm/fs/xfs/xfsprogs-dev.git/tree/include/builddefs.in?h=for-next#n113

Quoted 2 lines

Please don't take it as a review, it's just a question that came to my mind as I don't fully understand the implications of using nonnull here.

<nod>

--D

Quoted 2 lines

Carlos

Reviewed-by
#6 lore raw
Quoted 17 lines · Darrick J. Wong
Quoted 13 lines · Carlos Maiolino

repair/progress.h extern void summary_report(void); · line 41

Quoted 11 lines
repair/progress.h +1 -1
index 0b06b2c4f43f..c09aa69413ac 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(struct xfs_mount *mp, int end, int phase, char *buf);
-extern char *duration(time_t val, char *buf);
+char *duration(time_t val, char *buf) __attribute__((nonnull(2)));

Once nonnull() is used here, shouldn't we also set -Wnonnull to CFLAGS?

Already set via -Wall, at least if you're using gcc 12.2:

Quoted 7 lines

-Wnonnull
Warn about passing a null pointer for arguments marked as requiring a non-null value by the "nonnull" function attribute.

-Wnonnull is included in -Wall and -Wformat. It can be disabled with the -Wno-nonnull option.

Ok, thanks for letting me know :) feel free to add:

Quoted 12 lines

https://git.kernel.org/pub/scm/fs/xfs/xfsprogs-dev.git/tree/include/builddefs.in?h=for-next#n113

Quoted 2 lines

Please don't take it as a review, it's just a question that came to my mind as I don't fully understand the implications of using nonnull here.

<nod>

--D

Quoted 2 lines

Carlos