1
0
mirror of https://github.com/Yubico/yubico-pam.git synced 2025-02-01 01:52:17 +01:00

Open file handlers manually using open() and fdopen() instead of using 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).
This commit is contained in:
Karol Babioch 2018-04-11 13:37:17 +02:00
parent 0b595ee1cd
commit e5bd2ef844
3 changed files with 24 additions and 6 deletions

View File

@ -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;
}
}
}
}

8
util.c
View File

@ -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;
}

View File

@ -38,6 +38,7 @@
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <ykpers.h>
@ -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;
@ -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;