Turn lockdown into an LSM
Subsystems (4)
Hi James,
This is a quick attempt to integrate lockdown into the existing LSM framework. It adds a new lockdown security hook and an LSM that defines the existing coarse-grained policy, and also adds a new DEFINE_EARLY_LSM() definition in order to permit lockdown (and potentially other modules) to be initialised at the top of kernel init in order to allow policy to be imposed on stuff that happens in setup_arch(). The goal here is to allow policy to be devolved to other LSMs on systems that have a secure mechanism for loading LSM policy early in boot, allowing creation of arbitrarily complicated policies without interfering with the common-case coarse-grained approach.
This should probably be extended so a uapi-exposed constant is passed to the hook in order to make it easier to write policy in other LSMs, but does this broadly look like you were imagining?
RFC 1/2 security: Support early LSMs
The lockdown module is intended to allow for kernels to be locked down early in boot - sufficiently early that we don't have the ability to kmalloc() yet. Add support for early initialisation of some LSMs, and then add them to the list of names when we do full initialisation later.
--- include/asm-generic/vmlinux.lds.h | 8 +++++- include/linux/lsm_hooks.h | 6 ++++ include/linux/security.h | 1 + init/main.c | 1 + security/security.c | 48 +++++++++++++++++++++++++------ 5 files changed, 54 insertions(+), 10 deletions(-)
include/asm-generic/vmlinux.lds.h +7 -1
@@ -208,8 +208,13 @@ __start_lsm_info = .; \ KEEP(*(.lsm_info.init)) \ __end_lsm_info = .; +#define EARLY_LSM_TABLE () . = ALIGN(8); \ + __start_early_lsm_info = .; \ + KEEP(*(.early_lsm_info.init)) \ + __end_early_lsm_info = .;#else #define LSM_TABLE () +#define EARLY_LSM_TABLE ()#endif #define ___OF_TABLE (cfg, name) _OF_TABLE_##cfg(name)
@@ -610,7 +615,8 @@ ACPI_PROBE_TABLE(irqchip) \ ACPI_PROBE_TABLE(timer) \ EARLYCON_TABLE() \ - LSM_TABLE() + LSM_TABLE() \ + EARLY_LSM_TABLE()#define INIT_TEXT \ *(.init.text .init.text.*) \
include/linux/lsm_hooks.h +6 -0
@@ -2074,12 +2074,18 @@ struct lsm_info { };extern struct lsm_info __start_lsm_info[], __end_lsm_info[]; +extern struct lsm_info __start_early_lsm_info[], __end_early_lsm_info[];#define DEFINE_LSM (lsm) \ static struct lsm_info __lsm_##lsm \ __used __section(.lsm_info.init) \ __aligned(sizeof(unsigned long)) +#define DEFINE_EARLY_LSM (lsm) \ + static struct lsm_info __early_lsm_##lsm \ + __used __section(.early_lsm_info.init) \ + __aligned(sizeof(unsigned long)) +#ifdef CONFIG_SECURITY_SELINUX_DISABLE /* * Assuring the safety of deleting a security module is up to
include/linux/security.h +1 -0
@@ -191,6 +191,7 @@ int unregister_lsm_notifier(struct notifier_block *nb);/* prototypes */ extern int security_init (void ); +extern int early_security_init (void );/* Security operations */ int security_binder_set_context_mgr (struct task_struct *mgr);
init/main.c +1 -0
@@ -555,6 +555,7 @@ asmlinkage __visible void __init start_kernel(void)boot_cpu_init ();page_address_init ();pr_notice ("%s" , linux_banner); +early_security_init ();setup_arch (&command_line);/* * Set up the the initial canary and entropy after arch
security/security.c +39 -9
87 lines not on this page · raw message
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
RFC 2/2 Add the ability to lock down access to the running kernel image
In reply to Matthew GarrettFrom: David Howells <[email protected]>
Provide a single call to allow kernel code to determine whether the system should be locked down, thereby disallowing various accesses that might allow the running kernel image to be changed including the loading of modules that aren't validly signed with a key we recognise, fiddling with MSR registers and disallowing hibernation.
--- Documentation/ABI/testing/lockdown | 19 +++ .../admin-guide/kernel-parameters.txt | 9 + Documentation/admin-guide/lockdown.rst | 60 +++++++ include/linux/kernel.h | 7 + include/linux/lsm_hooks.h | 2 + include/linux/security.h | 6 +- security/Kconfig | 3 +- security/Makefile | 2 + security/lockdown/Kconfig | 46 +++++ security/lockdown/Makefile | 1 + security/lockdown/lockdown.c | 157 ++++++++++++++++++ security/security.c | 6 + 12 files changed, 316 insertions(+), 2 deletions(-) create mode 100644 Documentation/ABI/testing/lockdown create mode 100644 Documentation/admin-guide/lockdown.rst create mode 100644 security/lockdown/Kconfig create mode 100644 security/lockdown/Makefile create mode 100644 security/lockdown/lockdown.c
Documentation/ABI/testing/lockdown new file +19 -0
@@ -0,0 +1,19 @@ +What: security/lockdown +Date: March 2019 +Contact: Matthew Garrett <[email protected]> +Description: + If CONFIG_LOCK_DOWN_KERNEL is enabled, the kernel can be + moved to a more locked down state at runtime by writing to + this attribute. Valid values are: + + integrity: + The kernel will disable functionality that allows + userland to modify the running kernel image, other + than through the loading or execution of appropriately + signed objects. + + confidentiality: + The kernel will disable all functionality disabled by + the integrity mode, but additionally will disable + features that potentially permit userland to obtain + confidential information stored within the kernel.
Documentation/admin-guide/kernel-parameters.txt +9 -0
@@ -2213,6 +2213,15 @@ lockd.nlm_udpport=M [NFS] Assign UDP port. Format: <integer> + lockdown= [SECURITY] + { integrity | confidentiality } + Enable the kernel lockdown feature. If set to + integrity, kernel features that allow userland to + modify the running kernel are disabled. If set to + confidentiality, kernel features that allow userland + to extract confidential information from the kernel + are also disabled. + locktorture.nreaders_stress= [KNL] Set the number of locking read-acquisition kthreads. Defaults to being automatically set based on the
Documentation/admin-guide/lockdown.rst new file +60 -0
60 lines not on this page · raw message
include/linux/kernel.h +7 -0
@@ -340,6 +340,13 @@ static inline void refcount_error_report(struct pt_regs *regs, const char *err) { }#endif +enum lockdown_level { +LOCKDOWN_NONE , +LOCKDOWN_INTEGRITY , +LOCKDOWN_CONFIDENTIALITY , +LOCKDOWN_MAX , +}; +/* Internal, do not use. */ int __must_check _kstrtoul (const char *s,unsigned int base,unsigned long *res);int __must_check _kstrtol (const char *s,unsigned int base,long *res);
include/linux/lsm_hooks.h +2 -0
@@ -1781,6 +1781,7 @@ union security_list_options {int (*bpf_prog_alloc_security)(struct bpf_prog_aux *aux);void (*bpf_prog_free_security)(struct bpf_prog_aux *aux);#endif /* CONFIG_BPF_SYSCALL */ +int (*locked_down)(const char *where,enum lockdown_level level); };struct security_hook_heads {
@@ -2016,6 +2017,7 @@ struct security_hook_heads {struct hlist_head bpf_prog_alloc_security;struct hlist_head bpf_prog_free_security;#endif /* CONFIG_BPF_SYSCALL */ +struct hlist_head locked_down; } __randomize_layout;/*
include/linux/security.h +5 -1
@@ -384,6 +384,7 @@ void security_inode_invalidate_secctx(struct inode *inode);int security_inode_notifysecctx (struct inode *inode,void *ctx,u32 ctxlen);int security_inode_setsecctx (struct dentry *dentry,void *ctx,u32 ctxlen);int security_inode_getsecctx (struct inode *inode,void **ctx,u32 *ctxlen); +int security_is_locked_down (const char *where,enum lockdown_level level);#else /* CONFIG_SECURITY */ static inline int call_lsm_notifier (enum lsm_event event,void *data)
@@ -1173,6 +1174,10 @@ static inline int security_inode_getsecctx(struct inode *inode, void **ctx, u32 {return -EOPNOTSUPP ; } +static inline int security_is_locked_down (const char *where,enum lockdown_level level) +{ +return 0 ; +}#endif /* CONFIG_SECURITY */ #ifdef CONFIG_SECURITY_NETWORK
@@ -1800,4 +1805,3 @@ static inline void security_bpf_prog_free(struct bpf_prog_aux *aux)#endif /* CONFIG_BPF_SYSCALL */ #endif /* ! __LINUX_SECURITY_H */ -
security/Kconfig +2 -1
@@ -236,12 +236,13 @@ source "security/apparmor/Kconfig" source "security/loadpin/Kconfig" source "security/yama/Kconfig" source "security/safesetid/Kconfig" +source "security/lockdown/Kconfig" source "security/integrity/Kconfig" config LSM string "Ordered list of enabled LSMs" - default "yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor" + default "lockdown,yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor" help A comma-separated list of LSMs, in initialization order. Any LSMs left off this list will be ignored. This can be
security/Makefile +2 -0
@@ -11,6 +11,7 @@ subdir-$(CONFIG_SECURITY_APPARMOR) += apparmor subdir-$(CONFIG_SECURITY_YAMA) += yama subdir-$(CONFIG_SECURITY_LOADPIN) += loadpin subdir-$(CONFIG_SECURITY_SAFESETID) += safesetid +subdir-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown # always enable default capabilities obj-y += commoncap.o
@@ -27,6 +28,7 @@ obj-$(CONFIG_SECURITY_APPARMOR) += apparmor/ obj-$(CONFIG_SECURITY_YAMA) += yama/ obj-$(CONFIG_SECURITY_LOADPIN) += loadpin/ obj-$(CONFIG_SECURITY_SAFESETID) += safesetid/ +obj-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown/ obj-$(CONFIG_CGROUP_DEVICE) += device_cgroup.o # Object integrity file lists
security/lockdown/Kconfig new file +46 -0
46 lines not on this page · raw message
security/lockdown/Makefile new file +1 -0
@@ -0,0 +1 @@ +obj-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown.o
security/lockdown/lockdown.c new file +157 -0
157 lines not on this page · raw message
security/security.c +6 -0
@@ -2370,3 +2370,9 @@ void security_bpf_prog_free(struct bpf_prog_aux *aux) call_void_hook(bpf_prog_free_security, aux); }#endif /* CONFIG_BPF_SYSCALL */ + +int security_is_locked_down (const char *where,enum lockdown_level level) +{ +return call_int_hook (locked_down,0 , where, level); +} +EXPORT_SYMBOL (security_is_locked_down);
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
James Morris
maintainerQuoted 13 lines · Matthew Garrett
Hi James,
This is a quick attempt to integrate lockdown into the existing LSM framework. It adds a new lockdown security hook and an LSM that defines the existing coarse-grained policy, and also adds a new
DEFINE_EARLY_LSM()definition in order to permit lockdown (and potentially other modules) to be initialised at the top of kernel init in order to allow policy to be imposed on stuff that happens insetup_arch(). The goal here is to allow policy to be devolved to other LSMs on systems that have a secure mechanism for loading LSM policy early in boot, allowing creation of arbitrarily complicated policies without interfering with the common-case coarse-grained approach.
Quoted 3 lines
This should probably be extended so a uapi-exposed constant is passed to the hook in order to make it easier to write policy in other LSMs, but does this broadly look like you were imagining?
This looks promising!
An LSM could also potentially implement its own policy for the hook.
Signature · 2 lines
James Morris <[email protected]>
James Morris
maintainerRe: [RFC 2/2] Add the ability to lock down access to the running kernel image
In reply to Matthew Garrettinclude/linux/lsm_hooks.h union security_list_options {
Quoted 3 lines · Matthew Garrett
include/linux/lsm_hooks.h +2
-0+int (*locked_down)(const char *where,enum lockdown_level level); +static int lockdown_is_locked_down (const char *what,enum lockdown_level level)
I'm guessing 'what' is the best option here.
Signature · 2 lines
James Morris <[email protected]>
Quoted 1 line · James Morris
An LSM could also potentially implement its own policy for the hook.
That was my plan. Right now the hook just gets an ASCII description of the reason for the lockdown - that seems suboptimal for cases like SELinux. What information would you want? My initial thinking was to just have a stable enum of lockdown reasons that's in the UAPI headers and then let other LSM tooling consume that, but I haven't spent enough time with the internals of SELinux to know if there'd be a more attractive solution.
Quoted 11 lines · Matthew Garrett
Quoted 1 line · James Morris
An LSM could also potentially implement its own policy for the hook.
That was my plan. Right now the hook just gets an ASCII description of the reason for the lockdown - that seems suboptimal for cases like SELinux. What information would you want? My initial thinking was to just have a stable enum of lockdown reasons that's in the UAPI headers and then let other LSM tooling consume that, but I haven't spent enough time with the internals of SELinux to know if there'd be a more attractive solution.
I may be in the minority here, but I see this issue as a significant downside of making lockdown more flexible. If we stick with just "this may violate integrity" and "this may violate confidentiality", then the ABI surface is nice and narrow. If we start having a big uapi list of things that might qualify for lockdown, we need to worry about compatibility issues.
This isn't purely theoretical. Lockdown has some interesting interactions with eBPF. I don't want to be in a situation where v1 of lockdown has a few eBPF hooks, but a later update improves the eBPF vs lockdown interaction so that you can do more with eBPF on a locked down kernel. But now any such change has to worry about breaking the lockdown LSM ABI.
And I still think it would be nice to have some credible use case for a more fine grained policy than just the tri-state. Having a lockdown policy of "may not violate kernel confidentiality except using kprobes" may be convenient, but it's also basically worthless, since kernel confidentiality is gone.
All this being said, I do see one big benefit for LSM integration:
SELinux or another LSM could allow certain privileged tasks to bypass lockdown. This seems fine, except that there's potential nastiness where current->cred isn't actually a valid thing to look at in the current context.
So I guess my proposal is: use LSM, but make the hook very coarse
grained: int security_violate_confidentiality(const struct cred *) and int security_violate_integrity(const struct cred *).
--Andy
James Morris
maintainerQuoted 5 lines · Andy Lutomirski
And I still think it would be nice to have some credible use case for a more fine grained policy than just the tri-state. Having a lockdown policy of "may not violate kernel confidentiality except using kprobes" may be convenient, but it's also basically worthless, since kernel confidentiality is gone.
This is an important point, but there's also "can't use any lockdown features because the admin might need to use kprobes". I mention a use-case below.
I think it's fine (and probably preferred) to keep the default behavior tri-state and allow LSMs to implement finer-grained policies.
Quoted 3 lines
All this being said, I do see one big benefit for LSM integration:
SELinux or another LSM could allow certain privileged tasks to bypass lockdown.
Some environments _need_ a "break glass" option, and a well-defined policy (e.g. an SELinux domain which can only be entered via serial console, with 2FA or JIT credentials) to selectively un-lock the kernel lockdown in production would mean the difference between having a fleet of millions of nodes 99.999% locked down vs 0%.
Quoted 3 lines
This seems fine, except that there's potential nastiness where
current->credisn't actually a valid thing to look at in the current context.
Right.
Can we identify any such cases in the current patchset?
One option would be for the LSM to assign a default (untrusted/unknown) value for the subject and then apply policy as needed (e.g. allow or deny these).
Quoted 3 lines
So I guess my proposal is: use LSM, but make the hook very coarse
grained: intsecurity_violate_confidentiality(const struct cred *)and intsecurity_violate_integrity(const struct cred *).
Perhaps security_kernel_unlock_*
Signature · 2 lines
James Morris <[email protected]>
Quoted 20 lines · Andy Lutomirski
Quoted 11 lines · Matthew Garrett
Quoted 1 line · James Morris
An LSM could also potentially implement its own policy for the hook.
That was my plan. Right now the hook just gets an ASCII description of the reason for the lockdown - that seems suboptimal for cases like SELinux. What information would you want? My initial thinking was to just have a stable enum of lockdown reasons that's in the UAPI headers and then let other LSM tooling consume that, but I haven't spent enough time with the internals of SELinux to know if there'd be a more attractive solution.
I may be in the minority here, but I see this issue as a significant downside of making lockdown more flexible. If we stick with just "this may violate integrity" and "this may violate confidentiality", then the ABI surface is nice and narrow. If we start having a big uapi list of things that might qualify for lockdown, we need to worry about compatibility issues.
Quoted 6 lines
This isn't purely theoretical. Lockdown has some interesting interactions with eBPF. I don't want to be in a situation where v1 of lockdown has a few eBPF hooks, but a later update improves the eBPF vs lockdown interaction so that you can do more with eBPF on a locked down kernel. But now any such change has to worry about breaking the lockdown LSM ABI.
I think we could keep the enum itself private to the kernel, and internally each security module could map the values to whatever permissions it wants to define. SELinux itself has the ability to add/remove/re-arrange its permission definitions without breaking userspace/policy; they are dynamically mapped at policy load time. If a given permission doesn't exist in the loaded policy, there is a policy flag to control whether the policy should be rejected, or the permission should always be denied, or the permission should always be allowed. There is also support for an extensible set of "policy capabilities" to control whether new/changed permission checking logic for a given permission/operation should be enabled for that policy or if the kernel should continue using the older logic.
Quoted 5 lines
And I still think it would be nice to have some credible use case for a more fine grained policy than just the tri-state. Having a lockdown policy of "may not violate kernel confidentiality except using kprobes" may be convenient, but it's also basically worthless, since kernel confidentiality is gone.
I would expect that distros and users would have to make pragmatic tradeoffs in this area, and it would be nice if they could have a choice beyond all-or-nothing. Do all of the interfaces being locked down expose the same degree of confidentiality or integrity risk?
Quoted 3 lines
All this being said, I do see one big benefit for LSM integration:
SELinux or another LSM could allow certain privileged tasks to bypass lockdown.
That seems to violate the intent of lockdown as I understood it, and turns security_is_locked_down() into a finer-grained capable() call.
Also, if I understand correctly, this could only be done if one were to disable the lockdown module in the lsm list, since the security framework will return non-zero (i.e. the operation is locked down) if any module that implements the hook returns non-zero; LSM is "restrictive". At that point, SELinux or the other LSM would be the sole arbiter of lockdown decisions. SELinux or the other LSM also wouldn't have access to the kernel_locked_down level unless that was exported in some manner from the lockdown module. Not sure how to compose these.
This seems fine, except that there's potential nastiness
Quoted 6 lines
where
current->credisn't actually a valid thing to look at in the current context.So I guess my proposal is: use LSM, but make the hook very coarse
grained: intsecurity_violate_confidentiality(const struct cred *)and intsecurity_violate_integrity(const struct cred *).
Not sure one can construct a useful policy at that granularity.
James Morris
maintainerQuoted 10 lines · Stephen Smalley
That seems to violate the intent of lockdown as I understood it, and turns
security_is_locked_down()into a finer-grainedcapable()call. Also, if I understand correctly, this could only be done if one were to disable the lockdown module in the lsm list, since the security framework will return non-zero (i.e. the operation is locked down) if any module that implements the hook returns non-zero; LSM is "restrictive". At that point SELinux or the other LSM would be the sole arbiter of lockdown decisions. SELinux or the other LSM also wouldn't have access to thekernel_locked_downlevel unless that was exported in some manner from the lockdown module. Not sure how to compose these.
Right, I was envisaging the LSM replacing the default.
i.e. the default is tristate OR fine grained LSM policy.
They could in theory be composed restrictively, but this is likely not useful given the coarse grained default policy. All the LSM could do is either further restrict none or integrity.
We'd need to figure out how to avoid confusing users in the case where multiple LSMs are registered for the hooks, possibly by having the lockdown LSM gate this and update the securityfs lockdown node with something like "lsm:smack".
Signature · 2 lines
James Morris <[email protected]>
Quoted 16 lines · James Morris
Quoted 10 lines · Stephen Smalley
That seems to violate the intent of lockdown as I understood it, and turns
security_is_locked_down()into a finer-grainedcapable()call. Also, if I understand correctly, this could only be done if one were to disable the lockdown module in the lsm list, since the security framework will return non-zero (i.e. the operation is locked down) if any module that implements the hook returns non-zero; LSM is "restrictive". At that point SELinux or the other LSM would be the sole arbiter of lockdown decisions. SELinux or the other LSM also wouldn't have access to thekernel_locked_downlevel unless that was exported in some manner from the lockdown module. Not sure how to compose these.Right, I was envisaging the LSM replacing the default.
i.e. the default is tristate OR fine grained LSM policy.
Quoted 8 lines
They could in theory be composed restrictively, but this is likely not useful given the coarse grained default policy. All the LSM could do is either further restrict none or integrity.
We'd need to figure out how to avoid confusing users in the case where multiple LSMs are registered for the hooks, possibly by having the lockdown LSM gate this and update the securityfs lockdown node with something like "lsm:smack".
The way I'd propose dealing with multiple LSMs using the securityfs interface is the same as I'm proposing for /proc/.../attr/current and SO_PEERSEC. A new interface /proc/self/attr/display contains the name of the LSM that the current process will see when looking at process or security attributes that are "shared". Writing to display is unprivileged and changes which LSM you get information for.
Adornments like "lsm:smack" often require modification of programs that fear change. The same would be true of a prctl(). The "display" file approach is no harder for applications that are getting modified and much easier for scripts.
Quoted 17 lines · James Morris
Quoted 10 lines · Stephen Smalley
That seems to violate the intent of lockdown as I understood it, and turns
security_is_locked_down()into a finer-grainedcapable()call. Also, if I understand correctly, this could only be done if one were to disable the lockdown module in the lsm list, since the security framework will return non-zero (i.e. the operation is locked down) if any module that implements the hook returns non-zero; LSM is "restrictive". At that point SELinux or the other LSM would be the sole arbiter of lockdown decisions. SELinux or the other LSM also wouldn't have access to thekernel_locked_downlevel unless that was exported in some manner from the lockdown module. Not sure how to compose these.Right, I was envisaging the LSM replacing the default.
i.e. the default is tristate OR fine grained LSM policy.
Quoted 8 lines
They could in theory be composed restrictively, but this is likely not useful given the coarse grained default policy. All the LSM could do is either further restrict none or integrity.
We'd need to figure out how to avoid confusing users in the case where multiple LSMs are registered for the hooks, possibly by having the lockdown LSM gate this and update the securityfs lockdown node with something like "lsm:smack".
Some kind of transition from the lockdown module to other security modules might be needed, e.g. you might need to start with lockdown=integrity to protect the kernel up to the point where a policy is loaded, then hand off to SELinux or another security module to handle further requests.