diff options
| -rw-r--r-- | Makefile | 13 | ||||
| -rw-r--r-- | src/edit.c | 28 | ||||
| -rw-r--r-- | src/hook.c | 42 | ||||
| -rw-r--r-- | src/policy.c | 15 | ||||
| -rw-r--r-- | src/validate.c | 32 | ||||
| -rw-r--r-- | test/parse.c | 71 | ||||
| -rwxr-xr-x | test/run.sh | 80 |
7 files changed, 259 insertions, 22 deletions
@@ -11,15 +11,22 @@ CMD_OBJ = $(CMD_SRC:%=$(OBJ_DIR)/%.o) TST_SRC = $(wildcard $(TST_DIR)/*.c) TST_RUN = $(TST_SRC:$(TST_DIR)/%.c=$(OBJ_DIR)/test_runner/%) +TST_OBJ = $(TST_SRC:$(TST_DIR)/%.c=$(OBJ_DIR)/$(TST_DIR)/%.c.o) CC_SRC = $(wildcard $(SRC_DIR)/*.c) CC_OBJ = $(CC_SRC:%=$(OBJ_DIR)/%.o) +# -MMD writes a makefile of the headers each object actually includes. Without +# it, editing src/authz.h rebuilds nothing: the objects still match their .c +# files, so `make test' passes against a binary that is not the code on disk. CFLAGS = -std=c99 -Wall -Wextra -Wpedantic -I$(SRC_DIR) CFLAGS += -D_POSIX_C_SOURCE=200809L +CFLAGS += -MMD -MP CFLAGS += -g LDFLAGS = +DEPS = $(CC_OBJ:.o=.d) $(CMD_OBJ:.o=.d) $(TST_OBJ:.o=.d) + $(OBJ_DIR)/%.c.o: %.c @mkdir -p $(@D) $(CC) $(CFLAGS) -o $@ -c $< @@ -50,7 +57,7 @@ test: $(PROG) $(TST_RUN) .PHONY: sanitizer sanitizer: CFLAGS := -std=c99 -Wall -Wextra -Wpedantic -I$(SRC_DIR) \ -D_POSIX_C_SOURCE=200809L -O1 -g -fsanitize=address,undefined \ - -fno-omit-frame-pointer + -fno-omit-frame-pointer -MMD -MP sanitizer: LDFLAGS := -fsanitize=address,undefined sanitizer: test @@ -101,3 +108,7 @@ clean: @$(RM) -rf obj/ @$(RM) -rf $(TST_DIR)/tmp @$(RM) $(PROG) + +# Included last: the .d files only exist after something has been compiled, and +# a missing one is not an error. +-include $(DEPS) @@ -194,6 +194,26 @@ static int is_rule_for(const char *line, const char *user, const char *repo) return same; } +// A ref pattern reaches the file as one field. Whitespace would split it into +// two patterns when the file is read back - granting a ref nobody asked for - and +// '#' starts a comment, so the pattern would be quietly cut short. An argument +// carrying either means something other than what was typed. + +static int ref_field_ok(const char *s) +{ + size_t i; + + if (s[0] == '\0') + return 0; + for (i = 0; s[i] != '\0'; i++) { + unsigned char c = (unsigned char)s[i]; + + if (isspace(c) || c == '#') + return 0; + } + return 1; +} + static char *rule_text(const char *user, const char *repo, const char *level, int nrefs, char **refs) { @@ -315,6 +335,7 @@ int cmd_grant(const char *path, edit_opts o, int argc, char **argv) int i; int first = -1; int replaced = -1; + int appended = 0; int dropped = 0; int created = 0; @@ -343,6 +364,10 @@ int cmd_grant(const char *path, edit_opts o, int argc, char **argv) progname, argv[i]); return 64; } + if (!ref_field_ok(argv[i])) { + fprintf(stderr, "%s: not a ref pattern: %s\n", progname, argv[i]); + return 64; + } } if (authz_reject_options(argc, argv) != 0) @@ -382,6 +407,7 @@ int cmd_grant(const char *path, edit_opts o, int argc, char **argv) if (first < 0) { lines_push(&lines, text); // the buffer belongs to the file now replaced = (int)lines.n; + appended = 1; } else { size_t w; @@ -404,7 +430,7 @@ int cmd_grant(const char *path, edit_opts o, int argc, char **argv) else if (dropped > 0) snprintf(what, sizeof(what), "granted %s %s %s (line %d, %d duplicate(s) removed)", argv[0], argv[1], argv[2], replaced, dropped); - else if ((int)lines.n == replaced) + else if (appended) snprintf(what, sizeof(what), "granted %s %s %s (appended as line %d)", argv[0], argv[1], argv[2], replaced); else @@ -225,14 +225,28 @@ static const char *identity(hook_opts o) return NULL; } -static int flag(const char *arg, const char *name, const char **slot) +// "--ff=yes" and "--ff yes" mean the same thing. The usage text writes the +// two-word form and validate accepts both, so authorize accepts both: an option +// the help offers must not come back as "unknown argument". + +static int option(int argc, char **argv, int *i, const char *name, + const char **slot) { size_t n = strlen(name); - if (strncmp(arg, name, n) != 0 || arg[n] != '=') - return 0; - *slot = arg + n + 1; - return 1; + if (strcmp(argv[*i], name) == 0) { + if (*i + 1 >= argc) { + fprintf(stderr, "%s: %s wants a value\n", progname, name); + return -1; + } + *slot = argv[++*i]; + return 1; + } + if (strncmp(argv[*i], name, n) == 0 && argv[*i][n] == '=') { + *slot = argv[*i] + n + 1; + return 1; + } + return 0; } int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv) @@ -253,13 +267,17 @@ int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv) o.head = "auto"; for (i = 0; i < argc; i++) { - if (flag(argv[i], "--user", &o.user)) - continue; - if (flag(argv[i], "--repo", &o.repo)) - continue; - if (flag(argv[i], "--ff", &o.ff)) - continue; - if (flag(argv[i], "--head", &o.head)) + int got = option(argc, argv, &i, "--user", &o.user); + + if (got == 0) + got = option(argc, argv, &i, "--repo", &o.repo); + if (got == 0) + got = option(argc, argv, &i, "--ff", &o.ff); + if (got == 0) + got = option(argc, argv, &i, "--head", &o.head); + if (got < 0) + return 64; + if (got > 0) continue; if (strcmp(argv[i], "--no-git") == 0) { o.use_git = 0; diff --git a/src/policy.c b/src/policy.c index aae99a1..47a64f1 100644 --- a/src/policy.c +++ b/src/policy.c @@ -53,13 +53,13 @@ int authz_split_ws(char *line, char **f, int max) p++; if (*p == '\0') break; + if (n == max) + return n + 1; // "too many"; the caller decides f[n++] = p; while (*p != '\0' && !isspace((unsigned char)*p)) p++; if (*p != '\0') *p++ = '\0'; - if (n == max) - return n + 1; // "too many"; the caller decides } return n; } @@ -323,6 +323,17 @@ int policy_load(policy_t *p) parse_rule(p, text, lineno); } free(line); + // A read that stopped because the file would not give an answer is not an + // empty policy: a directory, an I/O error halfway, a mount pulled out. The + // lines read so far are not the file, so this is the broken-host answer (3), + // not the empty-policy answer (0). + if (ferror(f)) { + fclose(f); + p->unreadable = 1; + fprintf(stderr, "%s: cannot read the policy: ", p->path); + perror(""); + return -2; + } fclose(f); return p->errors > 0 ? -1 : 0; } diff --git a/src/validate.c b/src/validate.c index 7004731..91be26a 100644 --- a/src/validate.c +++ b/src/validate.c @@ -65,6 +65,18 @@ static int ci_equal(const char *a, const char *b) return *a == '\0' && *b == '\0'; } +// git reads a section name without regard to case, so [CORE] opens core. +static int ci_prefix(const char *line, const char *want) +{ + while (*want != '\0') { + if (tolower((unsigned char)*line) != tolower((unsigned char)*want)) + return 0; + line++; + want++; + } + return 1; +} + static char *strip(char *s) { char *end; @@ -103,7 +115,7 @@ static int ini_value(const char *path, const char *section, const char *key, if (*t == '\0' || *t == '#' || *t == ';') continue; if (*t == '[') { - in = strncmp(t, here, strlen(here)) == 0; + in = ci_prefix(t, here); continue; } if (!in) @@ -130,14 +142,17 @@ static int ini_value(const char *path, const char *section, const char *key, return found; } -// sshd_config's "Directive value", last one winning, case-insensitive. +// sshd_config's "Directive value", case-insensitive. sshd keeps the first value +// it reads for a directive, not the last, so the first match is the answer and +// every later line is one sshd never looked at - which is the difference between +// a host that accepts key identities and one that silently drops them. + static int directive_value(const char *path, const char *name, char *out, size_t outsz) { FILE *f; char *line = NULL; size_t cap = 0; ssize_t len; - int found = 0; f = fopen(path, "r"); if (f == NULL) @@ -157,11 +172,13 @@ static int directive_value(const char *path, const char *name, char *out, size_t if (!ci_equal(t, name)) continue; snprintf(out, outsz, "%s", strip(sp + 1)); - found = 1; + free(line); + fclose(f); + return 1; } free(line); fclose(f); - return found; + return 0; } static void check_repos(const policy_t *p, const char *root) @@ -268,9 +285,12 @@ static void check_sshd(const policy_t *p, validate_opts o) vok("PermitUserEnvironment yes"); rc = directive_value(o.sshd_config, "ForceCommand", value, sizeof(value)); - if (rc == 0) + if (rc != 1) verr("%s sets no ForceCommand: a key can run an interactive shell", o.sshd_config); + else if (ci_equal(value, "none")) + verr("ForceCommand is none in %s: that is how a Match block turns it back " + "off, and a key can run an interactive shell", o.sshd_config); else vok("ForceCommand %s", value); diff --git a/test/parse.c b/test/parse.c index 5c8cb5f..49fdb55 100644 --- a/test/parse.c +++ b/test/parse.c @@ -2,6 +2,9 @@ // poisons the whole file. #include "test.h" +#include <errno.h> +#include <sys/stat.h> + static void test_parse(void) { policy_t p; @@ -117,12 +120,80 @@ static void test_known_users(void) policy_free(&p); } +// A field count of exactly MAX_FIELDS is not "too many": the array has room for +// it. Only the 65th field on a line is the one that does not fit. +static void test_split_field_limit(void) +{ + char line[MAX_FIELDS * 3]; + char *f[MAX_FIELDS]; + int i; + int n; + + line[0] = '\0'; + for (i = 0; i < MAX_FIELDS; i++) + strcat(line, i ? " x" : "x"); + n = authz_split_ws(line, f, MAX_FIELDS); + CHECK_INT(n, MAX_FIELDS); + + // splitting writes over the text, so the line is built again + line[0] = '\0'; + for (i = 0; i <= MAX_FIELDS; i++) + strcat(line, i ? " x" : "x"); + n = authz_split_ws(line, f, MAX_FIELDS); + CHECK(n > MAX_FIELDS); // one field too many, said so +} + +// A rule line with exactly MAX_FIELDS fields is a rule like any other: 61 ref +// patterns on a user and repository. It used to be refused as "too many". +static void test_rule_at_the_field_limit(void) +{ + policy_t p; + char text[MAX_FIELDS * 20]; + int i; + int rc; + + strcpy(text, "bob team write"); + for (i = 0; i < MAX_FIELDS - 3; i++) + strcat(text, " refs/heads/r"); + strcat(text, "\n"); + + rc = load_text(&p, text); + CHECK_INT(rc, 0); + CHECK_INT(p.nrules, 1); + if (p.nrules == 1) // else the CHECK above already said so + CHECK_INT(p.rules[0].nrefs, MAX_FIELDS - 3); + policy_free(&p); +} + +// A directory is not a policy that happens to be empty. Nothing can be read out +// of it, which is the same broken host as a missing file, and has to answer 3 +// rather than 1 so a monitor can tell the two apart. +static void test_policy_that_is_a_directory(void) +{ + policy_t p; + int rc; + + if (mkdir(tmproot, 0755) != 0 && errno != EEXIST) { + note("cannot make %s; skipped", tmproot); + return; + } + + policy_init(&p, tmproot); + rc = policy_load(&p); + CHECK_INT(rc, -2); + CHECK(p.unreadable); + policy_free(&p); +} + int main(void) { test_init(""); test_parse(); test_known_users(); + test_split_field_limit(); + test_rule_at_the_field_limit(); + test_policy_that_is_a_directory(); return test_report("parse"); } diff --git a/test/run.sh b/test/run.sh index bc1a9e4..a608468 100755 --- a/test/run.sh +++ b/test/run.sh @@ -388,6 +388,34 @@ expect "no refs at all is nothing to deny" 0 "-" "$rc" "$out" out=$(printf '\n\n' | push bob team); rc=$? expect "blank lines are skipped" 0 "-" "$rc" "$out" +# ------------------------------------------------------------ option forms + +# The usage text writes these as two words ("--user U", "--ff auto|yes|no"), and +# validate accepts both spellings, so authorize has to accept them too: a person +# following the help must not be told the option is unknown. + +section "authorize: the options as the usage spells them" + +out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x | + "$AUTHZ" --policy "$P" authorize --user bob --repo team --no-git 2>&1); rc=$? +expect "--user and --repo as two words" 0 "-" "$rc" "$out" + +out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x | + "$AUTHZ" --policy "$P" authorize --user bob --repo team --ff no 2>&1); rc=$? +expect "--ff no as two words costs force" 1 "needs force" "$rc" "$out" + +out=$(printf '%s %s %s\n' "$B" "$ZERO" refs/heads/main | + "$AUTHZ" --policy "$P" authorize --user bob --repo team --head none 2>&1); rc=$? +expect "--head none as two words" 1 "-" "$rc" "$out" + +out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x | + "$AUTHZ" --policy "$P" authorize --user=bob --repo=team --no-git 2>&1); rc=$? +expect "and the = form still works" 0 "-" "$rc" "$out" + +out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x | + "$AUTHZ" --policy "$P" authorize --user 2>&1); rc=$? +expect "an option with no value says so" 64 "wants a value" "$rc" "$out" + # --------------------------------------------------------- the hook by name section "authorize: reached as pre-receive" @@ -644,6 +672,33 @@ else fi expect "warnings alone still let the host serve" 0 "is in force" "$rc" "$out" +# sshd reads a directive once and keeps the first answer it got, and "none" is +# how a Match block turns a ForceCommand back off - both of those are the same +# wide-open state as having no ForceCommand at all, so validate has to see them +# that way too. + +mkhost +printf 'PermitUserEnvironment yes\nForceCommand none\n' >"$H/sshd_config" +out=$(host_validate); rc=$? +expect "ForceCommand none is as open as no ForceCommand" 1 "interactive shell" "$rc" "$out" + +mkhost +printf 'PermitUserEnvironment no\nPermitUserEnvironment yes\n' >"$H/sshd_config" +out=$(host_validate); rc=$? +expect "sshd keeps the first PermitUserEnvironment, not the last" 1 "anonymous" "$rc" "$out" + +mkhost +printf 'PermitUserEnvironment yes\nForceCommand /usr/bin/git-shell\nForceCommand none\n' >"$H/sshd_config" +out=$(host_validate); rc=$? +expect "and the first ForceCommand is the one that counts" 0 "ForceCommand /usr/bin/git-shell" "$rc" "$out" + +# git spells section names case-insensitively, so [CORE] sets core.hooksPath just +# as [core] does; refusing to see it reports a broken host that is not broken. +mkhost +printf '[CORE]\n\thooksPath = %s/hooks\n' "$H" >"$H/gitconfig" +out=$(host_validate); rc=$? +expect "a gitconfig section name is case-insensitive, like git's own" 0 "core.hooksPath" "$rc" "$out" + # -------------------------------------------------------------- edit (grant) section "grant and revoke" @@ -697,6 +752,31 @@ out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob group/team write 2>&1); rc=$? expect "grant rejects a repository name with a slash in it" 64 "invalid repo pattern" "$rc" "$out" out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team write '!refs/heads/main' 2>&1); rc=$? expect "grant has no deny" 64 "no deny" "$rc" "$out" + +# A ref argument has to be one field. The line it lands on is split on whitespace +# again when the file is read, and '#' starts a comment, so writing an argument +# verbatim would either grant more than was asked for or leave a file that no +# longer parses - and a policy that does not parse denies every push there is. +cp "$TMP/edit.conf" "$TMP/edit.conf.before" +out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team write 'refs/heads/a b' 2>&1); rc=$? +expect "a ref argument with a space in it is refused" 64 "not a ref pattern" "$rc" "$out" +out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team write 'refs/heads/a#b' 2>&1); rc=$? +expect "a ref argument with a comment marker is refused" 64 "not a ref pattern" "$rc" "$out" +out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team write "$(printf 'refs/heads/a\nb')" 2>&1); rc=$? +expect "a ref argument with a newline in it is refused" 64 "not a ref pattern" "$rc" "$out" +cmp -s "$TMP/edit.conf" "$TMP/edit.conf.before" || + bad "a refused grant still changed the file" + +# The pair's rule being the last line of the file is still a replacement: nothing +# was appended, and the message says which line was taken over. +cat >"$TMP/lastline.conf" <<'EOF' +alice * admin +bob team write refs/heads/feature/* +EOF +out=$("$AUTHZ" --policy "$TMP/lastline.conf" grant bob team admin 2>&1); rc=$? +expect "replacing the last line says replaced, not appended" 0 "replaced line 2" "$rc" "$out" +out=$("$AUTHZ" --policy "$TMP/lastline.conf" grant zed team admin 2>&1); rc=$? +expect "and a rule with no pair yet is still an append" 0 "appended as line 3" "$rc" "$out" out=$("$AUTHZ" --policy "$TMP/edit.conf" protect refs/heads/main 2>&1); rc=$? expect "protect is gone, and says it is unknown" 64 "unknown command" "$rc" "$out" printf 'set protected-refs refs/heads/main\n' >>"$TMP/edit.conf" |
