aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorJakob Unterwurzacher2026-11-11 21:12:21 +0100
committerJakob Unterwurzacher2026-11-11 21:12:21 +0100
commit3bfeb314d993e9bf88e343b446f4b247dead3695 (patch)
tree14c00aed50edae7e86ff5f7e3ad610ff1cdb3707 /internal
parentdfdf47d790fa758a9f443764ad296e86c9007ece (diff)
syscallcompat: freebsd: Renameat2: don't follow symlinks for RENAME_NOREPLACE
The RENAME_NOREPLACE emulation checked for an existing newpath with Fstatat(..., 0), which follows symlinks. A dangling symlink at newpath returned ENOENT, so we went ahead and replaced it, violating NOREPLACE. Use AT_SYMLINK_NOFOLLOW. Also return errors other than ENOENT instead of proceeding with the rename, and drop a duplicated NOREPLACE|EXCHANGE check. The check-then-rename emulation remains inherently racy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'internal')
-rw-r--r--internal/syscallcompat/sys_freebsd.go12
1 files changed, 7 insertions, 5 deletions
diff --git a/internal/syscallcompat/sys_freebsd.go b/internal/syscallcompat/sys_freebsd.go
index 0d28f3e..9c8c9e8 100644
--- a/internal/syscallcompat/sys_freebsd.go
+++ b/internal/syscallcompat/sys_freebsd.go
@@ -140,19 +140,21 @@ func Renameat2(olddirfd int, oldpath string, newdirfd int, newpath string, flags
if flags&(RENAME_NOREPLACE|RENAME_EXCHANGE) == RENAME_NOREPLACE|RENAME_EXCHANGE {
return unix.EINVAL
}
- if flags&(RENAME_NOREPLACE|RENAME_EXCHANGE) == RENAME_NOREPLACE|RENAME_EXCHANGE {
- return unix.EINVAL
- }
if flags&RENAME_NOREPLACE != 0 {
var st unix.Stat_t
- err = unix.Fstatat(newdirfd, newpath, &st, 0)
+ // AT_SYMLINK_NOFOLLOW: a (possibly dangling) symlink at newpath
+ // counts as existing, like on Linux.
+ err = unix.Fstatat(newdirfd, newpath, &st, unix.AT_SYMLINK_NOFOLLOW)
if err == nil {
- // Assume newpath is an existing file if we can stat() it.
// On Linux, RENAME_NOREPLACE fails with EEXIST if newpath
// already exists.
return unix.EEXIST
}
+ if err != unix.ENOENT {
+ // We could not determine whether newpath exists
+ return err
+ }
}
if flags&RENAME_EXCHANGE != 0 {
return unix.EINVAL