跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

rooted_linux.go

67 行 · 2.1 KB历史下载
1package reversesshfs
2
3import (
4 "errors"
5 "strconv"
6
7 "github.com/pkg/sftp"
8 "golang.org/x/sys/unix"
9)
10
11func statVFS(st *unix.Statfs_t) *sftp.StatVFS {
12 return &sftp.StatVFS{
13 Bsize: uint64(st.Bsize),
14 Frsize: uint64(st.Frsize),
15 Blocks: st.Blocks,
16 Bfree: st.Bfree,
17 Bavail: st.Bavail,
18 Files: st.Files,
19 Ffree: st.Ffree,
20 Favail: st.Ffree,
21 Flag: uint64(st.Flags),
22 Namemax: uint64(st.Namelen),
23 }
24}
25
26// renameNoReplace renames, failing with EEXIST when the new path exists, atomically.
27func renameNoReplace(oldfd int, oldBase string, newfd int, newBase string) error {
28 return unix.Renameat2(oldfd, oldBase, newfd, newBase, unix.RENAME_NOREPLACE)
29}
30
31// fchmodatNoFollow never follows a symlink at base, and fails on one.
32// Only fchmodat2 (kernel 6.6+) supports AT_SYMLINK_NOFOLLOW, and it fails with EOPNOTSUPP on a symlink.
33func fchmodatNoFollow(dirfd int, base string, mode uint32) error {
34 err := unix.Fchmodat(dirfd, base, mode, unix.AT_SYMLINK_NOFOLLOW)
35 if !errors.Is(err, unix.EOPNOTSUPP) && !errors.Is(err, unix.ENOSYS) {
36 return err
37 }
38 return fchmodatOPath(dirfd, base, mode)
39}
40
41// fchmodatOPath is the fallback of fchmodatNoFollow, as in glibc:
42// fchmod does not work on an O_PATH fd, but chmod on its /proc/self/fd entry does,
43// and unlike opening base for reading, needs no read permission.
44func fchmodatOPath(dirfd int, base string, mode uint32) error {
45 fd, err := unix.Openat(dirfd, base, unix.O_PATH|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0)
46 if err != nil {
47 return err
48 }
49 defer unix.Close(fd)
50 var st unix.Stat_t
51 if err := unix.Fstat(fd, &st); err != nil {
52 return err
53 }
54 if st.Mode&unix.S_IFMT == unix.S_IFLNK {
55 return unix.ELOOP
56 }
57 return unix.Chmod("/proc/self/fd/"+strconv.Itoa(fd), mode)
58}
59
60// NO_XDEV also catches a bind mount pointing outside the authorized directory
61// on the same filesystem. Kernels without openat2 fail closed.
62func cloudOpenat(fd int, name string, flags int, mode uint32) (int, error) {
63 if flags&unix.O_CREAT == 0 {
64 mode = 0
65 }
66 return unix.Openat2(fd, name, &unix.OpenHow{Flags: uint64(flags), Mode: uint64(mode), Resolve: unix.RESOLVE_NO_XDEV | unix.RESOLVE_NO_SYMLINKS | unix.RESOLVE_BENEATH})
67}