Vulnerability: Improper input validation in umoci ### Impact

umoci 0.4.6 and earlier can be tricked into modifying host files by
creating a malicious layer that has a symlink with the name "." (or
"/"). Because umoci deletes inodes if they change types, this results in
the rootfs directory being replaced with an attacker-controlled symlink.
Subsequent image layers will then be applied on top of the target of the
symlink (which could be any directory on the host filesystem the user
running umoci has access to).

While umoci does have defences against symlink-based attacks, they are
all implemented by resolving things relative to the rootfs directory --
if the rootfs itself is a symlink, umoci resolves it first.

This vulnerability affects both "umoci unpack" and "umoci raw unpack".

### Patches
This issue has been patched in umoci 0.4.7, see the references section
for the specific commit which fixed this vulnerability.

### Workarounds
Note that if you use umoci as an unprivileged user (using the --rootless
flag) then umoci will not be able to overwrite any files that your user
doesn't have access to. Other possible mitigations are to run umoci
under an LSM profile such as AppArmor or SELinux to restrict the level
of access it has outside of container image directories.

### References
* [oss-security public disclosure](https://www.openwall.com/lists/oss-security/2021/04/06/2)
* [patch](https://github.com/opencontainers/umoci/commit/d9efc31daf2206f7d3fdb839863cf7a576a2eb57)

### Credits
Thanks to Robin Peraglie from Cure53 for discovering and reporting this
vulnerability.

### For more information

If you have any questions or comments about this advisory
* Open an issue in <https://github.com/opencontainers/umoci>.
* Email us at <security@opencontainers.org>.
Vulnerability ID: GHSA-9m95-8hx6-7p9v
CWE: CWE-20
Severity score: 5.5
Project: https://github.com/opencontainers/umoci
Published: 2022-02-15
File: oci/layer/tar_extract.go
Commit message: layer: don't permit / type to be changed on extraction

If users can change the type of / to a symlink, they can cause umoci to
overwrite host files. This is obviously bad, and is not caught by the
rest of our directory escape detection code because the root itself has
been changed to a different directory.

Fixes: CVE-2021-29136
Reported-by: Robin Peraglie <robin@cure53.de>
Tested-by: Daniel Dao <dqminh89@gmail.com>
Reviewed-by: Tycho Andersen <tycho@tycho.pizza>
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
Commit author: Aleksa Sarai

Vulnerable version (before fix):
 func (te *TarExtractor) UnpackEntry(root string, hdr *tar.Header, r io.Reader) ( 	if filepath.Join("/", hdr.Name) == "/" { 		// If we got an entry for the root, then unsafeDir is the full path. 		unsafeDir, file = hdr.Name, "." 	} 	dir, err := securejoin.SecureJoinVFS(root, unsafeDir, te.fsEval) 	if err != nil {

Fixed version (after fix):
 func (te *TarExtractor) UnpackEntry(root string, hdr *tar.Header, r io.Reader) ( 	if filepath.Join("/", hdr.Name) == "/" { 		// If we got an entry for the root, then unsafeDir is the full path. 		unsafeDir, file = hdr.Name, "." 		// If we're being asked to change the root type, bail because they may 		// change it to a symlink which we could inadvertently follow. 		if hdr.Typeflag != tar.TypeDir { 			return errors.New("malicious tar entry -- refusing to change type of root directory") 		} 	} 	dir, err := securejoin.SecureJoinVFS(root, unsafeDir, te.fsEval) 	if err != nil {

Patch:
patch: @@ -404,6 +404,11 @@ func (te *TarExtractor) UnpackEntry(root string, hdr *tar.Header, r io.Reader) (
 	if filepath.Join("/", hdr.Name) == "/" {
 		// If we got an entry for the root, then unsafeDir is the full path.
 		unsafeDir, file = hdr.Name, "."
+		// If we're being asked to change the root type, bail because they may
+		// change it to a symlink which we could inadvertently follow.
+		if hdr.Typeflag != tar.TypeDir {
+			return errors.New("malicious tar entry -- refusing to change type of root directory")
+		}
 	}
 	dir, err := securejoin.SecureJoinVFS(root, unsafeDir, te.fsEval)
 	if err != nil {
path: oci/layer/tar_extract.go