Skip to content

Commit

Permalink
libselinux: initialize temp value in SWIG wrapper to prevent freeing …
Browse files Browse the repository at this point in the history
…garbage

Currently this Python program triggers a segmentation fault in
libselinux SWIG wrapper:

    import selinux
    selinux.get_ordered_context_list()

gdb shows that the segmentation fault occurs when freeing some memory:

    Reading symbols from python...(no debugging symbols found)...done.
    Starting program: /usr/bin/python -c import\
    selinux\;selinux.get_ordered_context_list\(\)
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/usr/lib/libthread_db.so.1".

    Program received signal SIGSEGV, Segmentation fault.
    0x00007ffff789a304 in free () from /usr/lib/libc.so.6
    (gdb) bt
    #0  0x00007ffff789a304 in free () from /usr/lib/libc.so.6
    #1  0x00007ffff6011499 in freeconary (con=0x7ffff6ac5d00) at
    freeconary.c:14
    #2  0x00007ffff6296899 in _wrap_get_ordered_context_list
    (self=<optimized out>, args=<optimized out>) at
    selinuxswig_wrap.c:6185
    #3  0x00007ffff741891f in _PyCFunction_FastCallDict () from
    /usr/lib/libpython3.6m.so.1.0
    ...

SWIG generated the following code for _wrap_get_ordered_context_list():

    char ***arg3 = (char ***) 0 ;
    char **temp3 ;
    arg3 = &temp3;
    if (!PyArg_ParseTuple(args, "OO:get_ordered_context_list",&obj0,&obj1))
        SWIG_fail;
    /* ... */
  fail:
    if (*arg3) freeconary(*arg3);

If PyArg_ParseTuple fails, freeconary() is called on the value of
"temp3", which has not been initialized. Fix this by initializing temp
to NULL in the SWIG template.

A similar issue exists with security_get_boolean_names(). Fix it too.

This issue has been found using clang's static analyzer, on a system
which uses SWIG 3.0.12.

Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>
  • Loading branch information
fishilico authored and jwcart2 committed Mar 1, 2017
1 parent 6305bfb commit ded385d
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions libselinux/src/selinuxswig.i
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
%typedef unsigned mode_t;
%typedef unsigned pid_t;

%typemap(in, numinputs=0) (char ***names, int *len) (char **temp1, int temp2) {
%typemap(in, numinputs=0) (char ***names, int *len) (char **temp1=NULL, int temp2) {
$1 = &temp1;
$2 = &temp2;
}
Expand All @@ -33,7 +33,7 @@
}
}

%typemap(in, numinputs=0) (char ***) (char **temp) {
%typemap(in, numinputs=0) (char ***) (char **temp=NULL) {
$1 = &temp;
}

Expand Down

0 comments on commit ded385d

Please sign in to comment.