Check the return value of fcntl() when setting FD_CLOEXEC.

This should never fail unless the fd is invalid.
Problem reported by Matthias Gerstner of SUSE.
This commit is contained in:
Todd C. Miller
2021-01-06 10:16:00 -07:00
parent b132def0b1
commit 71339c574f
5 changed files with 56 additions and 20 deletions

View File

@@ -596,13 +596,14 @@ iolog_open(struct iolog_file *iol, int dfd, int iofd, const char *mode)
iol->compressed = true;
}
}
(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
if (fcntl(fd, F_SETFD, FD_CLOEXEC) != -1) {
#ifdef HAVE_ZLIB_H
if (iol->compressed)
iol->fd.g = gzdopen(fd, mode);
else
if (iol->compressed)
iol->fd.g = gzdopen(fd, mode);
else
#endif
iol->fd.f = fdopen(fd, mode);
iol->fd.f = fdopen(fd, mode);
}
if (iol->fd.v != NULL) {
switch ((flags & O_ACCMODE)) {
case O_WRONLY:

View File

@@ -180,7 +180,10 @@ sudo_debug_new_output(struct sudo_debug_instance *instance,
}
ignore_result(fchown(output->fd, (uid_t)-1, 0));
}
(void)fcntl(output->fd, F_SETFD, FD_CLOEXEC);
if (fcntl(output->fd, F_SETFD, FD_CLOEXEC) == -1) {
sudo_warn_nodebug("%s", output->filename);
goto bad;
}
if (sudo_debug_fds_size < output->fd) {
/* Bump fds size to the next multiple of 4 * NBBY. */
const int old_size = sudo_debug_fds_size / NBBY;

View File

@@ -64,8 +64,12 @@ mysetgrent(void)
{
if (grf == NULL) {
grf = fopen(grfile, "r");
if (grf != NULL)
(void)fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
if (grf != NULL) {
if (fcntl(fileno(grf), F_SETFD, FD_CLOEXEC) == -1) {
fclose(grf);
grf = NULL;
}
}
} else {
rewind(grf);
}
@@ -139,7 +143,10 @@ mygetgrnam(const char *name)
if (grf == NULL) {
if ((grf = fopen(grfile, "r")) == NULL)
return NULL;
(void)fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
if (fcntl(fileno(grf), F_SETFD, FD_CLOEXEC) == -1) {
fclose(grf);
return NULL;
}
} else {
rewind(grf);
}
@@ -162,7 +169,10 @@ mygetgrgid(gid_t gid)
if (grf == NULL) {
if ((grf = fopen(grfile, "r")) == NULL)
return NULL;
(void)fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
if (fcntl(fileno(grf), F_SETFD, FD_CLOEXEC) == -1) {
fclose(grf);
return NULL;
}
} else {
rewind(grf);
}

View File

@@ -56,8 +56,10 @@ linux_audit_open(void)
au_fd = AUDIT_NOT_CONFIGURED;
else
sudo_warn("%s", U_("unable to open audit system"));
} else {
(void)fcntl(au_fd, F_SETFD, FD_CLOEXEC);
} else if (fcntl(au_fd, F_SETFD, FD_CLOEXEC) == -1) {
sudo_warn("%s", U_("unable to open audit system"));
audit_close(au_fd);
au_fd = -1;
}
debug_return_int(au_fd);
}

View File

@@ -86,8 +86,12 @@ setpwent(void)
{
if (pwf == NULL) {
pwf = fopen(pwfile, "r");
if (pwf != NULL)
(void)fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC);
if (pwf != NULL) {
if (fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC) == -1) {
fclose(pwf);
pwf = NULL;
}
}
} else {
rewind(pwf);
}
@@ -164,7 +168,10 @@ getpwnam(const char *name)
if (pwf == NULL) {
if ((pwf = fopen(pwfile, "r")) == NULL)
return NULL;
(void)fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC);
if (fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC) == -1) {
fclose(pwf);
return NULL;
}
} else {
rewind(pwf);
}
@@ -187,7 +194,10 @@ getpwuid(uid_t uid)
if (pwf == NULL) {
if ((pwf = fopen(pwfile, "r")) == NULL)
return NULL;
(void)fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC);
if (fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC) == -1) {
fclose(pwf);
return NULL;
}
} else {
rewind(pwf);
}
@@ -215,8 +225,12 @@ setgrent(void)
{
if (grf == NULL) {
grf = fopen(grfile, "r");
if (grf != NULL)
(void)fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
if (grf != NULL) {
if (fcntl(fileno(grf), F_SETFD, FD_CLOEXEC) == -1) {
fclose(grf);
grf = NULL;
}
}
} else {
rewind(grf);
}
@@ -290,7 +304,10 @@ getgrnam(const char *name)
if (grf == NULL) {
if ((grf = fopen(grfile, "r")) == NULL)
return NULL;
(void)fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
if (fcntl(fileno(grf), F_SETFD, FD_CLOEXEC) == -1) {
fclose(grf);
grf = NULL;
}
} else {
rewind(grf);
}
@@ -313,7 +330,10 @@ getgrgid(gid_t gid)
if (grf == NULL) {
if ((grf = fopen(grfile, "r")) == NULL)
return NULL;
(void)fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
if (fcntl(fileno(grf), F_SETFD, FD_CLOEXEC) == -1) {
fclose(grf);
grf = NULL;
}
} else {
rewind(grf);
}