Change verity mu to be per file system

verity Mu should be per file system instead of global, so that enabling
and verifying in different file systems won't block each other.
Also Lock verity Mu in PRead.

PiperOrigin-RevId: 336779356
This commit is contained in:
Chong Cai
2020-10-12 17:35:22 -07:00
committed by gVisor bot
parent ef90fe1733
commit 4885931ac3
2 changed files with 19 additions and 13 deletions
+4 -4
View File
@@ -174,8 +174,8 @@ func (fs *filesystem) verifyChild(ctx context.Context, parent *dentry, child *de
return nil, err
}
verityMu.RLock()
defer verityMu.RUnlock()
fs.verityMu.RLock()
defer fs.verityMu.RUnlock()
// Read the offset of the child from the extended attributes of the
// corresponding Merkle tree file.
// This is the offset of the hash for child in its parent's Merkle tree
@@ -302,8 +302,8 @@ func (fs *filesystem) verifyStat(ctx context.Context, d *dentry, stat linux.Stat
return err
}
verityMu.RLock()
defer verityMu.RUnlock()
fs.verityMu.RLock()
defer fs.verityMu.RUnlock()
fd, err := vfsObj.OpenAt(ctx, fs.creds, &vfs.PathOperation{
Root: d.lowerMerkleVD,
+15 -9
View File
@@ -68,11 +68,6 @@ const sizeOfStringInt32 = 10
// flag.
var noCrashOnVerificationFailure bool
// verityMu synchronizes enabling verity files, protects files or directories
// from being enabled by different threads simultaneously. It also ensures that
// verity does not access files that are being enabled.
var verityMu sync.RWMutex
// FilesystemType implements vfs.FilesystemType.
//
// +stateify savable
@@ -106,6 +101,17 @@ type filesystem struct {
// to ensure consistent lock ordering between dentry.dirMu in different
// dentries.
renameMu sync.RWMutex `state:"nosave"`
// verityMu synchronizes enabling verity files, protects files or
// directories from being enabled by different threads simultaneously.
// It also ensures that verity does not access files that are being
// enabled.
//
// Also, the directory Merkle trees depends on the generated trees of
// its children. So they shouldn't be enabled the same time. This lock
// is for the whole file system to ensure that no more than one file is
// enabled the same time.
verityMu sync.RWMutex
}
// InternalFilesystemOptions may be passed as
@@ -594,10 +600,8 @@ func (fd *fileDescription) enableVerity(ctx context.Context, uio usermem.IO) (ui
return 0, syserror.EPERM
}
// Lock to prevent other threads performing enable or access the file
// while it's being enabled.
verityMu.Lock()
defer verityMu.Unlock()
fd.d.fs.verityMu.Lock()
defer fd.d.fs.verityMu.Unlock()
// In allowRuntimeEnable mode, the underlying fd and read/write fd for
// the Merkle tree file should have all been initialized. For any file
@@ -723,6 +727,8 @@ func (fd *fileDescription) PRead(ctx context.Context, dst usermem.IOSequence, of
return fd.lowerFD.PRead(ctx, dst, offset, opts)
}
fd.d.fs.verityMu.RLock()
defer fd.d.fs.verityMu.RUnlock()
// dataSize is the size of the whole file.
dataSize, err := fd.merkleReader.GetXattr(ctx, &vfs.GetXattrOptions{
Name: merkleSizeXattr,