Skip to content

Commit cdf92b7

Browse files
committed
Check at startup that standard streams are open
Various code that manipulates file descriptors assumes that they _are_ open. When changing this code, I don't want to consider the consequences of them not being open. Just check at startup that they are open and exit if they are not.
1 parent adbf4a2 commit cdf92b7

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

daemon/qrexec-daemon.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -1541,12 +1541,10 @@ int main(int argc, char **argv)
15411541
int null_fd = open("/dev/null", O_RDONLY|O_NOCTTY);
15421542
if (null_fd < 0)
15431543
err(1, "open(%s)", "/dev/null");
1544-
if (null_fd > 0) {
1545-
if (dup2(null_fd, 0) != 0)
1546-
err(1, "dup2(%d, 0)", null_fd);
1547-
if (null_fd > 2 && close(null_fd) != 0)
1548-
err(1, "close(%d)", null_fd);
1549-
}
1544+
if (dup2(null_fd, 0) != 0)
1545+
err(1, "dup2(%d, 0)", null_fd);
1546+
if (close(null_fd) != 0)
1547+
err(1, "close(%d)", null_fd);
15501548
}
15511549

15521550
setup_logging("qrexec-daemon");

libqrexec/log.c

+9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <unistd.h>
2727
#include <string.h>
2828
#include <errno.h>
29+
#include <fcntl.h>
30+
#include <err.h>
2931

3032
#include "libqrexec-utils.h"
3133

@@ -79,5 +81,12 @@ void qrexec_log(int level, int errnoval, const char *file, int line,
7981
}
8082

8183
void setup_logging(const char *program_name) {
84+
/* Make sure FD 0, 1 and 2 are open. Various code that manipulates
85+
* FDs breaks if they are not. */
86+
for (int i = 0; i < 3; ++i) {
87+
if (fcntl(i, F_GETFD) == -1 && errno == EBADF)
88+
errx(125, "File descriptor %d is closed, cannot continue", i);
89+
}
90+
8291
qrexec_program_name = program_name;
8392
}

0 commit comments

Comments
 (0)