From patchwork Thu Sep 29 11:39:18 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Janis Danisevskis X-Patchwork-Id: 9356383 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 B0308600C8 for ; Thu, 29 Sep 2016 11:43:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9611529971 for ; Thu, 29 Sep 2016 11:43:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8A9EF29973; Thu, 29 Sep 2016 11:43:17 +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 2A0CD29971 for ; Thu, 29 Sep 2016 11:43:15 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.30,414,1470700800"; d="scan'208";a="19654332" IronPort-PHdr: =?us-ascii?q?9a23=3AactHIhcs3K8SM11UJI0BZK+4lGMj4u6mDksu8pMi?= =?us-ascii?q?zoh2WeGdxc++Zh7h7PlgxGXEQZ/co6odzbGH6ea5CCdbvt6oizMrSNR0TRgLiM?= =?us-ascii?q?EbzUQLIfWuLgnFFsPsdDEwB89YVVVorDmROElRH9viNRWJ+iXhpQAbFhi3Dwdp?= =?us-ascii?q?POO9QteU1JXtkbnjsMaMKyxzxxOFKYtoKxu3qQiD/uI3uqBFbpgL9x3Sv3FTcP?= =?us-ascii?q?5Xz247bXianhL7+9vitMU7q3cYhuglv/Jkfe26Ov1gDO9uNzM8L2053sDgtAGZ?= =?us-ascii?q?BUvTvjpPGlkRxwFFBwnD8QHSQob6siy8sPF0niadI4m+Vr09Xzi46KZnDRvpkz?= =?us-ascii?q?svKy8y8GaRjNd5yq1cvlbpvBBk65LFa4GScvxld+XSes1eDWheVc9LS3ZpHpK3?= =?us-ascii?q?b4xJCfEIe+lfsdrTvVwL+Dm3Awi3TM/mzD5Ti2Ht3qwhm7AjHQjA3RcIFtQCqn?= =?us-ascii?q?PVoJP+M6JEArP997XB0TiWN6Ae4jz68oWdKUgs?= X-IPAS-Result: =?us-ascii?q?A2FmBABq/exX/wHyM5BdGwEBAQMBAQEJAQEBFwEBBAEBCgE?= =?us-ascii?q?BgxQBAQEBAR6BU7pRIIdmTAEBAQEBAQEBAgECWyeCMgQDAxUFORBVAg1fAgEDA?= =?us-ascii?q?QIPFRMGAQEMIAsBAgMJAQEXKQgIAwEtAwEFAQsRBwcLBRgEAYgrBKIFgTI+Mop?= =?us-ascii?q?WhTABAQWBAYciAQEIAQEBARsIEIQXghCIaxEBhXoBjjmLQo9yiVglhW+PJzGBE?= =?us-ascii?q?VSDDw4cgVFxhSZ4gSgBAQE?= Received: from unknown (HELO tarius.tycho.ncsc.mil) ([144.51.242.1]) by emsm-gh1-uea11.nsa.gov with ESMTP; 29 Sep 2016 11:43:12 +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 u8TBh649026747; Thu, 29 Sep 2016 07:43:09 -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 u8TBeBeh019157 for ; Thu, 29 Sep 2016 07:40:11 -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 u8TBeAUv026602 for ; Thu, 29 Sep 2016 07:40:11 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A1B2AQBH/exXfylSfUpdHAEBBAEBCgEBgz8BAQEBAYFxtkOEFYYeAoFfTAECAQEBAQECEwEBCQsLCReFEwIBAxIVGQEBNwEPUTQBBQEcBxIiiCsEogWBMj4yilaFMAEBBYEBhyIBAQEBAQUCARwIEIQXghCLZQuDB446i0KPcolYhhSPJzGBEYNjDhyBUXGHRgEBAQ X-IPAS-Result: A1B2AQBH/exXfylSfUpdHAEBBAEBCgEBgz8BAQEBAYFxtkOEFYYeAoFfTAECAQEBAQECEwEBCQsLCReFEwIBAxIVGQEBNwEPUTQBBQEcBxIiiCsEogWBMj4yilaFMAEBBYEBhyIBAQEBAQUCARwIEIQXghCLZQuDB446i0KPcolYhhSPJzGBEYNjDhyBUXGHRgEBAQ X-IronPort-AV: E=Sophos;i="5.30,414,1470715200"; d="scan'208";a="5735897" 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; 29 Sep 2016 07:39:52 -0400 IronPort-PHdr: =?us-ascii?q?9a23=3AxvG8kBwTM1+kAGDXCy+O+j09IxM/srCxBDY+r6Qd?= =?us-ascii?q?0OgeIJqq85mqBkHD//Il1AaPBtSBrasVwLGJ+4nbGkU4qa6bt34DdJEeHzQksu?= =?us-ascii?q?4x2zIaPcieFEfgJ+TrZSFpVO5LVVti4m3peRMNQJW2WVTerzWI4CIIHV2nbEwu?= =?us-ascii?q?d76zQ9+Z1Jv//tvx0qWbWx9Piju5bOE6BzSNhiKViPMrh5B/IL060BrDrygAUe?= =?us-ascii?q?1XwWR1OQDbxE6ktY+Gx5l57ylcgf8q9tYaGeWmJ+VrBYBfWS8rN2Ez+d3DqQjI?= =?us-ascii?q?TQzJ4GAVFGoRjF4AGAXM6h3gWZb99y/7rfZVxDiRPcqwS6s9Hzul8eMjUxPzoD?= =?us-ascii?q?sWPD4+tmfMg4p/i7wf6BC+rBVl39T8fJCeNP04eLjUO9wdWzlvRMFUAg5IBIKn?= =?us-ascii?q?J6cGCesaMPhDrIjm7w8MoRK4BBKEDeXj0DhJijn92qhsgLdpKh3PwAF1R4FGi3?= =?us-ascii?q?/TttigaP8f?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0FZAQD4/OxXfylSfUpdHAEBBAEBCgEBF?= =?us-ascii?q?wEBBAEBCgEBgxQBAQEBAYFxtkOEFYYeAoFfTAEBAQEBAQEBAgECEAEBCQsLCRc?= =?us-ascii?q?xgjIKGjkQVQINXwIBAxIVGQEBNwEPUTQBBQEcBxIiiCsEogaBMj4yilaFMAEBB?= =?us-ascii?q?YEBhyMBAQEBAQUCARwIEIQXghCLZQuDB446i0KPcolYhhSPJzGBEYNjDhyBUXG?= =?us-ascii?q?HRgEBAQ?= X-IPAS-Result: =?us-ascii?q?A0FZAQD4/OxXfylSfUpdHAEBBAEBCgEBFwEBBAEBCgEBgxQ?= =?us-ascii?q?BAQEBAYFxtkOEFYYeAoFfTAEBAQEBAQEBAgECEAEBCQsLCRcxgjIKGjkQVQINX?= =?us-ascii?q?wIBAxIVGQEBNwEPUTQBBQEcBxIiiCsEogaBMj4yilaFMAEBBYEBhyMBAQEBAQU?= =?us-ascii?q?CARwIEIQXghCLZQuDB446i0KPcolYhhSPJzGBEYNjDhyBUXGHRgEBAQ?= X-IronPort-AV: E=Sophos;i="5.30,414,1470700800"; d="scan'208";a="18091076" Received: from mail-wm0-f41.google.com ([74.125.82.41]) by emsm-gh1-uea10.nsa.gov with ESMTP/TLS/AES128-GCM-SHA256; 29 Sep 2016 11:39:26 +0000 Received: by mail-wm0-f41.google.com with SMTP id b4so22267061wmb.0 for ; Thu, 29 Sep 2016 04:39:26 -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:in-reply-to:references; bh=OWHwuxfzRDzNeCXBACZf4wXEoYw8Snbn4bjKTYsl1BU=; b=Qk53TgKTrb1LKsuCcIPUv+tcv2Ehfig1ZiHANCMkobLHeV10/b6jJuq/iHeO+/Bmd5 hcLZfV0CgI/rK25iOGQrmrAl2u8znrNt+MjSGdZH5EvewAxa7wcOfTcKb10BJy1G7Tsw nCa3zrjV7obaYu0PW0SuaHyCWmh/Tr0d0vHxfqlyz4O1XcVVAQTigc2K5MiejLp1l14o WGwt2anib0wusYhcKRCFa7kru/GTFmqRogKpWXACF4H4vOGkRzS39NRB/lagUdXnRKug uDoxttWr1mImoSFZs7ADNcUT4/OyJHb7JmrQJ/cI+psYRB84DA8QwojttgTkLKeBJkE7 n6zg== 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:in-reply-to :references; bh=OWHwuxfzRDzNeCXBACZf4wXEoYw8Snbn4bjKTYsl1BU=; b=RnN2/SkLlzHjssbpj4afpvfgEqNTkhp4xXtJTh2A+BKebwRGRpLQnG3JaKZKZBVTHq QCdj8wnOPiH93sgpMSymAD1UtoJBVDiEx+iawFY3b/eXRTObJDIa/gWSACXiIw1GfSH7 MtRcQYMVF+TL6mwXbLDzUvSG4N+5JX/CU19n3Uv1TeKHe/Dg1F5kQefLxfLzxE7FWXvk PHPLvj9jBMdgHUEzyYfnXMH1KHcQchNHIoU7qGAssxOPRE2v9f8REFNmMewytMq0P/Fm gE357Q8hx/J/sgWlLU/t1xbwAmK1bAS5tg5Ev00dhfD+XtJeedNljoB00GYqgpYHouVa AXDQ== X-Gm-Message-State: AA6/9Rm4B9wcD4KLC4/Tz+dyo6+9jDbR7h9fpBqYqxJSWF1TbE0FGathBIIEWMfx6QlVRg== X-Received: by 10.28.52.210 with SMTP id b201mr2274743wma.32.1475149166018; Thu, 29 Sep 2016 04:39:26 -0700 (PDT) Received: from localhost.localdomain (cpc8-nmal18-2-0-cust977.croy.cable.virginm.net. [94.174.27.210]) by smtp.gmail.com with ESMTPSA id rk14sm13660190wjb.6.2016.09.29.04.39.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 29 Sep 2016 04:39:25 -0700 (PDT) From: Janis Danisevskis To: selinux@tycho.nsa.gov, seandroid-list@tycho.nsa.gov, sds@tycho.nsa.gov, jwcart2@tycho.nsa.gov Subject: [PATCH 2/3] libselinux: android: fix lax service context lookup Date: Thu, 29 Sep 2016 12:39:18 +0100 Message-Id: <1475149159-21757-2-git-send-email-jdanis@android.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1475149159-21757-1-git-send-email-jdanis@android.com> References: <1475149159-21757-1-git-send-email-jdanis@android.com> 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 We use the same lookup function for service contexts that we use for property contexts. However, property contexts are namespace based and only compare the prefix. This may lead to service associations with a wrong label. This patch introduces a new back end for android services with a stricter lookup function. Now the service name must match the key of the service label exactly. Signed-off-by: Janis Danisevskis --- libselinux/include/selinux/label.h | 2 ++ libselinux/src/label.c | 1 + libselinux/src/label_backends_android.c | 54 +++++++++++++++++++++++++++++++-- libselinux/src/label_internal.h | 3 ++ libselinux/utils/selabel_digest.c | 2 ++ libselinux/utils/selabel_lookup.c | 2 ++ 6 files changed, 62 insertions(+), 2 deletions(-) diff --git a/libselinux/include/selinux/label.h b/libselinux/include/selinux/label.h index f0b1e10..277287e 100644 --- a/libselinux/include/selinux/label.h +++ b/libselinux/include/selinux/label.h @@ -34,6 +34,8 @@ struct selabel_handle; #define SELABEL_CTX_DB 3 /* Android property service contexts */ #define SELABEL_CTX_ANDROID_PROP 4 +/* Android service contexts */ +#define SELABEL_CTX_ANDROID_SERVICE 5 /* * Available options diff --git a/libselinux/src/label.c b/libselinux/src/label.c index 96a4ff1..eb0e766 100644 --- a/libselinux/src/label.c +++ b/libselinux/src/label.c @@ -45,6 +45,7 @@ static selabel_initfunc initfuncs[] = { CONFIG_X_BACKEND(selabel_x_init), CONFIG_DB_BACKEND(selabel_db_init), &selabel_property_init, + &selabel_service_init, }; static void selabel_subs_fini(struct selabel_sub *ptr) diff --git a/libselinux/src/label_backends_android.c b/libselinux/src/label_backends_android.c index 290b438..4d6ec86 100644 --- a/libselinux/src/label_backends_android.c +++ b/libselinux/src/label_backends_android.c @@ -244,7 +244,7 @@ static void closef(struct selabel_handle *rec) free(data); } -static struct selabel_lookup_rec *lookup(struct selabel_handle *rec, +static struct selabel_lookup_rec *property_lookup(struct selabel_handle *rec, const char *key, int __attribute__((unused)) type) { @@ -279,6 +279,38 @@ finish: return ret; } +static struct selabel_lookup_rec *service_lookup(struct selabel_handle *rec, + const char *key, int __attribute__((unused)) type) +{ + struct saved_data *data = (struct saved_data *)rec->data; + spec_t *spec_arr = data->spec_arr; + unsigned int i; + struct selabel_lookup_rec *ret = NULL; + + if (!data->nspec) { + errno = ENOENT; + goto finish; + } + + for (i = 0; i < data->nspec; i++) { + if (strcmp(spec_arr[i].property_key, key) == 0) + break; + if (strcmp(spec_arr[i].property_key, "*") == 0) + break; + } + + if (i >= data->nspec) { + /* No matching specification. */ + errno = ENOENT; + goto finish; + } + + ret = &spec_arr[i].lr; + +finish: + return ret; +} + static void stats(struct selabel_handle __attribute__((unused)) *rec) { selinux_log(SELINUX_WARNING, "'stats' functionality not implemented.\n"); @@ -298,7 +330,25 @@ int selabel_property_init(struct selabel_handle *rec, rec->data = data; rec->func_close = &closef; rec->func_stats = &stats; - rec->func_lookup = &lookup; + rec->func_lookup = &property_lookup; + + return init(rec, opts, nopts); +} + +int selabel_service_init(struct selabel_handle *rec, + const struct selinux_opt *opts, unsigned nopts) +{ + struct saved_data *data; + + data = (struct saved_data *)malloc(sizeof(*data)); + if (!data) + return -1; + memset(data, 0, sizeof(*data)); + + rec->data = data; + rec->func_close = &closef; + rec->func_stats = &stats; + rec->func_lookup = &service_lookup; return init(rec, opts, nopts); } diff --git a/libselinux/src/label_internal.h b/libselinux/src/label_internal.h index 7c55531..6a9481a 100644 --- a/libselinux/src/label_internal.h +++ b/libselinux/src/label_internal.h @@ -39,6 +39,9 @@ int selabel_db_init(struct selabel_handle *rec, int selabel_property_init(struct selabel_handle *rec, const struct selinux_opt *opts, unsigned nopts) hidden; +int selabel_service_init(struct selabel_handle *rec, + const struct selinux_opt *opts, + unsigned nopts) hidden; /* * Labeling internal structures diff --git a/libselinux/utils/selabel_digest.c b/libselinux/utils/selabel_digest.c index 38162a5..e4d84a5 100644 --- a/libselinux/utils/selabel_digest.c +++ b/libselinux/utils/selabel_digest.c @@ -92,6 +92,8 @@ int main(int argc, char **argv) backend = SELABEL_CTX_DB; } else if (!strcmp(optarg, "prop")) { backend = SELABEL_CTX_ANDROID_PROP; + } else if (!strcmp(optarg, "service")) { + backend = SELABEL_CTX_ANDROID_SERVICE; } else { fprintf(stderr, "Unknown backend: %s\n", optarg); diff --git a/libselinux/utils/selabel_lookup.c b/libselinux/utils/selabel_lookup.c index d0b1457..b678a89 100644 --- a/libselinux/utils/selabel_lookup.c +++ b/libselinux/utils/selabel_lookup.c @@ -57,6 +57,8 @@ int main(int argc, char **argv) backend = SELABEL_CTX_DB; } else if (!strcmp(optarg, "prop")) { backend = SELABEL_CTX_ANDROID_PROP; + } else if (!strcmp(optarg, "service")) { + backend = SELABEL_CTX_ANDROID_SERVICE; } else { fprintf(stderr, "Unknown backend: %s\n", optarg);