Commit Graph

8042 Commits

Author SHA1 Message Date
TW aca2021112
Merge pull request #7108 from pgerber/dotdot
Sanitize paths during archive creation and extraction
2023-06-10 14:38:57 +02:00
Thomas Waldmann 518c4fbca8
skip test_import_tar_with_dotdot for binary testing 2023-06-10 14:17:07 +02:00
eoli3n 095d5f463b improve patterns help: added declarative includes sample 2023-06-10 14:00:06 +02:00
eoli3n ba922d2e0a improve patterns help: define a pattern style 2023-06-10 14:00:06 +02:00
eoli3n 9bbb38d9ab improve patterns help 2023-06-10 14:00:06 +02:00
Thomas Waldmann b7ce3b1156
make sure we do not get backslashes into item paths
on windows, we also want slashes, not backslashes.
2023-06-10 12:52:00 +02:00
Thomas Waldmann db96c0c487
subclass MakePathSafeAction from Highlander 2023-06-10 11:41:31 +02:00
Peter Gerber 438cf2e7ef
Sanitize paths during archive creation/extraction/...
Paths are not always sanitized when creating an archive and,
more importantly, never when extracting one. The following example
shows how this can be used to attempt to write a file outside the
extraction directory:

$ echo abcdef | borg create -r ~/borg/a --stdin-name x/../../../../../etc/shadow archive-1 -
$ borg list -r ~/borg/a archive-1
-rw-rw---- root   root          7 Sun, 2022-10-23 19:14:27  x/../../../../../etc/shadow
$ mkdir borg/target
$ cd borg/target
$ borg extract -r ~/borg/a archive-1
x/../../../../../etc/shadow: makedirs: [Errno 13] Permission denied: '/home/user/borg/target/x/../../../../../etc'

Note that Borg tries to extract the file to /etc/shadow and the
permission error is a result of the user not having access.

This patch ensures file names are sanitized before archiving.
As for files extracted from the archive, paths are sanitized
by making all paths relative, removing '.' elements, and removing
superfluous slashes (as in '//'). '..' elements, however, are
rejected outright. The reasoning here is that it is easy to start
a path with './' or insert a '//' by accident (e.g. via --stdin-name
or import-tar). '..', however, seem unlikely to be the result
of an accident and could indicate a tampered repository.

With paths being sanitized as they are being read, this "errors"
will be corrected during the `borg transfer` required when upgrading
to Borg 2. Hence, the sanitation, when reading the archive,
can be removed once support for reading v1 repositories is dropped.
V2 repository will not contain non-sanitized paths. Of course,
a check for absolute paths and '..' elements needs to kept in
place to detect tempered archives.

I recommend treating this as a security issue. I see the following
cases where extracting a file outside the extraction path could
constitute a security risk:

a) When extraction is done as a different user than archive
creation. The user that created the archive may be able to
get a file overwritten as a different user.
b) When the archive is created on one host and extracted on
another. The user that created the archive may be able to
get a file overwritten on another host.
c) When an archive is created and extracted after a OS reinstall.
When a host is suspected compromised, it is common to reinstall
(or set up a new machine), extract the backups and then evaluate
their integrity. A user that manipulates the archive before such
a reinstall may be able to get a file overwritten outside the
extraction path and may evade integrity checks.

Notably absent is the creation and extraction on the same host as
the same user. In such case, an adversary must be assumed to be able
to replace any file directly.

This also (partially) fixes #7099.
2023-06-07 23:23:53 +02:00
TW ac4337a921
Merge pull request #7629 from ThomasWaldmann/update-changes-master
update CHANGES
2023-06-07 18:03:37 +02:00
Thomas Waldmann 57250d0c4b
update CHANGES 2023-06-07 17:46:02 +02:00
TW 97782f3f3c
Merge pull request #7624 from ThomasWaldmann/ci-xdistn-master
CI: try 4 parallel pytest-xdist workers
2023-06-07 00:14:40 +02:00
Thomas Waldmann 7092f24f4a
CI: try 4 parallel pytest-xdist workers 2023-06-06 23:22:03 +02:00
TW cad57e70c3
Merge pull request #7615 from ThomasWaldmann/serve-socket2
implement unix domain socket support
2023-06-06 23:21:15 +02:00
Thomas Waldmann ae97584218
fix logging, add some comments
shutting down logging is problematic as it is global
and we do multi-threaded execution, e.g. in tests.

thus, rather just flush the important loggers and keep
them alive.
2023-06-06 21:13:31 +02:00
Thomas Waldmann ffc59dd071
implement unix domain (ipc) socket support
server (listening) side:
borg serve --socket  # default location
borg serve --socket=/path/to/socket

client side:
borg -r socket:///path/to/repo create ...
borg --socket=/path/to/socket -r socket:///path/to/repo ...

served connections:
- for ssh: proto: one connection
- for socket: proto: many connections (one after the other)

The socket has user and group permissions (770).

skip socket tests on win32, they hang infinitely, until
github CI terminates them after 60 minutes.

socket tests: use unique socket name

don't use the standard / default socket name, otherwise tests
running in parallel would interfere with each other by using
the same socket / the same borg serve process.

write a .pid file, clean up .pid and .sock file at exit

add stderr print for accepted/finished socket connection
2023-06-06 21:12:54 +02:00
Thomas Waldmann 7878a34fd4
add get_runtime_dir / BORG_RUNTIME_DIR, tests, docs 2023-06-06 11:58:48 +02:00
TW 9f51600f20
Merge pull request #7627 from ThomasWaldmann/fix-chunksize-distrib-test-master
fix test_buzhash_chunksize_distribution
2023-06-06 10:43:53 +02:00
Thomas Waldmann 6625fb52ac
fix test_buzhash_chunksize_distribution
the last chunk can be smaller than 2**min_exp.
2023-06-06 10:09:53 +02:00
TW 6a4074d761
Merge pull request #7625 from bigtedde/testsuite-improvements
Parameterize version and item tests
2023-06-05 00:23:36 +02:00
bigtedde 83d821171e Parameterize 'version' and 'item' tests 2023-06-04 14:38:22 -07:00
TW e70b5b1e94
Merge pull request #7620 from bigtedde/parameterize_test_compress
Parametrize compression tests
2023-06-02 23:03:24 +02:00
bigtedde e0f6685498 parameterized compression tests 2023-06-02 12:55:35 -07:00
TW 05193d2530
Merge pull request #7623 from RayyanAnsari/windows-ci-merge
CI: merge Windows workflow with main
2023-06-01 22:47:54 +02:00
Rayyan Ansari 4f7d940769
CI: merge Windows workflow with main
This also enables us to avoid running it when lint fails.
2023-06-01 20:25:42 +01:00
TW 021c9b656c
Merge pull request #7619 from DavidRambo/master
Add support and tests for shell-style alternatives.
2023-06-01 12:06:16 +02:00
David Rambo ee0ca13ab5 Update help docs to reflect support for alternative grouping in shell-style patterns 2023-05-31 16:57:31 -07:00
David Rambo 4efc7cd0bd Add support and tests for shell-style alternatives
Code review fixes

Remove empty line
2023-05-31 15:38:05 -07:00
TW 87b74f3b0d
Merge pull request #7607 from ThomasWaldmann/new-remote-logging
new remote and progress logging, cleanups
2023-05-30 00:14:23 +02:00
Thomas Waldmann e2ea5cf164
tests: fix usage of .reopen()
also:
add missing param to RemoteRepositoryTestCase.open method, but ignore it.
2023-05-29 23:02:42 +02:00
Thomas Waldmann 48c7879887
RemoteRepository: add .close method
- tears down logging (so no new log output is generated afterwards)
- sends all queued log output
- then returns

also: make stdin_fd / stdout_fd instance variables
2023-05-29 22:40:55 +02:00
Thomas Waldmann f84951b53c
add logging debugging functionality 2023-05-29 22:40:53 +02:00
Thomas Waldmann ac4b5c35da
borg serve: shutdown server after sending all queued log records 2023-05-29 22:40:52 +02:00
Thomas Waldmann 746cef1cba
teardown logging in exec_cmd
for normal borg command invocation:
- logging is set up in Archiver.run
- the atexit handler calls logging.shutdown when process terminates

for tests:
- Archiver.run called by exec_cmd
- no atexit handler executed as process lives on
- borg.logger.teardown (calls shutdown and configured=False) now
  called in exec_cmd
2023-05-29 22:40:50 +02:00
Thomas Waldmann dac4609468
remove_handlers 2023-05-29 22:40:49 +02:00
Thomas Waldmann c3a4568870
channel progress output via logging system
- simplify progress output (no \r, no terminal size related tweaks)
- emit progress output via the logging system (so it does not use stderr
  of borg serve)
- progress code always logs a json string, the json has all needed
  to either do json log output or plain text log output.
- use formatters to generate plain or json output from that.
- clean up setup_logging
- use a StderrHandler that always uses the **current** sys.stderr
- tweak TestPassphrase to not accidentally trigger just because of seeing 12 in output
2023-05-29 22:40:47 +02:00
Thomas Waldmann 0be545dc45
remove ProgressIndicatorEndless (not used) 2023-05-29 22:40:45 +02:00
Thomas Waldmann e351e67aee
RepositoryServer: do not use stderr for logging, see #7604
Instead, install a handler that sends the LogRecord dicts to a queue.
That queue is then emptied in the borg serve main loop and
the LogRecords are sent msgpacked via stdout to the client,
similar to the RPC results.

On the client side, the LogRecords are recreated from the
received dicts and fed into the clientside logging system.

As we use msgpacked LogRecord dicts, we don't need JSON for
this purpose on the borg serve side any more.
On the client side, the LogRecords will then be either formatted
as normal text or as JSON log output (by the clientside log
formatter).
2023-05-29 22:40:42 +02:00
TW ca68dd2565
Merge pull request #7614 from ThomasWaldmann/upgrade-cython-master
development.lock.txt: upgrade cython to 0.29.35, misc. other upgrades (master)
2023-05-29 15:46:58 +02:00
TW 326cf7b46a
Merge pull request #7609 from nain-F49FF806/master-clear-empty-dirs
Add function to clear empty directories at end of compact process.
2023-05-29 15:32:11 +02:00
Thomas Waldmann 8bbef3b672
development.lock.txt: upgrade cython to 0.29.35, misc. other upgrades 2023-05-29 15:18:50 +02:00
Thomas Waldmann 6ae23bc0d7
fix typo 2023-05-29 15:04:30 +02:00
nain 4a7a5b2253 Add function to clear empty directories at end of compact process.
Compact moves data to new segments, and then removes the old segments.
When enough segments are moved, directories holding the now cleared segments
may thus become empty.

With this commit any empty directories are cleared after segments compacting.
Fixes #6823
2023-05-28 01:48:32 -04:00
TW 00e19d047d
Merge pull request #7608 from nain-F49FF806/master-data-scandir-unify-function
unify scanning and listing of segment dirs / segment files and apply good practices
2023-05-27 20:48:01 +02:00
nain ffe237ce0c unify scanning and listing of segment dirs / segment files and apply good practices
+ os.scandir instead of os.listdir
  Improved speed and added flexibility with attributes (name, path, is_dir(), is_file())
+ use is_dir / is_file to make sure  we're reading only dirs / files respectively
+ Filtering to particular start, end index range built in
+ Move value bounds of segment (index) into constants module and use them instead

Resolves #7597

(forward patch from commits c9f35a16e9bf9e7073c486553177cef79ff1cb06^..edb5e749f512b7737b6933e13b7e61fefcd17bcb)
2023-05-27 07:54:32 -04:00
TW 85b6126629
Merge pull request #7605 from ThomasWaldmann/fix-_resolve_path
fix borg.remote._resolve_path
2023-05-24 17:32:38 +02:00
Thomas Waldmann 89297398c4
fix borg.remote._resolve_path
this used to call get_base_dir (and would have needed
legacy=True now to work like expected).

rather implemented the desired behaviour locally and
got rid of the legacy call (which was a bit strange
anyway as it also considered BORG_BASE_DIR, which is
unexpected when resolving ~).
2023-05-24 17:01:45 +02:00
TW fbb60140ac
Merge pull request #7603 from ThomasWaldmann/remove-legacy
borg.remote: remove legacy
2023-05-24 16:38:18 +02:00
Thomas Waldmann 90c6b7f794
remote exceptions: simplify sysinfo processing
in the sysinfo function, there is a way to suppress
all sysinfo output via an env var and just return an
empty string.

so we can expect it is always in unpacked, but it
might be the empty string.
2023-05-24 15:30:20 +02:00
Thomas Waldmann 2331341b74
simplify handle_error 2023-05-24 14:38:30 +02:00
Thomas Waldmann 4b796a7337
remote repos: remove support for borg < 1.1.0 ($LOG, logging setup)
log output:

always expect json, remove $LOG format support.

we keep limited support for unstructured format also,
just not to lose anything from remote stderr.
2023-05-24 14:38:24 +02:00