test-apt-update-nofallback 10 KB

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