Repository: google/gvisor
File: pkg/sentry/fsimpl/proc/tasks_sys.go (Go)

Code before:
// newSysDir returns the dentry corresponding to /proc/sys directory.
func (fs *filesystem) newSysDir(ctx context.Context, root *auth.Credentials, k *kernel.Kernel) kernfs.Inode {
	return fs.newStaticDir(ctx, root, map[string]kernfs.Inode{
		"kernel": fs.newStaticDir(ctx, root, map[string]kernfs.Inode{
			"cap_last_cap": fs.newInode(ctx, root, 0444, newStaticFile(fmt.Sprintf("%d\n", linux.CAP_LAST_CAP))),
			"hostname":     fs.newInode(ctx, root, 0444, &hostnameData{}),
			"overflowgid":  fs.newInode(ctx, root, 0444, newStaticFile(fmt.Sprintf("%d\n", auth.OverflowGID))),
			"overflowuid":  fs.newInode(ctx, root, 0444, newStaticFile(fmt.Sprintf("%d\n", auth.OverflowUID))),
			"pid_max":      fs.newInode(ctx, root, 0644, newStaticFile(fmt.Sprintf("%d\n", kernel.TasksLimit))),
			"random": fs.newStaticDir(ctx, root, map[string]kernfs.Inode{
				"boot_id": fs.newInode(ctx, root, 0444, newStaticFile(randUUID())),
			}),
			"sem":    fs.newInode(ctx, root, 0444, newStaticFile(fmt.Sprintf("%d\t%d\t%d\t%d\n", linux.SEMMSL, linux.SEMMNS, linux.SEMOPM, linux.SEMMNI))),
			"shmall": fs.newInode(ctx, root, 0444, ipcData(linux.SHMALL)),
			"shmmax": fs.newInode(ctx, root, 0444, ipcData(linux.SHMMAX)),
			"shmmni": fs.newInode(ctx, root, 0444, ipcData(linux.SHMMNI)),
			"msgmni": fs.newInode(ctx, root, 0444, ipcData(linux.MSGMNI)),
			"msgmax": fs.newInode(ctx, root, 0444, ipcData(linux.MSGMAX)),
			"msgmnb": fs.newInode(ctx, root, 0444, ipcData(linux.MSGMNB)),
			"yama": fs.newStaticDir(ctx, root, map[string]kernfs.Inode{
				"ptrace_scope": fs.newYAMAPtraceScopeFile(ctx, k, root),
			}),
			"keys": fs.newStaticDir(ctx, root, map[string]kernfs.Inode{
				"maxkeys": fs.newMaxKeySizeFile(ctx, k, root),
			}),
			"osrelease": fs.newInode(ctx, root, 0444, newStaticFile(version.LinuxRelease)),
		}),
		"fs": fs.newStaticDir(ctx, root, map[string]kernfs.Inode{
			"nr_open": fs.newInode(ctx, root, 0644, &atomicInt32File{val: &k.MaxFDLimit, min: 8, max: kernel.MaxFdLimit}),
		}),
		"vm": fs.newStaticDir(ctx, root, map[string]kernfs.Inode{
			"max_map_count":     fs.newInode(ctx, root, 0444, newStaticFile("2147483647\n")),
			"mmap_min_addr":     fs.newInode(ctx, root, 0444, &mmapMinAddrData{k: k}),
			"overcommit_memory": fs.newInode(ctx, root, 0444, newStaticFile("0\n")),
		}),
		"net": fs.newSysNetDir(ctx, root, k),
	})
}

// newSysNetDir returns the dentry corresponding to /proc/sys/net directory.
func (fs *filesystem) newSysNetDir(ctx context.Context, root *auth.Credentials, k *kernel.Kernel) kernfs.Inode {
	var contents map[string]kernfs.Inode

	// TODO(gvisor.dev/issue/1833): Support for using the network stack in the
	// network namespace of the calling process.
	if stack := k.RootNetworkNamespace().Stack(); stack != nil {
		contents = map[string]kernfs.Inode{
			"ipv4": fs.newStaticDir(ctx, root, map[string]kernfs.Inode{
				"ip_forward":          fs.newInode(ctx, root, 0444, &ipForwarding{stack: stack}),
				"ip_local_port_range": fs.newInode(ctx, root, 0644, &portRange{stack: stack}),
				"tcp_recovery":        fs.newInode(ctx, root, 0644, &tcpRecoveryData{stack: stack}),

Review comment (suggestion):
While we're at it, I think we should also add support for the other two files in this category: https://docs.kernel.org/admin-guide/sysctl/kernel.html#osrelease-ostype-version

```
"osrelease": fs.newInode(ctx, root, 0444, newStaticFile(version.LinuxRelease)),
"ostype": fs.newInode(ctx, root, 0444, newStaticFile(version.LinuxSysname)),
"version": fs.newInode(ctx, root, 0444, newStaticFile(version.LinuxVersion)),
```

Code after:
// newSysDir returns the dentry corresponding to /proc/sys directory.
func (fs *filesystem) newSysDir(ctx context.Context, root *auth.Credentials, k *kernel.Kernel) kernfs.Inode {
	return fs.newStaticDir(ctx, root, map[string]kernfs.Inode{
		"kernel": fs.newStaticDir(ctx, root, map[string]kernfs.Inode{
			"cap_last_cap": fs.newInode(ctx, root, 0444, newStaticFile(fmt.Sprintf("%d\n", linux.CAP_LAST_CAP))),
			"hostname":     fs.newInode(ctx, root, 0444, &hostnameData{}),
			"overflowgid":  fs.newInode(ctx, root, 0444, newStaticFile(fmt.Sprintf("%d\n", auth.OverflowGID))),
			"overflowuid":  fs.newInode(ctx, root, 0444, newStaticFile(fmt.Sprintf("%d\n", auth.OverflowUID))),
			"pid_max":      fs.newInode(ctx, root, 0644, newStaticFile(fmt.Sprintf("%d\n", kernel.TasksLimit))),
			"random": fs.newStaticDir(ctx, root, map[string]kernfs.Inode{
				"boot_id": fs.newInode(ctx, root, 0444, newStaticFile(randUUID())),
			}),
			"sem":    fs.newInode(ctx, root, 0444, newStaticFile(fmt.Sprintf("%d\t%d\t%d\t%d\n", linux.SEMMSL, linux.SEMMNS, linux.SEMOPM, linux.SEMMNI))),
			"shmall": fs.newInode(ctx, root, 0444, ipcData(linux.SHMALL)),
			"shmmax": fs.newInode(ctx, root, 0444, ipcData(linux.SHMMAX)),
			"shmmni": fs.newInode(ctx, root, 0444, ipcData(linux.SHMMNI)),
			"msgmni": fs.newInode(ctx, root, 0444, ipcData(linux.MSGMNI)),
			"msgmax": fs.newInode(ctx, root, 0444, ipcData(linux.MSGMAX)),
			"msgmnb": fs.newInode(ctx, root, 0444, ipcData(linux.MSGMNB)),
			"yama": fs.newStaticDir(ctx, root, map[string]kernfs.Inode{
				"ptrace_scope": fs.newYAMAPtraceScopeFile(ctx, k, root),
			}),
			"keys": fs.newStaticDir(ctx, root, map[string]kernfs.Inode{
				"maxkeys": fs.newMaxKeySizeFile(ctx, k, root),
			}),
			"osrelease": fs.newInode(ctx, root, 0444, newStaticFile(version.LinuxRelease)),
			"ostype":    fs.newInode(ctx, root, 0444, newStaticFile(version.LinuxSysname)),
			"version":   fs.newInode(ctx, root, 0444, newStaticFile(version.LinuxVersion)),
		}),
		"fs": fs.newStaticDir(ctx, root, map[string]kernfs.Inode{
			"nr_open": fs.newInode(ctx, root, 0644, &atomicInt32File{val: &k.MaxFDLimit, min: 8, max: kernel.MaxFdLimit}),
		}),
		"vm": fs.newStaticDir(ctx, root, map[string]kernfs.Inode{
			"max_map_count":     fs.newInode(ctx, root, 0444, newStaticFile("2147483647\n")),
			"mmap_min_addr":     fs.newInode(ctx, root, 0444, &mmapMinAddrData{k: k}),
			"overcommit_memory": fs.newInode(ctx, root, 0444, newStaticFile("0\n")),
		}),
		"net": fs.newSysNetDir(ctx, root, k),
	})
}

// newSysNetDir returns the dentry corresponding to /proc/sys/net directory.
func (fs *filesystem) newSysNetDir(ctx context.Context, root *auth.Credentials, k *kernel.Kernel) kernfs.Inode {
	var contents map[string]kernfs.Inode

	// TODO(gvisor.dev/issue/1833): Support for using the network stack in the
	// network namespace of the calling process.
	if stack := k.RootNetworkNamespace().Stack(); stack != nil {
		contents = map[string]kernfs.Inode{
			"ipv4": fs.newStaticDir(ctx, root, map[string]kernfs.Inode{
				"ip_forward":          fs.newInode(ctx, root, 0444, &ipForwarding{stack: stack}),