Skip to content

Commit

Permalink
Open file handlers manually using open() and fdopen() instead of usin…
Browse files Browse the repository at this point in the history
…g fopen()

A previous commit (d51124e) added the `e` flag to the `fopen()` calls. However
this flag is not supported on all platforms (MacOS) and will be silently
dropped (see #145). This patch works around those issues by manually opening
the file descriptor using `open()` with the `O_CLOEXEC` flag, and invoking
`fd_open()` on the resulting file descriptor to open an appropriate `FILE`
stream.

This makes sure that all files used by pam_yubico will be opened with the
`O_CLOEXEC` flag on all supported platforms to mitigate issues with missing
`fclose()` invocation (see #136).
  • Loading branch information
kbabioch committed Apr 11, 2018
1 parent 0b595ee commit e5bd2ef
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
11 changes: 8 additions & 3 deletions pam_yubico.c
Original file line number Diff line number Diff line change
Expand Up @@ -814,15 +814,20 @@ parse_cfg (int flags, int argc, const char **argv, struct cfg *cfg)
else
{
struct stat st;
int fd;
FILE *file;
if(lstat(filename, &st) == 0)
{
if(S_ISREG(st.st_mode))
{
file = fopen(filename, "ae");
if(file)
fd = open(filename, O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP);
if (fd >= 0)
{
cfg->debug_file = file;
file = fdopen(fd, "a");
if (file)
{
cfg->debug_file = file;
}
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion util.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,14 @@ int generate_random(void *buf, int len)
{
FILE *u;
int res;
int fd;

u = fopen("/dev/urandom", "re");
fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
if (fd < 0) {
return -1;
}

u = fdopen(fd, "r");
if (!u) {
return -1;
}
Expand Down
11 changes: 9 additions & 2 deletions ykpamcfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>

#include <ykpers.h>

Expand Down Expand Up @@ -143,6 +144,7 @@ do_add_hmac_chalresp(YK_KEY *yk, uint8_t slot, bool verbose, char *output_dir, u
unsigned int response_len;
char *fn;
struct passwd *p;
int fd;
FILE *f = NULL;
struct stat st;

Expand Down Expand Up @@ -237,11 +239,16 @@ do_add_hmac_chalresp(YK_KEY *yk, uint8_t slot, bool verbose, char *output_dir, u

umask(077);

f = fopen (fn, "we");
if (! f) {
fd = open (fn, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, S_IRUSR | S_IWUSR);
if (fd < 0) {
fprintf (stderr, "Failed opening '%s' for writing : %s\n", fn, strerror (errno));
goto out;
}
f = fdopen (fd, "w");
if (! f) {
fprintf (stderr, "fdopen: %s\n", strerror (errno));
goto out;
}

if (! write_chalresp_state (f, &state))
goto out;
Expand Down

0 comments on commit e5bd2ef

Please sign in to comment.