Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2019, Ori Bernstein <ori@openbsd.org>
4 *
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.
8 *
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.
16 */
18 /*
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.
23 *
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.
27 *
28 * We exec(2) after a fork(2). Parts of our library functionality are
29 * accessible via separate executables in a libexec directory.
30 */
32 #define GOT_IMSG_FD_CHILD (STDERR_FILENO + 1)
34 #ifndef GOT_LIBEXECDIR
35 #define GOT_LIBEXECDIR /usr/libexec
36 #endif
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 {
76 int imsg_fd;
77 pid_t pid;
78 struct imsgbuf *ibuf;
79 };
81 enum got_imsg_type {
82 /* An error occured while processing a request. */
83 GOT_IMSG_ERROR,
85 /* Stop the child process. */
86 GOT_IMSG_STOP,
88 /*
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.
95 */
96 GOT_IMSG_OBJECT_REQUEST,
97 GOT_IMSG_OBJECT,
98 GOT_IMSG_COMMIT_REQUEST,
99 GOT_IMSG_COMMIT,
100 GOT_IMSG_COMMIT_LOGMSG,
101 GOT_IMSG_TREE_REQUEST,
102 GOT_IMSG_TREE,
103 GOT_IMSG_TREE_ENTRY,
104 GOT_IMSG_BLOB_REQUEST,
105 GOT_IMSG_BLOB_OUTFD,
106 GOT_IMSG_BLOB,
107 GOT_IMSG_TAG_REQUEST,
108 GOT_IMSG_TAG,
109 GOT_IMSG_TAG_TAGMSG,
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_WANTED_REF,
116 GOT_IMSG_FETCH_OUTFD,
117 GOT_IMSG_FETCH_SYMREFS,
118 GOT_IMSG_FETCH_REF,
119 GOT_IMSG_FETCH_SERVER_PROGRESS,
120 GOT_IMSG_FETCH_DOWNLOAD_PROGRESS,
121 GOT_IMSG_FETCH_DONE,
122 GOT_IMSG_IDXPACK_REQUEST,
123 GOT_IMSG_IDXPACK_OUTFD,
124 GOT_IMSG_IDXPACK_PROGRESS,
125 GOT_IMSG_IDXPACK_DONE,
127 /* Messages related to pack files. */
128 GOT_IMSG_PACKIDX,
129 GOT_IMSG_PACK,
130 GOT_IMSG_PACKED_OBJECT_REQUEST,
131 GOT_IMSG_COMMIT_TRAVERSAL_REQUEST,
132 GOT_IMSG_TRAVERSED_COMMITS,
133 GOT_IMSG_COMMIT_TRAVERSAL_DONE,
135 /* Message sending file descriptor to a temporary file. */
136 GOT_IMSG_TMPFD,
138 /* Messages related to gitconfig files. */
139 GOT_IMSG_GITCONFIG_PARSE_REQUEST,
140 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST,
141 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST,
142 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST,
143 GOT_IMSG_GITCONFIG_REMOTES_REQUEST,
144 GOT_IMSG_GITCONFIG_INT_VAL,
145 GOT_IMSG_GITCONFIG_STR_VAL,
146 GOT_IMSG_GITCONFIG_REMOTES,
147 GOT_IMSG_GITCONFIG_REMOTE,
148 GOT_IMSG_GITCONFIG_OWNER_REQUEST,
149 GOT_IMSG_GITCONFIG_OWNER,
150 };
152 /* Structure for GOT_IMSG_ERROR. */
153 struct got_imsg_error {
154 int code; /* an error code from got_error.h */
155 int errno_code; /* in case code equals GOT_ERR_ERRNO */
156 } __attribute__((__packed__));
158 /*
159 * Structure for GOT_IMSG_TREE_REQUEST and GOT_IMSG_OBJECT data.
160 */
161 struct got_imsg_object {
162 uint8_t id[SHA1_DIGEST_LENGTH];
164 /* These fields are the same as in struct got_object. */
165 int type;
166 int flags;
167 size_t hdrlen;
168 size_t size;
169 off_t pack_offset;
170 int pack_idx;
171 } __attribute__((__packed__));
173 /* Structure for GOT_IMSG_COMMIT data. */
174 struct got_imsg_commit_object {
175 uint8_t tree_id[SHA1_DIGEST_LENGTH];
176 size_t author_len;
177 time_t author_time;
178 time_t author_gmtoff;
179 size_t committer_len;
180 time_t committer_time;
181 time_t committer_gmtoff;
182 size_t logmsg_len;
183 int nparents;
185 /*
186 * Followed by author_len + committer_len data bytes
187 */
189 /* Followed by 'nparents' SHA1_DIGEST_LENGTH length strings */
191 /*
192 * Followed by 'logmsg_len' bytes of commit log message data in
193 * one or more GOT_IMSG_COMMIT_LOGMSG messages.
194 */
195 } __attribute__((__packed__));
198 /* Structure for GOT_IMSG_TREE_ENTRY. */
199 struct got_imsg_tree_entry {
200 char id[SHA1_DIGEST_LENGTH];
201 mode_t mode;
202 /* Followed by entry's name in remaining data of imsg buffer. */
203 } __attribute__((__packed__));
205 /* Structure for GOT_IMSG_TREE_OBJECT_REPLY data. */
206 struct got_imsg_tree_object {
207 int nentries; /* This many TREE_ENTRY messages follow. */
208 };
210 /* Structure for GOT_IMSG_BLOB. */
211 struct got_imsg_blob {
212 size_t size;
213 size_t hdrlen;
215 /*
216 * If size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX, blob data follows
217 * in the imsg buffer. Otherwise, blob data has been written to a
218 * file descriptor passed via the GOT_IMSG_BLOB_OUTFD imsg.
219 */
220 #define GOT_PRIVSEP_INLINE_BLOB_DATA_MAX \
221 (MAX_IMSGSIZE - IMSG_HEADER_SIZE - sizeof(struct got_imsg_blob))
222 };
225 /* Structure for GOT_IMSG_TAG data. */
226 struct got_imsg_tag_object {
227 uint8_t id[SHA1_DIGEST_LENGTH];
228 int obj_type;
229 size_t tag_len;
230 size_t tagger_len;
231 time_t tagger_time;
232 time_t tagger_gmtoff;
233 size_t tagmsg_len;
235 /*
236 * Followed by tag_len + tagger_len data bytes
237 */
239 /*
240 * Followed by 'tagmsg_len' bytes of tag message data in
241 * one or more GOT_IMSG_TAG_TAGMSG messages.
242 */
243 } __attribute__((__packed__));
245 /* Structure for GOT_IMSG_FETCH_HAVE_REF data. */
246 struct got_imsg_fetch_have_ref {
247 uint8_t id[SHA1_DIGEST_LENGTH];
248 size_t name_len;
249 /* Followed by name_len data bytes. */
250 } __attribute__((__packed__));
252 /* Structure for GOT_IMSG_FETCH_WANTED_BRANCH data. */
253 struct got_imsg_fetch_wanted_branch {
254 size_t name_len;
255 /* Followed by name_len data bytes. */
256 } __attribute__((__packed__));
258 /* Structure for GOT_IMSG_FETCH_WANTED_REF data. */
259 struct got_imsg_fetch_wanted_ref {
260 size_t name_len;
261 /* Followed by name_len data bytes. */
262 } __attribute__((__packed__));
264 /* Structure for GOT_IMSG_FETCH_REQUEST data. */
265 struct got_imsg_fetch_request {
266 int fetch_all_branches;
267 int list_refs_only;
268 int verbosity;
269 size_t n_have_refs;
270 size_t n_wanted_branches;
271 size_t n_wanted_refs;
272 /* Followed by n_have_refs GOT_IMSG_FETCH_HAVE_REF messages. */
273 /* Followed by n_wanted_branches times GOT_IMSG_FETCH_WANTED_BRANCH. */
274 /* Followed by n_wanted_refs times GOT_IMSG_FETCH_WANTED_REF. */
275 } __attribute__((__packed__));
277 /* Structures for GOT_IMSG_FETCH_SYMREFS data. */
278 struct got_imsg_fetch_symref {
279 size_t name_len;
280 size_t target_len;
282 /*
283 * Followed by name_len + target_len data bytes.
284 */
285 } __attribute__((__packed__));
287 struct got_imsg_fetch_symrefs {
288 size_t nsymrefs;
290 /* Followed by nsymrefs times of got_imsg_fetch_symref data. */
291 } __attribute__((__packed__));
293 /* Structure for GOT_IMSG_FETCH_REF data. */
294 struct got_imsg_fetch_ref {
295 /* Describes a reference which will be fetched. */
296 uint8_t refid[SHA1_DIGEST_LENGTH];
297 /* Followed by reference name in remaining data of imsg buffer. */
298 };
300 /* Structure for GOT_IMSG_FETCH_DOWNLOAD_PROGRESS data. */
301 struct got_imsg_fetch_download_progress {
302 /* Number of packfile data bytes downloaded so far. */
303 off_t packfile_bytes;
304 };
306 /* Structure for GOT_IMSG_IDXPACK_REQUEST data. */
307 struct got_imsg_index_pack_request {
308 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
309 } __attribute__((__packed__));
311 /* Structure for GOT_IMSG_IDXPACK_PROGRESS data. */
312 struct got_imsg_index_pack_progress {
313 /* Total number of objects in pack file. */
314 int nobj_total;
316 /* Number of objects indexed so far. */
317 int nobj_indexed;
319 /* Number of non-deltified objects in pack file. */
320 int nobj_loose;
322 /* Number of deltified objects resolved so far. */
323 int nobj_resolved;
324 };
326 /* Structure for GOT_IMSG_PACKIDX. */
327 struct got_imsg_packidx {
328 size_t len;
329 /* Additionally, a file desciptor is passed via imsg. */
330 };
332 /* Structure for GOT_IMSG_PACK. */
333 struct got_imsg_pack {
334 char path_packfile[PATH_MAX];
335 size_t filesize;
336 /* Additionally, a file desciptor is passed via imsg. */
337 } __attribute__((__packed__));
339 /*
340 * Structure for GOT_IMSG_PACKED_OBJECT_REQUEST data.
341 */
342 struct got_imsg_packed_object {
343 uint8_t id[SHA1_DIGEST_LENGTH];
344 int idx;
345 } __attribute__((__packed__));
347 /* Structure for GOT_IMSG_COMMIT_TRAVERSAL_REQUEST */
348 struct got_imsg_commit_traversal_request {
349 uint8_t id[SHA1_DIGEST_LENGTH];
350 int idx;
351 size_t path_len;
352 /* Followed by path_len bytes of path data */
353 } __attribute__((__packed__));
355 /* Structure for GOT_IMSG_TRAVERSED_COMMITS */
356 struct got_imsg_traversed_commits {
357 size_t ncommits;
358 /* Followed by ncommit IDs of SHA1_DIGEST_LENGTH each */
359 } __attribute__((__packed__));
361 /*
362 * Structure for GOT_IMSG_GITCONFIG_REMOTE data.
363 */
364 struct got_imsg_remote {
365 size_t name_len;
366 size_t url_len;
367 int mirror_references;
369 /* Followed by name_len + url_len data bytes. */
370 } __attribute__((__packed__));
372 /*
373 * Structure for GOT_IMSG_GITCONFIG_REMOTES data.
374 */
375 struct got_imsg_remotes {
376 int nremotes; /* This many GOT_IMSG_GITCONFIG_REMOTE messages follow. */
377 };
379 struct got_remote_repo;
380 struct got_pack;
381 struct got_packidx;
382 struct got_pathlist_head;
384 const struct got_error *got_send_ack(pid_t);
385 const struct got_error *got_privsep_wait_for_child(pid_t);
386 const struct got_error *got_privsep_flush_imsg(struct imsgbuf *);
387 const struct got_error *got_privsep_send_stop(int);
388 const struct got_error *got_privsep_recv_imsg(struct imsg *, struct imsgbuf *,
389 size_t);
390 void got_privsep_send_error(struct imsgbuf *, const struct got_error *);
391 const struct got_error *got_privsep_send_ack(struct imsgbuf *);
392 const struct got_error *got_privsep_wait_ack(struct imsgbuf *);
393 const struct got_error *got_privsep_send_obj_req(struct imsgbuf *, int);
394 const struct got_error *got_privsep_send_commit_req(struct imsgbuf *, int,
395 struct got_object_id *, int);
396 const struct got_error *got_privsep_send_tree_req(struct imsgbuf *, int,
397 struct got_object_id *, int);
398 const struct got_error *got_privsep_send_tag_req(struct imsgbuf *, int,
399 struct got_object_id *, int);
400 const struct got_error *got_privsep_send_blob_req(struct imsgbuf *, int,
401 struct got_object_id *, int);
402 const struct got_error *got_privsep_send_blob_outfd(struct imsgbuf *, int);
403 const struct got_error *got_privsep_send_tmpfd(struct imsgbuf *, int);
404 const struct got_error *got_privsep_send_obj(struct imsgbuf *,
405 struct got_object *);
406 const struct got_error *got_privsep_send_index_pack_req(struct imsgbuf *,
407 uint8_t *, int);
408 const struct got_error *got_privsep_send_index_pack_outfd(struct imsgbuf *,
409 int);
410 const struct got_error *got_privsep_recv_index_progress(int *, int *, int *,
411 int *, int *, struct imsgbuf *ibuf);
412 const struct got_error *got_privsep_send_fetch_req(struct imsgbuf *, int,
413 struct got_pathlist_head *, int, struct got_pathlist_head *,
414 struct got_pathlist_head *, int, int);
415 const struct got_error *got_privsep_send_fetch_outfd(struct imsgbuf *, int);
416 const struct got_error *got_privsep_recv_fetch_progress(int *,
417 struct got_object_id **, char **, struct got_pathlist_head *, char **,
418 off_t *, uint8_t *, struct imsgbuf *);
419 const struct got_error *got_privsep_get_imsg_obj(struct got_object **,
420 struct imsg *, struct imsgbuf *);
421 const struct got_error *got_privsep_recv_obj(struct got_object **,
422 struct imsgbuf *);
423 const struct got_error *got_privsep_send_commit(struct imsgbuf *,
424 struct got_commit_object *);
425 const struct got_error *got_privsep_recv_commit(struct got_commit_object **,
426 struct imsgbuf *);
427 const struct got_error *got_privsep_recv_tree(struct got_tree_object **,
428 struct imsgbuf *);
429 const struct got_error *got_privsep_send_tree(struct imsgbuf *,
430 struct got_pathlist_head *, int);
431 const struct got_error *got_privsep_send_blob(struct imsgbuf *, size_t, size_t,
432 const uint8_t *);
433 const struct got_error *got_privsep_recv_blob(uint8_t **, size_t *, size_t *,
434 struct imsgbuf *);
435 const struct got_error *got_privsep_send_tag(struct imsgbuf *,
436 struct got_tag_object *);
437 const struct got_error *got_privsep_recv_tag(struct got_tag_object **,
438 struct imsgbuf *);
439 const struct got_error *got_privsep_init_pack_child(struct imsgbuf *,
440 struct got_pack *, struct got_packidx *);
441 const struct got_error *got_privsep_send_packed_obj_req(struct imsgbuf *, int,
442 struct got_object_id *);
443 const struct got_error *got_privsep_send_pack_child_ready(struct imsgbuf *);
445 const struct got_error *got_privsep_send_gitconfig_parse_req(struct imsgbuf *,
446 int);
447 const struct got_error *
448 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *);
449 const struct got_error *got_privsep_send_gitconfig_author_name_req(
450 struct imsgbuf *);
451 const struct got_error *got_privsep_send_gitconfig_author_email_req(
452 struct imsgbuf *);
453 const struct got_error *got_privsep_send_gitconfig_remotes_req(
454 struct imsgbuf *);
455 const struct got_error *got_privsep_send_gitconfig_owner_req(struct imsgbuf *);
456 const struct got_error *got_privsep_recv_gitconfig_str(char **,
457 struct imsgbuf *);
458 const struct got_error *got_privsep_recv_gitconfig_int(int *, struct imsgbuf *);
459 const struct got_error *got_privsep_recv_gitconfig_remotes(
460 struct got_remote_repo **, int *, struct imsgbuf *);
462 const struct got_error *got_privsep_send_commit_traversal_request(
463 struct imsgbuf *, struct got_object_id *, int, const char *);
464 const struct got_error *got_privsep_recv_traversed_commits(
465 struct got_commit_object **, struct got_object_id **,
466 struct got_object_id_queue *, struct imsgbuf *);
468 void got_privsep_exec_child(int[2], const char *, const char *);