docker_cli_cp_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "testing"
  10. )
  11. const (
  12. cpTestPathParent = "/some"
  13. cpTestPath = "/some/path"
  14. cpTestName = "test"
  15. cpFullPath = "/some/path/test"
  16. cpContainerContents = "holla, i am the container"
  17. cpHostContents = "hello, i am the host"
  18. )
  19. // Test for #5656
  20. // Check that garbage paths don't escape the container's rootfs
  21. func TestCpGarbagePath(t *testing.T) {
  22. out, exitCode, err := dockerCmd(t, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
  23. if err != nil || exitCode != 0 {
  24. t.Fatal("failed to create a container", out, err)
  25. }
  26. cleanedContainerID := stripTrailingCharacters(out)
  27. defer deleteContainer(cleanedContainerID)
  28. out, _, err = dockerCmd(t, "wait", cleanedContainerID)
  29. if err != nil || stripTrailingCharacters(out) != "0" {
  30. t.Fatal("failed to set up container", out, err)
  31. }
  32. if err := os.MkdirAll(cpTestPath, os.ModeDir); err != nil {
  33. t.Fatal(err)
  34. }
  35. hostFile, err := os.Create(cpFullPath)
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. defer hostFile.Close()
  40. defer os.RemoveAll(cpTestPathParent)
  41. fmt.Fprintf(hostFile, "%s", cpHostContents)
  42. tmpdir, err := ioutil.TempDir("", "docker-integration")
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. tmpname := filepath.Join(tmpdir, cpTestName)
  47. defer os.RemoveAll(tmpdir)
  48. path := filepath.Join("../../../../../../../../../../../../", cpFullPath)
  49. _, _, err = dockerCmd(t, "cp", cleanedContainerID+":"+path, tmpdir)
  50. if err != nil {
  51. t.Fatalf("couldn't copy from garbage path: %s:%s %s", cleanedContainerID, path, err)
  52. }
  53. file, _ := os.Open(tmpname)
  54. defer file.Close()
  55. test, err := ioutil.ReadAll(file)
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. if string(test) == cpHostContents {
  60. t.Errorf("output matched host file -- garbage path can escape container rootfs")
  61. }
  62. if string(test) != cpContainerContents {
  63. t.Errorf("output doesn't match the input for garbage path")
  64. }
  65. logDone("cp - garbage paths relative to container's rootfs")
  66. }
  67. // Check that relative paths are relative to the container's rootfs
  68. func TestCpRelativePath(t *testing.T) {
  69. out, exitCode, err := dockerCmd(t, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
  70. if err != nil || exitCode != 0 {
  71. t.Fatal("failed to create a container", out, err)
  72. }
  73. cleanedContainerID := stripTrailingCharacters(out)
  74. defer deleteContainer(cleanedContainerID)
  75. out, _, err = dockerCmd(t, "wait", cleanedContainerID)
  76. if err != nil || stripTrailingCharacters(out) != "0" {
  77. t.Fatal("failed to set up container", out, err)
  78. }
  79. if err := os.MkdirAll(cpTestPath, os.ModeDir); err != nil {
  80. t.Fatal(err)
  81. }
  82. hostFile, err := os.Create(cpFullPath)
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. defer hostFile.Close()
  87. defer os.RemoveAll(cpTestPathParent)
  88. fmt.Fprintf(hostFile, "%s", cpHostContents)
  89. tmpdir, err := ioutil.TempDir("", "docker-integration")
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. tmpname := filepath.Join(tmpdir, cpTestName)
  94. defer os.RemoveAll(tmpdir)
  95. path, _ := filepath.Rel("/", cpFullPath)
  96. _, _, err = dockerCmd(t, "cp", cleanedContainerID+":"+path, tmpdir)
  97. if err != nil {
  98. t.Fatalf("couldn't copy from relative path: %s:%s %s", cleanedContainerID, path, err)
  99. }
  100. file, _ := os.Open(tmpname)
  101. defer file.Close()
  102. test, err := ioutil.ReadAll(file)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. if string(test) == cpHostContents {
  107. t.Errorf("output matched host file -- relative path can escape container rootfs")
  108. }
  109. if string(test) != cpContainerContents {
  110. t.Errorf("output doesn't match the input for relative path")
  111. }
  112. logDone("cp - relative paths relative to container's rootfs")
  113. }
  114. // Check that absolute paths are relative to the container's rootfs
  115. func TestCpAbsolutePath(t *testing.T) {
  116. out, exitCode, err := dockerCmd(t, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
  117. if err != nil || exitCode != 0 {
  118. t.Fatal("failed to create a container", out, err)
  119. }
  120. cleanedContainerID := stripTrailingCharacters(out)
  121. defer deleteContainer(cleanedContainerID)
  122. out, _, err = dockerCmd(t, "wait", cleanedContainerID)
  123. if err != nil || stripTrailingCharacters(out) != "0" {
  124. t.Fatal("failed to set up container", out, err)
  125. }
  126. if err := os.MkdirAll(cpTestPath, os.ModeDir); err != nil {
  127. t.Fatal(err)
  128. }
  129. hostFile, err := os.Create(cpFullPath)
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. defer hostFile.Close()
  134. defer os.RemoveAll(cpTestPathParent)
  135. fmt.Fprintf(hostFile, "%s", cpHostContents)
  136. tmpdir, err := ioutil.TempDir("", "docker-integration")
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. tmpname := filepath.Join(tmpdir, cpTestName)
  141. defer os.RemoveAll(tmpdir)
  142. path := cpFullPath
  143. _, _, err = dockerCmd(t, "cp", cleanedContainerID+":"+path, tmpdir)
  144. if err != nil {
  145. t.Fatalf("couldn't copy from absolute path: %s:%s %s", cleanedContainerID, path, err)
  146. }
  147. file, _ := os.Open(tmpname)
  148. defer file.Close()
  149. test, err := ioutil.ReadAll(file)
  150. if err != nil {
  151. t.Fatal(err)
  152. }
  153. if string(test) == cpHostContents {
  154. t.Errorf("output matched host file -- absolute path can escape container rootfs")
  155. }
  156. if string(test) != cpContainerContents {
  157. t.Errorf("output doesn't match the input for absolute path")
  158. }
  159. logDone("cp - absolute paths relative to container's rootfs")
  160. }
  161. // Test for #5619
  162. // Check that absolute symlinks are still relative to the container's rootfs
  163. func TestCpAbsoluteSymlink(t *testing.T) {
  164. out, exitCode, err := dockerCmd(t, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpFullPath+" container_path")
  165. if err != nil || exitCode != 0 {
  166. t.Fatal("failed to create a container", out, err)
  167. }
  168. cleanedContainerID := stripTrailingCharacters(out)
  169. defer deleteContainer(cleanedContainerID)
  170. out, _, err = dockerCmd(t, "wait", cleanedContainerID)
  171. if err != nil || stripTrailingCharacters(out) != "0" {
  172. t.Fatal("failed to set up container", out, err)
  173. }
  174. if err := os.MkdirAll(cpTestPath, os.ModeDir); err != nil {
  175. t.Fatal(err)
  176. }
  177. hostFile, err := os.Create(cpFullPath)
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. defer hostFile.Close()
  182. defer os.RemoveAll(cpTestPathParent)
  183. fmt.Fprintf(hostFile, "%s", cpHostContents)
  184. tmpdir, err := ioutil.TempDir("", "docker-integration")
  185. if err != nil {
  186. t.Fatal(err)
  187. }
  188. tmpname := filepath.Join(tmpdir, cpTestName)
  189. defer os.RemoveAll(tmpdir)
  190. path := filepath.Join("/", "container_path")
  191. _, _, err = dockerCmd(t, "cp", cleanedContainerID+":"+path, tmpdir)
  192. if err != nil {
  193. t.Fatalf("couldn't copy from absolute path: %s:%s %s", cleanedContainerID, path, err)
  194. }
  195. file, _ := os.Open(tmpname)
  196. defer file.Close()
  197. test, err := ioutil.ReadAll(file)
  198. if err != nil {
  199. t.Fatal(err)
  200. }
  201. if string(test) == cpHostContents {
  202. t.Errorf("output matched host file -- absolute symlink can escape container rootfs")
  203. }
  204. if string(test) != cpContainerContents {
  205. t.Errorf("output doesn't match the input for absolute symlink")
  206. }
  207. logDone("cp - absolute symlink relative to container's rootfs")
  208. }
  209. // Test for #5619
  210. // Check that symlinks which are part of the resource path are still relative to the container's rootfs
  211. func TestCpSymlinkComponent(t *testing.T) {
  212. out, exitCode, err := dockerCmd(t, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpTestPath+" container_path")
  213. if err != nil || exitCode != 0 {
  214. t.Fatal("failed to create a container", out, err)
  215. }
  216. cleanedContainerID := stripTrailingCharacters(out)
  217. defer deleteContainer(cleanedContainerID)
  218. out, _, err = dockerCmd(t, "wait", cleanedContainerID)
  219. if err != nil || stripTrailingCharacters(out) != "0" {
  220. t.Fatal("failed to set up container", out, err)
  221. }
  222. if err := os.MkdirAll(cpTestPath, os.ModeDir); err != nil {
  223. t.Fatal(err)
  224. }
  225. hostFile, err := os.Create(cpFullPath)
  226. if err != nil {
  227. t.Fatal(err)
  228. }
  229. defer hostFile.Close()
  230. defer os.RemoveAll(cpTestPathParent)
  231. fmt.Fprintf(hostFile, "%s", cpHostContents)
  232. tmpdir, err := ioutil.TempDir("", "docker-integration")
  233. if err != nil {
  234. t.Fatal(err)
  235. }
  236. tmpname := filepath.Join(tmpdir, cpTestName)
  237. defer os.RemoveAll(tmpdir)
  238. path := filepath.Join("/", "container_path", cpTestName)
  239. _, _, err = dockerCmd(t, "cp", cleanedContainerID+":"+path, tmpdir)
  240. if err != nil {
  241. t.Fatalf("couldn't copy from symlink path component: %s:%s %s", cleanedContainerID, path, err)
  242. }
  243. file, _ := os.Open(tmpname)
  244. defer file.Close()
  245. test, err := ioutil.ReadAll(file)
  246. if err != nil {
  247. t.Fatal(err)
  248. }
  249. if string(test) == cpHostContents {
  250. t.Errorf("output matched host file -- symlink path component can escape container rootfs")
  251. }
  252. if string(test) != cpContainerContents {
  253. t.Errorf("output doesn't match the input for symlink path component")
  254. }
  255. logDone("cp - symlink path components relative to container's rootfs")
  256. }
  257. // Check that cp with unprivileged user doesn't return any error
  258. func TestCpUnprivilegedUser(t *testing.T) {
  259. out, exitCode, err := dockerCmd(t, "run", "-d", "busybox", "/bin/sh", "-c", "touch "+cpTestName)
  260. if err != nil || exitCode != 0 {
  261. t.Fatal("failed to create a container", out, err)
  262. }
  263. cleanedContainerID := stripTrailingCharacters(out)
  264. defer deleteContainer(cleanedContainerID)
  265. out, _, err = dockerCmd(t, "wait", cleanedContainerID)
  266. if err != nil || stripTrailingCharacters(out) != "0" {
  267. t.Fatal("failed to set up container", out, err)
  268. }
  269. tmpdir, err := ioutil.TempDir("", "docker-integration")
  270. if err != nil {
  271. t.Fatal(err)
  272. }
  273. defer os.RemoveAll(tmpdir)
  274. if err = os.Chmod(tmpdir, 0777); err != nil {
  275. t.Fatal(err)
  276. }
  277. path := cpTestName
  278. _, _, err = runCommandWithOutput(exec.Command("su", "unprivilegeduser", "-c", dockerBinary+" cp "+cleanedContainerID+":"+path+" "+tmpdir))
  279. if err != nil {
  280. t.Fatalf("couldn't copy with unprivileged user: %s:%s %s", cleanedContainerID, path, err)
  281. }
  282. logDone("cp - unprivileged user")
  283. }
  284. func TestCpVolumePath(t *testing.T) {
  285. tmpDir, err := ioutil.TempDir("", "cp-test-volumepath")
  286. if err != nil {
  287. t.Fatal(err)
  288. }
  289. defer os.RemoveAll(tmpDir)
  290. outDir, err := ioutil.TempDir("", "cp-test-volumepath-out")
  291. if err != nil {
  292. t.Fatal(err)
  293. }
  294. defer os.RemoveAll(outDir)
  295. _, err = os.Create(tmpDir + "/test")
  296. if err != nil {
  297. t.Fatal(err)
  298. }
  299. out, exitCode, err := dockerCmd(t, "run", "-d", "-v", "/foo", "-v", tmpDir+"/test:/test", "-v", tmpDir+":/baz", "busybox", "/bin/sh", "-c", "touch /foo/bar")
  300. if err != nil || exitCode != 0 {
  301. t.Fatal("failed to create a container", out, err)
  302. }
  303. cleanedContainerID := stripTrailingCharacters(out)
  304. defer deleteContainer(cleanedContainerID)
  305. out, _, err = dockerCmd(t, "wait", cleanedContainerID)
  306. if err != nil || stripTrailingCharacters(out) != "0" {
  307. t.Fatal("failed to set up container", out, err)
  308. }
  309. // Copy actual volume path
  310. _, _, err = dockerCmd(t, "cp", cleanedContainerID+":/foo", outDir)
  311. if err != nil {
  312. t.Fatalf("couldn't copy from volume path: %s:%s %v", cleanedContainerID, "/foo", err)
  313. }
  314. stat, err := os.Stat(outDir + "/foo")
  315. if err != nil {
  316. t.Fatal(err)
  317. }
  318. if !stat.IsDir() {
  319. t.Fatal("expected copied content to be dir")
  320. }
  321. stat, err = os.Stat(outDir + "/foo/bar")
  322. if err != nil {
  323. t.Fatal(err)
  324. }
  325. if stat.IsDir() {
  326. t.Fatal("Expected file `bar` to be a file")
  327. }
  328. // Copy file nested in volume
  329. _, _, err = dockerCmd(t, "cp", cleanedContainerID+":/foo/bar", outDir)
  330. if err != nil {
  331. t.Fatalf("couldn't copy from volume path: %s:%s %v", cleanedContainerID, "/foo", err)
  332. }
  333. stat, err = os.Stat(outDir + "/bar")
  334. if err != nil {
  335. t.Fatal(err)
  336. }
  337. if stat.IsDir() {
  338. t.Fatal("Expected file `bar` to be a file")
  339. }
  340. // Copy Bind-mounted dir
  341. _, _, err = dockerCmd(t, "cp", cleanedContainerID+":/baz", outDir)
  342. if err != nil {
  343. t.Fatalf("couldn't copy from bind-mounted volume path: %s:%s %v", cleanedContainerID, "/baz", err)
  344. }
  345. stat, err = os.Stat(outDir + "/baz")
  346. if err != nil {
  347. t.Fatal(err)
  348. }
  349. if !stat.IsDir() {
  350. t.Fatal("Expected `baz` to be a dir")
  351. }
  352. // Copy file nested in bind-mounted dir
  353. _, _, err = dockerCmd(t, "cp", cleanedContainerID+":/baz/test", outDir)
  354. fb, err := ioutil.ReadFile(outDir + "/baz/test")
  355. if err != nil {
  356. t.Fatal(err)
  357. }
  358. fb2, err := ioutil.ReadFile(tmpDir + "/test")
  359. if err != nil {
  360. t.Fatal(err)
  361. }
  362. if !bytes.Equal(fb, fb2) {
  363. t.Fatalf("Expected copied file to be duplicate of bind-mounted file")
  364. }
  365. // Copy bind-mounted file
  366. _, _, err = dockerCmd(t, "cp", cleanedContainerID+":/test", outDir)
  367. fb, err = ioutil.ReadFile(outDir + "/test")
  368. if err != nil {
  369. t.Fatal(err)
  370. }
  371. fb2, err = ioutil.ReadFile(tmpDir + "/test")
  372. if err != nil {
  373. t.Fatal(err)
  374. }
  375. if !bytes.Equal(fb, fb2) {
  376. t.Fatalf("Expected copied file to be duplicate of bind-mounted file")
  377. }
  378. logDone("cp - volume path")
  379. }
  380. func TestCpToDot(t *testing.T) {
  381. out, exitCode, err := dockerCmd(t, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /test")
  382. if err != nil || exitCode != 0 {
  383. t.Fatal("failed to create a container", out, err)
  384. }
  385. cleanedContainerID := stripTrailingCharacters(out)
  386. defer deleteContainer(cleanedContainerID)
  387. out, _, err = dockerCmd(t, "wait", cleanedContainerID)
  388. if err != nil || stripTrailingCharacters(out) != "0" {
  389. t.Fatal("failed to set up container", out, err)
  390. }
  391. tmpdir, err := ioutil.TempDir("", "docker-integration")
  392. if err != nil {
  393. t.Fatal(err)
  394. }
  395. defer os.RemoveAll(tmpdir)
  396. cwd, err := os.Getwd()
  397. if err != nil {
  398. t.Fatal(err)
  399. }
  400. defer os.Chdir(cwd)
  401. if err := os.Chdir(tmpdir); err != nil {
  402. t.Fatal(err)
  403. }
  404. _, _, err = dockerCmd(t, "cp", cleanedContainerID+":/test", ".")
  405. if err != nil {
  406. t.Fatalf("couldn't docker cp to \".\" path: %s", err)
  407. }
  408. content, err := ioutil.ReadFile("./test")
  409. if string(content) != "lololol\n" {
  410. t.Fatalf("Wrong content in copied file %q, should be %q", content, "lololol\n")
  411. }
  412. logDone("cp - to dot path")
  413. }