From patchwork Mon Jul 29 20:43:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 13745781 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5D41B18E762; Mon, 29 Jul 2024 20:43:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722285834; cv=none; b=fZ1dNDq8VOkuUfTzp4WRisQVV5r648VFegh8A2NgRGoSOIIQd12q3Zs7m8xeQBDap4FWHHfBWThSsdv2Uew/hqnmo6OnzsyJYNWnAWuyUCXguW2PLXUEWOJgt6SK+Fokik9aWXoT0QoEhaAl7jf4jN5fBhV12NDbEkftQcEyawg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722285834; c=relaxed/simple; bh=BKEQT8+KYO2Lx+4PYMNXeCjW8FDuqJOIi+Cq4x3uRkQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=qvNN0KDyDDCCktdrL5GMCnLRy7w+c9ysT2AgxEuQDW+pKk4LvAZ/1zkPkpcItJmpXmSFqyNEZwokuVo6wZnZhAx9n52xhKn1S5N903ie2BXDbvdp2eJj6q2vIyQwZCijwkm59o52zrrs4Tqfq45XoibEH4gRO6aJ30po1fd9S9I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=J3oxi84t; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="J3oxi84t" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1722285818; bh=BKEQT8+KYO2Lx+4PYMNXeCjW8FDuqJOIi+Cq4x3uRkQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=J3oxi84tTAvzqz4jmYEfFLCbaxJaRJSr6/V/xRBnLQFh4ZgSRJKXK+oTkGTn5+FaJ oYkQUPdyB5rnwPOcahsiSoxIRX8C8I3LdDo83wRTzVMWnPtY8f86QrKptpyUb2D3q7 84I4Fp8F+ymJiXdVanMZ6iT5HcDcvQuF1zVQF5pw= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Mon, 29 Jul 2024 22:43:30 +0200 Subject: [PATCH 1/5] bpf: Constify ctl_table argument of filter function Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240729-sysctl-const-api-v1-1-ca628c7a942c@weissschuh.net> References: <20240729-sysctl-const-api-v1-0-ca628c7a942c@weissschuh.net> In-Reply-To: <20240729-sysctl-const-api-v1-0-ca628c7a942c@weissschuh.net> To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Luis Chamberlain , Kees Cook , Joel Granados Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1722285818; l=1677; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=BKEQT8+KYO2Lx+4PYMNXeCjW8FDuqJOIi+Cq4x3uRkQ=; b=V0Qa4gSui+7cgZ5N4mQ0QnvGulTtQsNALhcaFCC5bM57lCvZZFFvU2qkRds6yqjax9Apm9nqT 9dZvvmZ7vO/A0NE1xZy8d96zTMTvfuDlDYQMsaK8wNk+USsANsZc6uc X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= 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. Signed-off-by: Thomas Weißschuh --- include/linux/bpf-cgroup.h | 2 +- kernel/bpf/cgroup.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h index fb3c3e7181e6..814a9d1c4a84 100644 --- a/include/linux/bpf-cgroup.h +++ b/include/linux/bpf-cgroup.h @@ -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); diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c index 8ba73042a239..429a43746886 100644 --- a/kernel/bpf/cgroup.c +++ b/kernel/bpf/cgroup.c @@ -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) { From patchwork Mon Jul 29 20:43:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 13745777 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 20CE5189F20; Mon, 29 Jul 2024 20:43:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722285830; cv=none; b=jpzfYs82O8a5nGAv+f7iU9B2JfdGyJ7NLQkFZidOep0IEalZncxt3IC7MPUSLI09NVtaZiP5MuwLzDL8QGiWWumx6Mpl1Z0K6INvnprE9BlsoXUWxM2/2YFhEmzOKA9yNTVFKNKCtIPk0/KUsa06tXV33hoKx3yPI4LZLJElTAU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722285830; c=relaxed/simple; bh=QBMFBfHQ0hS4XM/axBbjz4zqGkS9cNCQXu14+iU6vAc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ToJVwK12THCVnnmP6S38cl3/cpxQYwIIR+gVgHIsmXG0BdH1R/kzwqpGGoT3w/YSQEzks2Xko7m0U/n+XiPNShREkKYD2i4xhG+wsUS1XkSOrhJULZ1X/n3pTOCHmRB7Fse3F+9MkNr/gyfXruCL7rNeRxYcosF8SzM1WyW87nE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=HfHSfjsf; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="HfHSfjsf" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1722285818; bh=QBMFBfHQ0hS4XM/axBbjz4zqGkS9cNCQXu14+iU6vAc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=HfHSfjsfSJ5wBaDRlBDCzJSjPMBUrEQ1Pie0bNmpgETRV3ffuIgWcyTk+H58BTDli 3EcGpmDV+XWRZPDqlUvbTsRnb9ndhnKvBYcz8tiR40plWvfIISNe1r6yEOv5vcpXaz 7kOdFOAght1CjIv2DpIaEo6qKJk4T7m+ESwP/YeI= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Mon, 29 Jul 2024 22:43:31 +0200 Subject: [PATCH 2/5] sysctl: move internal interfaces to const struct ctl_table Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240729-sysctl-const-api-v1-2-ca628c7a942c@weissschuh.net> References: <20240729-sysctl-const-api-v1-0-ca628c7a942c@weissschuh.net> In-Reply-To: <20240729-sysctl-const-api-v1-0-ca628c7a942c@weissschuh.net> To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Luis Chamberlain , Kees Cook , Joel Granados Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1722285818; l=13731; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=QBMFBfHQ0hS4XM/axBbjz4zqGkS9cNCQXu14+iU6vAc=; b=ccf8CH6WUxmPeg+phZkpqq4ufYqGDmu8TAea+RQFE10vJbdlQyrDogZKu7v2l2R2TDkjWT0GH /gbfX8Amw/wAsvRe7Gbq+93zZgd3xA7ote4I97unpYHBJf5AMxy+a6G X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= As 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. Signed-off-by: Thomas Weißschuh --- fs/proc/internal.h | 2 +- fs/proc/proc_sysctl.c | 81 +++++++++++++++++++++++++------------------------- include/linux/sysctl.h | 2 +- 3 files changed, 43 insertions(+), 42 deletions(-) diff --git a/fs/proc/internal.h b/fs/proc/internal.h index a8a8576d8592..fcab5dd7ddb1 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -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; diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c index 9553e77c9d31..fa8cf9bf2988 100644 --- a/fs/proc/proc_sysctl.c +++ b/fs/proc/proc_sysctl.c @@ -83,7 +83,7 @@ static DEFINE_SPINLOCK(sysctl_lock); static void drop_sysctl_table(struct ctl_table_header *header); static int sysctl_follow_link(struct ctl_table_header **phead, - struct ctl_table **pentry); + const struct ctl_table **pentry); static int insert_links(struct ctl_table_header *head); static void put_links(struct ctl_table_header *header); @@ -105,11 +105,11 @@ static int namecmp(const char *name1, int len1, const char *name2, int len2) } /* Called under sysctl_lock */ -static struct ctl_table *find_entry(struct ctl_table_header **phead, +static const struct ctl_table *find_entry(struct ctl_table_header **phead, struct ctl_dir *dir, const char *name, int namelen) { struct ctl_table_header *head; - struct ctl_table *entry; + const struct ctl_table *entry; struct rb_node *node = dir->root.rb_node; while (node) @@ -136,7 +136,7 @@ static struct ctl_table *find_entry(struct ctl_table_header **phead, return NULL; } -static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry) +static int insert_entry(struct ctl_table_header *head, const struct ctl_table *entry) { struct rb_node *node = &head->node[entry - head->ctl_table].node; struct rb_node **p = &head->parent->root.rb_node; @@ -146,7 +146,7 @@ static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry) while (*p) { struct ctl_table_header *parent_head; - struct ctl_table *parent_entry; + const struct ctl_table *parent_entry; struct ctl_node *parent_node; const char *parent_name; int cmp; @@ -175,7 +175,7 @@ static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry) return 0; } -static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry) +static void erase_entry(struct ctl_table_header *head, const struct ctl_table *entry) { struct rb_node *node = &head->node[entry - head->ctl_table].node; @@ -184,7 +184,7 @@ static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry) static void init_header(struct ctl_table_header *head, struct ctl_table_root *root, struct ctl_table_set *set, - struct ctl_node *node, struct ctl_table *table, size_t table_size) + struct ctl_node *node, const struct ctl_table *table, size_t table_size) { head->ctl_table = table; head->ctl_table_size = table_size; @@ -199,7 +199,7 @@ static void init_header(struct ctl_table_header *head, head->node = node; INIT_HLIST_HEAD(&head->inodes); if (node) { - struct ctl_table *entry; + const struct ctl_table *entry; list_for_each_table_entry(entry, head) { node->header = head; @@ -212,7 +212,7 @@ static void init_header(struct ctl_table_header *head, static void erase_header(struct ctl_table_header *head) { - struct ctl_table *entry; + const struct ctl_table *entry; list_for_each_table_entry(entry, head) erase_entry(head, entry); @@ -220,7 +220,7 @@ static void erase_header(struct ctl_table_header *head) static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header) { - struct ctl_table *entry; + const struct ctl_table *entry; struct ctl_table_header *dir_h = &dir->header; int err; @@ -339,12 +339,12 @@ lookup_header_set(struct ctl_table_root *root) return set; } -static struct ctl_table *lookup_entry(struct ctl_table_header **phead, - struct ctl_dir *dir, - const char *name, int namelen) +static const struct ctl_table *lookup_entry(struct ctl_table_header **phead, + struct ctl_dir *dir, + const char *name, int namelen) { struct ctl_table_header *head; - struct ctl_table *entry; + const struct ctl_table *entry; spin_lock(&sysctl_lock); entry = find_entry(&head, dir, name, namelen); @@ -369,10 +369,10 @@ static struct ctl_node *first_usable_entry(struct rb_node *node) } static void first_entry(struct ctl_dir *dir, - struct ctl_table_header **phead, struct ctl_table **pentry) + struct ctl_table_header **phead, const struct ctl_table **pentry) { struct ctl_table_header *head = NULL; - struct ctl_table *entry = NULL; + const struct ctl_table *entry = NULL; struct ctl_node *ctl_node; spin_lock(&sysctl_lock); @@ -386,10 +386,10 @@ static void first_entry(struct ctl_dir *dir, *pentry = entry; } -static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry) +static void next_entry(struct ctl_table_header **phead, const struct ctl_table **pentry) { struct ctl_table_header *head = *phead; - struct ctl_table *entry = *pentry; + const struct ctl_table *entry = *pentry; struct ctl_node *ctl_node = &head->node[entry - head->ctl_table]; spin_lock(&sysctl_lock); @@ -422,7 +422,7 @@ static int test_perm(int mode, int op) return -EACCES; } -static int sysctl_perm(struct ctl_table_header *head, struct ctl_table *table, int op) +static int sysctl_perm(struct ctl_table_header *head, const struct ctl_table *table, int op) { struct ctl_table_root *root = head->root; int mode; @@ -436,7 +436,7 @@ static int sysctl_perm(struct ctl_table_header *head, struct ctl_table *table, i } static struct inode *proc_sys_make_inode(struct super_block *sb, - struct ctl_table_header *head, struct ctl_table *table) + struct ctl_table_header *head, const struct ctl_table *table) { struct ctl_table_root *root = head->root; struct inode *inode; @@ -507,7 +507,7 @@ static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry, struct ctl_table_header *head = grab_header(dir); struct ctl_table_header *h = NULL; const struct qstr *name = &dentry->d_name; - struct ctl_table *p; + const struct ctl_table *p; struct inode *inode; struct dentry *err = ERR_PTR(-ENOENT); struct ctl_dir *ctl_dir; @@ -545,7 +545,7 @@ static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter, { struct inode *inode = file_inode(iocb->ki_filp); struct ctl_table_header *head = grab_header(inode); - struct ctl_table *table = PROC_I(inode)->sysctl_entry; + const struct ctl_table *table = PROC_I(inode)->sysctl_entry; size_t count = iov_iter_count(iter); char *kbuf; ssize_t error; @@ -619,7 +619,7 @@ static ssize_t proc_sys_write(struct kiocb *iocb, struct iov_iter *iter) static int proc_sys_open(struct inode *inode, struct file *filp) { struct ctl_table_header *head = grab_header(inode); - struct ctl_table *table = PROC_I(inode)->sysctl_entry; + const struct ctl_table *table = PROC_I(inode)->sysctl_entry; /* sysctl was unregistered */ if (IS_ERR(head)) @@ -637,7 +637,7 @@ static __poll_t proc_sys_poll(struct file *filp, poll_table *wait) { struct inode *inode = file_inode(filp); struct ctl_table_header *head = grab_header(inode); - struct ctl_table *table = PROC_I(inode)->sysctl_entry; + const struct ctl_table *table = PROC_I(inode)->sysctl_entry; __poll_t ret = DEFAULT_POLLMASK; unsigned long event; @@ -668,7 +668,7 @@ static __poll_t proc_sys_poll(struct file *filp, poll_table *wait) static bool proc_sys_fill_cache(struct file *file, struct dir_context *ctx, struct ctl_table_header *head, - struct ctl_table *table) + const struct ctl_table *table) { struct dentry *child, *dir = file->f_path.dentry; struct inode *inode; @@ -712,7 +712,7 @@ static bool proc_sys_fill_cache(struct file *file, static bool proc_sys_link_fill_cache(struct file *file, struct dir_context *ctx, struct ctl_table_header *head, - struct ctl_table *table) + const struct ctl_table *table) { bool ret = true; @@ -730,7 +730,7 @@ static bool proc_sys_link_fill_cache(struct file *file, return ret; } -static int scan(struct ctl_table_header *head, struct ctl_table *table, +static int scan(struct ctl_table_header *head, const struct ctl_table *table, unsigned long *pos, struct file *file, struct dir_context *ctx) { @@ -754,7 +754,7 @@ static int proc_sys_readdir(struct file *file, struct dir_context *ctx) { struct ctl_table_header *head = grab_header(file_inode(file)); struct ctl_table_header *h = NULL; - struct ctl_table *entry; + const struct ctl_table *entry; struct ctl_dir *ctl_dir; unsigned long pos; @@ -787,7 +787,7 @@ static int proc_sys_permission(struct mnt_idmap *idmap, * are _NOT_ writeable, capabilities or not. */ struct ctl_table_header *head; - struct ctl_table *table; + const struct ctl_table *table; int error; /* Executable files are not allowed under /proc/sys/ */ @@ -831,7 +831,7 @@ static int proc_sys_getattr(struct mnt_idmap *idmap, { struct inode *inode = d_inode(path->dentry); struct ctl_table_header *head = grab_header(inode); - struct ctl_table *table = PROC_I(inode)->sysctl_entry; + const struct ctl_table *table = PROC_I(inode)->sysctl_entry; if (IS_ERR(head)) return PTR_ERR(head); @@ -930,7 +930,7 @@ static struct ctl_dir *find_subdir(struct ctl_dir *dir, const char *name, int namelen) { struct ctl_table_header *head; - struct ctl_table *entry; + const struct ctl_table *entry; entry = find_entry(&head, dir, name, namelen); if (!entry) @@ -1041,12 +1041,12 @@ static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir) } static int sysctl_follow_link(struct ctl_table_header **phead, - struct ctl_table **pentry) + const struct ctl_table **pentry) { struct ctl_table_header *head; + const struct ctl_table *entry; struct ctl_table_root *root; struct ctl_table_set *set; - struct ctl_table *entry; struct ctl_dir *dir; int ret; @@ -1073,7 +1073,7 @@ static int sysctl_follow_link(struct ctl_table_header **phead, return ret; } -static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...) +static int sysctl_err(const char *path, const struct ctl_table *table, char *fmt, ...) { struct va_format vaf; va_list args; @@ -1089,7 +1089,7 @@ static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...) return -EINVAL; } -static int sysctl_check_table_array(const char *path, struct ctl_table *table) +static int sysctl_check_table_array(const char *path, const struct ctl_table *table) { unsigned int extra; int err = 0; @@ -1128,7 +1128,7 @@ static int sysctl_check_table_array(const char *path, struct ctl_table *table) static int sysctl_check_table(const char *path, struct ctl_table_header *header) { - struct ctl_table *entry; + const struct ctl_table *entry; int err = 0; list_for_each_table_entry(entry, header) { if (!entry->procname) @@ -1164,8 +1164,9 @@ static int sysctl_check_table(const char *path, struct ctl_table_header *header) static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table_header *head) { - struct ctl_table *link_table, *entry, *link; + struct ctl_table *link_table, *link; struct ctl_table_header *links; + const struct ctl_table *entry; struct ctl_node *node; char *link_name; int name_bytes; @@ -1210,7 +1211,7 @@ static bool get_links(struct ctl_dir *dir, struct ctl_table_root *link_root) { struct ctl_table_header *tmp_head; - struct ctl_table *entry, *link; + const struct ctl_table *entry, *link; if (header->ctl_table_size == 0 || sysctl_is_perm_empty_ctl_header(header)) @@ -1461,7 +1462,7 @@ static void put_links(struct ctl_table_header *header) struct ctl_table_root *root = header->root; struct ctl_dir *parent = header->parent; struct ctl_dir *core_parent; - struct ctl_table *entry; + const struct ctl_table *entry; if (header->set == root_set) return; @@ -1472,7 +1473,7 @@ static void put_links(struct ctl_table_header *header) list_for_each_table_entry(entry, header) { struct ctl_table_header *link_head; - struct ctl_table *link; + const struct ctl_table *link; const char *name = entry->procname; link = find_entry(&link_head, core_parent, name, strlen(name)); diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h index aa4c6d44aaa0..a473deaf5a91 100644 --- a/include/linux/sysctl.h +++ b/include/linux/sysctl.h @@ -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; From patchwork Mon Jul 29 20:43:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 13745780 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 20C4A1891B8; Mon, 29 Jul 2024 20:43:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722285831; cv=none; b=Wk6VLvHplY0HhWpafDu0laXMUbW1ZCn/FhUeZed7UjgTRcyOi+vQRQ+irzJYnN/vzIL5H+wEp2N6Cd1Gmgtx9re9jpDPpJpmHRhGzC2x/NwGJw7MnUGK5iRbdzyIWPZ7TU6DoE/nWXM2a3xUjEEiMHsXmUIvyRflejYkEIViHRw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722285831; c=relaxed/simple; bh=eBR9mLKyR+1Vm40w0K6RV+ewAxwcZvcVWHkLIwPlM/c=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=uNTrFZPcI/Hc7VMzQUIN0mRbiXERJQn9pHv28ByZOslwz+iGMgJnS9d8IZyAHur81i6XzAD2qu0DBJKoM122FFVBdf+iOP3GDTIq3tdIkXe14iphkGjgl9b5nicuCVgmMOWCCmusn2dD71ZyBH594vOq4tdrcOvkjvi6DaYgJNk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=hiPVmPQQ; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="hiPVmPQQ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1722285818; bh=eBR9mLKyR+1Vm40w0K6RV+ewAxwcZvcVWHkLIwPlM/c=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=hiPVmPQQxh3rtvNeRv0hFqYq9KcWd7pJChdrwRIGQOi3A74Qmxk8QKw/RequWo3Xl 3jg+isS+iDZj22KdfqnNiBqz7jqYEFJ3CLA653JG1jO102eLbYrhdTMfKafBRJkF0U SLjkTyRVxzIWjYNVtk7ZtAA1svXs0gIHZf0jjCZw= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Mon, 29 Jul 2024 22:43:32 +0200 Subject: [PATCH 3/5] sysctl: allow registration of const struct ctl_table Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240729-sysctl-const-api-v1-3-ca628c7a942c@weissschuh.net> References: <20240729-sysctl-const-api-v1-0-ca628c7a942c@weissschuh.net> In-Reply-To: <20240729-sysctl-const-api-v1-0-ca628c7a942c@weissschuh.net> To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Luis Chamberlain , Kees Cook , Joel Granados Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1722285818; l=3585; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=eBR9mLKyR+1Vm40w0K6RV+ewAxwcZvcVWHkLIwPlM/c=; b=gKC2S9/35q9gVhQwvAeGADioL+dzgjlSbKVf6WTY1O/kWKfJD5cjNvyaMKA2nPOL00Tw/jUW7 TQpFe01SYtQDBFRRtH/ij4kSX8gP+SqfCIuAXy8VoL2K7y2S8ISikCj X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Putting 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". Signed-off-by: Thomas Weißschuh --- fs/proc/proc_sysctl.c | 6 +++--- include/linux/sysctl.h | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c index fa8cf9bf2988..01e006cc1163 100644 --- a/fs/proc/proc_sysctl.c +++ b/fs/proc/proc_sysctl.c @@ -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); diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h index a473deaf5a91..202855befa8b 100644 --- a/include/linux/sysctl.h +++ b/include/linux/sysctl.h @@ -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; From patchwork Mon Jul 29 20:43:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 13745778 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DC4C716DEDA; Mon, 29 Jul 2024 20:43:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722285831; cv=none; b=tZXcUNNCMYmLLUEjINoCP1CxLtyOkL5yTotQxfzcVms+hV0tJMSMO+yTvvkwHeHPleQa2Xn1Ra8d1iiUKf9bKD/HorDN6RCSl6irxNKGn04c0HgXzXMhYNd6OklCWGIOdvs2nv5jSiZNqwFhAeHo0ZDJJV4EbB57ng4pRpyIN7k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722285831; c=relaxed/simple; bh=qZXkT1m6kcK7yZXSMPzw5QiS2oUJ7uAmwCFZNL/ZQwc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=pEqU6OXAOrqSv1efpUYohj1ow5fjiYsCxasdwcuLbVLwde8dBlUYdOj5Q46knL1p+Z11DhGy/WnBzOadZhAMkGfgQLTm0SF+EHFf4ZiiMcSZOLOhEkJCcVdkVO3Y0G+PnJVeCQfegk67rJQvA6x4UmloB0ZtlbZN+GY+bsutyck= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=TAN57LSb; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="TAN57LSb" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1722285818; bh=qZXkT1m6kcK7yZXSMPzw5QiS2oUJ7uAmwCFZNL/ZQwc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=TAN57LSb24MSSTuw/+AQnemeLldOcxXfpH8wQCnzQwDOOJTH4YZfKfOHRBfB3Eahs OMKklB3As9UZsnU886s/yKG7aTPgMqZ/Yh/W5TrqXxiJgLt/CVs5ow46CVDRn5wYLJ tD6NRhKA8WZp2c8ZZQ+MYKpFFLJfQ8ZSEvfOynhk= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Mon, 29 Jul 2024 22:43:33 +0200 Subject: [PATCH 4/5] sysctl: make internal ctl_tables const Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240729-sysctl-const-api-v1-4-ca628c7a942c@weissschuh.net> References: <20240729-sysctl-const-api-v1-0-ca628c7a942c@weissschuh.net> In-Reply-To: <20240729-sysctl-const-api-v1-0-ca628c7a942c@weissschuh.net> To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Luis Chamberlain , Kees Cook , Joel Granados Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1722285818; l=1106; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=qZXkT1m6kcK7yZXSMPzw5QiS2oUJ7uAmwCFZNL/ZQwc=; b=LdQo1qto1d9jseDguzEBJD+8pNzCLgH7Su6S6XVBOzUVXTKdyotcY4uMr2iFMoaglir/9clog j3nxiIXB96UAyeTsjjAw+xLhp4lmp4dJ/xLcV6KfnGqe89Z7Bzc4Pj3 X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Now that the sysctl core can handle registration of "const struct ctl_table" constify the sysctl internal tables. Signed-off-by: Thomas Weißschuh --- fs/proc/proc_sysctl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c index 01e006cc1163..c6cf21436d6c 100644 --- a/fs/proc/proc_sysctl.c +++ b/fs/proc/proc_sysctl.c @@ -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, From patchwork Mon Jul 29 20:43:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 13745776 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 20C9E1891C6; Mon, 29 Jul 2024 20:43:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722285830; cv=none; b=CJcsEzK2QlxtyHqv04nJd6VdWhQOHDhPi2QIqTIFMIfMYQuEA/grCmR5DNkMzxRed7wzezy2HuoM7503WA14JTVON1ByQq1L1YwkFIXJBBreeZlf2ZUAhVEVgg7DgH3/PBFz0Ayu8imOgHPLQL5AUs3ihb9nRpdHAdpoGvdVyrA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722285830; c=relaxed/simple; bh=wBgdt4L4EyoDAU+mT9ij7hDNmnSqVWf3X3bm35G+jsQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Fsmy0bxagmKPlv6UZtVjcT5f4GdozY+QDJTiX/4NN3OZTfxGRVan+j2imeaNqKM8BLyKt/j6kQDCHlmECUOSThwHOfoOVk2EpN4j3m8iB8bjDfEzS62muSuoe/qGJFCoVbG8QCoYgm48yf59Ek8dQUQcFrYaA9Gga3N87TNx2lk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=TPzVjM2w; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="TPzVjM2w" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1722285819; bh=wBgdt4L4EyoDAU+mT9ij7hDNmnSqVWf3X3bm35G+jsQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=TPzVjM2wg7X8BQNIY5PrLqbPKjOoInXhnlpIzqrsA1AV0qKJ9H3uFGW75fwVoqNdo imQB3o14qh72nB03ckKlCUDGUmwp0CD3Oyr0rh5VONVMplWpgKpBhF+97lqZIdXC8b wRtt1jS6LPoESbwTOFSbZaoqTwpIv8S+gJR2NoPw= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Mon, 29 Jul 2024 22:43:34 +0200 Subject: [PATCH 5/5] const_structs.checkpatch: add ctl_table Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240729-sysctl-const-api-v1-5-ca628c7a942c@weissschuh.net> References: <20240729-sysctl-const-api-v1-0-ca628c7a942c@weissschuh.net> In-Reply-To: <20240729-sysctl-const-api-v1-0-ca628c7a942c@weissschuh.net> To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Luis Chamberlain , Kees Cook , Joel Granados Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1722285818; l=622; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=wBgdt4L4EyoDAU+mT9ij7hDNmnSqVWf3X3bm35G+jsQ=; b=b5gttQlm7Hfv4Ca/iEHJVjTqRqCqYvih6IXJUWLLtBn+wsC/8SK+SqdvzyZkZTPWw7QX7DNQs nyUPELfVUhjAzf0xB91FBLDs416whwpz15MFAcTw0rM6q7LZ7ITnCDj X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Now that the sysctl core can handle "const struct ctl_table", make sure that new usages of the struct already enter the tree as const. Signed-off-by: Thomas Weißschuh --- scripts/const_structs.checkpatch | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/const_structs.checkpatch b/scripts/const_structs.checkpatch index 014b3bfe3237..e8609a03c3d8 100644 --- a/scripts/const_structs.checkpatch +++ b/scripts/const_structs.checkpatch @@ -6,6 +6,7 @@ bus_type clk_ops comedi_lrange component_ops +ctl_table dentry_operations dev_pm_ops device_type