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_OUTFD,
114 GOT_IMSG_FETCH_SYMREFS,
115 GOT_IMSG_FETCH_REF,
116 GOT_IMSG_FETCH_SERVER_PROGRESS,
117 GOT_IMSG_FETCH_DOWNLOAD_PROGRESS,
118 GOT_IMSG_FETCH_DONE,
119 GOT_IMSG_IDXPACK_REQUEST,
120 GOT_IMSG_IDXPACK_OUTFD,
121 GOT_IMSG_IDXPACK_PROGRESS,
122 GOT_IMSG_IDXPACK_DONE,
124 /* Messages related to pack files. */
125 GOT_IMSG_PACKIDX,
126 GOT_IMSG_PACK,
127 GOT_IMSG_PACKED_OBJECT_REQUEST,
128 GOT_IMSG_COMMIT_TRAVERSAL_REQUEST,
129 GOT_IMSG_TRAVERSED_COMMITS,
130 GOT_IMSG_COMMIT_TRAVERSAL_DONE,
132 /* Message sending file descriptor to a temporary file. */
133 GOT_IMSG_TMPFD,
135 /* Messages related to gitconfig files. */
136 GOT_IMSG_GITCONFIG_PARSE_REQUEST,
137 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST,
138 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST,
139 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST,
140 GOT_IMSG_GITCONFIG_REMOTES_REQUEST,
141 GOT_IMSG_GITCONFIG_INT_VAL,
142 GOT_IMSG_GITCONFIG_STR_VAL,
143 GOT_IMSG_GITCONFIG_REMOTES,
144 GOT_IMSG_GITCONFIG_REMOTE,
145 GOT_IMSG_GITCONFIG_OWNER_REQUEST,
146 GOT_IMSG_GITCONFIG_OWNER,
147 };
149 /* Structure for GOT_IMSG_ERROR. */
150 struct got_imsg_error {
151 int code; /* an error code from got_error.h */
152 int errno_code; /* in case code equals GOT_ERR_ERRNO */
153 } __attribute__((__packed__));
155 /*
156 * Structure for GOT_IMSG_TREE_REQUEST and GOT_IMSG_OBJECT data.
157 */
158 struct got_imsg_object {
159 uint8_t id[SHA1_DIGEST_LENGTH];
161 /* These fields are the same as in struct got_object. */
162 int type;
163 int flags;
164 size_t hdrlen;
165 size_t size;
166 off_t pack_offset;
167 int pack_idx;
168 } __attribute__((__packed__));
170 /* Structure for GOT_IMSG_COMMIT data. */
171 struct got_imsg_commit_object {
172 uint8_t tree_id[SHA1_DIGEST_LENGTH];
173 size_t author_len;
174 time_t author_time;
175 time_t author_gmtoff;
176 size_t committer_len;
177 time_t committer_time;
178 time_t committer_gmtoff;
179 size_t logmsg_len;
180 int nparents;
182 /*
183 * Followed by author_len + committer_len data bytes
184 */
186 /* Followed by 'nparents' SHA1_DIGEST_LENGTH length strings */
188 /*
189 * Followed by 'logmsg_len' bytes of commit log message data in
190 * one or more GOT_IMSG_COMMIT_LOGMSG messages.
191 */
192 } __attribute__((__packed__));
195 /* Structure for GOT_IMSG_TREE_ENTRY. */
196 struct got_imsg_tree_entry {
197 char id[SHA1_DIGEST_LENGTH];
198 mode_t mode;
199 /* Followed by entry's name in remaining data of imsg buffer. */
200 } __attribute__((__packed__));
202 /* Structure for GOT_IMSG_TREE_OBJECT_REPLY data. */
203 struct got_imsg_tree_object {
204 int nentries; /* This many TREE_ENTRY messages follow. */
205 };
207 /* Structure for GOT_IMSG_BLOB. */
208 struct got_imsg_blob {
209 size_t size;
210 size_t hdrlen;
212 /*
213 * If size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX, blob data follows
214 * in the imsg buffer. Otherwise, blob data has been written to a
215 * file descriptor passed via the GOT_IMSG_BLOB_OUTFD imsg.
216 */
217 #define GOT_PRIVSEP_INLINE_BLOB_DATA_MAX \
218 (MAX_IMSGSIZE - IMSG_HEADER_SIZE - sizeof(struct got_imsg_blob))
219 };
222 /* Structure for GOT_IMSG_TAG data. */
223 struct got_imsg_tag_object {
224 uint8_t id[SHA1_DIGEST_LENGTH];
225 int obj_type;
226 size_t tag_len;
227 size_t tagger_len;
228 time_t tagger_time;
229 time_t tagger_gmtoff;
230 size_t tagmsg_len;
232 /*
233 * Followed by tag_len + tagger_len data bytes
234 */
236 /*
237 * Followed by 'tagmsg_len' bytes of tag message data in
238 * one or more GOT_IMSG_TAG_TAGMSG messages.
239 */
240 } __attribute__((__packed__));
242 /* Structures for GOT_IMSG_FETCH_REQUEST data. */
243 struct got_imsg_fetch_have_ref {
244 uint8_t id[SHA1_DIGEST_LENGTH];
245 size_t name_len;
246 /* Followed by name_len data bytes. */
247 } __attribute__((__packed__));
249 struct got_imsg_fetch_request {
250 int fetch_all_branches;
251 size_t n_have_refs;
252 /* Followed by n_have_refs times of got_imsg_fetch_have_ref data. */
253 } __attribute__((__packed__));
255 /* Structures for GOT_IMSG_FETCH_SYMREFS data. */
256 struct got_imsg_fetch_symref {
257 size_t name_len;
258 size_t target_len;
260 /*
261 * Followed by name_len + target_len data bytes.
262 */
263 } __attribute__((__packed__));
265 struct got_imsg_fetch_symrefs {
266 size_t nsymrefs;
268 /* Followed by nsymrefs times of got_imsg_fetch_symref data. */
269 } __attribute__((__packed__));
271 /* Structure for GOT_IMSG_FETCH_REF data. */
272 struct got_imsg_fetch_ref {
273 /* Describes a reference which will be fetched. */
274 uint8_t refid[SHA1_DIGEST_LENGTH];
275 /* Followed by reference name in remaining data of imsg buffer. */
276 };
278 /* Structure for GOT_IMSG_FETCH_DOWNLOAD_PROGRESS data. */
279 struct got_imsg_fetch_download_progress {
280 /* Number of packfile data bytes downloaded so far. */
281 off_t packfile_bytes;
282 };
284 /* Structure for GOT_IMSG_IDXPACK_REQUEST data. */
285 struct got_imsg_index_pack_request {
286 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
287 } __attribute__((__packed__));
289 /* Structure for GOT_IMSG_IDXPACK_PROGRESS data. */
290 struct got_imsg_index_pack_progress {
291 /* Total number of objects in pack file. */
292 int nobj_total;
294 /* Number of objects indexed so far. */
295 int nobj_indexed;
297 /* Number of non-deltified objects in pack file. */
298 int nobj_loose;
300 /* Number of deltified objects resolved so far. */
301 int nobj_resolved;
302 };
304 /* Structure for GOT_IMSG_PACKIDX. */
305 struct got_imsg_packidx {
306 size_t len;
307 /* Additionally, a file desciptor is passed via imsg. */
308 };
310 /* Structure for GOT_IMSG_PACK. */
311 struct got_imsg_pack {
312 char path_packfile[PATH_MAX];
313 size_t filesize;
314 /* Additionally, a file desciptor is passed via imsg. */
315 } __attribute__((__packed__));
317 /*
318 * Structure for GOT_IMSG_PACKED_OBJECT_REQUEST data.
319 */
320 struct got_imsg_packed_object {
321 uint8_t id[SHA1_DIGEST_LENGTH];
322 int idx;
323 } __attribute__((__packed__));
325 /* Structure for GOT_IMSG_COMMIT_TRAVERSAL_REQUEST */
326 struct got_imsg_commit_traversal_request {
327 uint8_t id[SHA1_DIGEST_LENGTH];
328 int idx;
329 size_t path_len;
330 /* Followed by path_len bytes of path data */
331 } __attribute__((__packed__));
333 /* Structure for GOT_IMSG_TRAVERSED_COMMITS */
334 struct got_imsg_traversed_commits {
335 size_t ncommits;
336 /* Followed by ncommit IDs of SHA1_DIGEST_LENGTH each */
337 } __attribute__((__packed__));
339 /*
340 * Structure for GOT_IMSG_GITCONFIG_REMOTE data.
341 */
342 struct got_imsg_remote {
343 size_t name_len;
344 size_t url_len;
345 int mirror_references;
347 /* Followed by name_len + url_len data bytes. */
348 };
350 /*
351 * Structure for GOT_IMSG_GITCONFIG_REMOTES data.
352 */
353 struct got_imsg_remotes {
354 int nremotes; /* This many GOT_IMSG_GITCONFIG_REMOTE messages follow. */
355 };
357 struct got_remote_repo;
358 struct got_pack;
359 struct got_packidx;
360 struct got_pathlist_head;
362 const struct got_error *got_send_ack(pid_t);
363 const struct got_error *got_privsep_wait_for_child(pid_t);
364 const struct got_error *got_privsep_send_stop(int);
365 const struct got_error *got_privsep_recv_imsg(struct imsg *, struct imsgbuf *,
366 size_t);
367 void got_privsep_send_error(struct imsgbuf *, const struct got_error *);
368 const struct got_error *got_privsep_send_ack(struct imsgbuf *);
369 const struct got_error *got_privsep_wait_ack(struct imsgbuf *);
370 const struct got_error *got_privsep_send_obj_req(struct imsgbuf *, int);
371 const struct got_error *got_privsep_send_commit_req(struct imsgbuf *, int,
372 struct got_object_id *, int);
373 const struct got_error *got_privsep_send_tree_req(struct imsgbuf *, int,
374 struct got_object_id *, int);
375 const struct got_error *got_privsep_send_tag_req(struct imsgbuf *, int,
376 struct got_object_id *, int);
377 const struct got_error *got_privsep_send_blob_req(struct imsgbuf *, int,
378 struct got_object_id *, int);
379 const struct got_error *got_privsep_send_blob_outfd(struct imsgbuf *, int);
380 const struct got_error *got_privsep_send_tmpfd(struct imsgbuf *, int);
381 const struct got_error *got_privsep_send_obj(struct imsgbuf *,
382 struct got_object *);
383 const struct got_error *got_privsep_send_index_pack_req(struct imsgbuf *,
384 uint8_t *, int);
385 const struct got_error *got_privsep_send_index_pack_outfd(struct imsgbuf *,
386 int);
387 const struct got_error *got_privsep_send_index_pack_progress(struct imsgbuf *,
388 int, int, int, int);
389 const struct got_error *got_privsep_send_index_pack_done(struct imsgbuf *);
390 const struct got_error *got_privsep_recv_index_progress(int *, int *, int *,
391 int *, int *, struct imsgbuf *ibuf);
392 const struct got_error *got_privsep_send_fetch_req(struct imsgbuf *, int,
393 struct got_pathlist_head *, int);
394 const struct got_error *got_privsep_send_fetch_outfd(struct imsgbuf *, int);
395 const struct got_error *got_privsep_send_fetch_symrefs(struct imsgbuf *,
396 struct got_pathlist_head *);
397 const struct got_error *got_privsep_send_fetch_ref(struct imsgbuf *,
398 struct got_object_id *, const char *);
399 const struct got_error *got_privsep_send_fetch_server_progress(struct imsgbuf *,
400 const char *, size_t);
401 const struct got_error *got_privsep_send_fetch_download_progress(struct imsgbuf *,
402 off_t);
403 const struct got_error *got_privsep_recv_fetch_progress(int *,
404 struct got_object_id **, char **, struct got_pathlist_head *,
405 char **, off_t *, struct imsgbuf *);
406 const struct got_error *got_privsep_send_fetch_done(struct imsgbuf *,
407 struct got_object_id);
408 const struct got_error *got_privsep_get_imsg_obj(struct got_object **,
409 struct imsg *, struct imsgbuf *);
410 const struct got_error *got_privsep_recv_obj(struct got_object **,
411 struct imsgbuf *);
412 const struct got_error *got_privsep_send_commit(struct imsgbuf *,
413 struct got_commit_object *);
414 const struct got_error *got_privsep_recv_commit(struct got_commit_object **,
415 struct imsgbuf *);
416 const struct got_error *got_privsep_recv_tree(struct got_tree_object **,
417 struct imsgbuf *);
418 const struct got_error *got_privsep_send_tree(struct imsgbuf *,
419 struct got_pathlist_head *, int);
420 const struct got_error *got_privsep_send_blob(struct imsgbuf *, size_t, size_t,
421 const uint8_t *);
422 const struct got_error *got_privsep_recv_blob(uint8_t **, size_t *, size_t *,
423 struct imsgbuf *);
424 const struct got_error *got_privsep_send_tag(struct imsgbuf *,
425 struct got_tag_object *);
426 const struct got_error *got_privsep_recv_tag(struct got_tag_object **,
427 struct imsgbuf *);
428 const struct got_error *got_privsep_init_pack_child(struct imsgbuf *,
429 struct got_pack *, struct got_packidx *);
430 const struct got_error *got_privsep_send_packed_obj_req(struct imsgbuf *, int,
431 struct got_object_id *);
432 const struct got_error *got_privsep_send_pack_child_ready(struct imsgbuf *);
434 const struct got_error *got_privsep_send_gitconfig_parse_req(struct imsgbuf *,
435 int);
436 const struct got_error *
437 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *);
438 const struct got_error *got_privsep_send_gitconfig_author_name_req(
439 struct imsgbuf *);
440 const struct got_error *got_privsep_send_gitconfig_author_email_req(
441 struct imsgbuf *);
442 const struct got_error *got_privsep_send_gitconfig_remotes_req(
443 struct imsgbuf *);
444 const struct got_error *got_privsep_send_gitconfig_owner_req(struct imsgbuf *);
445 const struct got_error *got_privsep_send_gitconfig_str(struct imsgbuf *,
446 const char *);
447 const struct got_error *got_privsep_recv_gitconfig_str(char **,
448 struct imsgbuf *);
449 const struct got_error *got_privsep_send_gitconfig_int(struct imsgbuf *, int);
450 const struct got_error *got_privsep_recv_gitconfig_int(int *, struct imsgbuf *);
451 const struct got_error *got_privsep_send_gitconfig_remotes(struct imsgbuf *,
452 struct got_remote_repo *, int);
453 const struct got_error *got_privsep_recv_gitconfig_remotes(
454 struct got_remote_repo **, int *, struct imsgbuf *);
456 const struct got_error *got_privsep_send_commit_traversal_request(
457 struct imsgbuf *, struct got_object_id *, int, const char *);
458 const struct got_error *got_privsep_recv_traversed_commits(
459 struct got_commit_object **, struct got_object_id **,
460 struct got_object_id_queue *, struct imsgbuf *);
461 const struct got_error *got_privsep_send_traversed_commits(
462 struct got_object_id *, size_t, struct imsgbuf *);
463 const struct got_error *got_privsep_send_commit_traversal_done(
464 struct imsgbuf *);
466 void got_privsep_exec_child(int[2], const char *, const char *);