Browse code

Merge pull request #42352 from AkihiroSuda/cherrypick-41724

[20.10 backport] Use v2 capabilities in layer archives

Brian Goff authored on 2021/06/02 07:34:42
Showing 2 changed files
... ...
@@ -118,15 +118,6 @@ func TestBuildUserNamespaceValidateCapabilitiesAreV2(t *testing.T) {
118 118
 	_, err = stdcopy.StdCopy(actualStdout, actualStderr, logReader)
119 119
 	assert.NilError(t, err)
120 120
 	if strings.TrimSpace(actualStdout.String()) != "/bin/sleep cap_net_bind_service=eip" {
121
-		// Activate when fix is merged: https://github.com/moby/moby/pull/41724
122
-		//t.Fatalf("run produced invalid output: %q, expected %q", actualStdout.String(), "/bin/sleep cap_net_bind_service=eip")
123
-		// t.Logf("run produced invalid output (expected until #41724 merges): %q, expected %q",
124
-		// 	actualStdout.String(),
125
-		// 	"/bin/sleep cap_net_bind_service=eip")
126
-	} else {
127
-		// Shouldn't happen until fix is merged: https://github.com/moby/moby/pull/41724
128
-		t.Fatalf("run produced valid output (unexpected until #41724 merges): %q, expected %q",
129
-			actualStdout.String(),
130
-			"/bin/sleep cap_net_bind_service=eip")
121
+		t.Fatalf("run produced invalid output: %q, expected %q", actualStdout.String(), "/bin/sleep cap_net_bind_service=eip")
131 122
 	}
132 123
 }
... ...
@@ -402,10 +402,24 @@ func fillGo18FileTypeBits(mode int64, fi os.FileInfo) int64 {
402 402
 // ReadSecurityXattrToTarHeader reads security.capability xattr from filesystem
403 403
 // to a tar header
404 404
 func ReadSecurityXattrToTarHeader(path string, hdr *tar.Header) error {
405
+	const (
406
+		// Values based on linux/include/uapi/linux/capability.h
407
+		xattrCapsSz2    = 20
408
+		versionOffset   = 3
409
+		vfsCapRevision2 = 2
410
+		vfsCapRevision3 = 3
411
+	)
405 412
 	capability, _ := system.Lgetxattr(path, "security.capability")
406 413
 	if capability != nil {
414
+		length := len(capability)
415
+		if capability[versionOffset] == vfsCapRevision3 {
416
+			// Convert VFS_CAP_REVISION_3 to VFS_CAP_REVISION_2 as root UID makes no
417
+			// sense outside the user namespace the archive is built in.
418
+			capability[versionOffset] = vfsCapRevision2
419
+			length = xattrCapsSz2
420
+		}
407 421
 		hdr.Xattrs = make(map[string]string)
408
-		hdr.Xattrs["security.capability"] = string(capability)
422
+		hdr.Xattrs["security.capability"] = string(capability[:length])
409 423
 	}
410 424
 	return nil
411 425
 }