Re: [PATCH] libsemanage: treat module data as bytes in the Python bindings

Stephen Smalley <[email protected]>
Newsgroups org.kernel.vger.selinux
Message-ID <CAEjxPJ4zPXxsvVYPiKL+4q85my7f06HjD44CShs0waivAwUziw@mail.gmail.com>
On Mon, Aug 17, 2026 at 10:35 AM James Carter <[email protected]> wrote:
>
> On Thu, Aug 13, 2026 at 2:41 PM Stephen Smalley
> <[email protected]> wrote:
> >
> > semanage_module_install() / semanage_module_install_info() take a
> > (char *data, size_t data_len) pair, and semanage_module_extract()
> > returns (void **mapped_data, size_t *data_len). In both directions
> > the payload is an opaque module blob (.pp / bzip2 / CIL / HLL), not a
> > NUL-terminated C string.
> >
> > On Python 3 the current SWIG bindings get this wrong on both sides:
> >
> > - install()/install_info(): the SWIG default for a bare "char *"
> >   only accepts str objects and UTF-8-encodes them, so a .pp blob
> >   cannot be passed at all, and even for CIL text the separately
> >   supplied data_len is wrong for any non-ASCII content since
> >   Python len(str) counts code points, not UTF-8 bytes.
> >
> > - extract(): %cstring_output_allocate_size turns the mapped buffer
> >   into a str via UTF-8 decoding, which for a binary module either
> >   raises UnicodeDecodeError or (with surrogateescape) yields a str
> >   that cannot be fed straight back into install().
> >
> > Add a (char *module_data, size_t data_len) input typemap that
> > accepts any object exposing the buffer protocol (bytes, bytearray,
> > mmap, ...) as a single argument and derives the length from it, and
> > apply it to the (char *data, size_t data_len) pair of
> > semanage_module_install_info() as well.
> >
> > Replace the extract() output typemap with an explicit multi-argument
> > typemap that returns a bytes object built from the mapped buffer and
> > then unmaps it, so extract() -> install() round-trips.  Drop the
> > now-unused <cstring.i> include.
> >
> > The Python signatures become
> >   semanage_module_install(sh, data, name, ext_lang)
> >   semanage_module_install_info(sh, modinfo, data)
> > There are no in-tree Python callers.
> >
> > Fixes: https://github.com/SELinuxProject/selinux/issues/502
> > Signed-off-by: Stephen Smalley <[email protected]>
>
> Acked-by: James Carter <[email protected]>

Thanks, merged.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.