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,
106 /* Message sending file descriptor to a temporary file. */
107 GOT_IMSG_TMPFD,
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_INT_VAL,
115 GOT_IMSG_GITCONFIG_STR_VAL,
116 };
118 /* Structure for GOT_IMSG_ERROR. */
119 struct got_imsg_error {
120 int code; /* an error code from got_error.h */
121 int errno_code; /* in case code equals GOT_ERR_ERRNO */
122 } __attribute__((__packed__));
124 /*
125 * Structure for GOT_IMSG_TREE_REQUEST and GOT_IMSG_OBJECT data.
126 */
127 struct got_imsg_object {
128 uint8_t id[SHA1_DIGEST_LENGTH];
130 /* These fields are the same as in struct got_object. */
131 int type;
132 int flags;
133 size_t hdrlen;
134 size_t size;
135 off_t pack_offset;
136 int pack_idx;
137 } __attribute__((__packed__));
139 /* Structure for GOT_IMSG_COMMIT data. */
140 struct got_imsg_commit_object {
141 uint8_t tree_id[SHA1_DIGEST_LENGTH];
142 size_t author_len;
143 time_t author_time;
144 time_t author_gmtoff;
145 size_t committer_len;
146 time_t committer_time;
147 time_t committer_gmtoff;
148 size_t logmsg_len;
149 int nparents;
151 /*
152 * Followed by author_len + committer_len data bytes
153 */
155 /* Followed by 'nparents' SHA1_DIGEST_LENGTH length strings */
157 /*
158 * Followed by 'logmsg_len' bytes of commit log message data in
159 * one or more GOT_IMSG_COMMIT_LOGMSG messages.
160 */
161 } __attribute__((__packed__));
164 /* Structure for GOT_IMSG_TREE_ENTRY. */
165 struct got_imsg_tree_entry {
166 char id[SHA1_DIGEST_LENGTH];
167 mode_t mode;
168 /* Followed by entry's name in remaining data of imsg buffer. */
169 } __attribute__((__packed__));
171 /* Structure for GOT_IMSG_TREE_OBJECT_REPLY data. */
172 struct got_imsg_tree_object {
173 int nentries; /* This many TREE_ENTRY messages follow. */
174 };
176 /* Structure for GOT_IMSG_BLOB. */
177 struct got_imsg_blob {
178 size_t size;
179 size_t hdrlen;
181 /*
182 * If size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX, blob data follows
183 * in the imsg buffer. Otherwise, blob data has been written to a
184 * file descriptor passed via the GOT_IMSG_BLOB_OUTFD imsg.
185 */
186 #define GOT_PRIVSEP_INLINE_BLOB_DATA_MAX \
187 (MAX_IMSGSIZE - IMSG_HEADER_SIZE - sizeof(struct got_imsg_blob))
188 };
191 /* Structure for GOT_IMSG_TAG data. */
192 struct got_imsg_tag_object {
193 uint8_t id[SHA1_DIGEST_LENGTH];
194 int obj_type;
195 size_t tag_len;
196 size_t tagger_len;
197 time_t tagger_time;
198 time_t tagger_gmtoff;
199 size_t tagmsg_len;
201 /*
202 * Followed by tag_len + tagger_len data bytes
203 */
205 /*
206 * Followed by 'tagmsg_len' bytes of tag message data in
207 * one or more GOT_IMSG_TAG_TAGMSG messages.
208 */
209 } __attribute__((__packed__));
211 /* Structure for GOT_IMSG_PACKIDX. */
212 struct got_imsg_packidx {
213 size_t len;
214 /* Additionally, a file desciptor is passed via imsg. */
215 };
217 /* Structure for GOT_IMSG_PACK. */
218 struct got_imsg_pack {
219 char path_packfile[PATH_MAX];
220 size_t filesize;
221 /* Additionally, a file desciptor is passed via imsg. */
222 } __attribute__((__packed__));
224 /*
225 * Structure for GOT_IMSG_PACKED_OBJECT_REQUEST data.
226 */
227 struct got_imsg_packed_object {
228 uint8_t id[SHA1_DIGEST_LENGTH];
229 int idx;
230 } __attribute__((__packed__));
232 struct got_pack;
233 struct got_packidx;
234 struct got_pathlist_head;
236 const struct got_error *got_privsep_wait_for_child(pid_t);
237 const struct got_error *got_privsep_send_stop(int);
238 const struct got_error *got_privsep_recv_imsg(struct imsg *, struct imsgbuf *,
239 size_t);
240 void got_privsep_send_error(struct imsgbuf *, const struct got_error *);
241 const struct got_error *got_privsep_send_obj_req(struct imsgbuf *, int);
242 const struct got_error *got_privsep_send_commit_req(struct imsgbuf *, int,
243 struct got_object_id *, int);
244 const struct got_error *got_privsep_send_tree_req(struct imsgbuf *, int,
245 struct got_object_id *, int);
246 const struct got_error *got_privsep_send_tag_req(struct imsgbuf *, int,
247 struct got_object_id *, int);
248 const struct got_error *got_privsep_send_blob_req(struct imsgbuf *, int,
249 struct got_object_id *, int);
250 const struct got_error *got_privsep_send_blob_outfd(struct imsgbuf *, int);
251 const struct got_error *got_privsep_send_tmpfd(struct imsgbuf *, int);
252 const struct got_error *got_privsep_send_obj(struct imsgbuf *,
253 struct got_object *);
254 const struct got_error *got_privsep_get_imsg_obj(struct got_object **,
255 struct imsg *, struct imsgbuf *);
256 const struct got_error *got_privsep_recv_obj(struct got_object **,
257 struct imsgbuf *);
258 const struct got_error *got_privsep_send_commit(struct imsgbuf *,
259 struct got_commit_object *);
260 const struct got_error *got_privsep_recv_commit(struct got_commit_object **,
261 struct imsgbuf *);
262 const struct got_error *got_privsep_recv_tree(struct got_tree_object **,
263 struct imsgbuf *);
264 const struct got_error *got_privsep_send_tree(struct imsgbuf *,
265 struct got_pathlist_head *, int);
266 const struct got_error *got_privsep_send_blob(struct imsgbuf *, size_t, size_t,
267 const uint8_t *);
268 const struct got_error *got_privsep_recv_blob(uint8_t **, size_t *, size_t *,
269 struct imsgbuf *);
270 const struct got_error *got_privsep_send_tag(struct imsgbuf *,
271 struct got_tag_object *);
272 const struct got_error *got_privsep_recv_tag(struct got_tag_object **,
273 struct imsgbuf *);
274 const struct got_error *got_privsep_init_pack_child(struct imsgbuf *,
275 struct got_pack *, struct got_packidx *);
276 const struct got_error *got_privsep_send_packed_obj_req(struct imsgbuf *, int,
277 struct got_object_id *);
278 const struct got_error *got_privsep_send_pack_child_ready(struct imsgbuf *);
280 const struct got_error *got_privsep_send_gitconfig_parse_req(struct imsgbuf *,
281 int);
282 const struct got_error *
283 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *);
284 const struct got_error *got_privsep_send_gitconfig_author_name_req(
285 struct imsgbuf *);
286 const struct got_error *got_privsep_send_gitconfig_author_email_req(
287 struct imsgbuf *);
288 const struct got_error *got_privsep_send_gitconfig_str(struct imsgbuf *,
289 const char *);
290 const struct got_error *got_privsep_recv_gitconfig_str(char **,
291 struct imsgbuf *);
292 const struct got_error *got_privsep_send_gitconfig_int(struct imsgbuf *, int);
293 const struct got_error *got_privsep_recv_gitconfig_int(int *, struct imsgbuf *);
295 void got_privsep_exec_child(int[2], const char *, const char *);