1
0
mirror of https://github.com/openbsd/src.git synced 2025-01-10 06:47:55 -08:00

Another use for fcntl() and thus of the superfluous 3rd parameter

is when sanitising standard fd's before calling daemon().

Use a tweaked version of the ssh(1) function in all three places
found using fcntl() this way.

ok jca@ beck@
This commit is contained in:
krw 2016-04-02 14:37:42 +00:00
parent 108bfb3cb6
commit 426a6f8cd3
3 changed files with 70 additions and 27 deletions

View File

@ -1,4 +1,4 @@
/* $OpenBSD: isakmpd.c,v 1.103 2015/08/20 22:02:21 deraadt Exp $ */
/* $OpenBSD: isakmpd.c,v 1.104 2016/04/02 14:37:42 krw Exp $ */
/* $EOM: isakmpd.c,v 1.54 2000/10/05 09:28:22 niklas Exp $ */
/*
@ -42,6 +42,7 @@
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <paths.h>
#include "app.h"
#include "conf.h"
@ -99,6 +100,7 @@ static char *report_file = "/var/run/isakmpd.report";
volatile sig_atomic_t sigtermed = 0;
void daemon_shutdown_now(int);
void set_slave_signals(void);
void sanitise_stdfd(void);
/* The default path of the PID file. */
char *pid_file = "/var/run/isakmpd.pid";
@ -360,6 +362,29 @@ write_pid_file(void)
pid_file);
}
void
sanitise_stdfd(void)
{
int nullfd, dupfd;
if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
fprintf(stderr, "Couldn't open /dev/null: %s\n",
strerror(errno));
exit(1);
}
while (++dupfd <= STDERR_FILENO) {
/* Only populate closed fds */
if (fcntl(dupfd, F_GETFL) == -1 && errno == EBADF) {
if (dup2(nullfd, dupfd) == -1) {
fprintf(stderr, "dup2: %s\n", strerror(errno));
exit(1);
}
}
}
if (nullfd > STDERR_FILENO)
close(nullfd);
}
int
main(int argc, char *argv[])
{
@ -374,9 +399,7 @@ main(int argc, char *argv[])
* Make sure init() won't alloc fd 0, 1 or 2, as daemon() will close
* them.
*/
for (n = 0; n <= 2; n++)
if (fcntl(n, F_GETFL, 0) == -1 && errno == EBADF)
(void) open("/dev/null", n ? O_WRONLY : O_RDONLY, 0);
sanitise_stdfd();
/* Log cmd line parsing and initialization errors to stderr. */
log_to(stderr);

View File

@ -1,4 +1,4 @@
/* $OpenBSD: skeyaudit.c,v 1.26 2015/11/01 14:02:37 tim Exp $ */
/* $OpenBSD: skeyaudit.c,v 1.27 2016/04/02 14:37:42 krw Exp $ */
/*
* Copyright (c) 1997, 2000, 2003 Todd C. Miller <Todd.Miller@courtesan.com>
@ -36,9 +36,33 @@
#include <skey.h>
void notify(struct passwd *, int, int);
void sanitise_stdfd(void);
FILE *runsendmail(struct passwd *, int *);
__dead void usage(void);
void
sanitise_stdfd(void)
{
int nullfd, dupfd;
if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
fprintf(stderr, "Couldn't open /dev/null: %s\n",
strerror(errno));
exit(1);
}
while (++dupfd <= STDERR_FILENO) {
/* Only populate closed fds. */
if (fcntl(dupfd, F_GETFL) == -1 && errno == EBADF) {
if (dup2(nullfd, dupfd) == -1) {
fprintf(stderr, "dup2: %s\n", strerror(errno));
exit(1);
}
}
}
if (nullfd > STDERR_FILENO)
close(nullfd);
}
int
main(int argc, char **argv)
{
@ -80,19 +104,15 @@ main(int argc, char **argv)
err(1, "pledge");
}
/* If we are in interactive mode, STDOUT_FILENO *must* be open. */
if (iflag && fcntl(STDOUT_FILENO, F_GETFL) == -1 && errno == EBADF)
exit(1);
/*
* Make sure STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO are open.
* If not, open /dev/null in their place or bail.
* If we are in interactive mode, STDOUT_FILENO *must* be open.
*/
for (ch = STDIN_FILENO; ch <= STDERR_FILENO; ch++) {
if (fcntl(ch, F_GETFL, 0) == -1 && errno == EBADF) {
if (ch == STDOUT_FILENO && iflag)
exit(1); /* need stdout for -i */
if (open(_PATH_DEVNULL, O_RDWR, 0644) == -1)
exit(1); /* just bail */
}
}
sanitise_stdfd();
if (argc - optind > 0)
usage();

View File

@ -1,4 +1,4 @@
/* $OpenBSD: misc.c,v 1.102 2016/03/02 22:42:40 dtucker Exp $ */
/* $OpenBSD: misc.c,v 1.103 2016/04/02 14:37:42 krw Exp $ */
/*
* Copyright (c) 2000 Markus Friedl. All rights reserved.
* Copyright (c) 2005,2006 Damien Miller. All rights reserved.
@ -75,9 +75,9 @@ set_nonblock(int fd)
{
int val;
val = fcntl(fd, F_GETFL, 0);
val = fcntl(fd, F_GETFL);
if (val < 0) {
error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
error("fcntl(%d, F_GETFL): %s", fd, strerror(errno));
return (-1);
}
if (val & O_NONBLOCK) {
@ -99,9 +99,9 @@ unset_nonblock(int fd)
{
int val;
val = fcntl(fd, F_GETFL, 0);
val = fcntl(fd, F_GETFL);
if (val < 0) {
error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
error("fcntl(%d, F_GETFL): %s", fd, strerror(errno));
return (-1);
}
if (!(val & O_NONBLOCK)) {
@ -705,16 +705,16 @@ sanitise_stdfd(void)
strerror(errno));
exit(1);
}
while (++dupfd <= 2) {
/* Only clobber closed fds */
if (fcntl(dupfd, F_GETFL, 0) >= 0)
continue;
if (dup2(nullfd, dupfd) == -1) {
fprintf(stderr, "dup2: %s\n", strerror(errno));
exit(1);
while (++dupfd <= STDERR_FILENO) {
/* Only populate closed fds. */
if (fcntl(dupfd, F_GETFL) == -1 && errno == EBADF) {
if (dup2(nullfd, dupfd) == -1) {
fprintf(stderr, "dup2: %s\n", strerror(errno));
exit(1);
}
}
}
if (nullfd > 2)
if (nullfd > STDERR_FILENO)
close(nullfd);
}