| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | @@ -3,6 +3,7 @@ |
| 6 | package reversesshfs |
| 7 | |
| 8 | import ( |
| 9 | + "encoding/binary" |
| 10 | "errors" |
| 11 | "io" |
| 12 | "os" |
| 13 | @@ -168,6 +169,17 @@ func (h *rootedHandlers) OpenFile(r *sftp.Request) (sftp.WriterAtReaderAt, error |
| 14 | return h.openFile(r) |
| 15 | } |
| 16 | |
| 17 | +// openMode returns the permissions sent with an Open request. |
| 18 | +// For Open, pkg/sftp puts the open flags in r.Flags and drops the attribute flags, |
| 19 | +// so r.AttrFlags and r.Attributes must not be used: SSH_FXF_APPEND reads as permissions. |
| 20 | +// Permissions alone are the only 4-byte attributes, which is what sshfs always sends. |
| 21 | +func openMode(r *sftp.Request) (uint32, bool) { |
| 22 | + if len(r.Attrs) != 4 { |
| 23 | + return 0, false |
| 24 | + } |
| 25 | + return binary.BigEndian.Uint32(r.Attrs) & 0o7777, true |
| 26 | +} |
| 27 | + |
| 28 | // Filecmd implements sftp.FileCmder. |
| 29 | func (h *rootedHandlers) Filecmd(r *sftp.Request) error { |
| 30 | switch r.Method { |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | @@ -208,6 +208,51 @@ func TestRootedChmodWithoutReadPermission(t *testing.T) { |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | +// SSH_FXF_APPEND has the bit of SSH_FILEXFER_ATTR_PERMISSIONS: an append open used to |
| 40 | +// panic the server, and with it the process serving the mount. |
| 41 | +func TestRootedOpenAppend(t *testing.T) { |
| 42 | + c, root := setupRooted(t, false) |
| 43 | + for _, s := range []string{"src/main.go", "src/new.go"} { |
| 44 | + f, err := c.OpenFile(filepath.Join(root, s), os.O_WRONLY|os.O_APPEND|os.O_CREATE) |
| 45 | + if err != nil { |
| 46 | + t.Fatalf("%s: %v", s, err) |
| 47 | + } |
| 48 | + // The server ignores SSH_FXF_APPEND: the client sends the offset, as sshfs does. |
| 49 | + if _, err := f.Seek(0, io.SeekEnd); err != nil { |
| 50 | + t.Fatalf("%s: %v", s, err) |
| 51 | + } |
| 52 | + if _, err := f.Write([]byte("+")); err != nil { |
| 53 | + t.Fatalf("%s: %v", s, err) |
| 54 | + } |
| 55 | + f.Close() |
| 56 | + } |
| 57 | + for s, want := range map[string]string{"src/main.go": "orig+", "src/new.go": "+"} { |
| 58 | + if b, err := os.ReadFile(filepath.Join(root, s)); err != nil || string(b) != want { |
| 59 | + t.Fatalf("%s: %q, %v", s, b, err) |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// sshfs sends its open flags with the permissions alone as attributes. |
| 65 | +func TestRootedOpenMode(t *testing.T) { |
| 66 | + if runtime.GOOS == "windows" { |
| 67 | + t.Skip("mode bits other than the owner write bit are not supported on Windows") |
| 68 | + } |
| 69 | + _, root, h := setupRootedHandlers(t, false) |
| 70 | + const write, appendFlag, creat = 0x2, 0x4, 0x8 |
| 71 | + r := sftp.NewRequest("Put", filepath.Join(root, "src", "run.sh")) |
| 72 | + r.Flags = write | appendFlag | creat |
| 73 | + r.Attrs = []byte{0, 0, 0o1, 0o355} // 0o755 |
| 74 | + f, err := h.openFile(r) |
| 75 | + if err != nil { |
| 76 | + t.Fatal(err) |
| 77 | + } |
| 78 | + f.Close() |
| 79 | + if fi, err := os.Stat(filepath.Join(root, "src", "run.sh")); err != nil || fi.Mode().Perm() != 0o755 { |
| 80 | + t.Fatalf("%v, %v", fi, err) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | func TestRootedDeniesProtectedWrites(t *testing.T) { |
| 85 | type env struct { |
| 86 | c *sftp.Client |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | @@ -104,8 +104,8 @@ func (h *rootedHandlers) openFile(r *sftp.Request) (*os.File, error) { |
| 92 | flags |= unix.O_EXCL |
| 93 | } |
| 94 | var mode uint32 = 0o644 |
| 95 | - if r.AttrFlags().Permissions { |
| 96 | - mode = r.Attributes().Mode & 0o7777 |
| 97 | + if m, ok := openMode(r); ok { |
| 98 | + mode = m |
| 99 | } |
| 100 | dirfd, base, err := h.openParent(rel) |
| 101 | if err != nil { |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | @@ -248,7 +248,7 @@ func (h *rootedHandlers) openFile(r *sftp.Request) (*os.File, error) { |
| 107 | disposition = windows.FILE_OPEN_IF |
| 108 | } |
| 109 | var attrs uint32 = fileAttributeNormal |
| 110 | - if r.AttrFlags().Permissions && r.Attributes().Mode&0o200 == 0 { |
| 111 | + if m, ok := openMode(r); ok && m&0o200 == 0 { |
| 112 | attrs = fileAttributeReadonly |
| 113 | } |
| 114 | fh, err := ntCreate(parent, base, access, attrs, disposition, options, windows.OBJ_DONT_REPARSE) |