| 1 | diff -ruN a/pkg/reversesshfs/rooted.go b/pkg/reversesshfs/rooted.go |
| 2 | |
| 3 | |
| 4 | @@ -164,24 +164,45 @@ |
| 5 | // writableFile is a file opened by openFile: checked against readonlyNames, without following symlinks. |
| 6 | // FSETSTAT applies to it directly. Files opened for reading only may have been opened through a symlink |
| 7 | // to a read-only name, so FSETSTAT on them goes through the path checks of setstat. |
| 8 | -type writableFile struct{ *os.File } |
| 9 | +type writableFile struct { |
| 10 | + *os.File |
| 11 | + appends bool // opened with SSH_FXF_APPEND |
| 12 | +} |
| 13 | + |
| 14 | +// WriteAt ignores off for a file opened with SSH_FXF_APPEND, and writes at the end of the file |
| 15 | +// as it is on the host, as OpenSSH's sftp-server does: the client computed off from the size it |
| 16 | +// knew, and the host may have grown the file since. Requests are processed in order, so writes keep theirs. |
| 17 | +func (f writableFile) WriteAt(b []byte, off int64) (int, error) { |
| 18 | + if f.appends { |
| 19 | + return appendWrite(f.File, b) |
| 20 | + } |
| 21 | + return f.File.WriteAt(b, off) |
| 22 | +} |
| 23 | + |
| 24 | +func (h *rootedHandlers) openWritable(r *sftp.Request) (writableFile, error) { |
| 25 | + f, err := h.openFile(r) |
| 26 | + if err != nil { |
| 27 | + return writableFile{}, err |
| 28 | + } |
| 29 | + return writableFile{File: f, appends: r.Pflags().Append}, nil |
| 30 | +} |
| 31 | |
| 32 | // Filewrite implements sftp.FileWriter. |
| 33 | func (h *rootedHandlers) Filewrite(r *sftp.Request) (io.WriterAt, error) { |
| 34 | - f, err := h.openFile(r) |
| 35 | + f, err := h.openWritable(r) |
| 36 | if err != nil { |
| 37 | return nil, err |
| 38 | } |
| 39 | - return writableFile{f}, nil |
| 40 | + return f, nil |
| 41 | } |
| 42 | |
| 43 | // OpenFile implements sftp.OpenFileWriter. |
| 44 | func (h *rootedHandlers) OpenFile(r *sftp.Request) (sftp.WriterAtReaderAt, error) { |
| 45 | - f, err := h.openFile(r) |
| 46 | + f, err := h.openWritable(r) |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | - return writableFile{f}, nil |
| 51 | + return f, nil |
| 52 | } |
| 53 | |
| 54 | // openMode returns the permissions sent with an Open request. |
| 55 | @@ -251,7 +272,7 @@ |
| 56 | // SFTP v3 times are in seconds. |
| 57 | mtime := uint32(fi.ModTime().Unix()) |
| 58 | attrs := r.Attributes() |
| 59 | - return attrs.Atime == mtime && attrs.Mtime == mtime |
| 60 | + return attrs != nil && attrs.Atime == mtime && attrs.Mtime == mtime |
| 61 | } |
| 62 | |
| 63 | // Filelist implements sftp.FileLister. |
| 64 | diff -ruN a/pkg/reversesshfs/rooted_test.go b/pkg/reversesshfs/rooted_test.go |
| 65 | |
| 66 | |
| 67 | @@ -217,10 +217,7 @@ |
| 68 | if err != nil { |
| 69 | t.Fatalf("%s: %v", s, err) |
| 70 | } |
| 71 | - // The server ignores SSH_FXF_APPEND: the client sends the offset, as sshfs does. |
| 72 | - if _, err := f.Seek(0, io.SeekEnd); err != nil { |
| 73 | - t.Fatalf("%s: %v", s, err) |
| 74 | - } |
| 75 | + // Offset 0: with SSH_FXF_APPEND, writes go to the end whatever the client sends. |
| 76 | if _, err := f.Write([]byte("+")); err != nil { |
| 77 | t.Fatalf("%s: %v", s, err) |
| 78 | } |
| 79 | @@ -233,6 +230,54 @@ |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | +// The host grows the file after the client learned its size: an append must not overwrite that. |
| 84 | +func TestRootedAppendAfterHostWrite(t *testing.T) { |
| 85 | + c, root := setupRooted(t, false) |
| 86 | + p := filepath.Join(root, "src", "main.go") |
| 87 | + f, err := c.OpenFile(p, os.O_WRONLY|os.O_APPEND) |
| 88 | + if err != nil { |
| 89 | + t.Fatal(err) |
| 90 | + } |
| 91 | + defer f.Close() |
| 92 | + if _, err := f.WriteAt([]byte("-guest1"), 4); err != nil { |
| 93 | + t.Fatal(err) |
| 94 | + } |
| 95 | + hf, err := os.OpenFile(p, os.O_WRONLY|os.O_APPEND, 0) |
| 96 | + if err != nil { |
| 97 | + t.Fatal(err) |
| 98 | + } |
| 99 | + if _, err := hf.WriteString("-host"); err != nil { |
| 100 | + t.Fatal(err) |
| 101 | + } |
| 102 | + hf.Close() |
| 103 | + if _, err := f.WriteAt([]byte("-guest2"), 11); err != nil { |
| 104 | + t.Fatal(err) |
| 105 | + } |
| 106 | + if b, err := os.ReadFile(p); err != nil || string(b) != "orig-guest1-host-guest2" { |
| 107 | + t.Fatalf("%q, %v", b, err) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Attributes shorter than their flags announce: an error, not a panic of the process serving the mount. |
| 112 | +func TestRootedSetstatShortAttributes(t *testing.T) { |
| 113 | + _, root, h := setupRootedHandlers(t, false) |
| 114 | + for _, rel := range []string{"src/main.go", ".git/config"} { |
| 115 | + for _, flags := range []uint32{0x1, 0x2, 0x4, 0x8} { |
| 116 | + r := sftp.NewRequest("Setstat", filepath.Join(root, rel)) |
| 117 | + r.Flags = flags |
| 118 | + if err := h.Filecmd(r); err == nil { |
| 119 | + t.Fatalf("%s, flags %#x: no error", rel, flags) |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + r := sftp.NewRequest("Mkdir", filepath.Join(root, "src", "dir")) |
| 124 | + r.Flags = 0x4 |
| 125 | + if err := h.Filecmd(r); err != nil { |
| 126 | + t.Fatal(err) |
| 127 | + } |
| 128 | + assertUnchanged(t, root) |
| 129 | +} |
| 130 | + |
| 131 | // FSTAT and FSETSTAT apply to the open file, not to whatever is now at the path it was opened with. |
| 132 | func TestRootedFstatFsetstatAfterRename(t *testing.T) { |
| 133 | c, root := setupRooted(t, false) |
| 134 | diff -ruN a/pkg/reversesshfs/rooted_unix.go b/pkg/reversesshfs/rooted_unix.go |
| 135 | |
| 136 | |
| 137 | @@ -14,6 +14,12 @@ |
| 138 | |
| 139 | var errDenied error = unix.EACCES |
| 140 | |
| 141 | +// appendWrite writes b at the end of f, which openFile opened with O_APPEND. |
| 142 | +// pwrite(2) would honor its offset on macOS. |
| 143 | +func appendWrite(f *os.File, b []byte) (int, error) { |
| 144 | + return f.Write(b) |
| 145 | +} |
| 146 | + |
| 147 | type rootedSys struct { |
| 148 | rootFD int |
| 149 | } |
| 150 | @@ -93,7 +99,10 @@ |
| 151 | default: |
| 152 | flags |= unix.O_RDONLY |
| 153 | } |
| 154 | - // O_APPEND is not set, as it conflicts with WriteAt; the client sends offsets. |
| 155 | + // See writableFile.WriteAt. |
| 156 | + if pf.Append { |
| 157 | + flags |= unix.O_APPEND |
| 158 | + } |
| 159 | if pf.Creat { |
| 160 | flags |= unix.O_CREAT |
| 161 | } |
| 162 | @@ -148,8 +157,8 @@ |
| 163 | |
| 164 | func (h *rootedHandlers) mkdir(r *sftp.Request) error { |
| 165 | var mode uint32 = 0o755 |
| 166 | - if r.AttrFlags().Permissions { |
| 167 | - mode = r.Attributes().Mode & 0o7777 |
| 168 | + if a := r.Attributes(); a != nil && r.AttrFlags().Permissions { |
| 169 | + mode = a.Mode & 0o7777 |
| 170 | } |
| 171 | return h.inParent("mkdir", r.Filepath, func(dirfd int, base string) error { |
| 172 | return unix.Mkdirat(dirfd, base, mode) |
| 173 | @@ -211,13 +220,16 @@ |
| 174 | if err != nil { |
| 175 | return err |
| 176 | } |
| 177 | + flags := r.AttrFlags() |
| 178 | + attrs := r.Attributes() |
| 179 | + if attrs == nil { |
| 180 | + return unix.EINVAL |
| 181 | + } |
| 182 | dirfd, base, err := h.openParent(rel) |
| 183 | if err != nil { |
| 184 | return err |
| 185 | } |
| 186 | defer unix.Close(dirfd) |
| 187 | - flags := r.AttrFlags() |
| 188 | - attrs := r.Attributes() |
| 189 | if flags.Size { |
| 190 | fd, err := unix.Openat(dirfd, base, unix.O_WRONLY|unix.O_NOFOLLOW|unix.O_CLOEXEC|unix.O_NONBLOCK, 0) |
| 191 | if err != nil { |
| 192 | diff -ruN a/pkg/reversesshfs/rooted_windows.go b/pkg/reversesshfs/rooted_windows.go |
| 193 | |
| 194 | |
| 195 | @@ -15,6 +15,15 @@ |
| 196 | |
| 197 | var errDenied error = windows.ERROR_ACCESS_DENIED |
| 198 | |
| 199 | +// appendWrite writes b at the end of f, as it is when the write happens. |
| 200 | +func appendWrite(f *os.File, b []byte) (int, error) { |
| 201 | + var n uint32 |
| 202 | + // Offset and OffsetHigh both 0xFFFFFFFF: write to the end of the file (WriteFile documentation). |
| 203 | + ov := windows.Overlapped{Offset: 0xFFFFFFFF, OffsetHigh: 0xFFFFFFFF} |
| 204 | + err := windows.WriteFile(windows.Handle(f.Fd()), b, &n, &ov) |
| 205 | + return int(n), err |
| 206 | +} |
| 207 | + |
| 208 | // The first four are not defined in x/sys/windows. |
| 209 | const ( |
| 210 | fileNameNormalized = 0x0 // FILE_NAME_NORMALIZED |
| 211 | @@ -240,7 +249,7 @@ |
| 212 | default: |
| 213 | access = windows.FILE_GENERIC_READ |
| 214 | } |
| 215 | - // FILE_APPEND_DATA semantics are not requested, as the client sends offsets. |
| 216 | + // Appends are done by writableFile.WriteAt, so FILE_WRITE_DATA stays for truncation. |
| 217 | disposition := uint32(windows.FILE_OPEN) |
| 218 | switch { |
| 219 | case pf.Creat && pf.Excl: |