Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config file support #326

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ m4/lt~obsolete.m4
tests/.deps/
tests/dlsym_check
man/pam_u2f.8
man/pam_u2f.8.txt
pamu2fcfg/cmdline.c
pamu2fcfg/cmdline.h
pamu2fcfg/pamu2fcfg
man/pamu2fcfg.1
tests/get_devices
tests/cfg
fuzz/fuzz_format_parsers
3 changes: 3 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ AM_CPPFLAGS = $(LIBFIDO2_CFLAGS) $(LIBCRYPTO_CFLAGS)
if ENABLE_FUZZING
AM_CPPFLAGS += -fsanitize=fuzzer-no-link
endif
AM_CPPFLAGS += -D SCONFDIR='"@SCONFDIR@"'

noinst_LTLIBRARIES = libmodule.la
libmodule_la_SOURCES = pam-u2f.c
Expand All @@ -26,6 +27,7 @@ libmodule_la_SOURCES += drop_privs.h
libmodule_la_SOURCES += expand.c
libmodule_la_SOURCES += explicit_bzero.c
libmodule_la_SOURCES += util.c util.h
libmodule_la_SOURCES += cfg.c cfg.h
libmodule_la_LIBADD = -lpam $(LIBFIDO2_LIBS) $(LIBCRYPTO_LIBS)

pampluginexecdir = $(PAMDIR)
Expand All @@ -44,6 +46,7 @@ pam_u2f_la_LDFLAGS += -Wl,--wrap=strdup
pam_u2f_la_LDFLAGS += -Wl,--wrap=calloc
pam_u2f_la_LDFLAGS += -Wl,--wrap=malloc
pam_u2f_la_LDFLAGS += -Wl,--wrap=open
pam_u2f_la_LDFLAGS += -Wl,--wrap=openat
pam_u2f_la_LDFLAGS += -Wl,--wrap=close
pam_u2f_la_LDFLAGS += -Wl,--wrap=fdopen
pam_u2f_la_LDFLAGS += -Wl,--wrap=fstat
Expand Down
32 changes: 32 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ recommended that you start a separate shell with root privileges while
configuring PAM to be able to revert changes if something goes wrong.
Test your configuration thoroughly before closing the root shell.

[[moduleArguments]]
=== Module Arguments

[horizontal]
Expand Down Expand Up @@ -240,6 +241,14 @@ FIDO devices. It is not possible to mix native credentials and SSH
credentials. Once this option is enabled all credentials will be parsed
as SSH.

conf=/path/to/pam_u2f.conf::
Set an alternative location for the <<confFile,configuration file>>.
The supplied path must be absolute and must correspond to an existing
regular file.

The options specified on the module command line override the values
from the <<confFile,configuration file>>.

IMPORTANT: On dynamic networks (e.g. where hostnames are set by DHCP),
users should not rely on the default origin and appid
("pam://$HOSTNAME") but set those parameters explicitly to the same
Expand Down Expand Up @@ -404,6 +413,29 @@ defined in the authorization mapping file. If during an authentication attempt
a connected device is removed or a new device is plugged in, the authentication
restarts from the top of the list.

[[confFile]]
== Configuration file

A configuration file can be used to set the default
<<moduleArguments,module arguments>>.

The file has a `name = value` format, with comments starting with the `#`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section reads like a list, but is not a list, should we rephrase or add bullet points?

(same comment for man pages. as an additional follow up, we might want to consider a pam_u2f.conf.5)

character.
White spaces at the beginning of line, end of line, and around
the `=` sign are ignored.

Any `conf` argument in the configuration file is ignored.

The maximum size for the configuration file is 4 KiB.

The default path for the configuration file is
`/etc/security/pam_u2f.conf`. Note that it may have been set to another
value by the distribution. The default file is allowed to not exist. An
alternative path may be set in the module command line options.

The options specified on the module command line override the values
from the configuration file.

== SELinux Note

Due to an issue with Fedora Linux, and possibly with other
Expand Down
310 changes: 310 additions & 0 deletions cfg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,310 @@
/* Copyright (C) 2021-2024 Yubico AB - See COPYING */
#include <ctype.h>
LDVG marked this conversation as resolved.
Show resolved Hide resolved
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

#include <security/pam_modules.h>

#include "cfg.h"
#include "debug.h"

static void cfg_load_arg_debug(cfg_t *cfg, const char *arg) {
if (strcmp(arg, "debug") == 0)
cfg->debug = 1;
else if (strncmp(arg, "debug_file=", strlen("debug_file=")) == 0) {
debug_close(cfg->debug_file);
cfg->debug_file = debug_open(arg + strlen("debug_file="));
}
}

static void cfg_load_arg(cfg_t *cfg, const char *arg) {
if (strncmp(arg, "max_devices=", 12) == 0) {
sscanf(arg, "max_devices=%u", &cfg->max_devs);
} else if (strcmp(arg, "manual") == 0) {
cfg->manual = 1;
} else if (strcmp(arg, "nouserok") == 0) {
cfg->nouserok = 1;
} else if (strcmp(arg, "openasuser") == 0) {
cfg->openasuser = 1;
} else if (strcmp(arg, "alwaysok") == 0) {
cfg->alwaysok = 1;
} else if (strcmp(arg, "interactive") == 0) {
cfg->interactive = 1;
} else if (strcmp(arg, "cue") == 0) {
cfg->cue = 1;
} else if (strcmp(arg, "nodetect") == 0) {
cfg->nodetect = 1;
} else if (strcmp(arg, "expand") == 0) {
cfg->expand = 1;
} else if (strncmp(arg, "userpresence=", 13) == 0) {
sscanf(arg, "userpresence=%d", &cfg->userpresence);
} else if (strncmp(arg, "userverification=", 17) == 0) {
sscanf(arg, "userverification=%d", &cfg->userverification);
} else if (strncmp(arg, "pinverification=", 16) == 0) {
sscanf(arg, "pinverification=%d", &cfg->pinverification);
} else if (strncmp(arg, "authfile=", 9) == 0) {
cfg->auth_file = arg + 9;
} else if (strcmp(arg, "sshformat") == 0) {
cfg->sshformat = 1;
} else if (strncmp(arg, "authpending_file=", 17) == 0) {
cfg->authpending_file = arg + 17;
} else if (strncmp(arg, "origin=", 7) == 0) {
cfg->origin = arg + 7;
} else if (strncmp(arg, "appid=", 6) == 0) {
cfg->appid = arg + 6;
} else if (strncmp(arg, "prompt=", 7) == 0) {
cfg->prompt = arg + 7;
} else if (strncmp(arg, "cue_prompt=", 11) == 0) {
cfg->cue_prompt = arg + 11;
} else
cfg_load_arg_debug(cfg, arg);
}

static int slurp(int fd, size_t to_read, char **dst) {
char *buffer, *w;

if (to_read > CFG_MAX_FILE_SIZE)
return PAM_SERVICE_ERR;

buffer = malloc(to_read + 1);
LDVG marked this conversation as resolved.
Show resolved Hide resolved
if (!buffer)
return PAM_BUF_ERR;

w = buffer;
while (to_read) {
ssize_t r;

r = read(fd, w, to_read);
if (r < 0) {
free(buffer);
return PAM_SYSTEM_ERR;
}

if (r == 0)
break;

w += r;
to_read -= r;
}

*w = '\0';
*dst = buffer;
return PAM_SUCCESS;
}

// Open the given path while ensuring certain security properties hold.
//
// On success returns PAM_SUCCESS
// On failure returns PAM_SERVICE_ERR and sets errno to indicate the error.
static int open_safely(int *outfd, size_t *outsize, const char *path) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a huge fan of using errno this way. It appears we only need to be able to discern ENOENT, success, and a generic error for everything else. What about returning -ENOENT, a non-negative integer (fd), and -EINVAL respectively instead of the PAM_* value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea behind this is to use PAM_* error codes consistently everywhere for error handling.
It is feasible to do as you suggest, but that would be the only function that does error checking differently.
If that is in your opinion more beautiful than writing errno, then I can comply with your request. Please let me know :-)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we have several other functions that return 0 or 1 or some other error code, I'd rather use "internal" return values for these internal functions, we only really need to concern ourselves with PAM_ return values for functions that set retval in pam_sm_authenticate()

int fd, serrno;
size_t len;
struct stat st;

len = strlen(path);
if (!len || path[0] != '/' || path[len - 1] == '/') {
errno = EINVAL;
return PAM_SERVICE_ERR;
}
Comment on lines +108 to +112
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't *path != '/' suffice? S_ISREG should catch trying to open up a directory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct.
A left-over from previous iteration (path security) where I used strtok_r to split string on /.
On such situation this was a corner case to check.


fd = open(path, O_RDONLY | O_CLOEXEC | O_NOCTTY | O_NOFOLLOW, 0);
if (fd == -1)
return PAM_SERVICE_ERR;

if (fstat(fd, &st))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to be explicit here with fstat(fd, &st) != 0.

goto fail;

errno = EINVAL;
#ifndef PAM_U2F_TESTING
if (st.st_uid != 0)
goto fail;
#endif
if (!S_ISREG(st.st_mode) || st.st_mode & (S_IWGRP | S_IWOTH))
goto fail;

errno = 0;
*outfd = fd;
*outsize = st.st_size;
return PAM_SUCCESS;

fail:
serrno = errno;
close(fd);
errno = serrno;
return PAM_SERVICE_ERR;
}

static char *ltrim(char *s) {
while (isspace((unsigned char) *s))
s++;
return s;
}

static char *rtrim(char *s) {
size_t l;

l = strlen(s);

while (l > 0 && isspace((unsigned char) s[l - 1]))
s[--l] = '\0';

return s;
}

// Transform a line from the configuration file in an equivalent
// module command line value. Comments are stripped.
//
// E.g.
// 'foo = bar' => 'foo=bar'
// 'baz' => 'baz'
// 'baz # etc' => 'baz'
static const char *pack(char *s) {
size_t n;
char *v;

s[strcspn(s, "#")] = '\0';
s = ltrim(s);

v = strchr(s, '=');
if (!v)
return rtrim(s);

*v++ = '\0';
v = ltrim(rtrim(v));

s = rtrim(s);
n = strlen(s);
s[n++] = '=';

memmove(s + n, v, strlen(v) + 1);

return s;
}

static void cfg_load_buffer(cfg_t *cfg, char *buffer) {
char *saveptr_out = NULL, *line;

line = strtok_r(buffer, "\n", &saveptr_out);
while (line) {
char *buf;
const char *arg;

// Pin the next line before messing with the buffer.
buf = line;
line = strtok_r(NULL, "\n", &saveptr_out);

arg = pack(buf);
if (!*arg)
continue;

cfg_load_arg(cfg, arg);
}
}

static int cfg_load_defaults(cfg_t *cfg, const char *config_path) {
int fd, r;
size_t fsize;
char *buffer = NULL;

r = open_safely(&fd, &fsize, config_path ? config_path : CFG_DEFAULT_PATH);
if (r)
return r;

if (errno == ENOENT) {
// Only the default config file is allowed to be missing
return config_path ? PAM_SERVICE_ERR : PAM_SUCCESS;
}

r = slurp(fd, fsize, &buffer);
if (r)
goto exit;

cfg_load_buffer(cfg, buffer);
cfg->defaults_buffer = buffer;
buffer = NULL;
r = PAM_SUCCESS;

exit:
free(buffer);
close(fd);
return r;
}

static void cfg_reset(cfg_t *cfg) {
memset(cfg, 0, sizeof(cfg_t));
cfg->debug_file = DEFAULT_DEBUG_FILE;
cfg->userpresence = -1;
cfg->userverification = -1;
cfg->pinverification = -1;
}

int cfg_init(cfg_t *cfg, int flags, int argc, const char **argv) {
int i, r;
const char *config_path = NULL;

(void) flags; // prevent unused warning when unit-testing.

cfg_reset(cfg);

for (i = 0; i < argc; i++) {
if (strncmp(argv[i], "conf=", strlen("conf=")) == 0)
config_path = argv[i] + strlen("conf=");
else
cfg_load_arg_debug(cfg, argv[i]);
}

r = cfg_load_defaults(cfg, config_path);
if (r != PAM_SUCCESS)
goto exit;

for (i = 0; i < argc; i++) {
if (strncmp(argv[i], "conf=", strlen("conf=")) == 0)
continue;

Comment on lines +265 to +267
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since cfg_load_arg() does not handle conf=, should we just drop this short circuiting?

cfg_load_arg(cfg, argv[i]);
}

exit:
if (cfg->debug) {
debug_dbg(cfg, "called.");
debug_dbg(cfg, "flags %d argc %d", flags, argc);
for (i = 0; i < argc; i++) {
debug_dbg(cfg, "argv[%d]=%s", i, argv[i]);
}
debug_dbg(cfg, "max_devices=%d", cfg->max_devs);
debug_dbg(cfg, "debug=%d", cfg->debug);
debug_dbg(cfg, "interactive=%d", cfg->interactive);
debug_dbg(cfg, "cue=%d", cfg->cue);
debug_dbg(cfg, "nodetect=%d", cfg->nodetect);
debug_dbg(cfg, "userpresence=%d", cfg->userpresence);
debug_dbg(cfg, "userverification=%d", cfg->userverification);
debug_dbg(cfg, "pinverification=%d", cfg->pinverification);
debug_dbg(cfg, "manual=%d", cfg->manual);
debug_dbg(cfg, "nouserok=%d", cfg->nouserok);
debug_dbg(cfg, "openasuser=%d", cfg->openasuser);
debug_dbg(cfg, "alwaysok=%d", cfg->alwaysok);
debug_dbg(cfg, "sshformat=%d", cfg->sshformat);
debug_dbg(cfg, "expand=%d", cfg->expand);
debug_dbg(cfg, "authfile=%s", cfg->auth_file ? cfg->auth_file : "(null)");
debug_dbg(cfg, "authpending_file=%s",
cfg->authpending_file ? cfg->authpending_file : "(null)");
debug_dbg(cfg, "origin=%s", cfg->origin ? cfg->origin : "(null)");
debug_dbg(cfg, "appid=%s", cfg->appid ? cfg->appid : "(null)");
debug_dbg(cfg, "prompt=%s", cfg->prompt ? cfg->prompt : "(null)");
}

if (r != PAM_SUCCESS)
cfg_free(cfg);

return r;
}

void cfg_free(cfg_t *cfg) {
debug_close(cfg->debug_file);
free(cfg->defaults_buffer);
cfg_reset(cfg);
}
Loading
Loading