From cea21ddca3b526e60b6c16e8bf6d1f29e40151f5 Mon Sep 17 00:00:00 2001 From: Hannes von Haugwitz Date: Wed, 30 Dec 2020 19:41:30 +0100 Subject: [PATCH] Refactor logging and config parsing code --- include/util.h | 3 +++ src/aide.c | 16 ++++++++-------- src/base64.c | 5 +++-- src/be.c | 13 ++++--------- src/commandconf.c | 35 ++++++++++++++++------------------- src/compare_db.c | 32 ++++++++++++++++---------------- src/conf_lex.l | 25 +++++++++++++------------ src/conf_yacc.y | 4 ++-- src/db.c | 22 +++++++++++----------- src/db_disk.c | 16 ++++++++-------- src/db_file.c | 30 ++++++++++++------------------ src/db_sql.c | 20 ++++++-------------- src/do_md.c | 27 ++++++++++++++------------- src/error.c | 4 ++-- src/gen_list.c | 29 +++++++++++++---------------- src/list.c | 27 +++++---------------------- src/md.c | 3 ++- src/util.c | 40 +++++++++++++++++++++++++++++----------- 18 files changed, 167 insertions(+), 184 deletions(-) diff --git a/include/util.h b/include/util.h index 7998853..4b41665 100644 --- a/include/util.h +++ b/include/util.h @@ -38,6 +38,9 @@ # define stricmp(a,b) strcasecmp( (a), (b) ) #endif +void* checked_malloc(size_t); +void* checked_strdup(const char *); + int cmpurl(url_t*, url_t*); url_t* parse_url(char*); diff --git a/src/aide.c b/src/aide.c index bcae227..1f1ff10 100644 --- a/src/aide.c +++ b/src/aide.c @@ -183,7 +183,7 @@ static int read_param(int argc,char**argv) if (optarg!=NULL) { const char* pcre_error; int pcre_erroffset; - conf->limit=malloc(strlen(optarg)+1); + conf->limit=checked_malloc(strlen(optarg)+1); strcpy(conf->limit,optarg); if((conf->limit_crx=pcre_compile(conf->limit, PCRE_ANCHORED, &pcre_error, &pcre_erroffset, NULL)) == NULL) { error(0,_("Error in limit regexp '%s' at %i: %s\n"), conf->limit, pcre_erroffset, pcre_error); @@ -265,13 +265,13 @@ static void setdefaults_before_config() { char* urlstr=INITIALERRORSTO; url_t* u=NULL; - char* s=(char*)malloc(sizeof(char)*MAXHOSTNAMELEN+1); + char* s=(char*)checked_malloc(sizeof(char)*MAXHOSTNAMELEN+1); DB_ATTR_TYPE X; /* Set up the hostname */ - conf=(db_config*)malloc(sizeof(db_config)); + conf=(db_config*)checked_malloc(sizeof(db_config)); conf->defsyms=NULL; if (gethostname(s,MAXHOSTNAMELEN)==-1) { @@ -325,7 +325,7 @@ static void setdefaults_before_config() conf->old_dbnewmdstr=NULL; conf->old_dboldmdstr=NULL; - conf->db_out_order=(DB_FIELD*)malloc(sizeof(DB_FIELD)*db_unknown); + conf->db_out_order=(DB_FIELD*)checked_malloc(sizeof(DB_FIELD)*db_unknown); conf->db_out_size=1; conf->db_out_order[0]=db_filename; conf->symlinks_found=0; @@ -469,14 +469,14 @@ static void setdefaults_after_config() { if(conf->db_in_url==NULL){ url_t* u=NULL; - u=(url_t*)malloc(sizeof(url_t)); + u=(url_t*)checked_malloc(sizeof(url_t)); u->type=url_file; u->value=DEFAULT_DB; conf->db_in_url=u; } if(conf->db_out_url==NULL){ url_t* u=NULL; - u=(url_t*)malloc(sizeof(url_t)); + u=(url_t*)checked_malloc(sizeof(url_t)); u->type=url_file; u->value=DEFAULT_DB_OUT; conf->db_out_url=u; @@ -485,7 +485,7 @@ static void setdefaults_after_config() url_t* u=NULL; /* Don't free this one because conf->report_url needs it */ - u=(url_t*)malloc(sizeof(url_t)); + u=(url_t*)checked_malloc(sizeof(url_t)); u->type=url_stdout; u->value=""; error_init(u,0); @@ -582,7 +582,7 @@ int main(int argc,char**argv) mhash(conf->confmd,"\n",1); }; mhash(conf->confmd, NULL,0); - dig=(byte*)malloc(sizeof(byte)*mhash_get_block_size(conf->confhmactype)); + dig=(byte*)checked_malloc(sizeof(byte)*mhash_get_block_size(conf->confhmactype)); mhash_deinit(conf->confmd,(void*)dig); digstr=encode_base64(dig,mhash_get_block_size(conf->confhmactype)); diff --git a/src/base64.c b/src/base64.c index 1b0f301..54f4d26 100644 --- a/src/base64.c +++ b/src/base64.c @@ -36,6 +36,7 @@ #include #include "base64.h" #include "report.h" +#include "util.h" /*for locale support*/ #include "locale-aide.h" /*for locale support*/ @@ -102,7 +103,7 @@ char* encode_base64(byte* src,size_t ssize) /* length of encoded base64 string (padded) */ size_t length = sizeof(char)* ((ssize + 2) / 3) * 4; - outbuf = (char *)malloc(length + 1); + outbuf = (char *)checked_malloc(length + 1); /* Initialize working pointers */ inb = src; @@ -194,7 +195,7 @@ byte* decode_base64(char* src,size_t ssize, size_t *ret_len) /* Initialize working pointers */ inb = src; - outbuf = (byte *)malloc(length + 1); + outbuf = (byte *)checked_malloc(length + 1); l = 0; triple = 0; diff --git a/src/be.c b/src/be.c index 2d72e92..6350648 100644 --- a/src/be.c +++ b/src/be.c @@ -51,7 +51,7 @@ static int be_sql_readinit(psql_data* ret) { char* s; char declare []="DECLARE aidecursor CURSOR FOR select * from "; - s = (char*)malloc(strlen(declare)+strlen(ret->table)+1); + s = (char*)checked_malloc(strlen(declare)+strlen(ret->table)+1); s[0]=0; s=strcat(s,declare); s=strcat(s,ret->table); @@ -250,14 +250,9 @@ FILE* be_init(int inout,url_t* u,int iszipped) char *pghost, *pgport, *pgoptions, *pgtty, *dbName, *login, *pwd; char *tmp,*tmp2; - psql_data* ret = (psql_data*) malloc(sizeof(psql_data)*1); + psql_data* ret = (psql_data*) checked_malloc(sizeof(psql_data)*1); - if (ret==NULL) { - error(0,"Not enough memory for postgres sql connection\n"); - return ret; - } - - tmp=strdup(u->value); + tmp=checked_strdup(u->value); tmp2=tmp; pgtty=NULL;pgoptions=NULL; @@ -327,7 +322,7 @@ FILE* be_init(int inout,url_t* u,int iszipped) /* Otherwise we would become to situation that name of table would be freeed */ - ret->table = strdup(ret->table); + ret->table = checked_strdup(ret->table); /* And now we have made a connection to database.. Next thing we do is to begin a new transaction block */ diff --git a/src/commandconf.c b/src/commandconf.c index 406c684..2019a1c 100644 --- a/src/commandconf.c +++ b/src/commandconf.c @@ -63,10 +63,10 @@ int commandconf(const char mode,const char* line) switch(mode){ case 'B':{ if(before==NULL){ - before=strdup(line); + before=checked_strdup(line); } else { - tmp=(char*)malloc(sizeof(char) + tmp=(char*)checked_malloc(sizeof(char) *(strlen(before)+strlen(line)+2)); tmp[0]='\0'; strcat(tmp,before); @@ -78,15 +78,15 @@ int commandconf(const char mode,const char* line) break; } case 'C':{ - config=strdup(line); + config=checked_strdup(line); break; } case 'A':{ if(after==NULL){ - after=strdup(line); + after=checked_strdup(line); } else { - tmp=(char*)malloc(sizeof(char) + tmp=(char*)checked_malloc(sizeof(char) *(strlen(after)+strlen(line)+2)); strcpy(tmp,after); strcat(tmp,"\n"); @@ -128,7 +128,7 @@ int commandconf(const char mode,const char* line) } l+=strlen("@@include \n\n\n")+1; - all=(char*)malloc(sizeof(char)*l); + all=(char*)checked_malloc(sizeof(char)*l); memset(all,0,l); if(before!=NULL){ @@ -537,12 +537,12 @@ void putbackvariable(char* var) { char* a=NULL; - char* v=strdup(var); + char* v=checked_strdup(var); char* subst_begin=strstr(v,"@@{"); char* subst_end=strstr(subst_begin,"}"); - char* tmp=(char*)malloc((subst_end-subst_begin)+1); + char* tmp=(char*)checked_malloc((subst_end-subst_begin)+1); tmp = strncpy(tmp,subst_begin+3,subst_end-subst_begin-3); @@ -578,7 +578,7 @@ void do_define(char* name, char* value) list* l=NULL; if(!(l=list_find(name,conf->defsyms))){ - s=(symba*)malloc(sizeof(symba)); + s=(symba*)checked_malloc(sizeof(symba)); s->name=name; s->value=value; conf->defsyms=list_append(conf->defsyms,(void*)s); @@ -694,10 +694,7 @@ int do_ifxhost(int mode,char* name) char* s=NULL; char *p; doit=mode; - s=(char*)malloc(sizeof(char)*MAXHOSTNAMELEN+1); - if (s == NULL) { - error(0,_("Couldn't malloc hostname buffer")); - } + s=(char*)checked_malloc(sizeof(char)*MAXHOSTNAMELEN+1); s[MAXHOSTNAMELEN] = '\0'; if(gethostname(s,MAXHOSTNAMELEN)==-1){ @@ -722,7 +719,7 @@ list* append_rxlist(char* rx,DB_ATTR_TYPE attr,list* rxlst, RESTRICTION_TYPE res extern long conf_lineno; /* defined & set in conf_lex.l */ rx_rule* r=NULL; - r=(rx_rule*)malloc(sizeof(rx_rule)); + r=(rx_rule*)checked_malloc(sizeof(rx_rule)); r->rx=rx; r->attr=attr; r->conf_lineno = conf_lineno; @@ -754,7 +751,7 @@ void do_groupdef(char* group,DB_ATTR_TYPE value) return; } /* This is a new group */ - s=(symba*)malloc(sizeof(symba)); + s=(symba*)checked_malloc(sizeof(symba)); s->name=group; s->ival=value; conf->groupsyms=list_append(conf->groupsyms,(void*)s); @@ -997,7 +994,7 @@ const char* db_key_7=DBHMACKEY_07; void* get_conf_key(void) { void* r; - char* m=(char*)malloc(strlen(aide_key_1)+ + char* m=(char*)checked_malloc(strlen(aide_key_1)+ strlen(aide_key_2)+ strlen(aide_key_3)+ strlen(aide_key_4)+ @@ -1028,7 +1025,7 @@ void* get_conf_key(void) { size_t get_conf_key_len(void) { size_t len=0; - char* m=(char*)malloc(strlen(aide_key_1)+ + char* m=(char*)checked_malloc(strlen(aide_key_1)+ strlen(aide_key_2)+ strlen(aide_key_3)+ strlen(aide_key_4)+ @@ -1059,7 +1056,7 @@ size_t get_conf_key_len(void) { void* get_db_key(void) { void* r; - char* m=(char*)malloc(strlen(db_key_1)+ + char* m=(char*)checked_malloc(strlen(db_key_1)+ strlen(db_key_2)+ strlen(db_key_3)+ strlen(db_key_4)+ @@ -1090,7 +1087,7 @@ void* get_db_key(void) { size_t get_db_key_len(void) { size_t len=0; - char* m=(char*)malloc(strlen(db_key_1)+ + char* m=(char*)checked_malloc(strlen(db_key_1)+ strlen(db_key_2)+ strlen(db_key_3)+ strlen(db_key_4)+ diff --git a/src/compare_db.c b/src/compare_db.c index 5a7ec44..67049ed 100644 --- a/src/compare_db.c +++ b/src/compare_db.c @@ -202,7 +202,7 @@ static char* report_attrs(DB_ATTR_TYPE attrs) { j += strlen(attrs_string[i])+1; } } - str = malloc(j * sizeof (char)); + str = checked_malloc(j * sizeof (char)); j=0; for (int i = 0; i < num_attrs; ++i) { if ((1LLU<num; } - *values = malloc(n * sizeof(char*)); - (*values)[0]=malloc((6+floor(log10(n)))*sizeof(char)); + *values = checked_malloc(n * sizeof(char*)); + (*values)[0]=checked_malloc((6+floor(log10(n)))*sizeof(char)); snprintf((*values)[0], 6+floor(log10(n)), "num=%d", n-1); if (n>1) { size_t num = 0; @@ -274,12 +274,12 @@ static int xattrs2array(xattrs_type* xattrs, char* **values) { len = xstrnspn(val, xattrs->ents[num - 1].vsz, PRINTABLE_XATTR_VALS); if ((len == xattrs->ents[num - 1].vsz) || ((len == (xattrs->ents[num - 1].vsz - 1)) && !val[len])) { length = 8 + width + strlen(xattrs->ents[num - 1].key) + strlen(val); - (*values)[num]=malloc(length *sizeof(char)); + (*values)[num]=checked_malloc(length *sizeof(char)); snprintf((*values)[num], length , "[%.*zd] %s = %s", width, num, xattrs->ents[num - 1].key, val); } else { val = encode_base64(xattrs->ents[num - 1].val, xattrs->ents[num - 1].vsz); length = 10 + width + strlen(xattrs->ents[num - 1].key) + strlen(val); - (*values)[num]=malloc( length *sizeof(char)); + (*values)[num]=checked_malloc( length *sizeof(char)); snprintf((*values)[num], length , "[%.*zd] %s <=> %s", width, num, xattrs->ents[num - 1].key, val); free(val); } @@ -298,7 +298,7 @@ static int acl2array(acl_type* acl, char* **values) { i = k = 0; \ while (acl->x[i]) { \ if (acl->x[i]=='\n') { \ - (*values)[j]=malloc(4+(i-k)*sizeof(char)); \ + (*values)[j]=checked_malloc(4+(i-k)*sizeof(char)); \ snprintf((*values)[j], 4+(i-k), "%c: %s", y, &acl->x[k]); \ j++; \ k=i+1; \ @@ -310,7 +310,7 @@ static int acl2array(acl_type* acl, char* **values) { int j, k, i; if (acl->acl_a) { i = 0; while (acl->acl_a[i]) { if (acl->acl_a[i++]=='\n') { n++; } } } if (acl->acl_d) { i = 0; while (acl->acl_d[i]) { if (acl->acl_d[i++]=='\n') { n++; } } } - *values = malloc(n * sizeof(char*)); + *values = checked_malloc(n * sizeof(char*)); j = 0; easy_posix_acl(acl_a, 'A') easy_posix_acl(acl_d, 'D') @@ -326,7 +326,7 @@ static int acl2array(acl_type* acl, char* **values) { #ifdef WITH_E2FSATTRS static char* e2fsattrs2string(unsigned long flags, int flags_only) { int length = sizeof(flag_bits)/sizeof(long); - char* string = malloc ((length+1) * sizeof (char)); + char* string = checked_malloc ((length+1) * sizeof (char)); int j = 0; for (int i = 0 ; i < length ; i++) { if (!flags_only && flag_bits[i]&(conf->report_ignore_e2fsattrs)) { @@ -367,7 +367,7 @@ static char* get_file_type_string(mode_t mode) { } static char* byte_to_base16(byte* src, size_t ssize) { - char* str = malloc((2*ssize+1) * sizeof (char)); + char* str = checked_malloc((2*ssize+1) * sizeof (char)); size_t i; for(i=0; i < ssize; ++i) { snprintf(&str[2*i], 3, "%02x", src[i]); @@ -380,7 +380,7 @@ static int get_attribute_values(DB_ATTR_TYPE attr, db_line* line, #define easy_string(s) \ l = strlen(s)+1; \ -*values[0] = malloc(l * sizeof (char)); \ +*values[0] = checked_malloc(l * sizeof (char)); \ snprintf(*values[0], l, "%s",s); #define easy_md(a,b,c) \ @@ -394,12 +394,12 @@ snprintf(*values[0], l, "%s",s); #define easy_number(a,b,c) \ } else if (a&attr) { \ l = 2+floor(line->b?log10(line->b):0); \ - *values[0] = malloc(l * sizeof (char)); \ + *values[0] = checked_malloc(l * sizeof (char)); \ snprintf(*values[0], l, c,line->b); #define easy_time(a,b) \ } else if (a&attr) { \ - *values[0] = malloc(time_string_len * sizeof (char)); \ + *values[0] = checked_malloc(time_string_len * sizeof (char)); \ strftime(*values[0], time_string_len, time_format, localtime(&(line->b))); if (line==NULL || !(line->attr&attr)) { @@ -415,7 +415,7 @@ snprintf(*values[0], l, "%s",s); #endif } else { int l; - *values = malloc(1 * sizeof (char*)); + *values = checked_malloc(1 * sizeof (char*)); if (DB_FTYPE&attr) { easy_string(get_file_type_string(line->perm)) } else if (DB_LINKNAME&attr) { @@ -466,7 +466,7 @@ static void print_line(seltree* node) { if(conf->summarize_changes==1) { int i; int length = sizeof(summary_attributes)/sizeof(DB_ATTR_TYPE); - char* summary = malloc ((length+1) * sizeof (char)); + char* summary = checked_malloc ((length+1) * sizeof (char)); if (node->checked&(NODE_ADDED|NODE_REMOVED)) { summary[0]=get_file_type_char(((node->checked&NODE_REMOVED)?node->old_data:node->new_data)->perm); for(i=1;istart_time))); error(2,_("Start timestamp: %s (AIDE " AIDEVERSION ")\n"), time); free(time); time=NULL; @@ -715,7 +715,7 @@ static void print_report_databases() { static void print_report_footer() { - char *time = malloc(time_string_len * sizeof (char)); + char *time = checked_malloc(time_string_len * sizeof (char)); int run_time = (int) difftime(conf->end_time, conf->start_time); strftime(time, time_string_len, time_format, localtime(&(conf->end_time))); diff --git a/src/conf_lex.l b/src/conf_lex.l index b08d7a7..73a3e64 100644 --- a/src/conf_lex.l +++ b/src/conf_lex.l @@ -40,6 +40,7 @@ EX [" "\t]* #include "list.h" #include "symboltable.h" #include "commandconf.h" +#include "util.h" void includehandler(void) ; void conf_put_token(const char*); @@ -91,26 +92,26 @@ int var_in_conflval=0; ^[\ \t]*"=/"({C}|{PC})* { - conflval.s=strdup(conftext+1+firstnotempty(conftext)); + conflval.s=checked_strdup(conftext+1+firstnotempty(conftext)); BEGIN(EXPR); return (TEQURXRULE); } ^[\ \t]*"/"({C}|{PC})* { - conflval.s=strdup(conftext+firstnotempty(conftext)); + conflval.s=checked_strdup(conftext+firstnotempty(conftext)); error(230,"%li:Selrule\n",conf_lineno); BEGIN(EXPR); return (TSELRXRULE ); } ^[\ \t]*"!/"({C}|{PC})* { - conflval.s=strdup(conftext+1+firstnotempty(conftext)); + conflval.s=checked_strdup(conftext+1+firstnotempty(conftext)); error(230,"%li:Negrule\n",conf_lineno); return (TNEGRXRULE ); } ^[\ \t]*({L})+ { - conflval.s=strdup(conftext+firstnotempty(conftext)); + conflval.s=checked_strdup(conftext+firstnotempty(conftext)); error(230,"%li:Equrule\n",conf_lineno); BEGIN(EXPREQUHUNT); return (TSTRING); @@ -126,7 +127,7 @@ int var_in_conflval=0; } [\ \t]*({L}|{D}|">")+ { - conflval.s=strdup(conftext+firstnotempty(conftext)); + conflval.s=checked_strdup(conftext+firstnotempty(conftext)); return (TSTRING); } @@ -146,14 +147,14 @@ int var_in_conflval=0; ({L})+ { error(230," %s",conftext); - conflval.s=strdup(conftext); + conflval.s=checked_strdup(conftext); BEGIN (DEFSTMT2); return (TSTRING); } ({C})+ { error(230," %s",conftext); - conflval.s=strdup(conftext); + conflval.s=checked_strdup(conftext); return (TSTRING); } @@ -209,7 +210,7 @@ int var_in_conflval=0; ({L})+ { error(230," %s\n",conftext); - conflval.s=strdup(conftext); + conflval.s=checked_strdup(conftext); BEGIN 0; return (TSTRING); } @@ -228,7 +229,7 @@ int var_in_conflval=0; ({L}|"-")+ { error(230," %s\n",conftext); - conflval.s=strdup(conftext); + conflval.s=checked_strdup(conftext); BEGIN 0; return (TSTRING); } @@ -384,7 +385,7 @@ int var_in_conflval=0; } ([A-Za-z]+|"0") { - conflval.s=strdup(conftext); + conflval.s=checked_strdup(conftext); BEGIN 0; return (TSTRING); } @@ -424,14 +425,14 @@ int var_in_conflval=0; } ({C})+ { - conflval.s=strdup(conftext); + conflval.s=checked_strdup(conftext); BEGIN 0; return (TSTRING); } ({L})+ { - conflval.s=strdup(conftext); + conflval.s=checked_strdup(conftext); return (TSTRING); } diff --git a/src/conf_yacc.y b/src/conf_yacc.y index 7ce75cf..7df2c3f 100644 --- a/src/conf_yacc.y +++ b/src/conf_yacc.y @@ -306,7 +306,7 @@ db_attrs : TDATABASE_ATTRS expr { beginconfigstmt : TBEGIN_CONFIG TSTRING { #ifdef WITH_MHASH conf->do_configmd=1; - conf->old_confmdstr=strdup($2); + conf->old_confmdstr=checked_strdup($2); #endif } ; @@ -415,7 +415,7 @@ recursion_stopper : TRECSTOP TSTRING { } ; config_version : TCONFIG_VERSION TSTRING { - conf->config_version=strdup($2); + conf->config_version=checked_strdup($2); } ; %% diff --git a/src/db.c b/src/db.c index 9d49854..7db2efc 100644 --- a/src/db.c +++ b/src/db.c @@ -151,7 +151,7 @@ static struct md_container *init_db_attrs(URL_TYPE type) { case url_https: case url_ftp: #endif /* WITH CURL */ - mdc = malloc(sizeof(struct md_container)); /* freed in close_db_attrs */ + mdc = checked_malloc(sizeof(struct md_container)); /* freed in close_db_attrs */ mdc->todo_attr = conf->db_attrs; init_md(mdc); break; @@ -170,7 +170,7 @@ static db_line *close_db_attrs (struct md_container *mdc, char *url_value) { db_line *line = NULL; if (mdc != NULL) { close_md(mdc); - line = malloc(sizeof(struct db_line)); + line = checked_malloc(sizeof(struct db_line)); line->filename = url_value; line->perm = 0; line->attr = conf->db_attrs; @@ -346,7 +346,7 @@ static char *db_readchar(char *s) return (NULL); if (s[1] == '-') - return (strdup("")); + return (checked_strdup("")); if (s[1] == '0') { @@ -359,7 +359,7 @@ static char *db_readchar(char *s) decode_string(s); - return strdup(s); + return checked_strdup(s); } #define WARN_ONCE(x) case db_ ## x : { \ @@ -373,7 +373,7 @@ static char *db_readchar(char *s) db_line* db_char2line(char** ss,int db){ int i; - db_line* line=(db_line*)malloc(sizeof(db_line)*1); + db_line* line=(db_line*)checked_malloc(sizeof(db_line)*1); int* db_osize=0; DB_FIELD** db_order=NULL; @@ -430,7 +430,7 @@ db_line* db_char2line(char** ss,int db){ case db_filename : { if(ss[(*db_order)[i]]!=NULL){ decode_string(ss[(*db_order)[i]]); - line->fullpath=strdup(ss[(*db_order)[i]]); + line->fullpath=checked_strdup(ss[(*db_order)[i]]); line->filename=line->fullpath; } else { error(0,"db_char2line():Error while reading database\n"); @@ -555,9 +555,9 @@ db_line* db_char2line(char** ss,int db){ pos=endp+1; /* Warning! if acl in database is corrupted then this will break down. */ - line->acl=malloc(sizeof(acl_type)); + line->acl=checked_malloc(sizeof(acl_type)); line->acl->entries=entries; - line->acl->acl=malloc(sizeof(aclent_t)*entries); + line->acl->acl=checked_malloc(sizeof(aclent_t)*entries); for (lc=0;lcacl->acl[lc].a_type=strtol(pos,&endp,10); pos=endp+1; @@ -581,7 +581,7 @@ db_line* db_char2line(char** ss,int db){ line->acl = NULL; else if (!strcmp(tval, "POSIX")) { - line->acl = malloc(sizeof(acl_type)); + line->acl = checked_malloc(sizeof(acl_type)); line->acl->acl_a = NULL; line->acl->acl_d = NULL; @@ -602,7 +602,7 @@ db_line* db_char2line(char** ss,int db){ num = readlong(tval, "xattrs"); if (num) { - line->xattrs = malloc(sizeof(xattrs_type)); + line->xattrs = checked_malloc(sizeof(xattrs_type)); line->xattrs->ents = calloc(sizeof(xattr_node), num); line->xattrs->sz = num; line->xattrs->num = num; @@ -613,7 +613,7 @@ db_line* db_char2line(char** ss,int db){ size_t vsz = 0; tval = strtok(NULL, ","); - line->xattrs->ents[num].key = db_readchar(strdup(tval)); + line->xattrs->ents[num].key = db_readchar(checked_strdup(tval)); tval = strtok(NULL, ","); val = base64tobyte(tval, strlen(tval), &vsz); line->xattrs->ents[num].val = val; diff --git a/src/db_disk.c b/src/db_disk.c index 36748e7..49f9249 100644 --- a/src/db_disk.c +++ b/src/db_disk.c @@ -117,7 +117,7 @@ static char *name_construct (const char *s) len++; } - ret = (char *) malloc (len); + ret = (char *) checked_malloc (len); ret[0] = (char) 0; strncpy(ret, conf->root_prefix, conf->root_prefix_length+1); strncat (ret, r->path, len2); @@ -146,12 +146,12 @@ void add_child (db_line * fil) return; } - new_r = malloc (sizeof (seltree)); + new_r = checked_malloc (sizeof (seltree)); new_r->attr = 0; i = strlen (fil->filename); - new_r->path = malloc (i + 1); + new_r->path = checked_malloc (i + 1); strncpy(new_r->path, fil->filename, i+1); new_r->childs = NULL; new_r->sel_rx_lst = NULL; @@ -200,7 +200,7 @@ db_line *db_readline_disk () /* root needs special handling */ if (!root_handled) { root_handled = 1; - fullname=malloc((conf->root_prefix_length+2)*sizeof(char)); + fullname=checked_malloc((conf->root_prefix_length+2)*sizeof(char)); strncpy(fullname, conf->root_prefix, conf->root_prefix_length+1); strncat (fullname, "/", 1); if (!get_file_status(fullname, &fs)) { @@ -339,7 +339,7 @@ recursion: error (255, "r->childs %p, r->parent %p,r->checked %i\n", r->childs, r->parent, r->checked); - fullname=malloc((conf->root_prefix_length+strlen(r->path)+1)*sizeof(char)); + fullname=checked_malloc((conf->root_prefix_length+strlen(r->path)+1)*sizeof(char)); strncpy(fullname, conf->root_prefix, conf->root_prefix_length+1); strncat(fullname, r->path, strlen(r->path)); dirh=open_dir(fullname); @@ -419,9 +419,9 @@ int db_disk_init () # ifdef HAVE_READDIR_R resp = (struct AIDE_DIRENT_TYPE **) - malloc (sizeof (struct AIDE_DIRENT_TYPE) + _POSIX_PATH_MAX); + checked_malloc (sizeof (struct AIDE_DIRENT_TYPE) + _POSIX_PATH_MAX); entp = (struct AIDE_DIRENT_TYPE *) - malloc (sizeof (struct AIDE_DIRENT_TYPE) + _POSIX_PATH_MAX); + checked_malloc (sizeof (struct AIDE_DIRENT_TYPE) + _POSIX_PATH_MAX); # else # ifdef HAVE_READDIR /* @@ -434,7 +434,7 @@ int db_disk_init () # endif - char* fullname=malloc((conf->root_prefix_length+2)*sizeof(char)); + char* fullname=checked_malloc((conf->root_prefix_length+2)*sizeof(char)); strncpy(fullname, conf->root_prefix, conf->root_prefix_length+1); strncat (fullname, "/", 1); dirh=open_dir(fullname); diff --git a/src/db_file.c b/src/db_file.c index dcacaef..4863458 100644 --- a/src/db_file.c +++ b/src/db_file.c @@ -67,7 +67,7 @@ void handle_gzipped_input(int out,gzFile* gzp){ int nread=0; int err=0; - int* buf=malloc(ZBUFSIZE); + int* buf=checked_malloc(ZBUFSIZE); buf[0]='\0'; error(200,"handle_gzipped_input(),%d\n",out); while(!gzeof(*gzp)){ @@ -133,11 +133,8 @@ int dofprintf( const char* s,...) retval=vsnprintf(buf,3,s,ap); va_end(ap); - temp=(char*)malloc(retval+2); - if(temp==NULL){ - error(0,"Unable to alloc %i bytes\n",retval+2); - return -1; - } + temp=(char*)checked_malloc(retval+2); + va_start(ap,s); retval=vsnprintf(temp,retval+1,s,ap); va_end(ap); @@ -189,7 +186,7 @@ int db_file_read_spec(int db){ } } - *db_order=(DB_FIELD*) malloc(1*sizeof(DB_FIELD)); + *db_order=(DB_FIELD*) checked_malloc(1*sizeof(DB_FIELD)); while ((i=db_scan())!=TNEWLINE){ switch (i) { @@ -425,7 +422,7 @@ char** db_readline_file(int db){ db_buff(db,NULL); } - s=(char**)malloc(sizeof(char*)*db_unknown); + s=(char**)checked_malloc(sizeof(char*)*db_unknown); /* We NEED this to avoid Bus errors on Suns */ for(i=0;idbhmactype)); + checked_malloc(sizeof(byte)*mhash_get_block_size(conf->dbhmactype)); mhash_deinit(*md,(void*)dig); digstr=encode_base64(dig,mhash_get_block_size(conf->dbhmactype)); if(strncmp(digstr,*oldmdstr,strlen(digstr))!=0){ @@ -701,11 +698,8 @@ int db_write_time_base64(time_t i,FILE* file,int a) } - ptr=(char*)malloc(sizeof(char)*TIMEBUFSIZE); - if (ptr==NULL) { - error(0,"\nCannot allocate memory.\n"); - abort(); - } + ptr=(char*)checked_malloc(sizeof(char)*TIMEBUFSIZE); + memset((void*)ptr,0,sizeof(char)*TIMEBUFSIZE); sprintf(ptr,"%li",i); @@ -1092,7 +1086,7 @@ int db_close_file(db_config* dbconf){ /* Let's write @@end_db */ if (dbconf->dbnewmd!=NULL) { mhash(dbconf->dbnewmd, NULL ,0); - dig=(byte*)malloc(sizeof(byte)*mhash_get_block_size(dbconf->dbhmactype)); + dig=(byte*)checked_malloc(sizeof(byte)*mhash_get_block_size(dbconf->dbhmactype)); mhash_deinit(dbconf->dbnewmd,(void*)dig); digstr=encode_base64(dig,mhash_get_block_size(dbconf->dbhmactype)); dbconf->do_dbnewmd=0; diff --git a/src/db_sql.c b/src/db_sql.c index 1545790..71a4d7a 100644 --- a/src/db_sql.c +++ b/src/db_sql.c @@ -38,6 +38,7 @@ #include "db_config.h" #include "libpq-fe.h" #include "report.h" +#include "util.h" #ifdef WITH_MHASH #include @@ -74,7 +75,7 @@ int db_writespec_sql(db_config* conf){ char* s; int ret = RETOK; - s = (char*) malloc(sizeof(char)*1024); /* Hope 1023 bytes is + s = (char*) checked_malloc(sizeof(char)*1024); /* Hope 1023 bytes is enough for string... 390 + length of table name should be enough. @@ -206,7 +207,7 @@ void db_readline_sql_char(void** d,int db,const int i, db_config* conf) s = (char*)PQgetvalue(data->res,cr,des); if (s!=NULL) { - *d=(void*)strdup((char*)s); + *d=(void*)checked_strdup((char*)s); } else { *d=NULL; } @@ -270,7 +271,7 @@ db_line* db_readline_sql(int db, db_config* conf) { error(255,"Everything read from SQL\n"); return NULL; } - rline=(db_line*)malloc(1*sizeof(db_line)); + rline=(db_line*)checked_malloc(1*sizeof(db_line)); db_readline_sql_byte((void*)&(rline->md5),db,(int)db_md5, conf); db_readline_sql_byte((void*)&(rline->sha1),db,db_sha1, conf); @@ -348,11 +349,7 @@ void sql_write_time_base64(time_t data,char* s,int i){ } - ptr=(char*)malloc(sizeof(char)*TIMEBUFSIZE); - if (ptr==NULL) { - error(0,"\nCannot allocate memory..\n"); - abort(); - } + ptr=(char*)checked_malloc(sizeof(char)*TIMEBUFSIZE); memset((void*)ptr,0,sizeof(char)*TIMEBUFSIZE); @@ -398,12 +395,7 @@ void sql_write_byte_base64(byte*data,size_t len,char* s,int i ) char* db_get_sql(db_line* line,db_config* conf){ int i; - char* s=(char*) malloc(sizeof(char)*10240); /* FIXME .. */ - - if (s==NULL) { - error(0,"\nCannot allocate memory..\n"); - abort(); - } + char* s=(char*) checked_malloc(sizeof(char)*10240); /* FIXME .. */ s[0]=0; diff --git a/src/do_md.c b/src/do_md.c index 6a309b9..44493f3 100644 --- a/src/do_md.c +++ b/src/do_md.c @@ -42,6 +42,7 @@ #include "db_config.h" #include "do_md.h" #include "report.h" +#include "util.h" #include "list.h" /*for locale support*/ #include "locale-aide.h" @@ -337,7 +338,7 @@ void calc_md(struct AIDE_STAT_TYPE* old_fs,db_line* line) { } #endif #endif /* not HAVE_MMAP */ - buf=malloc(READ_BLOCK_SIZE); + buf=checked_malloc(READ_BLOCK_SIZE); #if READ_BLOCK_SIZE>SSIZE_MAX #error "READ_BLOCK_SIZE" is too large. Max value is SSIZE_MAX, and current is READ_BLOCK_SIZE #endif @@ -479,14 +480,14 @@ void acl2line(db_line* line) { } /* assume memory allocs work, like rest of AIDE code... */ - ret = malloc(sizeof(acl_type)); + ret = checked_malloc(sizeof(acl_type)); /* use tmp, so free() can be called instead of acl_free() */ tmp = acl_to_text(acl_a, NULL); if (!tmp || !*tmp) ret->acl_a = NULL; else - ret->acl_a = strdup(tmp); + ret->acl_a = checked_strdup(tmp); acl_free(tmp); if (!acl_d) @@ -497,7 +498,7 @@ void acl2line(db_line* line) { if (!tmp || !*tmp) ret->acl_d = NULL; else - ret->acl_d = strdup(tmp); + ret->acl_d = checked_strdup(tmp); acl_free(tmp); } @@ -509,7 +510,7 @@ void acl2line(db_line* line) { #ifdef WITH_SUN_ACL if(DB_ACL&line->attr) { /* There might be a bug here. */ int res; - line->acl=malloc(sizeof(acl_type)); + line->acl=checked_malloc(sizeof(acl_type)); line->acl->entries=acl(line->fullpath,GETACLCNT,0,NULL); if (line->acl->entries==-1) { char* er=strerror(errno); @@ -520,7 +521,7 @@ void acl2line(db_line* line) { error(0,"ACL query failed for %s:%s\n",line->fullpath,er); } } else { - line->acl->acl=malloc(sizeof(aclent_t)*line->acl->entries); + line->acl->acl=checked_malloc(sizeof(aclent_t)*line->acl->entries); res=acl(line->fullpath,GETACL,line->acl->entries,line->acl->acl); if (res==-1) { error(0,"ACL error %s\n",strerror(errno)); @@ -541,10 +542,10 @@ void acl2line(db_line* line) { static xattrs_type *xattr_new(void) { xattrs_type *ret = NULL; - ret = malloc(sizeof(xattrs_type)); + ret = checked_malloc(sizeof(xattrs_type)); ret->num = 0; ret->sz = 2; - ret->ents = malloc(sizeof(xattr_node) * ret->sz); + ret->ents = checked_malloc(sizeof(xattr_node) * ret->sz); return (ret); } @@ -553,7 +554,7 @@ static void *xzmemdup(const void *ptr, size_t len) { /* always keeps a 0 at the end... */ void *ret = NULL; - ret = malloc(len+1); + ret = checked_malloc(len+1); memcpy(ret, ptr, len); ((char*)ret)[len] = 0; @@ -567,7 +568,7 @@ static void xattr_add(xattrs_type *xattrs, const char *key, const char xattrs->ents = realloc(xattrs->ents, sizeof(xattr_node) * xattrs->sz); } - xattrs->ents[xattrs->num].key = strdup(key); + xattrs->ents[xattrs->num].key = checked_strdup(key); xattrs->ents[xattrs->num].val = xzmemdup(val, vsz); xattrs->ents[xattrs->num].vsz = vsz; @@ -585,7 +586,7 @@ void xattrs2line(db_line *line) { return; /* assume memory allocs work, like rest of AIDE code... */ - if (!xatrs) xatrs = malloc(xsz); + if (!xatrs) xatrs = checked_malloc(xsz); while (((xret = llistxattr(line->fullpath, xatrs, xsz)) == -1) && (errno == ERANGE)) { xsz <<= 1; @@ -601,7 +602,7 @@ void xattrs2line(db_line *line) { static ssize_t asz = 1024; static char *val = NULL; - if (!val) val = malloc(asz); + if (!val) val = checked_malloc(asz); xattrs = xattr_new(); @@ -649,7 +650,7 @@ void selinux2line(db_line *line) { return; } - line->cntx = strdup(cntx); + line->cntx = checked_strdup(cntx); freecon(cntx); } diff --git a/src/error.c b/src/error.c index eaac426..25fe2b6 100644 --- a/src/error.c +++ b/src/error.c @@ -210,13 +210,13 @@ void error(int errorlevel,char* error_msg,...) db_line line; int len; memset(&line,0,sizeof(db_line)); - line.filename=(char*)malloc(3); + line.filename=(char*)checked_malloc(3); if (line.filename!=NULL) { va_start(ap,error_msg); len=vsnprintf(line.filename,2,error_msg,ap); va_end(ap); free(line.filename); - line.filename=malloc(len+2); + line.filename=checked_malloc(len+2); line.filename[0]='#'; if (line.filename!=NULL) { line.attr=DB_FILENAME; diff --git a/src/gen_list.c b/src/gen_list.c index 536390c..c5726fb 100644 --- a/src/gen_list.c +++ b/src/gen_list.c @@ -57,6 +57,7 @@ #endif #include "md.h" #include "do_md.h" +#include "util.h" void hsymlnk(db_line* line); void fs2db_line(struct AIDE_STAT_TYPE* fs,db_line* line); @@ -281,7 +282,7 @@ char* strrxtok(char* rx) /* The following code assumes that the first character is a slash */ size_t lastslash=1; - p=strdup(rx); + p=checked_strdup(rx); p[0]='/'; for(i=1;iconf_lineno = r->conf_lineno; - node->rx=strdup(r->rx); + node->rx=checked_strdup(r->rx); } else { node->conf_lineno = -1; node->rx=NULL; @@ -458,9 +459,9 @@ seltree* new_seltree_node( seltree* parent=NULL; char* tmprxtok = NULL; - node=(seltree*)malloc(sizeof(seltree)); + node=(seltree*)checked_malloc(sizeof(seltree)); node->childs=NULL; - node->path=strdup(path); + node->path=checked_strdup(path); node->sel_rx_lst=NULL; node->neg_rx_lst=NULL; node->equ_rx_lst=NULL; @@ -526,7 +527,7 @@ void gen_seltree(list* rxlist,seltree* tree,char type) error(0,_("Error in regexp '%s' at %i: %s\n"),curr_rule->rx, pcre_erroffset, pcre_error); }else{ /* replace regexp text with regexp compiled */ - rxc=(rx_rule*)malloc(sizeof(rx_rule)); + rxc=(rx_rule*)checked_malloc(sizeof(rx_rule)); /* and copy the rest */ rxc->rx=curr_rule->rx; rxc->crx=rxtmp; @@ -1004,7 +1005,7 @@ int check_rxtree(char* filename,seltree* tree,DB_ATTR_TYPE* attr, mode_t perm) char * parentname=NULL; seltree* pnode=NULL; - parentname=strdup(filename); + parentname=checked_strdup(filename); tmp=strrchr(parentname,'/'); if(tmp!=parentname){ *tmp='\0'; @@ -1079,7 +1080,7 @@ db_line* get_file_attrs(char* filename,DB_ATTR_TYPE attr, struct AIDE_STAT_TYPE Malloc if we have something to store.. */ - line=(db_line*)malloc(sizeof(db_line)); + line=(db_line*)checked_malloc(sizeof(db_line)); memset(line,0,sizeof(db_line)); @@ -1255,11 +1256,7 @@ void hsymlnk(db_line* line) { Is this valid?? No, We should do this elsewhere. */ - line->linkname=(char*)malloc(_POSIX_PATH_MAX+1); - if(line->linkname==NULL){ - error(0,_("malloc failed in hsymlnk()\n")); - abort(); - } + line->linkname=(char*)checked_malloc(_POSIX_PATH_MAX+1); /* Remember to nullify the buffer, because man page says diff --git a/src/list.c b/src/list.c index c1b2464..9a0fd5b 100644 --- a/src/list.c +++ b/src/list.c @@ -26,6 +26,7 @@ /*for locale support*/ #include "locale-aide.h" /*for locale support*/ +#include "util.h" /* list * limitations: @@ -49,17 +50,9 @@ list* list_sorted_insert(list* listp, void* data, int (*compare) (const void*, const void*)) { list* newitem=NULL; list* curitem=NULL; - newitem=(list*)malloc(sizeof(list)); - if (newitem==NULL) { - error(0,"Not enough memory to add a new item to list.\n"); - exit(EXIT_FAILURE); - } + newitem=(list*)checked_malloc(sizeof(list)); if (listp==NULL){ - list_header* header=(list_header*)malloc(sizeof(list_header)); - if (header==NULL){ - error(0,"Not enough memory for list header allocation\n"); - exit(EXIT_FAILURE); - } + list_header* header=(list_header*)checked_malloc(sizeof(list_header)); newitem->data=data; newitem->header=header; newitem->next=NULL; @@ -118,20 +111,10 @@ list* list_sorted_insert(list* listp, void* data, int (*compare) (const void*, c list* list_append(list* listp,void*data) { list* newitem=NULL; - newitem=(list*)malloc(sizeof(list)); - - if (newitem==NULL) { - error(0,"Not enough memory to add a new item to list.\n"); - exit(EXIT_FAILURE); - } + newitem=(list*)checked_malloc(sizeof(list)); if(listp==NULL){ - list_header* header=(list_header*)malloc(sizeof(list_header)); - - if (header==NULL){ - error(0,"Not enough memory for list header allocation\n"); - exit(EXIT_FAILURE); - } + list_header* header=(list_header*)checked_malloc(sizeof(list_header)); newitem->data=data; newitem->header=header; diff --git a/src/md.c b/src/md.c index c7ffb88..1d99ad0 100644 --- a/src/md.c +++ b/src/md.c @@ -23,6 +23,7 @@ #include #include "md.h" #include "report.h" +#include "util.h" #include #ifdef WITH_MHASH #include @@ -360,7 +361,7 @@ void md2line(struct md_container* md,struct db_line* line) { error(255,"Line has %llu\n",a); \ if (md->calc_attr&a) { \ error(255,"copying %llu\n",a); \ - line->b=(byte*)malloc(c); \ + line->b=(byte*)checked_malloc(c); \ memcpy(line->b,md->b,c); \ } else { \ line->attr&=~a; \ diff --git a/src/util.c b/src/util.c index ea43827..8bebb0d 100644 --- a/src/util.c +++ b/src/util.c @@ -51,6 +51,24 @@ static const int url_value[] = { const int url_ntypes=sizeof(url_value)/sizeof(URL_TYPE); +void* checked_malloc(size_t size) { + void * p = malloc(size); + if (p == NULL) { + error(0, "malloc: failed to allocate %d bytes of memory\n", size); + exit(EXIT_FAILURE); + } + return p; +} + +void* checked_strdup(const char *s) { + void * p = strdup(s); + if (p == NULL) { + error(0, "strdup: failed to allocate memory\n"); + exit(EXIT_FAILURE); + } + return p; +} + int cmpurl(url_t* u1,url_t* u2) { if(u1->type!= u2->type){ @@ -74,10 +92,10 @@ url_t* parse_url(char* val) return NULL; } - u=(url_t*)malloc(sizeof(url_t)); + u=(url_t*)checked_malloc(sizeof(url_t)); /* We don't want to modify the original hence strdup(val) */ - val_copy=strdup(val); + val_copy=checked_strdup(val); for(r=val_copy;r[0]!=':'&&r[0]!='\0';r++); if(r[0]!='\0'){ @@ -95,11 +113,11 @@ url_t* parse_url(char* val) switch (u->type) { case url_file : { if(r[0]=='/'&&(r+1)[0]=='/'&&(r+2)[0]=='/'){ - u->value=strdup(r+2); + u->value=checked_strdup(r+2); break; } if(r[0]=='/'&&(r+1)[0]=='/'&&(r+2)[0]!='/'){ - char*hostname=(char*)malloc(sizeof(char)*MAXHOSTNAMELEN); + char*hostname=(char*)checked_malloc(sizeof(char)*MAXHOSTNAMELEN); char* t=r+2; r+=2; for(i=0;r[0]!='/'&&r[0]!='\0';r++,i++); @@ -107,7 +125,7 @@ url_t* parse_url(char* val) error(0,"Invalid file-URL,no path after hostname: file:%s\n",t); return NULL; } - u->value=strdup(r); + u->value=checked_strdup(r); r[0]='\0'; if(gethostname(hostname,MAXHOSTNAMELEN)==-1){ strncpy(hostname,"localhost", 10); @@ -123,14 +141,14 @@ url_t* parse_url(char* val) free(hostname); break; } - u->value=strdup(r); + u->value=checked_strdup(r); break; } case url_https : case url_http : case url_ftp : { - u->value=strdup(val); + u->value=checked_strdup(val); break; } case url_unknown : { @@ -138,7 +156,7 @@ url_t* parse_url(char* val) break; } default : { - u->value=strdup(r); + u->value=checked_strdup(r); break; } } @@ -203,7 +221,7 @@ char* encode_string (const char* s) } } - res = (char *)malloc (i + 1); + res = (char *)checked_malloc (i + 1); s = b; for (p = res; *s; s++){ if (strchr (URL_UNSAFE, *s)||!ISPRINT((int)*s)) @@ -226,7 +244,7 @@ char* perm_to_char(mode_t perm) char*pc=NULL; int i=0; - pc=(char*)malloc(sizeof(char)*11); + pc=(char*)checked_malloc(sizeof(char)*11); for(i=0;i<10;i++){ pc[i]='-'; } @@ -371,7 +389,7 @@ char *expand_tilde(char *path) { path_len = strlen(path+sizeof(char)); homedir_len = strlen(homedir); full_len = homedir_len+path_len; - full = malloc(sizeof(char) * (full_len+1)); + full = checked_malloc(sizeof(char) * (full_len+1)); strncpy(full, homedir, homedir_len); strncpy(full+homedir_len, path+sizeof(char), path_len); full[full_len] = '\0'; -- 2.27.0