docker_cli_cp_test.go 16 KB

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