XBPS Library API 20240111
The X Binary Package System
transaction_internalize.c
1/*-
2 * Copyright (c) 2021 Duncan Overbruck <mail@duncano.de>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25#include <errno.h>
26#include <fcntl.h>
27#include <limits.h>
28#include <stdlib.h>
29#include <string.h>
30
31#include <archive.h>
32#include <archive_entry.h>
33
34#include "xbps_api_impl.h"
35
36static int
37internalize_script(xbps_dictionary_t pkg_repod, const char *script,
38 struct archive *ar, struct archive_entry *entry)
39{
40 char buffer[BUFSIZ];
41 xbps_data_t data = NULL;
42 char *buf = NULL;
43 int64_t entry_size = archive_entry_size(entry);
44
45 if (entry_size == 0)
46 return 0;
47 if (entry_size < 0)
48 return -EINVAL;
49
50 if ((size_t)entry_size > sizeof buffer) {
51 buf = malloc(entry_size);
52 if (buf == NULL)
53 return -errno;
54 }
55
56 if (archive_read_data(ar, buf != NULL ? buf : buffer, entry_size) != entry_size) {
57 free(buf);
58 return -errno;
59 }
60
61 data = xbps_data_create_data(buf != NULL ? buf : buffer, entry_size);
62 if (data == NULL) {
63 free(buf);
64 return -errno;
65 }
66
67 free(buf);
68 xbps_dictionary_set(pkg_repod, script, data);
69 xbps_object_release(data);
70 return 0;
71}
72
73static int
74internalize_binpkg(struct xbps_handle *xhp, xbps_dictionary_t pkg_repod)
75{
76 char pkgfile[PATH_MAX];
77 xbps_dictionary_t filesd = NULL, propsd = NULL;
78 struct stat st;
79 struct archive *ar = NULL;
80 struct archive_entry *entry;
81 const char *pkgver, *pkgname, *binpkg_pkgver;
82 ssize_t l;
83 int pkg_fd = -1;
84 int rv = 0;
85
86 xbps_dictionary_get_cstring_nocopy(pkg_repod, "pkgver", &pkgver);
87 assert(pkgver);
88 xbps_dictionary_get_cstring_nocopy(pkg_repod, "pkgname", &pkgname);
89 assert(pkgname);
90
91 l = xbps_pkg_path(xhp, pkgfile, sizeof(pkgfile), pkg_repod);
92 if (l < 0)
93 return l;
94
95 if ((ar = archive_read_new()) == NULL)
96 return -errno;
97
98 /*
99 * Enable support for tar format and gzip/bzip2/lzma compression methods.
100 */
101 archive_read_support_filter_gzip(ar);
102 archive_read_support_filter_bzip2(ar);
103 archive_read_support_filter_xz(ar);
104 archive_read_support_filter_lz4(ar);
105 archive_read_support_filter_zstd(ar);
106 archive_read_support_format_tar(ar);
107
108 pkg_fd = open(pkgfile, O_RDONLY|O_CLOEXEC);
109 if (pkg_fd == -1) {
110 rv = -errno;
111 xbps_set_cb_state(xhp, XBPS_STATE_FILES_FAIL,
112 -rv, pkgver,
113 "%s: failed to open binary package `%s': %s",
114 pkgver, pkgfile, strerror(rv));
115 goto out;
116 }
117 if (fstat(pkg_fd, &st) == -1) {
118 rv = -errno;
119 xbps_set_cb_state(xhp, XBPS_STATE_FILES_FAIL,
120 -rv, pkgver,
121 "%s: failed to fstat binary package `%s': %s",
122 pkgver, pkgfile, strerror(rv));
123 goto out;
124 }
125 if (archive_read_open_fd(ar, pkg_fd, st.st_blksize) == ARCHIVE_FATAL) {
126 rv = archive_errno(ar);
127 xbps_set_cb_state(xhp, XBPS_STATE_FILES_FAIL,
128 -rv, pkgver,
129 "%s: failed to read binary package `%s': %s",
130 pkgver, pkgfile, strerror(rv));
131 goto out;
132 }
133
134 for (uint8_t i = 0; i < 4; i++) {
135 const char *entry_pname;
136 int ar_rv = archive_read_next_header(ar, &entry);
137 if (ar_rv == ARCHIVE_EOF || ar_rv == ARCHIVE_FATAL)
138 break;
139 else if (ar_rv == ARCHIVE_RETRY)
140 continue;
141
142 entry_pname = archive_entry_pathname(entry);
143
144 if (strcmp("./INSTALL", entry_pname) == 0) {
145 rv = internalize_script(pkg_repod, "install-script", ar, entry);
146 if (rv < 0)
147 goto out;
148 } else if (strcmp("./REMOVE", entry_pname) == 0) {
149 rv = internalize_script(pkg_repod, "remove-script", ar, entry);
150 if (rv < 0)
151 goto out;
152 } else if ((strcmp("./files.plist", entry_pname)) == 0) {
153 filesd = xbps_archive_get_dictionary(ar, entry);
154 if (filesd == NULL) {
155 rv = -EINVAL;
156 goto out;
157 }
158 } else if (strcmp("./props.plist", entry_pname) == 0) {
159 propsd = xbps_archive_get_dictionary(ar, entry);
160 if (propsd == NULL) {
161 rv = -EINVAL;
162 goto out;
163 }
164 } else {
165 break;
166 }
167 }
168
169 /*
170 * Bail out if required metadata files are not in archive.
171 */
172 if (propsd == NULL || filesd == NULL) {
173 rv = -ENODEV;
174 xbps_set_cb_state(xhp, XBPS_STATE_FILES_FAIL, -rv, pkgver,
175 "%s: [files] invalid binary package `%s'.", pkgver, pkgfile);
176 goto out;
177 }
178
179 /*
180 * Bail out if repo pkgver does not match binpkg pkgver, i.e. downgrade attack
181 * by advertising a old signed package with a new version.
182 */
183 xbps_dictionary_get_cstring_nocopy(propsd, "pkgver", &binpkg_pkgver);
184 if (strcmp(pkgver, binpkg_pkgver) != 0) {
185 rv = -EINVAL;
186 xbps_set_cb_state(xhp, XBPS_STATE_FILES_FAIL, -rv, pkgver,
187 "%s: [files] pkgver mismatch repodata: `%s' binpkg: `%s'.",
188 pkgfile, pkgver, binpkg_pkgver);
189 goto out;
190 }
191
192out:
193 xbps_object_release(propsd);
194 xbps_object_release(filesd);
195 if (pkg_fd != -1)
196 close(pkg_fd);
197 if (ar != NULL)
198 archive_read_free(ar);
199 return rv;
200}
201
202int
203xbps_transaction_internalize(struct xbps_handle *xhp, xbps_object_iterator_t iter)
204{
205 xbps_object_t obj;
206
207 assert(xhp);
208 assert(iter);
209
210 while ((obj = xbps_object_iterator_next(iter)) != NULL) {
211 xbps_trans_type_t ttype;
212 int rv;
213
214 ttype = xbps_transaction_pkg_type(obj);
215 if (ttype != XBPS_TRANS_INSTALL && ttype != XBPS_TRANS_UPDATE)
216 continue;
217
218 rv = internalize_binpkg(xhp, obj);
219 if (rv < 0)
220 return rv;
221 }
222 xbps_object_iterator_reset(iter);
223
224 return 0;
225}
Generic XBPS structure handler for initialization.
Definition xbps.h:550
xbps_trans_type_t xbps_transaction_pkg_type(xbps_dictionary_t pkg_repod)
xbps_trans_type_t
Definition xbps.h:1320
ssize_t xbps_pkg_path(struct xbps_handle *xhp, char *dst, size_t dstsz, xbps_dictionary_t pkgd)
Definition util.c:325