From 0d1e299079392043fae24c2be524a0eefe7d8085 Mon Sep 17 00:00:00 2001
From: Nicolas Lacasse <nlacasse@google.com>
Date: Wed, 1 Apr 2020 12:05:17 -0700
Subject: Pass configurable FilesystemType to tmpfs.

PiperOrigin-RevId: 304234086
---
 pkg/sentry/fsimpl/tmpfs/tmpfs.go | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

(limited to 'pkg/sentry/fsimpl/tmpfs')

diff --git a/pkg/sentry/fsimpl/tmpfs/tmpfs.go b/pkg/sentry/fsimpl/tmpfs/tmpfs.go
index afd9f8533..8bc8818c0 100644
--- a/pkg/sentry/fsimpl/tmpfs/tmpfs.go
+++ b/pkg/sentry/fsimpl/tmpfs/tmpfs.go
@@ -77,6 +77,11 @@ type FilesystemOpts struct {
 	// RootSymlinkTarget is the target of the root symlink. Only valid if
 	// RootFileType == S_IFLNK.
 	RootSymlinkTarget string
+
+	// FilesystemType allows setting a different FilesystemType for this
+	// tmpfs filesystem. This allows tmpfs to "impersonate" other
+	// filesystems, like ramdiskfs and cgroupfs.
+	FilesystemType vfs.FilesystemType
 }
 
 // GetFilesystem implements vfs.FilesystemType.GetFilesystem.
@@ -91,15 +96,22 @@ func (fstype FilesystemType) GetFilesystem(ctx context.Context, vfsObj *vfs.Virt
 		clock:   clock,
 	}
 
-	fs.vfsfs.Init(vfsObj, &fstype, &fs)
-
-	typ := uint16(linux.S_IFDIR)
+	rootFileType := uint16(linux.S_IFDIR)
+	newFSType := vfs.FilesystemType(&fstype)
 	tmpfsOpts, ok := opts.InternalData.(FilesystemOpts)
-	if ok && tmpfsOpts.RootFileType != 0 {
-		typ = tmpfsOpts.RootFileType
+	if ok {
+		if tmpfsOpts.RootFileType != 0 {
+			rootFileType = tmpfsOpts.RootFileType
+		}
+		if tmpfsOpts.FilesystemType != nil {
+			newFSType = tmpfsOpts.FilesystemType
+		}
 	}
+
+	fs.vfsfs.Init(vfsObj, newFSType, &fs)
+
 	var root *inode
-	switch typ {
+	switch rootFileType {
 	case linux.S_IFREG:
 		root = fs.newRegularFile(creds, 0777)
 	case linux.S_IFLNK:
@@ -107,7 +119,7 @@ func (fstype FilesystemType) GetFilesystem(ctx context.Context, vfsObj *vfs.Virt
 	case linux.S_IFDIR:
 		root = fs.newDirectory(creds, 01777)
 	default:
-		return nil, nil, fmt.Errorf("invalid tmpfs root file type: %#o", typ)
+		return nil, nil, fmt.Errorf("invalid tmpfs root file type: %#o", rootFileType)
 	}
 	return &fs.vfsfs, &fs.newDentry(root).vfsd, nil
 }
-- 
cgit v1.2.3