fix(brain_edit): snapshot, rollback and lock file go through the brain root (#192)
brain_edit did its own I/O by path around notes.Edit: - the pre-edit snapshot used os.ReadFile, which hung on a FIFO note held open by a writer; - the rollback (when the provenance stamp fails) used os.WriteFile; - the edit lock "<note>.lock" was opened with O_CREATE by path, so a planted dangling .lock symlink made a successful edit create a file at the link's target, in the brain or outside it. Snapshot is brainfs ReadRegular, rollback WriteRegular, and the lock is opened and removed through new brainfs OpenLock/RemoveLock: clean path, real parent directories, an existing lock must be regular, and the opened handle must be the file at the path (SameFile). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -136,6 +136,49 @@ func (b *Brain) ReadDirDirs(relDir string) ([]string, error) {
|
||||
return names, nil
|
||||
}
|
||||
|
||||
// OpenLock opens (creating if absent) a regular lock file at a clean
|
||||
// brain-relative path for flock, through the root (hyperguild#192): every
|
||||
// directory above it must be real, an existing lock must be a regular file,
|
||||
// and the opened handle must be the file at the path (os.Root follows an
|
||||
// in-root final symlink even with O_NOFOLLOW). A dangling or swapped-in
|
||||
// symlink is refused; nothing is created at its target.
|
||||
func (b *Brain) OpenLock(rel string) (*os.File, error) {
|
||||
if err := cleanRel(rel); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := b.realDir(path.Dir(rel)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if fi, err := b.root.Lstat(rel); err == nil && !fi.Mode().IsRegular() {
|
||||
return nil, fmt.Errorf("%s: %w", rel, ErrNotRegular)
|
||||
} else if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||
return nil, err
|
||||
}
|
||||
f, err := b.root.OpenFile(rel, os.O_RDWR|os.O_CREATE|syscall.O_NOFOLLOW|syscall.O_NONBLOCK, 0o644)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hfi, herr := f.Stat()
|
||||
lfi, lerr := b.root.Lstat(rel)
|
||||
if herr != nil || lerr != nil || !lfi.Mode().IsRegular() || !os.SameFile(lfi, hfi) {
|
||||
_ = f.Close()
|
||||
return nil, fmt.Errorf("%s: changed while opening: %w", rel, ErrNotRegular)
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// RemoveLock removes the lock file at rel through the root (a symlink there
|
||||
// is removed itself, never its target).
|
||||
func (b *Brain) RemoveLock(rel string) error {
|
||||
if err := cleanRel(rel); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := b.realDir(path.Dir(rel)); err != nil {
|
||||
return err
|
||||
}
|
||||
return b.root.Remove(rel)
|
||||
}
|
||||
|
||||
// ReadRegular reads a brain-relative path, refusing anything that is not a
|
||||
// regular file, both before the open and on the opened handle.
|
||||
func (b *Brain) ReadRegular(rel string) ([]byte, error) {
|
||||
|
||||
Reference in New Issue
Block a user