From 3851705a73235baa6d153970c95921d17a39d77a Mon Sep 17 00:00:00 2001 From: Ruidong Cao Date: Thu, 28 Feb 2019 16:43:59 -0800 Subject: Fix procfs bugs Current procfs has some bugs. After executing ls twice, many dirs come out with same name like "1" or ".". Files like "cpuinfo" disappear. Here variable names is a slice with cap() > len(). Sort after appending to it will not alloc a new space and impact orignal slice. Same to m. Signed-off-by: Ruidong Cao Change-Id: I83e5cd1c7968c6fe28c35ea4fee497488d4f9eef PiperOrigin-RevId: 236222270 --- pkg/sentry/fs/dentry.go | 3 ++- pkg/sentry/fs/ramfs/dir.go | 13 ++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pkg/sentry/fs/dentry.go b/pkg/sentry/fs/dentry.go index ef6d1a870..4879df4d6 100644 --- a/pkg/sentry/fs/dentry.go +++ b/pkg/sentry/fs/dentry.go @@ -185,7 +185,8 @@ func NewSortedDentryMap(entries map[string]DentAttr) *SortedDentryMap { return s } -// GetAll returns all names and entries in s. +// GetAll returns all names and entries in s. Callers should not modify the +// returned values. func (s *SortedDentryMap) GetAll() ([]string, map[string]DentAttr) { return s.names, s.entries } diff --git a/pkg/sentry/fs/ramfs/dir.go b/pkg/sentry/fs/ramfs/dir.go index 696825eb5..4da876ebd 100644 --- a/pkg/sentry/fs/ramfs/dir.go +++ b/pkg/sentry/fs/ramfs/dir.go @@ -148,7 +148,18 @@ func (d *Dir) FindChild(name string) (*fs.Inode, bool) { func (d *Dir) Children() ([]string, map[string]fs.DentAttr) { d.mu.Lock() defer d.mu.Unlock() - return d.dentryMap.GetAll() + + // Return a copy to prevent callers from modifying our children. + names, entries := d.dentryMap.GetAll() + namesCopy := make([]string, len(names)) + copy(namesCopy, names) + + entriesCopy := make(map[string]fs.DentAttr) + for k, v := range entries { + entriesCopy[k] = v + } + + return namesCopy, entriesCopy } // removeChildLocked attempts to remove an entry from this directory. -- cgit v1.2.3