From patchwork Wed May 4 20:41:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Carter X-Patchwork-Id: 9018721 Return-Path: X-Original-To: patchwork-selinux@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 71DBD9F39D for ; Wed, 4 May 2016 20:42:46 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 330CA203A5 for ; Wed, 4 May 2016 20:42:45 +0000 (UTC) Received: from emsm-gh1-uea10.nsa.gov (emsm-gh1-uea10.nsa.gov [8.44.101.8]) by mail.kernel.org (Postfix) with ESMTP id 1229E20397 for ; Wed, 4 May 2016 20:42:43 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.24,578,1454976000"; d="scan'208";a="13364116" IronPort-PHdr: =?us-ascii?q?9a23=3A0gpvvxyrWkHBtyXXCy+O+j09IxM/srCxBDY+r6Qd?= =?us-ascii?q?0e8eIJqq85mqBkHD//Il1AaPBtWKraoewLOL6ujJYi8p39WoiDg6aptCVhsI24?= =?us-ascii?q?09vjcLJ4q7M3D9N+PgdCcgHc5PBxdP9nC/NlVJSo6lPwWB6kO74TNaIBjjLw09?= =?us-ascii?q?fr2zQd6DyZztnLnvotX6WEZhunmUWftKNhK4rAHc5IE9oLBJDeIP8CbPuWZCYO?= =?us-ascii?q?9MxGlldhq5lhf44dqsrtY4q3wD888784Z8dYmyP+FiFf0LRAghZns44MztqAnr?= =?us-ascii?q?URqE5nxaVH4f1BVPHVvr9hb/C7X4tybh/sF0wjWXJ4WiT68/UC6+tY93WRToj2?= =?us-ascii?q?EBLDd//2bJ3J8jxJlHqQ6s8kQsi7XfZ5uYYb8kJq4=3D?= X-IPAS-Result: =?us-ascii?q?A2EFBQD7XSpX/wHyM5BeHAGDG4FBD7tmH4c2TAEBAQEBAQI?= =?us-ascii?q?CYieCLYIVAgQBAiQTFCAOAwkBARcpCAgDAS0VEQ4LBRgEiAm9fR6OfREBhXUFm?= =?us-ascii?q?BmOGAKJUIVBRY5vYoIFG4FnUIclgTUBAQE?= Received: from unknown (HELO tarius.tycho.ncsc.mil) ([144.51.242.1]) by emsm-gh1-uea10.nsa.gov with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 04 May 2016 20:42:41 +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 u44KgeOf015792; Wed, 4 May 2016 16:42:41 -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 u44KdQZE015129 for ; Wed, 4 May 2016 16:39:26 -0400 Received: from moss-lions.infosec.tycho.ncsc.mil (moss-lions [192.168.25.4]) by tarius.tycho.ncsc.mil (8.14.4/8.14.4) with ESMTP id u44KdQa3014889 for ; Wed, 4 May 2016 16:39:26 -0400 From: James Carter To: selinux@tycho.nsa.gov Subject: [PATCH 3/6 v2] libsepol/cil: Add cil_tree_log() and supporting functions Date: Wed, 4 May 2016 16:41:03 -0400 Message-Id: <1462394466-25076-4-git-send-email-jwcart2@tycho.nsa.gov> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1462394466-25076-1-git-send-email-jwcart2@tycho.nsa.gov> References: <1462394466-25076-1-git-send-email-jwcart2@tycho.nsa.gov> 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-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Provide more detailed log messages containing all relevant CIL and high-level language source file information through cil_tree_log(). cil_tree_log() uses two new functions: cil_tree_get_next_path() and cil_tree_get_cil_path(). cil_tree_get_next_path() traverses up the parse tree or AST until it finds the next CIL or high-level language source information nodes. It will return the path and whether or not the path is for a CIL file. cil_tree_get_cil_path() uses cil_tree_get_next_path() to return the CIL path. Example cil_tree_log() message: Problem at line 21 of policy.cil (from line 11 of foo.hll) (from line 2 of bar.hll) Signed-off-by: James Carter --- libsepol/cil/src/cil_tree.c | 86 +++++++++++++++++++++++++++++++++++++++++++++ libsepol/cil/src/cil_tree.h | 4 +++ 2 files changed, 90 insertions(+) diff --git a/libsepol/cil/src/cil_tree.c b/libsepol/cil/src/cil_tree.c index 6e56dd1..ef82143 100644 --- a/libsepol/cil/src/cil_tree.c +++ b/libsepol/cil/src/cil_tree.c @@ -59,6 +59,92 @@ __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) void cil_tree_e exit(1); } +struct cil_tree_node *cil_tree_get_next_path(struct cil_tree_node *node, char **path, int* is_cil) +{ + if (!node) { + return NULL; + } + + node = node->parent; + + while (node) { + if (node->flavor == CIL_NODE && node->data == NULL) { + if (node->cl_head->data == CIL_KEY_SRC_INFO) { + /* Parse Tree */ + *path = node->cl_head->next->next->data; + *is_cil = (node->cl_head->next->data == CIL_KEY_SRC_CIL); + return node; + } + node = node->parent; + } else if (node->flavor == CIL_SRC_INFO) { + /* AST */ + struct cil_src_info *info = node->data; + *path = info->path; + *is_cil = info->is_cil; + return node; + } else { + if (node->flavor == CIL_CALL) { + struct cil_call *call = node->data; + node = NODE(call->macro); + } else if (node->flavor == CIL_BLOCKINHERIT) { + struct cil_blockinherit *inherit = node->data; + node = NODE(inherit->block); + } else { + node = node->parent; + } + } + } + + return NULL; +} + +char *cil_tree_get_cil_path(struct cil_tree_node *node) +{ + char *path = NULL; + int is_cil; + + while (node) { + node = cil_tree_get_next_path(node, &path, &is_cil); + if (node && is_cil) { + return path; + } + } + + return NULL; +} + +__attribute__((format (printf, 3, 4))) void cil_tree_log(struct cil_tree_node *node, enum cil_log_level lvl, const char* msg, ...) +{ + va_list ap; + + va_start(ap, msg); + cil_vlog(lvl, msg, ap); + va_end(ap); + + if (node) { + char *path = NULL; + int is_cil; + unsigned hll_line = node->hll_line; + + path = cil_tree_get_cil_path(node); + + if (path != NULL) { + cil_log(lvl, " at line %d of %s", node->line, path); + } + + while (node) { + node = cil_tree_get_next_path(node, &path, &is_cil); + if (node && !is_cil) { + cil_log(lvl," (from line %d of %s)", hll_line, path); + path = NULL; + hll_line = node->hll_line; + } + } + } + + cil_log(lvl,"\n"); +} + int cil_tree_init(struct cil_tree **tree) { struct cil_tree *new_tree = cil_malloc(sizeof(*new_tree)); diff --git a/libsepol/cil/src/cil_tree.h b/libsepol/cil/src/cil_tree.h index 43d6b98..318eecd 100644 --- a/libsepol/cil/src/cil_tree.h +++ b/libsepol/cil/src/cil_tree.h @@ -51,6 +51,10 @@ struct cil_tree_node { void *data; }; +struct cil_tree_node *cil_tree_get_next_path(struct cil_tree_node *node, char **path, int* is_cil); +char *cil_tree_get_cil_path(struct cil_tree_node *node); +__attribute__((format (printf, 3, 4))) void cil_tree_log(struct cil_tree_node *node, enum cil_log_level lvl, const char* msg, ...); + int cil_tree_init(struct cil_tree **tree); void cil_tree_destroy(struct cil_tree **tree); void cil_tree_subtree_destroy(struct cil_tree_node *node);