浏览代码

Move security opts to HostConfig

These settings need to be in the HostConfig so that they are not
committed to an image and cannot introduce a security issue.

We can safely move this field from the Config to the HostConfig
without any regressions because these settings are consumed at container
created and used to populate fields on the Container struct.  Because of
this, existing settings will be honored for containers already created
on a daemon with custom security settings and prevent values being
consumed via an Image.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
Michael Crosby 10 年之前
父节点
当前提交
c9379eb3fb
共有 6 个文件被更改,包括 12 次插入10 次删除
  1. 5 6
      daemon/daemon.go
  2. 1 1
      daemon/daemon_unit_test.go
  3. 3 0
      daemon/start.go
  4. 0 2
      runconfig/config.go
  5. 2 0
      runconfig/hostconfig.go
  6. 1 1
      runconfig/parse.go

+ 5 - 6
daemon/daemon.go

@@ -528,10 +528,10 @@ func (daemon *Daemon) getEntrypointAndArgs(configEntrypoint, configCmd []string)
 	return entrypoint, args
 	return entrypoint, args
 }
 }
 
 
-func parseSecurityOpt(container *Container, config *runconfig.Config) error {
+func parseSecurityOpt(container *Container, config *runconfig.HostConfig) error {
 	var (
 	var (
-		label_opts []string
-		err        error
+		labelOpts []string
+		err       error
 	)
 	)
 
 
 	for _, opt := range config.SecurityOpt {
 	for _, opt := range config.SecurityOpt {
@@ -541,7 +541,7 @@ func parseSecurityOpt(container *Container, config *runconfig.Config) error {
 		}
 		}
 		switch con[0] {
 		switch con[0] {
 		case "label":
 		case "label":
-			label_opts = append(label_opts, con[1])
+			labelOpts = append(labelOpts, con[1])
 		case "apparmor":
 		case "apparmor":
 			container.AppArmorProfile = con[1]
 			container.AppArmorProfile = con[1]
 		default:
 		default:
@@ -549,7 +549,7 @@ func parseSecurityOpt(container *Container, config *runconfig.Config) error {
 		}
 		}
 	}
 	}
 
 
-	container.ProcessLabel, container.MountLabel, err = label.InitLabels(label_opts)
+	container.ProcessLabel, container.MountLabel, err = label.InitLabels(labelOpts)
 	return err
 	return err
 }
 }
 
 
@@ -583,7 +583,6 @@ func (daemon *Daemon) newContainer(name string, config *runconfig.Config, img *i
 		execCommands:    newExecStore(),
 		execCommands:    newExecStore(),
 	}
 	}
 	container.root = daemon.containerRoot(container.ID)
 	container.root = daemon.containerRoot(container.ID)
-	err = parseSecurityOpt(container, config)
 	return container, err
 	return container, err
 }
 }
 
 

+ 1 - 1
daemon/daemon_unit_test.go

@@ -8,7 +8,7 @@ import (
 
 
 func TestParseSecurityOpt(t *testing.T) {
 func TestParseSecurityOpt(t *testing.T) {
 	container := &Container{}
 	container := &Container{}
-	config := &runconfig.Config{}
+	config := &runconfig.HostConfig{}
 
 
 	// test apparmor
 	// test apparmor
 	config.SecurityOpt = []string{"apparmor:test_profile"}
 	config.SecurityOpt = []string{"apparmor:test_profile"}

+ 3 - 0
daemon/start.go

@@ -44,6 +44,9 @@ func (daemon *Daemon) ContainerStart(job *engine.Job) engine.Status {
 }
 }
 
 
 func (daemon *Daemon) setHostConfig(container *Container, hostConfig *runconfig.HostConfig) error {
 func (daemon *Daemon) setHostConfig(container *Container, hostConfig *runconfig.HostConfig) error {
+	if err := parseSecurityOpt(container, hostConfig); err != nil {
+		return err
+	}
 	// Validate the HostConfig binds. Make sure that:
 	// Validate the HostConfig binds. Make sure that:
 	// the source exists
 	// the source exists
 	for _, bind := range hostConfig.Binds {
 	for _, bind := range hostConfig.Binds {

+ 0 - 2
runconfig/config.go

@@ -32,7 +32,6 @@ type Config struct {
 	Entrypoint      []string
 	Entrypoint      []string
 	NetworkDisabled bool
 	NetworkDisabled bool
 	OnBuild         []string
 	OnBuild         []string
-	SecurityOpt     []string
 }
 }
 
 
 func ContainerConfigFromJob(job *engine.Job) *Config {
 func ContainerConfigFromJob(job *engine.Job) *Config {
@@ -56,7 +55,6 @@ func ContainerConfigFromJob(job *engine.Job) *Config {
 	}
 	}
 	job.GetenvJson("ExposedPorts", &config.ExposedPorts)
 	job.GetenvJson("ExposedPorts", &config.ExposedPorts)
 	job.GetenvJson("Volumes", &config.Volumes)
 	job.GetenvJson("Volumes", &config.Volumes)
-	config.SecurityOpt = job.GetenvList("SecurityOpt")
 	if PortSpecs := job.GetenvList("PortSpecs"); PortSpecs != nil {
 	if PortSpecs := job.GetenvList("PortSpecs"); PortSpecs != nil {
 		config.PortSpecs = PortSpecs
 		config.PortSpecs = PortSpecs
 	}
 	}

+ 2 - 0
runconfig/hostconfig.go

@@ -56,6 +56,7 @@ type HostConfig struct {
 	CapAdd          []string
 	CapAdd          []string
 	CapDrop         []string
 	CapDrop         []string
 	RestartPolicy   RestartPolicy
 	RestartPolicy   RestartPolicy
+	SecurityOpt     []string
 }
 }
 
 
 // This is used by the create command when you want to set both the
 // This is used by the create command when you want to set both the
@@ -90,6 +91,7 @@ func ContainerHostConfigFromJob(job *engine.Job) *HostConfig {
 	job.GetenvJson("PortBindings", &hostConfig.PortBindings)
 	job.GetenvJson("PortBindings", &hostConfig.PortBindings)
 	job.GetenvJson("Devices", &hostConfig.Devices)
 	job.GetenvJson("Devices", &hostConfig.Devices)
 	job.GetenvJson("RestartPolicy", &hostConfig.RestartPolicy)
 	job.GetenvJson("RestartPolicy", &hostConfig.RestartPolicy)
+	hostConfig.SecurityOpt = job.GetenvList("SecurityOpt")
 	if Binds := job.GetenvList("Binds"); Binds != nil {
 	if Binds := job.GetenvList("Binds"); Binds != nil {
 		hostConfig.Binds = Binds
 		hostConfig.Binds = Binds
 	}
 	}

+ 1 - 1
runconfig/parse.go

@@ -256,7 +256,6 @@ func Parse(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Config,
 		Volumes:         flVolumes.GetMap(),
 		Volumes:         flVolumes.GetMap(),
 		Entrypoint:      entrypoint,
 		Entrypoint:      entrypoint,
 		WorkingDir:      *flWorkingDir,
 		WorkingDir:      *flWorkingDir,
-		SecurityOpt:     flSecurityOpt.GetAll(),
 	}
 	}
 
 
 	hostConfig := &HostConfig{
 	hostConfig := &HostConfig{
@@ -276,6 +275,7 @@ func Parse(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Config,
 		CapAdd:          flCapAdd.GetAll(),
 		CapAdd:          flCapAdd.GetAll(),
 		CapDrop:         flCapDrop.GetAll(),
 		CapDrop:         flCapDrop.GetAll(),
 		RestartPolicy:   restartPolicy,
 		RestartPolicy:   restartPolicy,
+		SecurityOpt:     flSecurityOpt.GetAll(),
 	}
 	}
 
 
 	if sysInfo != nil && flMemory > 0 && !sysInfo.SwapLimit {
 	if sysInfo != nil && flMemory > 0 && !sysInfo.SwapLimit {