From 706c01073dca10fb0762f048d6fb72da1facd2a4 Mon Sep 17 00:00:00 2001 From: Alessandro Boch Date: Mon, 4 May 2015 23:45:07 -0700 Subject: [PATCH] CreateOptionPortMapping to store a copy of the passed bindings - Given this will be internal data, make a defensive copy to protect from client inadvertently modifications. Signed-off-by: Alessandro Boch --- libnetwork/endpoint.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/libnetwork/endpoint.go b/libnetwork/endpoint.go index d8e98064fc..605616064a 100644 --- a/libnetwork/endpoint.go +++ b/libnetwork/endpoint.go @@ -486,13 +486,18 @@ func JoinOptionUseDefaultSandbox() EndpointOption { // ports option to be passed to network.CreateEndpoint() method. func CreateOptionPortMapping(portBindings []netutils.PortBinding) EndpointOption { return func(ep *endpoint) { - // Store endpoint label - ep.generic[options.PortMap] = portBindings - // Extract exposed ports as this is the only concern of libnetwork endpoint - ep.exposedPorts = make([]netutils.TransportPort, 0, len(portBindings)) + // Extract and store exposed ports as this is the only concern of libnetwork endpoint + // Store a copy of the bindings as generic data to pass to the driver + pbs := make([]netutils.PortBinding, 0, len(portBindings)) + exp := make([]netutils.TransportPort, 0, len(portBindings)) + for _, b := range portBindings { - ep.exposedPorts = append(ep.exposedPorts, netutils.TransportPort{Proto: b.Proto, Port: b.Port}) + pbs = append(pbs, b.GetCopy()) + exp = append(exp, netutils.TransportPort{Proto: b.Proto, Port: b.Port}) } + + ep.generic[options.PortMap] = pbs + ep.exposedPorts = exp } }