diff --git a/docs/changelog.rst b/docs/changelog.rst index 12e418ada..29087fa0d 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -125,6 +125,8 @@ Detailed list of changes 0.51.1 [future] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +- dnd kitten: Fix a malicious remote drop source being able to write files outside the staging directory by sending a directory with the same name as a previously sent symlink (follow up to :cve:`2026-54056`) + - Fix a crash when calling :code:`Screen.hyperlink_at()` with coordinates outside the screen, such as from a watcher (:iss:`10890`) - Fix animated images stopping when switching to a tab without animations and then back (:iss:`10894`) diff --git a/kittens/dnd/drop.go b/kittens/dnd/drop.go index 8403c7ece..38d61a90b 100644 --- a/kittens/dnd/drop.go +++ b/kittens/dnd/drop.go @@ -117,28 +117,34 @@ type remote_dir_entry struct { const case_conflict_template = "case-conflict-%d-%s" -// The key under which a case and normalization insensitive filesystem -// stores name, so that names that would alias each other compare equal. -func filesystem_name_key(name string) string { +// The key under which the filesystem stores name, so that names that would +// alias each other compare equal. On a case and normalization insensitive +// filesystem, names differing only in case or normalization alias each other. +func filesystem_name_key(name string, is_case_sensitive_filesystem bool) string { + if is_case_sensitive_filesystem { + return name + } return strings.ToLower(norm.NFC.String(name)) } +// Ensure no two names in names refer to the same directory entry. This is +// needed even on case sensitive filesystems as a remote source can send +// duplicate names, which must not be allowed to alias each other, for example, +// a symlink and a directory with the same name. func uniqify_child_names(names []string, is_case_sensitive_filesystem bool) []string { - if !is_case_sensitive_filesystem { - seen := utils.NewSet[string](len(names)) - for i, x := range names { - names[i] = uniqify_name(x, seen) - } + seen := utils.NewSet[string](len(names)) + for i, x := range names { + names[i] = uniqify_name(x, seen, is_case_sensitive_filesystem) } return names } -func uniqify_name(name string, seen *utils.Set[string]) string { +func uniqify_name(name string, seen *utils.Set[string], is_case_sensitive_filesystem bool) string { ans := name - key := filesystem_name_key(ans) + key := filesystem_name_key(ans, is_case_sensitive_filesystem) for q := 0; seen.Has(key); q++ { ans = fmt.Sprintf(case_conflict_template, q+1, name) - key = filesystem_name_key(ans) + key = filesystem_name_key(ans, is_case_sensitive_filesystem) } seen.Add(key) return ans @@ -189,10 +195,7 @@ func do_local_copy(ctx context.Context, dest_dir *os.File, uri_list []string, ac if src_file, err = os.Open(path); err != nil { return err } - if err := utils.MkdirAt(dest_dir, name, st.Mode().Perm()); err != nil { - return err - } - d, err := utils.OpenDirAt(dest_dir, name) + d, err := utils.CreateExclusiveDirAt(dest_dir, name, st.Mode().Perm()) if err != nil { return err } @@ -499,7 +502,9 @@ func (d *remote_dir_entry) add_remote_data(data []byte, output_buf []byte, has_m return derr } } else { // directory - if f, derr := utils.CreateDirAt(d.base_dir.handle, d.name, 0o755); derr != nil { + // Never reuse an existing entry, it could be a symlink + // staged by the remote source. + if f, derr := utils.CreateExclusiveDirAt(d.base_dir.handle, d.name, 0o755); derr != nil { return derr } else { handle := new_dir_handle(f) @@ -987,9 +992,7 @@ func (dnd *dnd) all_mime_data_dropped() (err error) { drop_status.root_remote_dir.num_children_finished++ } else { name := filepath.Base(u.path) - if !dnd.is_case_sensitive_filesystem { - name = uniqify_name(name, seen) - } + name = uniqify_name(name, seen, dnd.is_case_sensitive_filesystem) c = &remote_dir_entry{base_dir: dnd.drop_status.dropping_to.newref(), name: name} dnd.queue_data_request(DC{Type: 'r', X: idx + 1, Y: i + 1}) } diff --git a/kittens/dnd/drop_test.go b/kittens/dnd/drop_test.go index 97d6472f3..7ed056ced 100644 --- a/kittens/dnd/drop_test.go +++ b/kittens/dnd/drop_test.go @@ -3,6 +3,7 @@ package dnd import ( + "bytes" "context" "fmt" "os" @@ -1223,6 +1224,37 @@ func TestUniqifyChildNamesNormalization(t *testing.T) { } } +func TestUniqifyChildNamesCaseSensitive(t *testing.T) { + got := uniqify_child_names([]string{"x", "X", "x", "x"}, true) + want := []string{"x", "X", fmt.Sprintf(case_conflict_template, 1, "x"), fmt.Sprintf(case_conflict_template, 2, "x")} + if !equalStringSlices(got, want) { + t.Errorf("got %q, want %q", got, want) + } +} + +// A remote directory entry must never be created through a symlink staged +// earlier by the remote source under the same name. +func TestRemoteDirDoesNotFollowStagedSymlink(t *testing.T) { + staging, outside := t.TempDir(), t.TempDir() + h := new_dir_handle(openDir(t, staging)) + defer h.unref() + buf := make([]byte, 4096) + link := &remote_dir_entry{base_dir: h.newref(), name: "x", item_type: 1, dest: &bufferWriteCloser{bytes.NewBufferString(outside)}} + if err := link.add_remote_data(nil, buf, false, true); err != nil { + t.Fatalf("staging symlink: %v", err) + } + dir := &remote_dir_entry{base_dir: h.newref(), name: "x", item_type: 2, dest: &bufferWriteCloser{bytes.NewBufferString("pwned")}} + if err := dir.add_remote_data(nil, buf, false, true); err == nil { + t.Fatal("creating a directory over a staged symlink unexpectedly succeeded") + } + if len(dir.children) != 0 { + t.Errorf("directory entry got children: %d", len(dir.children)) + } + if entries, err := os.ReadDir(outside); err != nil || len(entries) != 0 { + t.Errorf("outside directory was modified: %v %v", entries, err) + } +} + // TestRenameContents_SimpleFiles verifies that plain files are moved from src // to dst. func TestRenameContents_SimpleFiles(t *testing.T) { diff --git a/tools/utils/file_at_fd.go b/tools/utils/file_at_fd.go index 398c3ceb7..6b701c96c 100644 --- a/tools/utils/file_at_fd.go +++ b/tools/utils/file_at_fd.go @@ -85,17 +85,30 @@ func CreateExclusiveAt(dirFile *os.File, name string, permissions os.FileMode) ( return openAt(dirFile, name, unix.O_RDWR|unix.O_CREAT|unix.O_EXCL, permissions) } +// Opens a directory relative to the directory pointed to by dirFile. Fails if +// name is a symlink, even one pointing to a directory. +func OpenDirNoFollowAt(dirFile *os.File, name string) (*os.File, error) { + return openAt(dirFile, name, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_NOFOLLOW, 0) +} + // Create the specified directory, open it and return the file object. If the // directory already exists, it is opened and returned, without changing its -// permissions, matching the behavior of CreateAt(). +// permissions, matching the behavior of CreateAt(). Fails if name is a +// symlink, it is never followed. func CreateDirAt(parent *os.File, name string, permissions os.FileMode) (*os.File, error) { - if err := MkdirAt(parent, name, permissions); err != nil { - if errors.Is(err, unix.EEXIST) { - return OpenDirAt(parent, name) - } + if err := MkdirAt(parent, name, permissions); err != nil && !errors.Is(err, unix.EEXIST) { return nil, err } - return OpenDirAt(parent, name) + return OpenDirNoFollowAt(parent, name) +} + +// Create the specified directory, open it and return the file object. Fails +// if a directory entry with the same name already exists. +func CreateExclusiveDirAt(parent *os.File, name string, permissions os.FileMode) (*os.File, error) { + if err := MkdirAt(parent, name, permissions); err != nil { + return nil, err + } + return OpenDirNoFollowAt(parent, name) } // Internal helper to wrap the unix.Openat syscall diff --git a/tools/utils/file_at_fd_test.go b/tools/utils/file_at_fd_test.go index ec2c04aa1..8ebd5030d 100644 --- a/tools/utils/file_at_fd_test.go +++ b/tools/utils/file_at_fd_test.go @@ -163,6 +163,25 @@ func TestCreateDirAt(t *testing.T) { t.Fatalf("CreateDirAt existing: %v", err) } sub2.Close() + + if err := os.Symlink(tmp, filepath.Join(tmp, "link")); err != nil { + t.Fatal(err) + } + if sub3, err := CreateDirAt(d, "link", 0700); err == nil { + sub3.Close() + t.Fatal("CreateDirAt followed a symlink") + } + for _, name := range []string{"mydir", "link"} { + if sub4, err := CreateExclusiveDirAt(d, name, 0700); err == nil { + sub4.Close() + t.Fatalf("CreateExclusiveDirAt succeeded for existing entry: %s", name) + } + } + sub5, err := CreateExclusiveDirAt(d, "newdir", 0700) + if err != nil { + t.Fatalf("CreateExclusiveDirAt new: %v", err) + } + sub5.Close() } func TestSymlinkAt(t *testing.T) {