test-apt-update-nofallback 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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_cve_2012_0214()
  70. {
  71. # see https://bugs.launchpad.net/ubuntu/+source/apt/+bug/947108
  72. #
  73. # it was possible to MITM the download so that InRelease/Release.gpg
  74. # are not delivered (404) and a altered Release file was send
  75. #
  76. # apt left the old InRelease file in /var/lib/apt/lists and downloaded
  77. # the unauthenticated Release file too giving the false impression that
  78. # Release was authenticated
  79. #
  80. # Note that this is pretty much impossible nowdays because:
  81. # a) InRelease is left as is, not split to InRelease/Release as it was
  82. # in the old days
  83. # b) we refuse to go from signed->unsigned
  84. #
  85. # Still worth having a regression test the simulates the condition
  86. # setup archive with InRelease
  87. setupaptarchive_with_lists_clean
  88. testsuccess aptget update
  89. # do what CVE-2012-0214 did
  90. rm $APTARCHIVE/dists/unstable/InRelease
  91. rm $APTARCHIVE/dists/unstable/Release.gpg
  92. inject_evil_package
  93. # build valid Release file
  94. aptftparchive -qq release ./aptarchive > aptarchive/dists/unstable/Release
  95. assert_update_is_refused_and_last_good_state_used
  96. # ensure there is no _Release file downloaded
  97. testfailure ls rootdir/var/lib/apt/lists/*_Release
  98. }
  99. test_subvert_inrelease()
  100. {
  101. # setup archive with InRelease
  102. setupaptarchive_with_lists_clean
  103. testsuccess aptget update
  104. # replace InRelease with something else
  105. mv $APTARCHIVE/dists/unstable/Release $APTARCHIVE/dists/unstable/InRelease
  106. testequal "W: Failed to fetch file:${APTARCHIVE}/dists/unstable/InRelease Does not start with a cleartext signature
  107. E: Some index files failed to download. They have been ignored, or old ones used instead." aptget update -qq
  108. # ensure we keep the repo
  109. assert_repo_is_intact
  110. }
  111. test_inrelease_to_invalid_inrelease()
  112. {
  113. # setup archive with InRelease
  114. setupaptarchive_with_lists_clean
  115. testsuccess aptget update
  116. # now remove InRelease and subvert Release do no longer verify
  117. sed -i 's/Codename.*/Codename: evil!'/ $APTARCHIVE/dists/unstable/InRelease
  118. inject_evil_package
  119. 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>
  120. W: Failed to fetch file:${APTARCHIVE}/dists/unstable/InRelease
  121. W: Some index files failed to download. They have been ignored, or old ones used instead." aptget update -qq
  122. # ensure we keep the repo
  123. assert_repo_is_intact
  124. testfailure grep "evil" rootdir/var/lib/apt/lists/*InRelease
  125. }
  126. test_release_gpg_to_invalid_release_release_gpg()
  127. {
  128. # setup archive with InRelease
  129. setupaptarchive_with_lists_clean
  130. rm $APTARCHIVE/dists/unstable/InRelease
  131. testsuccess aptget update
  132. # now subvert Release do no longer verify
  133. echo "Some evil data" >> $APTARCHIVE/dists/unstable/Release
  134. inject_evil_package
  135. testequal "E: The repository 'file: unstable Release.gpg' is no longer signed." aptget update -qq
  136. assert_repo_is_intact
  137. testfailure grep "evil" rootdir/var/lib/apt/lists/*Release
  138. }
  139. TESTDIR=$(readlink -f $(dirname $0))
  140. . $TESTDIR/framework
  141. setupenvironment
  142. configarchitecture "i386"
  143. # a "normal" package with source and binary
  144. buildsimplenativepackage 'foo' 'all' '2.0'
  145. # setup the archive and ensure we have a single package that installs fine
  146. setupaptarchive
  147. APTARCHIVE=$(readlink -f ./aptarchive)
  148. assert_repo_is_intact
  149. # test the various cases where a repo may go from signed->unsigned
  150. msgmsg "test_from_inrelease_to_unsigned"
  151. test_from_inrelease_to_unsigned
  152. msgmsg "test_from_release_gpg_to_unsigned"
  153. test_from_release_gpg_to_unsigned
  154. # ensure we do not regress on CVE-2012-0214
  155. msgmsg "test_cve_2012_0214"
  156. test_cve_2012_0214
  157. # ensure InRelase can not be subverted
  158. msgmsg "test_subvert_inrelease"
  159. test_subvert_inrelease
  160. # ensure we revert to last good state if InRelease does not verify
  161. msgmsg "test_inrelease_to_invalid_inrelease"
  162. test_inrelease_to_invalid_inrelease
  163. # ensure we revert to last good state if Release/Release.gpg does not verify
  164. msgmsg "test_release_gpg_to_invalid_release_release_gpg"
  165. test_release_gpg_to_invalid_release_release_gpg