Commit Graph

43 Commits

Author SHA1 Message Date
Todd C. Miller
e343e07543 Use #include <foo.h> instead of #include "foo.h" in most cases.
We rely on the include path to find many of these headers.  It
especially doesn't make sense to use #include "foo.h" for headers
in the top-level include directory.
2023-09-25 10:13:28 -06:00
Todd C. Miller
1a00423afd Add free function for sudo Python module.
This reduces the amount of memory leaked on unload.
2023-07-25 09:33:03 -06:00
Todd C. Miller
09b9632699 Merge sudo_module_register_loghandler and sudo_module_set_default_loghandler.
We now create the LogHandler class for each interpreter in
python_plugin_init() instead of just once in sudo_module_init().
This fixes the crash seen in Py_EndInterpreter() with Python 3.12
and significantly reduces the number of leaked objects tracked by
MemorySanitizer.
2023-07-24 11:07:45 -06:00
Todd C. Miller
e710f79420 Revert last change, wrong diff committed. 2023-07-20 07:49:17 -06:00
Todd C. Miller
a12dbfe909 We can rely on Py_FinalizeEx() to free sub-interpreters. 2023-07-19 09:16:51 -06:00
Todd C. Miller
7a10cdc286 _python_plugin_new_interpreter switches to the new interpreter
No need to do PyThreadState_Swap in the caller.
2023-07-20 06:58:53 -06:00
Todd C. Miller
cc939297a1 Call PyImport_AppendInittab after pre-initialization.
Also remove redundant PyConfig settings.
2023-07-19 09:13:23 -06:00
Todd C. Miller
da60384ad8 Use Py_InitializeFromConfig() not Py_InitializeEx() for Python >= 3.8.
Avoids deprecation warnings on Python 3.12.
2023-07-18 20:18:38 -06:00
Todd C. Miller
fa1b86fca6 Remove the Python plugin import blocker code.
The sudo.conf file is considered a trusted source of information
and these checks suffer from TOCTOU issues anyway.
2022-12-26 07:43:55 -07:00
Todd C. Miller
f26a2e7626 Fix some dead stores noted by PVS Studio.
Since rc is initialized to SUDO_RC_ERROR there is no need to set
it to SUDO_RC_ERROR again on failure if rc has not been changed
since initialization.
2022-12-11 13:46:04 -07:00
Todd C. Miller
13672f28df Make sudo pass -Wwrite-strings 2022-06-28 16:33:15 -06:00
Todd C. Miller
f5ac1317c4 Make sudo pass -Wmissing-prototypes 2022-06-27 12:48:03 -06:00
Todd C. Miller
dfda098ae7 Deinit python subinterpreters in reverse order (last to first).
This appears to work around a crash on OpenBSD with Python 3.9.10.
2022-03-06 18:39:12 -07:00
Todd C. Miller
853e710f4a Only emulate Py_FinalizeEx for Python 3.[0-5]. 2022-01-12 13:07:21 -07:00
Todd C. Miller
657897b8bf Check that the python module we actually loaded is what we intended.
This is intended to provide a more useful error message if the
user defines a module which conflicts with a system python module.
For example, a module called test.py would conflicts with the system
python test module.
2021-07-08 15:50:04 -06:00
Todd C. Miller
d688f4d34e Back out regex use in python tests, filter the output instead.
This makes it possible to regenerate the test output again.
Also adds an update_test_data target to the Makefile.
2020-11-11 19:04:01 -07:00
Todd C. Miller
84e6e6ccf9 Update copyright year on some files where it was out of date. 2020-08-31 14:09:36 -06:00
Todd C. Miller
cd74b83c21 Make most python tests pass with Python 3.4
Dictionary order is not stable in Python < 3.6 so we need to sort
by key to have consistent results.
The LogHandler output is also different on older Python versions.
Also, don't stop running python tests after the first error.
2020-04-07 14:03:58 -06:00
Todd C. Miller
8a2c0d784f Sort the list of possible plugins before printing it.
This gives more reproducible error messages for the tests.
2020-04-07 14:03:58 -06:00
Todd C. Miller
93f5e1be36 Fall back to using Py_Finalize() for Python version < 3.6 2020-04-06 07:05:20 -06:00
Robert Manner
c039a99c10 plugins/python/sudo_module: add sudo.LogHandler
so python log system can be used with sudo logsystem.
Loggers use it by default (the handler is set on the root logger).
If that is not the intent, it can be overridden explicitly.
2020-02-28 05:46:54 -07:00
Robert Manner
34b4bb72d6 plugins/python: autodetect ClassName field
If "ClassName" is not specified, load the one and only sudo.Plugin from
the module (if so), otherwise display which plugins are available from
which the system admin can choose.
2020-02-28 05:46:54 -07:00
Robert Manner
5c96b4407d plugins/python/plugin_common: add a default search path for python plugins
If the ModulePath is relative, assume it is under
"/usr/local/libexec/sudo/python" or wherever the sudo plugins are in a
"python" subdirectory.
2020-02-28 05:46:54 -07:00
Robert Manner
f387cdf53f plugins/python: make show_version display the plugin in verbose mode
Before it only displayed the plugin version, now it also displays
which python plugin is loaded to be more useful.
2020-02-26 13:15:52 -07:00
Robert Manner
38fc37b214 plugins/python/approval: fix show_version crash when it is not implemented
For approval plugins show_version is not optional.
2020-02-26 13:15:52 -07:00
Robert Manner
3be61db35c plugins/python: restore the original python inittab after interpreter deinit 2020-02-19 11:48:16 -07:00
Robert Manner
27de7dd24d plugins/python: only deinit interpreters when sudo unlinks the plugin
This only happens when sudo unloads the last python plugin.
The reason doing so is because there are some python modules which
does not support importing them again after destroying the interpreter
which has imported them previously.

Another solution would be to just leak the interpreters (let the kernel
free up), but then there might be some python resources like open files
would not get cleaned up correctly if the plugin is badly written.

Tests are meant to test the scenario sudo does, so I have modified them
to generally do not unlink but only a few times (~per plugin type) so it
does not use 48 interpreters (one gets started on every plugin->open) and
it is visible at least which type of plugin fails deinit if there is an
error.
2020-02-19 11:48:16 -07:00
Robert Manner
8a9218d161 plugins/python/debug: adapt debug refcount solution of sudoers plugin 2020-02-19 11:48:16 -07:00
Robert Manner
a71828b385 plugins/python/python_plugin_common.c: release py_args in close
even if the arguments are not used (eg. when there is no "close" call
in the plugin).

It was not really a memleak, because interpreter is deinitialized anyway,
which frees the object.
2020-02-14 09:25:37 -07:00
Robert Manner
23af39b005 plugins/python: add python approval plugin wrapper 2020-02-12 11:16:00 -07:00
Robert Manner
b1d2ccecd0 plugins/python: make storing errstr more explicit
The error is always stored in plugin_ctx, but it is only set into errstr
if the API version is enough. (Previously it worked the opposite:
we only stored the error if API level was enough.)
2020-02-12 11:16:00 -07:00
Robert Manner
b165242035 plugins/python/sudo_module: let a reject also supply error message
Same as sudo.PluginError exception, have a sudo.PluginReject exception
as well. Added common base exception as well.
2020-02-12 11:16:00 -07:00
Robert Manner
3dd5f37af7 plugins/python: add support for callback errstr arguments
Plugins can raise a sudo.PluginError exception to add context message
for the failure.

The callback's errstr gets filled up with the specified message.
But, as sudo expects a string constant (will not free the string),
we store it in the plugin context at least until next callback
invocation.
2020-02-10 05:24:16 -07:00
Robert Manner
36694fce86 plugins/python/python_plugin_common: close can get custom arguments
For the audit plugin.
Ensure we do not fail if plugin_ctx->py_instance is NULL (because
plugin init has failed).
2020-02-10 05:24:16 -07:00
Robert Manner
21c02e1732 plugins/python/sudo_python_module.c: use IntEnums instead of constants
It is a bit more code, but it is more "pythonic" and easier to debug
as the enum values also know their names.

It is also an API break, eg. sudo.RC_OK becomes sudo.RC.OK as sudo.RC will
be the "type" of the enum, but I guess that is acceptable before the
initial release.
2020-02-10 05:24:16 -07:00
Robert Manner
22c64f58c0 plugins/python: use separate python interpreter for each plugin
On each plugin initialization we create a separate python interpreter
which gets stored in the plugin_ctx. The main interpreter is
stored in py_ctx and is used for creating more interpreters (if more plugins
get loaded) and final python deinitialization.

The "traceback" module import and the ImportBlocker initialization was
moved, because it has to happen inside the plugin specific interpreters.
2020-01-24 12:25:15 -07:00
Robert Manner
d67c64bb37 plugins/python/pyhelpers: have a default sudo_printf function
Adapted the default sudo_printf from sudoers plugin to be able to print
errors before plugin open() gets called. (This is used by the multiple io
plugin loading to display error for too much plugin load.)

Since this makes us always have a sudo_log, I have removed the logic about
whether it is available or not.
2020-01-23 12:46:14 -07:00
Robert Manner
185471f263 plugins/python/python_plugin_policy: fix validate() call
When calling validate() python function, TypeError exception was thrown
("argument list must be a tuple"), because the call does not have
arguments, and python does not accept empty tuple for execution. NULL
must be used instead, which was handled as argument construction failure
previously.
2020-01-20 06:30:20 -07:00
Robert Manner
2ee1dd3ec3 plugins/python: fix confusing version display
IO/Group/Policy Python API version is displayed instead of sudo version,
because that is not very meaningful in this context.

They are only displayed in verbose mode.

Example plugins express it more concrete that they are displaying their
version, not the API version.
2020-01-20 06:30:20 -07:00
Robert Manner
5be81eb25b plugins/python/python_plugin_common: raise debug level for module import 2020-01-20 06:30:20 -07:00
Todd C. Miller
486ee2b71f debug_decl and debug_decl_vars now require a semicolon at the end. 2019-12-22 08:48:16 -07:00
Robert Manner
d888d44594 plugins/python: make group plugin able to debug
It does not get the debug settings, so it looks them up through
sudo_conf.
2019-12-14 12:55:42 -07:00
Robert Manner
babdcbd031 plugins/python: a plugin which can load policy/io plugin written in python 2019-12-14 12:55:42 -07:00