diff options
| author | dev <dev@example> | 2026-11-13 20:34:17 +0000 |
|---|---|---|
| committer | dev <dev@example> | 2026-11-13 20:34:17 +0000 |
| commit | e755a181b0e4c952c35d80e2aa1acd584d2e4595 (patch) | |
| tree | 2e176a3946acc7999149092dd4fdfcbe2cb7c987 /src/edit.c | |
| parent | 1fd64267f678b8c6d17b2bcae702d577df1fc9e4 (diff) | |
| download | access-control-e755a181b0e4c952c35d80e2aa1acd584d2e4595.tar.gz access-control-e755a181b0e4c952c35d80e2aa1acd584d2e4595.zip | |
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.
Diffstat (limited to 'src/edit.c')
| -rw-r--r-- | src/edit.c | 241 |
1 files changed, 158 insertions, 83 deletions
@@ -27,6 +27,7 @@ #include <sys/stat.h> #include <unistd.h> +#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("# <user> <repo> <write|force|admin> " + "[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 <user> <repo>\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 <name>] <ref pattern> ...\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 <name>] <ref pattern> ...\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); } } |
