test-apt-update-nofallback 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #!/bin/sh
  2. #
  3. # ensure we never fallback from a signed to a unsigned repo
  4. #
  5. # hash checks are done in
  6. #
  7. set -e
  8. simulate_mitm_and_inject_evil_package()
  9. {
  10. redatereleasefiles '+1 hour'
  11. rm -f "$APTARCHIVE/dists/unstable/InRelease"
  12. rm -f "$APTARCHIVE/dists/unstable/Release.gpg"
  13. inject_evil_package
  14. }
  15. inject_evil_package()
  16. {
  17. cat > "$APTARCHIVE/dists/unstable/main/binary-i386/Packages" <<EOF
  18. Package: evil
  19. Installed-Size: 29
  20. Maintainer: Joe Sixpack <joe@example.org>
  21. Architecture: all
  22. Version: 1.0
  23. Filename: pool/evil_1.0_all.deb
  24. Size: 1270
  25. Description: an autogenerated evil package
  26. EOF
  27. # avoid ims hit
  28. touch -d '+1hour' aptarchive/dists/unstable/main/binary-i386/Packages
  29. compressfile aptarchive/dists/unstable/main/binary-i386/Packages
  30. }
  31. assert_update_is_refused_and_last_good_state_used()
  32. {
  33. testfailuremsg "E: The repository 'file:${APTARCHIVE} unstable Release' is no longer signed." aptget update
  34. assert_repo_is_intact
  35. }
  36. assert_repo_is_intact()
  37. {
  38. testsuccessequal "foo/unstable 2.0 all" apt list -q
  39. testsuccess aptget install -y -s foo
  40. testfailure aptget install -y evil
  41. testsuccess aptget source foo --print-uris
  42. LISTDIR=rootdir/var/lib/apt/lists
  43. testempty find "$LISTDIR" -name 'InRelease' -o -name 'Release.gpg'
  44. }
  45. setupaptarchive_with_lists_clean()
  46. {
  47. setupaptarchive --no-update
  48. rm -rf rootdir/var/lib/apt/lists
  49. }
  50. test_from_inrelease_to_unsigned()
  51. {
  52. # setup archive with InRelease file
  53. setupaptarchive_with_lists_clean
  54. testsuccess aptget update
  55. listcurrentlistsdirectory > lists.before
  56. simulate_mitm_and_inject_evil_package
  57. assert_update_is_refused_and_last_good_state_used
  58. testfileequal lists.before "$(listcurrentlistsdirectory)"
  59. }
  60. test_from_release_gpg_to_unsigned()
  61. {
  62. # setup archive with Release/Release.gpg (but no InRelease)
  63. setupaptarchive_with_lists_clean
  64. rm "$APTARCHIVE/dists/unstable/InRelease"
  65. testsuccess aptget update
  66. listcurrentlistsdirectory > lists.before
  67. simulate_mitm_and_inject_evil_package
  68. assert_update_is_refused_and_last_good_state_used
  69. testfileequal lists.before "$(listcurrentlistsdirectory)"
  70. }
  71. test_from_inrelease_to_unsigned_with_override()
  72. {
  73. # setup archive with InRelease file
  74. setupaptarchive_with_lists_clean
  75. testsuccess aptget update
  76. # simulate moving to a unsigned but otherwise valid repo
  77. simulate_mitm_and_inject_evil_package
  78. generatereleasefiles '+2 hours'
  79. find "$APTARCHIVE" -name '*Packages*' -exec touch -d '+2 hours' {} \;
  80. # and ensure we can update to it (with enough force)
  81. testwarning aptget update --allow-insecure-repositories \
  82. -o Acquire::AllowDowngradeToInsecureRepositories=1 -o Debug::pkgAcquire::Worker=1 -o Debug::pkgAcquire::Auth=1
  83. # but that the individual packages are still considered untrusted
  84. testfailureequal "WARNING: The following packages cannot be authenticated!
  85. evil
  86. E: There were unauthenticated packages and -y was used without --allow-unauthenticated" aptget install -qq -y evil
  87. }
  88. test_cve_2012_0214()
  89. {
  90. # see https://bugs.launchpad.net/ubuntu/+source/apt/+bug/947108
  91. #
  92. # it was possible to MITM the download so that InRelease/Release.gpg
  93. # are not delivered (404) and a altered Release file was send
  94. #
  95. # apt left the old InRelease file in /var/lib/apt/lists and downloaded
  96. # the unauthenticated Release file too giving the false impression that
  97. # Release was authenticated
  98. #
  99. # Note that this is pretty much impossible nowadays because:
  100. # a) InRelease is left as is, not split to InRelease/Release as it was
  101. # in the old days
  102. # b) we refuse to go from signed->unsigned
  103. #
  104. # Still worth having a regression test the simulates the condition
  105. # setup archive with InRelease
  106. setupaptarchive_with_lists_clean
  107. testsuccess aptget update
  108. listcurrentlistsdirectory > lists.before
  109. # do what CVE-2012-0214 did
  110. rm "$APTARCHIVE/dists/unstable/InRelease"
  111. rm "$APTARCHIVE/dists/unstable/Release.gpg"
  112. inject_evil_package
  113. # build valid Release file
  114. aptftparchive -qq release ./aptarchive > aptarchive/dists/unstable/Release
  115. assert_update_is_refused_and_last_good_state_used
  116. testfileequal lists.before "$(listcurrentlistsdirectory)"
  117. # ensure there is no _Release file downloaded
  118. testfailure ls rootdir/var/lib/apt/lists/*_Release
  119. }
  120. test_subvert_inrelease()
  121. {
  122. # setup archive with InRelease
  123. setupaptarchive_with_lists_clean
  124. testsuccess aptget update
  125. listcurrentlistsdirectory > lists.before
  126. # replace InRelease with something else
  127. mv "$APTARCHIVE/dists/unstable/Release" "$APTARCHIVE/dists/unstable/InRelease"
  128. testfailuremsg "W: Failed to fetch file:${APTARCHIVE}/dists/unstable/InRelease Clearsigned file isn't valid, got 'NOSPLIT' (does the network require authentication?)
  129. E: Some index files failed to download. They have been ignored, or old ones used instead." aptget update
  130. # ensure we keep the repo
  131. testfileequal lists.before "$(listcurrentlistsdirectory)"
  132. assert_repo_is_intact
  133. }
  134. test_inrelease_to_invalid_inrelease()
  135. {
  136. # setup archive with InRelease
  137. setupaptarchive_with_lists_clean
  138. testsuccess aptget update
  139. listcurrentlistsdirectory > lists.before
  140. # now remove InRelease and subvert Release do no longer verify
  141. sed -i 's/^Codename:.*/Codename: evil!/' "$APTARCHIVE/dists/unstable/InRelease"
  142. inject_evil_package
  143. testwarningequal "W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: file:${APTARCHIVE} unstable InRelease: The following signatures were invalid: BADSIG 5A90D141DBAC8DAE Joe Sixpack (APT Testcases Dummy) <joe@example.org>
  144. W: Failed to fetch file:${APTARCHIVE}/dists/unstable/InRelease The following signatures were invalid: BADSIG 5A90D141DBAC8DAE Joe Sixpack (APT Testcases Dummy) <joe@example.org>
  145. W: Some index files failed to download. They have been ignored, or old ones used instead." aptget update -qq
  146. # ensure we keep the repo
  147. testfailure grep 'evil' rootdir/var/lib/apt/lists/*InRelease
  148. testfileequal lists.before "$(listcurrentlistsdirectory)"
  149. assert_repo_is_intact
  150. }
  151. test_release_gpg_to_invalid_release_release_gpg()
  152. {
  153. # setup archive with InRelease
  154. setupaptarchive_with_lists_clean
  155. rm "$APTARCHIVE/dists/unstable/InRelease"
  156. testsuccess aptget update
  157. listcurrentlistsdirectory > lists.before
  158. # now subvert Release do no longer verify
  159. echo "Some evil data" >> "$APTARCHIVE/dists/unstable/Release"
  160. inject_evil_package
  161. testwarningequal "W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: file:${APTARCHIVE} unstable Release: The following signatures were invalid: BADSIG 5A90D141DBAC8DAE Joe Sixpack (APT Testcases Dummy) <joe@example.org>
  162. W: Failed to fetch file:${APTARCHIVE}/dists/unstable/Release.gpg The following signatures were invalid: BADSIG 5A90D141DBAC8DAE Joe Sixpack (APT Testcases Dummy) <joe@example.org>
  163. W: Some index files failed to download. They have been ignored, or old ones used instead." aptget update -qq
  164. testfailure grep 'evil' rootdir/var/lib/apt/lists/*Release
  165. testfileequal lists.before "$(listcurrentlistsdirectory)"
  166. assert_repo_is_intact
  167. }
  168. TESTDIR=$(readlink -f $(dirname $0))
  169. . $TESTDIR/framework
  170. setupenvironment
  171. configarchitecture "i386"
  172. # a "normal" package with source and binary
  173. buildsimplenativepackage 'foo' 'all' '2.0'
  174. # setup the archive and ensure we have a single package that installs fine
  175. setupaptarchive
  176. APTARCHIVE="$(readlink -f ./aptarchive)"
  177. assert_repo_is_intact
  178. # test the various cases where a repo may go from signed->unsigned
  179. msgmsg "test_from_inrelease_to_unsigned"
  180. test_from_inrelease_to_unsigned
  181. msgmsg "test_from_release_gpg_to_unsigned"
  182. test_from_release_gpg_to_unsigned
  183. # ensure we do not regress on CVE-2012-0214
  184. msgmsg "test_cve_2012_0214"
  185. test_cve_2012_0214
  186. # ensure InRelase can not be subverted
  187. msgmsg "test_subvert_inrelease"
  188. test_subvert_inrelease
  189. # ensure we revert to last good state if InRelease does not verify
  190. msgmsg "test_inrelease_to_invalid_inrelease"
  191. test_inrelease_to_invalid_inrelease
  192. # ensure we revert to last good state if Release/Release.gpg does not verify
  193. msgmsg "test_release_gpg_to_invalid_release_release_gpg"
  194. test_release_gpg_to_invalid_release_release_gpg
  195. # ensure we can override the downgrade error
  196. msgmsg "test_from_inrelease_to_unsigned_with_override"
  197. test_from_inrelease_to_unsigned_with_override