From e755a181b0e4c952c35d80e2aa1acd584d2e4595 Mon Sep 17 00:00:00 2001 From: dev Date: Fri, 13 Nov 2026 20:34:17 +0000 Subject: Editing: options anywhere, a file that is not there yet, one owner per line Three things the editing commands got wrong, two of them in the same direction - taking an argument for something it is not. --out and --stdout were only accepted before the names, which is not where people put them, and "grant bob team write --out /tmp/new.conf" therefore granted a ref pattern called "--out" and wrote it into the file. Options are now pulled out from wherever they sit and what is left compacted, and a name that starts with "--" is reported as the typo it is rather than granted. A file that does not exist was refused with "create one with --out", which tells the operator to do by hand what the command exists to do. grant and protect now start the file, grammar written at the top of it, and say so in the report; revoke reports that there was nothing to remove. A file that does not parse is still refused: editing something the parser cannot read would rewrite the parts that parse and keep the damage. And the line array. rule_text() handed its buffer to the array while the command also freed it, and protect handed a string literal to an array whose free function frees every entry it holds: the first was a use-after-free and the second corrupted the heap badly enough to write garbage into the file being edited. One owner per line now, the copy made where the array takes it, and finish() - where every command ends - releases the file it read. Found by running the suite against AddressSanitizer, which is why `make sanitizer` exists and why everything frees what it read: a process that exits in a millisecond does not need the memory back, but it does need that report to stay empty, or the one real leak hides among forty. --- src/edit.c | 241 ++++++++++++++++++++++++++++++++++++++++--------------------- src/main.c | 195 +++++++++++++++++++++++++++---------------------- 2 files changed, 268 insertions(+), 168 deletions(-) (limited to 'src') diff --git a/src/edit.c b/src/edit.c index 4193b44..1c34ae4 100644 --- a/src/edit.c +++ b/src/edit.c @@ -27,6 +27,7 @@ #include #include +#define HIDDEN_PREFIX "/.authz" #define TEMPLATE_SUFFIX ".XXXXXX" typedef struct { @@ -103,15 +104,22 @@ static int read_lines(const char *path, lines_t *l) static int write_atomic(const lines_t *l, const char *path) { char *dirtext = xstrdup(path); - char *dir = dirname(dirtext); + char *dir; char *template; + size_t need; struct stat st; int fd; size_t i; int had = stat(path, &st) == 0; - template = xmalloc(strlen(dir) + strlen(TEMPLATE_SUFFIX) + 2); - sprintf(template, "%s/.authz%s", dir, TEMPLATE_SUFFIX); + /* dirname may return a pointer into its argument or a fresh one, and may + * fail; the copy is ours either way and dirtext is freed at the end. */ + dir = dirname(dirtext); + if (dir == NULL) + dir = (char *)"."; + need = strlen(dir) + strlen(HIDDEN_PREFIX) + strlen(TEMPLATE_SUFFIX) + 1; + template = xmalloc(need); + snprintf(template, need, "%s%s%s", dir, HIDDEN_PREFIX, TEMPLATE_SUFFIX); fd = mkstemp(template); if (fd < 0) { fprintf(stderr, "%s: cannot write beside %s: %s\n", progname, path, @@ -239,58 +247,97 @@ static char *rule_text(const char *user, const char *repo, const char *level, } /* Fail before touching anything if the file cannot be parsed. */ -static int refuse_broken(const char *path) +/* Load a policy for editing. A policy that does not parse is refused: editing + * a file we cannot read would rewrite the parts we can read and keep the + * damage. A policy that is not there yet is a bootstrap, not an error - the + * source copy has to start somewhere - so the file begins with the grammar + * written down, which is also the least surprising thing to find in it. */ +static int refuse_broken(const char *path, int *created) { policy_t probe; int rc; + if (created != NULL) + *created = 0; policy_init(&probe, path); rc = policy_load(&probe); policy_free(&probe); - if (rc == -2) - return 3; if (rc == -1) { fprintf(stderr, "%s: %s does not parse; fix it before editing\n", progname, path); return 3; } + if (rc == -2 && created != NULL) + *created = 1; return 0; } -/* Parse the shared options of the three editing commands. - * Returns -1 and prints a message on an unknown argument. */ +/* Pull the shared options out of the argument list. They are allowed anywhere + * rather than only at the front, because "grant bob team write --out + * /tmp/new.conf" is what people type - and treating "--out" as a ref pattern + * would grant something nobody asked for. */ int authz_parse_out(int argc, char **argv, edit_opts *o, int *consumed) { + int i; int n = 0; - while (n < argc && strncmp(argv[n], "--", 2) == 0) { - if (strcmp(argv[n], "--stdout") == 0) { + for (i = 0; i < argc; i++) { + if (strcmp(argv[i], "--stdout") == 0) { o->out = "-"; - } else if (strncmp(argv[n], "--out=", 6) == 0) { - o->out = argv[n] + 6; - } else { - fprintf(stderr, "%s: unknown option %s\n", progname, argv[n]); - return -1; + continue; } - n++; + if (strcmp(argv[i], "--out") == 0) { + if (i + 1 >= argc) { + fprintf(stderr, "%s: --out wants a file\n", progname); + return -1; + } + o->out = argv[++i]; + continue; + } + if (strncmp(argv[i], "--out=", 6) == 0) { + o->out = argv[i] + 6; + continue; + } + argv[n++] = argv[i]; } - *consumed = n; + *consumed = argc - n; return 0; } -static int finish(const lines_t *l, const char *path, edit_opts o, const char *what) +int authz_reject_options(int argc, char **argv) { + int i; + + for (i = 0; i < argc; i++) { + if (argv[i][0] == '-' && argv[i][1] == '-') { + fprintf(stderr, "%s: unknown option \"%s\" - options come before the " + "names, and nothing here starts with a dash\n", progname, argv[i]); + return 1; + } + } + return 0; +} + +/* Write the result and release the lines: every command ends here, so this is + * where the file it read stops being held. */ +static int finish(lines_t *l, const char *path, edit_opts o, const char *what) +{ + int rc; + if (o.out != NULL && strcmp(o.out, "-") == 0) { size_t i; for (i = 0; i < l->n; i++) fputs(l->v[i], stdout); fprintf(stderr, "%s: %s (to stdout; %s untouched)\n", progname, what, path); - return 0; + rc = 0; + } else if (write_atomic(l, o.out != NULL ? o.out : path) != 0) { + rc = 1; + } else { + printf("%s: %s: %s\n", progname, what, o.out != NULL ? o.out : path); + rc = 0; } - if (write_atomic(l, o.out != NULL ? o.out : path) != 0) - return 1; - printf("%s: %s: %s\n", progname, what, o.out != NULL ? o.out : path); - return 0; + lines_free(l); + return rc; } /* ------------------------------------------------------------------ commands */ @@ -303,6 +350,7 @@ int cmd_grant(const char *path, edit_opts o, int argc, char **argv) int first = -1; int replaced = -1; int dropped = 0; + int created = 0; if (argc < 3) { fprintf(stderr, "%s: grant wants " @@ -331,9 +379,21 @@ int cmd_grant(const char *path, edit_opts o, int argc, char **argv) } } - if (refuse_broken(path) != 0) + if (authz_reject_options(argc, argv) != 0) + return 64; + if (refuse_broken(path, &created) != 0) return 3; - if (read_lines(path, &lines) != 0) + if (created) { + /* Bootstrapped lines are xstrdup'd for the same reason as the rest: + * lines_free frees every entry it holds, and a string literal is not + * something free() may be handed. */ + lines_init(&lines); + lines_push(&lines, xstrdup("# git-authz policy\n")); + lines_push(&lines, xstrdup("# " + "[ref pattern ...] levels stack: " + "admin > force > write\n")); + lines_push(&lines, xstrdup("set protected-refs refs/heads/main\n")); + } else if (read_lines(path, &lines) != 0) return 3; /* More than one rule for the same pair is legal but pointless - the @@ -344,43 +404,38 @@ int cmd_grant(const char *path, edit_opts o, int argc, char **argv) for (i = 0; i < (int)lines.n; i++) { if (!is_rule_for(lines.v[i], argv[0], argv[1])) continue; - if (first < 0) - first = i; - else - dropped++; + if (first < 0) { + first = i; /* the line the new rule takes over */ + continue; + } + free(lines.v[i]); /* a later rule for the same pair */ + lines.v[i] = NULL; + dropped++; } if (first < 0) { - lines_push(&lines, text); + lines_push(&lines, text); /* the buffer belongs to the file now */ replaced = (int)lines.n; - } else if (dropped == 0) { - free(lines.v[first]); - lines.v[first] = text; - replaced = first + 1; } else { - lines_t kept; + size_t w; - lines_init(&kept); - for (i = 0; i < (int)lines.n; i++) { - if (i == first) { - lines_push(&kept, text); - continue; - } - if (is_rule_for(lines.v[i], argv[0], argv[1])) { - free(lines.v[i]); - continue; - } - lines_push(&kept, lines.v[i]); + free(lines.v[first]); + lines.v[first] = text; + for (i = 0, w = 0; i < (int)lines.n; i++) { + if (lines.v[i] != NULL) + lines.v[w++] = lines.v[i]; } - free(lines.v); - lines = kept; + lines.n = w; replaced = first + 1; } { - char what[128]; + char what[512]; - if (dropped > 0) + if (created) + snprintf(what, sizeof(what), "granted %s %s %s (new policy, line %d)", + argv[0], argv[1], argv[2], replaced); + 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) @@ -396,7 +451,7 @@ int cmd_grant(const char *path, edit_opts o, int argc, char **argv) int cmd_revoke(const char *path, edit_opts o, int argc, char **argv) { lines_t lines; - lines_t kept; + size_t w; int i; int gone = 0; @@ -404,33 +459,38 @@ int cmd_revoke(const char *path, edit_opts o, int argc, char **argv) fprintf(stderr, "%s: revoke wants \n", progname); return 64; } - if (refuse_broken(path) != 0) - return 3; - if (read_lines(path, &lines) != 0) + if (authz_reject_options(argc, argv) != 0) + return 64; + if (refuse_broken(path, NULL) != 0) return 3; + if (read_lines(path, &lines) != 0) { + fprintf(stderr, "%s: no rule for %s on %s to remove\n", progname, argv[0], + argv[1]); + return 1; + } - lines_init(&kept); - for (i = 0; i < (int)lines.n; i++) { + for (i = 0, w = 0; i < (int)lines.n; i++) { if (is_rule_for(lines.v[i], argv[0], argv[1])) { free(lines.v[i]); gone++; continue; } - lines_push(&kept, lines.v[i]); + lines.v[w++] = lines.v[i]; } + lines.n = w; + if (gone == 0) { fprintf(stderr, "%s: no rule for %s on %s to remove\n", progname, argv[0], argv[1]); lines_free(&lines); - lines_free(&kept); return 1; } { - char what[128]; + char what[512]; snprintf(what, sizeof(what), "revoked %s on %s (%d rule(s) removed)", argv[0], argv[1], gone); - return finish(&kept, path, o, what); + return finish(&lines, path, o, what); } } @@ -439,42 +499,51 @@ int cmd_protect(const char *path, edit_opts o, int argc, char **argv) lines_t lines; const char *repo = NULL; char key[256]; + char *line; strbuf_t text; int i; int replaced = -1; - - if (argc < 1) { - fprintf(stderr, "%s: protect wants [--repo ] ...\n", - progname); - return 64; - } - if (strcmp(argv[0], "--repo") == 0) { - if (argc < 2) { + int created = 0; + for (i = 0; i < argc; i++) { /* --repo anywhere, like --out */ + if (strcmp(argv[i], "--repo") != 0) + continue; + if (i + 1 >= argc) { fprintf(stderr, "%s: --repo wants a repository name\n", progname); return 64; } - repo = argv[1]; - argv += 2; + repo = argv[i + 1]; + for (; i + 2 < argc; i++) + argv[i] = argv[i + 2]; argc -= 2; - if (argc < 1) { - fprintf(stderr, "%s: protect wants at least one ref pattern\n", progname); - return 64; - } + break; + } + if (argc < 1) { + fprintf(stderr, "%s: protect wants [--repo ] ...\n", + progname); + return 64; } if (repo != NULL && !authz_repo_pattern_ok(repo)) { fprintf(stderr, "%s: invalid repo pattern \"%s\"\n", progname, repo); return 64; } for (i = 0; i < argc; i++) { + if (argv[i][0] == '-' && argv[i][1] == '-') { + fprintf(stderr, "%s: unknown option \"%s\" (the only one here is " + "--repo, which comes first)\n", progname, argv[i]); + return 64; + } if (argv[i][0] == '!' || argv[i][0] == '-') { fprintf(stderr, "%s: ref patterns are additive, there is no deny: %s\n", progname, argv[i]); return 64; } } - if (refuse_broken(path) != 0) + if (refuse_broken(path, &created) != 0) return 3; - if (read_lines(path, &lines) != 0) + if (created) { + lines_init(&lines); + lines_push(&lines, xstrdup("# git-authz policy\n")); + } else if (read_lines(path, &lines) != 0) return 3; if (repo != NULL) @@ -492,25 +561,31 @@ int cmd_protect(const char *path, edit_opts o, int argc, char **argv) } sb_add(&text, "\n"); + line = xstrdup(text.s); /* the array owns its own copy; text is + * released by sb_free below */ for (i = 0; i < (int)lines.n; i++) { if (!is_set_for(lines.v[i], key)) continue; free(lines.v[i]); - lines.v[i] = text.s; + lines.v[i] = line; replaced = i + 1; break; } if (replaced < 0) { - lines_push(&lines, text.s); + lines_push(&lines, line); replaced = (int)lines.n; - } else { - free(text.s); } + sb_free(&text); + { - char what[160]; + char what[512]; - snprintf(what, sizeof(what), "protected refs for %s (%s, line %d)", - repo != NULL ? repo : "every repository", key, replaced); + if (created) + snprintf(what, sizeof(what), "protected refs for %s (%s, new policy)", + repo != NULL ? repo : "every repository", key); + else + snprintf(what, sizeof(what), "protected refs for %s (%s, line %d)", + repo != NULL ? repo : "every repository", key, replaced); return finish(&lines, path, o, what); } } diff --git a/src/main.c b/src/main.c index 1d7879b..368751b 100644 --- a/src/main.c +++ b/src/main.c @@ -92,7 +92,9 @@ static int one_option(int argc, char **argv, int *i, const char *name, const cha return 0; } -/* Called when the policy is not usable: the only safe answer is no. */ +/* The policy is not usable, so the only safe answer is no. Three, not one: + * the push was refused because this host is broken, not because that person is + * not allowed, and whoever is debugging wants the two to sound different. */ static int deny_policy(const char *path, int rc) { fprintf(stderr, "%s: deny: the policy at %s %s\n", progname, path, @@ -100,6 +102,95 @@ static int deny_policy(const char *path, int rc) return 3; } +/* Everything that answers out of a loaded policy. Split out of main so the + * policy is freed on the way out whichever way that is: a process this short + * lived could leave the heap to the kernel, but then a sanitizer run reports + * forty leaks and stops showing the one that matters. */ +static int dispatch(policy_t *p, int rc, const char *cmd, int argc, char **argv, + const char *path, hook_opts ho, validate_opts vo) +{ + if (strcmp(cmd, "validate") == 0) { + int j; + + for (j = 0; j < argc; j++) { + int got; + + got = one_option(argc, argv, &j, "--root", &vo.root); + if (got == 0) + got = one_option(argc, argv, &j, "--hook-dir", &vo.hook_dir); + if (got == 0) + got = one_option(argc, argv, &j, "--gitconfig", &vo.gitconfig); + if (got == 0) + got = one_option(argc, argv, &j, "--sshd-config", &vo.sshd_config); + if (got == 0) + got = one_option(argc, argv, &j, "--passwd", &vo.passwd); + if (got == 0) + got = one_option(argc, argv, &j, "--keys", &vo.keys); + if (got == 0) + got = one_option(argc, argv, &j, "--htpasswd", &vo.htpasswd); + if (got < 0) + return 64; + if (got == 0 && strcmp(argv[j], "--strict") == 0) { + vo.strict = 1; + continue; + } + /* The policy is the part that decides who may push. Everything + * else is how this host is wired, and a host without a git account + * yet - a container being built, a config directory being checked + * in - still wants its policy read. */ + if (got == 0 && strcmp(argv[j], "--no-host") == 0) { + vo.root = NULL; + vo.hook_dir = NULL; + vo.gitconfig = NULL; + vo.sshd_config = NULL; + vo.passwd = NULL; + vo.keys = NULL; + vo.htpasswd = NULL; + continue; + } + if (got == 0) { + fprintf(stderr, "%s: validate: unknown argument %s\n", progname, + argv[j]); + return 64; + } + } + /* A policy that does not parse is itself the finding, so it is passed + * down and reported rather than turning this into a bare exit 3. */ + return cmd_validate(p, vo); + } + + if (strcmp(cmd, "authorize") == 0) { + if (rc != 0) + return deny_policy(path, rc); + return cmd_authorize(p, ho, argc, argv); + } + + if (strcmp(cmd, "check") == 0) { + decision_t d; + int level; + + if (argc < 3 || argc > 4) { + fprintf(stderr, "%s: check wants [ref]\n", progname); + return 64; + } + level = authz_level_of(argv[2]); + if (level < 0) { + fprintf(stderr, "%s: unknown access level \"%s\" (write force admin)\n", + progname, argv[2]); + return 64; + } + if (rc != 0) + return deny_policy(path, rc); + authz_check(p, argv[0], argv[1], (level_t)level, argc > 3 ? argv[3] : NULL, &d); + printf("%s: %s\n", d.allowed ? "allow" : "deny", d.why); + return d.allowed ? 0 : 1; + } + + fprintf(stderr, "%s: unknown command \"%s\"\n", progname, cmd); + usage(stderr); + return 64; +} + int main(int argc, char **argv) { const char *path; @@ -111,6 +202,7 @@ int main(int argc, char **argv) edit_opts eo; int i; int rc; + int status; base = strrchr(argv[0], '/'); progname = base != NULL ? base + 1 : argv[0]; @@ -131,14 +223,20 @@ int main(int argc, char **argv) vo.htpasswd = DEFAULT_HTPASSWD; memset(&eo, 0, sizeof(eo)); - /* Reached as the hook itself: git's arguments to a hook are not ours, so - * they are ignored, and the policy comes from the environment. */ + /* Reached through the hook symlink. Git's own arguments to a hook are not + * this program's, so they are dropped, and the policy comes from the + * environment - a hook is not in a position to be told where the policy is + * by a person who is not there. */ if (strcmp(progname, "pre-receive") == 0) { policy_init(&p, path); rc = policy_load(&p); - if (rc != 0) + if (rc != 0) { + policy_free(&p); return deny_policy(path, rc); - return cmd_authorize(&p, ho, 0, NULL); + } + status = cmd_authorize(&p, ho, 0, NULL); + policy_free(&p); + return status; } i = 1; @@ -173,14 +271,16 @@ int main(int argc, char **argv) return 0; } + /* The three commands that change the file. They read it themselves, and + * they must not disagree with the checker about what a rule means, which is + * why they are in the same binary. */ if (strcmp(cmd, "grant") == 0 || strcmp(cmd, "revoke") == 0 || strcmp(cmd, "protect") == 0) { int consumed = 0; if (authz_parse_out(argc, argv, &eo, &consumed) != 0) return 64; - argc -= consumed; - argv += consumed; + argc -= consumed; /* what is left was compacted to the front */ if (strcmp(cmd, "grant") == 0) return cmd_grant(path, eo, argc, argv); if (strcmp(cmd, "revoke") == 0) @@ -188,84 +288,9 @@ int main(int argc, char **argv) return cmd_protect(path, eo, argc, argv); } - /* Everything else answers from the policy, so read it once, here. */ policy_init(&p, path); rc = policy_load(&p); - - if (strcmp(cmd, "validate") == 0) { - int j; - - for (j = 0; j < argc; j++) { - int got; - - got = one_option(argc, argv, &j, "--root", &vo.root); - if (got == 0) - got = one_option(argc, argv, &j, "--hook-dir", &vo.hook_dir); - if (got == 0) - got = one_option(argc, argv, &j, "--gitconfig", &vo.gitconfig); - if (got == 0) - got = one_option(argc, argv, &j, "--sshd-config", &vo.sshd_config); - if (got == 0) - got = one_option(argc, argv, &j, "--passwd", &vo.passwd); - if (got == 0) - got = one_option(argc, argv, &j, "--keys", &vo.keys); - if (got == 0) - got = one_option(argc, argv, &j, "--htpasswd", &vo.htpasswd); - if (got < 0) - return 64; - if (got == 0 && strcmp(argv[j], "--strict") == 0) { - vo.strict = 1; - continue; - } - if (got == 0 && strcmp(argv[j], "--no-host") == 0) { - vo.root = NULL; - vo.hook_dir = NULL; - vo.sshd_config = NULL; - vo.passwd = NULL; - vo.keys = NULL; - vo.htpasswd = NULL; - vo.gitconfig = NULL; - continue; - } - if (got == 0) { - fprintf(stderr, "%s: validate: unknown argument %s\n", progname, - argv[j]); - return 64; - } - } - /* cmd_validate reports the parse errors itself: a policy that does not - * parse is the finding, not a reason to say nothing. */ - return cmd_validate(&p, vo); - } - - if (strcmp(cmd, "authorize") == 0) { - if (rc != 0) - return deny_policy(path, rc); - return cmd_authorize(&p, ho, argc, argv); - } - - if (strcmp(cmd, "check") == 0) { - decision_t d; - int level; - - if (rc != 0) - return deny_policy(path, rc); - if (argc < 3 || argc > 4) { - fprintf(stderr, "%s: check wants [ref]\n", progname); - return 64; - } - level = authz_level_of(argv[2]); - if (level < 0) { - fprintf(stderr, "%s: unknown access level \"%s\" (write force admin)\n", - progname, argv[2]); - return 64; - } - authz_check(&p, argv[0], argv[1], (level_t)level, argc > 3 ? argv[3] : NULL, &d); - printf("%s: %s\n", d.allowed ? "allow" : "deny", d.why); - return d.allowed ? 0 : 1; - } - - fprintf(stderr, "%s: unknown command \"%s\"\n", progname, cmd); - usage(stderr); - return 64; + status = dispatch(&p, rc, cmd, argc, argv, path, ho, vo); + policy_free(&p); + return status; } -- cgit v1.2.3