From patchwork Thu Sep 15 23:13:58 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: 9334903 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 1DF46601C2 for ; Thu, 15 Sep 2016 23:16:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0199D296D0 for ; Thu, 15 Sep 2016 23:16:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EA1B529C77; Thu, 15 Sep 2016 23:16:56 +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 4FC80296D0 for ; Thu, 15 Sep 2016 23:16:55 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.30,341,1470700800"; d="scan'208";a="19295445" IronPort-PHdr: =?us-ascii?q?9a23=3AY+E0mxX8a3WDOIUzr6ATi24tu9zV8LGtZVwlr6E/?= =?us-ascii?q?grcLSJyIuqrYZxeAt8tkgFKBZ4jH8fUM07OQ6PG5HzNeqs/a+DBaKdoXBkdD0Z?= =?us-ascii?q?1X1yUbQ+e9QXXhK/DrayFoVO9jb3RCu0+BDE5OBczlbEfTqHDhpRQbGxH4KBYn?= =?us-ascii?q?br+tQt2asc272qiI9oHJZE0Q3XzmMOo0c0v99F6Z9pFPx9AzcuBpklqBi0ALUt?= =?us-ascii?q?we/XlvK1OXkkS0zeaL17knzR5tvek8/dVLS6TwcvdwZ7VZCDM7LzJ9v5Wz5lH+?= =?us-ascii?q?Vw/H2l8wGiVTy0IJUED560ThU5PwtDbqnvZs0ymde8vtRPY7Xirmp7xmQRnkki?= =?us-ascii?q?AGO3s98XrLotBhh6Jc5hS6rlpwxJCQKJqZL9Jib6jdepUcXmMHUcFPE2RDHYi1?= =?us-ascii?q?dZdVJ/YQNuZf6Y/mrh0BqgXtKxOrAbbwyztMh3bzm6Z8yeMrHBvaxyQhGc4DtD?= =?us-ascii?q?Lfq9CmZ+8pTempwfyQnn34ZPRM1GK4sdDF?= X-IPAS-Result: =?us-ascii?q?A2EZBAASK9tX/wHyM5BdGgEBAQECAQEBAQgBAQEBFwEBBAE?= =?us-ascii?q?BCgEBgw8BAQEBAR6BU7pXIIdqTAEBAQEBAQEBAgECWyeCMgQDEwWCGAI3FCAOA?= =?us-ascii?q?wkCFwghCAgDAS0VGAcLBRgEiCnCPwEkiDKGYBEBhXgFiDKGc4pDj1kCiXiFa5B?= =?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:16:54 +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 u8FNEWIV011554; Thu, 15 Sep 2016 19:14:40 -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 u8FNEUaQ268886 for ; Thu, 15 Sep 2016 19:14:30 -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 u8FNETC0011541; Thu, 15 Sep 2016 19:14:29 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A1DUAwDQKttX/yNjr8ZdGgEBAQECAQEBAQgBAQEBgzoBAQEBAR6BU7ZMhBIUhgqBZUwBAgEBAQEBAl6GCDCBDxKISsIyAQEIJ4gyiVgLgwcFiDKGc4pDj1kCj2OQWVSCfRuBbxw0AYcAAQEB X-IPAS-Result: A1DUAwDQKttX/yNjr8ZdGgEBAQECAQEBAQgBAQEBgzoBAQEBAR6BU7ZMhBIUhgqBZUwBAgEBAQEBAl6GCDCBDxKISsIyAQEIJ4gyiVgLgwcFiDKGc4pDj1kCj2OQWVSCfRuBbxw0AYcAAQEB X-IronPort-AV: E=Sophos;i="5.30,341,1470715200"; d="scan'208";a="5708973" 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:14:28 -0400 IronPort-PHdr: =?us-ascii?q?9a23=3AzM9KJRXTuFYs2h15m5V6+jyyBA7V8LGtZVwlr6E/?= =?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?A0EXBAASK9tX/yNjr8ZdGgEBAQECAQEBA?= =?us-ascii?q?QgBAQEBFgEBAQMBAQEJAQEBgw8BAQEBAR6BU7ZMhBIUhgqBZUwBAQEBAQEBAQI?= =?us-ascii?q?BAlsngjIEARUFgxAwgQ8SiErCMwEBCAIliDKMagWIMoZzikOPWQKPY5BZVIJ9G?= =?us-ascii?q?4FvHDQBhUCBQAEBAQ?= X-IPAS-Result: =?us-ascii?q?A0EXBAASK9tX/yNjr8ZdGgEBAQECAQEBAQgBAQEBFgEBAQM?= =?us-ascii?q?BAQEJAQEBgw8BAQEBAR6BU7ZMhBIUhgqBZUwBAQEBAQEBAQIBAlsngjIEARUFg?= =?us-ascii?q?xAwgQ8SiErCMwEBCAIliDKMagWIMoZzikOPWQKPY5BZVIJ9G4FvHDQBhUCBQAE?= =?us-ascii?q?BAQ?= X-IronPort-AV: E=Sophos;i="5.30,341,1470700800"; d="scan'208";a="19295418" Received: from fmsmga002-icc.fm.intel.com ([198.175.99.35]) by emsm-gh1-uea11.nsa.gov with ESMTP; 15 Sep 2016 23:14:03 +0000 Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga002-icc.fm.intel.com with ESMTP; 15 Sep 2016 16:14:02 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos; i="5.30,341,1470726000"; d="scan'208"; a="1051234539" Received: from vmarias-mobl3.amr.corp.intel.com (HELO wcrobert-MOBL1.amr.corp.intel.com) ([10.249.9.60]) by orsmga002.jf.intel.com with ESMTP; 15 Sep 2016 16:14:01 -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 v3] libselinux: correct error path to always try text Date: Thu, 15 Sep 2016 16:13:58 -0700 Message-Id: <1473981238-11051-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 | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c index 9faecdb..0dee059 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,33 @@ 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);