xfsdump: enable dump header checksums
PATCH v2 1/1 xfsdump: enable dump header checksums
Various structures in a dump file optionally contain a checksum, but the code to compute and validate the checksum has not been enabled. The checksum code has a negligible performance impact and so this patch enables the checksum code unconditionally. Also:
- make sure all header sizes are multiples of 4 bytes (a requirement of the checksum routine)
- zero structures to ensure internal padding has a known value
- fix a bug in dump_extattr_buildrecord() which checksummed the wrong header structure
- add calc_checksum() and is_checksum_valid() routines to cut down on duplicate code
--- common/content_inode.h | 25 ++++++++++++++ dump/content.c | 85 +++++++++++------------------------------------- restore/Makefile | 2 +- restore/content.c | 40 ++-------------------- 4 files changed, 49 insertions(+), 103 deletions(-)
common/content_inode.h +25 -0
@@ -347,4 +347,29 @@ typedef struct extattrhdr extattrhdr_t;/* a linux "secure" mode attribute */ +/* Routines for calculating and validating checksums on xfsdump headers. +* The header length must be an integral number of u_int32_t's. +*/ +static inline u_int32_t +calc_checksum (void *bufp,size_t len) +{ +u_int32_t sum =0 ; +u_int32_t *sump = (u_int32_t *)bufp; +u_int32_t *endp = (void *)sump + len; +while (sump < endp) + sum += *sump++; +return ~sum +1 ; +} + +static inline bool_t +is_checksum_valid (void *bufp,size_t len) +{ +u_int32_t sum =0 ; +u_int32_t *sump = (u_int32_t *)bufp; +u_int32_t *endp = (void *)sump + len; +while (sump < endp) + sum += *sump++; +return sum ==0 ?BOOL_TRUE :BOOL_FALSE ; +} +#endif /* CONTENT_INODE_H */
dump/content.c +19 -66
175 lines not on this page · raw message
restore/Makefile +1 -1
@@ -103,7 +103,7 @@ LLDLIBS = $(LIBUUID) $(LIBHANDLE) $(LIBATTR) $(LIBRMT) LTDEPENDENCIES = $(LIBRMT) LCFLAGS = -DRESTORE -DRMT -DBASED -DDOSOCKS -DINVCONVFIX -DPIPEINVFIX \ - -DEOMFIX -DSESSCPLT -DWHITEPARSE -DDIRENTHDR_CHECKSUM \ + -DEOMFIX -DSESSCPLT -DWHITEPARSE \ -DF_FSSETDM default: depend $(LTCOMMAND)
restore/content.c +4 -36
@@ -8000,11 +8000,6 @@ read_filehdr( drive_t *drivep, filehdr_t *fhdrp, bool_t fhcs )drive_ops_t *dop = drivep->d_opsp;/* REFERENCED */ intgen_t nread; -#ifdef FILEHDR_CHECKSUM - registeru_int32_t *sump = (u_int32_t * )fhdrp; - registeru_int32_t *endp = (u_int32_t * )( fhdrp +1 ); - registeru_int32_t sum; -#endif /* FILEHDR_CHECKSUM */ intgen_t rval;filehdr_t tmpfh;
@@ -8041,21 +8036,18 @@ read_filehdr( drive_t *drivep, filehdr_t *fhdrp, bool_t fhcs ) bstatp->bs_ino, bstatp->bs_mode ); -#ifdef FILEHDR_CHECKSUM if ( fhcs ) {if ( ! ( fhdrp->fh_flags &FILEHDR_FLAGS_CHECKSUM )) {mlog (MLOG_NORMAL |MLOG_WARNING ,_ ("corrupt file header\n" ) );return RV_CORRUPT ; } -for ( sum =0 ; sump < endp ; sum += *sump++ ) ; -if ( sum ) { +if ( !is_checksum_valid ( fhdrp,FILEHDR_SZ )) {mlog (MLOG_NORMAL |MLOG_WARNING ,_ ("bad file header checksum\n" ) );return RV_CORRUPT ; } } -#endif /* FILEHDR_CHECKSUM */ return RV_OK ; }
@@ -8067,11 +8059,6 @@ read_extenthdr( drive_t *drivep, extenthdr_t *ehdrp, bool_t ehcs )drive_ops_t *dop = drivep->d_opsp;/* REFERENCED */ intgen_t nread; -#ifdef EXTENTHDR_CHECKSUM - registeru_int32_t *sump = (u_int32_t * )ehdrp; - registeru_int32_t *endp = (u_int32_t * )( ehdrp +1 ); - registeru_int32_t sum; -#endif /* EXTENTHDR_CHECKSUM */ intgen_t rval;extenthdr_t tmpeh;
@@ -8108,21 +8095,18 @@ read_extenthdr( drive_t *drivep, extenthdr_t *ehdrp, bool_t ehcs ) ehdrp->eh_type, ehdrp->eh_flags ); -#ifdef EXTENTHDR_CHECKSUM if ( ehcs ) {if ( ! ( ehdrp->eh_flags &EXTENTHDR_FLAGS_CHECKSUM )) {mlog (MLOG_NORMAL |MLOG_WARNING ,_ ("corrupt extent header\n" ) );return RV_CORRUPT ; } -for ( sum =0 ; sump < endp ; sum += *sump++ ) ; -if ( sum ) { +if ( !is_checksum_valid ( ehdrp,EXTENTHDR_SZ )) {mlog (MLOG_NORMAL |MLOG_WARNING ,_ ("bad extent header checksum\n" ) );return RV_CORRUPT ; } } -#endif /* EXTENTHDR_CHECKSUM */ return RV_OK ; }
@@ -8137,11 +8121,6 @@ read_dirent( drive_t *drivep,drive_ops_t *dop = drivep->d_opsp;/* REFERENCED */ intgen_t nread; -#ifdef DIRENTHDR_CHECKSUM - registeru_int32_t *sump = (u_int32_t * )dhdrp; - registeru_int32_t *endp = (u_int32_t * )( dhdrp +1 ); - registeru_int32_t sum; -#endif /* DIRENTHDR_CHECKSUM */ intgen_t rval;direnthdr_t tmpdh;
@@ -8180,21 +8159,18 @@ read_dirent( drive_t *drivep, (size_t )dhdrp->dh_gen, ( size_t )dhdrp->dh_sz ); -#ifdef DIRENTHDR_CHECKSUM if ( dhcs ) {if ( dhdrp->dh_sz ==0 ) {mlog (MLOG_NORMAL |MLOG_WARNING ,_ ("corrupt directory entry header\n" ) );return RV_CORRUPT ; } -for ( sum =0 ; sump < endp ; sum += *sump++ ) ; -if ( sum ) { +if ( !is_checksum_valid ( dhdrp,DIRENTHDR_SZ )) {mlog (MLOG_NORMAL |MLOG_WARNING ,_ ("bad directory entry header checksum\n" ) );return RV_CORRUPT ; } } -#endif /* DIRENTHDR_CHECKSUM */ /* if null, return */
@@ -8246,11 +8222,6 @@ read_extattrhdr( drive_t *drivep, extattrhdr_t *ahdrp, bool_t ahcs )drive_ops_t *dop = drivep->d_opsp;/* REFERENCED */ intgen_t nread; -#ifdef EXTATTRHDR_CHECKSUM - registeru_int32_t *sump = (u_int32_t * )ahdrp; - registeru_int32_t *endp = (u_int32_t * )( ahdrp +1 ); - registeru_int32_t sum; -#endif /* EXTATTRHDR_CHECKSUM */ intgen_t rval;extattrhdr_t tmpah;
@@ -8288,21 +8259,18 @@ read_extattrhdr( drive_t *drivep, extattrhdr_t *ahdrp, bool_t ahcs ) ahdrp->ah_valsz, ahdrp->ah_checksum ); -#ifdef EXTATTRHDR_CHECKSUM if ( ahcs ) {if ( ! ( ahdrp->ah_flags &EXTATTRHDR_FLAGS_CHECKSUM )) {mlog (MLOG_NORMAL |MLOG_WARNING ,_ ("corrupt extattr header\n" ) );return RV_CORRUPT ; } -for ( sum =0 ; sump < endp ; sum += *sump++ ) ; -if ( sum ) { +if ( !is_checksum_valid ( ahdrp,EXTATTRHDR_SZ )) {mlog (MLOG_NORMAL |MLOG_WARNING ,_ ("bad extattr header checksum\n" ) );return RV_CORRUPT ; } } -#endif /* EXTATTRHDR_CHECKSUM */ return RV_OK ; }
Signature · 6 lines
1.7.0.4 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Quoted 13 lines · Bill Kendall
Various structures in a dump file optionally contain a checksum, but the code to compute and validate the checksum has not been enabled. The checksum code has a negligible performance impact and so this patch enables the checksum code unconditionally. Also:
- make sure all header sizes are multiples of 4 bytes (a requirement of the checksum routine)
- zero structures to ensure internal padding has a known value
- fix a bug indump_extattr_buildrecord()which checksummed the wrong header structure
- addcalc_checksum()andis_checksum_valid()routines to cut down on duplicate code
Quoted 1 line
Signed-off-by Bill Kendall <[email protected]>
This looks good. I have a few comments for you below but unless you decide to send me an update I'll use this as-is.
Quoted 14 lines
--- common/content_inode.h | 25 ++++++++++++++ dump/content.c | 85 +++++++++++------------------------------------- restore/Makefile | 2 +- restore/content.c | 40 ++-------------------- 4 files changed, 49 insertions(+), 103 deletions(-)common/content_inode.h +0
-0@@ -347,4 +347,29 @@ typedef struct extattrhdr extattrhdr_t;/* a linux "secure" mode attribute */
common/content_inode.h typedef struct extattrhdr extattrhdr_t; · line 358
Quoted 10 lines
common/content_inode.h +9
-0+/* Routines for calculating and validating checksums on xfsdump headers. +* The header length must be an integral number of u_int32_t's. +*/ +static inline u_int32_t +calc_checksum (void *bufp,size_t len) +{ +u_int32_t sum =0 ; +u_int32_t *sump = (u_int32_t *)bufp; +u_int32_t *endp = (void *)sump + len
No need to cast a (void *) object to another pointer type (and vice-versa).
And although gcc allows arithmetic on void pointers, it is not standard, so (char *) would be a more portable choice.
The multiple-of-4 assumption would be well stated with an assertion.
common/content_inode.h typedef struct extattrhdr extattrhdr_t; · line 366
Quoted 9 lines
common/content_inode.h +8
-0; +while (sump < endp) + sum += *sump++; +return ~sum +1 ; +} + +static inline bool_t +is_checksum_valid (void *bufp,size_t len) +{
. . .
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Quoted 11 lines · Alex Elder
common/content_inode.h typedef struct extattrhdr extattrhdr_t;
Quoted 6 lines · Bill Kendall
common/content_inode.h +6
-0+static inline u_int32_t +calc_checksum (void *bufp,size_t len) +{ +u_int32_t sum =0 ; +u_int32_t *sump = (u_int32_t *)bufp; +u_int32_t *endp = (void *)sump + lenNo need to cast a (void *) object to another
pointer type (and vice-versa).
Quoted 3 lines
And although gcc allows arithmetic on void pointers, it is not standard, so (char *) would be a more portable choice.
Ha...I couldn't remember and when gcc didn't complain I went with it. Using (char *) requires the result to be cast back:
u_int32_t *endp = (u_int32_t *)((char *)sump + len)
This is a bit cleaner:
u_int32_t *endp = sump + len / sizeof(u_int32_t);
Quoted 3 lines
The multiple-of-4 assumption would be well stated with an assertion.
I didn't want to add the extra cycles to a checksum routine, but realistically it won't affect the run time of dump or restore and asserts are likely disabled on released binaries anyway. I'll add the assert here and remove the ones done during initialization.
Bill
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs