Skip to content

Commit 3d43321

Browse files
keesxjamesmorris
authored andcommitted
modules: sysctl to block module loading
Implement a sysctl file that disables module-loading system-wide since there is no longer a viable way to remove CAP_SYS_MODULE after the system bounding capability set was removed in 2.6.25. Value can only be set to "1", and is tested only if standard capability checks allow CAP_SYS_MODULE. Given existing /dev/mem protections, this should allow administrators a one-way method to block module loading after initial boot-time module loading has finished. Signed-off-by: Kees Cook <kees.cook@canonical.com> Acked-by: Serge Hallyn <serue@us.ibm.com> Signed-off-by: James Morris <jmorris@namei.org>
1 parent 8a6f83a commit 3d43321

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

‎Documentation/sysctl/kernel.txt‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ show up in /proc/sys/kernel:
3030
- kstack_depth_to_print [ X86 only ]
3131
- l2cr [ PPC only ]
3232
- modprobe ==> Documentation/debugging-modules.txt
33+
- modules_disabled
3334
- msgmax
3435
- msgmnb
3536
- msgmni
@@ -179,6 +180,16 @@ kernel stack.
179180

180181
==============================================================
181182

183+
modules_disabled:
184+
185+
A toggle value indicating if modules are allowed to be loaded
186+
in an otherwise modular kernel. This toggle defaults to off
187+
(0), but can be set true (1). Once true, modules can be
188+
neither loaded nor unloaded, and the toggle cannot be set back
189+
to false.
190+
191+
==============================================================
192+
182193
osrelease, ostype & version:
183194

184195
# cat osrelease

‎kernel/module.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,14 +778,17 @@ static void wait_for_zero_refcount(struct module *mod)
778778
mutex_lock(&module_mutex);
779779
}
780780

781+
/* Block module loading/unloading? */
782+
int modules_disabled = 0;
783+
781784
SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
782785
unsigned int, flags)
783786
{
784787
struct module *mod;
785788
char name[MODULE_NAME_LEN];
786789
int ret, forced = 0;
787790

788-
if (!capable(CAP_SYS_MODULE))
791+
if (!capable(CAP_SYS_MODULE) || modules_disabled)
789792
return -EPERM;
790793

791794
if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
@@ -2349,7 +2352,7 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
23492352
int ret = 0;
23502353

23512354
/* Must have permission */
2352-
if (!capable(CAP_SYS_MODULE))
2355+
if (!capable(CAP_SYS_MODULE) || modules_disabled)
23532356
return -EPERM;
23542357

23552358
/* Only one module load at a time, please */

‎kernel/sysctl.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ static int ngroups_max = NGROUPS_MAX;
113113

114114
#ifdef CONFIG_MODULES
115115
extern char modprobe_path[];
116+
extern int modules_disabled;
116117
#endif
117118
#ifdef CONFIG_CHR_DEV_SG
118119
extern int sg_big_buff;
@@ -533,6 +534,17 @@ static struct ctl_table kern_table[] = {
533534
.proc_handler = &proc_dostring,
534535
.strategy = &sysctl_string,
535536
},
537+
{
538+
.ctl_name = CTL_UNNUMBERED,
539+
.procname = "modules_disabled",
540+
.data = &modules_disabled,
541+
.maxlen = sizeof(int),
542+
.mode = 0644,
543+
/* only handle a transition from default "0" to "1" */
544+
.proc_handler = &proc_dointvec_minmax,
545+
.extra1 = &one,
546+
.extra2 = &one,
547+
},
536548
#endif
537549
#if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
538550
{

0 commit comments

Comments
 (0)