Skip to content

Commit 0e8548c

Browse files
committed
Issue #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 0e14ee2 commit 0e8548c

4 files changed

Lines changed: 50 additions & 7 deletions

File tree

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:`__builtin__`, :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

@@ -338,7 +339,7 @@ Initialization, Finalization, and Threads
338339
``sys.version``.
339340

340341

341-
.. cfunction:: void PySys_SetArgv(int argc, char **argv)
342+
.. cfunction:: void PySys_SetArgvEx(int argc, char **argv, int updatepath)
342343

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

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

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

366394
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
@@ -11,6 +11,7 @@ PyAPI_FUNC(PyObject *) PySys_GetObject(char *);
1111
PyAPI_FUNC(int) PySys_SetObject(char *, PyObject *);
1212
PyAPI_FUNC(FILE *) PySys_GetFile(char *, FILE *);
1313
PyAPI_FUNC(void) PySys_SetArgv(int, char **);
14+
PyAPI_FUNC(void) PySys_SetArgvEx(int, char **, int);
1415
PyAPI_FUNC(void) PySys_SetPath(char *);
1516

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

Misc/NEWS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ Core and Builtins
1818
- Issue #7079: Fix a possible crash when closing a file object while using
1919
it from another thread. Patch by Daniel Stutzbach.
2020

21+
C-API
22+
-----
23+
24+
- Issue #5753: A new C API function, :cfunc:`PySys_SetArgvEx`, allows
25+
embedders of the interpreter to set sys.argv without also modifying
26+
sys.path. This helps fix `CVE-2008-5983
27+
<http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983>`_.
28+
2129
Library
2230
-------
2331

Python/sysmodule.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,7 +1649,7 @@ makeargvobject(int argc, char **argv)
16491649
}
16501650

16511651
void
1652-
PySys_SetArgv(int argc, char **argv)
1652+
PySys_SetArgvEx(int argc, char **argv, int updatepath)
16531653
{
16541654
#if defined(HAVE_REALPATH)
16551655
char fullpath[MAXPATHLEN];
@@ -1662,7 +1662,7 @@ PySys_SetArgv(int argc, char **argv)
16621662
Py_FatalError("no mem for sys.argv");
16631663
if (PySys_SetObject("argv", av) != 0)
16641664
Py_FatalError("can't assign sys.argv");
1665-
if (path != NULL) {
1665+
if (updatepath && path != NULL) {
16661666
char *argv0 = argv[0];
16671667
char *p = NULL;
16681668
Py_ssize_t n = 0;
@@ -1752,6 +1752,12 @@ PySys_SetArgv(int argc, char **argv)
17521752
Py_DECREF(av);
17531753
}
17541754

1755+
void
1756+
PySys_SetArgv(int argc, char **argv)
1757+
{
1758+
PySys_SetArgvEx(argc, argv, 1);
1759+
}
1760+
17551761

17561762
/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
17571763
Adapted from code submitted by Just van Rossum.

0 commit comments

Comments
 (0)