Skip to content

Commit 59438b4

Browse files
stephensmalleypcmoore
authored andcommitted
security,lockdown,selinux: implement SELinux lockdown
Implement a SELinux hook for lockdown. If the lockdown module is also enabled, then a denial by the lockdown module will take precedence over SELinux, so SELinux can only further restrict lockdown decisions. The SELinux hook only distinguishes at the granularity of integrity versus confidentiality similar to the lockdown module, but includes the full lockdown reason as part of the audit record as a hint in diagnosing what triggered the denial. To support this auditing, move the lockdown_reasons[] string array from being private to the lockdown module to the security framework so that it can be used by the lsm audit code and so that it is always available even when the lockdown module is disabled. Note that the SELinux implementation allows the integrity and confidentiality reasons to be controlled independently from one another. Thus, in an SELinux policy, one could allow operations that specify an integrity reason while blocking operations that specify a confidentiality reason. The SELinux hook implementation is stricter than the lockdown module in validating the provided reason value. Sample AVC audit output from denials: avc: denied { integrity } for pid=3402 comm="fwupd" lockdown_reason="/dev/mem,kmem,port" scontext=system_u:system_r:fwupd_t:s0 tcontext=system_u:system_r:fwupd_t:s0 tclass=lockdown permissive=0 avc: denied { confidentiality } for pid=4628 comm="cp" lockdown_reason="/proc/kcore access" scontext=unconfined_u:unconfined_r:test_lockdown_integrity_t:s0-s0:c0.c1023 tcontext=unconfined_u:unconfined_r:test_lockdown_integrity_t:s0-s0:c0.c1023 tclass=lockdown permissive=0 Signed-off-by: Stephen Smalley <sds@tycho.nsa.gov> Reviewed-by: James Morris <jamorris@linux.microsoft.com> [PM: some merge fuzz do the the perf hooks] Signed-off-by: Paul Moore <paul@paul-moore.com>
1 parent d97bd23 commit 59438b4

7 files changed

Lines changed: 74 additions & 27 deletions

File tree

‎include/linux/lsm_audit.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ struct common_audit_data {
7474
#define LSM_AUDIT_DATA_FILE 12
7575
#define LSM_AUDIT_DATA_IBPKEY 13
7676
#define LSM_AUDIT_DATA_IBENDPORT 14
77+
#define LSM_AUDIT_DATA_LOCKDOWN 15
7778
union {
7879
struct path path;
7980
struct dentry *dentry;
@@ -93,6 +94,7 @@ struct common_audit_data {
9394
struct file *file;
9495
struct lsm_ibpkey_audit *ibpkey;
9596
struct lsm_ibendport_audit *ibendport;
97+
int reason;
9698
} u;
9799
/* this union contains LSM specific data */
98100
union {

‎include/linux/security.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ enum lockdown_reason {
128128
LOCKDOWN_CONFIDENTIALITY_MAX,
129129
};
130130

131+
extern const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX+1];
132+
131133
/* These functions are in security/commoncap.c */
132134
extern int cap_capable(const struct cred *cred, struct user_namespace *ns,
133135
int cap, unsigned int opts);

‎security/lockdown/lockdown.c‎

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,6 @@
1616

1717
static enum lockdown_reason kernel_locked_down;
1818

19-
static const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX+1] = {
20-
[LOCKDOWN_NONE] = "none",
21-
[LOCKDOWN_MODULE_SIGNATURE] = "unsigned module loading",
22-
[LOCKDOWN_DEV_MEM] = "/dev/mem,kmem,port",
23-
[LOCKDOWN_EFI_TEST] = "/dev/efi_test access",
24-
[LOCKDOWN_KEXEC] = "kexec of unsigned images",
25-
[LOCKDOWN_HIBERNATION] = "hibernation",
26-
[LOCKDOWN_PCI_ACCESS] = "direct PCI access",
27-
[LOCKDOWN_IOPORT] = "raw io port access",
28-
[LOCKDOWN_MSR] = "raw MSR access",
29-
[LOCKDOWN_ACPI_TABLES] = "modifying ACPI tables",
30-
[LOCKDOWN_PCMCIA_CIS] = "direct PCMCIA CIS storage",
31-
[LOCKDOWN_TIOCSSERIAL] = "reconfiguration of serial port IO",
32-
[LOCKDOWN_MODULE_PARAMETERS] = "unsafe module parameters",
33-
[LOCKDOWN_MMIOTRACE] = "unsafe mmio",
34-
[LOCKDOWN_DEBUGFS] = "debugfs access",
35-
[LOCKDOWN_XMON_WR] = "xmon write access",
36-
[LOCKDOWN_INTEGRITY_MAX] = "integrity",
37-
[LOCKDOWN_KCORE] = "/proc/kcore access",
38-
[LOCKDOWN_KPROBES] = "use of kprobes",
39-
[LOCKDOWN_BPF_READ] = "use of bpf to read kernel RAM",
40-
[LOCKDOWN_PERF] = "unsafe use of perf",
41-
[LOCKDOWN_TRACEFS] = "use of tracefs",
42-
[LOCKDOWN_XMON_RW] = "xmon read and write access",
43-
[LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality",
44-
};
45-
4619
static const enum lockdown_reason lockdown_levels[] = {LOCKDOWN_NONE,
4720
LOCKDOWN_INTEGRITY_MAX,
4821
LOCKDOWN_CONFIDENTIALITY_MAX};

‎security/lsm_audit.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <linux/dccp.h>
2828
#include <linux/sctp.h>
2929
#include <linux/lsm_audit.h>
30+
#include <linux/security.h>
3031

3132
/**
3233
* ipv4_skb_to_auditdata : fill auditdata from skb
@@ -425,6 +426,10 @@ static void dump_common_audit_data(struct audit_buffer *ab,
425426
a->u.ibendport->dev_name,
426427
a->u.ibendport->port);
427428
break;
429+
case LSM_AUDIT_DATA_LOCKDOWN:
430+
audit_log_format(ab, " lockdown_reason=");
431+
audit_log_string(ab, lockdown_reasons[a->u.reason]);
432+
break;
428433
} /* switch (a->type) */
429434
}
430435

‎security/security.c‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,39 @@
3535
#define LSM_COUNT (__end_lsm_info - __start_lsm_info)
3636
#define EARLY_LSM_COUNT (__end_early_lsm_info - __start_early_lsm_info)
3737

38+
/*
39+
* These are descriptions of the reasons that can be passed to the
40+
* security_locked_down() LSM hook. Placing this array here allows
41+
* all security modules to use the same descriptions for auditing
42+
* purposes.
43+
*/
44+
const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX+1] = {
45+
[LOCKDOWN_NONE] = "none",
46+
[LOCKDOWN_MODULE_SIGNATURE] = "unsigned module loading",
47+
[LOCKDOWN_DEV_MEM] = "/dev/mem,kmem,port",
48+
[LOCKDOWN_EFI_TEST] = "/dev/efi_test access",
49+
[LOCKDOWN_KEXEC] = "kexec of unsigned images",
50+
[LOCKDOWN_HIBERNATION] = "hibernation",
51+
[LOCKDOWN_PCI_ACCESS] = "direct PCI access",
52+
[LOCKDOWN_IOPORT] = "raw io port access",
53+
[LOCKDOWN_MSR] = "raw MSR access",
54+
[LOCKDOWN_ACPI_TABLES] = "modifying ACPI tables",
55+
[LOCKDOWN_PCMCIA_CIS] = "direct PCMCIA CIS storage",
56+
[LOCKDOWN_TIOCSSERIAL] = "reconfiguration of serial port IO",
57+
[LOCKDOWN_MODULE_PARAMETERS] = "unsafe module parameters",
58+
[LOCKDOWN_MMIOTRACE] = "unsafe mmio",
59+
[LOCKDOWN_DEBUGFS] = "debugfs access",
60+
[LOCKDOWN_XMON_WR] = "xmon write access",
61+
[LOCKDOWN_INTEGRITY_MAX] = "integrity",
62+
[LOCKDOWN_KCORE] = "/proc/kcore access",
63+
[LOCKDOWN_KPROBES] = "use of kprobes",
64+
[LOCKDOWN_BPF_READ] = "use of bpf to read kernel RAM",
65+
[LOCKDOWN_PERF] = "unsafe use of perf",
66+
[LOCKDOWN_TRACEFS] = "use of tracefs",
67+
[LOCKDOWN_XMON_RW] = "xmon read and write access",
68+
[LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality",
69+
};
70+
3871
struct security_hook_heads security_hook_heads __lsm_ro_after_init;
3972
static BLOCKING_NOTIFIER_HEAD(blocking_lsm_notifier_chain);
4073

‎security/selinux/hooks.c‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6795,6 +6795,34 @@ static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
67956795
}
67966796
#endif
67976797

6798+
static int selinux_lockdown(enum lockdown_reason what)
6799+
{
6800+
struct common_audit_data ad;
6801+
u32 sid = current_sid();
6802+
int invalid_reason = (what <= LOCKDOWN_NONE) ||
6803+
(what == LOCKDOWN_INTEGRITY_MAX) ||
6804+
(what >= LOCKDOWN_CONFIDENTIALITY_MAX);
6805+
6806+
if (WARN(invalid_reason, "Invalid lockdown reason")) {
6807+
audit_log(audit_context(),
6808+
GFP_ATOMIC, AUDIT_SELINUX_ERR,
6809+
"lockdown_reason=invalid");
6810+
return -EINVAL;
6811+
}
6812+
6813+
ad.type = LSM_AUDIT_DATA_LOCKDOWN;
6814+
ad.u.reason = what;
6815+
6816+
if (what <= LOCKDOWN_INTEGRITY_MAX)
6817+
return avc_has_perm(&selinux_state,
6818+
sid, sid, SECCLASS_LOCKDOWN,
6819+
LOCKDOWN__INTEGRITY, &ad);
6820+
else
6821+
return avc_has_perm(&selinux_state,
6822+
sid, sid, SECCLASS_LOCKDOWN,
6823+
LOCKDOWN__CONFIDENTIALITY, &ad);
6824+
}
6825+
67986826
struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
67996827
.lbs_cred = sizeof(struct task_security_struct),
68006828
.lbs_file = sizeof(struct file_security_struct),
@@ -7107,6 +7135,8 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
71077135
LSM_HOOK_INIT(perf_event_read, selinux_perf_event_read),
71087136
LSM_HOOK_INIT(perf_event_write, selinux_perf_event_write),
71097137
#endif
7138+
7139+
LSM_HOOK_INIT(locked_down, selinux_lockdown),
71107140
};
71117141

71127142
static __init int selinux_init(void)

‎security/selinux/include/classmap.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ struct security_class_mapping secclass_map[] = {
246246
{ COMMON_SOCK_PERMS, NULL } },
247247
{ "perf_event",
248248
{"open", "cpu", "kernel", "tracepoint", "read", "write"} },
249+
{ "lockdown",
250+
{ "integrity", "confidentiality", NULL } },
249251
{ NULL }
250252
};
251253

0 commit comments

Comments
 (0)