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_PROGRESS,
114 GOT_IMSG_FETCH_DONE,
115 GOT_IMSG_IDXPACK_REQUEST,
116 GOT_IMSG_IDXPACK_DONE,
118 /* Messages related to pack files. */
119 GOT_IMSG_PACKIDX,
120 GOT_IMSG_PACK,
121 GOT_IMSG_PACKED_OBJECT_REQUEST,
122 GOT_IMSG_COMMIT_TRAVERSAL_REQUEST,
123 GOT_IMSG_TRAVERSED_COMMITS,
124 GOT_IMSG_COMMIT_TRAVERSAL_DONE,
126 /* Message sending file descriptor to a temporary file. */
127 GOT_IMSG_TMPFD,
129 /* Messages related to gitconfig files. */
130 GOT_IMSG_GITCONFIG_PARSE_REQUEST,
131 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST,
132 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST,
133 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST,
134 GOT_IMSG_GITCONFIG_REMOTES_REQUEST,
135 GOT_IMSG_GITCONFIG_INT_VAL,
136 GOT_IMSG_GITCONFIG_STR_VAL,
137 GOT_IMSG_GITCONFIG_REMOTES,
138 GOT_IMSG_GITCONFIG_REMOTE,
139 GOT_IMSG_GITCONFIG_OWNER_REQUEST,
140 GOT_IMSG_GITCONFIG_OWNER,
141 };
143 /* Structure for GOT_IMSG_ERROR. */
144 struct got_imsg_error {
145 int code; /* an error code from got_error.h */
146 int errno_code; /* in case code equals GOT_ERR_ERRNO */
147 } __attribute__((__packed__));
149 /*
150 * Structure for GOT_IMSG_TREE_REQUEST and GOT_IMSG_OBJECT data.
151 */
152 struct got_imsg_object {
153 uint8_t id[SHA1_DIGEST_LENGTH];
155 /* These fields are the same as in struct got_object. */
156 int type;
157 int flags;
158 size_t hdrlen;
159 size_t size;
160 off_t pack_offset;
161 int pack_idx;
162 } __attribute__((__packed__));
164 /* Structure for GOT_IMSG_COMMIT data. */
165 struct got_imsg_commit_object {
166 uint8_t tree_id[SHA1_DIGEST_LENGTH];
167 size_t author_len;
168 time_t author_time;
169 time_t author_gmtoff;
170 size_t committer_len;
171 time_t committer_time;
172 time_t committer_gmtoff;
173 size_t logmsg_len;
174 int nparents;
176 /*
177 * Followed by author_len + committer_len data bytes
178 */
180 /* Followed by 'nparents' SHA1_DIGEST_LENGTH length strings */
182 /*
183 * Followed by 'logmsg_len' bytes of commit log message data in
184 * one or more GOT_IMSG_COMMIT_LOGMSG messages.
185 */
186 } __attribute__((__packed__));
189 /* Structure for GOT_IMSG_TREE_ENTRY. */
190 struct got_imsg_tree_entry {
191 char id[SHA1_DIGEST_LENGTH];
192 mode_t mode;
193 /* Followed by entry's name in remaining data of imsg buffer. */
194 } __attribute__((__packed__));
196 /* Structure for GOT_IMSG_TREE_OBJECT_REPLY data. */
197 struct got_imsg_tree_object {
198 int nentries; /* This many TREE_ENTRY messages follow. */
199 };
201 /* Structure for GOT_IMSG_BLOB. */
202 struct got_imsg_blob {
203 size_t size;
204 size_t hdrlen;
206 /*
207 * If size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX, blob data follows
208 * in the imsg buffer. Otherwise, blob data has been written to a
209 * file descriptor passed via the GOT_IMSG_BLOB_OUTFD imsg.
210 */
211 #define GOT_PRIVSEP_INLINE_BLOB_DATA_MAX \
212 (MAX_IMSGSIZE - IMSG_HEADER_SIZE - sizeof(struct got_imsg_blob))
213 };
216 /* Structure for GOT_IMSG_TAG data. */
217 struct got_imsg_tag_object {
218 uint8_t id[SHA1_DIGEST_LENGTH];
219 int obj_type;
220 size_t tag_len;
221 size_t tagger_len;
222 time_t tagger_time;
223 time_t tagger_gmtoff;
224 size_t tagmsg_len;
226 /*
227 * Followed by tag_len + tagger_len data bytes
228 */
230 /*
231 * Followed by 'tagmsg_len' bytes of tag message data in
232 * one or more GOT_IMSG_TAG_TAGMSG messages.
233 */
234 } __attribute__((__packed__));
236 /* Structure for GOT_IMSG_FETCH_PROGRESS data. */
237 struct got_imsg_fetch_progress {
238 /* Descirbes a reference which will be fetched. */
239 uint8_t refid[SHA1_DIGEST_LENGTH];
240 /* Followed by reference name in remaining data of imsg buffer. */
241 };
243 /* Structure for GOT_IMSG_PACKIDX. */
244 struct got_imsg_packidx {
245 size_t len;
246 /* Additionally, a file desciptor is passed via imsg. */
247 };
249 /* Structure for GOT_IMSG_PACK. */
250 struct got_imsg_pack {
251 char path_packfile[PATH_MAX];
252 size_t filesize;
253 /* Additionally, a file desciptor is passed via imsg. */
254 } __attribute__((__packed__));
256 /*
257 * Structure for GOT_IMSG_PACKED_OBJECT_REQUEST data.
258 */
259 struct got_imsg_packed_object {
260 uint8_t id[SHA1_DIGEST_LENGTH];
261 int idx;
262 } __attribute__((__packed__));
264 /* Structure for GOT_IMSG_COMMIT_TRAVERSAL_REQUEST */
265 struct got_imsg_commit_traversal_request {
266 uint8_t id[SHA1_DIGEST_LENGTH];
267 int idx;
268 size_t path_len;
269 /* Followed by path_len bytes of path data */
270 } __attribute__((__packed__));
272 /* Structure for GOT_IMSG_TRAVERSED_COMMITS */
273 struct got_imsg_traversed_commits {
274 size_t ncommits;
275 /* Followed by ncommit IDs of SHA1_DIGEST_LENGTH each */
276 } __attribute__((__packed__));
278 /*
279 * Structure for GOT_IMSG_GITCONFIG_REMOTE data.
280 */
281 struct got_imsg_remote {
282 size_t name_len;
283 size_t url_len;
285 /* Followed by name_len + url_len data bytes. */
286 };
288 /*
289 * Structure for GOT_IMSG_GITCONFIG_REMOTES data.
290 */
291 struct got_imsg_remotes {
292 int nremotes; /* This many GOT_IMSG_GITCONFIG_REMOTE messages follow. */
293 };
295 struct got_remote_repo;
296 struct got_pack;
297 struct got_packidx;
298 struct got_pathlist_head;
300 const struct got_error *got_send_ack(pid_t);
301 const struct got_error *got_privsep_wait_for_child(pid_t);
302 const struct got_error *got_privsep_send_stop(int);
303 const struct got_error *got_privsep_recv_imsg(struct imsg *, struct imsgbuf *,
304 size_t);
305 void got_privsep_send_error(struct imsgbuf *, const struct got_error *);
306 const struct got_error *got_privsep_send_ack(struct imsgbuf *);
307 const struct got_error *got_privsep_wait_ack(struct imsgbuf *);
308 const struct got_error *got_privsep_send_obj_req(struct imsgbuf *, int);
309 const struct got_error *got_privsep_send_commit_req(struct imsgbuf *, int,
310 struct got_object_id *, int);
311 const struct got_error *got_privsep_send_tree_req(struct imsgbuf *, int,
312 struct got_object_id *, int);
313 const struct got_error *got_privsep_send_tag_req(struct imsgbuf *, int,
314 struct got_object_id *, int);
315 const struct got_error *got_privsep_send_blob_req(struct imsgbuf *, int,
316 struct got_object_id *, int);
317 const struct got_error *got_privsep_send_blob_outfd(struct imsgbuf *, int);
318 const struct got_error *got_privsep_send_tmpfd(struct imsgbuf *, int);
319 const struct got_error *got_privsep_send_obj(struct imsgbuf *,
320 struct got_object *);
321 const struct got_error *got_privsep_send_index_pack_req(struct imsgbuf *, int,
322 struct got_object_id *);
323 const struct got_error *got_privsep_send_index_pack_done(struct imsgbuf *);
324 const struct got_error *got_privsep_wait_index_pack_done(struct imsgbuf *);
325 const struct got_error *got_privsep_send_fetch_req(struct imsgbuf *, int);
326 const struct got_error *got_privsep_send_fetch_progress(struct imsgbuf *,
327 struct got_object_id *, const char *);
328 const struct got_error *got_privsep_recv_fetch_progress(int *,
329 struct got_object_id **, char **, struct imsgbuf *);
330 const struct got_error *got_privsep_send_fetch_done(struct imsgbuf *,
331 struct got_object_id);
332 const struct got_error *got_privsep_get_imsg_obj(struct got_object **,
333 struct imsg *, struct imsgbuf *);
334 const struct got_error *got_privsep_recv_obj(struct got_object **,
335 struct imsgbuf *);
336 const struct got_error *got_privsep_send_commit(struct imsgbuf *,
337 struct got_commit_object *);
338 const struct got_error *got_privsep_recv_commit(struct got_commit_object **,
339 struct imsgbuf *);
340 const struct got_error *got_privsep_recv_tree(struct got_tree_object **,
341 struct imsgbuf *);
342 const struct got_error *got_privsep_send_tree(struct imsgbuf *,
343 struct got_pathlist_head *, int);
344 const struct got_error *got_privsep_send_blob(struct imsgbuf *, size_t, size_t,
345 const uint8_t *);
346 const struct got_error *got_privsep_recv_blob(uint8_t **, size_t *, size_t *,
347 struct imsgbuf *);
348 const struct got_error *got_privsep_send_tag(struct imsgbuf *,
349 struct got_tag_object *);
350 const struct got_error *got_privsep_recv_tag(struct got_tag_object **,
351 struct imsgbuf *);
352 const struct got_error *got_privsep_init_pack_child(struct imsgbuf *,
353 struct got_pack *, struct got_packidx *);
354 const struct got_error *got_privsep_send_packed_obj_req(struct imsgbuf *, int,
355 struct got_object_id *);
356 const struct got_error *got_privsep_send_pack_child_ready(struct imsgbuf *);
358 const struct got_error *got_privsep_send_gitconfig_parse_req(struct imsgbuf *,
359 int);
360 const struct got_error *
361 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *);
362 const struct got_error *got_privsep_send_gitconfig_author_name_req(
363 struct imsgbuf *);
364 const struct got_error *got_privsep_send_gitconfig_author_email_req(
365 struct imsgbuf *);
366 const struct got_error *got_privsep_send_gitconfig_remotes_req(
367 struct imsgbuf *);
368 const struct got_error *got_privsep_send_gitconfig_owner_req(struct imsgbuf *);
369 const struct got_error *got_privsep_send_gitconfig_str(struct imsgbuf *,
370 const char *);
371 const struct got_error *got_privsep_recv_gitconfig_str(char **,
372 struct imsgbuf *);
373 const struct got_error *got_privsep_send_gitconfig_int(struct imsgbuf *, int);
374 const struct got_error *got_privsep_recv_gitconfig_int(int *, struct imsgbuf *);
375 const struct got_error *got_privsep_send_gitconfig_remotes(struct imsgbuf *,
376 struct got_remote_repo *, int);
377 const struct got_error *got_privsep_recv_gitconfig_remotes(
378 struct got_remote_repo **, int *, struct imsgbuf *);
380 const struct got_error *got_privsep_send_commit_traversal_request(
381 struct imsgbuf *, struct got_object_id *, int, const char *);
382 const struct got_error *got_privsep_recv_traversed_commits(
383 struct got_commit_object **, struct got_object_id **,
384 struct got_object_id_queue *, struct imsgbuf *);
385 const struct got_error *got_privsep_send_traversed_commits(
386 struct got_object_id *, size_t, struct imsgbuf *);
387 const struct got_error *got_privsep_send_commit_traversal_done(
388 struct imsgbuf *);
390 void got_privsep_exec_child(int[2], const char *, const char *);