Skip to content

Commit c8242e4

Browse files
committed
err/log: Stop closing stderr and stdout during shutdown
Closing these file descriptors can hide sanitiser logs. Fixes bug 33087; bugfix on 0.4.1.6.
1 parent 0ff3e8f commit c8242e4

7 files changed

Lines changed: 11 additions & 71 deletions

File tree

changes/bug33087

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
o Minor bugfixes (logging):
2+
- Stop closing stderr and stdout during shutdown. Closing these file
3+
descriptors can hide sanitiser logs.
4+
Fixes bug 33087; bugfix on 0.4.1.6.

src/lib/err/torerr.c

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -150,32 +150,6 @@ tor_log_reset_sigsafe_err_fds(void)
150150
tor_log_set_sigsafe_err_fds(fds, 1);
151151
}
152152

153-
/**
154-
* Close the list of fds that get errors from inside a signal handler or
155-
* other emergency condition. These fds are shared with the logging code:
156-
* closing them flushes the log buffers, and prevents any further logging.
157-
*
158-
* This function closes stderr, so it should only be called immediately before
159-
* process shutdown.
160-
*/
161-
void
162-
tor_log_close_sigsafe_err_fds(void)
163-
{
164-
int n_fds, i;
165-
const int *fds = NULL;
166-
167-
n_fds = tor_log_get_sigsafe_err_fds(&fds);
168-
for (i = 0; i < n_fds; ++i) {
169-
/* tor_log_close_sigsafe_err_fds_on_error() is called on error and on
170-
* shutdown, so we can't log or take any useful action if close()
171-
* fails. */
172-
(void)close(fds[i]);
173-
}
174-
175-
/* Don't even try logging, we've closed all the log fds. */
176-
tor_log_set_sigsafe_err_fds(NULL, 0);
177-
}
178-
179153
/**
180154
* Set the granularity (in ms) to use when reporting fatal errors outside
181155
* the logging system.
@@ -217,13 +191,12 @@ tor_raw_assertion_failed_msg_(const char *file, int line, const char *expr,
217191

218192
/**
219193
* Call the abort() function to kill the current process with a fatal
220-
* error. But first, close the raw error file descriptors, so error messages
221-
* are written before process termination.
194+
* error. This is a separate function, so that log users don't have to include
195+
* the header for abort().
222196
**/
223197
void
224198
tor_raw_abort_(void)
225199
{
226-
tor_log_close_sigsafe_err_fds();
227200
abort();
228201
}
229202

src/lib/err/torerr.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ void tor_log_err_sigsafe(const char *m, ...);
4040
int tor_log_get_sigsafe_err_fds(const int **out);
4141
void tor_log_set_sigsafe_err_fds(const int *fds, int n);
4242
void tor_log_reset_sigsafe_err_fds(void);
43-
void tor_log_close_sigsafe_err_fds(void);
4443
void tor_log_sigsafe_err_set_granularity(int ms);
4544

4645
void tor_raw_abort_(void) ATTR_NORETURN;

src/lib/err/torerr_sys.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@ subsys_torerr_initialize(void)
2727
static void
2828
subsys_torerr_shutdown(void)
2929
{
30-
/* Stop handling signals with backtraces, then close the logs. */
30+
/* Stop handling signals with backtraces. */
3131
clean_up_backtrace_handler();
32-
/* We can't log any log messages after this point: we've closed all the log
33-
* fds, including stdio. */
34-
tor_log_close_sigsafe_err_fds();
3532
}
3633

3734
const subsys_fns_t sys_torerr = {

src/lib/log/log.c

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -668,12 +668,8 @@ tor_log_update_sigsafe_err_fds(void)
668668

669669
/* log_fds and err_fds contain matching entries: log_fds are the fds used by
670670
* the log module, and err_fds are the fds used by the err module.
671-
* For stdio logs, the log_fd and err_fd values are identical,
672-
* and the err module closes the fd on shutdown.
673-
* For file logs, the err_fd is a dup() of the log_fd,
674-
* and the log and err modules both close their respective fds on shutdown.
675-
* (Once all fds representing a file are closed, the underlying file is
676-
* closed.)
671+
* For stdio logs, the log_fd and err_fd values are identical.
672+
* For file logs, the err_fd is a dup() of the log_fd.
677673
*/
678674
int log_fds[TOR_SIGSAFE_LOG_MAX_FDS];
679675
int err_fds[TOR_SIGSAFE_LOG_MAX_FDS];
@@ -704,12 +700,10 @@ tor_log_update_sigsafe_err_fds(void)
704700
log_fds[n_fds] = lf->fd;
705701
if (lf->needs_close) {
706702
/* File log fds are duplicated, because close_log() closes the log
707-
* module's fd, and tor_log_close_sigsafe_err_fds() closes the err
708703
* module's fd. Both refer to the same file. */
709704
err_fds[n_fds] = dup(lf->fd);
710705
} else {
711-
/* stdio log fds are not closed by the log module.
712-
* tor_log_close_sigsafe_err_fds() closes stdio logs. */
706+
/* stdio log fds are not closed by the log module. */
713707
err_fds[n_fds] = lf->fd;
714708
}
715709
n_fds++;
@@ -838,30 +832,6 @@ logs_free_all(void)
838832
* log mutex. */
839833
}
840834

841-
/** Close signal-safe log files.
842-
* Closing the log files makes the process and OS flush log buffers.
843-
*
844-
* This function is safe to call from a signal handler. It should only be
845-
* called when shutting down the log or err modules. It is currenly called
846-
* by the err module, when terminating the process on an abnormal condition.
847-
*/
848-
void
849-
logs_close_sigsafe(void)
850-
{
851-
logfile_t *victim, *next;
852-
/* We can't LOCK_LOGS() in a signal handler, because it may call
853-
* signal-unsafe functions. And we can't deallocate memory, either. */
854-
next = logfiles;
855-
logfiles = NULL;
856-
while (next) {
857-
victim = next;
858-
next = next->next;
859-
if (victim->needs_close) {
860-
close_log_sigsafe(victim);
861-
}
862-
}
863-
}
864-
865835
/** Remove and free the log entry <b>victim</b> from the linked-list
866836
* logfiles (it is probably present, but it might not be due to thread
867837
* racing issues). After this function is called, the caller shouldn't

src/lib/log/log.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ void logs_set_domain_logging(int enabled);
173173
int get_min_log_level(void);
174174
void switch_logs_debug(void);
175175
void logs_free_all(void);
176-
void logs_close_sigsafe(void);
177176
void add_temp_log(int min_severity);
178177
void close_temp_logs(void);
179178
void rollback_log_changes(void);

src/lib/log/util_bug.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,14 @@ tor_bug_occurred_(const char *fname, unsigned int line,
163163

164164
/**
165165
* Call the tor_raw_abort_() function to close raw logs, then kill the current
166-
* process with a fatal error. But first, close the file-based log file
167-
* descriptors, so error messages are written before process termination.
166+
* process with a fatal error.
168167
*
169168
* (This is a separate function so that we declare it in util_bug.h without
170169
* including torerr.h in all the users of util_bug.h)
171170
**/
172171
void
173172
tor_abort_(void)
174173
{
175-
logs_close_sigsafe();
176174
tor_raw_abort_();
177175
}
178176

0 commit comments

Comments
 (0)