Skip to content

Commit 60ba102

Browse files
committed
LoadPin: Refactor sysctl initialization
In preparation for shifting root mount when not enforcing, split sysctl logic out into a separate helper, and unconditionally register the sysctl, but only make it writable when the device is writable. Cc: Paul Moore <[email protected]> Cc: James Morris <[email protected]> Cc: "Serge E. Hallyn" <[email protected]> Cc: [email protected] Signed-off-by: Kees Cook <[email protected]> Reviewed-by: Serge Hallyn <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent b76ded2 commit 60ba102

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

security/loadpin/loadpin.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ static bool deny_reading_verity_digests;
5252
#endif
5353

5454
#ifdef CONFIG_SYSCTL
55-
5655
static struct ctl_path loadpin_sysctl_path[] = {
5756
{ .procname = "kernel", },
5857
{ .procname = "loadpin", },
@@ -66,18 +65,29 @@ static struct ctl_table loadpin_sysctl_table[] = {
6665
.maxlen = sizeof(int),
6766
.mode = 0644,
6867
.proc_handler = proc_dointvec_minmax,
69-
.extra1 = SYSCTL_ZERO,
68+
.extra1 = SYSCTL_ONE,
7069
.extra2 = SYSCTL_ONE,
7170
},
7271
{ }
7372
};
7473

75-
static void report_writable(struct super_block *mnt_sb, bool writable)
74+
static void set_sysctl(bool is_writable)
7675
{
7776
/*
7877
* If load pinning is not enforced via a read-only block
7978
* device, allow sysctl to change modes for testing.
8079
*/
80+
if (is_writable)
81+
loadpin_sysctl_table[0].extra1 = SYSCTL_ZERO;
82+
else
83+
loadpin_sysctl_table[0].extra1 = SYSCTL_ONE;
84+
}
85+
#else
86+
static inline void set_sysctl(bool is_writable) { }
87+
#endif
88+
89+
static void report_writable(struct super_block *mnt_sb, bool writable)
90+
{
8191
if (mnt_sb->s_bdev) {
8292
pr_info("%pg (%u:%u): %s\n", mnt_sb->s_bdev,
8393
MAJOR(mnt_sb->s_bdev->bd_dev),
@@ -86,21 +96,9 @@ static void report_writable(struct super_block *mnt_sb, bool writable)
8696
} else
8797
pr_info("mnt_sb lacks block device, treating as: writable\n");
8898

89-
if (writable) {
90-
if (!register_sysctl_paths(loadpin_sysctl_path,
91-
loadpin_sysctl_table))
92-
pr_notice("sysctl registration failed!\n");
93-
else
94-
pr_info("enforcement can be disabled.\n");
95-
} else
99+
if (!writable)
96100
pr_info("load pinning engaged.\n");
97101
}
98-
#else
99-
static void report_writable(struct super_block *mnt_sb, bool writable)
100-
{
101-
pr_info("load pinning engaged.\n");
102-
}
103-
#endif
104102

105103
/*
106104
* This must be called after early kernel init, since then the rootdev
@@ -172,6 +170,7 @@ static int loadpin_check(struct file *file, enum kernel_read_file_id id)
172170
*/
173171
spin_unlock(&pinned_root_spinlock);
174172
report_writable(pinned_root, load_root_writable);
173+
set_sysctl(load_root_writable);
175174
report_load(origin, file, "pinned");
176175
} else {
177176
spin_unlock(&pinned_root_spinlock);
@@ -259,6 +258,10 @@ static int __init loadpin_init(void)
259258
pr_info("ready to pin (currently %senforcing)\n",
260259
enforce ? "" : "not ");
261260
parse_exclude();
261+
#ifdef CONFIG_SYSCTL
262+
if (!register_sysctl_paths(loadpin_sysctl_path, loadpin_sysctl_table))
263+
pr_notice("sysctl registration failed!\n");
264+
#endif
262265
security_add_hooks(loadpin_hooks, ARRAY_SIZE(loadpin_hooks), "loadpin");
263266

264267
return 0;

0 commit comments

Comments
 (0)