2 * Copyright (c) 2018, 2019 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.
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <sys/syslimits.h>
34 #include "got_object.h"
35 #include "got_error.h"
37 #include "got_lib_sha1.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_inflate.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_parse.h"
42 #include "got_lib_privsep.h"
43 #include "got_lib_pack.h"
46 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 static const struct got_error *
54 poll_fd(int fd, int events, int timeout)
60 pfd[0].events = events;
62 n = poll(pfd, 1, timeout);
64 return got_error_from_errno("poll");
66 return got_error(GOT_ERR_TIMEOUT);
67 if (pfd[0].revents & (POLLERR | POLLNVAL))
68 return got_error_from_errno("poll error");
69 if (pfd[0].revents & (events | POLLHUP))
72 return got_error(GOT_ERR_INTERRUPT);
75 static const struct got_error *
76 read_imsg(struct imsgbuf *ibuf)
78 const struct got_error *err;
81 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
87 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
88 return got_error(GOT_ERR_PRIVSEP_NO_FD);
89 return got_error(GOT_ERR_PRIVSEP_READ);
92 return got_error(GOT_ERR_PRIVSEP_PIPE);
97 const struct got_error *
98 got_privsep_wait_for_child(pid_t pid)
102 if (waitpid(pid, &child_status, 0) == -1)
103 return got_error_from_errno("waitpid");
105 if (!WIFEXITED(child_status))
106 return got_error(GOT_ERR_PRIVSEP_DIED);
108 if (WEXITSTATUS(child_status) != 0)
109 return got_error(GOT_ERR_PRIVSEP_EXIT);
114 static const struct got_error *
115 recv_imsg_error(struct imsg *imsg, size_t datalen)
117 struct got_imsg_error *ierr;
119 if (datalen != sizeof(*ierr))
120 return got_error(GOT_ERR_PRIVSEP_LEN);
123 if (ierr->code == GOT_ERR_ERRNO) {
124 static struct got_error serr;
125 serr.code = GOT_ERR_ERRNO;
126 serr.msg = strerror(ierr->errno_code);
130 return got_error(ierr->code);
133 const struct got_error *
134 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
137 const struct got_error *err;
140 n = imsg_get(ibuf, imsg);
142 return got_error_from_errno("imsg_get");
145 err = read_imsg(ibuf);
148 n = imsg_get(ibuf, imsg);
151 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
152 return got_error(GOT_ERR_PRIVSEP_LEN);
154 if (imsg->hdr.type == GOT_IMSG_ERROR) {
155 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
156 return recv_imsg_error(imsg, datalen);
162 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
164 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
166 const struct got_error *poll_err;
167 struct got_imsg_error ierr;
170 ierr.code = err->code;
171 if (err->code == GOT_ERR_ERRNO)
172 ierr.errno_code = errno;
175 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
177 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
178 getprogname(), err->code, err->msg, strerror(errno));
182 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
184 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
185 getprogname(), err->code, err->msg, poll_err->msg);
189 ret = imsg_flush(ibuf);
191 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
192 getprogname(), err->code, err->msg, strerror(errno));
197 static const struct got_error *
198 flush_imsg(struct imsgbuf *ibuf)
200 const struct got_error *err;
202 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
206 if (imsg_flush(ibuf) == -1)
207 return got_error_from_errno("imsg_flush");
212 const struct got_error *
213 got_privsep_send_stop(int fd)
215 const struct got_error *err = NULL;
218 imsg_init(&ibuf, fd);
220 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
221 return got_error_from_errno("imsg_compose STOP");
223 err = flush_imsg(&ibuf);
228 const struct got_error *
229 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
231 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
233 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
235 return flush_imsg(ibuf);
238 const struct got_error *
239 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
240 struct got_object_id *id, int pack_idx)
242 const struct got_error *err = NULL;
243 struct got_imsg_packed_object iobj, *iobjp;
246 if (id) { /* commit is packed */
248 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
256 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
258 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
263 return flush_imsg(ibuf);
266 const struct got_error *
267 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
268 struct got_object_id *id, int pack_idx)
270 const struct got_error *err = NULL;
271 struct got_imsg_packed_object iobj, *iobjp;
274 if (id) { /* tree is packed */
276 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
284 if (imsg_compose(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, fd, iobjp, len)
286 err = got_error_from_errno("imsg_compose TREE_REQUEST");
291 return flush_imsg(ibuf);
294 const struct got_error *
295 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
296 struct got_object_id *id, int pack_idx)
298 struct got_imsg_packed_object iobj, *iobjp;
301 if (id) { /* tag is packed */
303 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
311 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
313 return got_error_from_errno("imsg_compose TAG_REQUEST");
315 return flush_imsg(ibuf);
318 const struct got_error *
319 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
320 struct got_object_id *id, int pack_idx)
322 const struct got_error *err = NULL;
323 struct got_imsg_packed_object iobj, *iobjp;
326 if (id) { /* blob is packed */
328 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
336 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
338 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
343 return flush_imsg(ibuf);
346 const struct got_error *
347 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
349 const struct got_error *err = NULL;
351 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
353 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
358 return flush_imsg(ibuf);
361 const struct got_error *
362 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
364 const struct got_error *err = NULL;
366 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
368 err = got_error_from_errno("imsg_compose TMPFD");
373 return flush_imsg(ibuf);
376 const struct got_error *
377 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
379 struct got_imsg_object iobj;
381 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
382 iobj.type = obj->type;
383 iobj.flags = obj->flags;
384 iobj.hdrlen = obj->hdrlen;
385 iobj.size = obj->size;
386 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
387 iobj.pack_offset = obj->pack_offset;
388 iobj.pack_idx = obj->pack_idx;
391 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
393 return got_error_from_errno("imsg_compose OBJECT");
395 return flush_imsg(ibuf);
398 const struct got_error *
399 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
400 struct imsgbuf *ibuf)
402 const struct got_error *err = NULL;
403 struct got_imsg_object *iobj;
404 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
406 if (datalen != sizeof(*iobj))
407 return got_error(GOT_ERR_PRIVSEP_LEN);
410 *obj = calloc(1, sizeof(**obj));
412 return got_error_from_errno("calloc");
414 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
415 (*obj)->type = iobj->type;
416 (*obj)->flags = iobj->flags;
417 (*obj)->hdrlen = iobj->hdrlen;
418 (*obj)->size = iobj->size;
419 /* path_packfile is handled by caller */
420 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
421 (*obj)->pack_offset = iobj->pack_offset;
422 (*obj)->pack_idx = iobj->pack_idx;
428 const struct got_error *
429 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
431 const struct got_error *err = NULL;
434 const size_t min_datalen =
435 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
439 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
443 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
445 switch (imsg.hdr.type) {
446 case GOT_IMSG_OBJECT:
447 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
450 err = got_error(GOT_ERR_PRIVSEP_MSG);
459 static const struct got_error *
460 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
463 const struct got_error *err = NULL;
464 size_t offset, remain;
469 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
471 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
472 commit->logmsg + offset, n) == -1) {
473 err = got_error_from_errno("imsg_compose "
478 err = flush_imsg(ibuf);
489 const struct got_error *
490 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
492 const struct got_error *err = NULL;
493 struct got_imsg_commit_object *icommit;
496 struct got_object_qid *qid;
497 size_t author_len = strlen(commit->author);
498 size_t committer_len = strlen(commit->committer);
499 size_t logmsg_len = strlen(commit->logmsg);
501 total = sizeof(*icommit) + author_len + committer_len +
502 commit->nparents * SHA1_DIGEST_LENGTH;
506 return got_error_from_errno("malloc");
508 icommit = (struct got_imsg_commit_object *)buf;
509 memcpy(icommit->tree_id, commit->tree_id->sha1,
510 sizeof(icommit->tree_id));
511 icommit->author_len = author_len;
512 icommit->author_time = commit->author_time;
513 icommit->author_gmtoff = commit->author_gmtoff;
514 icommit->committer_len = committer_len;
515 icommit->committer_time = commit->committer_time;
516 icommit->committer_gmtoff = commit->committer_gmtoff;
517 icommit->logmsg_len = logmsg_len;
518 icommit->nparents = commit->nparents;
520 len = sizeof(*icommit);
521 memcpy(buf + len, commit->author, author_len);
523 memcpy(buf + len, commit->committer, committer_len);
524 len += committer_len;
525 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
526 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
527 len += SHA1_DIGEST_LENGTH;
530 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
531 err = got_error_from_errno("imsg_compose COMMIT");
535 if (logmsg_len == 0 ||
536 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
537 err = flush_imsg(ibuf);
541 err = send_commit_logmsg(ibuf, commit, logmsg_len);
547 const struct got_error *
548 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
550 const struct got_error *err = NULL;
552 struct got_imsg_commit_object *icommit;
555 const size_t min_datalen =
556 MIN(sizeof(struct got_imsg_error),
557 sizeof(struct got_imsg_commit_object));
561 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
565 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
568 switch (imsg.hdr.type) {
569 case GOT_IMSG_COMMIT:
570 if (datalen < sizeof(*icommit)) {
571 err = got_error(GOT_ERR_PRIVSEP_LEN);
575 if (datalen != sizeof(*icommit) + icommit->author_len +
576 icommit->committer_len +
577 icommit->nparents * SHA1_DIGEST_LENGTH) {
578 err = got_error(GOT_ERR_PRIVSEP_LEN);
581 if (icommit->nparents < 0) {
582 err = got_error(GOT_ERR_PRIVSEP_LEN);
585 len += sizeof(*icommit);
587 *commit = got_object_commit_alloc_partial();
588 if (*commit == NULL) {
589 err = got_error_from_errno(
590 "got_object_commit_alloc_partial");
594 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
596 (*commit)->author_time = icommit->author_time;
597 (*commit)->author_gmtoff = icommit->author_gmtoff;
598 (*commit)->committer_time = icommit->committer_time;
599 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
601 if (icommit->author_len == 0) {
602 (*commit)->author = strdup("");
603 if ((*commit)->author == NULL) {
604 err = got_error_from_errno("strdup");
608 (*commit)->author = malloc(icommit->author_len + 1);
609 if ((*commit)->author == NULL) {
610 err = got_error_from_errno("malloc");
613 memcpy((*commit)->author, imsg.data + len,
614 icommit->author_len);
615 (*commit)->author[icommit->author_len] = '\0';
617 len += icommit->author_len;
619 if (icommit->committer_len == 0) {
620 (*commit)->committer = strdup("");
621 if ((*commit)->committer == NULL) {
622 err = got_error_from_errno("strdup");
626 (*commit)->committer =
627 malloc(icommit->committer_len + 1);
628 if ((*commit)->committer == NULL) {
629 err = got_error_from_errno("malloc");
632 memcpy((*commit)->committer, imsg.data + len,
633 icommit->committer_len);
634 (*commit)->committer[icommit->committer_len] = '\0';
636 len += icommit->committer_len;
638 if (icommit->logmsg_len == 0) {
639 (*commit)->logmsg = strdup("");
640 if ((*commit)->logmsg == NULL) {
641 err = got_error_from_errno("strdup");
645 size_t offset = 0, remain = icommit->logmsg_len;
647 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
648 if ((*commit)->logmsg == NULL) {
649 err = got_error_from_errno("malloc");
653 struct imsg imsg_log;
654 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
657 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
661 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
662 return got_error(GOT_ERR_PRIVSEP_MSG);
664 memcpy((*commit)->logmsg + offset,
666 imsg_free(&imsg_log);
670 (*commit)->logmsg[icommit->logmsg_len] = '\0';
673 for (i = 0; i < icommit->nparents; i++) {
674 struct got_object_qid *qid;
676 err = got_object_qid_alloc_partial(&qid);
679 memcpy(qid->id, imsg.data + len +
680 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
681 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
682 (*commit)->nparents++;
686 err = got_error(GOT_ERR_PRIVSEP_MSG);
695 const struct got_error *
696 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
698 const struct got_error *err = NULL;
699 struct got_imsg_tree_object itree;
700 struct got_tree_entry *te;
702 int nimsg; /* number of imsg queued in ibuf */
704 itree.nentries = tree->entries.nentries;
705 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
707 return got_error_from_errno("imsg_compose TREE");
709 totlen = sizeof(itree);
711 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
712 struct got_imsg_tree_entry *ite;
714 size_t len = sizeof(*ite) + strlen(te->name);
716 if (len > MAX_IMSGSIZE)
717 return got_error(GOT_ERR_NO_SPACE);
720 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
721 err = flush_imsg(ibuf);
729 return got_error_from_errno("malloc");
731 ite = (struct got_imsg_tree_entry *)buf;
732 memcpy(ite->id, te->id->sha1, sizeof(ite->id));
733 ite->mode = te->mode;
734 memcpy(buf + sizeof(*ite), te->name, strlen(te->name));
736 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
738 err = got_error_from_errno("imsg_compose TREE_ENTRY");
745 return flush_imsg(ibuf);
748 const struct got_error *
749 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
751 const struct got_error *err = NULL;
752 const size_t min_datalen =
753 MIN(sizeof(struct got_imsg_error),
754 sizeof(struct got_imsg_tree_object));
755 struct got_imsg_tree_object *itree;
760 err = read_imsg(ibuf);
768 struct got_imsg_tree_entry *ite;
769 struct got_tree_entry *te = NULL;
771 n = imsg_get(ibuf, &imsg);
773 if (*tree && (*tree)->entries.nentries != nentries)
778 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
779 return got_error(GOT_ERR_PRIVSEP_LEN);
781 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
783 switch (imsg.hdr.type) {
785 err = recv_imsg_error(&imsg, datalen);
788 /* This message should only appear once. */
790 err = got_error(GOT_ERR_PRIVSEP_MSG);
793 if (datalen != sizeof(*itree)) {
794 err = got_error(GOT_ERR_PRIVSEP_LEN);
798 *tree = malloc(sizeof(**tree));
800 err = got_error_from_errno("malloc");
803 (*tree)->entries.nentries = itree->nentries;
804 SIMPLEQ_INIT(&(*tree)->entries.head);
807 case GOT_IMSG_TREE_ENTRY:
808 /* This message should be preceeded by GOT_IMSG_TREE. */
810 err = got_error(GOT_ERR_PRIVSEP_MSG);
813 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
814 err = got_error(GOT_ERR_PRIVSEP_LEN);
818 /* Remaining data contains the entry's name. */
819 datalen -= sizeof(*ite);
820 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
821 err = got_error(GOT_ERR_PRIVSEP_LEN);
826 te = got_alloc_tree_entry_partial();
828 err = got_error_from_errno(
829 "got_alloc_tree_entry_partial");
832 te->name = malloc(datalen + 1);
833 if (te->name == NULL) {
835 err = got_error_from_errno("malloc");
838 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
839 te->name[datalen] = '\0';
841 memcpy(te->id->sha1, ite->id, SHA1_DIGEST_LENGTH);
842 te->mode = ite->mode;
843 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
847 err = got_error(GOT_ERR_PRIVSEP_MSG);
854 if (*tree && (*tree)->entries.nentries != nentries) {
856 err = got_error(GOT_ERR_PRIVSEP_LEN);
857 got_object_tree_close(*tree);
864 const struct got_error *
865 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
868 struct got_imsg_blob iblob;
871 iblob.hdrlen = hdrlen;
876 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
877 return got_error(GOT_ERR_NO_SPACE);
879 buf = malloc(sizeof(iblob) + size);
881 return got_error_from_errno("malloc");
883 memcpy(buf, &iblob, sizeof(iblob));
884 memcpy(buf + sizeof(iblob), data, size);
885 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
886 sizeof(iblob) + size) == -1) {
888 return got_error_from_errno("imsg_compose BLOB");
892 /* Data has already been written to file descriptor. */
893 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
894 sizeof(iblob)) == -1)
895 return got_error_from_errno("imsg_compose BLOB");
899 return flush_imsg(ibuf);
902 const struct got_error *
903 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
904 struct imsgbuf *ibuf)
906 const struct got_error *err = NULL;
908 struct got_imsg_blob *iblob;
913 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
917 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
919 switch (imsg.hdr.type) {
921 if (datalen < sizeof(*iblob)) {
922 err = got_error(GOT_ERR_PRIVSEP_LEN);
927 *hdrlen = iblob->hdrlen;
929 if (datalen == sizeof(*iblob)) {
930 /* Data has been written to file descriptor. */
934 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
935 err = got_error(GOT_ERR_PRIVSEP_LEN);
939 *outbuf = malloc(*size);
940 if (*outbuf == NULL) {
941 err = got_error_from_errno("malloc");
944 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
947 err = got_error(GOT_ERR_PRIVSEP_MSG);
956 static const struct got_error *
957 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
959 const struct got_error *err = NULL;
960 size_t offset, remain;
965 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
967 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
968 tag->tagmsg + offset, n) == -1) {
969 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
973 err = flush_imsg(ibuf);
984 const struct got_error *
985 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
987 const struct got_error *err = NULL;
988 struct got_imsg_tag_object *itag;
991 size_t tag_len = strlen(tag->tag);
992 size_t tagger_len = strlen(tag->tagger);
993 size_t tagmsg_len = strlen(tag->tagmsg);
995 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
999 return got_error_from_errno("malloc");
1001 itag = (struct got_imsg_tag_object *)buf;
1002 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1003 itag->obj_type = tag->obj_type;
1004 itag->tag_len = tag_len;
1005 itag->tagger_len = tagger_len;
1006 itag->tagger_time = tag->tagger_time;
1007 itag->tagger_gmtoff = tag->tagger_gmtoff;
1008 itag->tagmsg_len = tagmsg_len;
1010 len = sizeof(*itag);
1011 memcpy(buf + len, tag->tag, tag_len);
1013 memcpy(buf + len, tag->tagger, tagger_len);
1016 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1017 err = got_error_from_errno("imsg_compose TAG");
1021 if (tagmsg_len == 0 ||
1022 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1023 err = flush_imsg(ibuf);
1027 err = send_tagmsg(ibuf, tag, tagmsg_len);
1033 const struct got_error *
1034 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1036 const struct got_error *err = NULL;
1038 struct got_imsg_tag_object *itag;
1039 size_t len, datalen;
1040 const size_t min_datalen =
1041 MIN(sizeof(struct got_imsg_error),
1042 sizeof(struct got_imsg_tag_object));
1046 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1050 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1053 switch (imsg.hdr.type) {
1055 if (datalen < sizeof(*itag)) {
1056 err = got_error(GOT_ERR_PRIVSEP_LEN);
1060 if (datalen != sizeof(*itag) + itag->tag_len +
1062 err = got_error(GOT_ERR_PRIVSEP_LEN);
1065 len += sizeof(*itag);
1067 *tag = calloc(1, sizeof(**tag));
1069 err = got_error_from_errno("calloc");
1073 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1075 if (itag->tag_len == 0) {
1076 (*tag)->tag = strdup("");
1077 if ((*tag)->tag == NULL) {
1078 err = got_error_from_errno("strdup");
1082 (*tag)->tag = malloc(itag->tag_len + 1);
1083 if ((*tag)->tag == NULL) {
1084 err = got_error_from_errno("malloc");
1087 memcpy((*tag)->tag, imsg.data + len,
1089 (*tag)->tag[itag->tag_len] = '\0';
1091 len += itag->tag_len;
1093 (*tag)->obj_type = itag->obj_type;
1094 (*tag)->tagger_time = itag->tagger_time;
1095 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1097 if (itag->tagger_len == 0) {
1098 (*tag)->tagger = strdup("");
1099 if ((*tag)->tagger == NULL) {
1100 err = got_error_from_errno("strdup");
1104 (*tag)->tagger = malloc(itag->tagger_len + 1);
1105 if ((*tag)->tagger == NULL) {
1106 err = got_error_from_errno("malloc");
1109 memcpy((*tag)->tagger, imsg.data + len,
1111 (*tag)->tagger[itag->tagger_len] = '\0';
1113 len += itag->tagger_len;
1115 if (itag->tagmsg_len == 0) {
1116 (*tag)->tagmsg = strdup("");
1117 if ((*tag)->tagmsg == NULL) {
1118 err = got_error_from_errno("strdup");
1122 size_t offset = 0, remain = itag->tagmsg_len;
1124 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1125 if ((*tag)->tagmsg == NULL) {
1126 err = got_error_from_errno("malloc");
1129 while (remain > 0) {
1130 struct imsg imsg_log;
1131 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1134 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1138 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1139 return got_error(GOT_ERR_PRIVSEP_MSG);
1141 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1143 imsg_free(&imsg_log);
1147 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1152 err = got_error(GOT_ERR_PRIVSEP_MSG);
1161 const struct got_error *
1162 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1163 struct got_packidx *packidx)
1165 const struct got_error *err = NULL;
1166 struct got_imsg_packidx ipackidx;
1167 struct got_imsg_pack ipack;
1170 ipackidx.len = packidx->len;
1171 fd = dup(packidx->fd);
1173 return got_error_from_errno("dup");
1175 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1176 sizeof(ipackidx)) == -1) {
1177 err = got_error_from_errno("imsg_compose PACKIDX");
1182 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1183 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1184 return got_error(GOT_ERR_NO_SPACE);
1185 ipack.filesize = pack->filesize;
1189 return got_error_from_errno("dup");
1191 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1193 err = got_error_from_errno("imsg_compose PACK");
1198 return flush_imsg(ibuf);
1201 const struct got_error *
1202 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1203 struct got_object_id *id)
1205 struct got_imsg_packed_object iobj;
1208 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1210 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1211 &iobj, sizeof(iobj)) == -1)
1212 return got_error_from_errno("imsg_compose "
1213 "PACKED_OBJECT_REQUEST");
1215 return flush_imsg(ibuf);
1218 const struct got_error *
1219 got_privsep_unveil_exec_helpers(void)
1221 const char *helpers[] = {
1222 GOT_PATH_PROG_READ_PACK,
1223 GOT_PATH_PROG_READ_OBJECT,
1224 GOT_PATH_PROG_READ_COMMIT,
1225 GOT_PATH_PROG_READ_TREE,
1226 GOT_PATH_PROG_READ_BLOB,
1227 GOT_PATH_PROG_READ_TAG,
1231 for (i = 0; i < nitems(helpers); i++) {
1232 if (unveil(helpers[i], "x") == 0)
1234 return got_error_from_errno2("unveil", helpers[i]);