From patchwork Wed Sep 28 10:28:30 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Janis Danisevskis X-Patchwork-Id: 9353757 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 2346060756 for ; Wed, 28 Sep 2016 11:44:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 111772950F for ; Wed, 28 Sep 2016 11:44:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 04EFD29511; Wed, 28 Sep 2016 11:44:21 +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=-3.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,RCVD_IN_SORBS_SPAM,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from emsm-gh1-uea11.nsa.gov (emsm-gh1-uea11.nsa.gov [8.44.101.9]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9AA482950F for ; Wed, 28 Sep 2016 11:44:19 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.30,409,1470700800"; d="scan'208";a="19614892" IronPort-PHdr: =?us-ascii?q?9a23=3ATjfvLhzjabxRSkjXCy+O+j09IxM/srCxBDY+r6Qd?= =?us-ascii?q?0OwXIJqq85mqBkHD//Il1AaPBtSBrasbwLGH+4nbGkU4qa6bt34DdJEeHzQksu?= =?us-ascii?q?4x2zIaPcieFEfgJ+TrZSFpVO5LVVti4m3peRMNQJW2WVTerzWI4CIIHV2nbEwu?= =?us-ascii?q?d76zQ9GZ1JX//tvx0qWbWx9Piju5bOE6BzSNhiKViPMrh5B/IL060BrDrygAUe?= =?us-ascii?q?1XwWR1OQDbxE6ktY+N5porzwB887JkrpYBApPBeL4kQLBiAT8nKzp9pJGz9EqL?= =?us-ascii?q?cQza/XYYU2MLgjJUEgPF61f8RZ63vSzk8qJm1SCcMdfxTLxxXTW58493WRToj2?= =?us-ascii?q?EBLDd//2bJzoRrgLlzvAOqpxs5xZXdJo6SKr42fLzWdMkGH0JdT81RUGpHGYr6?= =?us-ascii?q?YIwRS6IDMe9erpLVqFwJoh+zQwKrAbDB0DhN013/26whm8goFwDd0RI7HNMV+C?= =?us-ascii?q?DWoNzzNbw6WOa51q7JwXPIaPYAimS105TBbh10+aLEZrl3a8eEkhEi?= X-IPAS-Result: =?us-ascii?q?A2H0BABmrOtX/wHyM5BcHAEBBAEBCgEBFwEBBAEBCgEBgxQ?= =?us-ascii?q?BAQEBAR6BRQ66UB2HakwBAQEBAQEBAQIBAlsngjIEAwMVBTkQVQINYQEEAg8VE?= =?us-ascii?q?wYBAQwgCwECAwkCFwUkCAgDAS0DAQUBCwMVBwsFGAQBiCukXoEyPjKKVoUwAQE?= =?us-ascii?q?FiDIMARwIEIQXghCIahEBhV0dmXuPbYlWJYVvSI5fMYERVIMPDhyBUXGEaHiBK?= =?us-ascii?q?AEBAQ?= Received: from unknown (HELO tarius.tycho.ncsc.mil) ([144.51.242.1]) by emsm-gh1-uea11.nsa.gov with ESMTP; 28 Sep 2016 11:44:17 +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 u8SBiEwN005498; Wed, 28 Sep 2016 07:44:15 -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 u8SASkrE294772 for ; Wed, 28 Sep 2016 06:28:46 -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 u8SASj7f030021 for ; Wed, 28 Sep 2016 06:28:46 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A1BVAwCqmutXfzZSfUpcHAEBBAEBCgEBgz8BAQEBAYFjDrY/hBWGHoFjTAECAQEBAQECEwEBCQsLCReFFQEVFRkBATcBLGgBBQEOFRIiiCukZ4EyPjKKVoUwAQEFiDQpCBCEF4IQi2QLgmodmXuPbYlWhhRIjl8xgRGDYw4cgVFxhwcBAQE X-IPAS-Result: A1BVAwCqmutXfzZSfUpcHAEBBAEBCgEBgz8BAQEBAYFjDrY/hBWGHoFjTAECAQEBAQECEwEBCQsLCReFFQEVFRkBATcBLGgBBQEOFRIiiCukZ4EyPjKKVoUwAQEFiDQpCBCEF4IQi2QLgmodmXuPbYlWhhRIjl8xgRGDYw4cgVFxhwcBAQE X-IronPort-AV: E=Sophos;i="5.30,409,1470715200"; d="scan'208";a="5733090" Received: from emsm-gh1-uea10.corp.nsa.gov (HELO emsm-gh1-uea10.nsa.gov) ([10.208.41.36]) by goalie.tycho.ncsc.mil with ESMTP; 28 Sep 2016 06:28:44 -0400 IronPort-PHdr: =?us-ascii?q?9a23=3AoYVT2RUvqYmDuUSni/VmTiZrssjV8LGtZVwlr6E/?= =?us-ascii?q?grcLSJyIuqrYZxCAt8tkgFKBZ4jH8fUM07OQ6PG6HzVfqszd+Fk5M7V0Hycfjs?= =?us-ascii?q?sXmwFySOWkMmbcaMDQUiohAc5ZX0Vk9XzoeWJcGcL5ekGA6ibqtW1aJBzzOEJP?= =?us-ascii?q?K/jvHcaK1oLshrr0osKYOVkArQH+SIs6FA+xowTVu5teqqpZAYF19CH0pGBVcf?= =?us-ascii?q?9d32JiKAHbtR/94sCt4MwrqHwI6LoXzMlaTaj2RakzTKQCRHR6ayFmrPHs4ALO?= =?us-ascii?q?SQqJ+2s0TnQdkh0OBRPMqh79QNO5qSbzsOZn3yiWeMn3VqwcRSWp76AtTgThzi?= =?us-ascii?q?gAKXpx623MotBhh6Jc5hS6rlpwxJCQKIOKP/NkYvn1YcIRRW0HWN1YESNGHMf0?= =?us-ascii?q?bYcGCesaFelfqIb5qh0Fqh7tKxOrAbbNwzlJnTfa1Ks7yOk/CgLHx0R0Hd4Htn?= =?us-ascii?q?XPhNzzOL0VUOzzx67Nm2aQJ8hK0CvwvdCbOisqpuuBCO4ofA=3D=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0F2AwBZmutXfzZSfUpcHAEBBAEBCgEBF?= =?us-ascii?q?wEFAQsBgxUBAQEBAYFjDrY/hBWGHoFjTAEBAQEBAQEBAgECEAEBCQsLCRcxgjI?= =?us-ascii?q?KGjkQVQINYQEVFRkBATcBLGgBBQEOFRIiiCuka4EyPjKKVoUwAQEFiDQpCBCEF?= =?us-ascii?q?4IQi2QLgmodmXuPbYlWhhRIjl8xgRGDYw4cgVFxhwcBAQE?= X-IPAS-Result: =?us-ascii?q?A0F2AwBZmutXfzZSfUpcHAEBBAEBCgEBFwEFAQsBgxUBAQE?= =?us-ascii?q?BAYFjDrY/hBWGHoFjTAEBAQEBAQEBAgECEAEBCQsLCRcxgjIKGjkQVQINYQEVF?= =?us-ascii?q?RkBATcBLGgBBQEOFRIiiCuka4EyPjKKVoUwAQEFiDQpCBCEF4IQi2QLgmodmXu?= =?us-ascii?q?PbYlWhhRIjl8xgRGDYw4cgVFxhwcBAQE?= X-IronPort-AV: E=Sophos;i="5.30,409,1470700800"; d="scan'208";a="18041359" Received: from mail-wm0-f54.google.com ([74.125.82.54]) by emsm-gh1-uea10.nsa.gov with ESMTP/TLS/AES128-GCM-SHA256; 28 Sep 2016 10:28:43 +0000 Received: by mail-wm0-f54.google.com with SMTP id l132so63094771wmf.1 for ; Wed, 28 Sep 2016 03:28:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20120917; h=from:to:cc:subject:date:message-id; bh=++WzjRoOp8GxZm5m/w000D8/O9dph74tzoq/UJPOH14=; b=FPMuaUDA5K4X00DqLoaa+19nn2byC/efHtwf1T33mp6PMoe5UWY0nS9V7UjHSo/HSC rhsrVh4QQTB/Vi7QVBCMq5LlS1nrzgAhe8/CNzj5N5fFa8XSZZmbUKTfd36dynIyjbOc yDgQY+Rzlnk1Nh3JUDq/e/veGluqJalh1BT3U0QFWo0Taj0PYabm2ziepdcg9Z7XUP6F IDlal/5bjvXGgvPELQPfzN5fHDdQx4Sn45h9nhoU8CxI19ulRuddCWP0v37K7AmTZB1t 6hFkdrFJlXCjiTlHqzzo77cPVAQkqzefxMgAsR02bbpkR8G1Z2eqPyKAWYFwGRKY5PV6 7J6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=++WzjRoOp8GxZm5m/w000D8/O9dph74tzoq/UJPOH14=; b=Sce48SjG0r2+F996GB18FcyZwXbOW//1iDp5rAUiTpj0j6TMZY82PDqGxmYvDb02jC dgAquiauz/nI5MZ7BkMec3F10sGYeNHBuaWJzUtWnRRXk2NhsIedHpSdaSEU/BDcEJPE FOaoqxDY5OsL7BqeyxfcmmhTHh+eBTZErdioklYco7Mdut7hk370FZPnW6W5LoW/Gpwo oyuLRzKyl4+4NeB8R91aQmYCCU9/OZN3wBfOLIv0CMjDQ2RP2MQmcj7rgC73pl1HEBSO YPWiOEaNbBfHb7/UzNTVE8qLKm6uViZFmC4dNZ0SAuLfD94xu6C6qBAZUxHj5VJJ6kIw 3f2g== X-Gm-Message-State: AA6/9Rn37+Up5gYpt3W/eqhW0tv3bk4RIExFcwTtyP61FuSO97uzWY9K9RrCyat1PLNBGQ== X-Received: by 10.28.125.209 with SMTP id y200mr7579857wmc.25.1475058522109; Wed, 28 Sep 2016 03:28:42 -0700 (PDT) Received: from jdanis.lon.corp.google.com ([100.120.40.90]) by smtp.gmail.com with ESMTPSA id 123sm8036318wmj.5.2016.09.28.03.28.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 28 Sep 2016 03:28:41 -0700 (PDT) From: Janis Danisevskis To: selinux@tycho.nsa.gov, seandroid-list@tycho.nsa.gov, sds@tycho.nsa.gov, jwcart2@tycho.nsa.gov, jdanis@google.com Subject: [PATCH 1/3] libselinux: Add architecture string to file_context.bin Date: Wed, 28 Sep 2016 11:28:30 +0100 Message-Id: <1475058512-76102-1-git-send-email-jdanis@android.com> X-Mailer: git-send-email 2.8.0.rc3.226.g39d4020 X-Mailman-Approved-At: Wed, 28 Sep 2016 07:39:52 -0400 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: Cc: Janis Danisevskis MIME-Version: 1.0 Errors-To: selinux-bounces@tycho.nsa.gov Sender: "Selinux" X-Virus-Scanned: ClamAV using ClamSMTP Serialized precompiled regular expressins are architecture dependent when using PCRE2. This patch - bumps the SELINUX_COMPILED_FCONTEXT version to 5 and - adds a field to the output indicating the architecture compatibility. libselinux can cope with an architecture mismatch by ignoring the precompiled data in the input file and recompiling the regular expressions at runtime. It can also load older versions of file_contexts.bin if they where built with sefcontext_compile using the exact same version of the pcre1/2 as selinux. Signed-off-by: Janis Danisevskis --- libselinux/src/label_file.c | 43 +++++++++++++++++++++++++++++- libselinux/src/label_file.h | 4 ++- libselinux/src/regex.c | 50 ++++++++++++++++++++++++++++++++--- libselinux/src/regex.h | 15 ++++++++++- libselinux/utils/sefcontext_compile.c | 13 +++++++++ 5 files changed, 119 insertions(+), 6 deletions(-) diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c index 7156825..13e05c1 100644 --- a/libselinux/src/label_file.c +++ b/libselinux/src/label_file.c @@ -126,6 +126,8 @@ static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec, uint32_t i, magic, version; uint32_t entry_len, stem_map_len, regex_array_len; const char *reg_version; + const char *reg_arch; + char reg_arch_matches = 0; mmap_area = malloc(sizeof(*mmap_area)); if (!mmap_area) { @@ -159,6 +161,10 @@ static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec, if (!reg_version) return -1; + reg_arch = regex_arch_string(); + if (!reg_arch) + return -1; + if (version >= SELINUX_COMPILED_FCONTEXT_PCRE_VERS) { len = strlen(reg_version); @@ -188,7 +194,42 @@ static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec, return -1; } free(str_buf); + + if (version >= SELINUX_COMPILED_FCONTEXT_REGEX_ARCH) { + len = strlen(reg_arch); + + rc = next_entry(&entry_len, mmap_area, + sizeof(uint32_t)); + if (rc < 0) + return -1; + + /* Check arch string lengths */ + if (len != entry_len) { + /* + * Skip the entry and conclude that we have + * a mismatch, which is not fatal. + */ + next_entry(NULL, mmap_area, entry_len); + goto end_arch_check; + } + + /* Check if arch string mismatch */ + str_buf = malloc(entry_len + 1); + if (!str_buf) + return -1; + + rc = next_entry(str_buf, mmap_area, entry_len); + if (rc < 0) { + free(str_buf); + return -1; + } + + str_buf[entry_len] = '\0'; + reg_arch_matches = strcmp(str_buf, reg_arch) == 0; + free(str_buf); + } } +end_arch_check: /* allocate the stems_data array */ rc = next_entry(&stem_map_len, mmap_area, sizeof(uint32_t)); @@ -349,7 +390,7 @@ static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec, spec->prefix_len = prefix_len; } - rc = regex_load_mmap(mmap_area, &spec->regex); + rc = regex_load_mmap(mmap_area, &spec->regex, reg_arch_matches); if (rc < 0) goto out; diff --git a/libselinux/src/label_file.h b/libselinux/src/label_file.h index 88f4294..00c0a5c 100644 --- a/libselinux/src/label_file.h +++ b/libselinux/src/label_file.h @@ -24,8 +24,10 @@ #define SELINUX_COMPILED_FCONTEXT_PCRE_VERS 2 #define SELINUX_COMPILED_FCONTEXT_MODE 3 #define SELINUX_COMPILED_FCONTEXT_PREFIX_LEN 4 +#define SELINUX_COMPILED_FCONTEXT_REGEX_ARCH 5 -#define SELINUX_COMPILED_FCONTEXT_MAX_VERS SELINUX_COMPILED_FCONTEXT_PREFIX_LEN +#define SELINUX_COMPILED_FCONTEXT_MAX_VERS \ + SELINUX_COMPILED_FCONTEXT_REGEX_ARCH /* A file security context specification. */ struct spec { diff --git a/libselinux/src/regex.c b/libselinux/src/regex.c index 750088e..a3b427b 100644 --- a/libselinux/src/regex.c +++ b/libselinux/src/regex.c @@ -7,6 +7,44 @@ #include "label_file.h" #ifdef USE_PCRE2 +#define REGEX_ARCH_SIZE_T PCRE2_SIZE +#else +#define REGEX_ARCH_SIZE_T size_t +#endif + +#ifndef __BYTE_ORDER__ +#error __BYTE_ORDER__ not defined. Unable to determine endianness. +#endif + +#ifdef USE_PCRE2 +char const *regex_arch_string(void) +{ + static char arch_string_buffer[32]; + static char const *arch_string = ""; + char const *endianness = NULL; + int rc; + + if (arch_string[0] == '\0') { + if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) + endianness = "el"; + else if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) + endianness = "eb"; + + if (!endianness) + return NULL; + + rc = snprintf(arch_string_buffer, sizeof(arch_string_buffer), + "%zu-%zu-%s", sizeof(void *), + sizeof(REGEX_ARCH_SIZE_T), + endianness); + if (rc < 0) + abort(); + + arch_string = &arch_string_buffer[0]; + } + return arch_string; +} + struct regex_data { pcre2_code *regex; /* compiled regular expression */ /* @@ -56,7 +94,8 @@ char const *regex_version(void) return version_buf; } -int regex_load_mmap(struct mmap_area *mmap_area, struct regex_data **regex) +int regex_load_mmap(struct mmap_area *mmap_area, struct regex_data **regex, + int do_load_precompregex) { int rc; uint32_t entry_len; @@ -65,7 +104,7 @@ int regex_load_mmap(struct mmap_area *mmap_area, struct regex_data **regex) if (rc < 0) return -1; - if (entry_len) { + if (entry_len && do_load_precompregex) { /* * this should yield exactly one because we store one pattern at * a time @@ -195,6 +234,10 @@ int regex_cmp(struct regex_data *regex1, struct regex_data *regex2) } #else // !USE_PCRE2 +char const *regex_arch_string(void) +{ + return "N/A"; +} /* Prior to version 8.20, libpcre did not have pcre_free_study() */ #if (PCRE_MAJOR < 8 || (PCRE_MAJOR == 8 && PCRE_MINOR < 20)) @@ -247,7 +290,8 @@ char const *regex_version(void) return pcre_version(); } -int regex_load_mmap(struct mmap_area *mmap_area, struct regex_data **regex) +int regex_load_mmap(struct mmap_area *mmap_area, struct regex_data **regex, + int unused __attribute__((unused))) { int rc; uint32_t entry_len; diff --git a/libselinux/src/regex.h b/libselinux/src/regex.h index 810b3c5..186c5ec 100644 --- a/libselinux/src/regex.h +++ b/libselinux/src/regex.h @@ -34,6 +34,16 @@ struct regex_error_data { struct mmap_area; /** + * regex_arch_string return a string that represents the pointer width, the + * width of what the backend considers a size type, and the endianness of the + * system that this library was build for. (e.g. for x86_64: "8-8-el"). + * This is required when loading stored regular espressions. PCRE2 regular + * expressions are not portable across architectures that do not have a + * matching arch-string. + */ +char const *regex_arch_string(void) hidden; + +/** * regex_verison returns the version string of the underlying regular * regular expressions library. In the case of PCRE it just returns the * result of pcre_version(). In the case of PCRE2, the very first time this @@ -86,12 +96,15 @@ int regex_prepare_data(struct regex_data **regex, char const *pattern_string, * representation of the precompiled pattern. * @arg regex If successful, the structure returned through *regex was allocated * with regex_data_create and must be freed with regex_data_free. + * @arg do_load_precompregex If non-zero precompiled patterns get loaded from + * the mmap region (ignored by PCRE1 back-end). * * @retval 0 on success * @retval -1 on error */ int regex_load_mmap(struct mmap_area *map_area, - struct regex_data **regex) hidden; + struct regex_data **regex, + int do_load_precompregex) hidden; /** * This function stores a precompiled regular expression to a file. * In the case of PCRE, it just dumps the binary representation of the diff --git a/libselinux/utils/sefcontext_compile.c b/libselinux/utils/sefcontext_compile.c index 70853e7..d91db9a 100644 --- a/libselinux/utils/sefcontext_compile.c +++ b/libselinux/utils/sefcontext_compile.c @@ -103,6 +103,7 @@ static int write_binary_file(struct saved_data *data, int fd, uint32_t i; int rc; const char *reg_version; + const char *reg_arch; bin_file = fdopen(fd, "w"); if (!bin_file) { @@ -133,6 +134,18 @@ static int write_binary_file(struct saved_data *data, int fd, if (len != section_len) goto err; + /* write regex arch string */ + reg_arch = regex_arch_string(); + if (!reg_arch) + goto err; + section_len = strlen(reg_arch); + len = fwrite(§ion_len, sizeof(uint32_t), 1, bin_file); + if (len != 1) + goto err; + len = fwrite(reg_arch, sizeof(char), section_len, bin_file); + if (len != section_len) + goto err; + /* write the number of stems coming */ section_len = data->num_stems; len = fwrite(§ion_len, sizeof(uint32_t), 1, bin_file);