From patchwork Wed Feb 6 19:45:29 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Lautrbach X-Patchwork-Id: 10799991 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6C61C6C2 for ; Wed, 6 Feb 2019 19:45:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5DBF32CC02 for ; Wed, 6 Feb 2019 19:45:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 529262CC2C; Wed, 6 Feb 2019 19:45:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C143D2CC06 for ; Wed, 6 Feb 2019 19:45:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726401AbfBFTpd (ORCPT ); Wed, 6 Feb 2019 14:45:33 -0500 Received: from mx1.redhat.com ([209.132.183.28]:55074 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726022AbfBFTpd (ORCPT ); Wed, 6 Feb 2019 14:45:33 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 428D77E9D8 for ; Wed, 6 Feb 2019 19:45:33 +0000 (UTC) Received: from workstation.redhat.com (ovpn-204-32.brq.redhat.com [10.40.204.32]) by smtp.corp.redhat.com (Postfix) with ESMTP id 622D445CD; Wed, 6 Feb 2019 19:45:32 +0000 (UTC) From: Petr Lautrbach To: selinux@vger.kernel.org Cc: Petr Lautrbach Subject: [PATCH] libsemanage: genhomedircon - improve handling large groups Date: Wed, 6 Feb 2019 20:45:29 +0100 Message-Id: <20190206194529.24952-1-plautrba@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Wed, 06 Feb 2019 19:45:33 +0000 (UTC) Sender: selinux-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP getgrnam_r() uses a preallocated buffer to store a structure containing the broken-out fields of the record in the group database. The size of this buffer is usually sysconf(_SC_GETGR_R_SIZE_MAX) == 1024 and it is not enough for groups with a large number of users. In these cases, getgrnam_r() returns -1 and sets errno to ERANGE and the caller can retry with a larger buffer. Fixes: $ semanage login -a -s user_u -r s0-s0:c1.c2 '%largegroup' libsemanage.semanage_direct_commit: semanage_genhomedircon returned error code -1. (Numerical result out of range). OSError: Numerical result out of range Signed-off-by: Petr Lautrbach Acked-by: Nicolas Iooss --- libsemanage/src/genhomedircon.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/libsemanage/src/genhomedircon.c b/libsemanage/src/genhomedircon.c index 591941fb..ac376671 100644 --- a/libsemanage/src/genhomedircon.c +++ b/libsemanage/src/genhomedircon.c @@ -1077,10 +1077,20 @@ static int get_group_users(genhomedircon_settings_t * s, const char *grname = selogin + 1; - if (getgrnam_r(grname, &grstorage, grbuf, - (size_t) grbuflen, &group) != 0) { - goto cleanup; + errno = 0; + while ( + (retval = getgrnam_r(grname, &grstorage, grbuf, (size_t) grbuflen, &group)) != 0 && + errno == ERANGE + ) { + char *new_grbuf; + grbuflen *= 2; + new_grbuf = realloc(grbuf, grbuflen); + if (new_grbuf == NULL) + goto cleanup; + grbuf = new_grbuf; } + if (retval == -1) + goto cleanup; if (group == NULL) { ERR(s->h_semanage, "Can't find group named %s\n", grname);