This diff should probably be three separate diffs, but changes are colliding (i.e. adjacent lines of code are being modified within different diffs).
The diff contains three modifications: - removal of unused logging code (see previous diff); - a cleanup of parse.y, converting it to use log.c; and - string to character array changes in preparation for the next pledge diff. Regards, Rob Index: ifstated.c =================================================================== RCS file: /cvs/src/usr.sbin/ifstated/ifstated.c,v retrieving revision 1.50 diff -u -p -r1.50 ifstated.c --- ifstated.c 4 Jul 2017 21:09:52 -0000 1.50 +++ ifstated.c 15 Jul 2017 17:56:31 -0000 @@ -639,7 +639,6 @@ clear_config(struct ifsd_config *oconf) TAILQ_REMOVE(&oconf->states, state, entries); remove_action(state->init, state); remove_action(state->body, state); - free(state->name); free(state); } remove_action(oconf->initstate.init, &oconf->initstate); @@ -656,12 +655,7 @@ remove_action(struct ifsd_action *action return; switch (action->type) { - case IFSD_ACTION_LOG: - free(action->act.logmessage); - break; case IFSD_ACTION_COMMAND: - free(action->act.command); - break; case IFSD_ACTION_CHANGESTATE: break; case IFSD_ACTION_CONDITION: @@ -697,7 +691,6 @@ remove_expression(struct ifsd_expression if (--expression->u.external->refcount == 0) { TAILQ_REMOVE(&state->external_tests, expression->u.external, entries); - free(expression->u.external->command); event_del(&expression->u.external->ev); free(expression->u.external); } Index: ifstated.h =================================================================== RCS file: /cvs/src/usr.sbin/ifstated/ifstated.h,v retrieving revision 1.16 diff -u -p -r1.16 ifstated.h --- ifstated.h 4 Jul 2017 21:04:14 -0000 1.16 +++ ifstated.h 15 Jul 2017 17:56:31 -0000 @@ -28,6 +28,7 @@ #include <sys/types.h> #include <sys/queue.h> +#include <sys/syslimits.h> struct ifsd_expression; TAILQ_HEAD(ifsd_expression_list, ifsd_expression); @@ -45,15 +46,15 @@ struct ifsd_ifstate { }; struct ifsd_external { - TAILQ_ENTRY(ifsd_external) entries; - struct event ev; - struct ifsd_expression_list expressions; - char *command; - int prevstatus; - u_int32_t frequency; - u_int32_t refcount; - u_int32_t lastexec; - pid_t pid; + TAILQ_ENTRY(ifsd_external) entries; + struct event ev; + struct ifsd_expression_list expressions; + char command[LINE_MAX]; + int prevstatus; + u_int32_t frequency; + u_int32_t refcount; + u_int32_t lastexec; + pid_t pid; }; struct ifsd_action; @@ -63,17 +64,15 @@ struct ifsd_action { TAILQ_ENTRY(ifsd_action) entries; struct ifsd_action *parent; union { - char *logmessage; - char *command; + char command[LINE_MAX]; struct ifsd_state *nextstate; - char *statename; + char statename[NAME_MAX]; struct { struct ifsd_action_list actions; struct ifsd_expression *expression; } c; } act; u_int32_t type; -#define IFSD_ACTION_LOG 0 #define IFSD_ACTION_COMMAND 1 #define IFSD_ACTION_CHANGESTATE 2 #define IFSD_ACTION_CONDITION 3 @@ -111,7 +110,7 @@ struct ifsd_state { struct ifsd_action *init; struct ifsd_action *body; u_int32_t entered; - char *name; + char name[NAME_MAX]; }; TAILQ_HEAD(ifsd_state_list, ifsd_state); Index: parse.y =================================================================== RCS file: /cvs/src/usr.sbin/ifstated/parse.y,v retrieving revision 1.44 diff -u -p -r1.44 parse.y --- parse.y 4 Jul 2017 21:13:03 -0000 1.44 +++ parse.y 15 Jul 2017 17:56:31 -0000 @@ -190,11 +190,12 @@ action : RUN STRING { struct ifsd_action *action; if ((action = calloc(1, sizeof(*action))) == NULL) - err(1, "action: calloc"); + fatal("run calloc"); action->type = IFSD_ACTION_COMMAND; - action->act.command = $2; - if (action->act.command == NULL) - err(1, "action: strdup"); + if (strlcpy(action->act.command, $2, + sizeof(action->act.command)) >= sizeof(action->act.command)) + fatalx("action command strlcpy truncation"); + free($2); TAILQ_INSERT_TAIL(&curaction->act.c.actions, action, entries); } @@ -207,9 +208,12 @@ action : RUN STRING { YYERROR; } if ((action = calloc(1, sizeof(*action))) == NULL) - err(1, "action: calloc"); + fatal("action calloc"); action->type = IFSD_ACTION_CHANGESTATE; - action->act.statename = $2; + if (strlcpy(action->act.statename, $2, + sizeof(action->act.statename)) >= sizeof(action->act.statename)) + fatalx("action state name strlcpy truncation"); + free($2); TAILQ_INSERT_TAIL(&curaction->act.c.actions, action, entries); } @@ -217,7 +221,7 @@ action : RUN STRING { struct ifsd_action *action; if ((action = calloc(1, sizeof(*action))) == NULL) - err(1, "action: calloc"); + fatal("action calloc"); action->type = IFSD_ACTION_CONDITION; TAILQ_INIT(&action->act.c.actions); TAILQ_INSERT_TAIL(&curaction->act.c.actions, @@ -275,7 +279,7 @@ ext_test : STRING EVERY NUMBER { term : if_test { if (($$ = calloc(1, sizeof(*$$))) == NULL) - err(1, NULL); + fatal(NULL); curaction->act.c.expression = $$; $$->type = IFSD_OPER_IFSTATE; $$->u.ifstate = $1; @@ -283,7 +287,7 @@ term : if_test { } | ext_test { if (($$ = calloc(1, sizeof(*$$))) == NULL) - err(1, NULL); + fatal(NULL); curaction->act.c.expression = $$; $$->type = IFSD_OPER_EXTERNAL; $$->u.external = $1; @@ -296,7 +300,7 @@ term : if_test { expr : '!' expr %prec UNARY { if (($$ = calloc(1, sizeof(*$$))) == NULL) - err(1, NULL); + fatal(NULL); curaction->act.c.expression = $$; $$->type = IFSD_OPER_NOT; $2->parent = $$; @@ -304,7 +308,7 @@ expr : '!' expr %prec UNARY { } | expr AND expr { if (($$ = calloc(1, sizeof(*$$))) == NULL) - err(1, NULL); + fatal(NULL); curaction->act.c.expression = $$; $$->type = IFSD_OPER_AND; $1->parent = $$; @@ -314,7 +318,7 @@ expr : '!' expr %prec UNARY { } | expr OR expr { if (($$ = calloc(1, sizeof(*$$))) == NULL) - err(1, NULL); + fatal(NULL); curaction->act.c.expression = $$; $$->type = IFSD_OPER_OR; $1->parent = $$; @@ -335,9 +339,12 @@ state : STATE string { YYERROR; } if ((state = calloc(1, sizeof(*curstate))) == NULL) - err(1, NULL); + fatal(NULL); init_state(state); - state->name = $2; + if (strlcpy(state->name, $2, + sizeof(state->name)) >= sizeof(state->name)) + fatalx("state name strlcpy truncation"); + free($2); curstate = state; curaction = state->body; } optnl '{' optnl stateopts_l '}' { @@ -371,7 +378,7 @@ yyerror(const char *fmt, ...) file->errors++; va_start(ap, fmt); if (vasprintf(&msg, fmt, ap) == -1) - fatalx("yyerror vasprintf"); + fatalx("%s: vasprintf", __func__); va_end(ap); logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg); free(msg); @@ -587,7 +594,7 @@ top: } yylval.v.string = strdup(buf); if (yylval.v.string == NULL) - err(1, "yylex: strdup"); + fatalx("%s: strdup", __func__); return (STRING); } @@ -645,7 +652,7 @@ nodigits: *p = '\0'; if ((token = lookup(buf)) == STRING) if ((yylval.v.string = strdup(buf)) == NULL) - err(1, "yylex: strdup"); + fatalx("%s: strdup", __func__); return (token); } if (c == '\n') { @@ -732,7 +739,7 @@ parse_config(char *filename, int opts) struct ifsd_state *state; if ((conf = calloc(1, sizeof(struct ifsd_config))) == NULL) { - err(1, NULL); + fatal(NULL); return (NULL); } @@ -879,9 +886,10 @@ cmdline_symset(char *s) len = strlen(s) - strlen(val) + 1; if ((sym = malloc(len)) == NULL) - err(1, NULL); + fatal(NULL); - strlcpy(sym, s, len); + if (strlcpy(sym, s, len) >= len) + fatalx("%s: strlcpy truncation", __func__); ret = symset(sym, val + 1, 1); free(sym); @@ -922,12 +930,12 @@ init_state(struct ifsd_state *state) TAILQ_INIT(&state->external_tests); if ((state->init = calloc(1, sizeof(*state->init))) == NULL) - err(1, "init_state: calloc"); + fatal("%s: calloc", __func__); state->init->type = IFSD_ACTION_CONDITION; TAILQ_INIT(&state->init->act.c.actions); if ((state->body = calloc(1, sizeof(*state->body))) == NULL) - err(1, "init_state: calloc"); + fatal("%s: calloc", __func__); state->body->type = IFSD_ACTION_CONDITION; TAILQ_INIT(&state->body->act.c.actions); } @@ -948,7 +956,7 @@ new_ifstate(u_short ifindex, int s) break; if (ifstate == NULL) { if ((ifstate = calloc(1, sizeof(*ifstate))) == NULL) - err(1, NULL); + fatal(NULL); ifstate->ifindex = ifindex; ifstate->ifstate = s; TAILQ_INIT(&ifstate->expressions); @@ -976,9 +984,10 @@ new_external(char *command, u_int32_t fr break; if (external == NULL) { if ((external = calloc(1, sizeof(*external))) == NULL) - err(1, NULL); - if ((external->command = strdup(command)) == NULL) - err(1, NULL); + fatal("%s: calloc", __func__); + if (strlcpy(external->command, command, + sizeof(external->command)) >= sizeof(external->command)) + fatalx("%s: strlcpy truncation", __func__); external->frequency = frequency; TAILQ_INIT(&external->expressions); TAILQ_INSERT_TAIL(&state->external_tests, external, entries);