From patchwork Mon Aug 8 17:28:21 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Roberts, William C" X-Patchwork-Id: 9269007 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 7BDA16075A for ; Mon, 8 Aug 2016 17:33:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6D79526E51 for ; Mon, 8 Aug 2016 17:33:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 61360283E4; Mon, 8 Aug 2016 17:33:57 +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.9 required=2.0 tests=BAYES_00 autolearn=ham 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 9661826E51 for ; Mon, 8 Aug 2016 17:33:56 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.28,491,1464652800"; d="scan'208";a="16393004" IronPort-PHdr: =?us-ascii?q?9a23=3AOEwHLBTdGYXh91uHKf7z3Qf/BNpsv+yvbD5Q0YIu?= =?us-ascii?q?jvd0So/mwa64bRaN2/xhgRfzUJnB7Loc0qyN4vimCDRLuM/Z+Fk5M7V0Hycfjs?= =?us-ascii?q?sXmwFySOWkMmbcaMDQUiohAc5ZX0Vk9XzoeWJcGcL5ekGA6ibqtW1aJBzzOEJP?= =?us-ascii?q?K/jvHcaK1oLshrj0qsWYOlwQzBOGIppKZC2sqgvQssREyaBDEY0WjiXzn31TZu?= =?us-ascii?q?5NznlpL1/A1zz158O34YIxu38I46FpytREGZneU+x4COUATWduD2dg/8DvtB/e?= =?us-ascii?q?XSOT93AcVSMQiRMODA/bqFnhU573vjH9t+Y43CiBIeXqXLs0XnKk9K4tRxj2zG?= =?us-ascii?q?8cOyUR7HDciss2irlS5h2muU9R2YnRNZ6UMP5/d6abdpUAQmBMRNpKfy1HHo64?= =?us-ascii?q?KYAICrlSdd1EppXw8gNd5SC1AhOhUaa2kjI=3D?= X-IPAS-Result: =?us-ascii?q?A2EeBgB4wahX/wHyM5BdGwEBAYMngVKlDYFrlBYgh0VMAQE?= =?us-ascii?q?BAQEBAgJaJ4IyBAMRghwCJBMUIA4DCQIXCCEICAMBLRUYBwsFGASIEMMFASSIK?= =?us-ascii?q?oZfEQGFdwWIJYZoiiyPCgKJaoVVApAsVIQaTgGGKIE2AQEB?= Received: from unknown (HELO tarius.tycho.ncsc.mil) ([144.51.242.1]) by emsm-gh1-uea10.nsa.gov with ESMTP; 08 Aug 2016 17:32:19 +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 u78HUZck008182; Mon, 8 Aug 2016 13:31:10 -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 u78HShjc042071 for ; Mon, 8 Aug 2016 13:28:43 -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 u78HStUo007607; Mon, 8 Aug 2016 13:28:55 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A1CBBgDbwKhX/yNjr8ZdHAEBgyeBUqUNkXyEDBSGCYFBTAEBAQEBAV6FM1IwgQ8SiDHCdQEwiCqMaAWIJYZoiiyPCgKPPwKQLFSEGk4Bh14BAQE X-IPAS-Result: A1CBBgDbwKhX/yNjr8ZdHAEBgyeBUqUNkXyEDBSGCYFBTAEBAQEBAV6FM1IwgQ8SiDHCdQEwiCqMaAWIJYZoiiyPCgKPPwKQLFSEGk4Bh14BAQE X-IronPort-AV: E=Sophos;i="5.28,491,1464667200"; d="scan'208";a="5631550" 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; 08 Aug 2016 13:32:15 -0400 IronPort-PHdr: =?us-ascii?q?9a23=3ARnmtIBUwKWNX0fpQ76l9g3I4nVbV8LGtZVwlr6E/?= =?us-ascii?q?grcLSJyIuqrYZxWAt8tkgFKBZ4jH8fUM07OQ6PG4Hz1Yqs/Y6DgrS99lb1c9k8?= =?us-ascii?q?IYnggtUoauKHbQC7rUVRE8B9lIT1R//nu2YgB/Ecf6YEDO8DXptWZBUiv2OQc9?= =?us-ascii?q?HOnpAIma153xjLHqvcyLKFwR3HKUWvBbFF2OtwLft80b08NJC50a7V/3mEZOYP?= =?us-ascii?q?lc3mhyJFiezF7W78a0+4N/oWwL46pyv+YJa6jxfrw5QLpEF3xmdjltvIy4gyLE?= =?us-ascii?q?VxeC6kERWWQOyFIIX1CEv1nGWcLqvy/7sPdt8DWLNs3xC7YvUHKt6LkvAATlgS?= =?us-ascii?q?ELLTk4/CTTi9dspL5KqxKm4RpkysjbZ5/RfOFyZYvBbNgaQixHRc8XWCtfUa2m?= =?us-ascii?q?aI5aEOsFOedRqs/24UEJpxakGRKEBeXzxzsOjXjzjv5y6PgoDQyThF9oJNkJqn?= =?us-ascii?q?mB6YutOQ=3D=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0HeBgD/v6hX/yNjr8ZdHAEBgyeBUqUNg?= =?us-ascii?q?WuQEYQMFIYJgUFMAQEBAQEBAgJaJ4IyBAETgkJSMIEPEogxwnQBCyWIKoxoBYg?= =?us-ascii?q?lhmiKLI8KAo8/ApAsVIQaTgGHXgEBAQ?= X-IPAS-Result: =?us-ascii?q?A0HeBgD/v6hX/yNjr8ZdHAEBgyeBUqUNgWuQEYQMFIYJgUF?= =?us-ascii?q?MAQEBAQEBAgJaJ4IyBAETgkJSMIEPEogxwnQBCyWIKoxoBYglhmiKLI8KAo8/A?= =?us-ascii?q?pAsVIQaTgGHXgEBAQ?= X-IronPort-AV: E=Sophos;i="5.28,491,1464652800"; d="scan'208";a="18303499" Received: from fmsmga002-icc.fm.intel.com ([198.175.99.35]) by emsm-gh1-uea11.nsa.gov with ESMTP; 08 Aug 2016 17:28:35 +0000 Received: from fmsmga004-icc.fm.intel.com ([198.175.99.11]) by fmsmga002-icc.fm.intel.com with ESMTP; 08 Aug 2016 10:28:28 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.28,491,1464678000"; d="scan'208";a="152492968" Received: from liangy-mobl1.amr.corp.intel.com (HELO wcrobert-MOBL1.amr.corp.intel.com) ([10.252.134.240]) by fmsmga004.fm.intel.com with ESMTP; 08 Aug 2016 10:28:25 -0700 From: william.c.roberts@intel.com To: selinux@tycho.nsa.gov, seandroid-list@tycho.nsa.gov, sds@tycho.nsa.gov Subject: [PATCH] libsepol: fix memory leak in expand.c Date: Mon, 8 Aug 2016 10:28:21 -0700 Message-Id: <1470677301-20065-1-git-send-email-william.c.roberts@intel.com> X-Mailer: git-send-email 1.9.1 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: MIME-Version: 1.0 Errors-To: selinux-bounces@tycho.nsa.gov Sender: "Selinux" X-Virus-Scanned: ClamAV using ClamSMTP From: William Roberts ebitmap_set_bit() can possible allocate nodes, however, the bail early style of type_set_expand() could leave internal ebitmaps allocated but not free'd. Modify type_set_expand() so that it free's all allocated ebitmaps before returning the error code to the calling routine. Signed-off-by: William Roberts --- libsepol/src/expand.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/libsepol/src/expand.c b/libsepol/src/expand.c index 4d3c623..0ad57f5 100644 --- a/libsepol/src/expand.c +++ b/libsepol/src/expand.c @@ -2497,6 +2497,7 @@ int type_set_expand(type_set_t * set, ebitmap_t * t, policydb_t * p, unsigned int i; ebitmap_t types, neg_types; ebitmap_node_t *tnode; + int rc =-1; ebitmap_init(&types); ebitmap_init(t); @@ -2511,7 +2512,7 @@ int type_set_expand(type_set_t * set, ebitmap_t * t, policydb_t * p, * what's available in the type_val_to_struct mapping */ if (i > p->p_types.nprim - 1) - return -1; + goto err_types; if (p->type_val_to_struct[i]->flavor == TYPE_ATTRIB) { @@ -2519,11 +2520,11 @@ int type_set_expand(type_set_t * set, ebitmap_t * t, policydb_t * p, (&types, &p->type_val_to_struct[i]-> types)) { - return -1; + goto err_types; } } else { if (ebitmap_set_bit(&types, i, 1)) { - return -1; + goto err_types; } } } @@ -2531,7 +2532,7 @@ int type_set_expand(type_set_t * set, ebitmap_t * t, policydb_t * p, } else { /* No expansion of attributes, just copy the set as is. */ if (ebitmap_cpy(&types, &set->types)) - return -1; + goto err_types; } /* Now do the same thing for negset */ @@ -2543,11 +2544,11 @@ int type_set_expand(type_set_t * set, ebitmap_t * t, policydb_t * p, if (ebitmap_union (&neg_types, &p->type_val_to_struct[i]->types)) { - return -1; + goto err_neg; } } else { if (ebitmap_set_bit(&neg_types, i, 1)) { - return -1; + goto err_neg; } } } @@ -2562,7 +2563,7 @@ int type_set_expand(type_set_t * set, ebitmap_t * t, policydb_t * p, p->type_val_to_struct[i]->flavor == TYPE_ATTRIB) continue; if (ebitmap_set_bit(t, i, 1)) - return -1; + goto err_neg; } goto out; } @@ -2571,7 +2572,7 @@ int type_set_expand(type_set_t * set, ebitmap_t * t, policydb_t * p, if (ebitmap_node_get_bit(tnode, i) && (!ebitmap_get_bit(&neg_types, i))) if (ebitmap_set_bit(t, i, 1)) - return -1; + goto err_neg; } if (set->flags & TYPE_COMP) { @@ -2583,20 +2584,23 @@ int type_set_expand(type_set_t * set, ebitmap_t * t, policydb_t * p, } if (ebitmap_get_bit(t, i)) { if (ebitmap_set_bit(t, i, 0)) - return -1; + goto err_neg; } else { if (ebitmap_set_bit(t, i, 1)) - return -1; + goto err_neg; } } } - out: + out: + rc = 0; - ebitmap_destroy(&types); + err_neg: ebitmap_destroy(&neg_types); + err_types: + ebitmap_destroy(&types); - return 0; + return rc; } static int copy_neverallow(policydb_t * dest_pol, uint32_t * typemap,