sandbox_dns_unix.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. //go:build !windows
  2. // +build !windows
  3. package libnetwork
  4. import (
  5. "fmt"
  6. "net"
  7. "os"
  8. "path"
  9. "path/filepath"
  10. "strconv"
  11. "strings"
  12. "github.com/docker/docker/libnetwork/etchosts"
  13. "github.com/docker/docker/libnetwork/resolvconf"
  14. "github.com/docker/docker/libnetwork/types"
  15. "github.com/sirupsen/logrus"
  16. )
  17. const (
  18. defaultPrefix = "/var/lib/docker/network/files"
  19. dirPerm = 0755
  20. filePerm = 0644
  21. )
  22. func (sb *sandbox) startResolver(restore bool) {
  23. sb.resolverOnce.Do(func() {
  24. var err error
  25. // The resolver is started with proxyDNS=false if the sandbox does not currently
  26. // have a gateway. So, if the Sandbox is only connected to an 'internal' network,
  27. // it will not forward DNS requests to external resolvers. The resolver's
  28. // proxyDNS setting is then updated as network Endpoints are added/removed.
  29. sb.resolver = NewResolver(resolverIPSandbox, sb.hasExternalConnectivity(), sb.Key(), sb)
  30. defer func() {
  31. if err != nil {
  32. sb.resolver = nil
  33. }
  34. }()
  35. // In the case of live restore container is already running with
  36. // right resolv.conf contents created before. Just update the
  37. // external DNS servers from the restored sandbox for embedded
  38. // server to use.
  39. if !restore {
  40. err = sb.rebuildDNS()
  41. if err != nil {
  42. logrus.Errorf("Updating resolv.conf failed for container %s, %q", sb.ContainerID(), err)
  43. return
  44. }
  45. }
  46. sb.resolver.SetExtServers(sb.extDNS)
  47. if err = sb.osSbox.InvokeFunc(sb.resolver.SetupFunc(0)); err != nil {
  48. logrus.Errorf("Resolver Setup function failed for container %s, %q", sb.ContainerID(), err)
  49. return
  50. }
  51. if err = sb.resolver.Start(); err != nil {
  52. logrus.Errorf("Resolver Start failed for container %s, %q", sb.ContainerID(), err)
  53. }
  54. })
  55. }
  56. func (sb *sandbox) setupResolutionFiles() error {
  57. if err := sb.buildHostsFile(); err != nil {
  58. return err
  59. }
  60. if err := sb.updateParentHosts(); err != nil {
  61. return err
  62. }
  63. return sb.setupDNS()
  64. }
  65. func (sb *sandbox) buildHostsFile() error {
  66. if sb.config.hostsPath == "" {
  67. sb.config.hostsPath = defaultPrefix + "/" + sb.id + "/hosts"
  68. }
  69. dir, _ := filepath.Split(sb.config.hostsPath)
  70. if err := createBasePath(dir); err != nil {
  71. return err
  72. }
  73. // This is for the host mode networking
  74. if sb.config.useDefaultSandBox && len(sb.config.extraHosts) == 0 {
  75. // We are working under the assumption that the origin file option had been properly expressed by the upper layer
  76. // if not here we are going to error out
  77. if err := copyFile(sb.config.originHostsPath, sb.config.hostsPath); err != nil && !os.IsNotExist(err) {
  78. return types.InternalErrorf("could not copy source hosts file %s to %s: %v", sb.config.originHostsPath, sb.config.hostsPath, err)
  79. }
  80. return nil
  81. }
  82. extraContent := make([]etchosts.Record, 0, len(sb.config.extraHosts))
  83. for _, extraHost := range sb.config.extraHosts {
  84. extraContent = append(extraContent, etchosts.Record{Hosts: extraHost.name, IP: extraHost.IP})
  85. }
  86. return etchosts.Build(sb.config.hostsPath, "", sb.config.hostName, sb.config.domainName, extraContent)
  87. }
  88. func (sb *sandbox) updateHostsFile(ifaceIPs []string) error {
  89. if len(ifaceIPs) == 0 {
  90. return nil
  91. }
  92. if sb.config.originHostsPath != "" {
  93. return nil
  94. }
  95. // User might have provided a FQDN in hostname or split it across hostname
  96. // and domainname. We want the FQDN and the bare hostname.
  97. fqdn := sb.config.hostName
  98. mhost := sb.config.hostName
  99. if sb.config.domainName != "" {
  100. fqdn = fmt.Sprintf("%s.%s", fqdn, sb.config.domainName)
  101. }
  102. parts := strings.SplitN(fqdn, ".", 2)
  103. if len(parts) == 2 {
  104. mhost = fmt.Sprintf("%s %s", fqdn, parts[0])
  105. }
  106. var extraContent []etchosts.Record
  107. for _, ip := range ifaceIPs {
  108. extraContent = append(extraContent, etchosts.Record{Hosts: mhost, IP: ip})
  109. }
  110. sb.addHostsEntries(extraContent)
  111. return nil
  112. }
  113. func (sb *sandbox) addHostsEntries(recs []etchosts.Record) {
  114. if err := etchosts.Add(sb.config.hostsPath, recs); err != nil {
  115. logrus.Warnf("Failed adding service host entries to the running container: %v", err)
  116. }
  117. }
  118. func (sb *sandbox) deleteHostsEntries(recs []etchosts.Record) {
  119. if err := etchosts.Delete(sb.config.hostsPath, recs); err != nil {
  120. logrus.Warnf("Failed deleting service host entries to the running container: %v", err)
  121. }
  122. }
  123. func (sb *sandbox) updateParentHosts() error {
  124. var pSb Sandbox
  125. for _, update := range sb.config.parentUpdates {
  126. sb.controller.WalkSandboxes(SandboxContainerWalker(&pSb, update.cid))
  127. if pSb == nil {
  128. continue
  129. }
  130. if err := etchosts.Update(pSb.(*sandbox).config.hostsPath, update.ip, update.name); err != nil {
  131. return err
  132. }
  133. }
  134. return nil
  135. }
  136. func (sb *sandbox) restorePath() {
  137. if sb.config.resolvConfPath == "" {
  138. sb.config.resolvConfPath = defaultPrefix + "/" + sb.id + "/resolv.conf"
  139. }
  140. sb.config.resolvConfHashFile = sb.config.resolvConfPath + ".hash"
  141. if sb.config.hostsPath == "" {
  142. sb.config.hostsPath = defaultPrefix + "/" + sb.id + "/hosts"
  143. }
  144. }
  145. func (sb *sandbox) setExternalResolvers(content []byte, addrType int, checkLoopback bool) {
  146. servers := resolvconf.GetNameservers(content, addrType)
  147. for _, ip := range servers {
  148. hostLoopback := false
  149. if checkLoopback && isIPv4Loopback(ip) {
  150. hostLoopback = true
  151. }
  152. sb.extDNS = append(sb.extDNS, extDNSEntry{
  153. IPStr: ip,
  154. HostLoopback: hostLoopback,
  155. })
  156. }
  157. }
  158. // isIPv4Loopback checks if the given IP address is an IPv4 loopback address.
  159. // It's based on the logic in Go's net.IP.IsLoopback(), but only the IPv4 part:
  160. // https://github.com/golang/go/blob/go1.16.6/src/net/ip.go#L120-L126
  161. func isIPv4Loopback(ipAddress string) bool {
  162. if ip := net.ParseIP(ipAddress); ip != nil {
  163. if ip4 := ip.To4(); ip4 != nil {
  164. return ip4[0] == 127
  165. }
  166. }
  167. return false
  168. }
  169. func (sb *sandbox) setupDNS() error {
  170. var newRC *resolvconf.File
  171. if sb.config.resolvConfPath == "" {
  172. sb.config.resolvConfPath = defaultPrefix + "/" + sb.id + "/resolv.conf"
  173. }
  174. sb.config.resolvConfHashFile = sb.config.resolvConfPath + ".hash"
  175. dir, _ := filepath.Split(sb.config.resolvConfPath)
  176. if err := createBasePath(dir); err != nil {
  177. return err
  178. }
  179. // When the user specify a conainter in the host namespace and do no have any dns option specified
  180. // we just copy the host resolv.conf from the host itself
  181. if sb.config.useDefaultSandBox && len(sb.config.dnsList) == 0 && len(sb.config.dnsSearchList) == 0 && len(sb.config.dnsOptionsList) == 0 {
  182. // We are working under the assumption that the origin file option had been properly expressed by the upper layer
  183. // if not here we are going to error out
  184. if err := copyFile(sb.config.originResolvConfPath, sb.config.resolvConfPath); err != nil {
  185. if !os.IsNotExist(err) {
  186. return fmt.Errorf("could not copy source resolv.conf file %s to %s: %v", sb.config.originResolvConfPath, sb.config.resolvConfPath, err)
  187. }
  188. logrus.Infof("%s does not exist, we create an empty resolv.conf for container", sb.config.originResolvConfPath)
  189. if err := createFile(sb.config.resolvConfPath); err != nil {
  190. return err
  191. }
  192. }
  193. return nil
  194. }
  195. originResolvConfPath := sb.config.originResolvConfPath
  196. if originResolvConfPath == "" {
  197. // fallback if not specified
  198. originResolvConfPath = resolvconf.Path()
  199. }
  200. currRC, err := resolvconf.GetSpecific(originResolvConfPath)
  201. if err != nil {
  202. if !os.IsNotExist(err) {
  203. return err
  204. }
  205. // it's ok to continue if /etc/resolv.conf doesn't exist, default resolvers (Google's Public DNS)
  206. // will be used
  207. currRC = &resolvconf.File{}
  208. logrus.Infof("/etc/resolv.conf does not exist")
  209. }
  210. if len(sb.config.dnsList) > 0 || len(sb.config.dnsSearchList) > 0 || len(sb.config.dnsOptionsList) > 0 {
  211. var (
  212. err error
  213. dnsList = resolvconf.GetNameservers(currRC.Content, resolvconf.IP)
  214. dnsSearchList = resolvconf.GetSearchDomains(currRC.Content)
  215. dnsOptionsList = resolvconf.GetOptions(currRC.Content)
  216. )
  217. if len(sb.config.dnsList) > 0 {
  218. dnsList = sb.config.dnsList
  219. }
  220. if len(sb.config.dnsSearchList) > 0 {
  221. dnsSearchList = sb.config.dnsSearchList
  222. }
  223. if len(sb.config.dnsOptionsList) > 0 {
  224. dnsOptionsList = sb.config.dnsOptionsList
  225. }
  226. newRC, err = resolvconf.Build(sb.config.resolvConfPath, dnsList, dnsSearchList, dnsOptionsList)
  227. if err != nil {
  228. return err
  229. }
  230. // After building the resolv.conf from the user config save the
  231. // external resolvers in the sandbox. Note that --dns 127.0.0.x
  232. // config refers to the loopback in the container namespace
  233. sb.setExternalResolvers(newRC.Content, resolvconf.IPv4, len(sb.config.dnsList) == 0)
  234. } else {
  235. // If the host resolv.conf file has 127.0.0.x container should
  236. // use the host resolver for queries. This is supported by the
  237. // docker embedded DNS server. Hence save the external resolvers
  238. // before filtering it out.
  239. sb.setExternalResolvers(currRC.Content, resolvconf.IPv4, true)
  240. // Replace any localhost/127.* (at this point we have no info about ipv6, pass it as true)
  241. if newRC, err = resolvconf.FilterResolvDNS(currRC.Content, true); err != nil {
  242. return err
  243. }
  244. // No contention on container resolv.conf file at sandbox creation
  245. if err := os.WriteFile(sb.config.resolvConfPath, newRC.Content, filePerm); err != nil {
  246. return types.InternalErrorf("failed to write unhaltered resolv.conf file content when setting up dns for sandbox %s: %v", sb.ID(), err)
  247. }
  248. }
  249. // Write hash
  250. if err := os.WriteFile(sb.config.resolvConfHashFile, []byte(newRC.Hash), filePerm); err != nil {
  251. return types.InternalErrorf("failed to write resolv.conf hash file when setting up dns for sandbox %s: %v", sb.ID(), err)
  252. }
  253. return nil
  254. }
  255. func (sb *sandbox) updateDNS(ipv6Enabled bool) error {
  256. var (
  257. currHash string
  258. hashFile = sb.config.resolvConfHashFile
  259. )
  260. // This is for the host mode networking
  261. if sb.config.useDefaultSandBox {
  262. return nil
  263. }
  264. if len(sb.config.dnsList) > 0 || len(sb.config.dnsSearchList) > 0 || len(sb.config.dnsOptionsList) > 0 {
  265. return nil
  266. }
  267. currRC, err := resolvconf.GetSpecific(sb.config.resolvConfPath)
  268. if err != nil {
  269. if !os.IsNotExist(err) {
  270. return err
  271. }
  272. } else {
  273. h, err := os.ReadFile(hashFile)
  274. if err != nil {
  275. if !os.IsNotExist(err) {
  276. return err
  277. }
  278. } else {
  279. currHash = string(h)
  280. }
  281. }
  282. if currHash != "" && currHash != currRC.Hash {
  283. // Seems the user has changed the container resolv.conf since the last time
  284. // we checked so return without doing anything.
  285. // logrus.Infof("Skipping update of resolv.conf file with ipv6Enabled: %t because file was touched by user", ipv6Enabled)
  286. return nil
  287. }
  288. // replace any localhost/127.* and remove IPv6 nameservers if IPv6 disabled.
  289. newRC, err := resolvconf.FilterResolvDNS(currRC.Content, ipv6Enabled)
  290. if err != nil {
  291. return err
  292. }
  293. err = os.WriteFile(sb.config.resolvConfPath, newRC.Content, 0644) //nolint:gosec // gosec complains about perms here, which must be 0644 in this case
  294. if err != nil {
  295. return err
  296. }
  297. // write the new hash in a temp file and rename it to make the update atomic
  298. dir := path.Dir(sb.config.resolvConfPath)
  299. tmpHashFile, err := os.CreateTemp(dir, "hash")
  300. if err != nil {
  301. return err
  302. }
  303. if err = tmpHashFile.Chmod(filePerm); err != nil {
  304. tmpHashFile.Close()
  305. return err
  306. }
  307. _, err = tmpHashFile.Write([]byte(newRC.Hash))
  308. if err1 := tmpHashFile.Close(); err == nil {
  309. err = err1
  310. }
  311. if err != nil {
  312. return err
  313. }
  314. return os.Rename(tmpHashFile.Name(), hashFile)
  315. }
  316. // Embedded DNS server has to be enabled for this sandbox. Rebuild the container's
  317. // resolv.conf by doing the following
  318. // - Add only the embedded server's IP to container's resolv.conf
  319. // - If the embedded server needs any resolv.conf options add it to the current list
  320. func (sb *sandbox) rebuildDNS() error {
  321. currRC, err := resolvconf.GetSpecific(sb.config.resolvConfPath)
  322. if err != nil {
  323. return err
  324. }
  325. if len(sb.extDNS) == 0 {
  326. sb.setExternalResolvers(currRC.Content, resolvconf.IPv4, false)
  327. }
  328. var (
  329. dnsList = []string{sb.resolver.NameServer()}
  330. dnsOptionsList = resolvconf.GetOptions(currRC.Content)
  331. dnsSearchList = resolvconf.GetSearchDomains(currRC.Content)
  332. )
  333. // external v6 DNS servers has to be listed in resolv.conf
  334. dnsList = append(dnsList, resolvconf.GetNameservers(currRC.Content, resolvconf.IPv6)...)
  335. // If the user config and embedded DNS server both have ndots option set,
  336. // remember the user's config so that unqualified names not in the docker
  337. // domain can be dropped.
  338. resOptions := sb.resolver.ResolverOptions()
  339. dnsOpt:
  340. for _, resOpt := range resOptions {
  341. if strings.Contains(resOpt, "ndots") {
  342. for _, option := range dnsOptionsList {
  343. if strings.Contains(option, "ndots") {
  344. parts := strings.Split(option, ":")
  345. if len(parts) != 2 {
  346. return fmt.Errorf("invalid ndots option %v", option)
  347. }
  348. if num, err := strconv.Atoi(parts[1]); err != nil {
  349. return fmt.Errorf("invalid number for ndots option: %v", parts[1])
  350. } else if num >= 0 {
  351. // if the user sets ndots, use the user setting
  352. sb.ndotsSet = true
  353. break dnsOpt
  354. } else {
  355. return fmt.Errorf("invalid number for ndots option: %v", num)
  356. }
  357. }
  358. }
  359. }
  360. }
  361. if !sb.ndotsSet {
  362. // if the user did not set the ndots, set it to 0 to prioritize the service name resolution
  363. // Ref: https://linux.die.net/man/5/resolv.conf
  364. dnsOptionsList = append(dnsOptionsList, resOptions...)
  365. }
  366. _, err = resolvconf.Build(sb.config.resolvConfPath, dnsList, dnsSearchList, dnsOptionsList)
  367. return err
  368. }
  369. func createBasePath(dir string) error {
  370. return os.MkdirAll(dir, dirPerm)
  371. }
  372. func createFile(path string) error {
  373. var f *os.File
  374. dir, _ := filepath.Split(path)
  375. err := createBasePath(dir)
  376. if err != nil {
  377. return err
  378. }
  379. f, err = os.Create(path)
  380. if err == nil {
  381. f.Close()
  382. }
  383. return err
  384. }
  385. func copyFile(src, dst string) error {
  386. sBytes, err := os.ReadFile(src)
  387. if err != nil {
  388. return err
  389. }
  390. return os.WriteFile(dst, sBytes, filePerm)
  391. }