Skip to content

Commit 6fb8e5b

Browse files
committed
os: Add support for long path names on unix RemoveAll
On unix systems, long enough path names will fail on doing syscall 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 and darwin. Fixes golang#27029 Co-authored-by: Giuseppe Capizzi <[email protected]> Co-authored-by: Julia Nedialkova <[email protected]> Change-Id: I9d8a634bb7a479e5ef80fc346aeff478bde1f607
1 parent 930ce09 commit 6fb8e5b

17 files changed

+633
-224
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
10+
const AT_REMOVEDIR = 0x80
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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 = 301
8+
const openatTrap uintptr = 295
9+
10+
const AT_REMOVEDIR = 0x200
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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 = 263
8+
const openatTrap uintptr = 257
9+
10+
const AT_REMOVEDIR = 0x200
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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(328)
8+
const openatTrap = uintptr(322)
9+
10+
const AT_REMOVEDIR = 0x200
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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(35)
8+
const openatTrap = uintptr(56)
9+
10+
const AT_REMOVEDIR = 0x200
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+
// +build mips64 mips64le
6+
7+
package unix
8+
9+
const unlinkatTrap = uintptr(5253)
10+
const openatTrap = uintptr(5247)
11+
12+
const AT_REMOVEDIR = 0x200
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+
// +build mips mipsle
6+
7+
package unix
8+
9+
const unlinkatTrap = uintptr(4294)
10+
const openatTrap = uintptr(4288)
11+
12+
const AT_REMOVEDIR = 0x200
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+
// +build ppc64 ppc64le
6+
7+
package unix
8+
9+
const unlinkatTrap = uintptr(292)
10+
const openatTrap = uintptr(286)
11+
12+
const AT_REMOVEDIR = 0x200
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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(294)
8+
const openatTrap = uintptr(288)
9+
10+
const AT_REMOVEDIR = 0x200
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 dragonfly freebsd netbsd openbsd
6+
7+
package unix
8+
9+
import (
10+
"syscall"
11+
"unsafe"
12+
)
13+
14+
func Openat(fdat int, path string, flags int, perm uint32) (int, error) {
15+
var pathBytePointer *byte
16+
pathBytePointer, err := syscall.BytePtrFromString(path)
17+
if err != nil {
18+
return 0, err
19+
}
20+
21+
fdPointer, _, errNo := syscall.Syscall6(openatTrap, uintptr(fdat), uintptr(unsafe.Pointer(pathBytePointer)), uintptr(flags), uintptr(perm), 0, 0)
22+
fd := int(fdPointer)
23+
if errNo != 0 {
24+
return 0, errNo
25+
}
26+
27+
return fd, nil
28+
}

0 commit comments

Comments
 (0)