Blob


1 /*
2 * Copyright (c) 2018 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
45 #define GOT_STRINGIFY(x) #x
46 #define GOT_STRINGVAL(x) GOT_STRINGIFY(x)
48 /* Paths to helper programs in libexec directory */
49 #define GOT_PATH_PROG_READ_OBJECT \
50 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_OBJECT)
51 #define GOT_PATH_PROG_READ_TREE \
52 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_TREE)
53 #define GOT_PATH_PROG_READ_COMMIT \
54 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_COMMIT)
55 #define GOT_PATH_PROG_READ_BLOB \
56 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_BLOB)
57 #define GOT_PATH_PROG_READ_TAG \
58 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_TAG)
59 #define GOT_PATH_PROG_READ_PACK \
60 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_PACK)
62 struct got_privsep_child {
63 int imsg_fd;
64 pid_t pid;
65 struct imsgbuf *ibuf;
66 };
68 enum got_imsg_type {
69 /* An error occured while processing a request. */
70 GOT_IMSG_ERROR,
72 /* Stop the child process. */
73 GOT_IMSG_STOP,
75 /*
76 * Messages concerned with read access to objects in a repository.
77 * Object and pack files are opened by the main process, where
78 * data may be read as a byte string but without any interpretation.
79 * Decompression and parsing of object and pack files occurs in a
80 * separate process which runs under pledge("stdio recvfd").
81 * This sandboxes our own repository parsing code, as well as zlib.
82 */
83 GOT_IMSG_OBJECT_REQUEST,
84 GOT_IMSG_OBJECT,
85 GOT_IMSG_COMMIT_REQUEST,
86 GOT_IMSG_COMMIT,
87 GOT_IMSG_COMMIT_LOGMSG,
88 GOT_IMSG_TREE_REQUEST,
89 GOT_IMSG_TREE,
90 GOT_IMSG_TREE_ENTRY,
91 GOT_IMSG_BLOB_REQUEST,
92 GOT_IMSG_BLOB_OUTFD,
93 GOT_IMSG_BLOB,
94 GOT_IMSG_TAG_REQUEST,
95 GOT_IMSG_TAG,
96 GOT_IMSG_TAG_TAGMSG,
98 /* Messages related to pack files. */
99 GOT_IMSG_PACKIDX,
100 GOT_IMSG_PACK,
101 GOT_IMSG_PACKED_OBJECT_REQUEST,
103 /* Message sending file desciprtor to a temporary file. */
104 GOT_IMSG_TMPFD,
105 };
107 /* Structure for GOT_IMSG_ERROR. */
108 struct got_imsg_error {
109 int code; /* an error code from got_error.h */
110 int errno_code; /* in case code equals GOT_ERR_ERRNO */
111 } __attribute__((__packed__));
113 /*
114 * Structure for GOT_IMSG_TREE_REQUEST, GOT_IMSG_COMMIT_REQUEST,
115 * and GOT_IMSG_OBJECT data.
116 */
117 struct got_imsg_object {
118 uint8_t id[SHA1_DIGEST_LENGTH];
120 /* These fields are the same as in struct got_object. */
121 int type;
122 int flags;
123 size_t hdrlen;
124 size_t size;
125 off_t pack_offset;
126 int pack_idx;
127 } __attribute__((__packed__));
129 /* Structure for GOT_IMSG_COMMIT data. */
130 struct got_imsg_commit_object {
131 uint8_t tree_id[SHA1_DIGEST_LENGTH];
132 size_t author_len;
133 time_t author_time;
134 time_t author_gmtoff;
135 size_t committer_len;
136 time_t committer_time;
137 time_t committer_gmtoff;
138 size_t logmsg_len;
139 int nparents;
141 /*
142 * Followed by author_len + committer_len data bytes
143 */
145 /* Followed by 'nparents' SHA1_DIGEST_LENGTH length strings */
147 /*
148 * Followed by 'logmsg_len' bytes of commit log message data in
149 * one or more GOT_IMSG_COMMIT_LOGMSG messages.
150 */
151 } __attribute__((__packed__));
154 /* Structure for GOT_IMSG_TREE_ENTRY. */
155 struct got_imsg_tree_entry {
156 char id[SHA1_DIGEST_LENGTH];
157 mode_t mode;
158 /* Followed by entry's name in remaining data of imsg buffer. */
159 } __attribute__((__packed__));
161 /* Structure for GOT_IMSG_TREE_OBJECT_REPLY data. */
162 struct got_imsg_tree_object {
163 int nentries; /* This many TREE_ENTRY messages follow. */
164 };
166 /* Structure for GOT_IMSG_BLOB. */
167 struct got_imsg_blob {
168 size_t size;
169 };
171 /* Structure for GOT_IMSG_TAG data. */
172 struct got_imsg_tag_object {
173 uint8_t id[SHA1_DIGEST_LENGTH];
174 int obj_type;
175 size_t tag_len;
176 size_t tagger_len;
177 time_t tagger_time;
178 time_t tagger_gmtoff;
179 size_t tagmsg_len;
181 /*
182 * Followed by tag_len + tagger_len data bytes
183 */
185 /*
186 * Followed by 'tagmsg_len' bytes of tag message data in
187 * one or more GOT_IMSG_TAG_TAGMSG messages.
188 */
189 } __attribute__((__packed__));
191 /* Structure for GOT_IMSG_PACKIDX. */
192 struct got_imsg_packidx {
193 size_t len;
194 /* Additionally, a file desciptor is passed via imsg. */
195 };
197 /* Structure for GOT_IMSG_PACK. */
198 struct got_imsg_pack {
199 char path_packfile[PATH_MAX];
200 size_t filesize;
201 /* Additionally, a file desciptor is passed via imsg. */
202 } __attribute__((__packed__));
204 /*
205 * Structure for GOT_IMSG_PACKED_OBJECT_REQUEST data.
206 */
207 struct got_imsg_packed_object {
208 uint8_t id[SHA1_DIGEST_LENGTH];
209 int idx;
210 } __attribute__((__packed__));
212 struct got_pack;
213 struct got_packidx;
215 const struct got_error *got_privsep_wait_for_child(pid_t);
216 const struct got_error *got_privsep_send_stop(int);
217 const struct got_error *got_privsep_recv_imsg(struct imsg *, struct imsgbuf *,
218 size_t);
219 void got_privsep_send_error(struct imsgbuf *, const struct got_error *);
220 const struct got_error *got_privsep_send_obj_req(struct imsgbuf *, int,
221 struct got_object *);
222 const struct got_error *got_privsep_send_blob_req(struct imsgbuf *, int);
223 const struct got_error *got_privsep_send_blob_outfd(struct imsgbuf *, int);
224 const struct got_error *got_privsep_send_tmpfd(struct imsgbuf *, int);
225 const struct got_error *got_privsep_send_obj(struct imsgbuf *,
226 struct got_object *);
227 const struct got_error *got_privsep_get_imsg_obj(struct got_object **,
228 struct imsg *, struct imsgbuf *);
229 const struct got_error *got_privsep_recv_obj(struct got_object **,
230 struct imsgbuf *);
231 const struct got_error *got_privsep_send_commit(struct imsgbuf *,
232 struct got_commit_object *);
233 const struct got_error *got_privsep_recv_commit(struct got_commit_object **,
234 struct imsgbuf *);
235 const struct got_error *got_privsep_recv_tree(struct got_tree_object **,
236 struct imsgbuf *);
237 const struct got_error *got_privsep_send_tree(struct imsgbuf *,
238 struct got_tree_object *);
239 const struct got_error *got_privsep_send_blob(struct imsgbuf *, size_t);
240 const struct got_error *got_privsep_recv_blob(size_t *, struct imsgbuf *);
241 const struct got_error *got_privsep_send_tag(struct imsgbuf *,
242 struct got_tag_object *);
243 const struct got_error *got_privsep_recv_tag(struct got_tag_object **,
244 struct imsgbuf *);
245 const struct got_error *got_privsep_init_pack_child(struct imsgbuf *,
246 struct got_pack *, struct got_packidx *);
247 const struct got_error *got_privsep_send_packed_obj_req(struct imsgbuf *, int,
248 struct got_object_id *);
249 const struct got_error *got_privsep_send_pack_child_ready(struct imsgbuf *);