docker_cli_cp_test.go 16 KB

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