2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
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.
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.
27 * We exec(2) after a fork(2). Parts of our library functionality are
28 * accessible via separate executables in a libexec directory.
31 #define GOT_IMSG_FD_CHILD (STDERR_FILENO + 1)
33 #ifndef GOT_LIBEXECDIR
34 #define GOT_LIBEXECDIR /usr/libexec
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 {
72 /* An error occured while processing a request. */
75 /* Stop the child process. */
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.
86 GOT_IMSG_OBJECT_REQUEST,
88 GOT_IMSG_COMMIT_REQUEST,
90 GOT_IMSG_COMMIT_LOGMSG,
91 GOT_IMSG_TREE_REQUEST,
94 GOT_IMSG_BLOB_REQUEST,
101 /* Messages related to pack files. */
104 GOT_IMSG_PACKED_OBJECT_REQUEST,
106 /* Message sending file descriptor to a temporary file. */
109 /* Messages related to gitconfig files. */
110 GOT_IMSG_GITCONFIG_PARSE_REQUEST,
111 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST,
112 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST,
113 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST,
114 GOT_IMSG_GITCONFIG_REMOTES_REQUEST,
115 GOT_IMSG_GITCONFIG_INT_VAL,
116 GOT_IMSG_GITCONFIG_STR_VAL,
117 GOT_IMSG_GITCONFIG_REMOTES,
118 GOT_IMSG_GITCONFIG_REMOTE,
121 /* Structure for GOT_IMSG_ERROR. */
122 struct got_imsg_error {
123 int code; /* an error code from got_error.h */
124 int errno_code; /* in case code equals GOT_ERR_ERRNO */
125 } __attribute__((__packed__));
128 * Structure for GOT_IMSG_TREE_REQUEST and GOT_IMSG_OBJECT data.
130 struct got_imsg_object {
131 uint8_t id[SHA1_DIGEST_LENGTH];
133 /* These fields are the same as in struct got_object. */
140 } __attribute__((__packed__));
142 /* Structure for GOT_IMSG_COMMIT data. */
143 struct got_imsg_commit_object {
144 uint8_t tree_id[SHA1_DIGEST_LENGTH];
147 time_t author_gmtoff;
148 size_t committer_len;
149 time_t committer_time;
150 time_t committer_gmtoff;
155 * Followed by author_len + committer_len data bytes
158 /* Followed by 'nparents' SHA1_DIGEST_LENGTH length strings */
161 * Followed by 'logmsg_len' bytes of commit log message data in
162 * one or more GOT_IMSG_COMMIT_LOGMSG messages.
164 } __attribute__((__packed__));
167 /* Structure for GOT_IMSG_TREE_ENTRY. */
168 struct got_imsg_tree_entry {
169 char id[SHA1_DIGEST_LENGTH];
171 /* Followed by entry's name in remaining data of imsg buffer. */
172 } __attribute__((__packed__));
174 /* Structure for GOT_IMSG_TREE_OBJECT_REPLY data. */
175 struct got_imsg_tree_object {
176 int nentries; /* This many TREE_ENTRY messages follow. */
179 /* Structure for GOT_IMSG_BLOB. */
180 struct got_imsg_blob {
185 * If size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX, blob data follows
186 * in the imsg buffer. Otherwise, blob data has been written to a
187 * file descriptor passed via the GOT_IMSG_BLOB_OUTFD imsg.
189 #define GOT_PRIVSEP_INLINE_BLOB_DATA_MAX \
190 (MAX_IMSGSIZE - IMSG_HEADER_SIZE - sizeof(struct got_imsg_blob))
194 /* Structure for GOT_IMSG_TAG data. */
195 struct got_imsg_tag_object {
196 uint8_t id[SHA1_DIGEST_LENGTH];
201 time_t tagger_gmtoff;
205 * Followed by tag_len + tagger_len data bytes
209 * Followed by 'tagmsg_len' bytes of tag message data in
210 * one or more GOT_IMSG_TAG_TAGMSG messages.
212 } __attribute__((__packed__));
214 /* Structure for GOT_IMSG_PACKIDX. */
215 struct got_imsg_packidx {
217 /* Additionally, a file desciptor is passed via imsg. */
220 /* Structure for GOT_IMSG_PACK. */
221 struct got_imsg_pack {
222 char path_packfile[PATH_MAX];
224 /* Additionally, a file desciptor is passed via imsg. */
225 } __attribute__((__packed__));
228 * Structure for GOT_IMSG_PACKED_OBJECT_REQUEST data.
230 struct got_imsg_packed_object {
231 uint8_t id[SHA1_DIGEST_LENGTH];
233 } __attribute__((__packed__));
236 * Structure for GOT_IMSG_GITCONFIG_REMOTE data.
238 struct got_imsg_remote {
242 /* Followed by name_len + url_len data bytes. */
246 * Structure for GOT_IMSG_GITCONFIG_REMOTES data.
248 struct got_imsg_remotes {
249 int nremotes; /* This many GOT_IMSG_GITCONFIG_REMOTE messages follow. */
252 struct got_remote_repo;
255 struct got_pathlist_head;
257 const struct got_error *got_privsep_wait_for_child(pid_t);
258 const struct got_error *got_privsep_send_stop(int);
259 const struct got_error *got_privsep_recv_imsg(struct imsg *, struct imsgbuf *,
261 void got_privsep_send_error(struct imsgbuf *, const struct got_error *);
262 const struct got_error *got_privsep_send_obj_req(struct imsgbuf *, int);
263 const struct got_error *got_privsep_send_commit_req(struct imsgbuf *, int,
264 struct got_object_id *, int);
265 const struct got_error *got_privsep_send_tree_req(struct imsgbuf *, int,
266 struct got_object_id *, int);
267 const struct got_error *got_privsep_send_tag_req(struct imsgbuf *, int,
268 struct got_object_id *, int);
269 const struct got_error *got_privsep_send_blob_req(struct imsgbuf *, int,
270 struct got_object_id *, int);
271 const struct got_error *got_privsep_send_blob_outfd(struct imsgbuf *, int);
272 const struct got_error *got_privsep_send_tmpfd(struct imsgbuf *, int);
273 const struct got_error *got_privsep_send_obj(struct imsgbuf *,
274 struct got_object *);
275 const struct got_error *got_privsep_get_imsg_obj(struct got_object **,
276 struct imsg *, struct imsgbuf *);
277 const struct got_error *got_privsep_recv_obj(struct got_object **,
279 const struct got_error *got_privsep_send_commit(struct imsgbuf *,
280 struct got_commit_object *);
281 const struct got_error *got_privsep_recv_commit(struct got_commit_object **,
283 const struct got_error *got_privsep_recv_tree(struct got_tree_object **,
285 const struct got_error *got_privsep_send_tree(struct imsgbuf *,
286 struct got_pathlist_head *, int);
287 const struct got_error *got_privsep_send_blob(struct imsgbuf *, size_t, size_t,
289 const struct got_error *got_privsep_recv_blob(uint8_t **, size_t *, size_t *,
291 const struct got_error *got_privsep_send_tag(struct imsgbuf *,
292 struct got_tag_object *);
293 const struct got_error *got_privsep_recv_tag(struct got_tag_object **,
295 const struct got_error *got_privsep_init_pack_child(struct imsgbuf *,
296 struct got_pack *, struct got_packidx *);
297 const struct got_error *got_privsep_send_packed_obj_req(struct imsgbuf *, int,
298 struct got_object_id *);
299 const struct got_error *got_privsep_send_pack_child_ready(struct imsgbuf *);
301 const struct got_error *got_privsep_send_gitconfig_parse_req(struct imsgbuf *,
303 const struct got_error *
304 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *);
305 const struct got_error *got_privsep_send_gitconfig_author_name_req(
307 const struct got_error *got_privsep_send_gitconfig_author_email_req(
309 const struct got_error *got_privsep_send_gitconfig_remotes_req(
311 const struct got_error *got_privsep_send_gitconfig_str(struct imsgbuf *,
313 const struct got_error *got_privsep_recv_gitconfig_str(char **,
315 const struct got_error *got_privsep_send_gitconfig_int(struct imsgbuf *, int);
316 const struct got_error *got_privsep_recv_gitconfig_int(int *, struct imsgbuf *);
317 const struct got_error *got_privsep_send_gitconfig_remotes(struct imsgbuf *,
318 struct got_remote_repo *, int);
319 const struct got_error *got_privsep_recv_gitconfig_remotes(
320 struct got_remote_repo **, int *, struct imsgbuf *);
322 void got_privsep_exec_child(int[2], const char *, const char *);