|
1 | 1 | #define _GNU_SOURCE |
2 | 2 | #include <arpa/inet.h> |
3 | 3 | #include <errno.h> |
| 4 | +#include <fcntl.h> |
4 | 5 | #include <signal.h> |
5 | 6 | #include <stdint.h> |
6 | 7 | #include <stdio.h> |
@@ -96,11 +97,73 @@ static int git_argv(int argc, char **argv) { |
96 | 97 | return 1; |
97 | 98 | } |
98 | 99 |
|
| 100 | +/* Git invokes this extra, operation-local hook with the ref locks held. The |
| 101 | + * transaction's old-value can be all zeroes even when deleting an existing |
| 102 | + * branch, so read the actual ref through the same trusted Git executable. |
| 103 | + * User hooks remain Git's responsibility; no repository metadata is written. */ |
| 104 | +static int reference_guard(int argc, char **argv) { |
| 105 | + if (argc != 5) return 1; |
| 106 | + if (strcmp(argv[4], "prepared") != 0) return 0; |
| 107 | + const char *git = getenv("BUSYSTACK_GIT_RUNTIME"), *ref = argv[2], *expected = argv[3]; |
| 108 | + size_t oid_length = strlen(expected); |
| 109 | + if (!git || git[0] != '/' || strncmp(ref, "refs/heads/", 11) != 0 || |
| 110 | + strlen(ref) > 262144 || (oid_length != 40 && oid_length != 64)) return 1; |
| 111 | + for (size_t i = 0; i < oid_length; ++i) if (hex_digit(expected[i]) < 0) return 1; |
| 112 | + alarm(10); |
| 113 | + char *line = malloc(262146); |
| 114 | + if (!line) return 1; |
| 115 | + size_t total = 0; |
| 116 | + int matched = 0; |
| 117 | + while (fgets(line, 262146, stdin)) { |
| 118 | + size_t length = strlen(line); |
| 119 | + total += length; |
| 120 | + if (!length || line[length - 1] != '\n' || total > 8388608) { free(line); return 1; } |
| 121 | + line[length - 1] = 0; |
| 122 | + char *first = strchr(line, ' '), *second = first ? strchr(first + 1, ' ') : NULL; |
| 123 | + if (!second) { free(line); return 1; } |
| 124 | + if (strcmp(second + 1, ref) == 0) matched = 1; |
| 125 | + } |
| 126 | + free(line); |
| 127 | + if (ferror(stdin)) return 1; |
| 128 | + if (!matched) return 0; |
| 129 | + int descriptors[2]; |
| 130 | + if (pipe2(descriptors, O_CLOEXEC) < 0) return 1; |
| 131 | + pid_t owner = getpid(), child = fork(); |
| 132 | + if (child < 0) { close(descriptors[0]); close(descriptors[1]); return 1; } |
| 133 | + if (child == 0) { |
| 134 | + close(descriptors[0]); |
| 135 | + if (prctl(PR_SET_PDEATHSIG, SIGKILL) < 0 || getppid() != owner || |
| 136 | + dup2(descriptors[1], STDOUT_FILENO) < 0) _exit(1); |
| 137 | + close(descriptors[1]); |
| 138 | + char *arguments[] = {(char *) git, "--no-pager", "-c", "core.fsmonitor=false", |
| 139 | + "rev-parse", "--verify", "--quiet", "--end-of-options", (char *) ref, NULL}; |
| 140 | + execv(git, arguments); _exit(127); |
| 141 | + } |
| 142 | + close(descriptors[1]); |
| 143 | + char actual[67]; size_t count = 0; |
| 144 | + while (count < sizeof(actual)) { |
| 145 | + ssize_t received = read(descriptors[0], actual + count, sizeof(actual) - count); |
| 146 | + if (received < 0 && errno == EINTR) continue; |
| 147 | + if (received <= 0) break; |
| 148 | + count += (size_t) received; |
| 149 | + } |
| 150 | + close(descriptors[0]); |
| 151 | + int status; |
| 152 | + while (waitpid(child, &status, 0) < 0) { if (errno != EINTR) return 1; } |
| 153 | + if (!WIFEXITED(status) || WEXITSTATUS(status) != 0 || count != oid_length + 1 || |
| 154 | + actual[oid_length] != '\n' || memcmp(actual, expected, oid_length) != 0) { |
| 155 | + fputs("BusyStack reviewed reference changed; refresh and review again.\n", stderr); |
| 156 | + return 1; |
| 157 | + } |
| 158 | + return 0; |
| 159 | +} |
| 160 | + |
99 | 161 | /* Installed prompt helper: no SDK or repository-owned helper scripts. |
100 | 162 | * Socket requests are authenticated and operation scoped. */ |
101 | 163 | int main(int argc, char **argv) { |
102 | 164 | if (argc >= 3 && strcmp(argv[1], "--supervise") == 0) return supervise(argc, argv); |
103 | 165 | if (argc >= 3 && strcmp(argv[1], "--git-argv") == 0) return git_argv(argc, argv); |
| 166 | + if (argc >= 2 && strcmp(argv[1], "--reference-guard") == 0) return reference_guard(argc, argv); |
104 | 167 | const char *socket_path = getenv("BUSYSTACK_GIT_PROMPT_SOCKET"); |
105 | 168 | const char *token = getenv("BUSYSTACK_GIT_PROMPT_TOKEN"); |
106 | 169 | const char *operation = getenv("BUSYSTACK_GIT_OPERATION"); |
|
0 commit comments