Fix segfaults on opening invalid archive files
[pacman-ng.git] / lib / libalpm / be_package.c
blob3188a2fed40714556459d230e8a3b8c5555de75c
1 /*
2 * be_package.c : backend for packages
4 * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
28 /* libarchive */
29 #include <archive.h>
30 #include <archive_entry.h>
32 /* libalpm */
33 #include "alpm_list.h"
34 #include "alpm.h"
35 #include "util.h"
36 #include "log.h"
37 #include "handle.h"
38 #include "package.h"
39 #include "deps.h" /* _alpm_splitdep */
41 struct package_changelog {
42 struct archive *archive;
43 int fd;
46 /**
47 * Open a package changelog for reading. Similar to fopen in functionality,
48 * except that the returned 'file stream' is from an archive.
49 * @param pkg the package (file) to read the changelog
50 * @return a 'file stream' to the package changelog
52 static void *_package_changelog_open(alpm_pkg_t *pkg)
54 ASSERT(pkg != NULL, return NULL);
56 struct package_changelog *changelog;
57 struct archive *archive;
58 struct archive_entry *entry;
59 const char *pkgfile = pkg->origin_data.file;
60 struct stat buf;
61 int fd;
63 fd = _alpm_open_archive(pkg->handle, pkgfile, &buf,
64 &archive, ALPM_ERR_PKG_OPEN);
65 if(fd < 0) {
66 return NULL;
69 while(archive_read_next_header(archive, &entry) == ARCHIVE_OK) {
70 const char *entry_name = archive_entry_pathname(entry);
72 if(strcmp(entry_name, ".CHANGELOG") == 0) {
73 changelog = malloc(sizeof(struct package_changelog));
74 if(!changelog) {
75 pkg->handle->pm_errno = ALPM_ERR_MEMORY;
76 archive_read_finish(archive);
77 CLOSE(fd);
78 return NULL;
80 changelog->archive = archive;
81 changelog->fd = fd;
82 return changelog;
85 /* we didn't find a changelog */
86 archive_read_finish(archive);
87 CLOSE(fd);
88 errno = ENOENT;
90 return NULL;
93 /**
94 * Read data from an open changelog 'file stream'. Similar to fread in
95 * functionality, this function takes a buffer and amount of data to read.
96 * @param ptr a buffer to fill with raw changelog data
97 * @param size the size of the buffer
98 * @param pkg the package that the changelog is being read from
99 * @param fp a 'file stream' to the package changelog
100 * @return the number of characters read, or 0 if there is no more data
102 static size_t _package_changelog_read(void *ptr, size_t size,
103 const alpm_pkg_t UNUSED *pkg, void *fp)
105 struct package_changelog *changelog = fp;
106 ssize_t sret = archive_read_data(changelog->archive, ptr, size);
107 /* Report error (negative values) */
108 if(sret < 0) {
109 RET_ERR(pkg->handle, ALPM_ERR_LIBARCHIVE, 0);
110 } else {
111 return (size_t)sret;
116 * Close a package changelog for reading. Similar to fclose in functionality,
117 * except that the 'file stream' is from an archive.
118 * @param pkg the package (file) that the changelog was read from
119 * @param fp a 'file stream' to the package changelog
120 * @return whether closing the package changelog stream was successful
122 static int _package_changelog_close(const alpm_pkg_t UNUSED *pkg, void *fp)
124 int ret;
125 struct package_changelog *changelog = fp;
126 ret = archive_read_finish(changelog->archive);
127 CLOSE(changelog->fd);
128 free(changelog);
129 return ret;
132 /** Package file operations struct accessor. We implement this as a method
133 * rather than a static struct as in be_files because we want to reuse the
134 * majority of the default_pkg_ops struct and add only a few operations of
135 * our own on top.
137 static struct pkg_operations *get_file_pkg_ops(void)
139 static struct pkg_operations file_pkg_ops;
140 static int file_pkg_ops_initialized = 0;
141 if(!file_pkg_ops_initialized) {
142 file_pkg_ops = default_pkg_ops;
143 file_pkg_ops.changelog_open = _package_changelog_open;
144 file_pkg_ops.changelog_read = _package_changelog_read;
145 file_pkg_ops.changelog_close = _package_changelog_close;
146 file_pkg_ops_initialized = 1;
148 return &file_pkg_ops;
152 * Parses the package description file for a package into a alpm_pkg_t struct.
153 * @param archive the archive to read from, pointed at the .PKGINFO entry
154 * @param newpkg an empty alpm_pkg_t struct to fill with package info
156 * @return 0 on success, -1 on error
158 static int parse_descfile(alpm_handle_t *handle, struct archive *a, alpm_pkg_t *newpkg)
160 char *ptr = NULL;
161 char *key = NULL;
162 int ret, linenum = 0;
163 struct archive_read_buffer buf;
165 memset(&buf, 0, sizeof(buf));
166 /* 512K for a line length seems reasonable */
167 buf.max_line_size = 512 * 1024;
169 /* loop until we reach EOF or other error */
170 while((ret = _alpm_archive_fgets(a, &buf)) == ARCHIVE_OK) {
171 size_t len = _alpm_strip_newline(buf.line);
173 linenum++;
174 key = buf.line;
175 if(len == 0 || key[0] == '#') {
176 continue;
178 /* line is always in this format: "key = value"
179 * we can be sure the " = " exists, so look for that */
180 ptr = memchr(key, ' ', len);
181 if(!ptr || (size_t)(ptr - key + 2) > len || memcmp(ptr, " = ", 3) != 0) {
182 _alpm_log(handle, ALPM_LOG_DEBUG,
183 "%s: syntax error in description file line %d\n",
184 newpkg->name ? newpkg->name : "error", linenum);
185 } else {
186 /* NULL the end of the key portion, move ptr to start of value */
187 *ptr = '\0';
188 ptr += 3;
189 if(strcmp(key, "pkgname") == 0) {
190 STRDUP(newpkg->name, ptr, return -1);
191 newpkg->name_hash = _alpm_hash_sdbm(newpkg->name);
192 } else if(strcmp(key, "pkgbase") == 0) {
193 /* not used atm */
194 } else if(strcmp(key, "pkgver") == 0) {
195 STRDUP(newpkg->version, ptr, return -1);
196 } else if(strcmp(key, "pkgdesc") == 0) {
197 STRDUP(newpkg->desc, ptr, return -1);
198 } else if(strcmp(key, "group") == 0) {
199 newpkg->groups = alpm_list_add(newpkg->groups, strdup(ptr));
200 } else if(strcmp(key, "url") == 0) {
201 STRDUP(newpkg->url, ptr, return -1);
202 } else if(strcmp(key, "license") == 0) {
203 newpkg->licenses = alpm_list_add(newpkg->licenses, strdup(ptr));
204 } else if(strcmp(key, "builddate") == 0) {
205 newpkg->builddate = _alpm_parsedate(ptr);
206 } else if(strcmp(key, "packager") == 0) {
207 STRDUP(newpkg->packager, ptr, return -1);
208 } else if(strcmp(key, "arch") == 0) {
209 STRDUP(newpkg->arch, ptr, return -1);
210 } else if(strcmp(key, "size") == 0) {
211 /* size in the raw package is uncompressed (installed) size */
212 newpkg->isize = _alpm_strtoofft(ptr);
213 } else if(strcmp(key, "depend") == 0) {
214 alpm_depend_t *dep = _alpm_splitdep(ptr);
215 newpkg->depends = alpm_list_add(newpkg->depends, dep);
216 } else if(strcmp(key, "optdepend") == 0) {
217 newpkg->optdepends = alpm_list_add(newpkg->optdepends, strdup(ptr));
218 } else if(strcmp(key, "conflict") == 0) {
219 alpm_depend_t *conflict = _alpm_splitdep(ptr);
220 newpkg->conflicts = alpm_list_add(newpkg->conflicts, conflict);
221 } else if(strcmp(key, "replaces") == 0) {
222 alpm_depend_t *replace = _alpm_splitdep(ptr);
223 newpkg->replaces = alpm_list_add(newpkg->replaces, replace);
224 } else if(strcmp(key, "provides") == 0) {
225 alpm_depend_t *provide = _alpm_splitdep(ptr);
226 newpkg->provides = alpm_list_add(newpkg->provides, provide);
227 } else if(strcmp(key, "backup") == 0) {
228 alpm_backup_t *backup;
229 CALLOC(backup, 1, sizeof(alpm_backup_t), return -1);
230 STRDUP(backup->name, ptr, return -1);
231 newpkg->backup = alpm_list_add(newpkg->backup, backup);
232 } else if(strcmp(key, "force") == 0) {
233 /* deprecated, skip it */
234 } else if(strcmp(key, "makepkgopt") == 0) {
235 /* not used atm */
236 } else {
237 _alpm_log(handle, ALPM_LOG_DEBUG, "%s: unknown key '%s' in description file line %d\n",
238 newpkg->name ? newpkg->name : "error", key, linenum);
242 if(ret != ARCHIVE_EOF) {
243 _alpm_log(handle, ALPM_LOG_DEBUG, "error parsing package descfile\n");
244 return -1;
247 return 0;
250 static void files_merge(alpm_file_t a[], alpm_file_t b[], alpm_file_t c[],
251 size_t m, size_t n)
253 size_t i = 0, j = 0, k = 0;
254 while(i < m && j < n) {
255 if(strcmp(a[i].name, b[j].name) < 0) {
256 c[k++] = a[i++];
257 } else {
258 c[k++] = b[j++];
261 while(i < m) {
262 c[k++] = a[i++];
264 while(j < n) {
265 c[k++] = b[j++];
269 static alpm_file_t *files_msort(alpm_file_t *files, size_t n)
271 alpm_file_t *work;
272 size_t blocksize = 1;
274 CALLOC(work, n, sizeof(alpm_file_t), return NULL);
276 for(blocksize = 1; blocksize < n; blocksize *= 2) {
277 size_t i, max_extent = 0;
278 for(i = 0; i < n - blocksize; i += 2 * blocksize) {
279 /* this limits our actual merge to the length of the array, since we will
280 * not likely be a perfect power of two. */
281 size_t right_blocksize = blocksize;
282 if(i + blocksize * 2 > n) {
283 right_blocksize = n - i - blocksize;
285 files_merge(files + i, files + i + blocksize, work + i,
286 blocksize, right_blocksize);
287 max_extent = i + blocksize + right_blocksize;
289 /* ensure we only copy what we actually touched on this merge pass,
290 * no more, no less */
291 memcpy(files, work, max_extent * sizeof(alpm_file_t));
293 free(work);
294 return files;
298 * Validate a package.
299 * @param handle the context handle
300 * @param pkgfile path to the package file
301 * @param syncpkg package object to load verification data from (md5sum,
302 * sha256sum, and/or base64 signature)
303 * @param level the required level of signature verification
304 * @param sigdata signature data from the package to pass back
305 * @return 0 if package is fully valid, -1 and pm_errno otherwise
307 int _alpm_pkg_validate_internal(alpm_handle_t *handle,
308 const char *pkgfile, alpm_pkg_t *syncpkg, alpm_siglevel_t level,
309 alpm_siglist_t **sigdata)
311 int has_sig;
312 handle->pm_errno = 0;
314 if(pkgfile == NULL || strlen(pkgfile) == 0) {
315 RET_ERR(handle, ALPM_ERR_WRONG_ARGS, -1);
318 /* attempt to access the package file, ensure it exists */
319 if(access(pkgfile, R_OK) != 0) {
320 RET_ERR(handle, ALPM_ERR_PKG_NOT_FOUND, -1);
323 /* can we get away with skipping checksums? */
324 has_sig = 0;
325 if(level & ALPM_SIG_PACKAGE) {
326 if(syncpkg && syncpkg->base64_sig) {
327 has_sig = 1;
328 } else {
329 char *sigpath = _alpm_sigpath(handle, pkgfile);
330 if(sigpath && !_alpm_access(handle, NULL, sigpath, R_OK)) {
331 has_sig = 1;
333 free(sigpath);
337 if(syncpkg && !has_sig) {
338 if(syncpkg->md5sum && !syncpkg->sha256sum) {
339 _alpm_log(handle, ALPM_LOG_DEBUG, "md5sum: %s\n", syncpkg->md5sum);
340 _alpm_log(handle, ALPM_LOG_DEBUG, "checking md5sum for %s\n", pkgfile);
341 if(_alpm_test_checksum(pkgfile, syncpkg->md5sum, ALPM_CSUM_MD5) != 0) {
342 RET_ERR(handle, ALPM_ERR_PKG_INVALID_CHECKSUM, -1);
346 if(syncpkg->sha256sum) {
347 _alpm_log(handle, ALPM_LOG_DEBUG, "sha256sum: %s\n", syncpkg->sha256sum);
348 _alpm_log(handle, ALPM_LOG_DEBUG, "checking sha256sum for %s\n", pkgfile);
349 if(_alpm_test_checksum(pkgfile, syncpkg->sha256sum, ALPM_CSUM_SHA256) != 0) {
350 RET_ERR(handle, ALPM_ERR_PKG_INVALID_CHECKSUM, -1);
355 /* even if we don't have a sig, run the check code if level tells us to */
356 if(has_sig || level & ALPM_SIG_PACKAGE) {
357 const char *sig = syncpkg ? syncpkg->base64_sig : NULL;
358 _alpm_log(handle, ALPM_LOG_DEBUG, "sig data: %s\n", sig ? sig : "<from .sig>");
359 if(_alpm_check_pgp_helper(handle, pkgfile, sig,
360 level & ALPM_SIG_PACKAGE_OPTIONAL, level & ALPM_SIG_PACKAGE_MARGINAL_OK,
361 level & ALPM_SIG_PACKAGE_UNKNOWN_OK, sigdata)) {
362 handle->pm_errno = ALPM_ERR_PKG_INVALID_SIG;
363 return -1;
367 return 0;
371 * Load a package and create the corresponding alpm_pkg_t struct.
372 * @param handle the context handle
373 * @param pkgfile path to the package file
374 * @param full whether to stop the load after metadata is read or continue
375 * through the full archive
377 alpm_pkg_t *_alpm_pkg_load_internal(alpm_handle_t *handle,
378 const char *pkgfile, int full)
380 int ret, fd, config = 0;
381 struct archive *archive;
382 struct archive_entry *entry;
383 alpm_pkg_t *newpkg;
384 struct stat st;
385 size_t files_count = 0, files_size = 0;
386 alpm_file_t *files = NULL;
388 if(pkgfile == NULL || strlen(pkgfile) == 0) {
389 RET_ERR(handle, ALPM_ERR_WRONG_ARGS, NULL);
392 fd = _alpm_open_archive(handle, pkgfile, &st, &archive, ALPM_ERR_PKG_OPEN);
393 if(fd < 0) {
394 if(errno == ENOENT) {
395 handle->pm_errno = ALPM_ERR_PKG_NOT_FOUND;
397 return NULL;
400 newpkg = _alpm_pkg_new();
401 if(newpkg == NULL) {
402 handle->pm_errno = ALPM_ERR_MEMORY;
403 goto error;
405 STRDUP(newpkg->filename, pkgfile,
406 handle->pm_errno = ALPM_ERR_MEMORY; goto error);
407 newpkg->size = st.st_size;
409 _alpm_log(handle, ALPM_LOG_DEBUG, "starting package load for %s\n", pkgfile);
411 /* If full is false, only read through the archive until we find our needed
412 * metadata. If it is true, read through the entire archive, which serves
413 * as a verfication of integrity and allows us to create the filelist. */
414 while((ret = archive_read_next_header(archive, &entry)) == ARCHIVE_OK) {
415 const char *entry_name = archive_entry_pathname(entry);
417 if(strcmp(entry_name, ".PKGINFO") == 0) {
418 /* parse the info file */
419 if(parse_descfile(handle, archive, newpkg) != 0) {
420 _alpm_log(handle, ALPM_LOG_ERROR, _("could not parse package description file in %s\n"),
421 pkgfile);
422 goto pkg_invalid;
424 if(newpkg->name == NULL || strlen(newpkg->name) == 0) {
425 _alpm_log(handle, ALPM_LOG_ERROR, _("missing package name in %s\n"), pkgfile);
426 goto pkg_invalid;
428 if(newpkg->version == NULL || strlen(newpkg->version) == 0) {
429 _alpm_log(handle, ALPM_LOG_ERROR, _("missing package version in %s\n"), pkgfile);
430 goto pkg_invalid;
432 config = 1;
433 continue;
434 } else if(strcmp(entry_name, ".INSTALL") == 0) {
435 newpkg->scriptlet = 1;
436 } else if(*entry_name == '.') {
437 /* for now, ignore all files starting with '.' that haven't
438 * already been handled (for future possibilities) */
439 } else if(full) {
440 /* Keep track of all files for filelist generation */
441 if(files_count >= files_size) {
442 size_t old_size = files_size;
443 if(files_size == 0) {
444 files_size = 4;
445 } else {
446 files_size *= 2;
448 files = realloc(files, sizeof(alpm_file_t) * files_size);
449 if(!files) {
450 ALLOC_FAIL(sizeof(alpm_file_t) * files_size);
451 goto error;
453 /* ensure all new memory is zeroed out, in both the initial
454 * allocation and later reallocs */
455 memset(files + old_size, 0,
456 sizeof(alpm_file_t) * (files_size - old_size));
458 STRDUP(files[files_count].name, entry_name, goto error);
459 files[files_count].size = archive_entry_size(entry);
460 files[files_count].mode = archive_entry_mode(entry);
461 files_count++;
464 if(archive_read_data_skip(archive)) {
465 _alpm_log(handle, ALPM_LOG_ERROR, _("error while reading package %s: %s\n"),
466 pkgfile, archive_error_string(archive));
467 handle->pm_errno = ALPM_ERR_LIBARCHIVE;
468 goto error;
471 /* if we are not doing a full read, see if we have all we need */
472 if(!full && config) {
473 break;
477 if(ret != ARCHIVE_EOF && ret != ARCHIVE_OK) { /* An error occured */
478 _alpm_log(handle, ALPM_LOG_ERROR, _("error while reading package %s: %s\n"),
479 pkgfile, archive_error_string(archive));
480 handle->pm_errno = ALPM_ERR_LIBARCHIVE;
481 goto error;
484 if(!config) {
485 _alpm_log(handle, ALPM_LOG_ERROR, _("missing package metadata in %s\n"), pkgfile);
486 goto pkg_invalid;
489 archive_read_finish(archive);
490 CLOSE(fd);
492 /* internal fields for package struct */
493 newpkg->origin = PKG_FROM_FILE;
494 newpkg->origin_data.file = strdup(pkgfile);
495 newpkg->ops = get_file_pkg_ops();
496 newpkg->handle = handle;
497 newpkg->infolevel = INFRQ_BASE | INFRQ_DESC | INFRQ_SCRIPTLET;
499 if(full) {
500 if(files) {
501 /* attempt to hand back any memory we don't need */
502 files = realloc(files, sizeof(alpm_file_t) * files_count);
503 /* "checking for conflicts" requires a sorted list, ensure that here */
504 _alpm_log(handle, ALPM_LOG_DEBUG,
505 "sorting package filelist for %s\n", pkgfile);
506 newpkg->files.files = files_msort(files, files_count);
508 newpkg->files.count = files_count;
509 newpkg->infolevel |= INFRQ_FILES;
512 return newpkg;
514 pkg_invalid:
515 handle->pm_errno = ALPM_ERR_PKG_INVALID;
516 error:
517 _alpm_pkg_free(newpkg);
518 archive_read_finish(archive);
519 if(fd >= 0) {
520 CLOSE(fd);
523 return NULL;
526 int SYMEXPORT alpm_pkg_load(alpm_handle_t *handle, const char *filename, int full,
527 alpm_siglevel_t level, alpm_pkg_t **pkg)
529 CHECK_HANDLE(handle, return -1);
530 ASSERT(pkg != NULL, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, -1));
532 if(_alpm_pkg_validate_internal(handle, filename, NULL, level, NULL) == -1) {
533 /* pm_errno is set by pkg_validate */
534 return -1;
536 *pkg = _alpm_pkg_load_internal(handle, filename, full);
537 if(*pkg == NULL) {
538 /* pm_errno is set by pkg_load */
539 return -1;
542 return 0;
545 /* vim: set ts=2 sw=2 noet: */