Skip to content

Commit 4bd6a73

Browse files
Matthew Wilcox (Oracle)Al Viro
authored andcommitted
sysctl: Convert to iter interfaces
Using the read_iter/write_iter interfaces allows for in-kernel users to set sysctls without using set_fs(). Also, the buffer is a string, so give it the real type of 'char *', not void *. [AV: Christoph's fixup folded in] Signed-off-by: Matthew Wilcox (Oracle) <[email protected]> Signed-off-by: Christoph Hellwig <[email protected]> Signed-off-by: Al Viro <[email protected]>
1 parent fd5a13f commit 4bd6a73

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

fs/proc/proc_sysctl.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/cred.h>
1313
#include <linux/namei.h>
1414
#include <linux/mm.h>
15+
#include <linux/uio.h>
1516
#include <linux/module.h>
1617
#include <linux/bpf-cgroup.h>
1718
#include <linux/mount.h>
@@ -540,13 +541,14 @@ static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
540541
return err;
541542
}
542543

543-
static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf,
544-
size_t count, loff_t *ppos, int write)
544+
static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter,
545+
int write)
545546
{
546-
struct inode *inode = file_inode(filp);
547+
struct inode *inode = file_inode(iocb->ki_filp);
547548
struct ctl_table_header *head = grab_header(inode);
548549
struct ctl_table *table = PROC_I(inode)->sysctl_entry;
549-
void *kbuf;
550+
size_t count = iov_iter_count(iter);
551+
char *kbuf;
550552
ssize_t error;
551553

552554
if (IS_ERR(head))
@@ -569,32 +571,30 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf,
569571
error = -ENOMEM;
570572
if (count >= KMALLOC_MAX_SIZE)
571573
goto out;
574+
kbuf = kzalloc(count + 1, GFP_KERNEL);
575+
if (!kbuf)
576+
goto out;
572577

573578
if (write) {
574-
kbuf = memdup_user_nul(ubuf, count);
575-
if (IS_ERR(kbuf)) {
576-
error = PTR_ERR(kbuf);
577-
goto out;
578-
}
579-
} else {
580-
kbuf = kzalloc(count, GFP_KERNEL);
581-
if (!kbuf)
582-
goto out;
579+
error = -EFAULT;
580+
if (!copy_from_iter_full(kbuf, count, iter))
581+
goto out_free_buf;
582+
kbuf[count] = '\0';
583583
}
584584

585585
error = BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, &kbuf, &count,
586-
ppos);
586+
&iocb->ki_pos);
587587
if (error)
588588
goto out_free_buf;
589589

590590
/* careful: calling conventions are nasty here */
591-
error = table->proc_handler(table, write, kbuf, &count, ppos);
591+
error = table->proc_handler(table, write, kbuf, &count, &iocb->ki_pos);
592592
if (error)
593593
goto out_free_buf;
594594

595595
if (!write) {
596596
error = -EFAULT;
597-
if (copy_to_user(ubuf, kbuf, count))
597+
if (copy_to_iter(kbuf, count, iter) < count)
598598
goto out_free_buf;
599599
}
600600

@@ -607,16 +607,14 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf,
607607
return error;
608608
}
609609

610-
static ssize_t proc_sys_read(struct file *filp, char __user *buf,
611-
size_t count, loff_t *ppos)
610+
static ssize_t proc_sys_read(struct kiocb *iocb, struct iov_iter *iter)
612611
{
613-
return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
612+
return proc_sys_call_handler(iocb, iter, 0);
614613
}
615614

616-
static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
617-
size_t count, loff_t *ppos)
615+
static ssize_t proc_sys_write(struct kiocb *iocb, struct iov_iter *iter)
618616
{
619-
return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
617+
return proc_sys_call_handler(iocb, iter, 1);
620618
}
621619

622620
static int proc_sys_open(struct inode *inode, struct file *filp)
@@ -853,8 +851,10 @@ static int proc_sys_getattr(const struct path *path, struct kstat *stat,
853851
static const struct file_operations proc_sys_file_operations = {
854852
.open = proc_sys_open,
855853
.poll = proc_sys_poll,
856-
.read = proc_sys_read,
857-
.write = proc_sys_write,
854+
.read_iter = proc_sys_read,
855+
.write_iter = proc_sys_write,
856+
.splice_read = generic_file_splice_read,
857+
.splice_write = iter_file_splice_write,
858858
.llseek = default_llseek,
859859
};
860860

include/linux/bpf-cgroup.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
136136

137137
int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head,
138138
struct ctl_table *table, int write,
139-
void **buf, size_t *pcount, loff_t *ppos,
139+
char **buf, size_t *pcount, loff_t *ppos,
140140
enum bpf_attach_type type);
141141

142142
int __cgroup_bpf_run_filter_setsockopt(struct sock *sock, int *level,

kernel/bpf/cgroup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,7 @@ const struct bpf_verifier_ops cg_dev_verifier_ops = {
12261226
*/
12271227
int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head,
12281228
struct ctl_table *table, int write,
1229-
void **buf, size_t *pcount, loff_t *ppos,
1229+
char **buf, size_t *pcount, loff_t *ppos,
12301230
enum bpf_attach_type type)
12311231
{
12321232
struct bpf_sysctl_kern ctx = {

0 commit comments

Comments
 (0)