From patchwork Tue Oct 18 21:31:51 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Cashman X-Patchwork-Id: 9383081 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 18D4D60487 for ; Tue, 18 Oct 2016 21:33:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0C24329690 for ; Tue, 18 Oct 2016 21:33:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F39EA296D6; Tue, 18 Oct 2016 21:33:24 +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=-1.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_NONE,T_DKIM_INVALID autolearn=no version=3.3.1 Received: from emsm-gh1-uea10.nsa.gov (emsm-gh1-uea10.nsa.gov [8.44.101.8]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 415C829690 for ; Tue, 18 Oct 2016 21:33:23 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.31,511,1473120000"; d="scan'208";a="7409" IronPort-PHdr: =?us-ascii?q?9a23=3AXBolMxKHgM1kFRdFXdmcpTZWNBhigK39O0sv0rFi?= =?us-ascii?q?tYgUL/3xwZ3uMQTl6Ol3ixeRBMOAuqgC1red6/yocFdDyK7JiGoFfp1IWk1Nou?= =?us-ascii?q?QttCtkPvS4D1bmJuXhdS0wEZcKflZk+3amLRodQ56mNBXsq3G/pQQfBg/4fVIs?= =?us-ascii?q?YL+kQMiP0o/vi6ibwN76W01wnj2zYLd/fl2djD76kY0ou7ZkMbs70RDTo3FFKK?= =?us-ascii?q?x8zGJsIk+PzV6nvp/jtKN592xsn95pt4sZCeSpN5g/GKdVCDUgLnAd+NzgtR6F?= =?us-ascii?q?SxCGoHQbTDY4iB1NViTM6hD3Wt/auzD6s+N7kH2WNsTtTbU9Xxyr6atxTx7kzi?= =?us-ascii?q?wAMmhqoynslsVsgfcD81qarBtlztuROdmY?= X-IPAS-Result: =?us-ascii?q?A2FJBQB4lAZY/wHyM5BcHAEBBAEBCgEBFwEBBAEBCgEBgxE?= =?us-ascii?q?BAQEBAR2BU7pyI4gMTAEBAQEBAQEBAgECXyeCMgQDEwUFORBVAg1hAQQCDxUTB?= =?us-ascii?q?gEBDCALAQIDCQIXCCEICAMBLQMBBQELHwsFGAQBiDCof4EyPjKKW4UwAQEFiDg?= =?us-ascii?q?dCRCEG4IICohuEQGFe489ilCQCIlbJYVzAkmOcDGBEVRGBgiCdw8cgXNShXl4g?= =?us-ascii?q?SgBAQE?= Received: from unknown (HELO tarius.tycho.ncsc.mil) ([144.51.242.1]) by emsm-gh1-uea10.nsa.gov with ESMTP; 18 Oct 2016 21:33:21 +0000 Received: from prometheus.infosec.tycho.ncsc.mil (prometheus [192.168.25.40]) by tarius.tycho.ncsc.mil (8.14.4/8.14.4) with ESMTP id u9ILWXgL005994; Tue, 18 Oct 2016 17:32:46 -0400 Received: from tarius.tycho.ncsc.mil (tarius.infosec.tycho.ncsc.mil [144.51.242.1]) by prometheus.infosec.tycho.ncsc.mil (8.15.2/8.15.2) with ESMTP id u9ILWV2g190271 for ; Tue, 18 Oct 2016 17:32:31 -0400 Received: from goalie.tycho.ncsc.mil (goalie [144.51.242.250]) by tarius.tycho.ncsc.mil (8.14.4/8.14.4) with ESMTP id u9ILWUdj005981 for ; Tue, 18 Oct 2016 17:32:30 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A1B4AACwlAZYhqnAVdFcHQEFAQsBgzwBAQEBAYFwtmKEF4YhggdMAQIBAQEBAQITAQEBCAsLCR2FEwEVFRkBATcBL2UBBQE1IogwqHyBMj4yiluFMAEBBYgsASgJEIQbgggKi2gLgwiPPYpQkAiJW4YYAkmOcDGBEYEaBgiCdw8RC4FzUogZAQEB X-IPAS-Result: A1B4AACwlAZYhqnAVdFcHQEFAQsBgzwBAQEBAYFwtmKEF4YhggdMAQIBAQEBAQITAQEBCAsLCR2FEwEVFRkBATcBL2UBBQE1IogwqHyBMj4yiluFMAEBBYgsASgJEIQbgggKi2gLgwiPPYpQkAiJW4YYAkmOcDGBEYEaBgiCdw8RC4FzUogZAQEB X-IronPort-AV: E=Sophos;i="5.31,511,1473134400"; d="scan'208";a="5770261" Received: from emsm-gh1-uea11.corp.nsa.gov (HELO emsm-gh1-uea11.nsa.gov) ([10.208.41.37]) by goalie.tycho.ncsc.mil with ESMTP; 18 Oct 2016 17:32:07 -0400 IronPort-PHdr: =?us-ascii?q?9a23=3A0+Bm/REyDhMjf+TNMgMvjp1GYnF86YWxBRYc798d?= =?us-ascii?q?s5kLTJ75pMqwAkXT6L1XgUPTWs2DsrQf2rCQ7v2rATBIyK3CmUhKSIZLWR4BhJ?= =?us-ascii?q?detC0bK+nBN3fGKuX3ZTcxBsVIWQwt1Xi6NU9IBJS2PAWK8TWM5DIfUi/yKRBy?= =?us-ascii?q?brysXNWD1YLqiqvqp9X6WEZhvHKFe7R8LRG7/036l/I9ps9cEJs30QbDuXBSeu?= =?us-ascii?q?5blitCLFOXmAvgtI/rpMYwu3cYh/V07MNEUKPnb4wkXLdYC3IgKGlz68r15jfZ?= =?us-ascii?q?Sg7aynIaU2gS2jFBGAjA5ReyCpv6uzD6s+N78CCcMdfxTLxyUjOnufQ4ACT0gT?= =?us-ascii?q?sKYmZquFrcjdZ92fpW?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0H3AAAxlAZYhqnAVdFcHQEFAQsBGAEFA?= =?us-ascii?q?QsBgxEBAQEBAYFwtmKEF4YhggdMAQEBAQEBAQECAQIQAQEBCAsLCR0vgjIYDDk?= =?us-ascii?q?QVQINYQEVFRkBATcBL2UBBQE1IogwqH+BMj4yiluFMAEBBYgsASgJEIQbgggKi?= =?us-ascii?q?2gLgwiPPYpQkAiJW4YYAkmOcDGBEYEaBgiCdw8RC4FzUogZAQEB?= X-IPAS-Result: =?us-ascii?q?A0H3AAAxlAZYhqnAVdFcHQEFAQsBGAEFAQsBgxEBAQEBAYF?= =?us-ascii?q?wtmKEF4YhggdMAQEBAQEBAQECAQIQAQEBCAsLCR0vgjIYDDkQVQINYQEVFRkBA?= =?us-ascii?q?TcBL2UBBQE1IogwqH+BMj4yiluFMAEBBYgsASgJEIQbgggKi2gLgwiPPYpQkAi?= =?us-ascii?q?JW4YYAkmOcDGBEYEaBgiCdw8RC4FzUogZAQEB?= X-IronPort-AV: E=Sophos;i="5.31,511,1473120000"; d="scan'208";a="24579" Received: from mail-pf0-f169.google.com ([209.85.192.169]) by emsm-gh1-uea11.nsa.gov with ESMTP/TLS/AES128-GCM-SHA256; 18 Oct 2016 21:32:04 +0000 Received: by mail-pf0-f169.google.com with SMTP id 128so2980021pfz.0 for ; Tue, 18 Oct 2016 14:32:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20120917; h=from:to:cc:subject:date:message-id; bh=4XSgn7kA6iaGEfOSiaJWl/ukr4RZpcUt9OPVPyXNdow=; b=KhbYvHFlKWjrxMRfQAWT8UQH/pIzij3aXFm7AvSLtSNp+5aRNp0lLf9V87jIBVPV0F 7pt55S/ceNcZBptkUxjWBb81jQLeT+HIKDN7bbBxDnsPeCeLRPayCL1TkE+dmMUulTPA n4fCiuSvkIduNSgygh50TZXQPXmzn0s2o8iCAxLb0eN94XgaEvYhhe5ko8OaTlhRtGu1 D1+lY75b1oZnfyJV/2gZSBmPf9JecHmjEAh0w1bdKOiaNZ0wc1HZ8oyslP/bZVGDGxoU wc2arYWdVU+56ACd6hi9LPWjWXK5oSqCQvLDWnMnQfH5SiTcc8FoeNhRtd5BdBi9tG4P pP9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=4XSgn7kA6iaGEfOSiaJWl/ukr4RZpcUt9OPVPyXNdow=; b=gD7Kb8q3JvT/h5dG+ysVMFELVfI93CpmjU12KfrqFId9uGAktUjTDXThTplzIOAabg BL/e53sxXziXouiZDlId+7JknEA9fmM2cXmQFyQQV1MsA3DLggSVosabLESzklMBDqki h8tenWesGPPIAYYravOhTnHH/Dbi1lpjFcBlbiQtSt8Ejz6hJGzTIl845VOU8PtTxV2K mOF9OUZKSGABFg8q/kz/cgg96issnRK+fPuFvTmc4n/Oh3BnOoT5SLKsjLEJO2x2eZ/C cwGaC8nfUSDip8CwydpEaekLyazu0bevOcH+xftigXkgDTFQqHNjpPBjCKw/RyoSnzQq kL1A== X-Gm-Message-State: AA6/9Rlt2Sv1htnzauq4/0lfPMw9kKJhxLZ2UiCuMxlvPJUGR3WhQuZiUsKBr7Hvb2i1Sw== X-Received: by 10.99.136.67 with SMTP id l64mr3465302pgd.115.1476826322938; Tue, 18 Oct 2016 14:32:02 -0700 (PDT) Received: from dcashman.mtv.corp.google.com ([100.98.112.101]) by smtp.gmail.com with ESMTPSA id f129sm7777143pfc.3.2016.10.18.14.32.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 18 Oct 2016 14:32:02 -0700 (PDT) From: Daniel Cashman To: selinux@tycho.nsa.gov Subject: [PATCH] libsepol: cil: cil_strpool: Allow multiple strpool users. Date: Tue, 18 Oct 2016 14:31:51 -0700 Message-Id: <1476826311-31992-1-git-send-email-dcashman@android.com> X-Mailer: git-send-email 2.8.0.rc3.226.g39d4020 X-BeenThere: selinux@tycho.nsa.gov X-Mailman-Version: 2.1.20 Precedence: list List-Id: "Security-Enhanced Linux \(SELinux\) mailing list" List-Post: List-Help: Cc: sds@tycho.nsa.gov MIME-Version: 1.0 Errors-To: selinux-bounces@tycho.nsa.gov Sender: "Selinux" X-Virus-Scanned: ClamAV using ClamSMTP From: dcashman cil_strpool currently provides an interface to a statically stored global data structure. This interface does not accomodate multiple consumers, however, as two calls to cil_strpool_init() will lead to a memory leak and a call to cil_strpool_destroy() by one consumer will remove data from use by others, and subsequently lead to a segfault on the next cil_strpool_destroy() invocation. Add a reference counter so that the strpool is only initialized once and protect the exported interface with a mutex. Tested by calling cil_db_init() on two cil_dbs and then calling cil_db_destroy() on each. Signed-off-by: Daniel Cashman --- libsepol/cil/src/cil_strpool.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/libsepol/cil/src/cil_strpool.c b/libsepol/cil/src/cil_strpool.c index ad2a334..5b7df8c 100644 --- a/libsepol/cil/src/cil_strpool.c +++ b/libsepol/cil/src/cil_strpool.c @@ -27,6 +27,7 @@ * either expressed or implied, of Tresys Technology, LLC. */ +#include #include #include #include @@ -40,6 +41,8 @@ struct cil_strpool_entry { char *str; }; +static pthread_mutex_t cil_strpool_mutex = PTHREAD_MUTEX_INITIALIZER; +static unsigned int cil_strpool_readers = 0; static hashtab_t cil_strpool_tab = NULL; static unsigned int cil_strpool_hash(hashtab_t h, hashtab_key_t key) @@ -68,16 +71,21 @@ char *cil_strpool_add(const char *str) { struct cil_strpool_entry *strpool_ref = NULL; + pthread_mutex_lock(&cil_strpool_mutex); + strpool_ref = hashtab_search(cil_strpool_tab, (hashtab_key_t)str); if (strpool_ref == NULL) { strpool_ref = cil_malloc(sizeof(*strpool_ref)); strpool_ref->str = cil_strdup(str); int rc = hashtab_insert(cil_strpool_tab, (hashtab_key_t)strpool_ref->str, strpool_ref); if (rc != SEPOL_OK) { + pthread_mutex_unlock(&cil_strpool_mutex); (*cil_mem_error_handler)(); + pthread_mutex_lock(&cil_strpool_mutex); } } + pthread_mutex_unlock(&cil_strpool_mutex); return strpool_ref->str; } @@ -91,14 +99,26 @@ static int cil_strpool_entry_destroy(hashtab_key_t k __attribute__ ((unused)), h void cil_strpool_init(void) { - cil_strpool_tab = hashtab_create(cil_strpool_hash, cil_strpool_compare, CIL_STRPOOL_TABLE_SIZE); + pthread_mutex_lock(&cil_strpool_mutex); if (cil_strpool_tab == NULL) { - (*cil_mem_error_handler)(); + cil_strpool_tab = hashtab_create(cil_strpool_hash, cil_strpool_compare, CIL_STRPOOL_TABLE_SIZE); + if (cil_strpool_tab == NULL) { + pthread_mutex_unlock(&cil_strpool_mutex); + (*cil_mem_error_handler)(); + return; + } } + cil_strpool_readers++; + pthread_mutex_unlock(&cil_strpool_mutex); } void cil_strpool_destroy(void) { - hashtab_map(cil_strpool_tab, cil_strpool_entry_destroy, NULL); - hashtab_destroy(cil_strpool_tab); + pthread_mutex_lock(&cil_strpool_mutex); + cil_strpool_readers--; + if (cil_strpool_readers == 0) { + hashtab_map(cil_strpool_tab, cil_strpool_entry_destroy, NULL); + hashtab_destroy(cil_strpool_tab); + } + pthread_mutex_unlock(&cil_strpool_mutex); }