From 9f27e9d8d99b1caf870667df41de29e52ca51779 Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Mon, 8 Aug 2016 01:23:45 -0500 Subject: [PATCH] library: strengthen the VAL macro validation functions One ought not to assume that random memory access will always succeed or, when it does, that an obviously bad item enumerator will always be found at that location. Thus, this patch corrects some really poor assumptions associated with the 'xtra_procps_debug.h' header file. [ and it does so in somewhat contorted ways so as to ] [ avoid several darn gcc -Wnonnull warning messages! ] Signed-off-by: Jim Warner --- proc/diskstats.c | 22 ++++++++++++--------- proc/meminfo.c | 22 ++++++++++++--------- proc/pids.c | 22 ++++++++++++--------- proc/slabinfo.c | 22 ++++++++++++--------- proc/stat.c | 22 ++++++++++++--------- proc/vmstat.c | 22 ++++++++++++--------- proc/xtra-procps-debug.h | 42 +++++++++++++++++++++++----------------- 7 files changed, 102 insertions(+), 72 deletions(-) diff --git a/proc/diskstats.c b/proc/diskstats.c index 6ed3f529..55125ff8 100644 --- a/proc/diskstats.c +++ b/proc/diskstats.c @@ -1014,7 +1014,7 @@ PROCPS_EXPORT struct diskstats_result *xtra_diskstats_get ( } // end: xtra_diskstats_get_ -PROCPS_EXPORT void xtra_diskstats_val ( +PROCPS_EXPORT struct diskstats_result *xtra_diskstats_val ( int relative_enum, const char *typestr, const struct diskstats_stack *stack, @@ -1022,17 +1022,21 @@ PROCPS_EXPORT void xtra_diskstats_val ( const char *file, int lineno) { - struct diskstats_result *r; char *str; + int i; - r = &stack->head[relative_enum]; - if (r->item < 0 || r->item >= DISKSTATS_logical_end) { - fprintf(stderr, "%s line %d: invalid item = %d, relative_enum = %d, type = %s\n" - , file, lineno, r->item, relative_enum, typestr); - return; + for (i = 0; stack->head[i].item < DISKSTATS_logical_end; i++) + ; + if (relative_enum < 0 || relative_enum >= i) { + fprintf(stderr, "%s line %d: invalid relative_enum = %d, type = %s\n" + , file, lineno, relative_enum, typestr); + return NULL; } - str = Item_table[r->item].type2str; + str = Item_table[stack->head[relative_enum].item].type2str; if (str[0] - && (strcmp(typestr, str))) + && (strcmp(typestr, str))) { fprintf(stderr, "%s line %d: was %s, expected %s\n", file, lineno, typestr, str); + return NULL; + } + return &stack->head[relative_enum]; } // end: xtra_diskstats_val diff --git a/proc/meminfo.c b/proc/meminfo.c index e213dda9..42e1f9a7 100644 --- a/proc/meminfo.c +++ b/proc/meminfo.c @@ -886,7 +886,7 @@ PROCPS_EXPORT struct meminfo_result *xtra_meminfo_get ( } // end: xtra_meminfo_get_ -PROCPS_EXPORT void xtra_meminfo_val ( +PROCPS_EXPORT struct meminfo_result *xtra_meminfo_val ( int relative_enum, const char *typestr, const struct meminfo_stack *stack, @@ -894,17 +894,21 @@ PROCPS_EXPORT void xtra_meminfo_val ( const char *file, int lineno) { - struct meminfo_result *r; char *str; + int i; - r = &stack->head[relative_enum]; - if (r->item < 0 || r->item >= MEMINFO_logical_end) { - fprintf(stderr, "%s line %d: invalid item = %d, relative_enum = %d, type = %s\n" - , file, lineno, r->item, relative_enum, typestr); - return; + for (i = 0; stack->head[i].item < MEMINFO_logical_end; i++) + ; + if (relative_enum < 0 || relative_enum >= i) { + fprintf(stderr, "%s line %d: invalid relative_enum = %d, type = %s\n" + , file, lineno, relative_enum, typestr); + return NULL; } - str = Item_table[r->item].type2str; + str = Item_table[stack->head[relative_enum].item].type2str; if (str[0] - && (strcmp(typestr, str))) + && (strcmp(typestr, str))) { fprintf(stderr, "%s line %d: was %s, expected %s\n", file, lineno, typestr, str); + return NULL; + } + return &stack->head[relative_enum]; } // end: xtra_meminfo_val diff --git a/proc/pids.c b/proc/pids.c index f3c33939..f8816a0d 100644 --- a/proc/pids.c +++ b/proc/pids.c @@ -1503,7 +1503,7 @@ PROCPS_EXPORT struct pids_stack **procps_pids_sort ( * 2) the '#include ' used */ -PROCPS_EXPORT void xtra_pids_val ( +PROCPS_EXPORT struct pids_result *xtra_pids_val ( int relative_enum, const char *typestr, const struct pids_stack *stack, @@ -1511,17 +1511,21 @@ PROCPS_EXPORT void xtra_pids_val ( const char *file, int lineno) { - struct pids_result *r; char *str; + int i; - r = &stack->head[relative_enum]; - if (r->item < 0 || r->item >= PIDS_logical_end) { - fprintf(stderr, "%s line %d: invalid item = %d, relative_enum = %d, type = %s\n" - , file, lineno, r->item, relative_enum, typestr); - return; + for (i = 0; stack->head[i].item < PIDS_logical_end; i++) + ; + if (relative_enum < 0 || relative_enum >= i) { + fprintf(stderr, "%s line %d: invalid relative_enum = %d, type = %s\n" + , file, lineno, relative_enum, typestr); + return NULL; } - str = Item_table[r->item].type2str; + str = Item_table[stack->head[relative_enum].item].type2str; if (str[0] - && (strcmp(typestr, str))) + && (strcmp(typestr, str))) { fprintf(stderr, "%s line %d: was %s, expected %s\n", file, lineno, typestr, str); + return NULL; + } + return &stack->head[relative_enum]; } // end: xtra_pids_val diff --git a/proc/slabinfo.c b/proc/slabinfo.c index 70023c15..a1989ff2 100644 --- a/proc/slabinfo.c +++ b/proc/slabinfo.c @@ -1043,7 +1043,7 @@ PROCPS_EXPORT struct slabinfo_result *xtra_slabinfo_get ( } // end: xtra_slabinfo_get_ -PROCPS_EXPORT void xtra_slabinfo_val ( +PROCPS_EXPORT struct slabinfo_result *xtra_slabinfo_val ( int relative_enum, const char *typestr, const struct slabinfo_stack *stack, @@ -1051,17 +1051,21 @@ PROCPS_EXPORT void xtra_slabinfo_val ( const char *file, int lineno) { - struct slabinfo_result *r; char *str; + int i; - r = &stack->head[relative_enum]; - if (r->item < 0 || r->item >= SLABINFO_logical_end) { - fprintf(stderr, "%s line %d: invalid item = %d, relative_enum = %d, type = %s\n" - , file, lineno, r->item, relative_enum, typestr); - return; + for (i = 0; stack->head[i].item < SLABINFO_logical_end; i++) + ; + if (relative_enum < 0 || relative_enum >= i) { + fprintf(stderr, "%s line %d: invalid relative_enum = %d, type = %s\n" + , file, lineno, relative_enum, typestr); + return NULL; } - str = Item_table[r->item].type2str; + str = Item_table[stack->head[relative_enum].item].type2str; if (str[0] - && (strcmp(typestr, str))) + && (strcmp(typestr, str))) { fprintf(stderr, "%s line %d: was %s, expected %s\n", file, lineno, typestr, str); + return NULL; + } + return &stack->head[relative_enum]; } // end: xtra_slabinfo_val diff --git a/proc/stat.c b/proc/stat.c index e2168667..45ebcde4 100644 --- a/proc/stat.c +++ b/proc/stat.c @@ -1161,7 +1161,7 @@ PROCPS_EXPORT struct stat_result *xtra_stat_get ( } // end: xtra_stat_get_ -PROCPS_EXPORT void xtra_stat_val ( +PROCPS_EXPORT struct stat_result *xtra_stat_val ( int relative_enum, const char *typestr, const struct stat_stack *stack, @@ -1169,17 +1169,21 @@ PROCPS_EXPORT void xtra_stat_val ( const char *file, int lineno) { - struct stat_result *r; char *str; + int i; - r = &stack->head[relative_enum]; - if (r->item < 0 || r->item >= STAT_logical_end) { - fprintf(stderr, "%s line %d: invalid item = %d, relative_enum = %d, type = %s\n" - , file, lineno, r->item, relative_enum, typestr); - return; + for (i = 0; stack->head[i].item < STAT_logical_end; i++) + ; + if (relative_enum < 0 || relative_enum >= i) { + fprintf(stderr, "%s line %d: invalid relative_enum = %d, type = %s\n" + , file, lineno, relative_enum, typestr); + return NULL; } - str = Item_table[r->item].type2str; + str = Item_table[stack->head[relative_enum].item].type2str; if (str[0] - && (strcmp(typestr, str))) + && (strcmp(typestr, str))) { fprintf(stderr, "%s line %d: was %s, expected %s\n", file, lineno, typestr, str); + return NULL; + } + return &stack->head[relative_enum]; } // end: xtra_stat_val diff --git a/proc/vmstat.c b/proc/vmstat.c index 686f261a..3f6cb8ad 100644 --- a/proc/vmstat.c +++ b/proc/vmstat.c @@ -1282,7 +1282,7 @@ PROCPS_EXPORT struct vmstat_result *xtra_vmstat_get ( } // end: xtra_vmstat_get_ -PROCPS_EXPORT void xtra_vmstat_val ( +PROCPS_EXPORT struct vmstat_result *xtra_vmstat_val ( int relative_enum, const char *typestr, const struct vmstat_stack *stack, @@ -1290,17 +1290,21 @@ PROCPS_EXPORT void xtra_vmstat_val ( const char *file, int lineno) { - struct vmstat_result *r; char *str; + int i; - r = &stack->head[relative_enum]; - if (r->item < 0 || r->item >= VMSTAT_logical_end) { - fprintf(stderr, "%s line %d: invalid item = %d, relative_enum = %d, type = %s\n" - , file, lineno, r->item, relative_enum, typestr); - return; + for (i = 0; stack->head[i].item < VMSTAT_logical_end; i++) + ; + if (relative_enum < 0 || relative_enum >= i) { + fprintf(stderr, "%s line %d: invalid relative_enum = %d, type = %s\n" + , file, lineno, relative_enum, typestr); + return NULL; } - str = Item_table[r->item].type2str; + str = Item_table[stack->head[relative_enum].item].type2str; if (str[0] - && (strcmp(typestr, str))) + && (strcmp(typestr, str))) { fprintf(stderr, "%s line %d: was %s, expected %s\n", file, lineno, typestr, str); + return NULL; + } + return &stack->head[relative_enum]; } // end: xtra_vmstat_val diff --git a/proc/xtra-procps-debug.h b/proc/xtra-procps-debug.h index 2d4fc39f..e8400ff8 100644 --- a/proc/xtra-procps-debug.h +++ b/proc/xtra-procps-debug.h @@ -43,7 +43,7 @@ struct diskstats_result *xtra_diskstats_get ( #ifdef DISKSTATS_VAL #undef DISKSTATS_VAL -void xtra_diskstats_val ( +struct diskstats_result *xtra_diskstats_val ( int relative_enum, const char *typestr, const struct diskstats_stack *stack, @@ -52,8 +52,9 @@ void xtra_diskstats_val ( int lineno); #define DISKSTATS_VAL( relative_enum, type, stack, info ) ( { \ - xtra_diskstats_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \ - stack -> head [ relative_enum ] . result . type; } ) + struct diskstats_result *r; \ + r = xtra_diskstats_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \ + r ? r->result . type : 0; } ) #endif // . . . . . . . . . . @@ -75,7 +76,7 @@ struct meminfo_result *xtra_meminfo_get ( #ifdef MEMINFO_VAL #undef MEMINFO_VAL -void xtra_meminfo_val ( +struct meminfo_result *xtra_meminfo_val ( int relative_enum, const char *typestr, const struct meminfo_stack *stack, @@ -84,15 +85,16 @@ void xtra_meminfo_val ( int lineno); #define MEMINFO_VAL( relative_enum, type, stack, info ) ( { \ - xtra_meminfo_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \ - stack -> head [ relative_enum ] . result . type; } ) + struct meminfo_result *r; \ + r = xtra_meminfo_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \ + r ? r->result . type : 0; } ) #endif // . . . . . . . . . . // --- PIDS ----------------------------------------------- #ifdef PIDS_VAL #undef PIDS_VAL -void xtra_pids_val ( +struct pids_result *xtra_pids_val ( int relative_enum, const char *typestr, const struct pids_stack *stack, @@ -101,8 +103,9 @@ void xtra_pids_val ( int lineno); #define PIDS_VAL( relative_enum, type, stack, info ) ( { \ - xtra_pids_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \ - stack -> head [ relative_enum ] . result . type; } ) + struct pids_result *r; \ + r = xtra_pids_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \ + r ? r->result . type : 0; } ) #endif // . . . . . . . . . . @@ -124,7 +127,7 @@ struct slabinfo_result *xtra_slabinfo_get ( #ifdef SLABINFO_VAL #undef SLABINFO_VAL -void xtra_slabinfo_val ( +struct slabinfo_result *xtra_slabinfo_val ( int relative_enum, const char *typestr, const struct slabinfo_stack *stack, @@ -133,8 +136,9 @@ void xtra_slabinfo_val ( int lineno); #define SLABINFO_VAL( relative_enum, type, stack, info ) ( { \ - xtra_slabinfo_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \ - stack -> head [ relative_enum ] . result . type; } ) + struct slabinfo_result *r; \ + r = xtra_slabinfo_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \ + r ? r->result . type : 0; } ) #endif // . . . . . . . . . . @@ -156,7 +160,7 @@ struct stat_result *xtra_stat_get ( #ifdef STAT_VAL #undef STAT_VAL -void xtra_stat_val ( +struct stat_result *xtra_stat_val ( int relative_enum, const char *typestr, const struct stat_stack *stack, @@ -165,8 +169,9 @@ void xtra_stat_val ( int lineno); #define STAT_VAL( relative_enum, type, stack, info ) ( { \ - xtra_stat_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \ - stack -> head [ relative_enum ] . result . type; } ) + struct stat_result *r; \ + r = xtra_stat_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \ + r ? r->result . type : 0; } ) #endif // . . . . . . . . . . @@ -188,7 +193,7 @@ struct vmstat_result *xtra_vmstat_get ( #ifdef VMSTAT_VAL #undef VMSTAT_VAL -void xtra_vmstat_val ( +struct vmstat_result *xtra_vmstat_val ( int relative_enum, const char *typestr, const struct vmstat_stack *stack, @@ -197,8 +202,9 @@ void xtra_vmstat_val ( int lineno); #define VMSTAT_VAL( relative_enum, type, stack, info ) ( { \ - xtra_vmstat_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \ - stack -> head [ relative_enum ] . result . type; } ) + struct vmstat_result *r; \ + r = xtra_vmstat_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \ + r ? r->result . type : 0; } ) #endif // . . . . . . . . . . #endif // end: XTRA_PROCPS_DEBUG_H -- 2.40.0