Skip to content

Commit

Permalink
selinux: stop passing selinux_state pointers and their offspring
Browse files Browse the repository at this point in the history
Linus observed that the pervasive passing of selinux_state pointers
introduced by me in commit aa8e712 ("selinux: wrap global selinux
state") adds overhead and complexity without providing any
benefit. The original idea was to pave the way for SELinux namespaces
but those have not yet been implemented and there isn't currently
a concrete plan to do so. Remove the passing of the selinux_state
pointers, reverting to direct use of the single global selinux_state,
and likewise remove passing of child pointers like the selinux_avc.
The selinux_policy pointer remains as it is needed for atomic switching
of policies.

Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Reported-by: kernel test robot <lkp@intel.com>
Link: https://lore.kernel.org/oe-kbuild-all/202303101057.mZ3Gv5fK-lkp@intel.com/
Signed-off-by: Stephen Smalley <stephen.smalley.work@gmail.com>
Signed-off-by: Paul Moore <paul@paul-moore.com>
  • Loading branch information
Stephen Smalley authored and Paul Moore committed Mar 14, 2023
1 parent f62ca0b commit e67b798
Show file tree
Hide file tree
Showing 18 changed files with 651 additions and 995 deletions.
197 changes: 86 additions & 111 deletions security/selinux/avc.c

Large diffs are not rendered by default.

549 changes: 208 additions & 341 deletions security/selinux/hooks.c

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion security/selinux/ibpkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ static int sel_ib_pkey_sid_slow(u64 subnet_prefix, u16 pkey_num, u32 *sid)
return 0;
}

ret = security_ib_pkey_sid(&selinux_state, subnet_prefix, pkey_num,
ret = security_ib_pkey_sid(subnet_prefix, pkey_num,
sid);
if (ret)
goto out;
Expand Down
37 changes: 16 additions & 21 deletions security/selinux/ima.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
/*
* selinux_ima_collect_state - Read selinux configuration settings
*
* @state: selinux_state
*
* On success returns the configuration settings string.
* On error, returns NULL.
*/
static char *selinux_ima_collect_state(struct selinux_state *state)
static char *selinux_ima_collect_state(void)
{
const char *on = "=1;", *off = "=0;";
char *buf;
Expand All @@ -39,26 +37,27 @@ static char *selinux_ima_collect_state(struct selinux_state *state)
rc = strscpy(buf, "initialized", buf_len);
WARN_ON(rc < 0);

rc = strlcat(buf, selinux_initialized(state) ? on : off, buf_len);
rc = strlcat(buf, selinux_initialized() ? on : off, buf_len);
WARN_ON(rc >= buf_len);

rc = strlcat(buf, "enforcing", buf_len);
WARN_ON(rc >= buf_len);

rc = strlcat(buf, enforcing_enabled(state) ? on : off, buf_len);
rc = strlcat(buf, enforcing_enabled() ? on : off, buf_len);
WARN_ON(rc >= buf_len);

rc = strlcat(buf, "checkreqprot", buf_len);
WARN_ON(rc >= buf_len);

rc = strlcat(buf, checkreqprot_get(state) ? on : off, buf_len);
rc = strlcat(buf, checkreqprot_get() ? on : off, buf_len);
WARN_ON(rc >= buf_len);

for (i = 0; i < __POLICYDB_CAP_MAX; i++) {
rc = strlcat(buf, selinux_policycap_names[i], buf_len);
WARN_ON(rc >= buf_len);

rc = strlcat(buf, state->policycap[i] ? on : off, buf_len);
rc = strlcat(buf, selinux_state.policycap[i] ? on : off,
buf_len);
WARN_ON(rc >= buf_len);
}

Expand All @@ -67,19 +66,17 @@ static char *selinux_ima_collect_state(struct selinux_state *state)

/*
* selinux_ima_measure_state_locked - Measure SELinux state and hash of policy
*
* @state: selinux state struct
*/
void selinux_ima_measure_state_locked(struct selinux_state *state)
void selinux_ima_measure_state_locked(void)
{
char *state_str = NULL;
void *policy = NULL;
size_t policy_len;
int rc = 0;

lockdep_assert_held(&state->policy_mutex);
lockdep_assert_held(&selinux_state.policy_mutex);

state_str = selinux_ima_collect_state(state);
state_str = selinux_ima_collect_state();
if (!state_str) {
pr_err("SELinux: %s: failed to read state.\n", __func__);
return;
Expand All @@ -94,10 +91,10 @@ void selinux_ima_measure_state_locked(struct selinux_state *state)
/*
* Measure SELinux policy only after initialization is completed.
*/
if (!selinux_initialized(state))
if (!selinux_initialized())
return;

rc = security_read_state_kernel(state, &policy, &policy_len);
rc = security_read_state_kernel(&policy, &policy_len);
if (rc) {
pr_err("SELinux: %s: failed to read policy %d.\n", __func__, rc);
return;
Expand All @@ -112,14 +109,12 @@ void selinux_ima_measure_state_locked(struct selinux_state *state)

/*
* selinux_ima_measure_state - Measure SELinux state and hash of policy
*
* @state: selinux state struct
*/
void selinux_ima_measure_state(struct selinux_state *state)
void selinux_ima_measure_state(void)
{
lockdep_assert_not_held(&state->policy_mutex);
lockdep_assert_not_held(&selinux_state.policy_mutex);

mutex_lock(&state->policy_mutex);
selinux_ima_measure_state_locked(state);
mutex_unlock(&state->policy_mutex);
mutex_lock(&selinux_state.policy_mutex);
selinux_ima_measure_state_locked();
mutex_unlock(&selinux_state.policy_mutex);
}
29 changes: 10 additions & 19 deletions security/selinux/include/avc.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ struct selinux_audit_data {
u32 audited;
u32 denied;
int result;
struct selinux_state *state;
} __randomize_layout;

/*
Expand Down Expand Up @@ -97,14 +96,12 @@ static inline u32 avc_audit_required(u32 requested,
return audited;
}

int slow_avc_audit(struct selinux_state *state,
u32 ssid, u32 tsid, u16 tclass,
int slow_avc_audit(u32 ssid, u32 tsid, u16 tclass,
u32 requested, u32 audited, u32 denied, int result,
struct common_audit_data *a);

/**
* avc_audit - Audit the granting or denial of permissions.
* @state: SELinux state
* @ssid: source security identifier
* @tsid: target security identifier
* @tclass: target security class
Expand All @@ -122,8 +119,7 @@ int slow_avc_audit(struct selinux_state *state,
* be performed under a lock, to allow the lock to be released
* before calling the auditing code.
*/
static inline int avc_audit(struct selinux_state *state,
u32 ssid, u32 tsid,
static inline int avc_audit(u32 ssid, u32 tsid,
u16 tclass, u32 requested,
struct av_decision *avd,
int result,
Expand All @@ -133,30 +129,27 @@ static inline int avc_audit(struct selinux_state *state,
audited = avc_audit_required(requested, avd, result, 0, &denied);
if (likely(!audited))
return 0;
return slow_avc_audit(state, ssid, tsid, tclass,
return slow_avc_audit(ssid, tsid, tclass,
requested, audited, denied, result,
a);
}

#define AVC_STRICT 1 /* Ignore permissive mode. */
#define AVC_EXTENDED_PERMS 2 /* update extended permissions */
int avc_has_perm_noaudit(struct selinux_state *state,
u32 ssid, u32 tsid,
int avc_has_perm_noaudit(u32 ssid, u32 tsid,
u16 tclass, u32 requested,
unsigned flags,
struct av_decision *avd);

int avc_has_perm(struct selinux_state *state,
u32 ssid, u32 tsid,
int avc_has_perm(u32 ssid, u32 tsid,
u16 tclass, u32 requested,
struct common_audit_data *auditdata);

int avc_has_extended_perms(struct selinux_state *state,
u32 ssid, u32 tsid, u16 tclass, u32 requested,
int avc_has_extended_perms(u32 ssid, u32 tsid, u16 tclass, u32 requested,
u8 driver, u8 perm, struct common_audit_data *ad);


u32 avc_policy_seqno(struct selinux_state *state);
u32 avc_policy_seqno(void);

#define AVC_CALLBACK_GRANT 1
#define AVC_CALLBACK_TRY_REVOKE 2
Expand All @@ -171,11 +164,9 @@ u32 avc_policy_seqno(struct selinux_state *state);
int avc_add_callback(int (*callback)(u32 event), u32 events);

/* Exported to selinuxfs */
struct selinux_avc;
int avc_get_hash_stats(struct selinux_avc *avc, char *page);
unsigned int avc_get_cache_threshold(struct selinux_avc *avc);
void avc_set_cache_threshold(struct selinux_avc *avc,
unsigned int cache_threshold);
int avc_get_hash_stats(char *page);
unsigned int avc_get_cache_threshold(void);
void avc_set_cache_threshold(unsigned int cache_threshold);

/* Attempt to free avc node cache */
void avc_disable(void);
Expand Down
3 changes: 1 addition & 2 deletions security/selinux/include/avc_ss.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

#include <linux/types.h>

struct selinux_avc;
int avc_ss_reset(struct selinux_avc *avc, u32 seqno);
int avc_ss_reset(u32 seqno);

/* Class/perm mapping support */
struct security_class_mapping {
Expand Down
4 changes: 2 additions & 2 deletions security/selinux/include/conditional.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
int security_get_bools(struct selinux_policy *policy,
u32 *len, char ***names, int **values);

int security_set_bools(struct selinux_state *state, u32 len, int *values);
int security_set_bools(u32 len, int *values);

int security_get_bool_value(struct selinux_state *state, u32 index);
int security_get_bool_value(u32 index);

#endif
10 changes: 4 additions & 6 deletions security/selinux/include/ima.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@
#include "security.h"

#ifdef CONFIG_IMA
extern void selinux_ima_measure_state(struct selinux_state *selinux_state);
extern void selinux_ima_measure_state_locked(
struct selinux_state *selinux_state);
extern void selinux_ima_measure_state(void);
extern void selinux_ima_measure_state_locked(void);
#else
static inline void selinux_ima_measure_state(struct selinux_state *selinux_state)
static inline void selinux_ima_measure_state(void)
{
}
static inline void selinux_ima_measure_state_locked(
struct selinux_state *selinux_state)
static inline void selinux_ima_measure_state_locked(void)
{
}
#endif
Expand Down
Loading

0 comments on commit e67b798

Please sign in to comment.