From patchwork Thu Sep 15 23:07:37 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: 9334893 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 44096601C2 for ; Thu, 15 Sep 2016 23:10:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1C39629C77 for ; Thu, 15 Sep 2016 23:10:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0BDC229C88; Thu, 15 Sep 2016 23:10:52 +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 (smtp.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 45C3F29C77 for ; Thu, 15 Sep 2016 23:10:50 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.30,341,1470700800"; d="scan'208";a="19295382" IronPort-PHdr: =?us-ascii?q?9a23=3AiNiz4hDT3TOTXuGYorEUUyQJP3N1i/DPJgcQr6Af?= =?us-ascii?q?oPdwSP/4oMbcNUDSrc9gkEXOFd2CrakV0qyJ7eu5AiRAuc/H6yFaNsQUFlcsso?= =?us-ascii?q?Y/p0QYGsmLCEn2frbBThcRO4B8bmJj5GyxKkNPGczzNBX4q3y26iMOSF2kbVIm?= =?us-ascii?q?btr8FoOatcmrzef6o8SVOFQRwmXjKuopZFXu9EOK55FQ2dMjYo8KiTLx6kNSfO?= =?us-ascii?q?pXwW46bXmypD3bovmKwZh47i5LsOgg/cMTGY/zfqA/UKAKRG9+azN92dfv/SXn?= =?us-ascii?q?YUPPoyJEEzZerh0dGAXB7RfnTr/toyD6sax7wyDcMsroCfgvVD2k471sSROtjC?= =?us-ascii?q?YcKxYl4WrXjYp2l6sdrxW/41Rkz5X8fJCeNP04eLjUO9wdWy4JXNlaXjZdWKug?= =?us-ascii?q?foAPCKwHJu8eoI7j9HUUqh7rGgioAOLmz3lDwGXx16Agz/8JEAfa0QhmFNUL4y?= =?us-ascii?q?ecl8n8KKpHCbP996LP1ziWKqoO1A=3D=3D?= X-IPAS-Result: =?us-ascii?q?A2EZBACnKdtX/wHyM5BdGgEBAQECAQEBAQgBAQEBFwEBBAE?= =?us-ascii?q?BCgEBgw8BAQEBAR6BU7pXIIdpTAEBAQEBAQEBAgECWyeCMgQDEwWCGAI3FCAOA?= =?us-ascii?q?wkCFwghCAgDAS0VGAcLBRgEiCnCPgEkiDKGYBEBhXgFiDKGc4pDj1kCiXiFa5B?= =?us-ascii?q?ZVIJ9G4FvUAGEYXiBJwEBAQ?= Received: from unknown (HELO tarius.tycho.ncsc.mil) ([144.51.242.1]) by emsm-gh1-uea11.nsa.gov with ESMTP; 15 Sep 2016 23:10: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 u8FN7krx010830; Thu, 15 Sep 2016 19:08:01 -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 u8FN7jbI268878 for ; Thu, 15 Sep 2016 19:07:45 -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 u8FN7i54010817; Thu, 15 Sep 2016 19:07:44 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A1DUAwCLKNtX/yNjr8ZdGgEBAQECAQEBAQgBAQEBgzoBAQEBAR6BU7ZMhBIUhgqBZEwBAgEBAQEBAl6GCDCBDxKISsIyAQEIJ4gyiVgLgwcFiDKGc4pDj1kCj2OQWVSCfRuBbxw0AYcAAQEB X-IPAS-Result: A1DUAwCLKNtX/yNjr8ZdGgEBAQECAQEBAQgBAQEBgzoBAQEBAR6BU7ZMhBIUhgqBZEwBAgEBAQEBAl6GCDCBDxKISsIyAQEIJ4gyiVgLgwcFiDKGc4pDj1kCj2OQWVSCfRuBbxw0AYcAAQEB X-IronPort-AV: E=Sophos;i="5.30,341,1470715200"; d="scan'208";a="5708967" 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 19:07:43 -0400 IronPort-PHdr: =?us-ascii?q?9a23=3AgP4I/hUvBBxKuTgy+JyoMw4Nsi/V8LGtZVwlr6E/?= =?us-ascii?q?grcLSJyIuqrYZxKFt8tkgFKBZ4jH8fUM07OQ6PG5HzNeqs/a+DBaKdoXBkdD0Z?= =?us-ascii?q?1X1yUbQ+e9QXXhK/DrayFoVO9jb3RCu0+BDE5OBczlbEfTqHDhpRQbGxH4KBYn?= =?us-ascii?q?br+tQt2asc272qiI9oHJZE0Q3XzmMOo0c0v99F6Z9pFPx9AzcuBpklqBi0ALUt?= =?us-ascii?q?we/XlvK1OXkkS0zeaL17knzR5tvek8/dVLS6TwcvdwZ7VZCDM7LzJ9v5Wz5lHr?= =?us-ascii?q?BDGC7XoEU2gQjgEAQ02ctEm7DaHKtTHmu+ZH1SKbJZe+DexsGGfq06A+UxLsiS?= =?us-ascii?q?EaJxYl4WrXjYp2l6sdrxW/41RkzofSbJuSNfY7eqLGYfsGVGFBWYBXTCUHDYSi?= =?us-ascii?q?K8MUA/EpIfdTr467oUAH6xS5G03kBvzkwyVU3FfqzKY61KInCgiA0ws+TPwUt3?= =?us-ascii?q?GBs9TxMKYbVKa+irPPxzjZc+h+2DHh5YyOeRck8qLEZq55bceEkRpnLAjClFjF?= =?us-ascii?q?7NK9Mg=3D=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0EXBACLKNtX/yNjr8ZdGgEBAQECAQEBA?= =?us-ascii?q?QgBAQEBFgEBAQMBAQEJAQEBgw8BAQEBAR6BU7ZMhBIUhgqBZEwBAQEBAQEBAQI?= =?us-ascii?q?BAlsngjIEARUFgxAwgQ8SiErCMgEBCAIliDKMagWIMoZzikOPWQKPY5BZVIJ9G?= =?us-ascii?q?4FvHDQBhUCBQAEBAQ?= X-IPAS-Result: =?us-ascii?q?A0EXBACLKNtX/yNjr8ZdGgEBAQECAQEBAQgBAQEBFgEBAQM?= =?us-ascii?q?BAQEJAQEBgw8BAQEBAR6BU7ZMhBIUhgqBZEwBAQEBAQEBAQIBAlsngjIEARUFg?= =?us-ascii?q?xAwgQ8SiErCMgEBCAIliDKMagWIMoZzikOPWQKPY5BZVIJ9G4FvHDQBhUCBQAE?= =?us-ascii?q?BAQ?= X-IronPort-AV: E=Sophos;i="5.30,341,1470700800"; d="scan'208";a="19295323" Received: from fmsmga002-icc.fm.intel.com ([198.175.99.35]) by emsm-gh1-uea11.nsa.gov with ESMTP; 15 Sep 2016 23:07:42 +0000 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga002-icc.fm.intel.com with ESMTP; 15 Sep 2016 16:07:42 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos; i="5.30,341,1470726000"; d="scan'208"; a="1030956247" 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 16:07:41 -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 v2] libselinux: correct error path to always try text Date: Thu, 15 Sep 2016 16:07:37 -0700 Message-Id: <1473980857-10199-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 | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c index 9faecdb..a03a5ce 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,34 @@ 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);