Skip to content

Commit f978fac

Browse files
committed
Merged revisions 81398 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r81398 | antoine.pitrou | 2010-05-21 19:12:38 +0200 (ven., 21 mai 2010) | 6 lines Issue python#5753: A new C API function, :cfunc:`PySys_SetArgvEx`, allows embedders of the interpreter to set sys.argv without also modifying sys.path. This helps fix `CVE-2008-5983 <http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983>`_. ........
1 parent cc6a982 commit f978fac

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

Doc/c-api/init.rst

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Initialization, Finalization, and Threads
2222
module: sys
2323
triple: module; search; path
2424
single: PySys_SetArgv()
25+
single: PySys_SetArgvEx()
2526
single: Py_Finalize()
2627

2728
Initialize the Python interpreter. In an application embedding Python, this
@@ -31,7 +32,7 @@ Initialization, Finalization, and Threads
3132
the table of loaded modules (``sys.modules``), and creates the fundamental
3233
modules :mod:`builtins`, :mod:`__main__` and :mod:`sys`. It also initializes
3334
the module search path (``sys.path``). It does not set ``sys.argv``; use
34-
:cfunc:`PySys_SetArgv` for that. This is a no-op when called for a second time
35+
:cfunc:`PySys_SetArgvEx` for that. This is a no-op when called for a second time
3536
(without calling :cfunc:`Py_Finalize` first). There is no return value; it is a
3637
fatal error if the initialization fails.
3738

@@ -337,7 +338,7 @@ Initialization, Finalization, and Threads
337338
``sys.version``.
338339

339340

340-
.. cfunction:: void PySys_SetArgv(int argc, wchar_t **argv)
341+
.. cfunction:: void PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
341342

342343
.. index::
343344
single: main()
@@ -352,14 +353,41 @@ Initialization, Finalization, and Threads
352353
string. If this function fails to initialize :data:`sys.argv`, a fatal
353354
condition is signalled using :cfunc:`Py_FatalError`.
354355

355-
This function also prepends the executed script's path to :data:`sys.path`.
356-
If no script is executed (in the case of calling ``python -c`` or just the
357-
interactive interpreter), the empty string is used instead.
356+
If *updatepath* is zero, this is all the function does. If *updatepath*
357+
is non-zero, the function also modifies :data:`sys.path` according to the
358+
following algorithm:
359+
360+
- If the name of an existing script is passed in ``argv[0]``, the absolute
361+
path of the directory where the script is located is prepended to
362+
:data:`sys.path`.
363+
- Otherwise (that is, if *argc* is 0 or ``argv[0]`` doesn't point
364+
to an existing file name), an empty string is prepended to
365+
:data:`sys.path`, which is the same as prepending the current working
366+
directory (``"."``).
367+
368+
.. note::
369+
It is recommended that applications embedding the Python interpreter
370+
for purposes other than executing a single script pass 0 as *updatepath*,
371+
and update :data:`sys.path` themselves if desired.
372+
See `CVE-2008-5983 <http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983>`_.
373+
374+
On versions before 3.1.3, you can achieve the same effect by manually
375+
popping the first :data:`sys.path` element after having called
376+
:cfunc:`PySys_SetArgv`, for example using::
377+
378+
PyRun_SimpleString("import sys; sys.path.pop(0)\n");
379+
380+
.. versionadded:: 3.1.3
358381

359382
.. XXX impl. doesn't seem consistent in allowing 0/NULL for the params;
360383
check w/ Guido.
361384
362385
386+
.. cfunction:: void PySys_SetArgv(int argc, wchar_t **argv)
387+
388+
This function works like :cfunc:`PySys_SetArgv` with *updatepath* set to 1.
389+
390+
363391
.. cfunction:: void Py_SetPythonHome(wchar_t *home)
364392

365393
Set the default "home" directory, that is, the location of the standard

Include/sysmodule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern "C" {
1010
PyAPI_FUNC(PyObject *) PySys_GetObject(const char *);
1111
PyAPI_FUNC(int) PySys_SetObject(const char *, PyObject *);
1212
PyAPI_FUNC(void) PySys_SetArgv(int, wchar_t **);
13+
PyAPI_FUNC(void) PySys_SetArgvEx(int, wchar_t **, int);
1314
PyAPI_FUNC(void) PySys_SetPath(const wchar_t *);
1415

1516
PyAPI_FUNC(void) PySys_WriteStdout(const char *format, ...)

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,11 @@ Core and Builtins
334334
C-API
335335
-----
336336

337+
- Issue #5753: A new C API function, :cfunc:`PySys_SetArgvEx`, allows
338+
embedders of the interpreter to set sys.argv without also modifying
339+
sys.path. This helps fix `CVE-2008-5983
340+
<http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983>`_.
341+
337342
- Add PyArg_ValidateKeywordArguments, which checks if all keyword arguments are
338343
strings in an efficient manner.
339344

Python/sysmodule.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,7 @@ _wrealpath(const wchar_t *path, wchar_t *resolved_path)
16681668
#endif
16691669

16701670
void
1671-
PySys_SetArgv(int argc, wchar_t **argv)
1671+
PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
16721672
{
16731673
#if defined(HAVE_REALPATH)
16741674
wchar_t fullpath[MAXPATHLEN];
@@ -1681,7 +1681,7 @@ PySys_SetArgv(int argc, wchar_t **argv)
16811681
Py_FatalError("no mem for sys.argv");
16821682
if (PySys_SetObject("argv", av) != 0)
16831683
Py_FatalError("can't assign sys.argv");
1684-
if (path != NULL) {
1684+
if (updatepath && path != NULL) {
16851685
wchar_t *argv0 = argv[0];
16861686
wchar_t *p = NULL;
16871687
Py_ssize_t n = 0;
@@ -1768,6 +1768,12 @@ PySys_SetArgv(int argc, wchar_t **argv)
17681768
Py_DECREF(av);
17691769
}
17701770

1771+
void
1772+
PySys_SetArgv(int argc, wchar_t **argv)
1773+
{
1774+
PySys_SetArgvEx(argc, argv, 1);
1775+
}
1776+
17711777
/* Reimplementation of PyFile_WriteString() no calling indirectly
17721778
PyErr_CheckSignals(): avoid the call to PyObject_Str(). */
17731779

0 commit comments

Comments
 (0)