Hash :
0e4002ca
Author :
Date :
2020-03-21T21:49:13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
/*
* Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
* Copyright (c) 2019, Ori Bernstein <ori@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* All code runs under the same UID but sensitive code paths are
* run in a separate process with tighter pledge(2) promises.
* Data is communicated between processes via imsg_flush(3) and imsg_read(3).
* This behaviour is transparent to users of the library.
*
* We generally transmit data in imsg buffers, split across several messages
* if necessary. File descriptors are used in cases where this is impractical,
* such as when accessing pack files or when transferring large blobs.
*
* We exec(2) after a fork(2). Parts of our library functionality are
* accessible via separate executables in a libexec directory.
*/
#define GOT_IMSG_FD_CHILD (STDERR_FILENO + 1)
#ifndef GOT_LIBEXECDIR
#define GOT_LIBEXECDIR /usr/libexec
#endif
/* Names of helper programs in libexec directory */
#define GOT_PROG_READ_OBJECT got-read-object
#define GOT_PROG_READ_TREE got-read-tree
#define GOT_PROG_READ_COMMIT got-read-commit
#define GOT_PROG_READ_BLOB got-read-blob
#define GOT_PROG_READ_TAG got-read-tag
#define GOT_PROG_READ_PACK got-read-pack
#define GOT_PROG_READ_GITCONFIG got-read-gitconfig
#define GOT_PROG_FETCH_PACK got-fetch-pack
#define GOT_PROG_INDEX_PACK got-index-pack
#define GOT_PROG_SEND_PACK got-send-pack
#define GOT_STRINGIFY(x) #x
#define GOT_STRINGVAL(x) GOT_STRINGIFY(x)
/* Paths to helper programs in libexec directory */
#define GOT_PATH_PROG_READ_OBJECT \
GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_OBJECT)
#define GOT_PATH_PROG_READ_TREE \
GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_TREE)
#define GOT_PATH_PROG_READ_COMMIT \
GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_COMMIT)
#define GOT_PATH_PROG_READ_BLOB \
GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_BLOB)
#define GOT_PATH_PROG_READ_TAG \
GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_TAG)
#define GOT_PATH_PROG_READ_PACK \
GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_PACK)
#define GOT_PATH_PROG_READ_GITCONFIG \
GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_GITCONFIG)
#define GOT_PATH_PROG_FETCH_PACK \
GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_FETCH_PACK)
#define GOT_PATH_PROG_SEND_PACK \
GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_SEND_PACK)
#define GOT_PATH_PROG_INDEX_PACK \
GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_INDEX_PACK)
struct got_privsep_child {
int imsg_fd;
pid_t pid;
struct imsgbuf *ibuf;
};
enum got_imsg_type {
/* An error occured while processing a request. */
GOT_IMSG_ERROR,
/* Stop the child process. */
GOT_IMSG_STOP,
/*
* Messages concerned with read access to objects in a repository.
* Object and pack files are opened by the main process, where
* data may be read as a byte string but without any interpretation.
* Decompression and parsing of object and pack files occurs in a
* separate process which runs under pledge("stdio recvfd").
* This sandboxes our own repository parsing code, as well as zlib.
*/
GOT_IMSG_OBJECT_REQUEST,
GOT_IMSG_OBJECT,
GOT_IMSG_COMMIT_REQUEST,
GOT_IMSG_COMMIT,
GOT_IMSG_COMMIT_LOGMSG,
GOT_IMSG_TREE_REQUEST,
GOT_IMSG_TREE,
GOT_IMSG_TREE_ENTRY,
GOT_IMSG_BLOB_REQUEST,
GOT_IMSG_BLOB_OUTFD,
GOT_IMSG_BLOB,
GOT_IMSG_TAG_REQUEST,
GOT_IMSG_TAG,
GOT_IMSG_TAG_TAGMSG,
/* Messages related to networking. */
GOT_IMSG_FETCH_REQUEST,
GOT_IMSG_FETCH_HAVE_REF,
GOT_IMSG_FETCH_WANTED_BRANCH,
GOT_IMSG_FETCH_WANTED_REF,
GOT_IMSG_FETCH_OUTFD,
GOT_IMSG_FETCH_SYMREFS,
GOT_IMSG_FETCH_REF,
GOT_IMSG_FETCH_SERVER_PROGRESS,
GOT_IMSG_FETCH_DOWNLOAD_PROGRESS,
GOT_IMSG_FETCH_DONE,
GOT_IMSG_IDXPACK_REQUEST,
GOT_IMSG_IDXPACK_OUTFD,
GOT_IMSG_IDXPACK_PROGRESS,
GOT_IMSG_IDXPACK_DONE,
/* Messages related to pack files. */
GOT_IMSG_PACKIDX,
GOT_IMSG_PACK,
GOT_IMSG_PACKED_OBJECT_REQUEST,
GOT_IMSG_COMMIT_TRAVERSAL_REQUEST,
GOT_IMSG_TRAVERSED_COMMITS,
GOT_IMSG_COMMIT_TRAVERSAL_DONE,
/* Message sending file descriptor to a temporary file. */
GOT_IMSG_TMPFD,
/* Messages related to gitconfig files. */
GOT_IMSG_GITCONFIG_PARSE_REQUEST,
GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST,
GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST,
GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST,
GOT_IMSG_GITCONFIG_REMOTES_REQUEST,
GOT_IMSG_GITCONFIG_INT_VAL,
GOT_IMSG_GITCONFIG_STR_VAL,
GOT_IMSG_GITCONFIG_REMOTES,
GOT_IMSG_GITCONFIG_REMOTE,
GOT_IMSG_GITCONFIG_OWNER_REQUEST,
GOT_IMSG_GITCONFIG_OWNER,
};
/* Structure for GOT_IMSG_ERROR. */
struct got_imsg_error {
int code; /* an error code from got_error.h */
int errno_code; /* in case code equals GOT_ERR_ERRNO */
} __attribute__((__packed__));
/*
* Structure for GOT_IMSG_TREE_REQUEST and GOT_IMSG_OBJECT data.
*/
struct got_imsg_object {
uint8_t id[SHA1_DIGEST_LENGTH];
/* These fields are the same as in struct got_object. */
int type;
int flags;
size_t hdrlen;
size_t size;
off_t pack_offset;
int pack_idx;
} __attribute__((__packed__));
/* Structure for GOT_IMSG_COMMIT data. */
struct got_imsg_commit_object {
uint8_t tree_id[SHA1_DIGEST_LENGTH];
size_t author_len;
time_t author_time;
time_t author_gmtoff;
size_t committer_len;
time_t committer_time;
time_t committer_gmtoff;
size_t logmsg_len;
int nparents;
/*
* Followed by author_len + committer_len data bytes
*/
/* Followed by 'nparents' SHA1_DIGEST_LENGTH length strings */
/*
* Followed by 'logmsg_len' bytes of commit log message data in
* one or more GOT_IMSG_COMMIT_LOGMSG messages.
*/
} __attribute__((__packed__));
/* Structure for GOT_IMSG_TREE_ENTRY. */
struct got_imsg_tree_entry {
char id[SHA1_DIGEST_LENGTH];
mode_t mode;
/* Followed by entry's name in remaining data of imsg buffer. */
} __attribute__((__packed__));
/* Structure for GOT_IMSG_TREE_OBJECT_REPLY data. */
struct got_imsg_tree_object {
int nentries; /* This many TREE_ENTRY messages follow. */
};
/* Structure for GOT_IMSG_BLOB. */
struct got_imsg_blob {
size_t size;
size_t hdrlen;
/*
* If size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX, blob data follows
* in the imsg buffer. Otherwise, blob data has been written to a
* file descriptor passed via the GOT_IMSG_BLOB_OUTFD imsg.
*/
#define GOT_PRIVSEP_INLINE_BLOB_DATA_MAX \
(MAX_IMSGSIZE - IMSG_HEADER_SIZE - sizeof(struct got_imsg_blob))
};
/* Structure for GOT_IMSG_TAG data. */
struct got_imsg_tag_object {
uint8_t id[SHA1_DIGEST_LENGTH];
int obj_type;
size_t tag_len;
size_t tagger_len;
time_t tagger_time;
time_t tagger_gmtoff;
size_t tagmsg_len;
/*
* Followed by tag_len + tagger_len data bytes
*/
/*
* Followed by 'tagmsg_len' bytes of tag message data in
* one or more GOT_IMSG_TAG_TAGMSG messages.
*/
} __attribute__((__packed__));
/* Structure for GOT_IMSG_FETCH_HAVE_REF data. */
struct got_imsg_fetch_have_ref {
uint8_t id[SHA1_DIGEST_LENGTH];
size_t name_len;
/* Followed by name_len data bytes. */
} __attribute__((__packed__));
/* Structure for GOT_IMSG_FETCH_WANTED_BRANCH data. */
struct got_imsg_fetch_wanted_branch {
size_t name_len;
/* Followed by name_len data bytes. */
} __attribute__((__packed__));
/* Structure for GOT_IMSG_FETCH_WANTED_REF data. */
struct got_imsg_fetch_wanted_ref {
size_t name_len;
/* Followed by name_len data bytes. */
} __attribute__((__packed__));
/* Structure for GOT_IMSG_FETCH_REQUEST data. */
struct got_imsg_fetch_request {
int fetch_all_branches;
int list_refs_only;
int verbosity;
size_t n_have_refs;
size_t n_wanted_branches;
size_t n_wanted_refs;
/* Followed by n_have_refs GOT_IMSG_FETCH_HAVE_REF messages. */
/* Followed by n_wanted_branches times GOT_IMSG_FETCH_WANTED_BRANCH. */
/* Followed by n_wanted_refs times GOT_IMSG_FETCH_WANTED_REF. */
} __attribute__((__packed__));
/* Structures for GOT_IMSG_FETCH_SYMREFS data. */
struct got_imsg_fetch_symref {
size_t name_len;
size_t target_len;
/*
* Followed by name_len + target_len data bytes.
*/
} __attribute__((__packed__));
struct got_imsg_fetch_symrefs {
size_t nsymrefs;
/* Followed by nsymrefs times of got_imsg_fetch_symref data. */
} __attribute__((__packed__));
/* Structure for GOT_IMSG_FETCH_REF data. */
struct got_imsg_fetch_ref {
/* Describes a reference which will be fetched. */
uint8_t refid[SHA1_DIGEST_LENGTH];
/* Followed by reference name in remaining data of imsg buffer. */
};
/* Structure for GOT_IMSG_FETCH_DOWNLOAD_PROGRESS data. */
struct got_imsg_fetch_download_progress {
/* Number of packfile data bytes downloaded so far. */
off_t packfile_bytes;
};
/* Structure for GOT_IMSG_IDXPACK_REQUEST data. */
struct got_imsg_index_pack_request {
uint8_t pack_hash[SHA1_DIGEST_LENGTH];
} __attribute__((__packed__));
/* Structure for GOT_IMSG_IDXPACK_PROGRESS data. */
struct got_imsg_index_pack_progress {
/* Total number of objects in pack file. */
int nobj_total;
/* Number of objects indexed so far. */
int nobj_indexed;
/* Number of non-deltified objects in pack file. */
int nobj_loose;
/* Number of deltified objects resolved so far. */
int nobj_resolved;
};
/* Structure for GOT_IMSG_PACKIDX. */
struct got_imsg_packidx {
size_t len;
/* Additionally, a file desciptor is passed via imsg. */
};
/* Structure for GOT_IMSG_PACK. */
struct got_imsg_pack {
char path_packfile[PATH_MAX];
size_t filesize;
/* Additionally, a file desciptor is passed via imsg. */
} __attribute__((__packed__));
/*
* Structure for GOT_IMSG_PACKED_OBJECT_REQUEST data.
*/
struct got_imsg_packed_object {
uint8_t id[SHA1_DIGEST_LENGTH];
int idx;
} __attribute__((__packed__));
/* Structure for GOT_IMSG_COMMIT_TRAVERSAL_REQUEST */
struct got_imsg_commit_traversal_request {
uint8_t id[SHA1_DIGEST_LENGTH];
int idx;
size_t path_len;
/* Followed by path_len bytes of path data */
} __attribute__((__packed__));
/* Structure for GOT_IMSG_TRAVERSED_COMMITS */
struct got_imsg_traversed_commits {
size_t ncommits;
/* Followed by ncommit IDs of SHA1_DIGEST_LENGTH each */
} __attribute__((__packed__));
/*
* Structure for GOT_IMSG_GITCONFIG_REMOTE data.
*/
struct got_imsg_remote {
size_t name_len;
size_t url_len;
int mirror_references;
/* Followed by name_len + url_len data bytes. */
} __attribute__((__packed__));
/*
* Structure for GOT_IMSG_GITCONFIG_REMOTES data.
*/
struct got_imsg_remotes {
int nremotes; /* This many GOT_IMSG_GITCONFIG_REMOTE messages follow. */
};
struct got_remote_repo;
struct got_pack;
struct got_packidx;
struct got_pathlist_head;
const struct got_error *got_send_ack(pid_t);
const struct got_error *got_privsep_wait_for_child(pid_t);
const struct got_error *got_privsep_send_stop(int);
const struct got_error *got_privsep_recv_imsg(struct imsg *, struct imsgbuf *,
size_t);
void got_privsep_send_error(struct imsgbuf *, const struct got_error *);
const struct got_error *got_privsep_send_ack(struct imsgbuf *);
const struct got_error *got_privsep_wait_ack(struct imsgbuf *);
const struct got_error *got_privsep_send_obj_req(struct imsgbuf *, int);
const struct got_error *got_privsep_send_commit_req(struct imsgbuf *, int,
struct got_object_id *, int);
const struct got_error *got_privsep_send_tree_req(struct imsgbuf *, int,
struct got_object_id *, int);
const struct got_error *got_privsep_send_tag_req(struct imsgbuf *, int,
struct got_object_id *, int);
const struct got_error *got_privsep_send_blob_req(struct imsgbuf *, int,
struct got_object_id *, int);
const struct got_error *got_privsep_send_blob_outfd(struct imsgbuf *, int);
const struct got_error *got_privsep_send_tmpfd(struct imsgbuf *, int);
const struct got_error *got_privsep_send_obj(struct imsgbuf *,
struct got_object *);
const struct got_error *got_privsep_send_index_pack_req(struct imsgbuf *,
uint8_t *, int);
const struct got_error *got_privsep_send_index_pack_outfd(struct imsgbuf *,
int);
const struct got_error *got_privsep_send_index_pack_progress(struct imsgbuf *,
int, int, int, int);
const struct got_error *got_privsep_send_index_pack_done(struct imsgbuf *);
const struct got_error *got_privsep_recv_index_progress(int *, int *, int *,
int *, int *, struct imsgbuf *ibuf);
const struct got_error *got_privsep_send_fetch_req(struct imsgbuf *, int,
struct got_pathlist_head *, int, struct got_pathlist_head *,
struct got_pathlist_head *, int, int);
const struct got_error *got_privsep_send_fetch_outfd(struct imsgbuf *, int);
const struct got_error *got_privsep_send_fetch_symrefs(struct imsgbuf *,
struct got_pathlist_head *);
const struct got_error *got_privsep_send_fetch_ref(struct imsgbuf *,
struct got_object_id *, const char *);
const struct got_error *got_privsep_send_fetch_server_progress(struct imsgbuf *,
const char *, size_t);
const struct got_error *got_privsep_send_fetch_download_progress(struct imsgbuf *,
off_t);
const struct got_error *got_privsep_recv_fetch_progress(int *,
struct got_object_id **, char **, struct got_pathlist_head *,
char **, off_t *, struct imsgbuf *);
const struct got_error *got_privsep_send_fetch_done(struct imsgbuf *,
struct got_object_id);
const struct got_error *got_privsep_get_imsg_obj(struct got_object **,
struct imsg *, struct imsgbuf *);
const struct got_error *got_privsep_recv_obj(struct got_object **,
struct imsgbuf *);
const struct got_error *got_privsep_send_commit(struct imsgbuf *,
struct got_commit_object *);
const struct got_error *got_privsep_recv_commit(struct got_commit_object **,
struct imsgbuf *);
const struct got_error *got_privsep_recv_tree(struct got_tree_object **,
struct imsgbuf *);
const struct got_error *got_privsep_send_tree(struct imsgbuf *,
struct got_pathlist_head *, int);
const struct got_error *got_privsep_send_blob(struct imsgbuf *, size_t, size_t,
const uint8_t *);
const struct got_error *got_privsep_recv_blob(uint8_t **, size_t *, size_t *,
struct imsgbuf *);
const struct got_error *got_privsep_send_tag(struct imsgbuf *,
struct got_tag_object *);
const struct got_error *got_privsep_recv_tag(struct got_tag_object **,
struct imsgbuf *);
const struct got_error *got_privsep_init_pack_child(struct imsgbuf *,
struct got_pack *, struct got_packidx *);
const struct got_error *got_privsep_send_packed_obj_req(struct imsgbuf *, int,
struct got_object_id *);
const struct got_error *got_privsep_send_pack_child_ready(struct imsgbuf *);
const struct got_error *got_privsep_send_gitconfig_parse_req(struct imsgbuf *,
int);
const struct got_error *
got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *);
const struct got_error *got_privsep_send_gitconfig_author_name_req(
struct imsgbuf *);
const struct got_error *got_privsep_send_gitconfig_author_email_req(
struct imsgbuf *);
const struct got_error *got_privsep_send_gitconfig_remotes_req(
struct imsgbuf *);
const struct got_error *got_privsep_send_gitconfig_owner_req(struct imsgbuf *);
const struct got_error *got_privsep_send_gitconfig_str(struct imsgbuf *,
const char *);
const struct got_error *got_privsep_recv_gitconfig_str(char **,
struct imsgbuf *);
const struct got_error *got_privsep_send_gitconfig_int(struct imsgbuf *, int);
const struct got_error *got_privsep_recv_gitconfig_int(int *, struct imsgbuf *);
const struct got_error *got_privsep_send_gitconfig_remotes(struct imsgbuf *,
struct got_remote_repo *, int);
const struct got_error *got_privsep_recv_gitconfig_remotes(
struct got_remote_repo **, int *, struct imsgbuf *);
const struct got_error *got_privsep_send_commit_traversal_request(
struct imsgbuf *, struct got_object_id *, int, const char *);
const struct got_error *got_privsep_recv_traversed_commits(
struct got_commit_object **, struct got_object_id **,
struct got_object_id_queue *, struct imsgbuf *);
const struct got_error *got_privsep_send_traversed_commits(
struct got_object_id *, size_t, struct imsgbuf *);
const struct got_error *got_privsep_send_commit_traversal_done(
struct imsgbuf *);
void got_privsep_exec_child(int[2], const char *, const char *);