Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmdline() can now throw OSError instead of AccessDenied #1443

Closed
amerry opened this issue Feb 27, 2019 · 7 comments
Closed

cmdline() can now throw OSError instead of AccessDenied #1443

amerry opened this issue Feb 27, 2019 · 7 comments

Comments

@amerry
Copy link

amerry commented Feb 27, 2019

When trying to get the cmdline() of a process without sufficient permissions on Windows, since 5.5.1, it raises OSError instead of psutil.AccessDenied.

Behaviour with 5.5.0:

>>> p = psutil.Process(26944)
>>> p
psutil.Process(pid=26944, name='svchost.exe', started='10:34:01')
>>> p.cmdline()
Traceback (most recent call last):
  File "C:\Python36-32\lib\site-packages\psutil\_pswindows.py", line 712, in cmdline
    ret = cext.proc_cmdline(self.pid, use_peb=True)
PermissionError: [WinError 5] Access is denied

During handling of the above exception, another exception occurred:

PermissionError: [WinError 5] Access is denied

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python36-32\lib\site-packages\psutil\__init__.py", line 768, in cmdline
    return self._proc.cmdline()
  File "C:\Python36-32\lib\site-packages\psutil\_pswindows.py", line 639, in wrapper
    return fun(self, *args, **kwargs)
  File "C:\Python36-32\lib\site-packages\psutil\_pswindows.py", line 715, in cmdline
    ret = cext.proc_cmdline(self.pid, use_peb=False)
SystemError: <class 'OSError'> returned a result with an error set

Behaviour with 5.5.1:

>>> p = psutil.Process(26944)
>>> p
psutil.Process(pid=26944, name='svchost.exe', started='10:34:01')
>>> p.cmdline()
Traceback (most recent call last):
  File "C:\Python36-32\lib\site-packages\psutil\_pswindows.py", line 635, in wrapper
    return fun(self, *args, **kwargs)
  File "C:\Python36-32\lib\site-packages\psutil\_pswindows.py", line 705, in cmdline
    ret = cext.proc_cmdline(self.pid)
PermissionError: [WinError 5] Access is denied

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python36-32\lib\site-packages\psutil\__init__.py", line 669, in cmdline
    return self._proc.cmdline()
  File "C:\Python36-32\lib\site-packages\psutil\_pswindows.py", line 638, in wrapper
    raise AccessDenied(self.pid, self._name)
psutil._exceptions.AccessDenied: psutil.AccessDenied (pid=26944, name='svchost.exe')

This is probably a direct result of #1398

@giampaolo
Copy link
Owner

giampaolo commented Feb 27, 2019

So, the difference in behavior between 5.5.0 and 5.5.1 is correct. Practically speaking it's good that you get AccessDenied and you can't do anything about that except catching AccessDenied or running the process with higher privilges.

As for the original change (#1398) I refactored that part of the code which now calls the 2 distinct methods from Python (instead of C) but it currently does not make any distinction about the Windows version against which the second method (no PEB) is tried:

ret = cext.proc_cmdline(self.pid, use_peb=False)

At this proposal I want to CC @EccoTheFlintstone and ask: in Python, should we avoid calling the use_peb=False method if Windows < 8.1? What happens if we do?
I know you already answered me this question but I kind of forgot. =)

@amerry
Copy link
Author

amerry commented Feb 27, 2019

Wait, sorry, I listed those the wrong way around. 5.5.0 has the right behaviour - throws AccessDenied - and 5.5.1 has the wrong behaviour - throws OSError.

One of my scripts broke when I upgraded, because I was throwing away AccessDenied, but that no longer works with 5.5.1.

@giampaolo
Copy link
Owner

Do you have Visual Studio installed? Could you try current GIT master version?
https://github.com/giampaolo/psutil/blob/master/INSTALL.rst#windows

@amerry
Copy link
Author

amerry commented Feb 27, 2019

I do! Compiling was surprisingly painless. And it looks like the problem doesn't exist on master:

>>> psutil.__version__
'5.6.0'
>>> p = psutil.Process(26944)
>>> p
psutil.Process(pid=26944, name='svchost.exe', started='10:34:01')
>>> p.cmdline()
Traceback (most recent call last):
  File "C:\dev\third-party\psutil\psutil\_pswindows.py", line 735, in cmdline
    ret = cext.proc_cmdline(self.pid, use_peb=True)
PermissionError: [WinError 5] Access is denied

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\dev\third-party\psutil\psutil\_pswindows.py", line 661, in wrapper
    return fun(self, *args, **kwargs)
  File "C:\dev\third-party\psutil\psutil\_pswindows.py", line 738, in cmdline
    ret = cext.proc_cmdline(self.pid, use_peb=False)
PermissionError: [WinError 5] Access is denied

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\dev\third-party\psutil\psutil\__init__.py", line 786, in cmdline
    return self._proc.cmdline()
  File "C:\dev\third-party\psutil\psutil\_pswindows.py", line 664, in wrapper
    raise AccessDenied(self.pid, self._name)
psutil.AccessDenied: psutil.AccessDenied (pid=26944, name='svchost.exe')

@amerry amerry closed this as completed Feb 27, 2019
@giampaolo
Copy link
Owner

Beautiful. Reopening as per my question to @EccoTheFlintstone.

@giampaolo giampaolo reopened this Feb 27, 2019
@EccoTheFlintstone
Copy link
Contributor

At this proposal I want to CC @EccoTheFlintstone and ask: in Python, should we avoid calling the use_peb=False method if Windows < 8.1? What happens if we do?
I know you already answered me this question but I kind of forgot. =)

I think the kernel will return an error code stating that the class 60 (ProcessCommandLineInformation) you requested in NtQueryInformationProcess is not supported

I did a check on the windows version (being >= 8.1) before calling this to avoid this case (and calling NtQueryInformationProcess for nothing)

@giampaolo
Copy link
Owner

This was solved as part of #1446. Closing and releasing a new version very soon (kind of high priority).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants