From 9acfd7b1a30afeec48f1223870435b9f0d6a40c8 Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Thu, 10 Aug 2023 18:32:24 +1000 Subject: [PATCH] timens: minor cleanups Fix up a few things that were flagged in the review of the original timens PR, namely around error handling and validation. Signed-off-by: Aleksa Sarai --- libcontainer/configs/validate/validator.go | 4 ++++ libcontainer/container_linux.go | 2 +- libcontainer/nsenter/nsexec.c | 14 ++++---------- libcontainer/specconv/spec_linux.go | 4 +--- 4 files changed, 10 insertions(+), 14 deletions(-) diff --git a/libcontainer/configs/validate/validator.go b/libcontainer/configs/validate/validator.go index 7e8e6c6f5..81eebba79 100644 --- a/libcontainer/configs/validate/validator.go +++ b/libcontainer/configs/validate/validator.go @@ -120,6 +120,10 @@ func namespaces(config *configs.Config) error { if _, err := os.Stat("/proc/self/timens_offsets"); os.IsNotExist(err) { return errors.New("time namespaces aren't enabled in the kernel") } + } else { + if config.TimeOffsets != nil { + return errors.New("time namespace offsets specified, but time namespace isn't enabled in the config") + } } return nil diff --git a/libcontainer/container_linux.go b/libcontainer/container_linux.go index daec0ec4b..1a2a6fe37 100644 --- a/libcontainer/container_linux.go +++ b/libcontainer/container_linux.go @@ -1192,7 +1192,7 @@ func (c *Container) bootstrapData(cloneFlags uintptr, nsMaps map[configs.Namespa } // write boottime and monotonic time ns offsets. - if c.config.Namespaces.Contains(configs.NEWTIME) && c.config.TimeOffsets != nil { + if c.config.TimeOffsets != nil { var offsetSpec bytes.Buffer for clock, offset := range c.config.TimeOffsets { fmt.Fprintf(&offsetSpec, "%s %d %d\n", clock, offset.Secs, offset.Nanosecs) diff --git a/libcontainer/nsenter/nsexec.c b/libcontainer/nsenter/nsexec.c index 8d4148cc6..07d9e0df1 100644 --- a/libcontainer/nsenter/nsexec.c +++ b/libcontainer/nsenter/nsexec.c @@ -758,15 +758,13 @@ void receive_idmapsources(int sockfd) receive_fd_sources(sockfd, "_LIBCONTAINER_IDMAP_FDS"); } -static void update_timens(char *map, size_t map_len) +static void update_timens_offsets(char *map, size_t map_len) { if (map == NULL || map_len == 0) return; write_log(DEBUG, "update /proc/self/timens_offsets to '%s'", map); - if (write_file(map, map_len, "/proc/self/timens_offsets") < 0) { - if (errno != EPERM) - bail("failed to update /proc/self/timens_offsets"); - } + if (write_file(map, map_len, "/proc/self/timens_offsets") < 0) + bail("failed to update /proc/self/timens_offsets"); } void nsexec(void) @@ -1174,6 +1172,7 @@ void nsexec(void) * was broken, so we'll just do it the long way anyway. */ try_unshare(config.cloneflags & ~CLONE_NEWCGROUP, "remaining namespaces (except cgroupns)"); + update_timens_offsets(config.timensoffset, config.timensoffset_len); /* Ask our parent to send the mount sources fds. */ if (config.mountsources) { @@ -1207,11 +1206,6 @@ void nsexec(void) bail("failed to sync with parent: SYNC_MOUNT_IDMAP_ACK: got %u", s); } - /* - * set boottime and monotonic timens offsets. - */ - update_timens(config.timensoffset, config.timensoffset_len); - /* * TODO: What about non-namespace clone flags that we're dropping here? * diff --git a/libcontainer/specconv/spec_linux.go b/libcontainer/specconv/spec_linux.go index 0afb4a6d8..5ccaf356e 100644 --- a/libcontainer/specconv/spec_linux.go +++ b/libcontainer/specconv/spec_linux.go @@ -422,6 +422,7 @@ func CreateLibcontainerConfig(opts *CreateOpts) (*configs.Config, error) { config.ReadonlyPaths = spec.Linux.ReadonlyPaths config.MountLabel = spec.Linux.MountLabel config.Sysctl = spec.Linux.Sysctl + config.TimeOffsets = spec.Linux.TimeOffsets if spec.Linux.Seccomp != nil { seccomp, err := SetupSeccomp(spec.Linux.Seccomp) if err != nil { @@ -436,9 +437,6 @@ func CreateLibcontainerConfig(opts *CreateOpts) (*configs.Config, error) { MemBwSchema: spec.Linux.IntelRdt.MemBwSchema, } } - - // update timens offsets - config.TimeOffsets = spec.Linux.TimeOffsets } // Set the host UID that should own the container's cgroup.