Skip to content

Commit b76ded2

Browse files
committed
LoadPin: Refactor read-only check into a helper
In preparation for allowing mounts to shift when not enforced, move read-only checking into a separate helper. 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 b5fc3ca commit b76ded2

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

security/loadpin/loadpin.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,28 +72,21 @@ static struct ctl_table loadpin_sysctl_table[] = {
7272
{ }
7373
};
7474

75-
/*
76-
* This must be called after early kernel init, since then the rootdev
77-
* is available.
78-
*/
79-
static void check_pinning_enforcement(struct super_block *mnt_sb)
75+
static void report_writable(struct super_block *mnt_sb, bool writable)
8076
{
81-
bool ro = false;
82-
8377
/*
8478
* If load pinning is not enforced via a read-only block
8579
* device, allow sysctl to change modes for testing.
8680
*/
8781
if (mnt_sb->s_bdev) {
88-
ro = bdev_read_only(mnt_sb->s_bdev);
8982
pr_info("%pg (%u:%u): %s\n", mnt_sb->s_bdev,
9083
MAJOR(mnt_sb->s_bdev->bd_dev),
9184
MINOR(mnt_sb->s_bdev->bd_dev),
92-
ro ? "read-only" : "writable");
85+
writable ? "writable" : "read-only");
9386
} else
9487
pr_info("mnt_sb lacks block device, treating as: writable\n");
9588

96-
if (!ro) {
89+
if (writable) {
9790
if (!register_sysctl_paths(loadpin_sysctl_path,
9891
loadpin_sysctl_table))
9992
pr_notice("sysctl registration failed!\n");
@@ -103,12 +96,26 @@ static void check_pinning_enforcement(struct super_block *mnt_sb)
10396
pr_info("load pinning engaged.\n");
10497
}
10598
#else
106-
static void check_pinning_enforcement(struct super_block *mnt_sb)
99+
static void report_writable(struct super_block *mnt_sb, bool writable)
107100
{
108101
pr_info("load pinning engaged.\n");
109102
}
110103
#endif
111104

105+
/*
106+
* This must be called after early kernel init, since then the rootdev
107+
* is available.
108+
*/
109+
static bool sb_is_writable(struct super_block *mnt_sb)
110+
{
111+
bool writable = true;
112+
113+
if (mnt_sb->s_bdev)
114+
writable = !bdev_read_only(mnt_sb->s_bdev);
115+
116+
return writable;
117+
}
118+
112119
static void loadpin_sb_free_security(struct super_block *mnt_sb)
113120
{
114121
/*
@@ -126,6 +133,7 @@ static int loadpin_check(struct file *file, enum kernel_read_file_id id)
126133
{
127134
struct super_block *load_root;
128135
const char *origin = kernel_read_file_id_str(id);
136+
bool load_root_writable;
129137

130138
/* If the file id is excluded, ignore the pinning. */
131139
if ((unsigned int)id < ARRAY_SIZE(ignore_read_file_id) &&
@@ -146,6 +154,7 @@ static int loadpin_check(struct file *file, enum kernel_read_file_id id)
146154
}
147155

148156
load_root = file->f_path.mnt->mnt_sb;
157+
load_root_writable = sb_is_writable(load_root);
149158

150159
/* First loaded module/firmware defines the root for all others. */
151160
spin_lock(&pinned_root_spinlock);
@@ -162,7 +171,7 @@ static int loadpin_check(struct file *file, enum kernel_read_file_id id)
162171
* enforcing. This would be purely cosmetic.
163172
*/
164173
spin_unlock(&pinned_root_spinlock);
165-
check_pinning_enforcement(pinned_root);
174+
report_writable(pinned_root, load_root_writable);
166175
report_load(origin, file, "pinned");
167176
} else {
168177
spin_unlock(&pinned_root_spinlock);

0 commit comments

Comments
 (0)