From bef8c7fb70164091071c792016371869b9c50ad2 Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Sat, 6 Aug 2016 11:11:11 -0500 Subject: [PATCH] library: ensure that all those 'GET' macros are robust When users call the native 'get' functions they have a responsibility to check that the result struct address was indeed returned. But when using those 'GET' macros there was no protection for possible NULL dereference. So this patch will add some protection for a potential failure of an underlying 'get' function. And should it occur then those 'GET' macros will just return a zero. Plus, we'll also mirror that behavior in the debugging header should the XTRA_PROCPS_DEBUG #define be active. And, we might as well add a warning when invalid items are passed to 'GET' macros, just like we do for 'VAL'. [ lastly, we added the missing opening parens/braces ] [ to 2 'GET' macros in that xtra-procps-debug.h file ] [ which went unnoticed until the qa folks caught up. ] Signed-off-by: Jim Warner --- proc/diskstats.c | 4 ++++ proc/diskstats.h | 5 +++-- proc/meminfo.c | 4 ++++ proc/meminfo.h | 5 +++-- proc/slabinfo.c | 4 ++++ proc/slabinfo.h | 5 +++-- proc/stat.c | 4 ++++ proc/stat.h | 5 +++-- proc/vmstat.c | 4 ++++ proc/vmstat.h | 5 +++-- proc/xtra-procps-debug.h | 24 +++++++++++++++++------- 11 files changed, 52 insertions(+), 17 deletions(-) diff --git a/proc/diskstats.c b/proc/diskstats.c index 019b5fd6..6ed3f529 100644 --- a/proc/diskstats.c +++ b/proc/diskstats.c @@ -1000,6 +1000,10 @@ PROCPS_EXPORT struct diskstats_result *xtra_diskstats_get ( { struct diskstats_result *r = procps_diskstats_get(info, name, actual_enum); + if (actual_enum < 0 || actual_enum >= DISKSTATS_logical_end) { + fprintf(stderr, "%s line %d: invalid item = %d, type = %s\n" + , file, lineno, actual_enum, typestr); + } if (r) { char *str = Item_table[r->item].type2str; if (str[0] diff --git a/proc/diskstats.h b/proc/diskstats.h index cebbd46f..4d8683f9 100644 --- a/proc/diskstats.h +++ b/proc/diskstats.h @@ -89,8 +89,9 @@ struct diskstats_reap { #define DISKSTATS_TYPE_DISK -11111 #define DISKSTATS_TYPE_PARTITION -22222 -#define DISKSTATS_GET( info, name, actual_enum, type ) \ - procps_diskstats_get( info, name, actual_enum ) -> result . type +#define DISKSTATS_GET( info, name, actual_enum, type ) ( { \ + struct diskstats_result *r = procps_diskstats_get( info, actual_enum ); \ + r ? r->result . type : 0; } ) #define DISKSTATS_VAL( relative_enum, type, stack, info ) \ stack -> head [ relative_enum ] . result . type diff --git a/proc/meminfo.c b/proc/meminfo.c index fd58349d..e213dda9 100644 --- a/proc/meminfo.c +++ b/proc/meminfo.c @@ -872,6 +872,10 @@ PROCPS_EXPORT struct meminfo_result *xtra_meminfo_get ( { struct meminfo_result *r = procps_meminfo_get(info, actual_enum); + if (actual_enum < 0 || actual_enum >= MEMINFO_logical_end) { + fprintf(stderr, "%s line %d: invalid item = %d, type = %s\n" + , file, lineno, actual_enum, typestr); + } if (r) { char *str = Item_table[r->item].type2str; if (str[0] diff --git a/proc/meminfo.h b/proc/meminfo.h index 9b3b0192..bd214d48 100644 --- a/proc/meminfo.h +++ b/proc/meminfo.h @@ -137,8 +137,9 @@ struct meminfo_stack { }; -#define MEMINFO_GET( info, actual_enum, type ) \ - procps_meminfo_get( info, actual_enum ) -> result . type +#define MEMINFO_GET( info, actual_enum, type ) ( { \ + struct meminfo_result *r = procps_meminfo_get( info, actual_enum ); \ + r ? r->result . type : 0; } ) #define MEMINFO_VAL( relative_enum, type, stack, info ) \ stack -> head [ relative_enum ] . result . type diff --git a/proc/slabinfo.c b/proc/slabinfo.c index 985b5075..70023c15 100644 --- a/proc/slabinfo.c +++ b/proc/slabinfo.c @@ -1029,6 +1029,10 @@ PROCPS_EXPORT struct slabinfo_result *xtra_slabinfo_get ( { struct slabinfo_result *r = procps_slabinfo_get(info, actual_enum); + if (actual_enum < 0 || actual_enum >= SLABINFO_logical_end) { + fprintf(stderr, "%s line %d: invalid item = %d, type = %s\n" + , file, lineno, actual_enum, typestr); + } if (r) { char *str = Item_table[r->item].type2str; if (str[0] diff --git a/proc/slabinfo.h b/proc/slabinfo.h index 30ad06b6..c19fd5d1 100644 --- a/proc/slabinfo.h +++ b/proc/slabinfo.h @@ -95,8 +95,9 @@ struct slabinfo_reap { }; -#define SLABINFO_GET( info, actual_enum, type ) \ - procps_slabinfo_get( info, actual_enum ) -> result . type +#define SLABINFO_GET( info, actual_enum, type ) ( { \ + struct slabinfo_result *r = procps_slabinfo_get( info, actual_enum ); \ + r ? r->result . type : 0; } ) #define SLABINFO_VAL( relative_enum, type, stack, info ) \ stack -> head [ relative_enum ] . result . type diff --git a/proc/stat.c b/proc/stat.c index 003c1e53..e2168667 100644 --- a/proc/stat.c +++ b/proc/stat.c @@ -1147,6 +1147,10 @@ PROCPS_EXPORT struct stat_result *xtra_stat_get ( { struct stat_result *r = procps_stat_get(info, actual_enum); + if (actual_enum < 0 || actual_enum >= STAT_logical_end) { + fprintf(stderr, "%s line %d: invalid item = %d, type = %s\n" + , file, lineno, actual_enum, typestr); + } if (r) { char *str = Item_table[r->item].type2str; if (str[0] diff --git a/proc/stat.h b/proc/stat.h index e6f74a47..9dfad108 100644 --- a/proc/stat.h +++ b/proc/stat.h @@ -105,8 +105,9 @@ struct stat_reaped { #define STAT_SUMMARY_ID -11111 #define STAT_NODE_INVALID -22222 -#define STAT_GET( info, actual_enum, type ) \ - procps_stat_get( info, actual_enum ) -> result . type +#define STAT_GET( info, actual_enum, type ) ( { \ + struct stat_result *r = procps_stat_get( info, actual_enum ); \ + r ? r->result . type : 0; } ) #define STAT_VAL( relative_enum, type, stack, info ) \ stack -> head [ relative_enum ] . result . type diff --git a/proc/vmstat.c b/proc/vmstat.c index 3b21bdd3..686f261a 100644 --- a/proc/vmstat.c +++ b/proc/vmstat.c @@ -1268,6 +1268,10 @@ PROCPS_EXPORT struct vmstat_result *xtra_vmstat_get ( { struct vmstat_result *r = procps_vmstat_get(info, actual_enum); + if (actual_enum < 0 || actual_enum >= VMSTAT_logical_end) { + fprintf(stderr, "%s line %d: invalid item = %d, type = %s\n" + , file, lineno, actual_enum, typestr); + } if (r) { char *str = Item_table[r->item].type2str; if (str[0] diff --git a/proc/vmstat.h b/proc/vmstat.h index 483c0e28..1f67bfef 100644 --- a/proc/vmstat.h +++ b/proc/vmstat.h @@ -285,8 +285,9 @@ struct vmstat_stack { }; -#define VMSTAT_GET( info, actual_enum, type ) \ - procps_vmstat_get( info, actual_enum ) -> result . type +#define VMSTAT_GET( info, actual_enum, type ) ( { \ + struct vmstat_result *r = procps_vmstat_get( info, actual_enum ); \ + r ? r->result . type : 0; } ) #define VMSTAT_VAL( relative_enum, type, stack, info ) \ stack -> head [ relative_enum ] . result . type diff --git a/proc/xtra-procps-debug.h b/proc/xtra-procps-debug.h index 7a8eb4ac..2d4fc39f 100644 --- a/proc/xtra-procps-debug.h +++ b/proc/xtra-procps-debug.h @@ -35,8 +35,10 @@ struct diskstats_result *xtra_diskstats_get ( const char *file, int lineno); -#define DISKSTATS_GET( info, name, actual_enum, type ) \ - xtra_diskstats_get(info, name, actual_enum , STRINGIFY(type), __FILE__, __LINE__) -> result . type; } ) +#define DISKSTATS_GET( info, name, actual_enum, type ) ( { \ + struct diskstats_result *r; \ + r = xtra_diskstats_get(info, name, actual_enum , STRINGIFY(type), __FILE__, __LINE__); \ + r ? r->result . type : 0; } ) #endif // . . . . . . . . . . #ifdef DISKSTATS_VAL @@ -66,7 +68,9 @@ struct meminfo_result *xtra_meminfo_get ( int lineno); #define MEMINFO_GET( info, actual_enum, type ) ( { \ - xtra_meminfo_get(info, actual_enum , STRINGIFY(type), __FILE__, __LINE__) -> result . type; } ) + struct meminfo_result *r; \ + r = xtra_meminfo_get(info, actual_enum , STRINGIFY(type), __FILE__, __LINE__); \ + r ? r->result . type : 0; } ) #endif // . . . . . . . . . . #ifdef MEMINFO_VAL @@ -112,8 +116,10 @@ struct slabinfo_result *xtra_slabinfo_get ( const char *file, int lineno); -#define SLABINFO_GET( info, actual_enum, type ) \ - xtra_slabinfo_get(info, actual_enum , STRINGIFY(type), __FILE__, __LINE__) -> result . type; } ) +#define SLABINFO_GET( info, actual_enum, type ) ( { \ + struct slabinfo_result *r; \ + r = xtra_slabinfo_get(info, actual_enum , STRINGIFY(type), __FILE__, __LINE__); \ + r ? r->result . type : 0; } ) #endif // . . . . . . . . . . #ifdef SLABINFO_VAL @@ -143,7 +149,9 @@ struct stat_result *xtra_stat_get ( int lineno); #define STAT_GET( info, actual_enum, type ) ( { \ - xtra_stat_get(info, actual_enum , STRINGIFY(type), __FILE__, __LINE__) -> result . type; } ) + struct stat_result *r; \ + r = xtra_stat_get(info, actual_enum , STRINGIFY(type), __FILE__, __LINE__); \ + r ? r->result . type : 0; } ) #endif // . . . . . . . . . . #ifdef STAT_VAL @@ -173,7 +181,9 @@ struct vmstat_result *xtra_vmstat_get ( int lineno); #define VMSTAT_GET( info, actual_enum, type ) ( { \ - xtra_vmstat_get(info, actual_enum , STRINGIFY(type), __FILE__, __LINE__) -> result . type; } ) + struct vmstat_result *r; \ + r = xtra_vmstat_get(info, actual_enum , STRINGIFY(type), __FILE__, __LINE__); \ + r ? r->result . type : 0; } ) #endif // . . . . . . . . . . #ifdef VMSTAT_VAL