test-apt-update-nofallback 7.3 KB

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