Skip to content

Commit d4ecaa0

Browse files
ostenbomgcapizziJulia Nedialkova
committed
os: Add support for long path names on unix RemoveAll
On unix systems, long enough path names will fail when performing syscalls like `Lstat`. The current RemoveAll uses several of these syscalls, and so will fail for long paths. This can be risky, as it can let users "hide" files from the system or otherwise make long enough paths for programs to fail. By using `Unlinkat` and `Openat` syscalls instead, RemoveAll is safer on unix systems. Initially implemented for linux, darwin, freebsd and openbsd. Fixes golang#27029 Co-authored-by: Giuseppe Capizzi <[email protected]> Co-authored-by: Julia Nedialkova <[email protected]>
1 parent 930ce09 commit d4ecaa0

21 files changed

+714
-225
lines changed

src/internal/syscall/unix/at.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build linux darwin freebsd openbsd netbsd dragonfly
6+
7+
package unix
8+
9+
import (
10+
"syscall"
11+
"unsafe"
12+
)
13+
14+
func Unlinkat(dirfd int, path string, flags int) error {
15+
var p *byte
16+
p, err := syscall.BytePtrFromString(path)
17+
if err != nil {
18+
return err
19+
}
20+
21+
_, _, errno := syscall.Syscall(unlinkatTrap, uintptr(dirfd), uintptr(unsafe.Pointer(p)), uintptr(flags))
22+
if errno != 0 {
23+
return errno
24+
}
25+
26+
return nil
27+
}
28+
29+
func Openat(dirfd int, path string, flags int, perm uint32) (int, error) {
30+
var p *byte
31+
p, err := syscall.BytePtrFromString(path)
32+
if err != nil {
33+
return 0, err
34+
}
35+
36+
fd, _, errno := syscall.Syscall6(openatTrap, uintptr(dirfd), uintptr(unsafe.Pointer(p)), uintptr(flags), uintptr(perm), 0, 0)
37+
if errno != 0 {
38+
return 0, errno
39+
}
40+
41+
return int(fd), nil
42+
}
43+
44+
func Fstatat(dirfd int, path string, stat *syscall.Stat_t, flags int) error {
45+
var p *byte
46+
p, err := syscall.BytePtrFromString(path)
47+
if err != nil {
48+
return err
49+
}
50+
51+
_, _, errno := syscall.Syscall6(fstatatTrap, uintptr(dirfd), uintptr(unsafe.Pointer(p)), uintptr(unsafe.Pointer(stat)), uintptr(flags), 0, 0)
52+
if errno != 0 {
53+
return errno
54+
}
55+
56+
return nil
57+
58+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package unix
6+
7+
const unlinkatTrap = uintptr(472)
8+
const openatTrap = uintptr(463)
9+
const fstatatTrap = uintptr(469)
10+
11+
const AT_REMOVEDIR = 0x80
12+
const AT_SYMLINK_NOFOLLOW = 0x0020
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package unix
6+
7+
import "syscall"
8+
9+
const unlinkatTrap uintptr = syscall.SYS_UNLINKAT
10+
const openatTrap uintptr = syscall.SYS_OPENAT
11+
const fstatatTrap uintptr = syscall.SYS_FSTATAT
12+
13+
const AT_REMOVEDIR = 0x2
14+
const AT_SYMLINK_NOFOLLOW = 0x1
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package unix
6+
7+
import "syscall"
8+
9+
const unlinkatTrap uintptr = syscall.SYS_UNLINKAT
10+
const openatTrap uintptr = syscall.SYS_OPENAT
11+
const fstatatTrap uintptr = syscall.SYS_FSTATAT
12+
13+
const AT_REMOVEDIR = 0x800
14+
const AT_SYMLINK_NOFOLLOW = 0x200
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package unix
6+
7+
import "syscall"
8+
9+
const unlinkatTrap uintptr = syscall.SYS_UNLINKAT
10+
const openatTrap uintptr = syscall.SYS_OPENAT
11+
12+
const AT_REMOVEDIR = 0x200
13+
const AT_SYMLINK_NOFOLLOW = 0x100
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package unix
6+
7+
const fstatatTrap uintptr = uintptr(300)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package unix
6+
7+
const fstatatTrap uintptr = uintptr(262)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package unix
6+
7+
const fstatatTrap uintptr = uintptr(327)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package unix
6+
7+
const fstatatTrap uintptr = uintptr(79)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build mips64 mips64le
6+
7+
package unix
8+
9+
const fstatatTrap uintptr = uintptr(5252)

0 commit comments

Comments
 (0)