docker_cli_cp_test.go 15 KB

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