summaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
authordev <dev@example>2026-11-13 20:34:17 +0000
committerdev <dev@example>2026-11-13 20:34:17 +0000
commite755a181b0e4c952c35d80e2aa1acd584d2e4595 (patch)
tree2e176a3946acc7999149092dd4fdfcbe2cb7c987 /src/main.c
parent1fd64267f678b8c6d17b2bcae702d577df1fc9e4 (diff)
downloadaccess-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/main.c')
-rw-r--r--src/main.c195
1 files changed, 110 insertions, 85 deletions
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 <user> <repo> <level> [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 <user> <repo> <level> [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;
}