Skip to content

Commit c3e3cab

Browse files
authored
Merge pull request #244 from nalind/doublefree-1.13.1
Fix potential double-frees in the journal log reader
2 parents 21f3ea4 + a1c40ed commit c3e3cab

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

daemon/logger/journald/read.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,20 @@ drain:
237237

238238
// free(NULL) is safe
239239
C.free(unsafe.Pointer(oldCursor))
240-
C.sd_journal_get_cursor(j, &cursor)
240+
if C.sd_journal_get_cursor(j, &cursor) != 0 {
241+
// ensure that we won't be freeing an address that's invalid
242+
cursor = nil
243+
}
241244
return cursor
242245
}
243246

244247
func (s *journald) followJournal(logWatcher *logger.LogWatcher, config logger.ReadConfig, j *C.sd_journal, pfd [2]C.int, cursor *C.char) *C.char {
245248
s.readers.mu.Lock()
246249
s.readers.readers[logWatcher] = logWatcher
247250
s.readers.mu.Unlock()
251+
252+
newCursor := make(chan *C.char)
253+
248254
go func() {
249255
// Keep copying journal data out until we're notified to stop
250256
// or we hit an error.
@@ -271,8 +277,8 @@ func (s *journald) followJournal(logWatcher *logger.LogWatcher, config logger.Re
271277
s.readers.mu.Lock()
272278
delete(s.readers.readers, logWatcher)
273279
s.readers.mu.Unlock()
274-
C.sd_journal_close(j)
275280
close(logWatcher.Msg)
281+
newCursor <- cursor
276282
}()
277283
// Wait until we're told to stop.
278284
select {
@@ -281,6 +287,8 @@ func (s *journald) followJournal(logWatcher *logger.LogWatcher, config logger.Re
281287
C.close(pfd[1])
282288
}
283289

290+
cursor = <-newCursor
291+
284292
return cursor
285293
}
286294

@@ -305,9 +313,9 @@ func (s *journald) readLogs(logWatcher *logger.LogWatcher, config logger.ReadCon
305313
following := false
306314
defer func(pfollowing *bool) {
307315
if !*pfollowing {
308-
C.sd_journal_close(j)
309316
close(logWatcher.Msg)
310317
}
318+
C.sd_journal_close(j)
311319
}(&following)
312320
// Remove limits on the size of data items that we'll retrieve.
313321
rc = C.sd_journal_set_data_threshold(j, C.size_t(0))

0 commit comments

Comments
 (0)