Browse code

More descriptive error when running a container with a too long hostname (#21445)

This fix tries to fix issues encountered when running a container with a hostname
that is longer than HOST_NAME_MAX(64).

Previously, `could not synchronise with container process` was generated as the
length of the regex check was missing.

This fix covers the length check so that a hostname that is longer than
HOST_NAME_MAX(64) will be given a correct error message.

Several unit tests cases and additional integration test cases are added as well.

This fix closes #21445.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

Yong Tang authored on 2016/03/24 13:41:21
Showing 3 changed files
... ...
@@ -4290,3 +4290,21 @@ func (s *DockerSuite) TestRunVolumeCopyFlag(c *check.C) {
4290 4290
 	out, _, err = dockerCmdWithError("run", "-v", "/foo:/bar:nocopy", "busybox", "true")
4291 4291
 	c.Assert(err, checker.NotNil, check.Commentf(out))
4292 4292
 }
4293
+
4294
+func (s *DockerSuite) TestRunTooLongHostname(c *check.C) {
4295
+	// Test case in #21445
4296
+	hostname1 := "this-is-a-way-too-long-hostname-but-it-should-give-a-nice-error.local"
4297
+	out, _, err := dockerCmdWithError("run", "--hostname", hostname1, "busybox", "echo", "test")
4298
+	c.Assert(err, checker.NotNil, check.Commentf("Expected docker run to fail!"))
4299
+	c.Assert(out, checker.Contains, "invalid hostname format for --hostname:", check.Commentf("Expected to have 'invalid hostname format for --hostname:' in the output, get: %s!", out))
4300
+
4301
+	// HOST_NAME_MAX=64 so 65 bytes will fail
4302
+	hostname2 := "this-is-a-hostname-with-65-bytes-so-it-should-give-an-error.local"
4303
+	out, _, err = dockerCmdWithError("run", "--hostname", hostname2, "busybox", "echo", "test")
4304
+	c.Assert(err, checker.NotNil, check.Commentf("Expected docker run to fail!"))
4305
+	c.Assert(out, checker.Contains, "invalid hostname format for --hostname:", check.Commentf("Expected to have 'invalid hostname format for --hostname:' in the output, get: %s!", out))
4306
+
4307
+	// 64 bytes will be OK
4308
+	hostname3 := "this-is-a-hostname-with-64-bytes-so-will-not-give-an-error.local"
4309
+	dockerCmd(c, "run", "--hostname", hostname3, "busybox", "echo", "test")
4310
+}
... ...
@@ -244,8 +244,9 @@ func Parse(cmd *flag.FlagSet, args []string) (*container.Config, *container.Host
244 244
 	// Validate if the given hostname is RFC 1123 (https://tools.ietf.org/html/rfc1123) compliant.
245 245
 	hostname := *flHostname
246 246
 	if hostname != "" {
247
+		// Linux hostname is limited to HOST_NAME_MAX=64, not not including the terminating null byte.
247 248
 		matched, _ := regexp.MatchString("^(([[:alnum:]]|[[:alnum:]][[:alnum:]\\-]*[[:alnum:]])\\.)*([[:alnum:]]|[[:alnum:]][[:alnum:]\\-]*[[:alnum:]])$", hostname)
248
-		if !matched {
249
+		if len(hostname) > 64 || !matched {
249 250
 			return nil, nil, nil, cmd, fmt.Errorf("invalid hostname format for --hostname: %s", hostname)
250 251
 		}
251 252
 	}
... ...
@@ -390,6 +390,7 @@ func TestParseHostname(t *testing.T) {
390 390
 		"host-name":   "host-name",
391 391
 		"hostname123": "hostname123",
392 392
 		"123hostname": "123hostname",
393
+		"hostname-of-64-bytes-long-should-be-valid-and-without-any-errors": "hostname-of-64-bytes-long-should-be-valid-and-without-any-errors",
393 394
 	}
394 395
 	invalidHostnames := map[string]string{
395 396
 		"^hostname": "invalid hostname format for --hostname: ^hostname",
... ...
@@ -397,6 +398,7 @@ func TestParseHostname(t *testing.T) {
397 397
 		"host&name": "invalid hostname format for --hostname: host&name",
398 398
 		"-hostname": "invalid hostname format for --hostname: -hostname",
399 399
 		"host_name": "invalid hostname format for --hostname: host_name",
400
+		"hostname-of-65-bytes-long-should-be-invalid-and-be-given-an-error": "invalid hostname format for --hostname: hostname-of-65-bytes-long-should-be-invalid-and-be-given-an-error",
400 401
 	}
401 402
 	hostnameWithDomain := "--hostname=hostname.domainname"
402 403
 	hostnameWithDomainTld := "--hostname=hostname.domainname.tld"