2 * Copyright (c) 2018 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_PACK got-read-pack
44 #define GOT_STRINGIFY(x) #x
45 #define GOT_STRINGVAL(x) GOT_STRINGIFY(x)
47 /* Paths to helper programs in libexec directory */
48 #define GOT_PATH_PROG_READ_OBJECT \
49 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_OBJECT)
50 #define GOT_PATH_PROG_READ_TREE \
51 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_TREE)
52 #define GOT_PATH_PROG_READ_COMMIT \
53 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_COMMIT)
54 #define GOT_PATH_PROG_READ_BLOB \
55 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_BLOB)
56 #define GOT_PATH_PROG_READ_PACK \
57 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_PACK)
59 struct got_privsep_child {
66 /* An error occured while processing a request. */
69 /* Stop the child process. */
73 * Messages concerned with read access to objects in a repository.
74 * Object and pack files are opened by the main process, where
75 * data may be read as a byte string but without any interpretation.
76 * Decompression and parsing of object and pack files occurs in a
77 * separate process which runs under pledge("stdio recvfd").
78 * This sandboxes our own repository parsing code, as well as zlib.
80 GOT_IMSG_OBJECT_REQUEST,
82 GOT_IMSG_COMMIT_REQUEST,
84 GOT_IMSG_COMMIT_LOGMSG,
85 GOT_IMSG_MINI_COMMIT_REQUEST,
87 GOT_IMSG_TREE_REQUEST,
90 GOT_IMSG_BLOB_REQUEST,
94 /* Messages related to pack files. */
97 GOT_IMSG_PACKED_OBJECT_REQUEST,
99 /* Message sending file desciprtor to a temporary file. */
103 /* Structure for GOT_IMSG_ERROR. */
104 struct got_imsg_error {
105 int code; /* an error code from got_error.h */
106 int errno_code; /* in case code equals GOT_ERR_ERRNO */
110 * Structure for GOT_IMSG_TREE_REQUEST, GOT_IMSG_COMMIT_REQUEST,
111 * and GOT_IMSG_OBJECT data.
113 struct got_imsg_object {
114 uint8_t id[SHA1_DIGEST_LENGTH];
116 /* These fields are the same as in struct got_object. */
125 /* Structure for GOT_IMSG_COMMIT data. */
126 struct got_imsg_commit_object {
127 uint8_t tree_id[SHA1_DIGEST_LENGTH];
130 size_t committer_len;
131 struct tm tm_committer;
136 * Followed by author_len + committer_len data bytes
139 /* Followed by 'nparents' SHA1_DIGEST_LENGTH length strings */
142 * Followed by 'logmsg_len' bytes of commit log message data in
143 * one or more GOT_IMSG_COMMIT_LOGMSG messages.
145 } __attribute__((__packed__));
147 /* Structure for GOT_IMSG_MINI_COMMIT data. */
148 struct got_imsg_commit_object_mini {
149 uint8_t tree_id[SHA1_DIGEST_LENGTH];
150 struct tm tm_committer;
153 /* Followed by 'nparents' SHA1_DIGEST_LENGTH length strings */
154 } __attribute__((__packed__));
156 /* Structure for GOT_IMSG_TREE_ENTRY. */
157 struct got_imsg_tree_entry {
158 char id[SHA1_DIGEST_LENGTH];
160 /* Followed by entry's name in remaining data of imsg buffer. */
161 } __attribute__((__packed__));
163 /* Structure for GOT_IMSG_TREE_OBJECT_REPLY data. */
164 struct got_imsg_tree_object {
165 int nentries; /* This many TREE_ENTRY messages follow. */
168 /* Structure for GOT_IMSG_BLOB. */
169 struct got_imsg_blob {
173 /* Structure for GOT_IMSG_PACKIDX. */
174 struct got_imsg_packidx {
176 /* Additionally, a file desciptor is passed via imsg. */
179 /* Structure for GOT_IMSG_PACK. */
180 struct got_imsg_pack {
181 char path_packfile[PATH_MAX];
183 /* Additionally, a file desciptor is passed via imsg. */
187 * Structure for GOT_IMSG_PACKED_OBJECT_REQUEST data.
189 struct got_imsg_packed_object {
190 uint8_t id[SHA1_DIGEST_LENGTH];
197 const struct got_error *got_privsep_wait_for_child(pid_t);
198 const struct got_error *got_privsep_send_stop(int);
199 const struct got_error *got_privsep_recv_imsg(struct imsg *, struct imsgbuf *,
201 void got_privsep_send_error(struct imsgbuf *, const struct got_error *);
202 const struct got_error *got_privsep_send_obj_req(struct imsgbuf *, int,
203 struct got_object *);
204 const struct got_error *got_privsep_send_blob_req(struct imsgbuf *, int);
205 const struct got_error *got_privsep_send_blob_outfd(struct imsgbuf *, int);
206 const struct got_error *got_privsep_send_tmpfd(struct imsgbuf *, int);
207 const struct got_error *got_privsep_send_obj(struct imsgbuf *,
208 struct got_object *);
209 const struct got_error *got_privsep_get_imsg_obj(struct got_object **,
210 struct imsg *, struct imsgbuf *);
211 const struct got_error *got_privsep_recv_obj(struct got_object **,
213 const struct got_error *got_privsep_send_commit(struct imsgbuf *,
214 struct got_commit_object *);
215 const struct got_error *got_privsep_send_mini_commit(struct imsgbuf *,
216 struct got_mini_commit_object *);
217 const struct got_error *got_privsep_recv_commit(struct got_commit_object **,
219 const struct got_error *got_privsep_recv_mini_commit(
220 struct got_mini_commit_object **, struct imsgbuf *);
221 const struct got_error *got_privsep_recv_tree(struct got_tree_object **,
223 const struct got_error *got_privsep_send_tree(struct imsgbuf *,
224 struct got_tree_object *);
225 const struct got_error *got_privsep_send_blob(struct imsgbuf *, size_t);
226 const struct got_error *got_privsep_recv_blob(size_t *, struct imsgbuf *);
227 const struct got_error *got_privsep_init_pack_child(struct imsgbuf *,
228 struct got_pack *, struct got_packidx *);
229 const struct got_error *got_privsep_send_packed_obj_req(struct imsgbuf *, int,
230 struct got_object_id *);
231 const struct got_error *got_privsep_send_mini_commit_req(struct imsgbuf *, int,
232 struct got_object *);