Skip to content

Commit 7b54081

Browse files
committed
Merge tag 'selinux-pr-20201012' of git://git.kernel.org/pub/scm/linux/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 ...
2 parents 01fb1e2 + 0d50f05 commit 7b54081

21 files changed

Lines changed: 1130 additions & 499 deletions

File tree

Documentation/ABI/obsolete/sysfs-selinux-checkreqprot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Description:
1515
actual protection), and Android and Linux distributions have been
1616
explicitly writing a "0" to /sys/fs/selinux/checkreqprot during
1717
initialization for some time. Support for setting checkreqprot to 1
18-
will be removed in a future kernel release, at which point the kernel
18+
will be removed no sooner than June 2021, at which point the kernel
1919
will always cease using checkreqprot internally and will always
2020
check the actual protections being applied upon mmap/mprotect calls.
2121
The checkreqprot selinuxfs node will remain for backward compatibility

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15621,6 +15621,7 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux.git
1562115621
F: Documentation/ABI/obsolete/sysfs-selinux-checkreqprot
1562215622
F: Documentation/ABI/obsolete/sysfs-selinux-disable
1562315623
F: Documentation/admin-guide/LSM/SELinux.rst
15624+
F: include/trace/events/avc.h
1562415625
F: include/uapi/linux/selinux_netlink.h
1562515626
F: scripts/selinux/
1562615627
F: security/selinux/

include/trace/events/avc.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Authors: Thiébaud Weksteen <tweek@google.com>
4+
* Peter Enderborg <Peter.Enderborg@sony.com>
5+
*/
6+
#undef TRACE_SYSTEM
7+
#define TRACE_SYSTEM avc
8+
9+
#if !defined(_TRACE_SELINUX_H) || defined(TRACE_HEADER_MULTI_READ)
10+
#define _TRACE_SELINUX_H
11+
12+
#include <linux/tracepoint.h>
13+
14+
TRACE_EVENT(selinux_audited,
15+
16+
TP_PROTO(struct selinux_audit_data *sad,
17+
char *scontext,
18+
char *tcontext,
19+
const char *tclass
20+
),
21+
22+
TP_ARGS(sad, scontext, tcontext, tclass),
23+
24+
TP_STRUCT__entry(
25+
__field(u32, requested)
26+
__field(u32, denied)
27+
__field(u32, audited)
28+
__field(int, result)
29+
__string(scontext, scontext)
30+
__string(tcontext, tcontext)
31+
__string(tclass, tclass)
32+
),
33+
34+
TP_fast_assign(
35+
__entry->requested = sad->requested;
36+
__entry->denied = sad->denied;
37+
__entry->audited = sad->audited;
38+
__entry->result = sad->result;
39+
__assign_str(tcontext, tcontext);
40+
__assign_str(scontext, scontext);
41+
__assign_str(tclass, tclass);
42+
),
43+
44+
TP_printk("requested=0x%x denied=0x%x audited=0x%x result=%d scontext=%s tcontext=%s tclass=%s",
45+
__entry->requested, __entry->denied, __entry->audited, __entry->result,
46+
__get_str(scontext), __get_str(tcontext), __get_str(tclass)
47+
)
48+
);
49+
50+
#endif
51+
52+
/* This part must be outside protection */
53+
#include <trace/define_trace.h>

scripts/selinux/mdp/mdp.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ struct security_class_mapping {
3535

3636
#include "classmap.h"
3737
#include "initial_sid_to_string.h"
38+
#include "policycap_names.h"
39+
40+
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
3841

3942
int main(int argc, char *argv[])
4043
{
@@ -115,6 +118,10 @@ int main(int argc, char *argv[])
115118
}
116119
}
117120

121+
/* enable all policy capabilities */
122+
for (i = 0; i < ARRAY_SIZE(selinux_policycap_names); i++)
123+
fprintf(fout, "policycap %s;\n", selinux_policycap_names[i]);
124+
118125
/* types, roles, and allows */
119126
fprintf(fout, "type base_t;\n");
120127
fprintf(fout, "role base_r;\n");

security/selinux/avc.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
#include "avc_ss.h"
3232
#include "classmap.h"
3333

34+
#define CREATE_TRACE_POINTS
35+
#include <trace/events/avc.h>
36+
3437
#define AVC_CACHE_SLOTS 512
3538
#define AVC_DEF_CACHE_THRESHOLD 512
3639
#define AVC_CACHE_RECLAIM 16
@@ -702,33 +705,37 @@ static void avc_audit_post_callback(struct audit_buffer *ab, void *a)
702705
{
703706
struct common_audit_data *ad = a;
704707
struct selinux_audit_data *sad = ad->selinux_audit_data;
705-
char *scontext;
708+
char *scontext = NULL;
709+
char *tcontext = NULL;
710+
const char *tclass = NULL;
706711
u32 scontext_len;
712+
u32 tcontext_len;
707713
int rc;
708714

709715
rc = security_sid_to_context(sad->state, sad->ssid, &scontext,
710716
&scontext_len);
711717
if (rc)
712718
audit_log_format(ab, " ssid=%d", sad->ssid);
713-
else {
719+
else
714720
audit_log_format(ab, " scontext=%s", scontext);
715-
kfree(scontext);
716-
}
717721

718-
rc = security_sid_to_context(sad->state, sad->tsid, &scontext,
719-
&scontext_len);
722+
rc = security_sid_to_context(sad->state, sad->tsid, &tcontext,
723+
&tcontext_len);
720724
if (rc)
721725
audit_log_format(ab, " tsid=%d", sad->tsid);
722-
else {
723-
audit_log_format(ab, " tcontext=%s", scontext);
724-
kfree(scontext);
725-
}
726+
else
727+
audit_log_format(ab, " tcontext=%s", tcontext);
726728

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

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

735+
trace_selinux_audited(sad, scontext, tcontext, tclass);
736+
kfree(tcontext);
737+
kfree(scontext);
738+
732739
/* in case of invalid context report also the actual context string */
733740
rc = security_sid_to_context_inval(sad->state, sad->ssid, &scontext,
734741
&scontext_len);

security/selinux/hooks.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,7 +1978,7 @@ static inline u32 file_to_av(struct file *file)
19781978
}
19791979

19801980
/*
1981-
* Convert a file to an access vector and include the correct open
1981+
* Convert a file to an access vector and include the correct
19821982
* open permission.
19831983
*/
19841984
static inline u32 open_file_to_av(struct file *file)
@@ -3271,6 +3271,9 @@ static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
32713271
return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
32723272
}
32733273

3274+
if (!selinux_initialized(&selinux_state))
3275+
return 0;
3276+
32743277
/* No one is allowed to remove a SELinux security label.
32753278
You can change the label, but all data must be labeled. */
32763279
return -EACCES;
@@ -3709,7 +3712,7 @@ static int selinux_mmap_file(struct file *file, unsigned long reqprot,
37093712
return rc;
37103713
}
37113714

3712-
if (selinux_state.checkreqprot)
3715+
if (checkreqprot_get(&selinux_state))
37133716
prot = reqprot;
37143717

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

3726-
if (selinux_state.checkreqprot)
3729+
if (checkreqprot_get(&selinux_state))
37273730
prot = reqprot;
37283731

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

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

72267229
memset(&selinux_state, 0, sizeof(selinux_state));
72277230
enforcing_set(&selinux_state, selinux_enforcing_boot);
7228-
selinux_state.checkreqprot = selinux_checkreqprot_boot;
7229-
selinux_ss_init(&selinux_state.ss);
7231+
checkreqprot_set(&selinux_state, selinux_checkreqprot_boot);
72307232
selinux_avc_init(&selinux_state.avc);
72317233
mutex_init(&selinux_state.status_lock);
7234+
mutex_init(&selinux_state.policy_mutex);
72327235

72337236
/* Set the security state for the initial task. */
72347237
cred_init_security();

security/selinux/include/conditional.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
#include "security.h"
1515

16-
int security_get_bools(struct selinux_state *state,
16+
int security_get_bools(struct selinux_policy *policy,
1717
u32 *len, char ***names, int **values);
1818

1919
int security_set_bools(struct selinux_state *state, u32 len, int *values);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _SELINUX_POLICYCAP_H_
3+
#define _SELINUX_POLICYCAP_H_
4+
5+
/* Policy capabilities */
6+
enum {
7+
POLICYDB_CAPABILITY_NETPEER,
8+
POLICYDB_CAPABILITY_OPENPERM,
9+
POLICYDB_CAPABILITY_EXTSOCKCLASS,
10+
POLICYDB_CAPABILITY_ALWAYSNETWORK,
11+
POLICYDB_CAPABILITY_CGROUPSECLABEL,
12+
POLICYDB_CAPABILITY_NNP_NOSUID_TRANSITION,
13+
POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS,
14+
__POLICYDB_CAPABILITY_MAX
15+
};
16+
#define POLICYDB_CAPABILITY_MAX (__POLICYDB_CAPABILITY_MAX - 1)
17+
18+
extern const char *selinux_policycap_names[__POLICYDB_CAPABILITY_MAX];
19+
20+
#endif /* _SELINUX_POLICYCAP_H_ */
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _SELINUX_POLICYCAP_NAMES_H_
3+
#define _SELINUX_POLICYCAP_NAMES_H_
4+
5+
#include "policycap.h"
6+
7+
/* Policy capability names */
8+
const char *selinux_policycap_names[__POLICYDB_CAPABILITY_MAX] = {
9+
"network_peer_controls",
10+
"open_perms",
11+
"extended_socket_class",
12+
"always_check_network",
13+
"cgroup_seclabel",
14+
"nnp_nosuid_transition",
15+
"genfs_seclabel_symlinks"
16+
};
17+
18+
#endif /* _SELINUX_POLICYCAP_NAMES_H_ */

0 commit comments

Comments
 (0)