From 189f313d0caebef915a85507ed977b82d46bc2df Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Wed, 24 Jan 2018 12:37:23 +0900 Subject: [PATCH] ssh/terminal: use duplicate handle in ReadPassword os.NewFile assigns finalizer to close file handle passed into ReadPassword. But that is not expected. Make a duplicate of original file handle, and pass copy handle into ReadPassword instead. Fixes golang/go#23525 Change-Id: I4d6725e9a1cc20defd1b58afc383e35a7f9ee4e9 Reviewed-on: https://go-review.googlesource.com/89395 Reviewed-by: Alex Brainman Run-TryBot: Alex Brainman TryBot-Result: Gobot Gobot --- util_windows.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/util_windows.go b/util_windows.go index 92944f3..4933ac3 100644 --- a/util_windows.go +++ b/util_windows.go @@ -93,5 +93,13 @@ func ReadPassword(fd int) ([]byte, error) { windows.SetConsoleMode(windows.Handle(fd), old) }() - return readPasswordLine(os.NewFile(uintptr(fd), "stdin")) + var h windows.Handle + p, _ := windows.GetCurrentProcess() + if err := windows.DuplicateHandle(p, windows.Handle(fd), p, &h, 0, false, windows.DUPLICATE_SAME_ACCESS); err != nil { + return nil, err + } + + f := os.NewFile(uintptr(h), "stdin") + defer f.Close() + return readPasswordLine(f) }