msys2 / msys2-runtime

Our friendly fork of Cygwin 💖 https://cygwin.org 💖 see the wiki for details
https://github.com/msys2/msys2-runtime/wiki
GNU General Public License v2.0
183 stars 39 forks source link

Problem with infinite loop in console handling #107

Open cminyard opened 2 years ago

cminyard commented 2 years ago

In winsup/cygwin/fhandler_console.cc there is the following code:

             while (n < total_read)
                {
                  DWORD len;
                  WriteConsoleInputW (p->input_handle, input_rec + n,
                                      min (total_read - n, inrec_size1), &len);
                  n += len;
                }

I was working on a remote login program, and it turns out that if you create a pseudo console in a higher integrity process than the process using the console, WriteConsoleInput will not work (See https://learn.microsoft.com/en-us/answers/questions/1040676/createpseudoconsole-with-reduced-integrity-level.html for details on that)

If WriteConsoleInputW() fails the above code will go into an infinite loop.

I was able to fix my program with great pain and suffering, but things like the above should be written to avoid possible infinite loops. The lack of error checking in general doesn't seem good to me.

dscho commented 2 years ago

Please provide a patch by way of a Pull Request.

lazka commented 8 months ago

The code now lives in https://github.com/cygwin/cygwin/blob/main/winsup/cygwin/fhandler/console.cc there are three buggy loops like this, two for WriteConsoleInputW and one for ReadConsoleInputW.