Skip to content

Commit ce0ec8d

Browse files
committed
Free existing contents of struct eventlog before overwriting.
In the unlikely event that there are duplicate keys in info_msgs, free the old string before overwriting with the new one.
1 parent e8695d5 commit ce0ec8d

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

logsrvd/iolog_writer.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,20 @@ strlist_copy(InfoMessage__StringList *strlist)
113113
debug_return_ptr(NULL);
114114
}
115115

116+
/*
117+
* Free a NULL-terminated string vector.
118+
*/
119+
static void
120+
strvec_free(char *vec[])
121+
{
122+
if (vec != NULL) {
123+
char **vp;
124+
for (vp = vec; *vp != NULL; vp++)
125+
free(*vp);
126+
free(vec);
127+
}
128+
}
129+
116130
/*
117131
* Fill in eventlog details from an AcceptMessage
118132
* Caller is responsible for freeing strings in struct eventlog.
@@ -180,6 +194,7 @@ evlog_new(TimeSpec *submit_time, InfoMessage **info_msgs, size_t infolen,
180194
}
181195
if (strcmp(key, "command") == 0) {
182196
if (type_matches(info, source, INFO_MESSAGE__VALUE_STRVAL)) {
197+
free(evlog->command);
183198
if ((evlog->command = strdup(info->u.strval)) == NULL) {
184199
sudo_warnx(U_("%s: %s"), __func__,
185200
U_("unable to allocate memory"));
@@ -205,6 +220,7 @@ evlog_new(TimeSpec *submit_time, InfoMessage **info_msgs, size_t infolen,
205220
case 'r':
206221
if (strcmp(key, "runargv") == 0) {
207222
if (type_matches(info, source, INFO_MESSAGE__VALUE_STRLISTVAL)) {
223+
strvec_free(evlog->runargv);
208224
evlog->runargv = strlist_copy(info->u.strlistval);
209225
if (evlog->runargv == NULL)
210226
goto bad;
@@ -213,6 +229,7 @@ evlog_new(TimeSpec *submit_time, InfoMessage **info_msgs, size_t infolen,
213229
}
214230
if (strcmp(key, "runchroot") == 0) {
215231
if (type_matches(info, source, INFO_MESSAGE__VALUE_STRVAL)) {
232+
free(evlog->runchroot);
216233
if ((evlog->runchroot = strdup(info->u.strval)) == NULL) {
217234
sudo_warnx(U_("%s: %s"), __func__,
218235
U_("unable to allocate memory"));
@@ -223,6 +240,7 @@ evlog_new(TimeSpec *submit_time, InfoMessage **info_msgs, size_t infolen,
223240
}
224241
if (strcmp(key, "runcwd") == 0) {
225242
if (type_matches(info, source, INFO_MESSAGE__VALUE_STRVAL)) {
243+
free(evlog->runcwd);
226244
if ((evlog->runcwd = strdup(info->u.strval)) == NULL) {
227245
sudo_warnx(U_("%s: %s"), __func__,
228246
U_("unable to allocate memory"));
@@ -233,6 +251,7 @@ evlog_new(TimeSpec *submit_time, InfoMessage **info_msgs, size_t infolen,
233251
}
234252
if (strcmp(key, "runenv") == 0) {
235253
if (type_matches(info, source, INFO_MESSAGE__VALUE_STRLISTVAL)) {
254+
strvec_free(evlog->runenv);
236255
evlog->runenv = strlist_copy(info->u.strlistval);
237256
if (evlog->runenv == NULL)
238257
goto bad;
@@ -252,6 +271,7 @@ evlog_new(TimeSpec *submit_time, InfoMessage **info_msgs, size_t infolen,
252271
}
253272
if (strcmp(key, "rungroup") == 0) {
254273
if (type_matches(info, source, INFO_MESSAGE__VALUE_STRVAL)) {
274+
free(evlog->rungroup);
255275
if ((evlog->rungroup = strdup(info->u.strval)) == NULL) {
256276
sudo_warnx(U_("%s: %s"), __func__,
257277
U_("unable to allocate memory"));
@@ -273,6 +293,7 @@ evlog_new(TimeSpec *submit_time, InfoMessage **info_msgs, size_t infolen,
273293
}
274294
if (strcmp(key, "runuser") == 0) {
275295
if (type_matches(info, source, INFO_MESSAGE__VALUE_STRVAL)) {
296+
free(evlog->runuser);
276297
if ((evlog->runuser = strdup(info->u.strval)) == NULL) {
277298
sudo_warnx(U_("%s: %s"), __func__,
278299
U_("unable to allocate memory"));
@@ -285,6 +306,7 @@ evlog_new(TimeSpec *submit_time, InfoMessage **info_msgs, size_t infolen,
285306
case 's':
286307
if (strcmp(key, "source") == 0) {
287308
if (type_matches(info, source, INFO_MESSAGE__VALUE_STRVAL)) {
309+
free(evlog->source);
288310
if ((evlog->source = strdup(info->u.strval)) == NULL) {
289311
sudo_warnx(U_("%s: %s"), __func__,
290312
U_("unable to allocate memory"));
@@ -295,6 +317,7 @@ evlog_new(TimeSpec *submit_time, InfoMessage **info_msgs, size_t infolen,
295317
}
296318
if (strcmp(key, "submitcwd") == 0) {
297319
if (type_matches(info, source, INFO_MESSAGE__VALUE_STRVAL)) {
320+
free(evlog->cwd);
298321
if ((evlog->cwd = strdup(info->u.strval)) == NULL) {
299322
sudo_warnx(U_("%s: %s"), __func__,
300323
U_("unable to allocate memory"));
@@ -305,6 +328,7 @@ evlog_new(TimeSpec *submit_time, InfoMessage **info_msgs, size_t infolen,
305328
}
306329
if (strcmp(key, "submitenv") == 0) {
307330
if (type_matches(info, source, INFO_MESSAGE__VALUE_STRLISTVAL)) {
331+
strvec_free(evlog->submitenv);
308332
evlog->submitenv = strlist_copy(info->u.strlistval);
309333
if (evlog->submitenv == NULL)
310334
goto bad;
@@ -313,6 +337,7 @@ evlog_new(TimeSpec *submit_time, InfoMessage **info_msgs, size_t infolen,
313337
}
314338
if (strcmp(key, "submitgroup") == 0) {
315339
if (type_matches(info, source, INFO_MESSAGE__VALUE_STRVAL)) {
340+
free(evlog->submitgroup);
316341
if ((evlog->submitgroup = strdup(info->u.strval)) == NULL) {
317342
sudo_warnx(U_("%s: %s"), __func__,
318343
U_("unable to allocate memory"));
@@ -323,6 +348,7 @@ evlog_new(TimeSpec *submit_time, InfoMessage **info_msgs, size_t infolen,
323348
}
324349
if (strcmp(key, "submithost") == 0) {
325350
if (type_matches(info, source, INFO_MESSAGE__VALUE_STRVAL)) {
351+
free(evlog->submithost);
326352
if ((evlog->submithost = strdup(info->u.strval)) == NULL) {
327353
sudo_warnx(U_("%s: %s"), __func__,
328354
U_("unable to allocate memory"));
@@ -333,6 +359,7 @@ evlog_new(TimeSpec *submit_time, InfoMessage **info_msgs, size_t infolen,
333359
}
334360
if (strcmp(key, "submituser") == 0) {
335361
if (type_matches(info, source, INFO_MESSAGE__VALUE_STRVAL)) {
362+
free(evlog->submituser);
336363
if ((evlog->submituser = strdup(info->u.strval)) == NULL) {
337364
sudo_warnx(U_("%s: %s"), __func__,
338365
U_("unable to allocate memory"));
@@ -345,6 +372,7 @@ evlog_new(TimeSpec *submit_time, InfoMessage **info_msgs, size_t infolen,
345372
case 't':
346373
if (strcmp(key, "ttyname") == 0) {
347374
if (type_matches(info, source, INFO_MESSAGE__VALUE_STRVAL)) {
375+
free(evlog->ttyname);
348376
if ((evlog->ttyname = strdup(info->u.strval)) == NULL) {
349377
sudo_warnx(U_("%s: %s"), __func__,
350378
U_("unable to allocate memory"));

logsrvd/logsrvd_local.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ store_accept_local(AcceptMessage *msg, uint8_t *buf, size_t len,
210210
}
211211
} else if (closure->log_io) {
212212
/* Sub-command from an existing session, set iolog and offset. */
213+
free(evlog->iolog_path);
213214
evlog->iolog_path = strdup(closure->evlog->iolog_path);
214215
if (evlog->iolog_path == NULL) {
215216
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
@@ -273,6 +274,7 @@ store_reject_local(RejectMessage *msg, uint8_t *buf, size_t len,
273274
closure->evlog = evlog;
274275
} else if (closure->log_io) {
275276
/* Sub-command from an existing session, set iolog and offset. */
277+
free(evlog->iolog_path);
276278
evlog->iolog_path = strdup(closure->evlog->iolog_path);
277279
if (evlog->iolog_path == NULL) {
278280
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
@@ -412,6 +414,7 @@ store_exit_local(ExitMessage *msg, uint8_t *buf, size_t len,
412414
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
413415
"command was killed by SIG%s%s", msg->signal,
414416
msg->dumped_core ? " (core dumped)" : "");
417+
free(evlog->signal_name);
415418
evlog->signal_name = strdup(msg->signal);
416419
if (evlog->signal_name == NULL) {
417420
closure->errstr = _("unable to allocate memory");

plugins/audit_json/audit_json.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ audit_json_open(unsigned int version, sudo_conv_t conversation,
141141
if (plugin_options != NULL) {
142142
for (cur = plugin_options; (cp = *cur) != NULL; cur++) {
143143
if (strncmp(cp, "logfile=", sizeof("logfile=") - 1) == 0) {
144+
free(state.logfile);
144145
state.logfile = strdup(cp + sizeof("logfile=") - 1);
145146
if (state.logfile == NULL)
146147
goto oom;

0 commit comments

Comments
 (0)