test-apt-update-nofallback 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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" "$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. compressfile aptarchive/dists/unstable/main/binary-i386/Packages
  29. }
  30. assert_update_is_refused_and_last_good_state_used()
  31. {
  32. testfailuremsg "E: The repository 'file:${APTARCHIVE} unstable Release' is no longer signed.
  33. N: Updating from such a repository can't be done securely, and is therefore disabled by default.
  34. N: See apt-secure(8) manpage for repository creation and user configuration details." aptget update
  35. assert_repo_is_intact
  36. }
  37. assert_repo_is_intact()
  38. {
  39. testsuccessequal "foo/unstable 2.0 all" apt list -qq
  40. testsuccess aptget install -y -s foo
  41. testfailure aptget install -y evil
  42. testsuccess aptget source foo --print-uris
  43. LISTDIR=rootdir/var/lib/apt/lists
  44. testempty find "$LISTDIR" -name 'InRelease' -o -name 'Release.gpg'
  45. }
  46. setupaptarchive_with_lists_clean()
  47. {
  48. setupaptarchive --no-update
  49. rm -rf rootdir/var/lib/apt/lists
  50. }
  51. test_from_inrelease_to_unsigned()
  52. {
  53. export APT_DONT_SIGN='Release.gpg'
  54. setupaptarchive_with_lists_clean
  55. testsuccess aptget update
  56. listcurrentlistsdirectory > lists.before
  57. simulate_mitm_and_inject_evil_package
  58. assert_update_is_refused_and_last_good_state_used
  59. testfileequal lists.before "$(listcurrentlistsdirectory)"
  60. }
  61. test_from_release_gpg_to_unsigned()
  62. {
  63. export APT_DONT_SIGN='InRelease'
  64. setupaptarchive_with_lists_clean
  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. export APT_DONT_SIGN='Release.gpg'
  74. # setup archive with InRelease file
  75. setupaptarchive_with_lists_clean
  76. testsuccess aptget update
  77. # simulate moving to a unsigned but otherwise valid repo
  78. simulate_mitm_and_inject_evil_package
  79. generatereleasefiles '+2 hours'
  80. find "$APTARCHIVE" -name '*Packages*' -exec touch -d '+2 hours' {} \;
  81. # and ensure we can update to it (with enough force)
  82. testfailure aptget update
  83. testfailure aptget update --allow-insecure-repositories
  84. testwarning aptget update --allow-insecure-repositories \
  85. -o Acquire::AllowDowngradeToInsecureRepositories=1 -o Debug::pkgAcquire::Worker=1 -o Debug::pkgAcquire::Auth=1
  86. # but that the individual packages are still considered untrusted
  87. testfailureequal "WARNING: The following packages cannot be authenticated!
  88. evil
  89. E: There were unauthenticated packages and -y was used without --allow-unauthenticated" aptget install -qq -y evil
  90. }
  91. test_from_inrelease_to_norelease_with_override()
  92. {
  93. # setup archive with InRelease file
  94. setupaptarchive_with_lists_clean
  95. testsuccess aptget update
  96. # simulate moving to a unsigned but otherwise valid repo
  97. simulate_mitm_and_inject_evil_package
  98. find "$APTARCHIVE" -name '*Release*' -delete
  99. find "$APTARCHIVE" -name '*Packages*' -exec touch -d '+2 hours' {} \;
  100. # and ensure we can update to it (with enough force)
  101. testfailure aptget update
  102. testfailure aptget update --allow-insecure-repositories
  103. testwarning aptget update --allow-insecure-repositories \
  104. -o Acquire::AllowDowngradeToInsecureRepositories=1 -o Debug::pkgAcquire::Worker=1 -o Debug::pkgAcquire::Auth=1
  105. # but that the individual packages are still considered untrusted
  106. testfailureequal "WARNING: The following packages cannot be authenticated!
  107. evil
  108. E: There were unauthenticated packages and -y was used without --allow-unauthenticated" aptget install -qq -y evil
  109. }
  110. test_cve_2012_0214()
  111. {
  112. # see https://bugs.launchpad.net/ubuntu/+source/apt/+bug/947108
  113. #
  114. # it was possible to MITM the download so that InRelease/Release.gpg
  115. # are not delivered (404) and a altered Release file was send
  116. #
  117. # apt left the old InRelease file in /var/lib/apt/lists and downloaded
  118. # the unauthenticated Release file too giving the false impression that
  119. # Release was authenticated
  120. #
  121. # Note that this is pretty much impossible nowadays because:
  122. # a) InRelease is left as is, not split to InRelease/Release as it was
  123. # in the old days
  124. # b) we refuse to go from signed->unsigned
  125. #
  126. # Still worth having a regression test the simulates the condition
  127. export APT_DONT_SIGN='Release.gpg'
  128. setupaptarchive_with_lists_clean
  129. testsuccess aptget update
  130. listcurrentlistsdirectory > lists.before
  131. # do what CVE-2012-0214 did
  132. rm -f "$APTARCHIVE/dists/unstable/InRelease" "$APTARCHIVE/dists/unstable/Release.gpg"
  133. inject_evil_package
  134. # build valid Release file
  135. aptftparchive -qq release ./aptarchive > aptarchive/dists/unstable/Release
  136. assert_update_is_refused_and_last_good_state_used
  137. testfileequal lists.before "$(listcurrentlistsdirectory)"
  138. # ensure there is no _Release file downloaded
  139. testfailure ls rootdir/var/lib/apt/lists/*_Release
  140. }
  141. test_subvert_inrelease()
  142. {
  143. export APT_DONT_SIGN='Release.gpg'
  144. setupaptarchive_with_lists_clean
  145. testsuccess aptget update
  146. listcurrentlistsdirectory > lists.before
  147. # replace InRelease with something else
  148. mv "$APTARCHIVE/dists/unstable/Release" "$APTARCHIVE/dists/unstable/InRelease"
  149. testfailuremsg "E: Failed to fetch file:${APTARCHIVE}/dists/unstable/InRelease Clearsigned file isn't valid, got 'NOSPLIT' (does the network require authentication?)
  150. E: Some index files failed to download. They have been ignored, or old ones used instead." aptget update
  151. # ensure we keep the repo
  152. testfileequal lists.before "$(listcurrentlistsdirectory)"
  153. assert_repo_is_intact
  154. }
  155. test_inrelease_to_invalid_inrelease()
  156. {
  157. export APT_DONT_SIGN='Release.gpg'
  158. setupaptarchive_with_lists_clean
  159. testsuccess aptget update
  160. listcurrentlistsdirectory > lists.before
  161. # now remove InRelease and subvert Release do no longer verify
  162. sed -i 's/^Codename:.*/Codename: evil!/' "$APTARCHIVE/dists/unstable/InRelease"
  163. inject_evil_package
  164. 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>
  165. W: Failed to fetch file:${APTARCHIVE}/dists/unstable/InRelease The following signatures were invalid: BADSIG 5A90D141DBAC8DAE Joe Sixpack (APT Testcases Dummy) <joe@example.org>
  166. W: Some index files failed to download. They have been ignored, or old ones used instead." aptget update -qq
  167. # ensure we keep the repo
  168. testfailure grep 'evil' rootdir/var/lib/apt/lists/*InRelease
  169. testfileequal lists.before "$(listcurrentlistsdirectory)"
  170. assert_repo_is_intact
  171. }
  172. test_release_gpg_to_invalid_release_release_gpg()
  173. {
  174. export APT_DONT_SIGN='InRelease'
  175. setupaptarchive_with_lists_clean
  176. testsuccess aptget update
  177. listcurrentlistsdirectory > lists.before
  178. # now subvert Release do no longer verify
  179. echo "Some evil data" >> "$APTARCHIVE/dists/unstable/Release"
  180. inject_evil_package
  181. 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>
  182. 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>
  183. W: Some index files failed to download. They have been ignored, or old ones used instead." aptget update -qq
  184. testfailure grep 'evil' rootdir/var/lib/apt/lists/*Release
  185. testfileequal lists.before "$(listcurrentlistsdirectory)"
  186. assert_repo_is_intact
  187. }
  188. TESTDIR="$(readlink -f "$(dirname "$0")")"
  189. . "$TESTDIR/framework"
  190. setupenvironment
  191. configarchitecture "i386"
  192. # a "normal" package with source and binary
  193. buildsimplenativepackage 'foo' 'all' '2.0'
  194. # setup the archive and ensure we have a single package that installs fine
  195. setupaptarchive
  196. APTARCHIVE="$(readlink -f ./aptarchive)"
  197. assert_repo_is_intact
  198. # test the various cases where a repo may go from signed->unsigned
  199. msgmsg "test_from_inrelease_to_unsigned"
  200. test_from_inrelease_to_unsigned
  201. msgmsg "test_from_release_gpg_to_unsigned"
  202. test_from_release_gpg_to_unsigned
  203. # ensure we do not regress on CVE-2012-0214
  204. msgmsg "test_cve_2012_0214"
  205. test_cve_2012_0214
  206. # ensure InRelease can not be subverted
  207. msgmsg "test_subvert_inrelease"
  208. test_subvert_inrelease
  209. # ensure we revert to last good state if InRelease does not verify
  210. msgmsg "test_inrelease_to_invalid_inrelease"
  211. test_inrelease_to_invalid_inrelease
  212. # ensure we revert to last good state if Release/Release.gpg does not verify
  213. msgmsg "test_release_gpg_to_invalid_release_release_gpg"
  214. test_release_gpg_to_invalid_release_release_gpg
  215. # ensure we can override the downgrade error
  216. msgmsg "test_from_inrelease_to_unsigned_with_override"
  217. test_from_inrelease_to_unsigned_with_override
  218. msgmsg "test_from_inrelease_to_norelease_with_override"
  219. test_from_inrelease_to_norelease_with_override