xfs_repair: detect null buf passed to duration
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
@@ -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 ) {
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
@@ -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)
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@@ -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)
PATCH v2 1/1 xfs_repair: detect null buf passed to duration
In reply to Darrick J. WongFrom: 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
@@ -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)
repair/progress.h extern void summary_report(void); · line 41
Quoted 11 lines
repair/progress.h +1
-1@@ -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
Quoted 13 lines · Carlos Maiolino
repair/progress.h extern void summary_report(void); · line 41
Quoted 11 lines
repair/progress.h +1
-1@@ -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
Carlos Maiolino
Reviewed-byQuoted 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@@ -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
Looks good: