sysctl: prepare sysctl core for const struct ctl_table
Subsystems (5)
cover sysctl: prepare sysctl core for const struct ctl_table
Adapt the internal and external APIs of the sysctl core to handle read-only instances of "struct ctl_table".
Patch 1: Trivial preparation commit for the sysctl BPF hook Patch 2: Adapts the internal sysctl APIs
Patch 3: Adapts the external sysctl APIs
Patch 4: Constifies the sysctl internal tables as proof that it works Patch 5: Updates scripts/const_structs.checkpatch for "struct ctl_table"
Motivation
==========
Moving structures containing function pointers into unmodifiable .rodata prevents attackers or bugs from corrupting and diverting those pointers.
Also the "struct ctl_table" exposed by the sysctl core were never meant to be mutated by users.
For this goal changes to both the sysctl core and "const" qualifiers for various sysctl APIs are necessary.
Full Process
============
* Drop ctl_table modifications from the sysctl core ([0], in mainline)
* Constify arguments to ctl_table_root::{set_ownership,permissions}
([1], in mainline)
* Migrate users of "ctl_table_header::ctl_table_arg" to "const".
(in mainline)
* Afterwards convert "ctl_table_header::ctl_table_arg" itself to const.
(in mainline)
* Prepare helpers used to implement proc_handlers throughout the tree to use "const struct ctl_table *". ([2], in mainline)
* Afterwards switch over all proc_handlers callbacks to use
"const struct ctl_table *" in one commit. (in mainline)
* Switch over the internals of the sysctl core to "const struct ctl_table *" (this series)
* Switch include/linux/sysctl.h to "const struct ctl_table *" (this series)
* Transition instances of "struct ctl_table" through the tree to const (to be done)
This series is meant to be applied through the sysctl tree.
---
Thomas Weißschuh (5):
bpf: Constify ctl_table argument of filter function
sysctl: move internal interfaces to const struct ctl_table
sysctl: allow registration of const struct ctl_table
sysctl: make internal ctl_tables const const_structs.checkpatch: add ctl_table
fs/proc/internal.h | 2 +- fs/proc/proc_sysctl.c | 91 ++++++++++++++++++++-------------------- include/linux/bpf-cgroup.h | 2 +- include/linux/sysctl.h | 12 +++--- kernel/bpf/cgroup.c | 2 +- scripts/const_structs.checkpatch | 1 + 6 files changed, 56 insertions(+), 54 deletions(-)
---
base-commit: 8400291e289ee6b2bf9779ff1c83a291501f017b
change-id: 20240729-sysctl-const-api-73954f3d62c1
Best regards,
Signature · 1 line
Thomas Weißschuh <[email protected]>
PATCH 1/5 bpf: Constify ctl_table argument of filter function
The sysctl core is moving to allow "struct ctl_table" in read-only memory. As a preparation for that all functions handling "struct ctl_table" need to be able to work with "const struct ctl_table".
As __cgroup_bpf_run_filter_sysctl() does not modify its table, it can be adapted trivially.
--- include/linux/bpf-cgroup.h | 2 +- kernel/bpf/cgroup.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
include/linux/bpf-cgroup.h +1 -1
@@ -138,7 +138,7 @@ int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor, short access, enum cgroup_bpf_attach_type atype);int __cgroup_bpf_run_filter_sysctl (struct ctl_table_header *head, -struct ctl_table *table,int write, +const struct ctl_table *table,int write,char **buf,size_t *pcount,loff_t *ppos,enum cgroup_bpf_attach_type atype);
kernel/bpf/cgroup.c +1 -1
@@ -1691,7 +1691,7 @@ const struct bpf_verifier_ops cg_dev_verifier_ops = {* returned value != 1 during execution. In all other cases 0 is returned. */ int __cgroup_bpf_run_filter_sysctl (struct ctl_table_header *head, -struct ctl_table *table,int write, +const struct ctl_table *table,int write,char **buf,size_t *pcount,loff_t *ppos,enum cgroup_bpf_attach_type atype) {
Signature · 1 line
2.45.2
PATCH 2/5 sysctl: move internal interfaces to const struct ctl_table
In reply to Thomas WeißschuhAs a preparation to make all the core sysctl code work with const struct ctl_table switch over the internal function to use the const variant.
Some pointers to "struct ctl_table" need to stay non-const as they are newly allocated and modified before registration.
--- fs/proc/internal.h | 2 +- fs/proc/proc_sysctl.c | 81 +++++++++++++++++++++++++------------------------- include/linux/sysctl.h | 2 +- 3 files changed, 43 insertions(+), 42 deletions(-)
fs/proc/internal.h +1 -1
@@ -102,7 +102,7 @@ struct proc_inode {union proc_op op;struct proc_dir_entry *pde;struct ctl_table_header *sysctl; -struct ctl_table *sysctl_entry; +const struct ctl_table *sysctl_entry;struct hlist_node sibling_inodes;const struct proc_ns_operations *ns_ops;struct inode vfs_inode;
fs/proc/proc_sysctl.c +41 -40
293 lines not on this page · raw message
include/linux/sysctl.h +1 -1
@@ -162,7 +162,7 @@ struct ctl_node { struct ctl_table_header { union { struct { - struct ctl_table *ctl_table; + const struct ctl_table *ctl_table; int ctl_table_size; int used; int count;
Signature · 1 line
2.45.2
PATCH 3/5 sysctl: allow registration of const struct ctl_table
In reply to Thomas WeißschuhPutting structure, especially those containing function pointers, into read-only memory makes the safer and easier to reason about. Change the sysctl registration APIs to allow registration of "const struct ctl_table".
--- fs/proc/proc_sysctl.c | 6 +++--- include/linux/sysctl.h | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-)
fs/proc/proc_sysctl.c +3 -3
@@ -1354,7 +1354,7 @@ static struct ctl_dir *sysctl_mkdir_p(struct ctl_dir *dir, const char *path)*/ struct ctl_table_header *__register_sysctl_table (struct ctl_table_set *set, -const char *path,struct ctl_table *table,size_t table_size) +const char *path,const struct ctl_table *table,size_t table_size) {struct ctl_table_root *root = set->dir.header.root;struct ctl_table_header *header;
@@ -1415,7 +1415,7 @@ struct ctl_table_header *__register_sysctl_table(* * See __register_sysctl_table for more details. */ -struct ctl_table_header *register_sysctl_sz (const char *path,struct ctl_table *table, +struct ctl_table_header *register_sysctl_sz (const char *path,const struct ctl_table *table,size_t table_size) { return __register_sysctl_table(&sysctl_table_root.default_set,
@@ -1444,7 +1444,7 @@ EXPORT_SYMBOL(register_sysctl_sz);* * Context: if your base directory does not exist it will be created for you. */ -void __init __register_sysctl_init (const char *path,struct ctl_table *table, +void __init __register_sysctl_init (const char *path,const struct ctl_table *table,const char *table_name,size_t table_size) {struct ctl_table_header *hdr =register_sysctl_sz (path, table, table_size);
include/linux/sysctl.h +5 -5
@@ -223,13 +223,13 @@ extern void retire_sysctl_set(struct ctl_table_set *set);struct ctl_table_header *__register_sysctl_table (struct ctl_table_set *set, -const char *path,struct ctl_table *table,size_t table_size); -struct ctl_table_header *register_sysctl_sz (const char *path,struct ctl_table *table, +const char *path,const struct ctl_table *table,size_t table_size); +struct ctl_table_header *register_sysctl_sz (const char *path,const struct ctl_table *table,size_t table_size);void unregister_sysctl_table (struct ctl_table_header * table);extern int sysctl_init_bases (void ); -extern void __register_sysctl_init (const char *path,struct ctl_table *table, +extern void __register_sysctl_init (const char *path,const struct ctl_table *table,const char *table_name,size_t table_size);#define register_sysctl_init (path, table) \ __register_sysctl_init(path, table, #table, ARRAY_SIZE(table))
@@ -251,7 +251,7 @@ extern int no_unaligned_warning;#else /* CONFIG_SYSCTL */ -static inline void register_sysctl_init (const char *path,struct ctl_table *table) +static inline void register_sysctl_init (const char *path,const struct ctl_table *table) { }
@@ -261,7 +261,7 @@ static inline struct ctl_table_header *register_sysctl_mount_point(const char *p }static inline struct ctl_table_header *register_sysctl_sz (const char *path, -struct ctl_table *table, +const struct ctl_table *table,size_t table_size) {return NULL ;
Signature · 1 line
2.45.2
PATCH 4/5 sysctl: make internal ctl_tables const
In reply to Thomas WeißschuhNow that the sysctl core can handle registration of "const struct ctl_table" constify the sysctl internal tables.
--- fs/proc/proc_sysctl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/proc/proc_sysctl.c +2 -2
@@ -30,7 +30,7 @@ static const struct file_operations proc_sys_dir_file_operations;static const struct inode_operations proc_sys_dir_operations;/* Support for permanently empty directories */ -static struct ctl_table sysctl_mount_point[] = { }; +static const struct ctl_table sysctl_mount_point[] = { };/** * register_sysctl_mount_point() - registers a sysctl mount point
@@ -62,7 +62,7 @@ void proc_sys_poll_notify(struct ctl_table_poll *poll)wake_up_interruptible (&poll->wait); } -static struct ctl_table root_table[] = { +static const struct ctl_table root_table[] = { { .procname ="" , .mode =S_IFDIR |S_IRUGO |S_IXUGO ,
Signature · 1 line
2.45.2
PATCH 5/5 const_structs.checkpatch: add ctl_table
In reply to Thomas WeißschuhNow that the sysctl core can handle "const struct ctl_table", make sure that new usages of the struct already enter the tree as const.
--- scripts/const_structs.checkpatch | 1 + 1 file changed, 1 insertion(+)
scripts/const_structs.checkpatch +1 -0
@@ -6,6 +6,7 @@ bus_type clk_ops comedi_lrange component_ops +ctl_table dentry_operations dev_pm_ops device_type
Signature · 1 line
2.45.2