From patchwork Wed Dec 19 21:33:37 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Stoppa X-Patchwork-Id: 10738219 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7117B6C5 for ; Wed, 19 Dec 2018 21:36:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 629AC286A4 for ; Wed, 19 Dec 2018 21:36:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 54999286BF; Wed, 19 Dec 2018 21:36:32 +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=-5.0 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id C307E286A4 for ; Wed, 19 Dec 2018 21:36:30 +0000 (UTC) Received: (qmail 1257 invoked by uid 550); 19 Dec 2018 21:34:36 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 1175 invoked from network); 19 Dec 2018 21:34:35 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=nFnYEqhQKjGId61gpPa5/uxgrXQl8+JI/cWK8VDpS5Y=; b=PrHzwn0tF5cZJ8rUcPtjyhBB7fZbNtctJAOvNrx1Oxs6IMttMC5N2zeIsjKNeWRc2R Tcb/3vkEBkE2YnLCKxy9k9DbdgE13xmPle3IC+iMSLMIvuyesjXYTBm8xAwyVIa2AGcH /beeel78v6AhNyt9ytcSLTURukyHaSXKh/ZGctqzdCRkOSX03aZjHTlPJUUD4eH/jCbA x5db8A4YA9MP5Pd45shyIQ4fsqljhgcbJ6WilKnFlCSamAsG00YPl93aOk2b33ZoBRhO 5vMcOFPsCVitBtYv9jQVk1ZaWS2BCUou741tRiulXeuCPZlWtNKjx/KJnY369bIvBg7O zg5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=nFnYEqhQKjGId61gpPa5/uxgrXQl8+JI/cWK8VDpS5Y=; b=JvyoF6C3lt10L0wmpUM0H1YzCkaK/DtjUiYB5HV2buVmRuiFBjoanD1JOzxHvtaCdE toDn0Ex4yRAlwsVB1Dhmk0jFOh++k+tQTP4lcZ2iNH3ZSJxxxDitEwlnfwRlFxrEZDRV nkuLXZq8wychmeC7JZLrq+cgdQPOjaj09uMtInu/yU/CIdnXYsKS0JciNT1yYWBvnIyL puW157s3bJ/6+4FOS4HroYK9r8nzsEo6eLs/Yzl6hkYxXsZD6IeV39D8NBWH1lbP2wnO rfM7HnkAwFJxzzPPtxmWCiWxp3f3igu6q5YZKxbtYAJ2NfdyG9kuivg/KC9be0Lgbkq0 QmeA== X-Gm-Message-State: AA+aEWYR20746MYkgRlX52HsqWcMJ7N/HxTSIEVizv68CNNRycdNai5P m1yBHcfQt2gAZ8NwRo4Hmgo= X-Google-Smtp-Source: AFSGD/WDSV/bqitpFVUBqXDWqTMforPVSVFRSbH5BBwX6cUs0/Ablg68IJ90z+EsAlf/wTbs5JI07g== X-Received: by 2002:a19:a149:: with SMTP id k70mr13332526lfe.5.1545255264592; Wed, 19 Dec 2018 13:34:24 -0800 (PST) From: Igor Stoppa X-Google-Original-From: Igor Stoppa To: Andy Lutomirski , Matthew Wilcox , Peter Zijlstra , Dave Hansen , Mimi Zohar Cc: igor.stoppa@huawei.com, Nadav Amit , Kees Cook , linux-integrity@vger.kernel.org, kernel-hardening@lists.openwall.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 11/12] IMA: turn ima_policy_flags into __wr_after_init Date: Wed, 19 Dec 2018 23:33:37 +0200 Message-Id: <20181219213338.26619-12-igor.stoppa@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181219213338.26619-1-igor.stoppa@huawei.com> References: <20181219213338.26619-1-igor.stoppa@huawei.com> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP The policy flags could be targeted by an attacker aiming at disabling IMA, so that there would be no trace of a file system modification in the measurement list. Since the flags can be altered at runtime, it is not possible to make them become fully read-only, for example with __ro_after_init. __wr_after_init can still provide some protection, at least against simple memory overwrite attacks Signed-off-by: Igor Stoppa CC: Andy Lutomirski CC: Nadav Amit CC: Matthew Wilcox CC: Peter Zijlstra CC: Kees Cook CC: Dave Hansen CC: Mimi Zohar CC: linux-integrity@vger.kernel.org CC: kernel-hardening@lists.openwall.com CC: linux-mm@kvack.org CC: linux-kernel@vger.kernel.org --- security/integrity/ima/ima.h | 3 ++- security/integrity/ima/ima_init.c | 5 +++-- security/integrity/ima/ima_policy.c | 9 +++++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h index cc12f3449a72..297c25f5122e 100644 --- a/security/integrity/ima/ima.h +++ b/security/integrity/ima/ima.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include "../integrity.h" @@ -50,7 +51,7 @@ enum tpm_pcrs { TPM_PCR0 = 0, TPM_PCR8 = 8 }; #define IMA_TEMPLATE_IMA_FMT "d|n" /* current content of the policy */ -extern int ima_policy_flag; +extern int ima_policy_flag __wr_after_init; /* set during initialization */ extern int ima_hash_algo; diff --git a/security/integrity/ima/ima_init.c b/security/integrity/ima/ima_init.c index 59d834219cd6..5f4e13e671bf 100644 --- a/security/integrity/ima/ima_init.c +++ b/security/integrity/ima/ima_init.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "ima.h" @@ -98,9 +99,9 @@ void __init ima_load_x509(void) { int unset_flags = ima_policy_flag & IMA_APPRAISE; - ima_policy_flag &= ~unset_flags; + wr_assign(ima_policy_flag, ima_policy_flag & ~unset_flags); integrity_load_x509(INTEGRITY_KEYRING_IMA, CONFIG_IMA_X509_PATH); - ima_policy_flag |= unset_flags; + wr_assign(ima_policy_flag, ima_policy_flag | unset_flags); } #endif diff --git a/security/integrity/ima/ima_policy.c b/security/integrity/ima/ima_policy.c index 7489cb7de6dc..2004de818d92 100644 --- a/security/integrity/ima/ima_policy.c +++ b/security/integrity/ima/ima_policy.c @@ -47,7 +47,7 @@ #define INVALID_PCR(a) (((a) < 0) || \ (a) >= (FIELD_SIZEOF(struct integrity_iint_cache, measured_pcrs) * 8)) -int ima_policy_flag; +int ima_policy_flag __wr_after_init; static int temp_ima_appraise; static int build_ima_appraise __ro_after_init; @@ -452,12 +452,13 @@ void ima_update_policy_flag(void) list_for_each_entry(entry, ima_rules, list) { if (entry->action & IMA_DO_MASK) - ima_policy_flag |= entry->action; + wr_assign(ima_policy_flag, + ima_policy_flag | entry->action); } ima_appraise |= (build_ima_appraise | temp_ima_appraise); if (!ima_appraise) - ima_policy_flag &= ~IMA_APPRAISE; + wr_assign(ima_policy_flag, ima_policy_flag & ~IMA_APPRAISE); } static int ima_appraise_flag(enum ima_hooks func) @@ -574,7 +575,7 @@ void ima_update_policy(void) list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu); if (ima_rules != policy) { - ima_policy_flag = 0; + wr_assign(ima_policy_flag, 0); ima_rules = policy; } ima_update_policy_flag();