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_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 {
60 int imsg_fd;
61 pid_t pid;
62 struct imsgbuf *ibuf;
63 };
65 enum got_imsg_type {
66 /* An error occured while processing a request. */
67 GOT_IMSG_ERROR,
69 /* Stop the child process. */
70 GOT_IMSG_STOP,
72 /*
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.
79 */
80 GOT_IMSG_OBJECT_REQUEST,
81 GOT_IMSG_OBJECT,
82 GOT_IMSG_COMMIT_REQUEST,
83 GOT_IMSG_COMMIT,
84 GOT_IMSG_TREE_REQUEST,
85 GOT_IMSG_TREE,
86 GOT_IMSG_TREE_ENTRY,
87 GOT_IMSG_BLOB_REQUEST,
88 GOT_IMSG_BLOB_OUTFD,
89 GOT_IMSG_BLOB,
91 /* Messages related to pack files. */
92 GOT_IMSG_PACKIDX,
93 GOT_IMSG_PACK,
94 GOT_IMSG_PACKED_OBJECT_REQUEST,
96 /* Messages for transmitting deltas and associated delta streams: */
97 GOT_IMSG_DELTA,
98 GOT_IMSG_DELTA_STREAM,
99 };
101 /* Structure for GOT_IMSG_ERROR. */
102 struct got_imsg_error {
103 int code; /* an error code from got_error.h */
104 int errno_code; /* in case code equals GOT_ERR_ERRNO */
105 };
107 /* Structure for GOT_IMSG_DELTA data. */
108 struct got_imsg_delta {
109 /* These fields are the same as in struct got_delta. */
110 off_t offset;
111 size_t tslen;
112 int type;
113 size_t size;
114 off_t data_offset;
115 size_t delta_len;
117 /*
118 * Followed by one or more DELTA_STREAM messages until delta_len
119 * bytes of delta stream have been transmitted.
120 */
121 };
123 /* Structure for GOT_IMSG_DELTA_STREAM data. */
124 struct got_imsg_delta_stream {
125 /*
126 * Empty since the following is implied:
127 * Read additional delta stream data from imsg buffer.
128 */
129 };
131 /*
132 * Structure for GOT_IMSG_TREE_REQUEST, GOT_IMSG_COMMIT_REQUEST,
133 * and GOT_IMSG_OBJECT data.
134 */
135 struct got_imsg_object {
136 /* These fields are the same as in struct got_object. */
137 int type;
138 int flags;
139 size_t hdrlen;
140 size_t size;
142 off_t pack_offset;
143 int ndeltas; /* this many GOT_IMSG_DELTA messages follow */
144 };
146 /* Structure for GOT_IMSG_COMMIT data. */
147 struct got_imsg_commit_object {
148 uint8_t tree_id[SHA1_DIGEST_LENGTH];
149 size_t author_len;
150 struct tm tm_author;
151 size_t committer_len;
152 struct tm tm_committer;
153 size_t logmsg_len;
154 int nparents;
156 /*
157 * Followed by author_len + committer_len + logmsg_len data bytes
158 */
160 /* Followed by 'nparents' SHA1_DIGEST_LENGTH length strings */
162 /* XXX should use more messages to support very large log messages */
163 } __attribute__((__packed__));
166 /* Structure for GOT_IMSG_TREE_ENTRY. */
167 struct got_imsg_tree_entry {
168 char id[SHA1_DIGEST_LENGTH];
169 mode_t mode;
170 /* Followed by entry's name in remaining data of imsg buffer. */
171 } __attribute__((__packed__));
173 /* Structure for GOT_IMSG_TREE_OBJECT_REPLY data. */
174 struct got_imsg_tree_object {
175 int nentries; /* This many TREE_ENTRY messages follow. */
176 };
178 /* Structure for GOT_IMSG_BLOB. */
179 struct got_imsg_blob {
180 size_t size;
181 };
183 /* Structure for GOT_IMSG_PACKIDX. */
184 struct got_imsg_packidx {
185 size_t len;
186 /* Additionally, a file desciptor is passed via imsg. */
187 };
189 /* Structure for GOT_IMSG_PACK. */
190 struct got_imsg_pack {
191 char path_packfile[PATH_MAX];
192 size_t filesize;
193 /* Additionally, a file desciptor is passed via imsg. */
194 };
196 /*
197 * Structure for GOT_IMSG_PACKED_OBJECT_REQUEST data.
198 */
199 struct got_imsg_packed_object {
200 int idx;
201 };
203 struct got_pack;
204 struct got_packidx;
206 const struct got_error *got_privsep_wait_for_child(pid_t);
207 const struct got_error *got_privsep_send_stop(int);
208 const struct got_error *got_privsep_recv_imsg(struct imsg *, struct imsgbuf *,
209 size_t);
210 void got_privsep_send_error(struct imsgbuf *, const struct got_error *);
211 const struct got_error *got_privsep_send_obj_req(struct imsgbuf *, int,
212 struct got_object *);
213 const struct got_error *got_privsep_send_blob_req(struct imsgbuf *, int, int);
214 const struct got_error *got_privsep_send_obj(struct imsgbuf *,
215 struct got_object *);
216 const struct got_error *got_privsep_recv_obj(struct got_object **,
217 struct imsgbuf *);
218 const struct got_error *got_privsep_send_commit(struct imsgbuf *,
219 struct got_commit_object *);
220 const struct got_error *got_privsep_recv_commit(struct got_commit_object **,
221 struct imsgbuf *);
222 const struct got_error *got_privsep_recv_tree(struct got_tree_object **,
223 struct imsgbuf *);
224 const struct got_error *got_privsep_send_tree(struct imsgbuf *,
225 struct got_tree_object *);
226 const struct got_error *got_privsep_send_blob(struct imsgbuf *, size_t);
227 const struct got_error *got_privsep_recv_blob(size_t *, struct imsgbuf *);
228 const struct got_error *got_privsep_init_pack_child(struct imsgbuf *,
229 struct got_pack *, struct got_packidx *);
230 const struct got_error *got_privsep_send_packed_obj_req(struct imsgbuf *, int);
231 const struct got_error *got_privsep_send_pack_child_ready(struct imsgbuf *);