Skip to content

Commit c783d71

Browse files
patmaddoxdch
authored andcommitted
syslogd: fix memory leak in casper_ttymsg()
nvlist_take_string_array(9) takes ownership of the array and its strings. casper_ttymsg() freed neither, leaking memory on every F_CONSOLE and F_TTY message. On long-running systems with high error-rate syslog traffic routed to /dev/console, syslogd.casper grew to hundreds of MB. Use nvlist_get_string_array(9) to borrow the array instead. Update casper_wallmsg() similarly. Approved by: src (des) Closes: #2222 Fixes: 61a29ec ("syslogd: Log messages using libcasper") MFC after: 3 days MFC to: stable/15 PR: 295488 Reported by: Pat Maddox <pat@patmaddox.com> Reviewed by: markj Tested by: dch
1 parent 22fa41b commit c783d71

1 file changed

Lines changed: 6 additions & 8 deletions

File tree

usr.sbin/syslogd/syslogd_cap_log.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,19 @@ cap_ttymsg(cap_channel_t *chan, struct iovec *iov, int iovcnt,
128128
int
129129
casper_ttymsg(nvlist_t *nvlin, nvlist_t *nvlout)
130130
{
131-
char **nvlstrs;
131+
const char * const *nvlstrs;
132132
struct iovec *iov;
133133
size_t iovcnt;
134134
int tmout;
135135
const char *line;
136136

137-
nvlstrs = nvlist_take_string_array(nvlin, "iov_strs", &iovcnt);
137+
nvlstrs = nvlist_get_string_array(nvlin, "iov_strs", &iovcnt);
138138
assert(iovcnt <= TTYMSG_IOV_MAX);
139139
iov = calloc(iovcnt, sizeof(*iov));
140140
if (iov == NULL)
141141
err(EXIT_FAILURE, "calloc");
142142
for (size_t i = 0; i < iovcnt; ++i) {
143-
iov[i].iov_base = nvlstrs[i];
143+
iov[i].iov_base = __DECONST(char *, nvlstrs[i]);
144144
iov[i].iov_len = strlen(nvlstrs[i]);
145145
}
146146
line = nvlist_get_string(nvlin, "line");
@@ -187,25 +187,23 @@ int
187187
casper_wallmsg(nvlist_t *nvlin)
188188
{
189189
const struct filed *f;
190-
char **nvlstrs;
190+
const char * const *nvlstrs;
191191
struct iovec *iov;
192192
size_t sz;
193193

194194
f = nvlist_get_binary(nvlin, "filed", &sz);
195195
assert(sz == sizeof(*f));
196-
nvlstrs = nvlist_take_string_array(nvlin, "iov_strs", &sz);
196+
nvlstrs = nvlist_get_string_array(nvlin, "iov_strs", &sz);
197197
assert(sz <= TTYMSG_IOV_MAX);
198198
iov = calloc(sz, sizeof(*iov));
199199
if (iov == NULL)
200200
err(EXIT_FAILURE, "calloc");
201201
for (size_t i = 0; i < sz; ++i) {
202-
iov[i].iov_base = nvlstrs[i];
202+
iov[i].iov_base = __DECONST(char *, nvlstrs[i]);
203203
iov[i].iov_len = strlen(nvlstrs[i]);
204204
}
205205
wallmsg(f, iov, sz);
206206

207-
for (size_t i = 0; i < sz; ++i)
208-
free(iov[i].iov_base);
209207
free(iov);
210208
return (0);
211209
}

0 commit comments

Comments
 (0)