Commit Graph

411 Commits (917698ec1d4c9a79e3580cce4cbeb56fd4fa5a96)

Author SHA1 Message Date
Timo Teräs 1a4f2e94dd db: consider control characters in filename as malicious
Especially a newline can produce havoc in the database file as
the filename is written there as-is. This hardenes the extraction
to consider any control character as malicious. Additional
hardening is added to database loading to better detect corrupt
state and return proper error code about it.

Reported-by: Luca Weiss <luca@z3ntu.xyz>
2021-02-04 22:30:26 +02:00
thibault.ferrante 7a3e8f4214 database.c: Fixed package DESCRIPTION parsing
Regression introduced by 0fb0d30 which makes parsing a description
a critical failure.

[TT: Minor stylistic change. Fix also missing final line change
from the earlier commit]
2021-01-14 16:19:26 +02:00
thibault.ferrante 0fb0d30477 database: Propagate errors when loading an APKINDEX
In case of failure when loading an APKINDEX, no errors are
propagated to the user which may uncorrectly interpret the
current problem.
2021-01-11 11:26:49 +02:00
Timo Teräs 705e002bb0 rename adb_trust to apk_trust, and use it as package signature keystore too 2020-10-09 16:09:19 +03:00
Timo Teräs a627ab8eb8 make apk_database optional for applets that don't need it
The new v3 applets don't use it, and eventually all applets will
use the new formats only.

This prepares the code for this, and moves trust, id_cache, and
root_fd to apk_ctx which is enough for v3 applets at this time.

The generic code is made to not initialize apk_database if it's
not needed.
2020-10-09 16:09:19 +03:00
Timo Teräs 354713d2f7 rename apk_db_options to apk_ctx, rework logging
makes apk_verbosity non-global

fixes #10682
2020-10-09 16:09:19 +03:00
Timo Teräs 7a7eca8670 make apk_flags non-global, make progress printing state non-global
ref #10682
2020-10-09 16:09:19 +03:00
Timo Teräs 9afb4e8fd1 make apk_force non-global, remove left-over apk_arch
ref #10682
2020-10-09 16:09:19 +03:00
Timo Teräs b2af872fff db: convert repository list to a string array 2020-10-09 16:09:19 +03:00
Timo Teräs 6d71f49c11 io: make ostream_file always use tmpname 2020-10-09 16:09:19 +03:00
Timo Teräs efe0c4afec adb: introduce apk-tools database format, and few applets
This is a flat buffers inspired format that allows fast
mmaped access to the data with low overhead, signature support
and relatively good forward support.
2020-10-09 16:09:19 +03:00
Timo Teräs 5f66b618ef various changes to make clang not give warnings 2020-10-07 21:16:35 +03:00
Timo Teräs 3fae0c693f db: make the --repositories-file change more announced
Document the version when changed. And print error with similar note
if the given repositories-file cannot be read.
2020-10-06 14:27:59 +03:00
Paul Spooren dac30d5049 database: automatically create missing cache dir
On some systems the `/var/` dir is mounted in a tmpfs which is reseted
after each reboot. For that reason no post-install script can handle the
creation of the cache dir at `/var/cache/apk`.

Check on database opnening if the folder is available, if not create it.
Fixes #10715

Signed-off-by: Paul Spooren <mail@aparcar.org>
2020-10-06 10:02:21 +03:00
Timo Teräs 7c2a3657fb db: make --repositories-file relative to host root
It used to be relative to the --root specified root, but that
causes issues with relative command line filenames and is unintuitive.
Update documentation accordingly. Fixes #10702.
2020-10-05 16:49:57 +03:00
Timo Teräs 6cedfe27ac db: mask password component of printed URLs
fixes #10710
2020-10-05 12:58:46 +03:00
Timo Teräs c269e9c24d db: check cache only if some repositories are enabled 2020-10-01 14:31:41 +03:00
Ariadne Conill fa63e46a75 database: clarify the deprecation notice for checksum-less packages
Closes #10708.
2020-08-29 20:37:57 +03:00
Ariadne Conill f4c3f0ad2a database: create additional device nodes when initializing root fs
Some triggers and maintainer scripts depend on /dev/random, /dev/urandom,
/dev/zero and /dev/console.
2020-05-27 08:29:01 +03:00
Timo Teräs d0edeec8fb make the atom functions not use global state
This greatly helps with memory management on applications that
may want to daemonize and open/close database several times.

Also the lifetime and "owner" of memory for all data is now
explicitly bound to owning struct apk_database, which might
be helpful when writing language bindings. As side effect, the
interned "atoms" are unique only within what apk_database, so
comparing packages from different apk_database may not work
as expected.

Fixes #10697
2020-05-19 12:02:56 +03:00
TBK 5d796b5678 use SPDX-License-Identifier in source files 2020-05-07 10:45:42 +03:00
Fredrik Gustafsson d61c009f7a apk: do not manage file ownership as non-root or when asked so
If apk is run as a non-root user, it's not possible to chown files.

Maintainers note: minor wording changes on commit log and man page.

Signed-off-by: Fredrik Gustafsson <fredrigu@axis.com>
2020-04-28 14:48:49 +03:00
Timo Teräs 845b6397ab db: fix database reading without scripts or triggers
Most notably this happens after blank database is initialized with --initdb,
but can happen also in other conditions.

The error checking changes modified behaviour if the file does not exist.
Explicitly check for ENOENT and ignore it. But the behaviour is improved
from earlier as now e.g. EPERM will be detected and reported correctly.

Fixes #10679

Fixes: 6da3e8eb15 "istream, archive, db: convert db and tar function to use istream"
2020-04-01 10:47:54 +03:00
Timo Teräs c054fbc11e db: fix unaligned memory access in csum_hash() 2020-02-21 09:33:58 +02:00
Timo Teräs 72be813930 io: add stream copy helper 2020-02-14 16:45:13 +02:00
Timo Teräs 45d313c51c remove apk_time() as it is causing problems with shared objects
Instead, to make sure test mode produces same output, redefine
time() for the test mode binary.

Reverts parts of 0b82bcc53e.
2020-02-04 10:31:10 +02:00
Reid Rankin aa882df511 document mysterious SHA-1 blob in database.c 2020-01-25 16:33:54 +02:00
Timo Teräs 9a76f0d6a6 db: additional clean up and hardening for apk extraction
This enforces all scripts to be in the control block, and
all data files to be in data block. Ignoring of dot files in
root is added back: packages without any real files will
ship one ".dummy" item in the data block to trigger processing
and validation to work.
2020-01-24 10:39:01 +02:00
Reid Rankin d25e5e3879 Harden signature verification process
This mostly boils down to making sure control_started and
data_started are consistently used to gate actions, instead of
relying whether on file names start with a '.'.

None of the weaknesses this fixes are exploitable, but they
might have become so after changes to seemingly-unrelated code,
so it's good to clean them up.
2020-01-24 09:28:48 +02:00
Timo Teräs f123d77e0b archive: make apk_tar_parse check and close input stream
simplifies other code quite a bit
2020-01-11 11:32:51 +02:00
Timo Teräs 7caa217731 convert remaining locations to use istream instead of bstream 2020-01-11 11:20:48 +02:00
Timo Teräs 6da3e8eb15 istream, archive, db: convert db and tar function to use istream 2020-01-11 11:20:45 +02:00
Timo Teräs 9dda2d3c21 archive: remove support for old packages without xattr checksums 2020-01-11 03:43:38 +02:00
Drew DeVault 7e2e440d4c database.c: drop GNU extension for fnmatch
The non-wildcard version of the function is case-sensitive anyway.
2020-01-09 08:47:31 +02:00
Timo Teräs e39334e44f io: remove unused size parameter from bstream close 2020-01-06 00:27:17 +02:00
Timo Teräs 063c3acbbd db: parse tabulator as repository separator char
Fixes #10662
2019-12-27 17:06:23 +02:00
Fredrik Gustafsson cfa8b736f8 Don't unpack files to system root
When unpacking a file that is in root, it got a temporary file
name /.apk...  however if the --root option was used it should
have the name root/.apk... otherwise unpacking will fail if the
user does not have write access to /.

Signed-off-by: Fredrik Gustafsson <fredrigu@axis.com>
2019-11-22 14:41:55 +02:00
Timo Teräs 1c47f37443 use fixed system time in test mode to have fixed test output
fixes test suite regression from previous commit
2019-06-03 16:19:17 +03:00
Timo Teräs 44daf80873 fix strncpy bounds errors
error: 'strncpy' specified bound 4096 equals destination size [-Werror=stringop-truncation]

Based on patch by Elan Ruusamäe <glen@delfi.ee>
2019-02-13 16:05:27 +02:00
Elan Ruusamäe 86922d1a34 include sys/sysmacros.h for makedev definition 2019-02-13 15:32:22 +02:00
Timo Teräs 47570f80e9 prefer selecting packages by their primary name 2018-11-12 14:16:36 +02:00
Timo Teräs beab8545eb add support for openssl 1.1 2018-10-26 08:22:08 +03:00
Timo Teräs 6484ed9849 rework unpacking of packages and harden package file format requirements
A crafted .apk file could to trick apk writing unverified data to
an unexpected file during temporary file creation due to bugs in handling
long link target name and the way a regular file is extracted.

Several hardening steps are implemented to avoid this:
 - the temporary file is now always first unlinked (apk thus reserved
   all filenames .apk.* to be it's working files)
 - the temporary file is after that created with O_EXCL to avoid races
 - the temporary file is no longer directly the archive entry name
   and thus directly controlled by potentially untrusted data
 - long file names and link target names are now rejected
 - hard link targets are now more rigorously checked
 - various additional checks added for the extraction process to
   error out early in case of malformed (or old legacy) file

Reported-by: Max Justicz <max@justi.cz>
2018-09-10 10:59:39 +03:00
Natanael Copa 13c534db77 prevent automatic repository index update with --no-network
We should not update repository index when --no-network is specified.

ref #9126
2018-08-21 12:25:37 +00:00
Timo Teräs e1a05c74cb prevent automatic repository index update for 'apk del'
ref #9063
2018-07-18 13:29:13 +03:00
Timo Teräs 258519b1cd db: fix refreshing index if time is zero
During netboot on systems without RTC, time() will be near zero,
and the index fill not exist. Thus the plain test of st.st_mtime
against system time failed. Verify that fstatat() succeeds.
2018-04-05 09:57:17 +03:00
Timo Teräs 5f7dcadc69 auto-update index only when database is opened for writing 2018-01-08 11:56:48 +02:00
Timo Teräs 8a28c6d0d4 enable automatic update of indexes controlled by --cache-max-age
This modifies apk cache for indexes to be automatically refreshed
periodically without explicit 'update' or '--update-cache' usage.

The default is to do if-modified-since request if the local copy
is older than 4 hours. This age can be changed with --cache-max-age.
Using --update-cache will change this age to 60 seconds to make
sure the cached copy is relatively new. The small age is in order
to try to avoid downloading indexes second time when apk-tools is
upgraded and apk re-execs after self-upgrade.

Accordingly using explicitly 'apk update' will now enforce
--force-refresh and request the very latest index by requesting
any potential http proxy to do refresh too.
2018-01-04 10:56:09 +02:00
Timo Teräs 039ff3bd46 split --force to several --force-[type] options
This unloads --force as several of the things are really not wanted
together. E.g. --force-refresh is a lot different from --force-broken-world
and doing --force to get the other might introduce unwanted behaviour.

--force is still kept for backwards compatibility and it enables
most things --force was used for.
2018-01-03 16:00:38 +02:00
Timo Teräs b0fcc56f22 db: fix triggers to report deleted directories
This change just changes to keep deleted directory items in
the hash with ref count zero and modified flag set. Those entries
are reused when needed. The side effect is that fire_triggers()
will now see those removed direcotries and reports them. Other
enumerators of the directories hash are protected to skip removed
directories when appropriate.
2018-01-03 09:31:10 +02:00
Timo Teräs 97e4d0531f db: handle default root correctly for /proc
dbopts->root may be null; use db->root instead

fixes #7162
2017-10-10 11:39:38 +03:00
A. Wilcox 13747fab83 database: include POSIX header for ST_* defines 2017-07-29 01:57:34 +00:00
Timo Teräs 51ec463e9d db: fix current directory instance caching in tar extraction
Depending how the directory entries are ordered, the cached dir
instance might not have been updated correctly. This has not been
a problem as the entries have been ordered, but is now triggered
on ppc.
2017-06-26 10:59:37 +03:00
A. Wilcox 17dd532e42 database: mark error on renameat failure 2017-06-26 10:32:38 +03:00
Timo Teräs 4d9c0c39b0 io: make io vtables const struct, and add accessors for them
This reduces function pointers in heap, and unifies how the
io functions are called.
2017-06-23 10:07:44 +03:00
Timo Teräs ede5165833 db: fix regression preventing remounting apk cache r/w
Introduced in commit c0f2d88f34.
fstatfs is needed to inspect the mount flags.
2017-05-25 18:06:27 +03:00
Kaarle Ritvanen 5c349335c7 db: separate init from open 2017-04-28 13:28:32 +03:00
Natanael Copa bcb4b06ff8 db: catch asprintf failure 2017-03-13 14:55:36 +01:00
Natanael Copa 93d6b2b529 db: prevent umount /proc when it was mounted by someone else
This fixes regression introduced by commit 72b878e655
2017-03-13 14:55:36 +01:00
Timo Teräs 6542d4ca2c db: allow overriding cache location 2017-02-27 11:14:33 +02:00
Timo Teräs c0f2d88f34 db: allow caching packages on tmpfs
fixes #5616

The original intention was not use unnecessary space on tmpfs
e.g. if the cache directory is a mount point, but accidentally
left unmounted. But there are valid cases when packages are
intentionally wanted to be cached on tmpfs. If caching is not
desired, the user can just remove the cache directory.
2017-02-27 11:05:01 +02:00
Timo Teräs 72b878e655 db: mount /proc for --root if it's not mounted, use mount syscall
musl c-library does not work properly without /proc, and potentially
running the scripts need this.
2017-02-27 10:52:16 +02:00
Henrik Riomar 349c61c961 add support for pre and post commit hooks
This allows for instance integration of etckeeper

[TT: Reorganized code a bit, and modified to use single
directory commit_hooks.d with argument for script of stage.]
2017-02-15 13:44:04 +02:00
Natanael Copa c43bfed8de db: add support for --no-cache
Implement --no-cache. The index is read directly from network and not
cached. This is useful for docker, where you install a set of packages
and directly after purge the cache. (see
1fc9e59d16/builder/scripts/apk-install)

fixes #4905
2015-12-07 12:50:32 +00:00
Timo Teräs cce4cff553 io, database: preserve [am]time for cached and fetched files
preserve [am]time for all packages and indexes. this fixes the caching
error that 'apk update' is after new index is generated, but before
the used mirror is synchronized. this caused local apkindex timestamp
to be newer than file in mirror, when in fact it was outdated index.

this also fixes fetched files to have build timestamp so that files
going to .iso or custom images have proper timestamps (rsync with
appropriate --modify-window now works)
2015-11-09 12:51:01 +02:00
Timo Teräs d99214ea55 db: stop extract more files if disk is full 2015-09-03 13:20:59 +03:00
Timo Teräs 57de8d0c5d relocate lock file to /lib/apk/db
the problem is that var/lock is on root installs symlink to /run/lock
(on tmpfs) and does not exist if doing chroot() to that root. fixes
apk to work when chrooted to existing rootfs install.
2015-07-02 09:56:50 +03:00
Timo Teräs 68453b99e6 remove db dir entry properly, so it can be recreated properly if needed
fixes #4261
2015-06-26 16:28:22 +03:00
Timo Teräs c4295e9cf8 for completeness free all arrays before exit
so valgrind does not report any leaks
2015-06-12 10:18:06 +03:00
Timo Teräs 4fab9290b6 speed improvements for database opening
resolve reverse dependencies after all packages have been loaded,
and avoid traversing the reverse name lists. now that we use
automatic virtual packages (soname, pkg-config, etc.) the reverse
dependency chains can become considerable longer than what it was
when the rdependency construction code was originally written.
2015-06-11 16:21:53 +03:00
Timo Teräs 37b65f1c01 fix directory permissions loading
commit 941fc1b1 uncovered a bug that directory permissions are
not updated properly at db load time if it's the default acl.
2015-06-01 11:22:58 +03:00
Timo Teräs 997aa99e3d do not extract files with malicious name
the security implications are not as high as compared to regular
tar/unzip archiver. this is because you are anyway trusting
the package to install files anywhere in the filesystem.

this serves rather as a sanity to check against errors in created
package.
2015-04-24 09:54:54 +03:00
Timo Teräs fd98e9c718 modifications for the previous commit in error message handling
forgot to --amend my changes
2015-04-24 09:32:22 +03:00
Timo Teräs 90e760339e make file install errors non-fatal, and xattr errors hidden
user xattrs on tmpfs are not supported no non-grsec kernels,
and many times root fs is mounted without user_xattr. Thus
to allow things to go smoothly on non-grsec kernels xattr
unsupported errors are now hidden.

xattrs can be fixed still now with "apk fix --xattrs"
2015-04-17 10:05:10 +03:00
Timo Teräs 3490ff789a fix tee io error handling
use ERR_PTR mechanism, and handle it at all places.
2015-04-13 09:42:27 +03:00
Timo Teräs 941fc1b10c make default dir/file acl implicit
apk in alpine 3.1 already supports this optimization, but kept
the database format. now is time to start using this feature.
2015-04-08 10:58:00 +03:00
Natanael Copa 944eae4b27 update: return failure if any mirror update failed
fixes #4040
2015-04-07 10:49:51 +02:00
Timo Teräs 8d1ec4c5bc calculate and store checksum of xattrs
ref #3027
2015-03-11 16:10:33 +02:00
Timo Teräs da6e455f70 rename file info related functions for consistency 2015-03-10 15:47:13 +02:00
Timo Teräs 255fd81d79 rework error handling for write streams 2015-03-10 13:15:58 +02:00
Timo Teräs 2a6896b2b4 rework error handling for read streams 2015-03-10 13:15:31 +02:00
Timo Teräs 4c1cdf5b9c remove old compat. field entry from installed db handling
Package pinning was first implemented with 'p' tag. However, it
was before any release renamed to 's', and 'p' was reserved for
package provides support for which is used now.
2015-02-04 11:54:08 +02:00
Timo Teräs cd6786bc30 always save 'world' with line feeds
using space was for backwards compatibility with apk-tools 2.2.2
and earlier (from January 2012)
2015-01-30 14:40:00 +02:00
Timo Teräs c553970061 remove support for old database location in /var
the location changed in apk-tools 2.1.0 (March 2011) which was
used in Alpine Linux 2.2.
2015-01-30 14:35:02 +02:00
Timo Teräs 02cd5a9c76 make del, fetch, fix and info return errors
In case all applet arguments are packages names (that is are not
including wildcards), return error if they do not match to some
package.
2014-12-08 08:31:41 +02:00
Kaarle Ritvanen f726e672c0 db: assign write permission to lock file
Allows running apk as an unprivileged user, e.g. with fakeroot.
Opening the lock file fails without the write permission.
2014-11-03 14:56:07 +02:00
Timo Teräs 4f823f2a50 make 'mode, uid, gid' triplet separate atomized struct
there are only few combinations for that triplet, and they
occur multiple times reducing the struct sizes a bit. make
sane defaults and prepare to not write defaults to disk
to reduce on-disk installed db size.
2014-11-01 20:18:57 +02:00
Timo Teräs bb1ac2557d db: improve and unify fdb error messages 2014-10-13 13:33:19 +03:00
Timo Teräs 82713dc389 db: unconditionally refresh index with --force 2014-10-09 19:32:48 +03:00
Timo Teräs 32627939f5 io,url,db: support for if-modified-since 2014-10-08 11:13:21 +03:00
Timo Teräs 5496560a4f db: fix crash if unable to download cache item
fixes #3371
2014-10-07 17:05:06 +03:00
Timo Teräs 09e48d8f06 db: rework directory permission handling
Apk used to reset directory permissions always, but this is undesirable
if user has modified the permissions - especially during tmpfs boot.
Though, it is desirable to update the permissions when packaging has
changed permissions, or a new package is installed and the merged
permission mask / owner changes.

Thus the new code updates the permissions only if:
 1) We are booting and directory is not in apkovl
 2) The directory is modified by a package install/remove/upgrade
 3) The filesystem directory permission matched database

Additionally "apk fix --directory-permissions" can be used to reset
all directory permissions to the database defaults.

Fixes #2966
2014-10-07 14:11:29 +03:00
Timo Teräs f5674b9582 db: use per-pkg architecture (if available) to form download url
Allows one arch index files to refer to other arch packages. Mostly
useful with noarch packages, but could be used e.g. to ship build
with some of packages optimized for specific cpu generation and
share most packages with the standard build.
2014-10-06 14:54:54 +03:00
Timo Teräs f911f7043b index: fix warning about package names without provider
It's real only if there's a package with actual dependency
(conflicts and install_if dependencies do not count).
2014-10-06 14:09:50 +03:00
Timo Teräs 1dc70477b5 solver: fix installation of non-repository packages during tmpfs boot
allow packages in the cache's installed to be selected for installation
by the solver. add test case for the issue.
2014-05-19 11:50:10 +03:00
Timo Teräs 73a03c2e40 db: allow replacing files of origin package is same
basically this makes packager's life simpler as there is no need
to list the subpkg names in replaces. this was also very error
prone and tedious job to do properly.
2014-04-25 22:17:49 +03:00
Timo Teräs d99e82c633 audit: include all files in etc/apk in overlay
This implements a new protected path flag '!' to include always
matching entries in the backup mode (overlay). This is also turned
on for etc/apk to include everything there in overlay, because
the full list of repositories and signing keys need to be in
overlay - just getting them from a package is not enough during
bootstrap.
2014-03-12 09:08:26 +02:00
Timo Teräs a6c4103df5 db: allow using cached packages with --no-network 2013-10-02 16:06:10 +03:00
Timo Teräs 74c4c95976 db, url: fix exec sentinels
http://ewontfix.com/11/
2013-10-01 16:22:29 +03:00
Timo Teräs 2721772488 db: minor improvements to an error and a verbose log messages 2013-09-20 21:54:33 +03:00