From patchwork Wed Aug 26 13:59:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ondrej Mosnacek X-Patchwork-Id: 11738505 X-Patchwork-Delegate: paul@paul-moore.com Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5B89C722 for ; Wed, 26 Aug 2020 14:05:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 446E8214F1 for ; Wed, 26 Aug 2020 14:05:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZQYh64yb" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726187AbgHZOF0 (ORCPT ); Wed, 26 Aug 2020 10:05:26 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:45804 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726763AbgHZOFW (ORCPT ); Wed, 26 Aug 2020 10:05:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1598450720; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+s6uiJQMkK1JRqK55TUXmwD7WclQNed8wLU6Nw1EONE=; b=ZQYh64ybcFz0ggizVzBVAw19/9KgU3JHrOnCNUfBidezxE2uubt/ECvuFYf2Td0+YwxDVI NLK9HPgo6Uvr2l95AmyWjTeYFrccLFIguIbQbSff7Hx3oyEKWVvkd55SCMejb9+UvzhgqQ /JTfit+ZOAmWlbW4D4xSUKX+NoQHQsA= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-74-O-J3oDCnNt-u7avASEmstw-1; Wed, 26 Aug 2020 09:59:10 -0400 X-MC-Unique: O-J3oDCnNt-u7avASEmstw-1 Received: by mail-wm1-f70.google.com with SMTP id p184so804078wmp.7 for ; Wed, 26 Aug 2020 06:59:10 -0700 (PDT) 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:mime-version:content-transfer-encoding; bh=+s6uiJQMkK1JRqK55TUXmwD7WclQNed8wLU6Nw1EONE=; b=X/E+eecoIsQHBqhhYbMiMQynXtNNrATNEFzfE0UwXdNbdiZ7gdMfJVQpph0KeCwKaR hC00uXQfqd/YkFrX1ed1s0wMGREbuSMzLgQmsKvhVZdodiTTig2hAaahg2lYGghEVnJO KX6EcEXv1Zn/Z8s4oE6fdQOQNtR0wyB9kaTDa+NfP4FdIyqJOiC8+ofgrQcmUEQ69HEO JGBYSKYq/oQdKlE8WrvFiOv/KCPZxLHzEvO/TIPYgpVNAs5dZ3tyoRYChJwNEj49HB++ MJb9UXjki3YalRXFDupz6cqvxMyrEOm/kjpuFyqitXy91wsK7YEf5nXf2yGa8f6FZpMM Mv/w== X-Gm-Message-State: AOAM533exvLjhMdbnf5FBxSUU2sjX3diWIYOS4wvRkecxcByRdeZJgn+ uk+mjmNfxdWdB0XxaLchcHutuazdpbzHT5UixAaenH72UxSBEF36jWjDKIiUMjlesaSQdFVYwWW 5d7ryXNv0Kvi+FNZCIw== X-Received: by 2002:a7b:c20a:: with SMTP id x10mr7792036wmi.177.1598450349097; Wed, 26 Aug 2020 06:59:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzzhF6VROxhTtu0r31ScTKYKjrgU0fAFvKV5NmrA7+7y+QAW+LYviqM38kdzpbde6it3TzJcA== X-Received: by 2002:a7b:c20a:: with SMTP id x10mr7792014wmi.177.1598450348874; Wed, 26 Aug 2020 06:59:08 -0700 (PDT) Received: from omos.redhat.com ([2a02:8308:b103:4000:e83d:a4fb:e589:6902]) by smtp.gmail.com with ESMTPSA id y24sm5622325wmi.17.2020.08.26.06.59.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Aug 2020 06:59:08 -0700 (PDT) From: Ondrej Mosnacek To: selinux@vger.kernel.org, Paul Moore Cc: Stephen Smalley Subject: [PATCH v2 1/3] selinux: simplify away security_policydb_len() Date: Wed, 26 Aug 2020 15:59:04 +0200 Message-Id: <20200826135906.1912186-2-omosnace@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200826135906.1912186-1-omosnace@redhat.com> References: <20200826135906.1912186-1-omosnace@redhat.com> MIME-Version: 1.0 Sender: selinux-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Remove the security_policydb_len() calls from sel_open_policy() and instead update the inode size from the size returned from security_read_policy(). Since after this change security_policydb_len() is only called from security_load_policy(), remove it entirely and just open-code it there. Also, since security_load_policy() is always called with fsi->mutex held, make it dereference the policy pointer directly and drop the unnecessary RCU locking. Signed-off-by: Ondrej Mosnacek Acked-by: Stephen Smalley --- security/selinux/include/security.h | 1 - security/selinux/selinuxfs.c | 12 +++++------ security/selinux/ss/services.c | 32 ++++++++--------------------- 3 files changed, 15 insertions(+), 30 deletions(-) diff --git a/security/selinux/include/security.h b/security/selinux/include/security.h index 505e51264d51c..839774929a10d 100644 --- a/security/selinux/include/security.h +++ b/security/selinux/include/security.h @@ -218,7 +218,6 @@ void selinux_policy_cancel(struct selinux_state *state, struct selinux_policy *policy); int security_read_policy(struct selinux_state *state, void **data, size_t *len); -size_t security_policydb_len(struct selinux_state *state); int security_policycap_supported(struct selinux_state *state, unsigned int req_cap); diff --git a/security/selinux/selinuxfs.c b/security/selinux/selinuxfs.c index d1872adf0c474..fc44c4b8c0692 100644 --- a/security/selinux/selinuxfs.c +++ b/security/selinux/selinuxfs.c @@ -417,16 +417,16 @@ static int sel_open_policy(struct inode *inode, struct file *filp) if (!plm) goto err; - if (i_size_read(inode) != security_policydb_len(state)) { - inode_lock(inode); - i_size_write(inode, security_policydb_len(state)); - inode_unlock(inode); - } - rc = security_read_policy(state, &plm->data, &plm->len); if (rc) goto err; + if ((size_t)i_size_read(inode) != plm->len) { + inode_lock(inode); + i_size_write(inode, plm->len); + inode_unlock(inode); + } + fsi->policy_opened = 1; filp->private_data = plm; diff --git a/security/selinux/ss/services.c b/security/selinux/ss/services.c index 8381614627569..7cc2f7486c18f 100644 --- a/security/selinux/ss/services.c +++ b/security/selinux/ss/services.c @@ -2331,22 +2331,6 @@ err: return rc; } -size_t security_policydb_len(struct selinux_state *state) -{ - struct selinux_policy *policy; - size_t len; - - if (!selinux_initialized(state)) - return 0; - - rcu_read_lock(); - policy = rcu_dereference(state->policy); - len = policy->policydb.len; - rcu_read_unlock(); - - return len; -} - /** * security_port_sid - Obtain the SID for a port. * @protocol: protocol number @@ -3912,11 +3896,17 @@ int security_read_policy(struct selinux_state *state, int rc; struct policy_file fp; - if (!selinux_initialized(state)) + /* + * NOTE: We do not need to take the rcu read lock + * around the code below because other policy-modifying + * operations are already excluded by selinuxfs via + * fsi->mutex. + */ + policy = rcu_dereference_check(state->policy, 1); + if (!policy) return -EINVAL; - *len = security_policydb_len(state); - + *len = policy->policydb.len; *data = vmalloc_user(*len); if (!*data) return -ENOMEM; @@ -3924,11 +3914,7 @@ int security_read_policy(struct selinux_state *state, fp.data = *data; fp.len = *len; - rcu_read_lock(); - policy = rcu_dereference(state->policy); rc = policydb_write(&policy->policydb, &fp); - rcu_read_unlock(); - if (rc) return rc; From patchwork Wed Aug 26 13:59:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ondrej Mosnacek X-Patchwork-Id: 11738501 X-Patchwork-Delegate: paul@paul-moore.com Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D039014F6 for ; Wed, 26 Aug 2020 14:05:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B40032075E for ; Wed, 26 Aug 2020 14:05:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="QnaX7iZn" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726700AbgHZOFX (ORCPT ); Wed, 26 Aug 2020 10:05:23 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:39298 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726753AbgHZOFV (ORCPT ); Wed, 26 Aug 2020 10:05:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1598450719; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8I1FDq5khX2w4its7kMgBnLds7+BJaMx39bHYbGDc7Q=; b=QnaX7iZnGTihdB+jA8IbwrWBUxf61KqOzc82ejIKKZ7SAc0mITVD/SkZDUKMbbjRcvg0yD AFBwRt/MDw+YSL+fwrSf7VESNT6Ay63BgkoYMGX5K0JNVPzIHZNnYKPEauhq14D6tgZ7SR NjmXVhoSAnLoQAk/PZ64ZGHzXbFpkdY= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-87-WOgzRS6oP-uDVb_BWwDloA-1; Wed, 26 Aug 2020 09:59:11 -0400 X-MC-Unique: WOgzRS6oP-uDVb_BWwDloA-1 Received: by mail-wm1-f71.google.com with SMTP id d22so802493wmd.2 for ; Wed, 26 Aug 2020 06:59:11 -0700 (PDT) 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:mime-version:content-transfer-encoding; bh=8I1FDq5khX2w4its7kMgBnLds7+BJaMx39bHYbGDc7Q=; b=cdt165MAcXNc2GKpFFJpWzgoOidd7QRbdmPSnNka1D9e+tBYsuxqEx+Rs2x7RovboB x/C8oUFde+dIpnVJ0JhtmjVel/RwmwL94HeP3RAdH/U146mrmp48Sl0kq7VWXbfDF7SL 4h9onAr5gY+ZI/7vlbFDJ3hY/vVQma3k1bv1MFLcF7wcRF9Zv0sKduki2I5e4oAuBVT3 M7ZbM23OSYq4LtHpcTdp7WNJgPCn4iGQ8TopoJgaR/IrUH0YLckXLpLxyNJWYl4Aputv wO7y59U2aSW8rBLMM3+LSb0cYCWNHLbV1oCqW3ytveOQZEy6cEu9HvunIP7CnLLLjEq9 3nsw== X-Gm-Message-State: AOAM532EAlmVEJzMOS/ZSma4uK2HnfZqwcSM/w6SHPUwJBO7GkP6RNLk iSWc6ooE6eMJcKQgM2l7akmPdKY1jlLiwAeV3s6KE7zbSPWhQ/vRchv3d7kk+6nWGIutnVrqSZ1 hZphAdHaTzTviBGbkvQ== X-Received: by 2002:a1c:dd85:: with SMTP id u127mr7669983wmg.65.1598450349932; Wed, 26 Aug 2020 06:59:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzbGM15Url2QLnlSlqk7h9+cfr8kdkFuWpOw6G0qFnXiz7UnU8y221Ze4SbNft0vy21k8SRDA== X-Received: by 2002:a1c:dd85:: with SMTP id u127mr7669960wmg.65.1598450349674; Wed, 26 Aug 2020 06:59:09 -0700 (PDT) Received: from omos.redhat.com ([2a02:8308:b103:4000:e83d:a4fb:e589:6902]) by smtp.gmail.com with ESMTPSA id y24sm5622325wmi.17.2020.08.26.06.59.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Aug 2020 06:59:09 -0700 (PDT) From: Ondrej Mosnacek To: selinux@vger.kernel.org, Paul Moore Cc: Stephen Smalley Subject: [PATCH v2 2/3] selinux: eliminate the redundant policycap array Date: Wed, 26 Aug 2020 15:59:05 +0200 Message-Id: <20200826135906.1912186-3-omosnace@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200826135906.1912186-1-omosnace@redhat.com> References: <20200826135906.1912186-1-omosnace@redhat.com> MIME-Version: 1.0 Sender: selinux-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org The policycap array in struct selinux_state is redundant and can be substituted by calling security_policycap_supported(). Signed-off-by: Ondrej Mosnacek --- security/selinux/include/security.h | 42 ++++++++++++----------------- security/selinux/ss/services.c | 27 ------------------- 2 files changed, 17 insertions(+), 52 deletions(-) diff --git a/security/selinux/include/security.h b/security/selinux/include/security.h index 839774929a10d..9ab8f8da47812 100644 --- a/security/selinux/include/security.h +++ b/security/selinux/include/security.h @@ -96,7 +96,6 @@ struct selinux_state { #endif bool checkreqprot; bool initialized; - bool policycap[__POLICYDB_CAPABILITY_MAX]; struct page *status_page; struct mutex status_lock; @@ -159,53 +158,49 @@ static inline bool selinux_disabled(struct selinux_state *state) } #endif +int security_policycap_supported(struct selinux_state *state, + unsigned int req_cap); + static inline bool selinux_policycap_netpeer(void) { - struct selinux_state *state = &selinux_state; - - return state->policycap[POLICYDB_CAPABILITY_NETPEER]; + return security_policycap_supported(&selinux_state, + POLICYDB_CAPABILITY_NETPEER); } static inline bool selinux_policycap_openperm(void) { - struct selinux_state *state = &selinux_state; - - return state->policycap[POLICYDB_CAPABILITY_OPENPERM]; + return security_policycap_supported(&selinux_state, + POLICYDB_CAPABILITY_OPENPERM); } static inline bool selinux_policycap_extsockclass(void) { - struct selinux_state *state = &selinux_state; - - return state->policycap[POLICYDB_CAPABILITY_EXTSOCKCLASS]; + return security_policycap_supported(&selinux_state, + POLICYDB_CAPABILITY_EXTSOCKCLASS); } static inline bool selinux_policycap_alwaysnetwork(void) { - struct selinux_state *state = &selinux_state; - - return state->policycap[POLICYDB_CAPABILITY_ALWAYSNETWORK]; + return security_policycap_supported(&selinux_state, + POLICYDB_CAPABILITY_ALWAYSNETWORK); } static inline bool selinux_policycap_cgroupseclabel(void) { - struct selinux_state *state = &selinux_state; - - return state->policycap[POLICYDB_CAPABILITY_CGROUPSECLABEL]; + return security_policycap_supported(&selinux_state, + POLICYDB_CAPABILITY_CGROUPSECLABEL); } static inline bool selinux_policycap_nnp_nosuid_transition(void) { - struct selinux_state *state = &selinux_state; - - return state->policycap[POLICYDB_CAPABILITY_NNP_NOSUID_TRANSITION]; + return security_policycap_supported(&selinux_state, + POLICYDB_CAPABILITY_NNP_NOSUID_TRANSITION); } static inline bool selinux_policycap_genfs_seclabel_symlinks(void) { - struct selinux_state *state = &selinux_state; - - return state->policycap[POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS]; + return security_policycap_supported(&selinux_state, + POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS); } int security_mls_enabled(struct selinux_state *state); @@ -219,9 +214,6 @@ void selinux_policy_cancel(struct selinux_state *state, int security_read_policy(struct selinux_state *state, void **data, size_t *len); -int security_policycap_supported(struct selinux_state *state, - unsigned int req_cap); - #define SEL_VEC_MAX 32 struct av_decision { u32 allowed; diff --git a/security/selinux/ss/services.c b/security/selinux/ss/services.c index 7cc2f7486c18f..e82a2cfe171f3 100644 --- a/security/selinux/ss/services.c +++ b/security/selinux/ss/services.c @@ -2113,30 +2113,6 @@ bad: return 0; } -static void security_load_policycaps(struct selinux_state *state, - struct selinux_policy *policy) -{ - struct policydb *p; - unsigned int i; - struct ebitmap_node *node; - - p = &policy->policydb; - - for (i = 0; i < ARRAY_SIZE(state->policycap); i++) - state->policycap[i] = ebitmap_get_bit(&p->policycaps, i); - - for (i = 0; i < ARRAY_SIZE(selinux_policycap_names); i++) - pr_info("SELinux: policy capability %s=%d\n", - selinux_policycap_names[i], - ebitmap_get_bit(&p->policycaps, i)); - - ebitmap_for_each_positive_bit(&p->policycaps, node, i) { - if (i >= ARRAY_SIZE(selinux_policycap_names)) - pr_info("SELinux: unknown policy capability %u\n", - i); - } -} - static int security_preserve_bools(struct selinux_policy *oldpolicy, struct selinux_policy *newpolicy); @@ -2218,9 +2194,6 @@ void selinux_policy_commit(struct selinux_state *state, /* Install the new policy. */ rcu_assign_pointer(state->policy, newpolicy); - /* Load the policycaps from the new policy */ - security_load_policycaps(state, newpolicy); - if (!selinux_initialized(state)) { /* * After first policy load, the security server is From patchwork Wed Aug 26 13:59:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ondrej Mosnacek X-Patchwork-Id: 11738503 X-Patchwork-Delegate: paul@paul-moore.com Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 24B9417C7 for ; Wed, 26 Aug 2020 14:05:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0ABEE214F1 for ; Wed, 26 Aug 2020 14:05:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="KJL7XzxI" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726514AbgHZOFZ (ORCPT ); Wed, 26 Aug 2020 10:05:25 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:60529 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726187AbgHZOFV (ORCPT ); Wed, 26 Aug 2020 10:05:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1598450719; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=l655GAyOHBeTwpCXtasjAikdbXD6mqUcm1R/KoxGFRY=; b=KJL7XzxIGx1f9Ywxqf3CozzuV3KC6lhd/5DUywc62MEHZmybvEB+o1RKL/BXsLXtioJu8i VifoFp8hTOeCk2UTZFcNDaVhVca05wWvCYEmAvuDKJ0b2SO/lL2TFW2ePFf2H8zos0omq4 xVY6Uome4tDpcxEZXorjGM40zScPcAM= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-421-P-b8DKVyNbqOF2l_zroiUQ-1; Wed, 26 Aug 2020 09:59:12 -0400 X-MC-Unique: P-b8DKVyNbqOF2l_zroiUQ-1 Received: by mail-wm1-f69.google.com with SMTP id f125so813766wma.3 for ; Wed, 26 Aug 2020 06:59:12 -0700 (PDT) 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:mime-version:content-transfer-encoding; bh=l655GAyOHBeTwpCXtasjAikdbXD6mqUcm1R/KoxGFRY=; b=ATv5kyReRgw4sYa9OIHATZpWHm/ugHctMCM+JVKAp301Z5+BvX84wgKDi9KbVm4jV/ QpdFmHRfJNm/uVBuFs2hxPg0oL0qC2VLfjfCBYsTV+0LjfjS69OfNriGSz21P6zD0Fmn i+7LOvfhupmYJkR4ZbXW1tXtsm0tPySqKGQuSAHo8+p3of5rFH4ePc7ZFhEyY2FTDHPj ArmU7Wc86lQuBwDkAZrkH0MWGpZ1+l7GgmY2wGrJjm0HRNhDvdg+bVVab6D4Zf4wJXPR 0P35EDsG7MOUvq+JCR9h5/1jwHpqsJvXbP0tVs6UbQBluRBPpcsNH4wG9OyShU+aZ6Ej y8gQ== X-Gm-Message-State: AOAM532i9FCXUA0jzEl3CE+meBs1zGutx7PMpJzOZYLYHgoi8O7HFE1H T9REtL+TMzSp0p7vASdZCLoYtUEzRMZFjSceEEfTl54+eTGsW/gPby8yQMtabUMP5bWHZiu2H8S +Ef5pmuJzpcLpZlHWwQ== X-Received: by 2002:a1c:7c0d:: with SMTP id x13mr7002780wmc.14.1598450351201; Wed, 26 Aug 2020 06:59:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzYmh5Vlj6euprpTFSDwOJuMI8kABwjevNrabVqh3H+bX1LbeSKpIPqL4WUmay8QavSdWV2KA== X-Received: by 2002:a1c:7c0d:: with SMTP id x13mr7002754wmc.14.1598450350871; Wed, 26 Aug 2020 06:59:10 -0700 (PDT) Received: from omos.redhat.com ([2a02:8308:b103:4000:e83d:a4fb:e589:6902]) by smtp.gmail.com with ESMTPSA id y24sm5622325wmi.17.2020.08.26.06.59.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Aug 2020 06:59:10 -0700 (PDT) From: Ondrej Mosnacek To: selinux@vger.kernel.org, Paul Moore Cc: Stephen Smalley Subject: [PATCH v2 3/3] selinux: remove the 'initialized' flag from selinux_state Date: Wed, 26 Aug 2020 15:59:06 +0200 Message-Id: <20200826135906.1912186-4-omosnace@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200826135906.1912186-1-omosnace@redhat.com> References: <20200826135906.1912186-1-omosnace@redhat.com> MIME-Version: 1.0 Sender: selinux-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org After the RCU conversion, it is possible to simply check the policy pointer against NULL instead. Signed-off-by: Ondrej Mosnacek Acked-by: Stephen Smalley --- security/selinux/include/security.h | 10 +--------- security/selinux/ss/services.c | 26 ++++++++++---------------- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/security/selinux/include/security.h b/security/selinux/include/security.h index 9ab8f8da47812..714c389cc72a0 100644 --- a/security/selinux/include/security.h +++ b/security/selinux/include/security.h @@ -95,7 +95,6 @@ struct selinux_state { bool enforcing; #endif bool checkreqprot; - bool initialized; struct page *status_page; struct mutex status_lock; @@ -110,14 +109,7 @@ extern struct selinux_state selinux_state; static inline bool selinux_initialized(const struct selinux_state *state) { - /* do a synchronized load to avoid race conditions */ - return smp_load_acquire(&state->initialized); -} - -static inline void selinux_mark_initialized(struct selinux_state *state) -{ - /* do a synchronized write to avoid race conditions */ - smp_store_release(&state->initialized, true); + return rcu_access_pointer(state->policy) != NULL; } #ifdef CONFIG_SECURITY_SELINUX_DEVELOP diff --git a/security/selinux/ss/services.c b/security/selinux/ss/services.c index e82a2cfe171f3..112ca3d9834d7 100644 --- a/security/selinux/ss/services.c +++ b/security/selinux/ss/services.c @@ -2118,9 +2118,6 @@ static int security_preserve_bools(struct selinux_policy *oldpolicy, static void selinux_policy_free(struct selinux_policy *policy) { - if (!policy) - return; - policydb_destroy(&policy->policydb); sidtab_destroy(policy->sidtab); kfree(policy->sidtab); @@ -2194,20 +2191,19 @@ void selinux_policy_commit(struct selinux_state *state, /* Install the new policy. */ rcu_assign_pointer(state->policy, newpolicy); - if (!selinux_initialized(state)) { + if (!oldpolicy) { /* * After first policy load, the security server is * marked as initialized and ready to handle requests and * any objects created prior to policy load are then labeled. */ - selinux_mark_initialized(state); selinux_complete_init(); + } else { + /* Free the old policy */ + synchronize_rcu(); + selinux_policy_free(oldpolicy); } - /* Free the old policy */ - synchronize_rcu(); - selinux_policy_free(oldpolicy); - /* Notify others of the policy change */ selinux_notify_policy_change(state, seqno); } @@ -2255,13 +2251,6 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len, goto err; } - - if (!selinux_initialized(state)) { - /* First policy load, so no need to preserve state from old policy */ - *newpolicyp = newpolicy; - return 0; - } - /* * NOTE: We do not need to take the rcu read lock * around the code below because other policy-modifying @@ -2269,6 +2258,11 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len, * fsi->mutex. */ oldpolicy = rcu_dereference_check(state->policy, 1); + if (!oldpolicy) { + /* First policy load, so no need to preserve state from old policy */ + *newpolicyp = newpolicy; + return 0; + } /* Preserve active boolean values from the old policy */ rc = security_preserve_bools(oldpolicy, newpolicy);