oci_linux.go 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "sort"
  8. "strconv"
  9. "strings"
  10. cdcgroups "github.com/containerd/cgroups/v3"
  11. "github.com/containerd/containerd/containers"
  12. coci "github.com/containerd/containerd/oci"
  13. "github.com/containerd/containerd/pkg/apparmor"
  14. "github.com/containerd/containerd/pkg/userns"
  15. "github.com/containerd/log"
  16. containertypes "github.com/docker/docker/api/types/container"
  17. "github.com/docker/docker/container"
  18. dconfig "github.com/docker/docker/daemon/config"
  19. "github.com/docker/docker/errdefs"
  20. "github.com/docker/docker/oci"
  21. "github.com/docker/docker/oci/caps"
  22. "github.com/docker/docker/pkg/idtools"
  23. "github.com/docker/docker/pkg/rootless/specconv"
  24. "github.com/docker/docker/pkg/stringid"
  25. volumemounts "github.com/docker/docker/volume/mounts"
  26. "github.com/moby/sys/mount"
  27. "github.com/moby/sys/mountinfo"
  28. "github.com/moby/sys/user"
  29. "github.com/opencontainers/runc/libcontainer/cgroups"
  30. specs "github.com/opencontainers/runtime-spec/specs-go"
  31. "github.com/pkg/errors"
  32. "golang.org/x/sys/unix"
  33. )
  34. const inContainerInitPath = "/sbin/" + dconfig.DefaultInitBinary
  35. // withRlimits sets the container's rlimits along with merging the daemon's rlimits
  36. func withRlimits(daemon *Daemon, daemonCfg *dconfig.Config, c *container.Container) coci.SpecOpts {
  37. return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
  38. var rlimits []specs.POSIXRlimit
  39. // We want to leave the original HostConfig alone so make a copy here
  40. hostConfig := *c.HostConfig
  41. // Merge with the daemon defaults
  42. daemon.mergeUlimits(&hostConfig, daemonCfg)
  43. for _, ul := range hostConfig.Ulimits {
  44. rlimits = append(rlimits, specs.POSIXRlimit{
  45. Type: "RLIMIT_" + strings.ToUpper(ul.Name),
  46. Soft: uint64(ul.Soft),
  47. Hard: uint64(ul.Hard),
  48. })
  49. }
  50. if s.Process == nil {
  51. s.Process = &specs.Process{}
  52. }
  53. s.Process.Rlimits = rlimits
  54. return nil
  55. }
  56. }
  57. // withLibnetwork sets the libnetwork hook
  58. func withLibnetwork(daemon *Daemon, daemonCfg *dconfig.Config, c *container.Container) coci.SpecOpts {
  59. return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
  60. if c.Config.NetworkDisabled {
  61. return nil
  62. }
  63. for _, ns := range s.Linux.Namespaces {
  64. if ns.Type == specs.NetworkNamespace && ns.Path == "" {
  65. if s.Hooks == nil {
  66. s.Hooks = &specs.Hooks{}
  67. }
  68. shortNetCtlrID := stringid.TruncateID(daemon.netController.ID())
  69. s.Hooks.Prestart = append(s.Hooks.Prestart, specs.Hook{
  70. Path: filepath.Join("/proc", strconv.Itoa(os.Getpid()), "exe"),
  71. Args: []string{"libnetwork-setkey", "-exec-root=" + daemonCfg.GetExecRoot(), c.ID, shortNetCtlrID},
  72. })
  73. }
  74. }
  75. return nil
  76. }
  77. }
  78. // withRootless sets the spec to the rootless configuration
  79. func withRootless(daemon *Daemon, daemonCfg *dconfig.Config) coci.SpecOpts {
  80. return func(_ context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
  81. var v2Controllers []string
  82. if cgroupDriver(daemonCfg) == cgroupSystemdDriver {
  83. if cdcgroups.Mode() != cdcgroups.Unified {
  84. return errors.New("rootless systemd driver doesn't support cgroup v1")
  85. }
  86. rootlesskitParentEUID := os.Getenv("ROOTLESSKIT_PARENT_EUID")
  87. if rootlesskitParentEUID == "" {
  88. return errors.New("$ROOTLESSKIT_PARENT_EUID is not set (requires RootlessKit v0.8.0)")
  89. }
  90. euid, err := strconv.Atoi(rootlesskitParentEUID)
  91. if err != nil {
  92. return errors.Wrap(err, "invalid $ROOTLESSKIT_PARENT_EUID: must be a numeric value")
  93. }
  94. controllersPath := fmt.Sprintf("/sys/fs/cgroup/user.slice/user-%d.slice/cgroup.controllers", euid)
  95. controllersFile, err := os.ReadFile(controllersPath)
  96. if err != nil {
  97. return err
  98. }
  99. v2Controllers = strings.Fields(string(controllersFile))
  100. }
  101. return specconv.ToRootless(s, v2Controllers)
  102. }
  103. }
  104. // withRootfulInRootless is used for "rootful-in-rootless" dind;
  105. // the daemon is running in UserNS but has no access to RootlessKit API socket, host filesystem, etc.
  106. func withRootfulInRootless(daemon *Daemon, daemonCfg *dconfig.Config) coci.SpecOpts {
  107. return func(_ context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
  108. specconv.ToRootfulInRootless(s)
  109. return nil
  110. }
  111. }
  112. // WithOOMScore sets the oom score
  113. func WithOOMScore(score *int) coci.SpecOpts {
  114. return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
  115. if s.Process == nil {
  116. s.Process = &specs.Process{}
  117. }
  118. s.Process.OOMScoreAdj = score
  119. return nil
  120. }
  121. }
  122. // WithSelinux sets the selinux labels
  123. func WithSelinux(c *container.Container) coci.SpecOpts {
  124. return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
  125. if s.Process == nil {
  126. s.Process = &specs.Process{}
  127. }
  128. if s.Linux == nil {
  129. s.Linux = &specs.Linux{}
  130. }
  131. s.Process.SelinuxLabel = c.GetProcessLabel()
  132. s.Linux.MountLabel = c.MountLabel
  133. return nil
  134. }
  135. }
  136. // WithApparmor sets the apparmor profile
  137. func WithApparmor(c *container.Container) coci.SpecOpts {
  138. return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
  139. if apparmor.HostSupports() {
  140. var appArmorProfile string
  141. if c.AppArmorProfile != "" {
  142. appArmorProfile = c.AppArmorProfile
  143. } else if c.HostConfig.Privileged {
  144. appArmorProfile = unconfinedAppArmorProfile
  145. } else {
  146. appArmorProfile = defaultAppArmorProfile
  147. }
  148. if appArmorProfile == defaultAppArmorProfile {
  149. // Unattended upgrades and other fun services can unload AppArmor
  150. // profiles inadvertently. Since we cannot store our profile in
  151. // /etc/apparmor.d, nor can we practically add other ways of
  152. // telling the system to keep our profile loaded, in order to make
  153. // sure that we keep the default profile enabled we dynamically
  154. // reload it if necessary.
  155. if err := ensureDefaultAppArmorProfile(); err != nil {
  156. return err
  157. }
  158. }
  159. if s.Process == nil {
  160. s.Process = &specs.Process{}
  161. }
  162. s.Process.ApparmorProfile = appArmorProfile
  163. }
  164. return nil
  165. }
  166. }
  167. // WithCapabilities sets the container's capabilties
  168. func WithCapabilities(c *container.Container) coci.SpecOpts {
  169. return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
  170. capabilities, err := caps.TweakCapabilities(
  171. caps.DefaultCapabilities(),
  172. c.HostConfig.CapAdd,
  173. c.HostConfig.CapDrop,
  174. c.HostConfig.Privileged,
  175. )
  176. if err != nil {
  177. return err
  178. }
  179. return oci.SetCapabilities(s, capabilities)
  180. }
  181. }
  182. func resourcePath(c *container.Container, getPath func() (string, error)) (string, error) {
  183. p, err := getPath()
  184. if err != nil {
  185. return "", err
  186. }
  187. return c.GetResourcePath(p)
  188. }
  189. func getUser(c *container.Container, username string) (specs.User, error) {
  190. var usr specs.User
  191. passwdPath, err := resourcePath(c, user.GetPasswdPath)
  192. if err != nil {
  193. return usr, err
  194. }
  195. groupPath, err := resourcePath(c, user.GetGroupPath)
  196. if err != nil {
  197. return usr, err
  198. }
  199. execUser, err := user.GetExecUserPath(username, nil, passwdPath, groupPath)
  200. if err != nil {
  201. return usr, err
  202. }
  203. usr.UID = uint32(execUser.Uid)
  204. usr.GID = uint32(execUser.Gid)
  205. usr.AdditionalGids = []uint32{usr.GID}
  206. var addGroups []int
  207. if len(c.HostConfig.GroupAdd) > 0 {
  208. addGroups, err = user.GetAdditionalGroupsPath(c.HostConfig.GroupAdd, groupPath)
  209. if err != nil {
  210. return usr, err
  211. }
  212. }
  213. for _, g := range append(execUser.Sgids, addGroups...) {
  214. usr.AdditionalGids = append(usr.AdditionalGids, uint32(g))
  215. }
  216. return usr, nil
  217. }
  218. func setNamespace(s *specs.Spec, ns specs.LinuxNamespace) {
  219. if s.Linux == nil {
  220. s.Linux = &specs.Linux{}
  221. }
  222. for i, n := range s.Linux.Namespaces {
  223. if n.Type == ns.Type {
  224. s.Linux.Namespaces[i] = ns
  225. return
  226. }
  227. }
  228. s.Linux.Namespaces = append(s.Linux.Namespaces, ns)
  229. }
  230. // WithNamespaces sets the container's namespaces
  231. func WithNamespaces(daemon *Daemon, c *container.Container) coci.SpecOpts {
  232. return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
  233. userNS := false
  234. // user
  235. if c.HostConfig.UsernsMode.IsPrivate() {
  236. if uidMap := daemon.idMapping.UIDMaps; uidMap != nil {
  237. userNS = true
  238. setNamespace(s, specs.LinuxNamespace{
  239. Type: specs.UserNamespace,
  240. })
  241. s.Linux.UIDMappings = specMapping(uidMap)
  242. s.Linux.GIDMappings = specMapping(daemon.idMapping.GIDMaps)
  243. }
  244. }
  245. // network
  246. if !c.Config.NetworkDisabled {
  247. networkMode := c.HostConfig.NetworkMode
  248. switch {
  249. case networkMode.IsContainer():
  250. nc, err := daemon.getNetworkedContainer(c.ID, networkMode.ConnectedContainer())
  251. if err != nil {
  252. return err
  253. }
  254. setNamespace(s, specs.LinuxNamespace{
  255. Type: specs.NetworkNamespace,
  256. Path: fmt.Sprintf("/proc/%d/ns/net", nc.State.GetPID()),
  257. })
  258. if userNS {
  259. // to share a net namespace, the containers must also share a user namespace.
  260. //
  261. // FIXME(thaJeztah): this will silently overwrite an earlier user namespace when joining multiple containers: https://github.com/moby/moby/issues/46210
  262. setNamespace(s, specs.LinuxNamespace{
  263. Type: specs.UserNamespace,
  264. Path: fmt.Sprintf("/proc/%d/ns/user", nc.State.GetPID()),
  265. })
  266. }
  267. case networkMode.IsHost():
  268. setNamespace(s, specs.LinuxNamespace{
  269. Type: specs.NetworkNamespace,
  270. Path: c.NetworkSettings.SandboxKey,
  271. })
  272. default:
  273. setNamespace(s, specs.LinuxNamespace{
  274. Type: specs.NetworkNamespace,
  275. })
  276. }
  277. }
  278. // ipc
  279. ipcMode := c.HostConfig.IpcMode
  280. if !ipcMode.Valid() {
  281. return errdefs.InvalidParameter(errors.Errorf("invalid IPC mode: %v", ipcMode))
  282. }
  283. switch {
  284. case ipcMode.IsContainer():
  285. ic, err := daemon.getIPCContainer(ipcMode.Container())
  286. if err != nil {
  287. return errors.Wrap(err, "failed to join IPC namespace")
  288. }
  289. setNamespace(s, specs.LinuxNamespace{
  290. Type: specs.IPCNamespace,
  291. Path: fmt.Sprintf("/proc/%d/ns/ipc", ic.State.GetPID()),
  292. })
  293. if userNS {
  294. // to share a IPC namespace, the containers must also share a user namespace.
  295. //
  296. // FIXME(thaJeztah): this will silently overwrite an earlier user namespace when joining multiple containers: https://github.com/moby/moby/issues/46210
  297. setNamespace(s, specs.LinuxNamespace{
  298. Type: specs.UserNamespace,
  299. Path: fmt.Sprintf("/proc/%d/ns/user", ic.State.GetPID()),
  300. })
  301. }
  302. case ipcMode.IsHost():
  303. oci.RemoveNamespace(s, specs.IPCNamespace)
  304. case ipcMode.IsEmpty():
  305. // A container was created by an older version of the daemon.
  306. // The default behavior used to be what is now called "shareable".
  307. fallthrough
  308. case ipcMode.IsPrivate(), ipcMode.IsShareable(), ipcMode.IsNone():
  309. setNamespace(s, specs.LinuxNamespace{
  310. Type: specs.IPCNamespace,
  311. })
  312. }
  313. // pid
  314. pidMode := c.HostConfig.PidMode
  315. if !pidMode.Valid() {
  316. return errdefs.InvalidParameter(errors.Errorf("invalid PID mode: %v", pidMode))
  317. }
  318. switch {
  319. case pidMode.IsContainer():
  320. pc, err := daemon.getPIDContainer(pidMode.Container())
  321. if err != nil {
  322. return errors.Wrap(err, "failed to join PID namespace")
  323. }
  324. setNamespace(s, specs.LinuxNamespace{
  325. Type: specs.PIDNamespace,
  326. Path: fmt.Sprintf("/proc/%d/ns/pid", pc.State.GetPID()),
  327. })
  328. if userNS {
  329. // to share a PID namespace, the containers must also share a user namespace.
  330. //
  331. // FIXME(thaJeztah): this will silently overwrite an earlier user namespace when joining multiple containers: https://github.com/moby/moby/issues/46210
  332. setNamespace(s, specs.LinuxNamespace{
  333. Type: specs.UserNamespace,
  334. Path: fmt.Sprintf("/proc/%d/ns/user", pc.State.GetPID()),
  335. })
  336. }
  337. case pidMode.IsHost():
  338. oci.RemoveNamespace(s, specs.PIDNamespace)
  339. default:
  340. setNamespace(s, specs.LinuxNamespace{
  341. Type: specs.PIDNamespace,
  342. })
  343. }
  344. // uts
  345. if !c.HostConfig.UTSMode.Valid() {
  346. return errdefs.InvalidParameter(errors.Errorf("invalid UTS mode: %v", c.HostConfig.UTSMode))
  347. }
  348. if c.HostConfig.UTSMode.IsHost() {
  349. oci.RemoveNamespace(s, specs.UTSNamespace)
  350. s.Hostname = ""
  351. }
  352. // cgroup
  353. if !c.HostConfig.CgroupnsMode.Valid() {
  354. return errdefs.InvalidParameter(errors.Errorf("invalid cgroup namespace mode: %v", c.HostConfig.CgroupnsMode))
  355. }
  356. if c.HostConfig.CgroupnsMode.IsPrivate() {
  357. setNamespace(s, specs.LinuxNamespace{
  358. Type: specs.CgroupNamespace,
  359. })
  360. }
  361. return nil
  362. }
  363. }
  364. func specMapping(s []idtools.IDMap) []specs.LinuxIDMapping {
  365. var ids []specs.LinuxIDMapping
  366. for _, item := range s {
  367. ids = append(ids, specs.LinuxIDMapping{
  368. HostID: uint32(item.HostID),
  369. ContainerID: uint32(item.ContainerID),
  370. Size: uint32(item.Size),
  371. })
  372. }
  373. return ids
  374. }
  375. // Get the source mount point of directory passed in as argument. Also return
  376. // optional fields.
  377. func getSourceMount(source string) (string, string, error) {
  378. // Ensure any symlinks are resolved.
  379. sourcePath, err := filepath.EvalSymlinks(source)
  380. if err != nil {
  381. return "", "", err
  382. }
  383. mi, err := mountinfo.GetMounts(mountinfo.ParentsFilter(sourcePath))
  384. if err != nil {
  385. return "", "", err
  386. }
  387. if len(mi) < 1 {
  388. return "", "", fmt.Errorf("Can't find mount point of %s", source)
  389. }
  390. // find the longest mount point
  391. var idx, maxlen int
  392. for i := range mi {
  393. if len(mi[i].Mountpoint) > maxlen {
  394. maxlen = len(mi[i].Mountpoint)
  395. idx = i
  396. }
  397. }
  398. return mi[idx].Mountpoint, mi[idx].Optional, nil
  399. }
  400. const (
  401. sharedPropagationOption = "shared:"
  402. slavePropagationOption = "master:"
  403. )
  404. // hasMountInfoOption checks if any of the passed any of the given option values
  405. // are set in the passed in option string.
  406. func hasMountInfoOption(opts string, vals ...string) bool {
  407. for _, opt := range strings.Split(opts, " ") {
  408. for _, val := range vals {
  409. if strings.HasPrefix(opt, val) {
  410. return true
  411. }
  412. }
  413. }
  414. return false
  415. }
  416. // Ensure mount point on which path is mounted, is shared.
  417. func ensureShared(path string) error {
  418. sourceMount, optionalOpts, err := getSourceMount(path)
  419. if err != nil {
  420. return err
  421. }
  422. // Make sure source mount point is shared.
  423. if !hasMountInfoOption(optionalOpts, sharedPropagationOption) {
  424. return errors.Errorf("path %s is mounted on %s but it is not a shared mount", path, sourceMount)
  425. }
  426. return nil
  427. }
  428. // Ensure mount point on which path is mounted, is either shared or slave.
  429. func ensureSharedOrSlave(path string) error {
  430. sourceMount, optionalOpts, err := getSourceMount(path)
  431. if err != nil {
  432. return err
  433. }
  434. if !hasMountInfoOption(optionalOpts, sharedPropagationOption, slavePropagationOption) {
  435. return errors.Errorf("path %s is mounted on %s but it is not a shared or slave mount", path, sourceMount)
  436. }
  437. return nil
  438. }
  439. // Get the set of mount flags that are set on the mount that contains the given
  440. // path and are locked by CL_UNPRIVILEGED. This is necessary to ensure that
  441. // bind-mounting "with options" will not fail with user namespaces, due to
  442. // kernel restrictions that require user namespace mounts to preserve
  443. // CL_UNPRIVILEGED locked flags.
  444. func getUnprivilegedMountFlags(path string) ([]string, error) {
  445. var statfs unix.Statfs_t
  446. if err := unix.Statfs(path, &statfs); err != nil {
  447. return nil, err
  448. }
  449. // The set of keys come from https://github.com/torvalds/linux/blob/v4.13/fs/namespace.c#L1034-L1048.
  450. unprivilegedFlags := map[uint64]string{
  451. unix.MS_RDONLY: "ro",
  452. unix.MS_NODEV: "nodev",
  453. unix.MS_NOEXEC: "noexec",
  454. unix.MS_NOSUID: "nosuid",
  455. unix.MS_NOATIME: "noatime",
  456. unix.MS_RELATIME: "relatime",
  457. unix.MS_NODIRATIME: "nodiratime",
  458. }
  459. var flags []string
  460. for mask, flag := range unprivilegedFlags {
  461. if uint64(statfs.Flags)&mask == mask {
  462. flags = append(flags, flag)
  463. }
  464. }
  465. return flags, nil
  466. }
  467. var (
  468. mountPropagationMap = map[string]int{
  469. "private": mount.PRIVATE,
  470. "rprivate": mount.RPRIVATE,
  471. "shared": mount.SHARED,
  472. "rshared": mount.RSHARED,
  473. "slave": mount.SLAVE,
  474. "rslave": mount.RSLAVE,
  475. }
  476. mountPropagationReverseMap = map[int]string{
  477. mount.PRIVATE: "private",
  478. mount.RPRIVATE: "rprivate",
  479. mount.SHARED: "shared",
  480. mount.RSHARED: "rshared",
  481. mount.SLAVE: "slave",
  482. mount.RSLAVE: "rslave",
  483. }
  484. )
  485. // inSlice tests whether a string is contained in a slice of strings or not.
  486. // Comparison is case sensitive
  487. func inSlice(slice []string, s string) bool {
  488. for _, ss := range slice {
  489. if s == ss {
  490. return true
  491. }
  492. }
  493. return false
  494. }
  495. // withMounts sets the container's mounts
  496. func withMounts(daemon *Daemon, daemonCfg *configStore, c *container.Container) coci.SpecOpts {
  497. return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) (err error) {
  498. if err := daemon.setupContainerMountsRoot(c); err != nil {
  499. return err
  500. }
  501. if err := daemon.setupIPCDirs(c); err != nil {
  502. return err
  503. }
  504. defer func() {
  505. if err != nil {
  506. daemon.cleanupSecretDir(c)
  507. }
  508. }()
  509. if err := daemon.setupSecretDir(c); err != nil {
  510. return err
  511. }
  512. ms, err := daemon.setupMounts(c)
  513. if err != nil {
  514. return err
  515. }
  516. if !c.HostConfig.IpcMode.IsPrivate() && !c.HostConfig.IpcMode.IsEmpty() {
  517. ms = append(ms, c.IpcMounts()...)
  518. }
  519. tmpfsMounts, err := c.TmpfsMounts()
  520. if err != nil {
  521. return err
  522. }
  523. ms = append(ms, tmpfsMounts...)
  524. secretMounts, err := c.SecretMounts()
  525. if err != nil {
  526. return err
  527. }
  528. ms = append(ms, secretMounts...)
  529. sort.Sort(mounts(ms))
  530. mounts := ms
  531. userMounts := make(map[string]struct{})
  532. for _, m := range mounts {
  533. userMounts[m.Destination] = struct{}{}
  534. }
  535. // Copy all mounts from spec to defaultMounts, except for
  536. // - mounts overridden by a user supplied mount;
  537. // - all mounts under /dev if a user supplied /dev is present;
  538. // - /dev/shm, in case IpcMode is none.
  539. // While at it, also
  540. // - set size for /dev/shm from shmsize.
  541. defaultMounts := s.Mounts[:0]
  542. _, mountDev := userMounts["/dev"]
  543. for _, m := range s.Mounts {
  544. if _, ok := userMounts[m.Destination]; ok {
  545. // filter out mount overridden by a user supplied mount
  546. continue
  547. }
  548. if mountDev && strings.HasPrefix(m.Destination, "/dev/") {
  549. // filter out everything under /dev if /dev is user-mounted
  550. continue
  551. }
  552. if m.Destination == "/dev/shm" {
  553. if c.HostConfig.IpcMode.IsNone() {
  554. // filter out /dev/shm for "none" IpcMode
  555. continue
  556. }
  557. // set size for /dev/shm mount from spec
  558. sizeOpt := "size=" + strconv.FormatInt(c.HostConfig.ShmSize, 10)
  559. m.Options = append(m.Options, sizeOpt)
  560. }
  561. defaultMounts = append(defaultMounts, m)
  562. }
  563. s.Mounts = defaultMounts
  564. for _, m := range mounts {
  565. if m.Source == "tmpfs" {
  566. data := m.Data
  567. parser := volumemounts.NewParser()
  568. options := []string{"noexec", "nosuid", "nodev", string(parser.DefaultPropagationMode())}
  569. if data != "" {
  570. options = append(options, strings.Split(data, ",")...)
  571. }
  572. merged, err := mount.MergeTmpfsOptions(options)
  573. if err != nil {
  574. return err
  575. }
  576. s.Mounts = append(s.Mounts, specs.Mount{Destination: m.Destination, Source: m.Source, Type: "tmpfs", Options: merged})
  577. continue
  578. }
  579. mt := specs.Mount{Destination: m.Destination, Source: m.Source, Type: "bind"}
  580. // Determine property of RootPropagation based on volume
  581. // properties. If a volume is shared, then keep root propagation
  582. // shared. This should work for slave and private volumes too.
  583. //
  584. // For slave volumes, it can be either [r]shared/[r]slave.
  585. //
  586. // For private volumes any root propagation value should work.
  587. pFlag := mountPropagationMap[m.Propagation]
  588. switch pFlag {
  589. case mount.SHARED, mount.RSHARED:
  590. if err := ensureShared(m.Source); err != nil {
  591. return err
  592. }
  593. rootpg := mountPropagationMap[s.Linux.RootfsPropagation]
  594. if rootpg != mount.SHARED && rootpg != mount.RSHARED {
  595. if s.Linux == nil {
  596. s.Linux = &specs.Linux{}
  597. }
  598. s.Linux.RootfsPropagation = mountPropagationReverseMap[mount.SHARED]
  599. }
  600. case mount.SLAVE, mount.RSLAVE:
  601. var fallback bool
  602. if err := ensureSharedOrSlave(m.Source); err != nil {
  603. // For backwards compatibility purposes, treat mounts from the daemon root
  604. // as special since we automatically add rslave propagation to these mounts
  605. // when the user did not set anything, so we should fallback to the old
  606. // behavior which is to use private propagation which is normally the
  607. // default.
  608. if !strings.HasPrefix(m.Source, daemon.root) && !strings.HasPrefix(daemon.root, m.Source) {
  609. return err
  610. }
  611. cm, ok := c.MountPoints[m.Destination]
  612. if !ok {
  613. return err
  614. }
  615. if cm.Spec.BindOptions != nil && cm.Spec.BindOptions.Propagation != "" {
  616. // This means the user explicitly set a propagation, do not fallback in that case.
  617. return err
  618. }
  619. fallback = true
  620. log.G(ctx).WithField("container", c.ID).WithField("source", m.Source).Warn("Falling back to default propagation for bind source in daemon root")
  621. }
  622. if !fallback {
  623. rootpg := mountPropagationMap[s.Linux.RootfsPropagation]
  624. if rootpg != mount.SHARED && rootpg != mount.RSHARED && rootpg != mount.SLAVE && rootpg != mount.RSLAVE {
  625. if s.Linux == nil {
  626. s.Linux = &specs.Linux{}
  627. }
  628. s.Linux.RootfsPropagation = mountPropagationReverseMap[mount.RSLAVE]
  629. }
  630. }
  631. }
  632. bindMode := "rbind"
  633. if m.NonRecursive {
  634. bindMode = "bind"
  635. }
  636. opts := []string{bindMode}
  637. if !m.Writable {
  638. rro := true
  639. if m.ReadOnlyNonRecursive {
  640. rro = false
  641. if m.ReadOnlyForceRecursive {
  642. return errors.New("mount options conflict: ReadOnlyNonRecursive && ReadOnlyForceRecursive")
  643. }
  644. }
  645. if rroErr := supportsRecursivelyReadOnly(daemonCfg, c.HostConfig.Runtime); rroErr != nil {
  646. rro = false
  647. if m.ReadOnlyForceRecursive {
  648. return rroErr
  649. }
  650. }
  651. if rro {
  652. opts = append(opts, "rro")
  653. } else {
  654. opts = append(opts, "ro")
  655. }
  656. }
  657. if pFlag != 0 {
  658. opts = append(opts, mountPropagationReverseMap[pFlag])
  659. }
  660. // If we are using user namespaces, then we must make sure that we
  661. // don't drop any of the CL_UNPRIVILEGED "locked" flags of the source
  662. // "mount" when we bind-mount. The reason for this is that at the point
  663. // when runc sets up the root filesystem, it is already inside a user
  664. // namespace, and thus cannot change any flags that are locked.
  665. if daemonCfg.RemappedRoot != "" || userns.RunningInUserNS() {
  666. unprivOpts, err := getUnprivilegedMountFlags(m.Source)
  667. if err != nil {
  668. return err
  669. }
  670. opts = append(opts, unprivOpts...)
  671. }
  672. mt.Options = opts
  673. s.Mounts = append(s.Mounts, mt)
  674. }
  675. if s.Root.Readonly {
  676. for i, m := range s.Mounts {
  677. switch m.Destination {
  678. case "/proc", "/dev/pts", "/dev/shm", "/dev/mqueue", "/dev":
  679. continue
  680. }
  681. if _, ok := userMounts[m.Destination]; !ok {
  682. if !inSlice(m.Options, "ro") {
  683. s.Mounts[i].Options = append(s.Mounts[i].Options, "ro")
  684. }
  685. }
  686. }
  687. }
  688. if c.HostConfig.Privileged {
  689. // clear readonly for /sys
  690. for i := range s.Mounts {
  691. if s.Mounts[i].Destination == "/sys" {
  692. clearReadOnly(&s.Mounts[i])
  693. }
  694. }
  695. if s.Linux != nil {
  696. s.Linux.ReadonlyPaths = nil
  697. s.Linux.MaskedPaths = nil
  698. }
  699. }
  700. // TODO: until a kernel/mount solution exists for handling remount in a user namespace,
  701. // we must clear the readonly flag for the cgroups mount (@mrunalp concurs)
  702. if uidMap := daemon.idMapping.UIDMaps; uidMap != nil || c.HostConfig.Privileged {
  703. for i, m := range s.Mounts {
  704. if m.Type == "cgroup" {
  705. clearReadOnly(&s.Mounts[i])
  706. }
  707. }
  708. }
  709. return nil
  710. }
  711. }
  712. // sysctlExists checks if a sysctl exists; runc will error if we add any that do not actually
  713. // exist, so do not add the default ones if running on an old kernel.
  714. func sysctlExists(s string) bool {
  715. f := filepath.Join("/proc", "sys", strings.ReplaceAll(s, ".", "/"))
  716. _, err := os.Stat(f)
  717. return err == nil
  718. }
  719. // withCommonOptions sets common docker options
  720. func withCommonOptions(daemon *Daemon, daemonCfg *dconfig.Config, c *container.Container) coci.SpecOpts {
  721. return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
  722. if c.BaseFS == "" {
  723. return errors.New("populateCommonSpec: BaseFS of container " + c.ID + " is unexpectedly empty")
  724. }
  725. linkedEnv, err := daemon.setupLinkedContainers(c)
  726. if err != nil {
  727. return err
  728. }
  729. s.Root = &specs.Root{
  730. Path: c.BaseFS,
  731. Readonly: c.HostConfig.ReadonlyRootfs,
  732. }
  733. if err := c.SetupWorkingDirectory(daemon.idMapping.RootPair()); err != nil {
  734. return err
  735. }
  736. cwd := c.Config.WorkingDir
  737. if len(cwd) == 0 {
  738. cwd = "/"
  739. }
  740. if s.Process == nil {
  741. s.Process = &specs.Process{}
  742. }
  743. s.Process.Args = append([]string{c.Path}, c.Args...)
  744. // only add the custom init if it is specified and the container is running in its
  745. // own private pid namespace. It does not make sense to add if it is running in the
  746. // host namespace or another container's pid namespace where we already have an init
  747. if c.HostConfig.PidMode.IsPrivate() {
  748. if (c.HostConfig.Init != nil && *c.HostConfig.Init) ||
  749. (c.HostConfig.Init == nil && daemonCfg.Init) {
  750. s.Process.Args = append([]string{inContainerInitPath, "--", c.Path}, c.Args...)
  751. path, err := daemonCfg.LookupInitPath() // this will fall back to DefaultInitBinary and return an absolute path
  752. if err != nil {
  753. return err
  754. }
  755. s.Mounts = append(s.Mounts, specs.Mount{
  756. Destination: inContainerInitPath,
  757. Type: "bind",
  758. Source: path,
  759. Options: []string{"bind", "ro"},
  760. })
  761. }
  762. }
  763. s.Process.Cwd = cwd
  764. s.Process.Env = c.CreateDaemonEnvironment(c.Config.Tty, linkedEnv)
  765. s.Process.Terminal = c.Config.Tty
  766. s.Hostname = c.Config.Hostname
  767. setLinuxDomainname(c, s)
  768. // Add default sysctls that are generally safe and useful; currently we
  769. // grant the capabilities to allow these anyway. You can override if
  770. // you want to restore the original behaviour.
  771. // We do not set network sysctls if network namespace is host, or if we are
  772. // joining an existing namespace, only if we create a new net namespace.
  773. if c.HostConfig.NetworkMode.IsPrivate() {
  774. // We cannot set up ping socket support in a user namespace
  775. userNS := daemonCfg.RemappedRoot != "" && c.HostConfig.UsernsMode.IsPrivate()
  776. if !userNS && !userns.RunningInUserNS() && sysctlExists("net.ipv4.ping_group_range") {
  777. // allow unprivileged ICMP echo sockets without CAP_NET_RAW
  778. s.Linux.Sysctl["net.ipv4.ping_group_range"] = "0 2147483647"
  779. }
  780. // allow opening any port less than 1024 without CAP_NET_BIND_SERVICE
  781. if sysctlExists("net.ipv4.ip_unprivileged_port_start") {
  782. s.Linux.Sysctl["net.ipv4.ip_unprivileged_port_start"] = "0"
  783. }
  784. }
  785. return nil
  786. }
  787. }
  788. // withCgroups sets the container's cgroups
  789. func withCgroups(daemon *Daemon, daemonCfg *dconfig.Config, c *container.Container) coci.SpecOpts {
  790. return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
  791. var cgroupsPath string
  792. scopePrefix := "docker"
  793. parent := "/docker"
  794. useSystemd := UsingSystemd(daemonCfg)
  795. if useSystemd {
  796. parent = "system.slice"
  797. if daemonCfg.Rootless {
  798. parent = "user.slice"
  799. }
  800. }
  801. if c.HostConfig.CgroupParent != "" {
  802. parent = c.HostConfig.CgroupParent
  803. } else if daemonCfg.CgroupParent != "" {
  804. parent = daemonCfg.CgroupParent
  805. }
  806. if useSystemd {
  807. cgroupsPath = parent + ":" + scopePrefix + ":" + c.ID
  808. log.G(ctx).Debugf("createSpec: cgroupsPath: %s", cgroupsPath)
  809. } else {
  810. cgroupsPath = filepath.Join(parent, c.ID)
  811. }
  812. if s.Linux == nil {
  813. s.Linux = &specs.Linux{}
  814. }
  815. s.Linux.CgroupsPath = cgroupsPath
  816. // the rest is only needed for CPU RT controller
  817. if daemonCfg.CPURealtimePeriod == 0 && daemonCfg.CPURealtimeRuntime == 0 {
  818. return nil
  819. }
  820. p := cgroupsPath
  821. if useSystemd {
  822. initPath, err := cgroups.GetInitCgroup("cpu")
  823. if err != nil {
  824. return errors.Wrap(err, "unable to init CPU RT controller")
  825. }
  826. _, err = cgroups.GetOwnCgroup("cpu")
  827. if err != nil {
  828. return errors.Wrap(err, "unable to init CPU RT controller")
  829. }
  830. p = filepath.Join(initPath, s.Linux.CgroupsPath)
  831. }
  832. // Clean path to guard against things like ../../../BAD
  833. parentPath := filepath.Dir(p)
  834. if !filepath.IsAbs(parentPath) {
  835. parentPath = filepath.Clean("/" + parentPath)
  836. }
  837. mnt, root, err := cgroups.FindCgroupMountpointAndRoot("", "cpu")
  838. if err != nil {
  839. return errors.Wrap(err, "unable to init CPU RT controller")
  840. }
  841. // When docker is run inside docker, the root is based of the host cgroup.
  842. // Should this be handled in runc/libcontainer/cgroups ?
  843. if strings.HasPrefix(root, "/docker/") {
  844. root = "/"
  845. }
  846. mnt = filepath.Join(mnt, root)
  847. if err := daemon.initCPURtController(daemonCfg, mnt, parentPath); err != nil {
  848. return errors.Wrap(err, "unable to init CPU RT controller")
  849. }
  850. return nil
  851. }
  852. }
  853. // WithDevices sets the container's devices
  854. func WithDevices(daemon *Daemon, c *container.Container) coci.SpecOpts {
  855. return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
  856. // Build lists of devices allowed and created within the container.
  857. var devs []specs.LinuxDevice
  858. devPermissions := s.Linux.Resources.Devices
  859. if c.HostConfig.Privileged {
  860. hostDevices, err := coci.HostDevices()
  861. if err != nil {
  862. return err
  863. }
  864. devs = append(devs, hostDevices...)
  865. // adding device mappings in privileged containers
  866. for _, deviceMapping := range c.HostConfig.Devices {
  867. // issue a warning that custom cgroup permissions are ignored in privileged mode
  868. if deviceMapping.CgroupPermissions != "rwm" {
  869. log.G(ctx).WithField("container", c.ID).Warnf("custom %s permissions for device %s are ignored in privileged mode", deviceMapping.CgroupPermissions, deviceMapping.PathOnHost)
  870. }
  871. // issue a warning that the device path already exists via /dev mounting in privileged mode
  872. if deviceMapping.PathOnHost == deviceMapping.PathInContainer {
  873. log.G(ctx).WithField("container", c.ID).Warnf("path in container %s already exists in privileged mode", deviceMapping.PathInContainer)
  874. continue
  875. }
  876. d, _, err := oci.DevicesFromPath(deviceMapping.PathOnHost, deviceMapping.PathInContainer, "rwm")
  877. if err != nil {
  878. return err
  879. }
  880. devs = append(devs, d...)
  881. }
  882. devPermissions = []specs.LinuxDeviceCgroup{
  883. {
  884. Allow: true,
  885. Access: "rwm",
  886. },
  887. }
  888. } else {
  889. for _, deviceMapping := range c.HostConfig.Devices {
  890. d, dPermissions, err := oci.DevicesFromPath(deviceMapping.PathOnHost, deviceMapping.PathInContainer, deviceMapping.CgroupPermissions)
  891. if err != nil {
  892. return err
  893. }
  894. devs = append(devs, d...)
  895. devPermissions = append(devPermissions, dPermissions...)
  896. }
  897. var err error
  898. devPermissions, err = oci.AppendDevicePermissionsFromCgroupRules(devPermissions, c.HostConfig.DeviceCgroupRules)
  899. if err != nil {
  900. return err
  901. }
  902. }
  903. if s.Linux == nil {
  904. s.Linux = &specs.Linux{}
  905. }
  906. if s.Linux.Resources == nil {
  907. s.Linux.Resources = &specs.LinuxResources{}
  908. }
  909. s.Linux.Devices = append(s.Linux.Devices, devs...)
  910. s.Linux.Resources.Devices = append(s.Linux.Resources.Devices, devPermissions...)
  911. for _, req := range c.HostConfig.DeviceRequests {
  912. if err := daemon.handleDevice(req, s); err != nil {
  913. return err
  914. }
  915. }
  916. return nil
  917. }
  918. }
  919. // WithResources applies the container resources
  920. func WithResources(c *container.Container) coci.SpecOpts {
  921. return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
  922. r := c.HostConfig.Resources
  923. weightDevices, err := getBlkioWeightDevices(r)
  924. if err != nil {
  925. return err
  926. }
  927. readBpsDevice, err := getBlkioThrottleDevices(r.BlkioDeviceReadBps)
  928. if err != nil {
  929. return err
  930. }
  931. writeBpsDevice, err := getBlkioThrottleDevices(r.BlkioDeviceWriteBps)
  932. if err != nil {
  933. return err
  934. }
  935. readIOpsDevice, err := getBlkioThrottleDevices(r.BlkioDeviceReadIOps)
  936. if err != nil {
  937. return err
  938. }
  939. writeIOpsDevice, err := getBlkioThrottleDevices(r.BlkioDeviceWriteIOps)
  940. if err != nil {
  941. return err
  942. }
  943. memoryRes := getMemoryResources(r)
  944. cpuRes, err := getCPUResources(r)
  945. if err != nil {
  946. return err
  947. }
  948. if s.Linux == nil {
  949. s.Linux = &specs.Linux{}
  950. }
  951. if s.Linux.Resources == nil {
  952. s.Linux.Resources = &specs.LinuxResources{}
  953. }
  954. s.Linux.Resources.Memory = memoryRes
  955. s.Linux.Resources.CPU = cpuRes
  956. s.Linux.Resources.BlockIO = &specs.LinuxBlockIO{
  957. WeightDevice: weightDevices,
  958. ThrottleReadBpsDevice: readBpsDevice,
  959. ThrottleWriteBpsDevice: writeBpsDevice,
  960. ThrottleReadIOPSDevice: readIOpsDevice,
  961. ThrottleWriteIOPSDevice: writeIOpsDevice,
  962. }
  963. if r.BlkioWeight != 0 {
  964. w := r.BlkioWeight
  965. s.Linux.Resources.BlockIO.Weight = &w
  966. }
  967. s.Linux.Resources.Pids = getPidsLimit(r)
  968. return nil
  969. }
  970. }
  971. // WithSysctls sets the container's sysctls
  972. func WithSysctls(c *container.Container) coci.SpecOpts {
  973. return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
  974. if len(c.HostConfig.Sysctls) == 0 {
  975. return nil
  976. }
  977. if s.Linux == nil {
  978. s.Linux = &specs.Linux{}
  979. }
  980. if s.Linux.Sysctl == nil {
  981. s.Linux.Sysctl = make(map[string]string)
  982. }
  983. // We merge the sysctls injected above with the HostConfig (latter takes
  984. // precedence for backwards-compatibility reasons).
  985. for k, v := range c.HostConfig.Sysctls {
  986. s.Linux.Sysctl[k] = v
  987. }
  988. return nil
  989. }
  990. }
  991. // WithUser sets the container's user
  992. func WithUser(c *container.Container) coci.SpecOpts {
  993. return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
  994. if s.Process == nil {
  995. s.Process = &specs.Process{}
  996. }
  997. var err error
  998. s.Process.User, err = getUser(c, c.Config.User)
  999. return err
  1000. }
  1001. }
  1002. func (daemon *Daemon) createSpec(ctx context.Context, daemonCfg *configStore, c *container.Container) (retSpec *specs.Spec, err error) {
  1003. var (
  1004. opts []coci.SpecOpts
  1005. s = oci.DefaultSpec()
  1006. )
  1007. opts = append(opts,
  1008. withCommonOptions(daemon, &daemonCfg.Config, c),
  1009. withCgroups(daemon, &daemonCfg.Config, c),
  1010. WithResources(c),
  1011. WithSysctls(c),
  1012. WithDevices(daemon, c),
  1013. withRlimits(daemon, &daemonCfg.Config, c),
  1014. WithNamespaces(daemon, c),
  1015. WithCapabilities(c),
  1016. WithSeccomp(daemon, c),
  1017. withMounts(daemon, daemonCfg, c),
  1018. withLibnetwork(daemon, &daemonCfg.Config, c),
  1019. WithApparmor(c),
  1020. WithSelinux(c),
  1021. WithOOMScore(&c.HostConfig.OomScoreAdj),
  1022. coci.WithAnnotations(c.HostConfig.Annotations),
  1023. WithUser(c),
  1024. )
  1025. if c.NoNewPrivileges {
  1026. opts = append(opts, coci.WithNoNewPrivileges)
  1027. }
  1028. if c.Config.Tty {
  1029. opts = append(opts, WithConsoleSize(c))
  1030. }
  1031. // Set the masked and readonly paths with regard to the host config options if they are set.
  1032. if c.HostConfig.MaskedPaths != nil {
  1033. opts = append(opts, coci.WithMaskedPaths(c.HostConfig.MaskedPaths))
  1034. }
  1035. if c.HostConfig.ReadonlyPaths != nil {
  1036. opts = append(opts, coci.WithReadonlyPaths(c.HostConfig.ReadonlyPaths))
  1037. }
  1038. if daemonCfg.Rootless {
  1039. opts = append(opts, withRootless(daemon, &daemonCfg.Config))
  1040. } else if userns.RunningInUserNS() {
  1041. opts = append(opts, withRootfulInRootless(daemon, &daemonCfg.Config))
  1042. }
  1043. var snapshotter, snapshotKey string
  1044. if daemon.UsesSnapshotter() {
  1045. snapshotter = daemon.imageService.StorageDriver()
  1046. snapshotKey = c.ID
  1047. }
  1048. return &s, coci.ApplyOpts(ctx, daemon.containerdClient, &containers.Container{
  1049. ID: c.ID,
  1050. Snapshotter: snapshotter,
  1051. SnapshotKey: snapshotKey,
  1052. }, &s, opts...)
  1053. }
  1054. func clearReadOnly(m *specs.Mount) {
  1055. var opt []string
  1056. for _, o := range m.Options {
  1057. if o != "ro" {
  1058. opt = append(opt, o)
  1059. }
  1060. }
  1061. m.Options = opt
  1062. }
  1063. // mergeUlimits merge the Ulimits from HostConfig with daemon defaults, and update HostConfig
  1064. func (daemon *Daemon) mergeUlimits(c *containertypes.HostConfig, daemonCfg *dconfig.Config) {
  1065. ulimits := c.Ulimits
  1066. // Merge ulimits with daemon defaults
  1067. ulIdx := make(map[string]struct{})
  1068. for _, ul := range ulimits {
  1069. ulIdx[ul.Name] = struct{}{}
  1070. }
  1071. for name, ul := range daemonCfg.Ulimits {
  1072. if _, exists := ulIdx[name]; !exists {
  1073. ulimits = append(ulimits, ul)
  1074. }
  1075. }
  1076. c.Ulimits = ulimits
  1077. }