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 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/syslimits.h>
21 #include <sys/wait.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <stdint.h>
28 #include <poll.h>
29 #include <imsg.h>
30 #include <sha1.h>
31 #include <zlib.h>
32 #include <time.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"
45 #ifndef MIN
46 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
47 #endif
49 static const struct got_error *
50 poll_fd(int fd, int events, int timeout)
51 {
52 struct pollfd pfd[1];
53 int n;
55 pfd[0].fd = fd;
56 pfd[0].events = events;
58 n = poll(pfd, 1, timeout);
59 if (n == -1)
60 return got_error_from_errno();
61 if (n == 0)
62 return got_error(GOT_ERR_TIMEOUT);
63 if (pfd[0].revents & (POLLERR | POLLNVAL))
64 return got_error_from_errno();
65 if (pfd[0].revents & (events | POLLHUP))
66 return NULL;
68 return got_error(GOT_ERR_INTERRUPT);
69 }
71 static const struct got_error *
72 read_imsg(struct imsgbuf *ibuf)
73 {
74 const struct got_error *err;
75 size_t n;
77 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
78 if (err)
79 return err;
81 n = imsg_read(ibuf);
82 if (n == -1) {
83 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
84 return got_error(GOT_ERR_PRIVSEP_NO_FD);
85 return got_error(GOT_ERR_PRIVSEP_READ);
86 }
87 if (n == 0)
88 return got_error(GOT_ERR_PRIVSEP_PIPE);
90 return NULL;
91 }
93 const struct got_error *
94 got_privsep_wait_for_child(pid_t pid)
95 {
96 int child_status;
98 waitpid(pid, &child_status, 0);
100 if (!WIFEXITED(child_status))
101 return got_error(GOT_ERR_PRIVSEP_DIED);
103 if (WEXITSTATUS(child_status) != 0)
104 return got_error(GOT_ERR_PRIVSEP_EXIT);
106 return NULL;
109 static const struct got_error *
110 recv_imsg_error(struct imsg *imsg, size_t datalen)
112 struct got_imsg_error *ierr;
114 if (datalen != sizeof(*ierr))
115 return got_error(GOT_ERR_PRIVSEP_LEN);
117 ierr = imsg->data;
118 if (ierr->code == GOT_ERR_ERRNO) {
119 static struct got_error serr;
120 serr.code = GOT_ERR_ERRNO;
121 serr.msg = strerror(ierr->errno_code);
122 return &serr;
125 return got_error(ierr->code);
128 const struct got_error *
129 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
130 size_t min_datalen)
132 const struct got_error *err;
133 ssize_t n;
135 n = imsg_get(ibuf, imsg);
136 if (n == -1)
137 return got_error_from_errno();
139 while (n == 0) {
140 err = read_imsg(ibuf);
141 if (err)
142 return err;
143 n = imsg_get(ibuf, imsg);
146 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
147 return got_error(GOT_ERR_PRIVSEP_LEN);
149 if (imsg->hdr.type == GOT_IMSG_ERROR) {
150 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
151 return recv_imsg_error(imsg, datalen);
154 return NULL;
157 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
158 void
159 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
161 const struct got_error *poll_err;
162 struct got_imsg_error ierr;
163 int ret;
165 ierr.code = err->code;
166 if (err->code == GOT_ERR_ERRNO)
167 ierr.errno_code = errno;
168 else
169 ierr.errno_code = 0;
170 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
171 if (ret == -1) {
172 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
173 getprogname(), err->code, err->msg, strerror(errno));
174 return;
177 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
178 if (poll_err) {
179 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
180 getprogname(), err->code, err->msg, poll_err->msg);
181 return;
184 ret = imsg_flush(ibuf);
185 if (ret == -1) {
186 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
187 getprogname(), err->code, err->msg, strerror(errno));
188 return;
192 static const struct got_error *
193 flush_imsg(struct imsgbuf *ibuf)
195 const struct got_error *err;
197 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
198 if (err)
199 return err;
201 if (imsg_flush(ibuf) == -1)
202 return got_error_from_errno();
204 return NULL;
207 const struct got_error *
208 got_privsep_send_stop(int fd)
210 const struct got_error *err = NULL;
211 struct imsgbuf ibuf;
213 imsg_init(&ibuf, fd);
215 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
216 return got_error_from_errno();
218 err = flush_imsg(&ibuf);
219 imsg_clear(&ibuf);
220 return err;
223 const struct got_error *
224 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
226 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
227 == -1)
228 return got_error_from_errno();
230 return flush_imsg(ibuf);
233 const struct got_error *
234 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
235 struct got_object_id *id, int pack_idx)
237 const struct got_error *err = NULL;
238 struct got_imsg_packed_object iobj, *iobjp;
239 size_t len;
241 if (id) { /* commit is packed */
242 iobj.idx = pack_idx;
243 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
244 iobjp = &iobj;
245 len = sizeof(iobj);
246 } else {
247 iobjp = NULL;
248 len = 0;
251 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
252 == -1) {
253 err = got_error_from_errno();
254 close(fd);
255 return err;
258 return flush_imsg(ibuf);
261 const struct got_error *
262 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
263 struct got_object_id *id, int pack_idx)
265 const struct got_error *err = NULL;
266 struct got_imsg_packed_object iobj, *iobjp;
267 size_t len;
269 if (id) { /* tree is packed */
270 iobj.idx = pack_idx;
271 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
272 iobjp = &iobj;
273 len = sizeof(iobj);
274 } else {
275 iobjp = NULL;
276 len = 0;
279 if (imsg_compose(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, fd, iobjp, len)
280 == -1) {
281 err = got_error_from_errno();
282 close(fd);
283 return err;
286 return flush_imsg(ibuf);
289 const struct got_error *
290 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
291 struct got_object_id *id, int pack_idx)
293 struct got_imsg_packed_object iobj, *iobjp;
294 size_t len;
296 if (id) { /* tag is packed */
297 iobj.idx = pack_idx;
298 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
299 iobjp = &iobj;
300 len = sizeof(iobj);
301 } else {
302 iobjp = NULL;
303 len = 0;
306 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
307 == -1)
308 return got_error_from_errno();
310 return flush_imsg(ibuf);
313 const struct got_error *
314 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
315 struct got_object_id *id, int pack_idx)
317 const struct got_error *err = NULL;
318 struct got_imsg_packed_object iobj, *iobjp;
319 size_t len;
321 if (id) { /* blob is packed */
322 iobj.idx = pack_idx;
323 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
324 iobjp = &iobj;
325 len = sizeof(iobj);
326 } else {
327 iobjp = NULL;
328 len = 0;
331 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
332 == -1) {
333 err = got_error_from_errno();
334 close(infd);
335 return err;
338 return flush_imsg(ibuf);
341 const struct got_error *
342 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
344 const struct got_error *err = NULL;
346 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
347 == -1) {
348 err = got_error_from_errno();
349 close(outfd);
350 return err;
353 return flush_imsg(ibuf);
356 const struct got_error *
357 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
359 const struct got_error *err = NULL;
361 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
362 == -1) {
363 err = got_error_from_errno();
364 close(fd);
365 return err;
368 return flush_imsg(ibuf);
371 const struct got_error *
372 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
374 struct got_imsg_object iobj;
376 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
377 iobj.type = obj->type;
378 iobj.flags = obj->flags;
379 iobj.hdrlen = obj->hdrlen;
380 iobj.size = obj->size;
381 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
382 iobj.pack_offset = obj->pack_offset;
383 iobj.pack_idx = obj->pack_idx;
386 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
387 == -1)
388 return got_error_from_errno();
390 return flush_imsg(ibuf);
393 const struct got_error *
394 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
395 struct imsgbuf *ibuf)
397 const struct got_error *err = NULL;
398 struct got_imsg_object *iobj;
399 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
401 if (datalen != sizeof(*iobj))
402 return got_error(GOT_ERR_PRIVSEP_LEN);
403 iobj = imsg->data;
405 *obj = calloc(1, sizeof(**obj));
406 if (*obj == NULL)
407 return got_error_from_errno();
409 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
410 (*obj)->type = iobj->type;
411 (*obj)->flags = iobj->flags;
412 (*obj)->hdrlen = iobj->hdrlen;
413 (*obj)->size = iobj->size;
414 /* path_packfile is handled by caller */
415 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
416 (*obj)->pack_offset = iobj->pack_offset;
417 (*obj)->pack_idx = iobj->pack_idx;
420 return err;
423 const struct got_error *
424 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
426 const struct got_error *err = NULL;
427 struct imsg imsg;
428 size_t datalen;
429 const size_t min_datalen =
430 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
432 *obj = NULL;
434 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
435 if (err)
436 return err;
438 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
440 switch (imsg.hdr.type) {
441 case GOT_IMSG_OBJECT:
442 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
443 break;
444 default:
445 err = got_error(GOT_ERR_PRIVSEP_MSG);
446 break;
449 imsg_free(&imsg);
451 return err;
454 static const struct got_error *
455 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
456 size_t logmsg_len)
458 const struct got_error *err = NULL;
459 size_t offset, remain;
461 offset = 0;
462 remain = logmsg_len;
463 while (remain > 0) {
464 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
466 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
467 commit->logmsg + offset, n) == -1) {
468 err = got_error_from_errno();
469 break;
472 err = flush_imsg(ibuf);
473 if (err)
474 break;
476 offset += n;
477 remain -= n;
480 return err;
483 const struct got_error *
484 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
486 const struct got_error *err = NULL;
487 struct got_imsg_commit_object *icommit;
488 uint8_t *buf;
489 size_t len, total;
490 struct got_object_qid *qid;
491 size_t author_len = strlen(commit->author);
492 size_t committer_len = strlen(commit->committer);
493 size_t logmsg_len = strlen(commit->logmsg);
495 total = sizeof(*icommit) + author_len + committer_len +
496 commit->nparents * SHA1_DIGEST_LENGTH;
498 buf = malloc(total);
499 if (buf == NULL)
500 return got_error_from_errno();
502 icommit = (struct got_imsg_commit_object *)buf;
503 memcpy(icommit->tree_id, commit->tree_id->sha1,
504 sizeof(icommit->tree_id));
505 icommit->author_len = author_len;
506 icommit->author_time = commit->author_time;
507 icommit->author_gmtoff = commit->author_gmtoff;
508 icommit->committer_len = committer_len;
509 icommit->committer_time = commit->committer_time;
510 icommit->committer_gmtoff = commit->committer_gmtoff;
511 icommit->logmsg_len = logmsg_len;
512 icommit->nparents = commit->nparents;
514 len = sizeof(*icommit);
515 memcpy(buf + len, commit->author, author_len);
516 len += author_len;
517 memcpy(buf + len, commit->committer, committer_len);
518 len += committer_len;
519 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
520 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
521 len += SHA1_DIGEST_LENGTH;
524 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
525 err = got_error_from_errno();
526 goto done;
529 if (logmsg_len == 0 ||
530 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
531 err = flush_imsg(ibuf);
532 if (err)
533 goto done;
535 err = send_commit_logmsg(ibuf, commit, logmsg_len);
536 done:
537 free(buf);
538 return err;
541 const struct got_error *
542 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
544 const struct got_error *err = NULL;
545 struct imsg imsg;
546 struct got_imsg_commit_object *icommit;
547 size_t len, datalen;
548 int i;
549 const size_t min_datalen =
550 MIN(sizeof(struct got_imsg_error),
551 sizeof(struct got_imsg_commit_object));
553 *commit = NULL;
555 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
556 if (err)
557 return err;
559 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
560 len = 0;
562 switch (imsg.hdr.type) {
563 case GOT_IMSG_COMMIT:
564 if (datalen < sizeof(*icommit)) {
565 err = got_error(GOT_ERR_PRIVSEP_LEN);
566 break;
568 icommit = imsg.data;
569 if (datalen != sizeof(*icommit) + icommit->author_len +
570 icommit->committer_len +
571 icommit->nparents * SHA1_DIGEST_LENGTH) {
572 err = got_error(GOT_ERR_PRIVSEP_LEN);
573 break;
575 if (icommit->nparents < 0) {
576 err = got_error(GOT_ERR_PRIVSEP_LEN);
577 break;
579 len += sizeof(*icommit);
581 *commit = got_object_commit_alloc_partial();
582 if (*commit == NULL) {
583 err = got_error_from_errno();
584 break;
587 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
588 SHA1_DIGEST_LENGTH);
589 (*commit)->author_time = icommit->author_time;
590 (*commit)->author_gmtoff = icommit->author_gmtoff;
591 (*commit)->committer_time = icommit->committer_time;
592 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
594 if (icommit->author_len == 0) {
595 (*commit)->author = strdup("");
596 if ((*commit)->author == NULL) {
597 err = got_error_from_errno();
598 break;
600 } else {
601 (*commit)->author = malloc(icommit->author_len + 1);
602 if ((*commit)->author == NULL) {
603 err = got_error_from_errno();
604 break;
606 memcpy((*commit)->author, imsg.data + len,
607 icommit->author_len);
608 (*commit)->author[icommit->author_len] = '\0';
610 len += icommit->author_len;
612 if (icommit->committer_len == 0) {
613 (*commit)->committer = strdup("");
614 if ((*commit)->committer == NULL) {
615 err = got_error_from_errno();
616 break;
618 } else {
619 (*commit)->committer =
620 malloc(icommit->committer_len + 1);
621 if ((*commit)->committer == NULL) {
622 err = got_error_from_errno();
623 break;
625 memcpy((*commit)->committer, imsg.data + len,
626 icommit->committer_len);
627 (*commit)->committer[icommit->committer_len] = '\0';
629 len += icommit->committer_len;
631 if (icommit->logmsg_len == 0) {
632 (*commit)->logmsg = strdup("");
633 if ((*commit)->logmsg == NULL) {
634 err = got_error_from_errno();
635 break;
637 } else {
638 size_t offset = 0, remain = icommit->logmsg_len;
640 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
641 if ((*commit)->logmsg == NULL) {
642 err = got_error_from_errno();
643 break;
645 while (remain > 0) {
646 struct imsg imsg_log;
647 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
648 remain);
650 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
651 if (err)
652 return err;
654 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
655 return got_error(GOT_ERR_PRIVSEP_MSG);
657 memcpy((*commit)->logmsg + offset,
658 imsg_log.data, n);
659 imsg_free(&imsg_log);
660 offset += n;
661 remain -= n;
663 (*commit)->logmsg[icommit->logmsg_len] = '\0';
666 for (i = 0; i < icommit->nparents; i++) {
667 struct got_object_qid *qid;
669 err = got_object_qid_alloc_partial(&qid);
670 if (err)
671 break;
672 memcpy(qid->id, imsg.data + len +
673 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
674 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
675 (*commit)->nparents++;
677 break;
678 default:
679 err = got_error(GOT_ERR_PRIVSEP_MSG);
680 break;
683 imsg_free(&imsg);
685 return err;
688 const struct got_error *
689 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
691 const struct got_error *err = NULL;
692 struct got_imsg_tree_object itree;
693 struct got_tree_entry *te;
694 size_t totlen;
695 int nimsg; /* number of imsg queued in ibuf */
697 itree.nentries = tree->entries.nentries;
698 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
699 == -1)
700 return got_error_from_errno();
702 totlen = sizeof(itree);
703 nimsg = 1;
704 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
705 struct got_imsg_tree_entry *ite;
706 uint8_t *buf = NULL;
707 size_t len = sizeof(*ite) + strlen(te->name);
709 if (len > MAX_IMSGSIZE)
710 return got_error(GOT_ERR_NO_SPACE);
712 nimsg++;
713 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
714 err = flush_imsg(ibuf);
715 if (err)
716 return err;
717 nimsg = 0;
720 buf = malloc(len);
721 if (buf == NULL)
722 return got_error_from_errno();
724 ite = (struct got_imsg_tree_entry *)buf;
725 memcpy(ite->id, te->id->sha1, sizeof(ite->id));
726 ite->mode = te->mode;
727 memcpy(buf + sizeof(*ite), te->name, strlen(te->name));
729 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
730 buf, len) == -1)
731 err = got_error_from_errno();
732 free(buf);
733 if (err)
734 return err;
735 totlen += len;
738 return flush_imsg(ibuf);
741 const struct got_error *
742 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
744 const struct got_error *err = NULL;
745 const size_t min_datalen =
746 MIN(sizeof(struct got_imsg_error),
747 sizeof(struct got_imsg_tree_object));
748 struct got_imsg_tree_object *itree;
749 int nentries = 0;
751 *tree = NULL;
752 get_more:
753 err = read_imsg(ibuf);
754 if (err)
755 goto done;
757 while (1) {
758 struct imsg imsg;
759 size_t n;
760 size_t datalen;
761 struct got_imsg_tree_entry *ite;
762 struct got_tree_entry *te = NULL;
764 n = imsg_get(ibuf, &imsg);
765 if (n == 0) {
766 if (*tree && (*tree)->entries.nentries != nentries)
767 goto get_more;
768 break;
771 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
772 return got_error(GOT_ERR_PRIVSEP_LEN);
774 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
776 switch (imsg.hdr.type) {
777 case GOT_IMSG_ERROR:
778 err = recv_imsg_error(&imsg, datalen);
779 break;
780 case GOT_IMSG_TREE:
781 /* This message should only appear once. */
782 if (*tree != NULL) {
783 err = got_error(GOT_ERR_PRIVSEP_MSG);
784 break;
786 if (datalen != sizeof(*itree)) {
787 err = got_error(GOT_ERR_PRIVSEP_LEN);
788 break;
790 itree = imsg.data;
791 *tree = malloc(sizeof(**tree));
792 if (*tree == NULL) {
793 err = got_error_from_errno();
794 break;
796 (*tree)->entries.nentries = itree->nentries;
797 SIMPLEQ_INIT(&(*tree)->entries.head);
798 (*tree)->refcnt = 0;
799 break;
800 case GOT_IMSG_TREE_ENTRY:
801 /* This message should be preceeded by GOT_IMSG_TREE. */
802 if (*tree == NULL) {
803 err = got_error(GOT_ERR_PRIVSEP_MSG);
804 break;
806 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
807 err = got_error(GOT_ERR_PRIVSEP_LEN);
808 break;
811 /* Remaining data contains the entry's name. */
812 datalen -= sizeof(*ite);
813 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
814 err = got_error(GOT_ERR_PRIVSEP_LEN);
815 break;
817 ite = imsg.data;
819 te = got_alloc_tree_entry_partial();
820 if (te == NULL) {
821 err = got_error_from_errno();
822 break;
824 te->name = malloc(datalen + 1);
825 if (te->name == NULL) {
826 free(te);
827 err = got_error_from_errno();
828 break;
830 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
831 te->name[datalen] = '\0';
833 memcpy(te->id->sha1, ite->id, SHA1_DIGEST_LENGTH);
834 te->mode = ite->mode;
835 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
836 nentries++;
837 break;
838 default:
839 err = got_error(GOT_ERR_PRIVSEP_MSG);
840 break;
843 imsg_free(&imsg);
845 done:
846 if (*tree && (*tree)->entries.nentries != nentries) {
847 if (err == NULL)
848 err = got_error(GOT_ERR_PRIVSEP_LEN);
849 got_object_tree_close(*tree);
850 *tree = NULL;
853 return err;
856 const struct got_error *
857 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
858 const uint8_t *data)
860 struct got_imsg_blob iblob;
862 iblob.size = size;
863 iblob.hdrlen = hdrlen;
865 if (data) {
866 uint8_t *buf;
868 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
869 return got_error(GOT_ERR_NO_SPACE);
871 buf = malloc(sizeof(iblob) + size);
872 if (buf == NULL)
873 return got_error_from_errno();
875 memcpy(buf, &iblob, sizeof(iblob));
876 memcpy(buf + sizeof(iblob), data, size);
877 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
878 sizeof(iblob) + size) == -1) {
879 free(buf);
880 return got_error_from_errno();
882 free(buf);
883 } else {
884 /* Data has already been written to file descriptor. */
885 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
886 sizeof(iblob)) == -1)
887 return got_error_from_errno();
891 return flush_imsg(ibuf);
894 const struct got_error *
895 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
896 struct imsgbuf *ibuf)
898 const struct got_error *err = NULL;
899 struct imsg imsg;
900 struct got_imsg_blob *iblob;
901 size_t datalen;
903 *outbuf = NULL;
905 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
906 if (err)
907 return err;
909 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
911 switch (imsg.hdr.type) {
912 case GOT_IMSG_BLOB:
913 if (datalen < sizeof(*iblob)) {
914 err = got_error(GOT_ERR_PRIVSEP_LEN);
915 break;
917 iblob = imsg.data;
918 *size = iblob->size;
919 *hdrlen = iblob->hdrlen;
921 if (datalen == sizeof(*iblob)) {
922 /* Data has been written to file descriptor. */
923 break;
926 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
927 err = got_error(GOT_ERR_PRIVSEP_LEN);
928 break;
931 *outbuf = malloc(*size);
932 if (*outbuf == NULL) {
933 err = got_error_from_errno();
934 break;
936 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
937 break;
938 default:
939 err = got_error(GOT_ERR_PRIVSEP_MSG);
940 break;
943 imsg_free(&imsg);
945 return err;
948 static const struct got_error *
949 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
951 const struct got_error *err = NULL;
952 size_t offset, remain;
954 offset = 0;
955 remain = tagmsg_len;
956 while (remain > 0) {
957 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
959 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
960 tag->tagmsg + offset, n) == -1) {
961 err = got_error_from_errno();
962 break;
965 err = flush_imsg(ibuf);
966 if (err)
967 break;
969 offset += n;
970 remain -= n;
973 return err;
976 const struct got_error *
977 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
979 const struct got_error *err = NULL;
980 struct got_imsg_tag_object *itag;
981 uint8_t *buf;
982 size_t len, total;
983 size_t tag_len = strlen(tag->tag);
984 size_t tagger_len = strlen(tag->tagger);
985 size_t tagmsg_len = strlen(tag->tagmsg);
987 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
989 buf = malloc(total);
990 if (buf == NULL)
991 return got_error_from_errno();
993 itag = (struct got_imsg_tag_object *)buf;
994 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
995 itag->obj_type = tag->obj_type;
996 itag->tag_len = tag_len;
997 itag->tagger_len = tagger_len;
998 itag->tagger_time = tag->tagger_time;
999 itag->tagger_gmtoff = tag->tagger_gmtoff;
1000 itag->tagmsg_len = tagmsg_len;
1002 len = sizeof(*itag);
1003 memcpy(buf + len, tag->tag, tag_len);
1004 len += tag_len;
1005 memcpy(buf + len, tag->tagger, tagger_len);
1006 len += tagger_len;
1008 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1009 err = got_error_from_errno();
1010 goto done;
1013 if (tagmsg_len == 0 ||
1014 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1015 err = flush_imsg(ibuf);
1016 if (err)
1017 goto done;
1019 err = send_tagmsg(ibuf, tag, tagmsg_len);
1020 done:
1021 free(buf);
1022 return err;
1025 const struct got_error *
1026 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1028 const struct got_error *err = NULL;
1029 struct imsg imsg;
1030 struct got_imsg_tag_object *itag;
1031 size_t len, datalen;
1032 const size_t min_datalen =
1033 MIN(sizeof(struct got_imsg_error),
1034 sizeof(struct got_imsg_tag_object));
1036 *tag = NULL;
1038 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1039 if (err)
1040 return err;
1042 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1043 len = 0;
1045 switch (imsg.hdr.type) {
1046 case GOT_IMSG_TAG:
1047 if (datalen < sizeof(*itag)) {
1048 err = got_error(GOT_ERR_PRIVSEP_LEN);
1049 break;
1051 itag = imsg.data;
1052 if (datalen != sizeof(*itag) + itag->tag_len +
1053 itag->tagger_len) {
1054 err = got_error(GOT_ERR_PRIVSEP_LEN);
1055 break;
1057 len += sizeof(*itag);
1059 *tag = calloc(1, sizeof(**tag));
1060 if (*tag == NULL) {
1061 err = got_error_from_errno();
1062 break;
1065 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1067 if (itag->tag_len == 0) {
1068 (*tag)->tag = strdup("");
1069 if ((*tag)->tag == NULL) {
1070 err = got_error_from_errno();
1071 break;
1073 } else {
1074 (*tag)->tag = malloc(itag->tag_len + 1);
1075 if ((*tag)->tag == NULL) {
1076 err = got_error_from_errno();
1077 break;
1079 memcpy((*tag)->tag, imsg.data + len,
1080 itag->tag_len);
1081 (*tag)->tag[itag->tag_len] = '\0';
1083 len += itag->tag_len;
1085 (*tag)->obj_type = itag->obj_type;
1086 (*tag)->tagger_time = itag->tagger_time;
1087 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1089 if (itag->tagger_len == 0) {
1090 (*tag)->tagger = strdup("");
1091 if ((*tag)->tagger == NULL) {
1092 err = got_error_from_errno();
1093 break;
1095 } else {
1096 (*tag)->tagger = malloc(itag->tagger_len + 1);
1097 if ((*tag)->tagger == NULL) {
1098 err = got_error_from_errno();
1099 break;
1101 memcpy((*tag)->tagger, imsg.data + len,
1102 itag->tagger_len);
1103 (*tag)->tagger[itag->tagger_len] = '\0';
1105 len += itag->tagger_len;
1107 if (itag->tagmsg_len == 0) {
1108 (*tag)->tagmsg = strdup("");
1109 if ((*tag)->tagmsg == NULL) {
1110 err = got_error_from_errno();
1111 break;
1113 } else {
1114 size_t offset = 0, remain = itag->tagmsg_len;
1116 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1117 if ((*tag)->tagmsg == NULL) {
1118 err = got_error_from_errno();
1119 break;
1121 while (remain > 0) {
1122 struct imsg imsg_log;
1123 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1124 remain);
1126 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1127 if (err)
1128 return err;
1130 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1131 return got_error(GOT_ERR_PRIVSEP_MSG);
1133 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1134 n);
1135 imsg_free(&imsg_log);
1136 offset += n;
1137 remain -= n;
1139 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1142 break;
1143 default:
1144 err = got_error(GOT_ERR_PRIVSEP_MSG);
1145 break;
1148 imsg_free(&imsg);
1150 return err;
1153 const struct got_error *
1154 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1155 struct got_packidx *packidx)
1157 const struct got_error *err = NULL;
1158 struct got_imsg_packidx ipackidx;
1159 struct got_imsg_pack ipack;
1160 int fd;
1162 ipackidx.len = packidx->len;
1163 fd = dup(packidx->fd);
1164 if (fd == -1)
1165 return got_error_from_errno();
1167 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1168 sizeof(ipackidx)) == -1) {
1169 err = got_error_from_errno();
1170 close(fd);
1171 return err;
1174 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1175 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1176 return got_error(GOT_ERR_NO_SPACE);
1177 ipack.filesize = pack->filesize;
1179 fd = dup(pack->fd);
1180 if (fd == -1)
1181 return got_error_from_errno();
1183 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1184 == -1) {
1185 err = got_error_from_errno();
1186 close(fd);
1187 return err;
1190 return flush_imsg(ibuf);
1193 const struct got_error *
1194 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1195 struct got_object_id *id)
1197 struct got_imsg_packed_object iobj;
1199 iobj.idx = idx;
1200 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1202 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1203 &iobj, sizeof(iobj)) == -1)
1204 return got_error_from_errno();
1206 return flush_imsg(ibuf);
1209 const struct got_error *
1210 got_privsep_unveil_exec_helpers(void)
1212 if (unveil(GOT_PATH_PROG_READ_PACK, "x") != 0 ||
1213 unveil(GOT_PATH_PROG_READ_OBJECT, "x") != 0 ||
1214 unveil(GOT_PATH_PROG_READ_COMMIT, "x") != 0 ||
1215 unveil(GOT_PATH_PROG_READ_TREE, "x") != 0 ||
1216 unveil(GOT_PATH_PROG_READ_BLOB, "x") != 0 ||
1217 unveil(GOT_PATH_PROG_READ_TAG, "x") != 0)
1218 return got_error_from_errno();
1220 return NULL;