radareorg/radare2-r2pipe

Confusing error message "OSError: [Errno 22] Invalid argument" when incorrect filepath is specified for `open`

projectbtle opened this issue · 14 comments

Issue Description:
When r2pipe is used to open a file, and the specified filepath is incorrect (i.e., points to a non-existent file), the stack trace doesn't end with "Cannot open file". It instead ends with "OSError: [Errno 22] Invalid argument" and points to the error being in open_sync.py. This can be quite confusing when troubleshooting.

Code that throws error (test.py):

import r2pipe

filepath = './make1.bin'

r2 = r2pipe.open(filepath, flags=['-m', '0'])
r2.cmd('e asm.arch=arm')
...
r2.quit()

Error message:

C:\code>python test.py
        [r] Cannot open './make1.bin'
        Traceback (most recent call last):
          File "test.py", line 5, in <module>
            r2.cmd('e asm.arch=arm')
          File "%AppData%\Local\Programs\Python\Python38-32\lib\site-packages\r2pipe\open_base.py", line 206, in cmd
            res = self._cmd(cmd, **kwargs)
          File "%AppData%\Local\Programs\Python\Python38-32\lib\site-packages\r2pipe\open_sync.py", line 102, in _cmd_process
            self.process.stdin.write((cmd + '\n').encode('utf8'))
        OSError: [Errno 22] Invalid argument

Even though the correct error is displayed right at the beginning, the OSError at the end is confusing when troubleshooting (because we tend to start from the last call and work our way up).

Environment:

  • OS:
    Windows 10 64-bit

  • radare2:
    radare2 4.3.0-git 23679 @ windows-x86-32 git.4.2.0-16-g04f065c68
    commit: 04f065c68c35e49996dc138560e99489e0a45dcb build: 25/01/2020__ 8:31:33.55

  • r2pipe:
    Version: 1.4.2

  • Python:
    Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)] on win32

Also encountering this issue, here is an example of r2pipe claiming my argument is invalid:

C:\Users\sordidlist
λ python3 -c "import r2pipe; r = r2pipe.open('C:/Users/sordidlist/Desktop/brainpan.exe', flags=['-A']); r.cmd('iz')"
Warning: set your favourite calling convention in `e anal.cc=?`
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\sordidlist\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\r2pipe\open_base.py", line 232, in cmd
    res = self._cmd(cmd, **kwargs)
  File "C:\Users\sordidlist\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\r2pipe\open_sync.py", line 109, in _cmd_process
    self.process.stdin.write((cmd + "\n").encode("utf8"))
OSError: [Errno 22] Invalid argument

Here is me doing the same thing in radare2.exe

C:\Users\sordidlist
λ radare2.exe -A Desktop\brainpan.exe
[Warning: set your favourite calling convention in `e anal.cc=?`
[x] Analyze all flags starting with sym. and entry0 (aa)
[x] Analyze function calls (aac)
[x] Analyze len bytes of instructions for references (aar)
[x] Finding and parsing C++ vtables (avrr)
[x] Type matching analysis for all functions (aaft)
[x] Propagate noreturn information (aanr)
[x] Use -AA or aaaa to perform additional experimental analysis.
[0x31171280]> iz
[Strings]
nth paddr      vaddr      len size section type  string
-------------------------------------------------------
0   0x00001400 0x31173000 21  22   .rdata  ascii [get_reply] s = [%s]\n
1   0x00001418 0x31173018 38  39   .rdata  ascii [get_reply] copied %d bytes to buffer\n
2   0x0000143f 0x3117303f 10  11   .rdata  ascii shitstorm\n
3   0x0000144c 0x3117304c 664 665  .rdata  ascii _|                            _|                                        \n_|_|_|    _|  _|_|    _|_|_|
    _|_|_|    _|_|_|      _|_|_|  _|_|_|  \n_|    _|  _|_|      _|    _|  _|  _|    _|  _|    _|  _|    _|  _|    _|\n_|    _|  _|        _|    _|  _|  _|    _|  _|    _|  _|    _|  _|    _|\n_|_|_|    _|          _|_|_|  _|  _|    _|  _|_|_|      _|_|_|  _|    _|\n
    _|                          \n                                            _|\n\n[________________________ WELCOME TO BRAINPAN _________________________]\n                          ENTER THE PASSWORD                              \n\n                          >>
4   0x000016e8 0x311732e8 40  41   .rdata  ascii                           ACCESS DENIED\n
5   0x00001714 0x31173314 41  42   .rdata  ascii                           ACCESS GRANTED\n
6   0x0000173e 0x3117333e 27  28   .rdata  ascii [+] initializing winsock...
7   0x0000175a 0x3117335a 27  28   .rdata  ascii [!] winsock init failed: %d
8   0x00001776 0x31173376 6   7    .rdata  ascii done.\n
9   0x00001780 0x31173380 31  32   .rdata  ascii [!] could not create socket: %d
10  0x000017a0 0x311733a0 27  28   .rdata  ascii [+] server socket created.\n
11  0x000017bc 0x311733bc 19  20   .rdata  ascii [!] bind failed: %d
12  0x000017d0 0x311733d0 25  26   .rdata  ascii [+] bind done on port %d\n
13  0x000017ea 0x311733ea 29  30   .rdata  ascii [+] waiting for connections.\n
14  0x00001808 0x31173408 25  26   .rdata  ascii [+] received connection.\n
15  0x00001822 0x31173422 16  17   .rdata  ascii [+] check is %d\n
16  0x00001833 0x31173433 21  22   .rdata  ascii [!] accept failed: %d
17  0x00001849 0x31173449 17  18   .rdata  ascii [+] cleaning up.\n
18  0x00001860 0x31173460 33  34   .rdata  ascii -LIBGCCW32-EH-3-SJLJ-GTHR-MINGW32
19  0x00001884 0x31173484 44  45   .rdata  ascii w32_sharedptr->size == sizeof(W32_EH_SHARED)
20  0x000018b4 0x311734b4 48  49   .rdata  ascii ../../gcc-3.4.5/gcc/config/i386/w32-shared-ptr.c
21  0x000018e8 0x311734e8 38  39   .rdata  ascii GetAtomNameA (atom, s, sizeof(s)) != 0

[0x31171280]> exit

I'm on a Windows 10 Pro, 64-bit VM
Using radare2-5.3.0 and python 3.7.9

I gave it a try with the latest build from git, but got the same behavior.

I discovered that if I copy the file to the current dir AND take out the flags in the open, then it doesn't get the error. For instance, this ran on my Windows VM without an error:

python3 -c "import r2pipe; r = r2pipe.open('brainpan.exe'); print(r.cmd('aaa'))"

I didn't actually get any output from the print(r.cmd('aaa')) for some reason but it did execute without an error.

I found that if I tried only taking out the flag, or only using the binary in the same path, I still got the error. I had to make sure to do both steps.

the command 'aaa' only prints to stderr, so its expected to not get any output from it.

Thanks for testing, i also noticed you used / instead of \ which is what i would expect from a windows path to contain. i think all those issues are in the python implementation of r2pipe.

Let me know if you want to go deep into the python module to add some prints here and there to determine what's going on. The example you pasted works fine for me on linux and macos. So it's windows and python specific issue :/

I'd be happy to take a crack at it, if you don't mind helping me with specific places and things to test.

Regarding the /, yeah, I should have also mentioned I tried the normal Windows slashes but it thought it was an escape character:

python3 -c "import r2pipe; r = r2pipe.open('C:\Users\sordidlist\Desktop\brainpan.exe'); r.cmd('aaa')"
  File "<string>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Also, I have a radare2 functionality question but it's not really a bug, so I'm not sure this thread is the place for it. Would you rather I open a new issue?

a1ext commented

I'd be happy to take a crack at it, if you don't mind helping me with specific places and things to test.

Regarding the /, yeah, I should have also mentioned I tried the normal Windows slashes but it thought it was an escape character:


python3 -c "import r2pipe; r = r2pipe.open('C:\Users\sordidlist\Desktop\brainpan.exe'); r.cmd('aaa')"

  File "<string>", line 1

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Also, I have a radare2 functionality question but it's not really a bug, so I'm not sure this thread is the place for it. Would you rather I open a new issue?

Yeah, because you are writing path in python code. Have you tried double backslashes \\ or to use single backslashes in a raw Python string like r'C:\Users\sordidlist\Desktop\brainpan.exe'?

Can confirm it works if I escape the backslashes like \\, and also if I use r'string'. I've used byte strings in python before but I didn't know about raw strings, that's cool.

So it's a no-issue? :D

The only other thing is the flags=['-A'] thing but the workaround of doing r.cmd('aaa') works just fine for me. I'll re-test the flags issue with the binary in the local dir later today but as I recall, that didn't work on Windows.

If you've got bigger bugs to squash, I'm okay with closing this issue.

There are always bugs to fix in r2land, but that's not a reason to close the issue, it should be fixed. but i can only attempt blind fixes because i dont have or use windows, if the issue is only with -A now, i have an idea about where could be the bug. If so, i can send you a link to the last r2 for windows with that fix (when i push it) and you can confirm the fix?

Yep I can test that out this evening for sure

anything bad happened that evening? 😱

closing, 4 years without feedback is enough for me to wait if the issue is still there please reopen or create a new ticket testing latest r2 and r2pipe. many things has changed and got fixed since then