2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2019, Ori Bernstein <ori@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * All code runs under the same UID but sensitive code paths are
20 * run in a separate process with tighter pledge(2) promises.
21 * Data is communicated between processes via imsg_flush(3) and imsg_read(3).
22 * This behaviour is transparent to users of the library.
24 * We generally transmit data in imsg buffers, split across several messages
25 * if necessary. File descriptors are used in cases where this is impractical,
26 * such as when accessing pack files or when transferring large blobs.
28 * We exec(2) after a fork(2). Parts of our library functionality are
29 * accessible via separate executables in a libexec directory.
32 #define GOT_IMSG_FD_CHILD (STDERR_FILENO + 1)
34 #ifndef GOT_LIBEXECDIR
35 #define GOT_LIBEXECDIR /usr/libexec
38 /* Names of helper programs in libexec directory */
39 #define GOT_PROG_READ_OBJECT got-read-object
40 #define GOT_PROG_READ_TREE got-read-tree
41 #define GOT_PROG_READ_COMMIT got-read-commit
42 #define GOT_PROG_READ_BLOB got-read-blob
43 #define GOT_PROG_READ_TAG got-read-tag
44 #define GOT_PROG_READ_PACK got-read-pack
45 #define GOT_PROG_READ_GITCONFIG got-read-gitconfig
46 #define GOT_PROG_FETCH_PACK got-fetch-pack
47 #define GOT_PROG_INDEX_PACK got-index-pack
48 #define GOT_PROG_SEND_PACK got-send-pack
50 #define GOT_STRINGIFY(x) #x
51 #define GOT_STRINGVAL(x) GOT_STRINGIFY(x)
53 /* Paths to helper programs in libexec directory */
54 #define GOT_PATH_PROG_READ_OBJECT \
55 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_OBJECT)
56 #define GOT_PATH_PROG_READ_TREE \
57 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_TREE)
58 #define GOT_PATH_PROG_READ_COMMIT \
59 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_COMMIT)
60 #define GOT_PATH_PROG_READ_BLOB \
61 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_BLOB)
62 #define GOT_PATH_PROG_READ_TAG \
63 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_TAG)
64 #define GOT_PATH_PROG_READ_PACK \
65 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_PACK)
66 #define GOT_PATH_PROG_READ_GITCONFIG \
67 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_GITCONFIG)
68 #define GOT_PATH_PROG_FETCH_PACK \
69 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_FETCH_PACK)
70 #define GOT_PATH_PROG_SEND_PACK \
71 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_SEND_PACK)
72 #define GOT_PATH_PROG_INDEX_PACK \
73 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_INDEX_PACK)
75 struct got_privsep_child {
82 /* An error occured while processing a request. */
85 /* Stop the child process. */
89 * Messages concerned with read access to objects in a repository.
90 * Object and pack files are opened by the main process, where
91 * data may be read as a byte string but without any interpretation.
92 * Decompression and parsing of object and pack files occurs in a
93 * separate process which runs under pledge("stdio recvfd").
94 * This sandboxes our own repository parsing code, as well as zlib.
96 GOT_IMSG_OBJECT_REQUEST,
98 GOT_IMSG_COMMIT_REQUEST,
100 GOT_IMSG_COMMIT_LOGMSG,
101 GOT_IMSG_TREE_REQUEST,
104 GOT_IMSG_BLOB_REQUEST,
107 GOT_IMSG_TAG_REQUEST,
111 /* Messages related to networking. */
112 GOT_IMSG_FETCH_REQUEST,
113 GOT_IMSG_FETCH_HAVE_REF,
114 GOT_IMSG_FETCH_WANTED_BRANCH,
115 GOT_IMSG_FETCH_OUTFD,
116 GOT_IMSG_FETCH_SYMREFS,
118 GOT_IMSG_FETCH_SERVER_PROGRESS,
119 GOT_IMSG_FETCH_DOWNLOAD_PROGRESS,
121 GOT_IMSG_IDXPACK_REQUEST,
122 GOT_IMSG_IDXPACK_OUTFD,
123 GOT_IMSG_IDXPACK_PROGRESS,
124 GOT_IMSG_IDXPACK_DONE,
126 /* Messages related to pack files. */
129 GOT_IMSG_PACKED_OBJECT_REQUEST,
130 GOT_IMSG_COMMIT_TRAVERSAL_REQUEST,
131 GOT_IMSG_TRAVERSED_COMMITS,
132 GOT_IMSG_COMMIT_TRAVERSAL_DONE,
134 /* Message sending file descriptor to a temporary file. */
137 /* Messages related to gitconfig files. */
138 GOT_IMSG_GITCONFIG_PARSE_REQUEST,
139 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST,
140 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST,
141 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST,
142 GOT_IMSG_GITCONFIG_REMOTES_REQUEST,
143 GOT_IMSG_GITCONFIG_INT_VAL,
144 GOT_IMSG_GITCONFIG_STR_VAL,
145 GOT_IMSG_GITCONFIG_REMOTES,
146 GOT_IMSG_GITCONFIG_REMOTE,
147 GOT_IMSG_GITCONFIG_OWNER_REQUEST,
148 GOT_IMSG_GITCONFIG_OWNER,
151 /* Structure for GOT_IMSG_ERROR. */
152 struct got_imsg_error {
153 int code; /* an error code from got_error.h */
154 int errno_code; /* in case code equals GOT_ERR_ERRNO */
155 } __attribute__((__packed__));
158 * Structure for GOT_IMSG_TREE_REQUEST and GOT_IMSG_OBJECT data.
160 struct got_imsg_object {
161 uint8_t id[SHA1_DIGEST_LENGTH];
163 /* These fields are the same as in struct got_object. */
170 } __attribute__((__packed__));
172 /* Structure for GOT_IMSG_COMMIT data. */
173 struct got_imsg_commit_object {
174 uint8_t tree_id[SHA1_DIGEST_LENGTH];
177 time_t author_gmtoff;
178 size_t committer_len;
179 time_t committer_time;
180 time_t committer_gmtoff;
185 * Followed by author_len + committer_len data bytes
188 /* Followed by 'nparents' SHA1_DIGEST_LENGTH length strings */
191 * Followed by 'logmsg_len' bytes of commit log message data in
192 * one or more GOT_IMSG_COMMIT_LOGMSG messages.
194 } __attribute__((__packed__));
197 /* Structure for GOT_IMSG_TREE_ENTRY. */
198 struct got_imsg_tree_entry {
199 char id[SHA1_DIGEST_LENGTH];
201 /* Followed by entry's name in remaining data of imsg buffer. */
202 } __attribute__((__packed__));
204 /* Structure for GOT_IMSG_TREE_OBJECT_REPLY data. */
205 struct got_imsg_tree_object {
206 int nentries; /* This many TREE_ENTRY messages follow. */
209 /* Structure for GOT_IMSG_BLOB. */
210 struct got_imsg_blob {
215 * If size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX, blob data follows
216 * in the imsg buffer. Otherwise, blob data has been written to a
217 * file descriptor passed via the GOT_IMSG_BLOB_OUTFD imsg.
219 #define GOT_PRIVSEP_INLINE_BLOB_DATA_MAX \
220 (MAX_IMSGSIZE - IMSG_HEADER_SIZE - sizeof(struct got_imsg_blob))
224 /* Structure for GOT_IMSG_TAG data. */
225 struct got_imsg_tag_object {
226 uint8_t id[SHA1_DIGEST_LENGTH];
231 time_t tagger_gmtoff;
235 * Followed by tag_len + tagger_len data bytes
239 * Followed by 'tagmsg_len' bytes of tag message data in
240 * one or more GOT_IMSG_TAG_TAGMSG messages.
242 } __attribute__((__packed__));
244 /* Structure for GOT_IMSG_FETCH_HAVE_REF data. */
245 struct got_imsg_fetch_have_ref {
246 uint8_t id[SHA1_DIGEST_LENGTH];
248 /* Followed by name_len data bytes. */
249 } __attribute__((__packed__));
251 /* Structure for GOT_IMSG_FETCH_WANTED_BRANCH data. */
252 struct got_imsg_fetch_wanted_branch {
254 /* Followed by name_len data bytes. */
255 } __attribute__((__packed__));
257 /* Structure for GOT_IMSG_FETCH_REQUEST data. */
258 struct got_imsg_fetch_request {
259 int fetch_all_branches;
263 size_t n_wanted_branches;
264 /* Followed by n_have_refs GOT_IMSG_FETCH_HAVE_REF messages. */
265 /* Followed by n_wanted_branches times GOT_IMSG_FETCH_WANTED_BRANCH. */
266 } __attribute__((__packed__));
268 /* Structures for GOT_IMSG_FETCH_SYMREFS data. */
269 struct got_imsg_fetch_symref {
274 * Followed by name_len + target_len data bytes.
276 } __attribute__((__packed__));
278 struct got_imsg_fetch_symrefs {
281 /* Followed by nsymrefs times of got_imsg_fetch_symref data. */
282 } __attribute__((__packed__));
284 /* Structure for GOT_IMSG_FETCH_REF data. */
285 struct got_imsg_fetch_ref {
286 /* Describes a reference which will be fetched. */
287 uint8_t refid[SHA1_DIGEST_LENGTH];
288 /* Followed by reference name in remaining data of imsg buffer. */
291 /* Structure for GOT_IMSG_FETCH_DOWNLOAD_PROGRESS data. */
292 struct got_imsg_fetch_download_progress {
293 /* Number of packfile data bytes downloaded so far. */
294 off_t packfile_bytes;
297 /* Structure for GOT_IMSG_IDXPACK_REQUEST data. */
298 struct got_imsg_index_pack_request {
299 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
300 } __attribute__((__packed__));
302 /* Structure for GOT_IMSG_IDXPACK_PROGRESS data. */
303 struct got_imsg_index_pack_progress {
304 /* Total number of objects in pack file. */
307 /* Number of objects indexed so far. */
310 /* Number of non-deltified objects in pack file. */
313 /* Number of deltified objects resolved so far. */
317 /* Structure for GOT_IMSG_PACKIDX. */
318 struct got_imsg_packidx {
320 /* Additionally, a file desciptor is passed via imsg. */
323 /* Structure for GOT_IMSG_PACK. */
324 struct got_imsg_pack {
325 char path_packfile[PATH_MAX];
327 /* Additionally, a file desciptor is passed via imsg. */
328 } __attribute__((__packed__));
331 * Structure for GOT_IMSG_PACKED_OBJECT_REQUEST data.
333 struct got_imsg_packed_object {
334 uint8_t id[SHA1_DIGEST_LENGTH];
336 } __attribute__((__packed__));
338 /* Structure for GOT_IMSG_COMMIT_TRAVERSAL_REQUEST */
339 struct got_imsg_commit_traversal_request {
340 uint8_t id[SHA1_DIGEST_LENGTH];
343 /* Followed by path_len bytes of path data */
344 } __attribute__((__packed__));
346 /* Structure for GOT_IMSG_TRAVERSED_COMMITS */
347 struct got_imsg_traversed_commits {
349 /* Followed by ncommit IDs of SHA1_DIGEST_LENGTH each */
350 } __attribute__((__packed__));
353 * Structure for GOT_IMSG_GITCONFIG_REMOTE data.
355 struct got_imsg_remote {
358 int mirror_references;
360 /* Followed by name_len + url_len data bytes. */
361 } __attribute__((__packed__));
364 * Structure for GOT_IMSG_GITCONFIG_REMOTES data.
366 struct got_imsg_remotes {
367 int nremotes; /* This many GOT_IMSG_GITCONFIG_REMOTE messages follow. */
370 struct got_remote_repo;
373 struct got_pathlist_head;
375 const struct got_error *got_send_ack(pid_t);
376 const struct got_error *got_privsep_wait_for_child(pid_t);
377 const struct got_error *got_privsep_send_stop(int);
378 const struct got_error *got_privsep_recv_imsg(struct imsg *, struct imsgbuf *,
380 void got_privsep_send_error(struct imsgbuf *, const struct got_error *);
381 const struct got_error *got_privsep_send_ack(struct imsgbuf *);
382 const struct got_error *got_privsep_wait_ack(struct imsgbuf *);
383 const struct got_error *got_privsep_send_obj_req(struct imsgbuf *, int);
384 const struct got_error *got_privsep_send_commit_req(struct imsgbuf *, int,
385 struct got_object_id *, int);
386 const struct got_error *got_privsep_send_tree_req(struct imsgbuf *, int,
387 struct got_object_id *, int);
388 const struct got_error *got_privsep_send_tag_req(struct imsgbuf *, int,
389 struct got_object_id *, int);
390 const struct got_error *got_privsep_send_blob_req(struct imsgbuf *, int,
391 struct got_object_id *, int);
392 const struct got_error *got_privsep_send_blob_outfd(struct imsgbuf *, int);
393 const struct got_error *got_privsep_send_tmpfd(struct imsgbuf *, int);
394 const struct got_error *got_privsep_send_obj(struct imsgbuf *,
395 struct got_object *);
396 const struct got_error *got_privsep_send_index_pack_req(struct imsgbuf *,
398 const struct got_error *got_privsep_send_index_pack_outfd(struct imsgbuf *,
400 const struct got_error *got_privsep_send_index_pack_progress(struct imsgbuf *,
402 const struct got_error *got_privsep_send_index_pack_done(struct imsgbuf *);
403 const struct got_error *got_privsep_recv_index_progress(int *, int *, int *,
404 int *, int *, struct imsgbuf *ibuf);
405 const struct got_error *got_privsep_send_fetch_req(struct imsgbuf *, int,
406 struct got_pathlist_head *, int, struct got_pathlist_head *, int, int);
407 const struct got_error *got_privsep_send_fetch_outfd(struct imsgbuf *, int);
408 const struct got_error *got_privsep_send_fetch_symrefs(struct imsgbuf *,
409 struct got_pathlist_head *);
410 const struct got_error *got_privsep_send_fetch_ref(struct imsgbuf *,
411 struct got_object_id *, const char *);
412 const struct got_error *got_privsep_send_fetch_server_progress(struct imsgbuf *,
413 const char *, size_t);
414 const struct got_error *got_privsep_send_fetch_download_progress(struct imsgbuf *,
416 const struct got_error *got_privsep_recv_fetch_progress(int *,
417 struct got_object_id **, char **, struct got_pathlist_head *,
418 char **, off_t *, struct imsgbuf *);
419 const struct got_error *got_privsep_send_fetch_done(struct imsgbuf *,
420 struct got_object_id);
421 const struct got_error *got_privsep_get_imsg_obj(struct got_object **,
422 struct imsg *, struct imsgbuf *);
423 const struct got_error *got_privsep_recv_obj(struct got_object **,
425 const struct got_error *got_privsep_send_commit(struct imsgbuf *,
426 struct got_commit_object *);
427 const struct got_error *got_privsep_recv_commit(struct got_commit_object **,
429 const struct got_error *got_privsep_recv_tree(struct got_tree_object **,
431 const struct got_error *got_privsep_send_tree(struct imsgbuf *,
432 struct got_pathlist_head *, int);
433 const struct got_error *got_privsep_send_blob(struct imsgbuf *, size_t, size_t,
435 const struct got_error *got_privsep_recv_blob(uint8_t **, size_t *, size_t *,
437 const struct got_error *got_privsep_send_tag(struct imsgbuf *,
438 struct got_tag_object *);
439 const struct got_error *got_privsep_recv_tag(struct got_tag_object **,
441 const struct got_error *got_privsep_init_pack_child(struct imsgbuf *,
442 struct got_pack *, struct got_packidx *);
443 const struct got_error *got_privsep_send_packed_obj_req(struct imsgbuf *, int,
444 struct got_object_id *);
445 const struct got_error *got_privsep_send_pack_child_ready(struct imsgbuf *);
447 const struct got_error *got_privsep_send_gitconfig_parse_req(struct imsgbuf *,
449 const struct got_error *
450 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *);
451 const struct got_error *got_privsep_send_gitconfig_author_name_req(
453 const struct got_error *got_privsep_send_gitconfig_author_email_req(
455 const struct got_error *got_privsep_send_gitconfig_remotes_req(
457 const struct got_error *got_privsep_send_gitconfig_owner_req(struct imsgbuf *);
458 const struct got_error *got_privsep_send_gitconfig_str(struct imsgbuf *,
460 const struct got_error *got_privsep_recv_gitconfig_str(char **,
462 const struct got_error *got_privsep_send_gitconfig_int(struct imsgbuf *, int);
463 const struct got_error *got_privsep_recv_gitconfig_int(int *, struct imsgbuf *);
464 const struct got_error *got_privsep_send_gitconfig_remotes(struct imsgbuf *,
465 struct got_remote_repo *, int);
466 const struct got_error *got_privsep_recv_gitconfig_remotes(
467 struct got_remote_repo **, int *, struct imsgbuf *);
469 const struct got_error *got_privsep_send_commit_traversal_request(
470 struct imsgbuf *, struct got_object_id *, int, const char *);
471 const struct got_error *got_privsep_recv_traversed_commits(
472 struct got_commit_object **, struct got_object_id **,
473 struct got_object_id_queue *, struct imsgbuf *);
474 const struct got_error *got_privsep_send_traversed_commits(
475 struct got_object_id *, size_t, struct imsgbuf *);
476 const struct got_error *got_privsep_send_commit_traversal_done(
479 void got_privsep_exec_child(int[2], const char *, const char *);