download: handle possibly invalid local tarballs
authorPetr Štetiar <ynezz@true.cz>
Thu, 19 Nov 2020 15:32:46 +0000 (16:32 +0100)
committerRISCi_ATOM <bob@bobcall.me>
Sun, 6 Dec 2020 06:35:52 +0000 (01:35 -0500)
Currently it's assumed, that already downloaded tarballs are always
fine, so no checksum checking is performed and the tarball is used even
if it might be corrupted.

From now on, we're going to always check the downloaded tarballs before
considering them valid.

Steps to reproduce:

 1. Remove cached tarball

   rm dl/libubox-2020-08-06-9e52171d.tar.xz

 2. Download valid tarball again

   make package/libubox/download

 3. Invalidate the tarball

   sed -i 's/PKG_MIRROR_HASH:=../PKG_MIRROR_HASH:=ff/' package/libs/libubox/Makefile

 4. Now compile with corrupt tarball source

   make package/libubox/{clean,compile}

Signed-off-by: Petr Štetiar <ynezz@true.cz>
(cherry picked from commit 4e19cbc553350b8146985367ba46514cf50e3393)

include/host-build.mk
include/package.mk
scripts/download.pl

index 827ea6bbfb1be72e38309de3fe48435e3aa8172f..79a9b1f8d605ca48e41001ad0177ca53c7e84fb9 100644 (file)
@@ -184,6 +184,8 @@ ifndef DUMP
     clean-build: host-clean-build
   endif
 
+  $(DL_DIR)/$(FILE): FORCE
+
   $(_host_target)host-prepare: $(HOST_STAMP_PREPARED)
   $(_host_target)host-configure: $(HOST_STAMP_CONFIGURED)
   $(_host_target)host-compile: $(HOST_STAMP_BUILT) $(HOST_STAMP_INSTALLED)
index c541f6edf7a967bb317c87b8a5c63ce98cb0673a..f6aa5ea8d03d0f51a08b74019bf30b12b027ad96 100644 (file)
@@ -185,6 +185,8 @@ define Build/CoreTargets
   $(call Build/Autoclean)
   $(call DefaultTargets)
 
+  $(DL_DIR)/$(FILE): FORCE
+
   download:
        $(foreach hook,$(Hooks/Download),
                $(call $(hook))$(sep)
index 13fd5c8d8df6f5dc76f7ab6fcda343fce4ba8113..44a24b8d6f7739c3336df26edae958ce45c82c99 100755 (executable)
@@ -271,6 +271,24 @@ foreach my $mirror (@ARGV) {
 
 push @mirrors, 'https://librecmc.org/librecmc/downloads/sources/v1.5';
 
+if (-f "$target/$filename") {
+       $hash_cmd and do {
+               if (system("cat '$target/$filename' | $hash_cmd > '$target/$filename.hash'")) {
+                       die "Failed to generate hash for $filename\n";
+               }
+
+               my $sum = `cat "$target/$filename.hash"`;
+               $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
+               $sum = $1;
+
+               exit 0 if $sum eq $file_hash;
+
+               die "Hash of the local file $filename does not match (file: $sum, requested: $file_hash) - deleting download.\n";
+               unlink "$target/$filename";
+               cleanup();
+       };
+}
+
 while (!-f "$target/$filename") {
        my $mirror = shift @mirrors;
        $mirror or die "No more mirrors to try - giving up.\n";