Skip to content

Commit

Permalink
Merge tag 'selinux-pr-20201012' of git://git.kernel.org/pub/scm/linux…
Browse files Browse the repository at this point in the history
…/kernel/git/pcmoore/selinux

Pull selinux updates from Paul Moore:
 "A decent number of SELinux patches for v5.10, twenty two in total. The
  highlights are listed below, but all of the patches pass our test
  suite and merge cleanly.

   - A number of changes to how the SELinux policy is loaded and managed
     inside the kernel with the goal of improving the atomicity of a
     SELinux policy load operation.

     These changes account for the bulk of the diffstat as well as the
     patch count. A special thanks to everyone who contributed patches
     and fixes for this work.

   - Convert the SELinux policy read-write lock to RCU.

   - A tracepoint was added for audited SELinux access control events;
     this should help provide a more unified backtrace across kernel and
     userspace.

   - Allow the removal of security.selinux xattrs when a SELinux policy
     is not loaded.

   - Enable policy capabilities in SELinux policies created with the
     scripts/selinux/mdp tool.

   - Provide some "no sooner than" dates for the SELinux checkreqprot
     sysfs deprecation"

* tag 'selinux-pr-20201012' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux: (22 commits)
  selinux: provide a "no sooner than" date for the checkreqprot removal
  selinux: Add helper functions to get and set checkreqprot
  selinux: access policycaps with READ_ONCE/WRITE_ONCE
  selinux: simplify away security_policydb_len()
  selinux: move policy mutex to selinux_state, use in lockdep checks
  selinux: fix error handling bugs in security_load_policy()
  selinux: convert policy read-write lock to RCU
  selinux: delete repeated words in comments
  selinux: add basic filtering for audit trace events
  selinux: add tracepoint on audited events
  selinux: Create new booleans and class dirs out of tree
  selinux: Standardize string literal usage for selinuxfs directory names
  selinux: Refactor selinuxfs directory populating functions
  selinux: Create function for selinuxfs directory cleanup
  selinux: permit removing security.selinux xattr before policy load
  selinux: fix memdup.cocci warnings
  selinux: avoid dereferencing the policy prior to initialization
  selinux: fix allocation failure check on newpolicy->sidtab
  selinux: refactor changing booleans
  selinux: move policy commit after updating selinuxfs
  ...
  • Loading branch information
torvalds committed Oct 13, 2020
2 parents 01fb1e2 + 0d50f05 commit 7b54081
Show file tree
Hide file tree
Showing 21 changed files with 1,130 additions and 499 deletions.
2 changes: 1 addition & 1 deletion Documentation/ABI/obsolete/sysfs-selinux-checkreqprot
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Description:
actual protection), and Android and Linux distributions have been
explicitly writing a "0" to /sys/fs/selinux/checkreqprot during
initialization for some time. Support for setting checkreqprot to 1
will be removed in a future kernel release, at which point the kernel
will be removed no sooner than June 2021, at which point the kernel
will always cease using checkreqprot internally and will always
check the actual protections being applied upon mmap/mprotect calls.
The checkreqprot selinuxfs node will remain for backward compatibility
Expand Down
1 change: 1 addition & 0 deletions MAINTAINERS
Original file line number Diff line number Diff line change
Expand Up @@ -15621,6 +15621,7 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux.git
F: Documentation/ABI/obsolete/sysfs-selinux-checkreqprot
F: Documentation/ABI/obsolete/sysfs-selinux-disable
F: Documentation/admin-guide/LSM/SELinux.rst
F: include/trace/events/avc.h
F: include/uapi/linux/selinux_netlink.h
F: scripts/selinux/
F: security/selinux/
Expand Down
53 changes: 53 additions & 0 deletions include/trace/events/avc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Authors: Thiébaud Weksteen <tweek@google.com>
* Peter Enderborg <Peter.Enderborg@sony.com>
*/
#undef TRACE_SYSTEM
#define TRACE_SYSTEM avc

#if !defined(_TRACE_SELINUX_H) || defined(TRACE_HEADER_MULTI_READ)
#define _TRACE_SELINUX_H

#include <linux/tracepoint.h>

TRACE_EVENT(selinux_audited,

TP_PROTO(struct selinux_audit_data *sad,
char *scontext,
char *tcontext,
const char *tclass
),

TP_ARGS(sad, scontext, tcontext, tclass),

TP_STRUCT__entry(
__field(u32, requested)
__field(u32, denied)
__field(u32, audited)
__field(int, result)
__string(scontext, scontext)
__string(tcontext, tcontext)
__string(tclass, tclass)
),

TP_fast_assign(
__entry->requested = sad->requested;
__entry->denied = sad->denied;
__entry->audited = sad->audited;
__entry->result = sad->result;
__assign_str(tcontext, tcontext);
__assign_str(scontext, scontext);
__assign_str(tclass, tclass);
),

TP_printk("requested=0x%x denied=0x%x audited=0x%x result=%d scontext=%s tcontext=%s tclass=%s",
__entry->requested, __entry->denied, __entry->audited, __entry->result,
__get_str(scontext), __get_str(tcontext), __get_str(tclass)
)
);

#endif

/* This part must be outside protection */
#include <trace/define_trace.h>
7 changes: 7 additions & 0 deletions scripts/selinux/mdp/mdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ struct security_class_mapping {

#include "classmap.h"
#include "initial_sid_to_string.h"
#include "policycap_names.h"

#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))

int main(int argc, char *argv[])
{
Expand Down Expand Up @@ -115,6 +118,10 @@ int main(int argc, char *argv[])
}
}

/* enable all policy capabilities */
for (i = 0; i < ARRAY_SIZE(selinux_policycap_names); i++)
fprintf(fout, "policycap %s;\n", selinux_policycap_names[i]);

/* types, roles, and allows */
fprintf(fout, "type base_t;\n");
fprintf(fout, "role base_r;\n");
Expand Down
29 changes: 18 additions & 11 deletions security/selinux/avc.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#include "avc_ss.h"
#include "classmap.h"

#define CREATE_TRACE_POINTS
#include <trace/events/avc.h>

#define AVC_CACHE_SLOTS 512
#define AVC_DEF_CACHE_THRESHOLD 512
#define AVC_CACHE_RECLAIM 16
Expand Down Expand Up @@ -702,33 +705,37 @@ static void avc_audit_post_callback(struct audit_buffer *ab, void *a)
{
struct common_audit_data *ad = a;
struct selinux_audit_data *sad = ad->selinux_audit_data;
char *scontext;
char *scontext = NULL;
char *tcontext = NULL;
const char *tclass = NULL;
u32 scontext_len;
u32 tcontext_len;
int rc;

rc = security_sid_to_context(sad->state, sad->ssid, &scontext,
&scontext_len);
if (rc)
audit_log_format(ab, " ssid=%d", sad->ssid);
else {
else
audit_log_format(ab, " scontext=%s", scontext);
kfree(scontext);
}

rc = security_sid_to_context(sad->state, sad->tsid, &scontext,
&scontext_len);
rc = security_sid_to_context(sad->state, sad->tsid, &tcontext,
&tcontext_len);
if (rc)
audit_log_format(ab, " tsid=%d", sad->tsid);
else {
audit_log_format(ab, " tcontext=%s", scontext);
kfree(scontext);
}
else
audit_log_format(ab, " tcontext=%s", tcontext);

audit_log_format(ab, " tclass=%s", secclass_map[sad->tclass-1].name);
tclass = secclass_map[sad->tclass-1].name;
audit_log_format(ab, " tclass=%s", tclass);

if (sad->denied)
audit_log_format(ab, " permissive=%u", sad->result ? 0 : 1);

trace_selinux_audited(sad, scontext, tcontext, tclass);
kfree(tcontext);
kfree(scontext);

/* in case of invalid context report also the actual context string */
rc = security_sid_to_context_inval(sad->state, sad->ssid, &scontext,
&scontext_len);
Expand Down
17 changes: 10 additions & 7 deletions security/selinux/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -1978,7 +1978,7 @@ static inline u32 file_to_av(struct file *file)
}

/*
* Convert a file to an access vector and include the correct open
* Convert a file to an access vector and include the correct
* open permission.
*/
static inline u32 open_file_to_av(struct file *file)
Expand Down Expand Up @@ -3271,6 +3271,9 @@ static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
}

if (!selinux_initialized(&selinux_state))
return 0;

/* No one is allowed to remove a SELinux security label.
You can change the label, but all data must be labeled. */
return -EACCES;
Expand Down Expand Up @@ -3709,7 +3712,7 @@ static int selinux_mmap_file(struct file *file, unsigned long reqprot,
return rc;
}

if (selinux_state.checkreqprot)
if (checkreqprot_get(&selinux_state))
prot = reqprot;

return file_map_prot_check(file, prot,
Expand All @@ -3723,7 +3726,7 @@ static int selinux_file_mprotect(struct vm_area_struct *vma,
const struct cred *cred = current_cred();
u32 sid = cred_sid(cred);

if (selinux_state.checkreqprot)
if (checkreqprot_get(&selinux_state))
prot = reqprot;

if (default_noexec &&
Expand Down Expand Up @@ -4438,7 +4441,7 @@ static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
*
* If @skb_sid is valid then the user:role:type information from @sk_sid is
* combined with the MLS information from @skb_sid in order to create
* @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
* @conn_sid. If @skb_sid is not valid then @conn_sid is simply a copy
* of @sk_sid. Returns zero on success, negative values on failure.
*
*/
Expand Down Expand Up @@ -5308,7 +5311,7 @@ static int selinux_sctp_bind_connect(struct sock *sk, int optname,

/* As selinux_sctp_bind_connect() is called by the
* SCTP protocol layer, the socket is already locked,
* therefore selinux_netlbl_socket_connect_locked() is
* therefore selinux_netlbl_socket_connect_locked()
* is called here. The situations handled are:
* sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
* whenever a new IP address is added or when a new
Expand Down Expand Up @@ -7225,10 +7228,10 @@ static __init int selinux_init(void)

memset(&selinux_state, 0, sizeof(selinux_state));
enforcing_set(&selinux_state, selinux_enforcing_boot);
selinux_state.checkreqprot = selinux_checkreqprot_boot;
selinux_ss_init(&selinux_state.ss);
checkreqprot_set(&selinux_state, selinux_checkreqprot_boot);
selinux_avc_init(&selinux_state.avc);
mutex_init(&selinux_state.status_lock);
mutex_init(&selinux_state.policy_mutex);

/* Set the security state for the initial task. */
cred_init_security();
Expand Down
2 changes: 1 addition & 1 deletion security/selinux/include/conditional.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#include "security.h"

int security_get_bools(struct selinux_state *state,
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);
Expand Down
20 changes: 20 additions & 0 deletions security/selinux/include/policycap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _SELINUX_POLICYCAP_H_
#define _SELINUX_POLICYCAP_H_

/* Policy capabilities */
enum {
POLICYDB_CAPABILITY_NETPEER,
POLICYDB_CAPABILITY_OPENPERM,
POLICYDB_CAPABILITY_EXTSOCKCLASS,
POLICYDB_CAPABILITY_ALWAYSNETWORK,
POLICYDB_CAPABILITY_CGROUPSECLABEL,
POLICYDB_CAPABILITY_NNP_NOSUID_TRANSITION,
POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS,
__POLICYDB_CAPABILITY_MAX
};
#define POLICYDB_CAPABILITY_MAX (__POLICYDB_CAPABILITY_MAX - 1)

extern const char *selinux_policycap_names[__POLICYDB_CAPABILITY_MAX];

#endif /* _SELINUX_POLICYCAP_H_ */
18 changes: 18 additions & 0 deletions security/selinux/include/policycap_names.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _SELINUX_POLICYCAP_NAMES_H_
#define _SELINUX_POLICYCAP_NAMES_H_

#include "policycap.h"

/* Policy capability names */
const char *selinux_policycap_names[__POLICYDB_CAPABILITY_MAX] = {
"network_peer_controls",
"open_perms",
"extended_socket_class",
"always_check_network",
"cgroup_seclabel",
"nnp_nosuid_transition",
"genfs_seclabel_symlinks"
};

#endif /* _SELINUX_POLICYCAP_NAMES_H_ */
Loading

0 comments on commit 7b54081

Please sign in to comment.