diff --git a/libcontainer/configs/config.go b/libcontainer/configs/config.go index 8ce881b1e9f..d43ea7860a2 100644 --- a/libcontainer/configs/config.go +++ b/libcontainer/configs/config.go @@ -119,7 +119,7 @@ type Config struct { // Hostname optionally sets the container's hostname if provided Hostname string `json:"hostname"` - // Hostname optionally sets the container's domainname if provided + // Domainname optionally sets the container's domainname if provided Domainname string `json:"domainname"` // Namespaces specifies the container's namespaces that it should setup when cloning the init process diff --git a/libcontainer/configs/validate/validator.go b/libcontainer/configs/validate/validator.go index 56313d22296..483e7a2ff3e 100644 --- a/libcontainer/configs/validate/validator.go +++ b/libcontainer/configs/validate/validator.go @@ -23,8 +23,7 @@ func Validate(config *configs.Config) error { cgroupsCheck, rootfs, network, - hostname, - domainname, + uts, security, namespaces, sysctl, @@ -76,14 +75,10 @@ func network(config *configs.Config) error { return nil } -func hostname(config *configs.Config) error { +func uts(config *configs.Config) error { if config.Hostname != "" && !config.Namespaces.Contains(configs.NEWUTS) { return errors.New("unable to set hostname without a private UTS namespace") } - return nil -} - -func domainname(config *configs.Config) error { if config.Domainname != "" && !config.Namespaces.Contains(configs.NEWUTS) { return errors.New("unable to set domainname without a private UTS namespace") } diff --git a/libcontainer/configs/validate/validator_test.go b/libcontainer/configs/validate/validator_test.go index 6858d2719ab..f59d0f2030c 100644 --- a/libcontainer/configs/validate/validator_test.go +++ b/libcontainer/configs/validate/validator_test.go @@ -82,10 +82,11 @@ func TestValidateHostname(t *testing.T) { } } -func TestValidateDomainname(t *testing.T) { +func TestValidateUTS(t *testing.T) { config := &configs.Config{ Rootfs: "/var", Domainname: "runc", + Hostname: "runc", Namespaces: configs.Namespaces( []configs.Namespace{ {Type: configs.NEWUTS}, @@ -99,7 +100,7 @@ func TestValidateDomainname(t *testing.T) { } } -func TestValidateHostnameWithoutUTSNamespace(t *testing.T) { +func TestValidateUTSWithoutUTSNamespace(t *testing.T) { config := &configs.Config{ Rootfs: "/var", Hostname: "runc", @@ -109,15 +110,13 @@ func TestValidateHostnameWithoutUTSNamespace(t *testing.T) { if err == nil { t.Error("Expected error to occur but it was nil") } -} -func TestValidateDomainnameWithoutUTSNamespace(t *testing.T) { - config := &configs.Config{ + config = &configs.Config{ Rootfs: "/var", Domainname: "runc", } - err := Validate(config) + err = Validate(config) if err == nil { t.Error("Expected error to occur but it was nil") } diff --git a/libcontainer/standard_init_linux.go b/libcontainer/standard_init_linux.go index 6ad25c9a4df..4b84974ec6b 100644 --- a/libcontainer/standard_init_linux.go +++ b/libcontainer/standard_init_linux.go @@ -126,6 +126,11 @@ func (l *linuxStandardInit) Init() error { return &os.SyscallError{Syscall: "sethostname", Err: err} } } + if domainname := l.config.Config.Domainname; domainname != "" { + if err := unix.Setdomainname([]byte(domainname)); err != nil { + return &os.SyscallError{Syscall: "setdomainname", Err: err} + } + } if err := apparmor.ApplyProfile(l.config.AppArmorProfile); err != nil { return fmt.Errorf("unable to apply apparmor profile: %w", err) }