|
| 1 | +From d461620d47450c72d9f0da215606949272df3398 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Rohit Rawat < [email protected]> |
| 3 | +Date: Sun, 10 Nov 2024 18:36:17 +0000 |
| 4 | +Subject: [PATCH] Backport CVE-2024-9676 fix |
| 5 | + |
| 6 | +Patch from https://github.com/containers/storage/pull/2146 by Matt Heon < [email protected]> |
| 7 | +--- |
| 8 | + .../github.com/containers/storage/.cirrus.yml | 2 +- |
| 9 | + .../github.com/containers/storage/userns.go | 92 +++++++++++++------ |
| 10 | + .../containers/storage/userns_unsupported.go | 14 +++ |
| 11 | + 3 files changed, 80 insertions(+), 28 deletions(-) |
| 12 | + create mode 100644 vendor/github.com/containers/storage/userns_unsupported.go |
| 13 | + |
| 14 | +diff --git a/vendor/github.com/containers/storage/.cirrus.yml b/vendor/github.com/containers/storage/.cirrus.yml |
| 15 | +index c41dd5d..9e61509 100644 |
| 16 | +--- a/vendor/github.com/containers/storage/.cirrus.yml |
| 17 | ++++ b/vendor/github.com/containers/storage/.cirrus.yml |
| 18 | +@@ -119,7 +119,7 @@ lint_task: |
| 19 | + env: |
| 20 | + CIRRUS_WORKING_DIR: "/go/src/github.com/containers/storage" |
| 21 | + container: |
| 22 | +- image: golang |
| 23 | ++ image: golang:1.19 |
| 24 | + modules_cache: |
| 25 | + fingerprint_script: cat go.sum |
| 26 | + folder: $GOPATH/pkg/mod |
| 27 | +diff --git a/vendor/github.com/containers/storage/userns.go b/vendor/github.com/containers/storage/userns.go |
| 28 | +index 32ae830..2c855da 100644 |
| 29 | +--- a/vendor/github.com/containers/storage/userns.go |
| 30 | ++++ b/vendor/github.com/containers/storage/userns.go |
| 31 | +@@ -1,18 +1,21 @@ |
| 32 | ++//go:build linux |
| 33 | ++ |
| 34 | + package storage |
| 35 | + |
| 36 | + import ( |
| 37 | + "fmt" |
| 38 | + "os" |
| 39 | + "os/user" |
| 40 | +- "path/filepath" |
| 41 | + "strconv" |
| 42 | + |
| 43 | + drivers "github.com/containers/storage/drivers" |
| 44 | + "github.com/containers/storage/pkg/idtools" |
| 45 | + "github.com/containers/storage/pkg/unshare" |
| 46 | + "github.com/containers/storage/types" |
| 47 | ++ securejoin "github.com/cyphar/filepath-securejoin" |
| 48 | + libcontainerUser "github.com/opencontainers/runc/libcontainer/user" |
| 49 | + "github.com/sirupsen/logrus" |
| 50 | ++ "golang.org/x/sys/unix" |
| 51 | + ) |
| 52 | + |
| 53 | + // getAdditionalSubIDs looks up the additional IDs configured for |
| 54 | +@@ -85,40 +88,59 @@ const nobodyUser = 65534 |
| 55 | + // parseMountedFiles returns the maximum UID and GID found in the /etc/passwd and |
| 56 | + // /etc/group files. |
| 57 | + func parseMountedFiles(containerMount, passwdFile, groupFile string) uint32 { |
| 58 | ++ var ( |
| 59 | ++ passwd *os.File |
| 60 | ++ group *os.File |
| 61 | ++ size int |
| 62 | ++ err error |
| 63 | ++ ) |
| 64 | + if passwdFile == "" { |
| 65 | +- passwdFile = filepath.Join(containerMount, "etc/passwd") |
| 66 | +- } |
| 67 | +- if groupFile == "" { |
| 68 | +- groupFile = filepath.Join(groupFile, "etc/group") |
| 69 | ++ passwd, err = secureOpen(containerMount, "/etc/passwd") |
| 70 | ++ } else { |
| 71 | ++ // User-specified override from a volume. Will not be in |
| 72 | ++ // container root. |
| 73 | ++ passwd, err = os.Open(passwdFile) |
| 74 | + } |
| 75 | +- |
| 76 | +- size := 0 |
| 77 | +- |
| 78 | +- users, err := libcontainerUser.ParsePasswdFile(passwdFile) |
| 79 | + if err == nil { |
| 80 | +- for _, u := range users { |
| 81 | +- // Skip the "nobody" user otherwise we end up with 65536 |
| 82 | +- // ids with most images |
| 83 | +- if u.Name == "nobody" { |
| 84 | +- continue |
| 85 | +- } |
| 86 | +- if u.Uid > size && u.Uid != nobodyUser { |
| 87 | +- size = u.Uid |
| 88 | +- } |
| 89 | +- if u.Gid > size && u.Gid != nobodyUser { |
| 90 | +- size = u.Gid |
| 91 | ++ defer passwd.Close() |
| 92 | ++ |
| 93 | ++ users, err := libcontainerUser.ParsePasswd(passwd) |
| 94 | ++ if err == nil { |
| 95 | ++ for _, u := range users { |
| 96 | ++ // Skip the "nobody" user otherwise we end up with 65536 |
| 97 | ++ // ids with most images |
| 98 | ++ if u.Name == "nobody" || u.Name == "nogroup" { |
| 99 | ++ continue |
| 100 | ++ } |
| 101 | ++ if u.Uid > size && u.Uid != nobodyUser { |
| 102 | ++ size = u.Uid + 1 |
| 103 | ++ } |
| 104 | ++ if u.Gid > size && u.Gid != nobodyUser { |
| 105 | ++ size = u.Gid + 1 |
| 106 | ++ } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | +- groups, err := libcontainerUser.ParseGroupFile(groupFile) |
| 112 | ++ if groupFile == "" { |
| 113 | ++ group, err = secureOpen(containerMount, "/etc/group") |
| 114 | ++ } else { |
| 115 | ++ // User-specified override from a volume. Will not be in |
| 116 | ++ // container root. |
| 117 | ++ group, err = os.Open(groupFile) |
| 118 | ++ } |
| 119 | + if err == nil { |
| 120 | +- for _, g := range groups { |
| 121 | +- if g.Name == "nobody" { |
| 122 | +- continue |
| 123 | +- } |
| 124 | +- if g.Gid > size && g.Gid != nobodyUser { |
| 125 | +- size = g.Gid |
| 126 | ++ defer group.Close() |
| 127 | ++ |
| 128 | ++ groups, err := libcontainerUser.ParseGroup(group) |
| 129 | ++ if err == nil { |
| 130 | ++ for _, g := range groups { |
| 131 | ++ if g.Name == "nobody" || g.Name == "nogroup" { |
| 132 | ++ continue |
| 133 | ++ } |
| 134 | ++ if g.Gid > size && g.Gid != nobodyUser { |
| 135 | ++ size = g.Gid + 1 |
| 136 | ++ } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +@@ -309,3 +331,19 @@ func getAutoUserNSIDMappings( |
| 141 | + gidMap := append(availableGIDs.zip(requestedContainerGIDs), additionalGIDMappings...) |
| 142 | + return uidMap, gidMap, nil |
| 143 | + } |
| 144 | ++ |
| 145 | ++// Securely open (read-only) a file in a container mount. |
| 146 | ++func secureOpen(containerMount, file string) (*os.File, error) { |
| 147 | ++ filePath, err := securejoin.SecureJoin(containerMount, file) |
| 148 | ++ if err != nil { |
| 149 | ++ return nil, err |
| 150 | ++ } |
| 151 | ++ |
| 152 | ++ flags := unix.O_PATH | unix.O_CLOEXEC | unix.O_RDONLY |
| 153 | ++ fileHandle, err := os.OpenFile(filePath, flags, 0) |
| 154 | ++ if err != nil { |
| 155 | ++ return nil, err |
| 156 | ++ } |
| 157 | ++ |
| 158 | ++ return fileHandle, nil |
| 159 | ++} |
| 160 | +diff --git a/vendor/github.com/containers/storage/userns_unsupported.go b/vendor/github.com/containers/storage/userns_unsupported.go |
| 161 | +new file mode 100644 |
| 162 | +index 0000000..e37c18f |
| 163 | +--- /dev/null |
| 164 | ++++ b/vendor/github.com/containers/storage/userns_unsupported.go |
| 165 | +@@ -0,0 +1,14 @@ |
| 166 | ++//go:build !linux |
| 167 | ++ |
| 168 | ++package storage |
| 169 | ++ |
| 170 | ++import ( |
| 171 | ++ "errors" |
| 172 | ++ |
| 173 | ++ "github.com/containers/storage/pkg/idtools" |
| 174 | ++ "github.com/containers/storage/types" |
| 175 | ++) |
| 176 | ++ |
| 177 | ++func (s *store) getAutoUserNS(_ *types.AutoUserNsOptions, _ *Image, _ rwLayerStore, _ []roLayerStore) ([]idtools.IDMap, []idtools.IDMap, error) { |
| 178 | ++ return nil, nil, errors.New("user namespaces are not supported on this platform") |
| 179 | ++} |
| 180 | +-- |
| 181 | +2.39.4 |
| 182 | + |
0 commit comments