Skip to content

Validate hostConfig on daemon. Check for invalid port specifications - #14231

Merged
cpuguy83 merged 1 commit into
moby:masterfrom
ankushagarwal:checkValidPort
Jun 30, 2015
Merged

cpuguy83 merged 1 commit into
moby:masterfrom
ankushagarwal:checkValidPort

Conversation

@ankushagarwal

Copy link
Copy Markdown
Contributor

Currently only checks for proper port specification as port
is specified as a string and any arbitrary string can be passed as port

Fixes #14230

@runcom

runcom commented Jun 28, 2015

Copy link
Copy Markdown
Member

@ankushagarwal

Copy link
Copy Markdown
Contributor Author

Yup, noticed that. That function (https://github.com/docker/docker/blob/master/nat/nat.go#L57) is very badly written. It tries to convert a string to int and panics when it can't. Instead of panic, it should be returning an error. I will try to open another issue to fix that. A lot of refactoring needs to be done to fix that.

In this fix, I am making sure that while parsing the HostConfig, the ports are in proper format and doing an early exit.

@runcom

runcom commented Jun 28, 2015

Copy link
Copy Markdown
Member

The problem is that panic tho. Wouldn't be better to add that kind of validation in https://github.com/ankushagarwal/docker/blob/checkValidPort/daemon/container_linux.go#L666 that is where this check is missing (I suppose, I'm actually on my phone, since there we are checking mac as well)? And then it won't be a mess to refactor Int() funciton not to panic. To me it's not right to validate in DecodeHostConfig and there's same loop here https://github.com/ankushagarwal/docker/blob/checkValidPort/daemon/container_linux.go#L680
This will also needs some tests as well and problem seems to be in the way we validate input range ports

@ankushagarwal

Copy link
Copy Markdown
Contributor Author

Agreed. Will refactor.

@ankushagarwal ankushagarwal changed the title Validate HostConfig on daemon Validate port strings in buildCreateEndpointOptions Jun 28, 2015
@ankushagarwal

Copy link
Copy Markdown
Contributor Author

On second thoughts, I think we should catch invalid Ports (and other invalid objects) as soon as we DecodeHostConfig rather than passing around the HostConfig object with an invalid port in it. If some other function decides to use the ports before it reaches https://github.com/ankushagarwal/docker/blob/checkValidPort/daemon/container_linux.go#L666, then it will result in a panic, which is not a good thing.

@LK4D4 LK4D4 added this to the 1.7.1 milestone Jun 29, 2015
@calavera

Copy link
Copy Markdown
Contributor

@ankushagarwal it would be awesome to have an integration test to make sure we don't break this again. Otherwise, the patch LGTM.

Comment thread nat/nat.go Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be:

_, err := ParsePort(portStr)
return err

Also, error is keyword for type in golang, better not use it for your variables.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed

@ankushagarwal

Copy link
Copy Markdown
Contributor Author

Updated error -> err
@calavera : I am not sure how to write integration tests for this as the client catches this problem (misformed ports) and does not send it to the daemon at all.

@calavera

Copy link
Copy Markdown
Contributor

you can write integration tests that talk with the api directly, like the ones in this file:

https://github.com/docker/docker/blob/master/integration-cli/docker_api_containers_test.go

@calavera

Copy link
Copy Markdown
Contributor

I think tests are failing because w.GetHostConfig() can return nil. You should check that.

@ankushagarwal

Copy link
Copy Markdown
Contributor Author

Added the nil check. Hopefully tests pass now. Will add the integration test now.

@ankushagarwal

Copy link
Copy Markdown
Contributor Author

Added an integration test. Can't figure out why experimental is failing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, can we change this to only supply required fields?
This would be config.Image and config.HostCofig.PortBindings for this test.

@ankushagarwal ankushagarwal changed the title Validate port strings in buildCreateEndpointOptions Validate hostConfig on daemon. Check for invalid port specifications Jun 30, 2015
@calavera

Copy link
Copy Markdown
Contributor

thanks @ankushagarwal. LGTM

@cpuguy83

Copy link
Copy Markdown
Member

LGTM

cpuguy83 added a commit that referenced this pull request Jun 30, 2015
Validate hostConfig on daemon. Check for invalid port specifications
@cpuguy83
cpuguy83 merged commit 2de810d into moby:master Jun 30, 2015
@calavera calavera self-assigned this Jun 30, 2015
@calavera

Copy link
Copy Markdown
Contributor

Cherry picked into the release branch.

@ankushagarwal
ankushagarwal deleted the checkValidPort branch June 30, 2015 20:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants