From patchwork Thu Sep 15 18:10:41 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: 9334543 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 8AEA5601C2 for ; Thu, 15 Sep 2016 18:13:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8476E29AEA for ; Thu, 15 Sep 2016 18:13:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 78FE329B07; Thu, 15 Sep 2016 18:13:58 +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 D553129AEA for ; Thu, 15 Sep 2016 18:13:55 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.30,340,1470700800"; d="scan'208";a="19286319" IronPort-PHdr: =?us-ascii?q?9a23=3Agmhh5xagP1+8AAo9EjQaBRz/LSx+4OfEezUN459i?= =?us-ascii?q?sYplN5qZpM+zbnLW6fgltlLVR4KTs6sC0LuP9f+9EjVZut7B6ClEK80UEUddyI?= =?us-ascii?q?0/pE8JOIa9E0r1LfrnPWQRPf9pcxtbxUy9KlVfA83kZlff8TWY5D8WHQjjZ0Iu?= =?us-ascii?q?frymUrDbg8n/7e2u4ZqbO1wO32vkJ+wvZ0zv9k2R7pBQ2to6bP5pi1PgmThhQ6?= =?us-ascii?q?xu32RmJFaezV7Xx/yb29pdyRlWoO8r7MVaUK/3LOwSRL1cCyk6YShuvJW4/STZ?= =?us-ascii?q?SUOzwldUEiBPylsbSzTCuQr3Wpb3rzvSqvt22C7cO9b/C78zR3Dq9KpiThPyjy?= =?us-ascii?q?YLcjo4633/lt17jKUdpgmo4RN43cqce42OHOZvdaPaO9UBTCxOWdgVHy5eCYqm?= =?us-ascii?q?c9EnE/sKPeEeqZL04VQJs0iQHw6pUfzuzjtJj3qw1usg1O4sCx3d9A0mA98K9n?= =?us-ascii?q?/TqYamfJwOWPy4mfGbhQ7IaOlbjHKksIU=3D?= X-IPAS-Result: =?us-ascii?q?A2EmBACM5NpX/wHyM5BcGgEBAQECAQEBAQgBAQEBFwEBBAE?= =?us-ascii?q?BCgEBgw8BAQEBAR6BU6YelDkgh2NMAQEBAQEBAQECAQJbJ4IyBAMTBYIYAjcUI?= =?us-ascii?q?A4DCQIXCCEICAMBLRUYBwsFGASIKcI1ASSIMoZgEQGFeAWIMoZzikOPWQKJeIV?= =?us-ascii?q?pApBZVIJ9G4FvUAGEYXiBJwEBAQ?= Received: from unknown (HELO tarius.tycho.ncsc.mil) ([144.51.242.1]) by emsm-gh1-uea11.nsa.gov with ESMTP; 15 Sep 2016 18:13:37 +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 u8FIBAbe013404; Thu, 15 Sep 2016 14:11:14 -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 u8FIBA9g267888 for ; Thu, 15 Sep 2016 14:11:10 -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 u8FIB9c9013393; Thu, 15 Sep 2016 14:11:09 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A1DLAwA449pX/yNjr8ZcGgEBAQECAQEBAQgBAQEBgzoBAQEBAR6BU7ZMhBIUhgqBXkwBAgEBAQEBAl6GCDCBDxKISsImAQEIJ4gyiVgLgwcFiDKGc4pDj1kCj2ECkFlUgn0bgW8cNAGHAAEBAQ X-IPAS-Result: A1DLAwA449pX/yNjr8ZcGgEBAQECAQEBAQgBAQEBgzoBAQEBAR6BU7ZMhBIUhgqBXkwBAgEBAQEBAl6GCDCBDxKISsImAQEIJ4gyiVgLgwcFiDKGc4pDj1kCj2ECkFlUgn0bgW8cNAGHAAEBAQ X-IronPort-AV: E=Sophos;i="5.30,340,1470715200"; d="scan'208";a="5708406" 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; 15 Sep 2016 14:11:09 -0400 IronPort-PHdr: =?us-ascii?q?9a23=3AGnbQvhx6mM4lTVPXCy+O+j09IxM/srCxBDY+r6Qd?= =?us-ascii?q?0OwSIJqq85mqBkHD//Il1AaPBtSCra0ZwLOP6OigATVGusfZ9ihaMdRlbFwssY?= =?us-ascii?q?0uhQsuAcqIWwXQDcXBSGgEJvlET0Jv5HqhMEJYS47UblzWpWCuv3ZJQk2sfTR8?= =?us-ascii?q?Kum9IIPOlcP/j7n0oMyKJVoVz2PkOvsydEzw9lSJ8JFOwMNLEeUY8lPxuHxGeu?= =?us-ascii?q?BblytDBGm4uFLC3Pq254Np6C9KuvgspIZqWKT+eLkkH/QDVGx1e0h83sDgtAHC?= =?us-ascii?q?QA2T/TNcFzxOylsbNTPC9g33Uq38uSri7KI9hHHbboXKSuUvVDCj6bp7YAP5gy?= =?us-ascii?q?cAcTgi+SfYjdI0xLlWqRStvRt+z8vQYZqJHOZvdaPaO9UBTCxOWdgVHzdNGaug?= =?us-ascii?q?foAPCKwHJu8eoI7j4xMMshC3HxX2LP/+wT9PwHnt1Os11PpyPxvB2Vk/HtYKsX?= =?us-ascii?q?3R6t6zLqAYXPqp14HJyynOa7Vd3jKuu8Dzbhk9rKTUDvpLes3LxBxqSVvI?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0EjBABc49pX/yNjr8ZcGgEBAQECAQEBA?= =?us-ascii?q?QgBAQEBFgEBAQMBAQEJAQEBgw8BAQEBAR6BU6YekC6EEhSGCoFeTAEBAQEBAQE?= =?us-ascii?q?BAgECWyeCMgQBFQWDEDCBDxKISsImAQEIAiWIMoxqBYgyhnOKQ49ZAo9hApBZV?= =?us-ascii?q?IJ9G4FvHDQBhUCBQAEBAQ?= X-IPAS-Result: =?us-ascii?q?A0EjBABc49pX/yNjr8ZcGgEBAQECAQEBAQgBAQEBFgEBAQM?= =?us-ascii?q?BAQEJAQEBgw8BAQEBAR6BU6YekC6EEhSGCoFeTAEBAQEBAQEBAgECWyeCMgQBF?= =?us-ascii?q?QWDEDCBDxKISsImAQEIAiWIMoxqBYgyhnOKQ49ZAo9hApBZVIJ9G4FvHDQBhUC?= =?us-ascii?q?BQAEBAQ?= X-IronPort-AV: E=Sophos;i="5.30,340,1470700800"; d="scan'208";a="19286212" Received: from fmsmga002-icc.fm.intel.com ([198.175.99.35]) by emsm-gh1-uea11.nsa.gov with ESMTP; 15 Sep 2016 18:11:08 +0000 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga002-icc.fm.intel.com with ESMTP; 15 Sep 2016 11:10:52 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos; i="5.30,340,1470726000"; d="scan'208"; a="1030761593" Received: from vmarias-mobl3.amr.corp.intel.com (HELO wcrobert-MOBL1.amr.corp.intel.com) ([10.249.9.60]) by orsmga001.jf.intel.com with ESMTP; 15 Sep 2016 11:10:52 -0700 From: william.c.roberts@intel.com To: selinux@tycho.nsa.gov, seandroid-list@tycho.nsa.gov, sds@tycho.nsa.gov, jwcart2@tycho.nsa.gov Subject: [PATCH] libselinux: correct error path to always try text Date: Thu, 15 Sep 2016 11:10:41 -0700 Message-Id: <1473963041-6289-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 patch 5e15a52aaa cleans up the process_file() but introduced a bug. If the binary file cannot be opened, always attempt to fall back to the textual file, this was not occurring. The logic should be: 1. Open the newest file based on base path + suffix vs base_path + suffix + ".bin". 2. If anything fails, attempt base_path + suffix. In the case that the file_contexts was the newest file and used for processing fails, it will attempt the same failure recovery, which will fail. It was decided to keep it this was for simplicity. Signed-off-by: William Roberts --- libselinux/src/label_file.c | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c index 9faecdb..e3dcdd6 100644 --- a/libselinux/src/label_file.c +++ b/libselinux/src/label_file.c @@ -447,7 +447,7 @@ static bool fcontext_is_binary(FILE *fp) #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) static FILE *open_file(const char *path, const char *suffix, - char *save_path, size_t len, struct stat *sb) + char *save_path, size_t len, struct stat *sb, bool force_text) { unsigned int i; int rc; @@ -469,7 +469,11 @@ static FILE *open_file(const char *path, const char *suffix, return NULL; } - for (i = 0; i < ARRAY_SIZE(fdetails); i++) { + size_t array_size = ARRAY_SIZE(fdetails); + if (force_text) + array_size--; + + for (i = 0; i < array_size; i++) { /* This handles the case if suffix is null */ path = rolling_append(stack_path, fdetails[i].suffix, @@ -515,24 +519,35 @@ static int process_file(const char *path, const char *suffix, const char *prefix, struct selabel_digest *digest) { int rc; + unsigned int i; struct stat sb; FILE *fp = NULL; char found_path[PATH_MAX]; - fp = open_file(path, suffix, found_path, sizeof(found_path), &sb); - if (fp == NULL) - return -1; + /* + * first path open the newest modified file, if it fails, the second + * pass falls through to the plain text file. + */ + for(i=0; i < 2; i++) + { + fp = open_file(path, suffix, found_path, sizeof(found_path), &sb, + i > 0); + if (fp == NULL) + return -1; - rc = fcontext_is_binary(fp) ? - load_mmap(fp, sb.st_size, rec, found_path) : - process_text_file(fp, prefix, rec, found_path); - if (rc < 0) - goto out; + rc = fcontext_is_binary(fp) ? + load_mmap(fp, sb.st_size, rec, found_path) : + process_text_file(fp, prefix, rec, found_path); + if (!rc) { + rc = digest_add_specfile(digest, fp, NULL, sb.st_size, found_path); + } - rc = digest_add_specfile(digest, fp, NULL, sb.st_size, found_path); -out: - fclose(fp); - return rc; + fclose(fp); + + if(!rc) + return 0; + } + return -1; } static void closef(struct selabel_handle *rec);