libsepol: destroy filename_trans list properly

OSS-Fuzz found a direct memory leak in policydb_filetrans_insert()
because filenametr_destroy() does not fully destroy the list associated
with a typetransition.

More precisely, let's consider this (minimized) CIL policy:

    (class CLASS (PERM))
    (classorder (CLASS))
    (sid SID)
    (sidorder (SID))
    (user USER)
    (role ROLE)
    (type TYPE) ; "type 1" in libsepol internal structures
    (type TYPE2) ; "type 2" in libsepol internal structures
    (type TYPE3) ; "type 3" in libsepol internal structures
    (category CAT)
    (categoryorder (CAT))
    (sensitivity SENS)
    (sensitivityorder (SENS))
    (sensitivitycategory SENS (CAT))
    (allow TYPE self (CLASS (PERM)))
    (roletype ROLE TYPE)
    (userrole USER ROLE)
    (userlevel USER (SENS))
    (userrange USER ((SENS)(SENS (CAT))))
    (sidcontext SID (USER ROLE TYPE ((SENS)(SENS))))

    (typetransition TYPE2 TYPE CLASS "some_file" TYPE2)
    (typetransition TYPE3 TYPE CLASS "some_file" TYPE3)

The two typetransition statements make policydb_filetrans_insert()
insert an item with key {ttype=1, tclass=1, name="some_file"} in the
hashmap p->filename_trans. This item contains a linked list of two
filename_trans_datum_t elements:

* The first one uses {otype=2, stypes=bitmap containing 2}
* The second one uses {otype=3, stypes=bitmap containing 3}

Nevertheless filenametr_destroy() (called by
hashtab_map(p->filename_trans, filenametr_destroy, NULL);) only frees
the first element. Fix this memory leak by freeing all elements.

This issue was introduced by commit 42ae834a7428 ("libsepol,checkpolicy:
optimize storage of filename transitions") and was never present in the
kernel, as filenametr_destroy() was modified appropriately in commit
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c3a276111ea2572399281988b3129683e2a6b60b

Fixes: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=29138
Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>
Acked-by: Ondrej Mosnacek <omosnace@redhat.com>
1 file changed
tree: e56e42c0b978e4b3cc6e5a7197084afe466bc8c1
  1. .circleci/
  2. .github/
  3. checkpolicy/
  4. dbus/
  5. gui/
  6. libselinux/
  7. libsemanage/
  8. libsepol/
  9. mcstrans/
  10. policycoreutils/
  11. python/
  12. restorecond/
  13. sandbox/
  14. scripts/
  15. secilc/
  16. semodule-utils/
  17. .gitignore
  18. .travis.yml
  19. CleanSpec.mk
  20. CONTRIBUTING.md
  21. lgtm.yml
  22. Makefile
  23. README.md
  24. VERSION
README.md

SELinux Userspace

SELinux logo Build Status

Please submit all bug reports and patches to selinux@vger.kernel.org.

Subscribe by sending “subscribe selinux” in the body of an email to majordomo@vger.kernel.org.

Archive of this mailing list is available on https://lore.kernel.org/selinux/.

Installation

SELinux libraries and tools are packaged in several Linux distributions:

Building and testing

Build dependencies on Fedora:

# For C libraries and programs
dnf install \
    audit-libs-devel \
    bison \
    bzip2-devel \
    CUnit-devel \
    diffutils \
    flex \
    gcc \
    gettext \
    glib2-devel \
    make \
    libcap-devel \
    libcap-ng-devel \
    pam-devel \
    pcre-devel \
    xmlto

# For Python and Ruby bindings
dnf install \
    python3-devel \
    ruby-devel \
    swig

Build dependencies on Debian:

# For C libraries and programs
apt-get install --no-install-recommends --no-install-suggests \
    bison \
    flex \
    gawk \
    gcc \
    gettext \
    make \
    libaudit-dev \
    libbz2-dev \
    libcap-dev \
    libcap-ng-dev \
    libcunit1-dev \
    libglib2.0-dev \
    libpcre3-dev \
    pkgconf \
    python3 \
    python3-distutils \
    systemd \
    xmlto

# For Python and Ruby bindings
apt-get install --no-install-recommends --no-install-suggests \
    python3-dev \
    ruby-dev \
    swig

To build and install everything under a private directory, run:

make clean distclean

make DESTDIR=~/obj install install-rubywrap install-pywrap

On Debian PYTHON_SETUP_ARGS=--install-layout=deb needs to be set when installing the python wrappers in order to create the correct python directory structure.

To run tests with the built libraries and programs, several paths (relative to $DESTDIR) need to be added to variables $LD_LIBRARY_PATH, $PATH and $PYTHONPATH. This can be done using ./scripts/env_use_destdir:

DESTDIR=~/obj ./scripts/env_use_destdir make test

Some tests require the reference policy to be installed (for example in python/sepolgen). In order to run these ones, instructions similar to the ones in section install of ./.travis.yml can be executed.

To install as the default system libraries and binaries (overwriting any previously installed ones - dangerous!), on x86_64, run:

make LIBDIR=/usr/lib64 SHLIBDIR=/lib64 install install-pywrap relabel

or on x86 (32-bit), run:

make install install-pywrap relabel

This may render your system unusable if the upstream SELinux userspace lacks library functions or other dependencies relied upon by your distribution. If it breaks, you get to keep both pieces.

Setting CFLAGS

Setting CFLAGS during the make process will cause the omission of many defaults. While the project strives to provide a reasonable set of default flags, custom CFLAGS could break the build, or have other undesired changes on the build output. Thus, be very careful when setting CFLAGS. CFLAGS that are encouraged to be set when overriding are:

  • -fno-semantic-interposition for gcc or compilers that do not do this. clang does this by default. clang-10 and up will support passing this flag, but ignore it. Previous clang versions fail.

macOS

To install libsepol on macOS (mainly for policy analysis):

cd libsepol; make PREFIX=/usr/local install

This requires GNU coreutils:

brew install coreutils