From: Stephen Smalley <sds@tycho.nsa.gov>
To: Steve Lawrence <slawrence@tresys.com>,
Thomas Hurd <thurd@tresys.com>,
selinux@tycho.nsa.gov
Subject: Re: [PATCH] libsemanage: Change bunzip to use heap instead of stack for buffer.
Date: Wed, 18 Mar 2015 14:33:19 -0400 [thread overview]
Message-ID: <5509C4EF.9040701@tycho.nsa.gov> (raw)
In-Reply-To: <5509C3BE.6010705@tycho.nsa.gov>
On 03/18/2015 02:28 PM, Stephen Smalley wrote:
> On 02/26/2015 11:26 AM, Steve Lawrence wrote:
>> On 02/26/2015 09:03 AM, Thomas Hurd wrote:
>>> Fixes segfault on systems with less than 256K stack size.
>>> After change, I was able to run semodule -l with a 32K stack size.
>>> Additionally, fix potential memory leak on realloc failure.
>>>
>>> Signed-off-by: Thomas Hurd <thurd@tresys.com>
>>
>> Acked-by: Steve Lawrence <slawrence@tresys.com>
>>
>> Thanks!
>
> This broke installing .pp files that are not bzip2'd.
> Previously, one could transparently installed compressed or uncompressed
> .pp files without difficulty.
Before:
/usr/sbin/semodule -i test_policy/test_policy.pp
After:
/usr/sbin/semodule -i test_policy/test_policy.pp
libsemanage.bunzip: Failure reading bz2 archive. (No such file or
directory).
>
>>
>>> ---
>>> libsemanage/src/direct_api.c | 61 ++++++++++++++++++++++++++++++++------------
>>> 1 file changed, 44 insertions(+), 17 deletions(-)
>>>
>>> diff --git a/libsemanage/src/direct_api.c b/libsemanage/src/direct_api.c
>>> index b0ed338..be6bd3c 100644
>>> --- a/libsemanage/src/direct_api.c
>>> +++ b/libsemanage/src/direct_api.c
>>> @@ -428,49 +428,76 @@ static ssize_t bzip(semanage_handle_t *sh, const char *filename, char *data,
>>> * in the file. Returns -1 if file could not be decompressed. */
>>> ssize_t bunzip(semanage_handle_t *sh, FILE *f, char **data)
>>> {
>>> - BZFILE* b;
>>> + BZFILE* b = NULL;
>>> size_t nBuf;
>>> - char buf[1<<18];
>>> - size_t size = sizeof(buf);
>>> + char* buf = NULL;
>>> + size_t size = 1<<18;
>>> + size_t bufsize = size;
>>> int bzerror;
>>> size_t total=0;
>>> + char* uncompress = NULL;
>>> + char* tmpalloc = NULL;
>>> + int ret = -1;
>>> +
>>> + buf = malloc(bufsize);
>>> + if (buf == NULL) {
>>> + ERR(sh, "Failure allocating memory.");
>>> + goto exit;
>>> + }
>>>
>>> if (!sh->conf->bzip_blocksize) {
>>> bzerror = fread(buf, 1, BZ2_MAGICLEN, f);
>>> rewind(f);
>>> - if ((bzerror != BZ2_MAGICLEN) || memcmp(buf, BZ2_MAGICSTR, BZ2_MAGICLEN))
>>> - return -1;
>>> + if ((bzerror != BZ2_MAGICLEN) || memcmp(buf, BZ2_MAGICSTR, BZ2_MAGICLEN)) {
>>> + ERR(sh, "bz2 magic number not found.");
>>> + goto exit;
>>> + }
>>> /* fall through */
>>> }
>>>
>>> b = BZ2_bzReadOpen ( &bzerror, f, 0, sh->conf->bzip_small, NULL, 0 );
>>> if ( bzerror != BZ_OK ) {
>>> - BZ2_bzReadClose ( &bzerror, b );
>>> - return -1;
>>> + ERR(sh, "Failure opening bz2 archive.");
>>> + goto exit;
>>> }
>>> -
>>> - char *uncompress = realloc(NULL, size);
>>> -
>>> +
>>> + uncompress = malloc(size);
>>> + if (uncompress == NULL) {
>>> + ERR(sh, "Failure allocating memory.");
>>> + goto exit;
>>> + }
>>> +
>>> while ( bzerror == BZ_OK) {
>>> - nBuf = BZ2_bzRead ( &bzerror, b, buf, sizeof(buf));
>>> + nBuf = BZ2_bzRead ( &bzerror, b, buf, bufsize);
>>> if (( bzerror == BZ_OK ) || ( bzerror == BZ_STREAM_END )) {
>>> if (total + nBuf > size) {
>>> size *= 2;
>>> - uncompress = realloc(uncompress, size);
>>> + tmpalloc = realloc(uncompress, size);
>>> + if (tmpalloc == NULL) {
>>> + ERR(sh, "Failure allocating memory.");
>>> + goto exit;
>>> + }
>>> + uncompress = tmpalloc;
>>> }
>>> memcpy(&uncompress[total], buf, nBuf);
>>> total += nBuf;
>>> }
>>> }
>>> if ( bzerror != BZ_STREAM_END ) {
>>> - BZ2_bzReadClose ( &bzerror, b );
>>> - free(uncompress);
>>> - return -1;
>>> + ERR(sh, "Failure reading bz2 archive.");
>>> + goto exit;
>>> }
>>> - BZ2_bzReadClose ( &bzerror, b );
>>>
>>> + ret = total;
>>> *data = uncompress;
>>> - return total;
>>> +
>>> +exit:
>>> + BZ2_bzReadClose ( &bzerror, b );
>>> + free(buf);
>>> + if ( ret < 0 ) {
>>> + free(uncompress);
>>> + }
>>> + return ret;
>>> }
>>>
>>> /* mmap() a file to '*data',
>>>
>>
>> _______________________________________________
>> Selinux mailing list
>> Selinux@tycho.nsa.gov
>> To unsubscribe, send email to Selinux-leave@tycho.nsa.gov.
>> To get help, send an email containing "help" to Selinux-request@tycho.nsa.gov.
>>
>>
>
next prev parent reply other threads:[~2015-03-18 18:33 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-02-26 14:03 [PATCH] libsemanage: Change bunzip to use heap instead of stack for buffer Thomas Hurd
2015-02-26 16:26 ` Steve Lawrence
2015-03-18 18:28 ` Stephen Smalley
2015-03-18 18:33 ` Stephen Smalley [this message]
2015-03-19 14:59 ` Thomas Hurd
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5509C4EF.9040701@tycho.nsa.gov \
--to=sds@tycho.nsa.gov \
--cc=selinux@tycho.nsa.gov \
--cc=slawrence@tresys.com \
--cc=thurd@tresys.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.