1
0
mirror of https://github.com/golang/go synced 2024-11-19 13:44:52 -07:00

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 <bradfitz@golang.org>
This commit is contained in:
Tim Wright 2017-11-04 19:35:23 -07:00 committed by Brad Fitzpatrick
parent ef0e2af7b0
commit 37b15baa3b
2 changed files with 25 additions and 0 deletions

View File

@ -721,6 +721,27 @@ func TestHardLink(t *testing.T) {
if !SameFile(tostat, fromstat) { if !SameFile(tostat, fromstat) {
t.Errorf("link %q, %q did not create hard link", to, from) t.Errorf("link %q, %q did not create hard link", to, from)
} }
// We should not be able to perform the same Link() a second time
err = Link(to, from)
switch err := err.(type) {
case *LinkError:
if err.Op != "link" {
t.Errorf("Link(%q, %q) err.Op = %q; want %q", to, from, err.Op, "link")
}
if err.Old != to {
t.Errorf("Link(%q, %q) err.Old = %q; want %q", to, from, err.Old, to)
}
if err.New != from {
t.Errorf("Link(%q, %q) err.New = %q; want %q", to, from, err.New, from)
}
if !IsExist(err.Err) {
t.Errorf("Link(%q, %q) err.Err = %q; want %q", to, from, err.Err, "file exists error")
}
case nil:
t.Errorf("link %q, %q: expected error, got nil", from, to)
default:
t.Errorf("link %q, %q: expected %T, got %T %v", from, to, new(LinkError), err, err)
}
} }
// chtmpdir changes the working directory to a new temporary directory and // chtmpdir changes the working directory to a new temporary directory and

View File

@ -636,6 +636,10 @@ func Link(path, link string) error {
if ip.Mode&S_IFMT == S_IFDIR { if ip.Mode&S_IFMT == S_IFDIR {
return EPERM return EPERM
} }
_, _, err = fs.dirlookup(dp, elem)
if err == nil {
return EEXIST
}
fs.dirlink(dp, elem, ip) fs.dirlink(dp, elem, ip)
return nil return nil
} }