Skip to content

Commit 37b15ba

Browse files
tenortimbradfitz
authored andcommitted
syscall: fix NaCl Link syscall error handling
The existing NaCl filesystem Link system call erroneously allowed a caller to call Link on an existing target which violates the POSIX standard and effectively corrupted the internal filesystem representation. Fixes #22383 Change-Id: I77b16c37af9bf00a1799fa84277f066180edac47 Reviewed-on: https://go-review.googlesource.com/76110 Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent ef0e2af commit 37b15ba

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/os/os_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,27 @@ func TestHardLink(t *testing.T) {
721721
if !SameFile(tostat, fromstat) {
722722
t.Errorf("link %q, %q did not create hard link", to, from)
723723
}
724+
// We should not be able to perform the same Link() a second time
725+
err = Link(to, from)
726+
switch err := err.(type) {
727+
case *LinkError:
728+
if err.Op != "link" {
729+
t.Errorf("Link(%q, %q) err.Op = %q; want %q", to, from, err.Op, "link")
730+
}
731+
if err.Old != to {
732+
t.Errorf("Link(%q, %q) err.Old = %q; want %q", to, from, err.Old, to)
733+
}
734+
if err.New != from {
735+
t.Errorf("Link(%q, %q) err.New = %q; want %q", to, from, err.New, from)
736+
}
737+
if !IsExist(err.Err) {
738+
t.Errorf("Link(%q, %q) err.Err = %q; want %q", to, from, err.Err, "file exists error")
739+
}
740+
case nil:
741+
t.Errorf("link %q, %q: expected error, got nil", from, to)
742+
default:
743+
t.Errorf("link %q, %q: expected %T, got %T %v", from, to, new(LinkError), err, err)
744+
}
724745
}
725746

726747
// chtmpdir changes the working directory to a new temporary directory and

src/syscall/fs_nacl.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,10 @@ func Link(path, link string) error {
636636
if ip.Mode&S_IFMT == S_IFDIR {
637637
return EPERM
638638
}
639+
_, _, err = fs.dirlookup(dp, elem)
640+
if err == nil {
641+
return EEXIST
642+
}
639643
fs.dirlink(dp, elem, ip)
640644
return nil
641645
}

0 commit comments

Comments
 (0)