docker_cli_cp_test.go 15 KB

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