Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 /*
18 * All code runs under the same UID but sensitive code paths are
19 * run in a separate process with tighter pledge(2) promises.
20 * Data is communicated between processes via imsg_flush(3) and imsg_read(3).
21 * This behaviour is transparent to users of the library.
22 *
23 * We generally transmit data in imsg buffers, split across several messages
24 * if necessary. File descriptors are used in cases where this is impractical,
25 * such as when accessing pack files or when transferring large blobs.
26 *
27 * We exec(2) after a fork(2). Parts of our library functionality are
28 * accessible via separate executables in a libexec directory.
29 */
31 #define GOT_IMSG_FD_CHILD (STDERR_FILENO + 1)
33 #ifndef GOT_LIBEXECDIR
34 #define GOT_LIBEXECDIR /usr/libexec
35 #endif
37 /* Names of helper programs in libexec directory */
38 #define GOT_PROG_READ_OBJECT got-read-object
39 #define GOT_PROG_READ_TREE got-read-tree
40 #define GOT_PROG_READ_COMMIT got-read-commit
41 #define GOT_PROG_READ_BLOB got-read-blob
42 #define GOT_PROG_READ_TAG got-read-tag
43 #define GOT_PROG_READ_PACK got-read-pack
44 #define GOT_PROG_READ_GITCONFIG got-read-gitconfig
46 #define GOT_STRINGIFY(x) #x
47 #define GOT_STRINGVAL(x) GOT_STRINGIFY(x)
49 /* Paths to helper programs in libexec directory */
50 #define GOT_PATH_PROG_READ_OBJECT \
51 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_OBJECT)
52 #define GOT_PATH_PROG_READ_TREE \
53 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_TREE)
54 #define GOT_PATH_PROG_READ_COMMIT \
55 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_COMMIT)
56 #define GOT_PATH_PROG_READ_BLOB \
57 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_BLOB)
58 #define GOT_PATH_PROG_READ_TAG \
59 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_TAG)
60 #define GOT_PATH_PROG_READ_PACK \
61 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_PACK)
62 #define GOT_PATH_PROG_READ_GITCONFIG \
63 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_GITCONFIG)
65 struct got_privsep_child {
66 int imsg_fd;
67 pid_t pid;
68 struct imsgbuf *ibuf;
69 };
71 enum got_imsg_type {
72 /* An error occured while processing a request. */
73 GOT_IMSG_ERROR,
75 /* Stop the child process. */
76 GOT_IMSG_STOP,
78 /*
79 * Messages concerned with read access to objects in a repository.
80 * Object and pack files are opened by the main process, where
81 * data may be read as a byte string but without any interpretation.
82 * Decompression and parsing of object and pack files occurs in a
83 * separate process which runs under pledge("stdio recvfd").
84 * This sandboxes our own repository parsing code, as well as zlib.
85 */
86 GOT_IMSG_OBJECT_REQUEST,
87 GOT_IMSG_OBJECT,
88 GOT_IMSG_COMMIT_REQUEST,
89 GOT_IMSG_COMMIT,
90 GOT_IMSG_COMMIT_LOGMSG,
91 GOT_IMSG_TREE_REQUEST,
92 GOT_IMSG_TREE,
93 GOT_IMSG_TREE_ENTRY,
94 GOT_IMSG_BLOB_REQUEST,
95 GOT_IMSG_BLOB_OUTFD,
96 GOT_IMSG_BLOB,
97 GOT_IMSG_TAG_REQUEST,
98 GOT_IMSG_TAG,
99 GOT_IMSG_TAG_TAGMSG,
101 /* Messages related to pack files. */
102 GOT_IMSG_PACKIDX,
103 GOT_IMSG_PACK,
104 GOT_IMSG_PACKED_OBJECT_REQUEST,
105 GOT_IMSG_COMMIT_TRAVERSAL_REQUEST,
106 GOT_IMSG_TRAVERSED_COMMITS,
107 GOT_IMSG_COMMIT_TRAVERSAL_DONE,
109 /* Message sending file descriptor to a temporary file. */
110 GOT_IMSG_TMPFD,
112 /* Messages related to gitconfig files. */
113 GOT_IMSG_GITCONFIG_PARSE_REQUEST,
114 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST,
115 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST,
116 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST,
117 GOT_IMSG_GITCONFIG_REMOTES_REQUEST,
118 GOT_IMSG_GITCONFIG_INT_VAL,
119 GOT_IMSG_GITCONFIG_STR_VAL,
120 GOT_IMSG_GITCONFIG_REMOTES,
121 GOT_IMSG_GITCONFIG_REMOTE,
122 GOT_IMSG_GITCONFIG_OWNER_REQUEST,
123 GOT_IMSG_GITCONFIG_OWNER,
124 };
126 /* Structure for GOT_IMSG_ERROR. */
127 struct got_imsg_error {
128 int code; /* an error code from got_error.h */
129 int errno_code; /* in case code equals GOT_ERR_ERRNO */
130 } __attribute__((__packed__));
132 /*
133 * Structure for GOT_IMSG_TREE_REQUEST and GOT_IMSG_OBJECT data.
134 */
135 struct got_imsg_object {
136 uint8_t id[SHA1_DIGEST_LENGTH];
138 /* These fields are the same as in struct got_object. */
139 int type;
140 int flags;
141 size_t hdrlen;
142 size_t size;
143 off_t pack_offset;
144 int pack_idx;
145 } __attribute__((__packed__));
147 /* Structure for GOT_IMSG_COMMIT data. */
148 struct got_imsg_commit_object {
149 uint8_t tree_id[SHA1_DIGEST_LENGTH];
150 size_t author_len;
151 time_t author_time;
152 time_t author_gmtoff;
153 size_t committer_len;
154 time_t committer_time;
155 time_t committer_gmtoff;
156 size_t logmsg_len;
157 int nparents;
159 /*
160 * Followed by author_len + committer_len data bytes
161 */
163 /* Followed by 'nparents' SHA1_DIGEST_LENGTH length strings */
165 /*
166 * Followed by 'logmsg_len' bytes of commit log message data in
167 * one or more GOT_IMSG_COMMIT_LOGMSG messages.
168 */
169 } __attribute__((__packed__));
172 /* Structure for GOT_IMSG_TREE_ENTRY. */
173 struct got_imsg_tree_entry {
174 char id[SHA1_DIGEST_LENGTH];
175 mode_t mode;
176 /* Followed by entry's name in remaining data of imsg buffer. */
177 } __attribute__((__packed__));
179 /* Structure for GOT_IMSG_TREE_OBJECT_REPLY data. */
180 struct got_imsg_tree_object {
181 int nentries; /* This many TREE_ENTRY messages follow. */
182 };
184 /* Structure for GOT_IMSG_BLOB. */
185 struct got_imsg_blob {
186 size_t size;
187 size_t hdrlen;
189 /*
190 * If size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX, blob data follows
191 * in the imsg buffer. Otherwise, blob data has been written to a
192 * file descriptor passed via the GOT_IMSG_BLOB_OUTFD imsg.
193 */
194 #define GOT_PRIVSEP_INLINE_BLOB_DATA_MAX \
195 (MAX_IMSGSIZE - IMSG_HEADER_SIZE - sizeof(struct got_imsg_blob))
196 };
199 /* Structure for GOT_IMSG_TAG data. */
200 struct got_imsg_tag_object {
201 uint8_t id[SHA1_DIGEST_LENGTH];
202 int obj_type;
203 size_t tag_len;
204 size_t tagger_len;
205 time_t tagger_time;
206 time_t tagger_gmtoff;
207 size_t tagmsg_len;
209 /*
210 * Followed by tag_len + tagger_len data bytes
211 */
213 /*
214 * Followed by 'tagmsg_len' bytes of tag message data in
215 * one or more GOT_IMSG_TAG_TAGMSG messages.
216 */
217 } __attribute__((__packed__));
219 /* Structure for GOT_IMSG_PACKIDX. */
220 struct got_imsg_packidx {
221 size_t len;
222 /* Additionally, a file desciptor is passed via imsg. */
223 };
225 /* Structure for GOT_IMSG_PACK. */
226 struct got_imsg_pack {
227 char path_packfile[PATH_MAX];
228 size_t filesize;
229 /* Additionally, a file desciptor is passed via imsg. */
230 } __attribute__((__packed__));
232 /*
233 * Structure for GOT_IMSG_PACKED_OBJECT_REQUEST data.
234 */
235 struct got_imsg_packed_object {
236 uint8_t id[SHA1_DIGEST_LENGTH];
237 int idx;
238 } __attribute__((__packed__));
240 /* Structure for GOT_IMSG_COMMIT_TRAVERSAL_REQUEST */
241 struct got_imsg_commit_traversal_request {
242 uint8_t id[SHA1_DIGEST_LENGTH];
243 int idx;
244 size_t path_len;
245 /* Followed by path_len bytes of path data */
246 } __attribute__((__packed__));
248 /* Structure for GOT_IMSG_TRAVERSED_COMMITS */
249 struct got_imsg_traversed_commits {
250 size_t ncommits;
251 /* Followed by ncommit IDs of SHA1_DIGEST_LENGTH each */
252 } __attribute__((__packed__));
254 /*
255 * Structure for GOT_IMSG_GITCONFIG_REMOTE data.
256 */
257 struct got_imsg_remote {
258 size_t name_len;
259 size_t url_len;
261 /* Followed by name_len + url_len data bytes. */
262 };
264 /*
265 * Structure for GOT_IMSG_GITCONFIG_REMOTES data.
266 */
267 struct got_imsg_remotes {
268 int nremotes; /* This many GOT_IMSG_GITCONFIG_REMOTE messages follow. */
269 };
271 struct got_remote_repo;
272 struct got_pack;
273 struct got_packidx;
274 struct got_pathlist_head;
276 const struct got_error *got_privsep_wait_for_child(pid_t);
277 const struct got_error *got_privsep_send_stop(int);
278 const struct got_error *got_privsep_recv_imsg(struct imsg *, struct imsgbuf *,
279 size_t);
280 void got_privsep_send_error(struct imsgbuf *, const struct got_error *);
281 const struct got_error *got_privsep_send_obj_req(struct imsgbuf *, int);
282 const struct got_error *got_privsep_send_commit_req(struct imsgbuf *, int,
283 struct got_object_id *, int);
284 const struct got_error *got_privsep_send_tree_req(struct imsgbuf *, int,
285 struct got_object_id *, int);
286 const struct got_error *got_privsep_send_tag_req(struct imsgbuf *, int,
287 struct got_object_id *, int);
288 const struct got_error *got_privsep_send_blob_req(struct imsgbuf *, int,
289 struct got_object_id *, int);
290 const struct got_error *got_privsep_send_blob_outfd(struct imsgbuf *, int);
291 const struct got_error *got_privsep_send_tmpfd(struct imsgbuf *, int);
292 const struct got_error *got_privsep_send_obj(struct imsgbuf *,
293 struct got_object *);
294 const struct got_error *got_privsep_get_imsg_obj(struct got_object **,
295 struct imsg *, struct imsgbuf *);
296 const struct got_error *got_privsep_recv_obj(struct got_object **,
297 struct imsgbuf *);
298 const struct got_error *got_privsep_send_commit(struct imsgbuf *,
299 struct got_commit_object *);
300 const struct got_error *got_privsep_recv_commit(struct got_commit_object **,
301 struct imsgbuf *);
302 const struct got_error *got_privsep_recv_tree(struct got_tree_object **,
303 struct imsgbuf *);
304 const struct got_error *got_privsep_send_tree(struct imsgbuf *,
305 struct got_pathlist_head *, int);
306 const struct got_error *got_privsep_send_blob(struct imsgbuf *, size_t, size_t,
307 const uint8_t *);
308 const struct got_error *got_privsep_recv_blob(uint8_t **, size_t *, size_t *,
309 struct imsgbuf *);
310 const struct got_error *got_privsep_send_tag(struct imsgbuf *,
311 struct got_tag_object *);
312 const struct got_error *got_privsep_recv_tag(struct got_tag_object **,
313 struct imsgbuf *);
314 const struct got_error *got_privsep_init_pack_child(struct imsgbuf *,
315 struct got_pack *, struct got_packidx *);
316 const struct got_error *got_privsep_send_packed_obj_req(struct imsgbuf *, int,
317 struct got_object_id *);
318 const struct got_error *got_privsep_send_pack_child_ready(struct imsgbuf *);
320 const struct got_error *got_privsep_send_gitconfig_parse_req(struct imsgbuf *,
321 int);
322 const struct got_error *
323 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *);
324 const struct got_error *got_privsep_send_gitconfig_author_name_req(
325 struct imsgbuf *);
326 const struct got_error *got_privsep_send_gitconfig_author_email_req(
327 struct imsgbuf *);
328 const struct got_error *got_privsep_send_gitconfig_remotes_req(
329 struct imsgbuf *);
330 const struct got_error *got_privsep_send_gitconfig_owner_req(struct imsgbuf *);
331 const struct got_error *got_privsep_send_gitconfig_str(struct imsgbuf *,
332 const char *);
333 const struct got_error *got_privsep_recv_gitconfig_str(char **,
334 struct imsgbuf *);
335 const struct got_error *got_privsep_send_gitconfig_int(struct imsgbuf *, int);
336 const struct got_error *got_privsep_recv_gitconfig_int(int *, struct imsgbuf *);
337 const struct got_error *got_privsep_send_gitconfig_remotes(struct imsgbuf *,
338 struct got_remote_repo *, int);
339 const struct got_error *got_privsep_recv_gitconfig_remotes(
340 struct got_remote_repo **, int *, struct imsgbuf *);
342 const struct got_error *got_privsep_send_commit_traversal_request(
343 struct imsgbuf *, struct got_object_id *, int, const char *);
344 const struct got_error *got_privsep_recv_traversed_commits(
345 struct got_commit_object **, struct got_object_id **,
346 struct got_object_id_queue *, struct imsgbuf *);
347 const struct got_error *got_privsep_send_traversed_commits(
348 struct got_object_id *, size_t, struct imsgbuf *);
349 const struct got_error *got_privsep_send_commit_traversal_done(
350 struct imsgbuf *);
352 void got_privsep_exec_child(int[2], const char *, const char *);