From patchwork Fri Aug 19 15:51:27 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: 9290463 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 55176607FF for ; Fri, 19 Aug 2016 15:53:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 45F48294AF for ; Fri, 19 Aug 2016 15:53:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3AA51294B7; Fri, 19 Aug 2016 15:53:04 +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=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from emsm-gh1-uea11.nsa.gov (emsm-gh1-uea11.nsa.gov [8.44.101.9]) (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 75CC1294AF for ; Fri, 19 Aug 2016 15:52:59 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.28,544,1464652800"; d="scan'208";a="18610360" IronPort-PHdr: =?us-ascii?q?9a23=3A4K/LnRUWBaVFcJ6eBPRCzqVikdPV8LGtZVwlr6E/?= =?us-ascii?q?grcLSJyIuqrYZxeAt8tkgFKBZ4jH8fUM07OQ6PG5HzZeqsfb+DBaKdoXBkdD0Z?= =?us-ascii?q?1X1yUbQ+e9QXXhK/DrayFoVO9jb3RCu0+BDE5OBczlbEfTqHDhpRQbGxH4KBYn?= =?us-ascii?q?br+tQt2asc272qiI9oHJZE0Q3XzmMOo0dkv9/F+Z9pFPx9AzcuBpklqBi0ALUt?= =?us-ascii?q?we/XlvK1OXkkS0zeaL17knzR5tvek8/dVLS6TwcvdwZ7VZCDM7LzJ9v5Wz5lH+?= =?us-ascii?q?Vw/H2l8wGiVTy0IJUED560ThU5PwtDbqnvZs0ymde8vtRPY7Xirmp6V0QxbylH?= =?us-ascii?q?0vKy8y8GaRjNd5yq1cvlbpvBF2xYLOZ4CZcf5/Zb/1YcIRRW0HWN1YESNGHMf0?= =?us-ascii?q?dIcUJ/YQNuZf6Y/mrh0BqgXtKxOrAbbwyztMh3bzm6Z8yeMrHBvaxyQhGc4DtD?= =?us-ascii?q?Lfq9CmZ+8pTempwfyQnn34ZPRM1GK4sdDF?= X-IPAS-Result: =?us-ascii?q?A2FQCgCJKrdX/wHyM5BeHAEBgyaBUqNSgXOUHiCCEoVUTAE?= =?us-ascii?q?BAQEBAQECAQJbJ4IyBAMRghwCJBMUIA4DCQIXCBkICAgDAS0VGAcLBRgEiBC6X?= =?us-ascii?q?wEkiCuGXxEBZA2FBgWOHnaKM48eAolrhV9Ij21UhBpQAYV2gTYBAQE?= Received: from unknown (HELO tarius.tycho.ncsc.mil) ([144.51.242.1]) by emsm-gh1-uea11.nsa.gov with ESMTP; 19 Aug 2016 15:52:48 +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 u7JFq1AP001256; Fri, 19 Aug 2016 11:52:13 -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 u7JFpYen025898 for ; Fri, 19 Aug 2016 11:51:34 -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 u7JFpYx3001204; Fri, 19 Aug 2016 11:51:34 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A1BYCQCZKbdX/yNjr8ZeHAEBgyaBUqNSkgyEDBSGCYFiTAEBAQEBAQECXoUzUjAZdhKIMbpUAQEIJ4grh1UNgXQLgwcFjh52ijOPHgKPSkiPbVSEGhw0AYV2gTYBAQE X-IPAS-Result: A1BYCQCZKbdX/yNjr8ZeHAEBgyaBUqNSkgyEDBSGCYFiTAEBAQEBAQECXoUzUjAZdhKIMbpUAQEIJ4grh1UNgXQLgwcFjh52ijOPHgKPSkiPbVSEGhw0AYV2gTYBAQE X-IronPort-AV: E=Sophos;i="5.28,544,1464667200"; d="scan'208";a="5654980" 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; 19 Aug 2016 11:51:33 -0400 IronPort-PHdr: =?us-ascii?q?9a23=3A54GInhGLt6/SRl3b8TqoGp1GYnF86YWxBRYc798d?= =?us-ascii?q?s5kLTJ74o8iwAkXT6L1XgUPTWs2DsrQf2rOQ6f6rCDZIoc7Y9itTKNoUD15NoP?= =?us-ascii?q?5VtjRoONSCB0z/IayiRA0BN+MGamVY+WqmO1NeAsf0ag6aiHSz6TkPBke3blIt?= =?us-ascii?q?dazLE4Lfx/66y/q1s8WKJV4Z3XznP/gjdl329VyX7ZhOx9M6a+4Y8VjgmjNwYe?= =?us-ascii?q?NYxGdldxq4vi3XwYOOxqNl6DlaoPk79sRNAu3QdqU8SqFEXnx9azhmrOWijxTI?= =?us-ascii?q?TBOO630ASS1W10MQW0mWpC39C4z8tir8q/pVxDiRPcqwS6s9Hzul8eMjSgDjgT?= =?us-ascii?q?0bbRYl4WrXjYp2l6sdrxW/41RkzofSbJuSNfY7eqLGYfsGVGFBWYBXTCUHDYSi?= =?us-ascii?q?K8MUA/EpIfdTr467oUAH6xS5G1qCHuTqnyBJgnv31Kh82KI7FgvLxhA7N9MIrH?= =?us-ascii?q?nQ6t7yMfRBGdupxbXFmG2QJ8hd3i3wvc2RKkgs?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0FNCgCJKrdX/yNjr8ZeHAEBgyaBUqNSg?= =?us-ascii?q?XOQGYQMFIIXg3KBYkwBAQEBAQEBAgECWyeCMgQBEwGCQVIwGXYSiDG6UwEBCAI?= =?us-ascii?q?liCuHVQ2FBgWOHnaKM48eAo9KSI9tVIQaHDQBhXaBNgEBAQ?= X-IPAS-Result: =?us-ascii?q?A0FNCgCJKrdX/yNjr8ZeHAEBgyaBUqNSgXOQGYQMFIIXg3K?= =?us-ascii?q?BYkwBAQEBAQEBAgECWyeCMgQBEwGCQVIwGXYSiDG6UwEBCAIliCuHVQ2FBgWOH?= =?us-ascii?q?naKM48eAo9KSI9tVIQaHDQBhXaBNgEBAQ?= X-IronPort-AV: E=Sophos;i="5.28,544,1464652800"; d="scan'208";a="18610304" Received: from fmsmga002-icc.fm.intel.com ([198.175.99.35]) by emsm-gh1-uea11.nsa.gov with ESMTP; 19 Aug 2016 15:51:32 +0000 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga002-icc.fm.intel.com with ESMTP; 19 Aug 2016 08:51:31 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos; i="5.28,544,1464678000"; d="scan'208"; a="1017417726" Received: from rskumar-mobl.amr.corp.intel.com (HELO wcrobert-MOBL1.amr.corp.intel.com) ([10.254.189.104]) by orsmga001.jf.intel.com with ESMTP; 19 Aug 2016 08:51:30 -0700 From: william.c.roberts@intel.com To: selinux@tycho.nsa.gov, jwcart2@tycho.nsa.gov, seandroid-list@tycho.nsa.gov, sds@tycho.nsa.gov Subject: [PATCH v2 1/2] libsepol: calloc all the *_to_val_structs Date: Fri, 19 Aug 2016 08:51:27 -0700 Message-Id: <1471621888-18694-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 The usage patterns between these structures seem similair to role_val_to_struct usages. Calloc these up to prevent any unitialized usages. Signed-off-by: William Roberts --- libsepol/src/mls.c | 2 +- libsepol/src/policydb.c | 6 +++--- libsepol/src/users.c | 9 ++++++++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/libsepol/src/mls.c b/libsepol/src/mls.c index 2dc5f2b..8047d91 100644 --- a/libsepol/src/mls.c +++ b/libsepol/src/mls.c @@ -312,7 +312,7 @@ int mls_context_isvalid(const policydb_t * p, const context_struct_t * c) if (!c->user || c->user > p->p_users.nprim) return 0; usrdatum = p->user_val_to_struct[c->user - 1]; - if (!mls_range_contains(usrdatum->exp_range, c->range)) + if (!usrdatum || !mls_range_contains(usrdatum->exp_range, c->range)) return 0; /* user may not be associated with range */ return 1; diff --git a/libsepol/src/policydb.c b/libsepol/src/policydb.c index c225ac6..5f888d3 100644 --- a/libsepol/src/policydb.c +++ b/libsepol/src/policydb.c @@ -1074,7 +1074,7 @@ int policydb_index_others(sepol_handle_t * handle, free(p->user_val_to_struct); p->user_val_to_struct = (user_datum_t **) - malloc(p->p_users.nprim * sizeof(user_datum_t *)); + calloc(p->p_users.nprim, sizeof(user_datum_t *)); if (!p->user_val_to_struct) return -1; @@ -4006,12 +4006,12 @@ int policydb_reindex_users(policydb_t * p) free(p->sym_val_to_name[i]); p->user_val_to_struct = (user_datum_t **) - malloc(p->p_users.nprim * sizeof(user_datum_t *)); + calloc(p->p_users.nprim, sizeof(user_datum_t *)); if (!p->user_val_to_struct) return -1; p->sym_val_to_name[i] = (char **) - malloc(p->symtab[i].nprim * sizeof(char *)); + calloc(p->symtab[i].nprim, sizeof(char *)); if (!p->sym_val_to_name[i]) return -1; diff --git a/libsepol/src/users.c b/libsepol/src/users.c index ce54c2b..3ffb166 100644 --- a/libsepol/src/users.c +++ b/libsepol/src/users.c @@ -19,12 +19,17 @@ static int user_to_record(sepol_handle_t * handle, const char *name = policydb->p_user_val_to_name[user_idx]; user_datum_t *usrdatum = policydb->user_val_to_struct[user_idx]; - ebitmap_t *roles = &(usrdatum->roles.roles); + ebitmap_t *roles; ebitmap_node_t *rnode; unsigned bit; sepol_user_t *tmp_record = NULL; + if (!usrdatum) + goto err; + + roles = &(usrdatum->roles.roles); + if (sepol_user_create(handle, &tmp_record) < 0) goto err; @@ -234,6 +239,7 @@ int sepol_user_modify(sepol_handle_t * handle, if (!tmp_ptr) goto omem; policydb->user_val_to_struct = tmp_ptr; + policydb->user_val_to_struct[policydb->p_users.nprim] = NULL; tmp_ptr = realloc(policydb->sym_val_to_name[SYM_USERS], (policydb->p_users.nprim + @@ -241,6 +247,7 @@ int sepol_user_modify(sepol_handle_t * handle, if (!tmp_ptr) goto omem; policydb->sym_val_to_name[SYM_USERS] = tmp_ptr; + policydb->p_user_val_to_name[policydb->p_users.nprim] = NULL; /* Need to copy the user name */ name = strdup(cname);