Cleanup of rw_copy_check_uvector and compat_rw_copy_check_uvector
Subsystems (6)
PATCH 1/1 Cleanup of rw_copy_check_uvector and compat_rw_copy_check_uvector
Hi Andrew,
This is the cleanup of rw_copy_check_uvector and compat_rw_copy_check_uvector discussed with Oleg a couple of weeks a go. Its a much better way of handling that the functions sometimes need to omit checking the memory that the iovecs point to compared to what I originally did for CMA.
Regards,
Chris
Signature · 11 lines
[email protected] Signed-off-by: Chris Yeoh <[email protected]> fs/aio.c | 4 ++-- fs/compat.c | 6 +++--- fs/read_write.c | 7 +++---- include/linux/compat.h | 3 +-- include/linux/fs.h | 10 ++++++++-- mm/process_vm_access.c | 16 ++++++++-------- security/keys/compat.c | 2 +- security/keys/keyctl.c | 2 +- 8 files changed, 27 insertions(+), 23 deletions(-)
fs/aio.c +2 -2
@@ -1467,13 +1467,13 @@ static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb, bool compat) ret =compat_rw_copy_check_uvector (type, (struct compat_iovec __user *)kiocb->ki_buf, kiocb->ki_nbytes,1 , &kiocb->ki_inline_vec, - &kiocb->ki_iovec,1 ); + &kiocb->ki_iovec); else#endif ret =rw_copy_check_uvector (type, (struct iovec __user *)kiocb->ki_buf, kiocb->ki_nbytes,1 , &kiocb->ki_inline_vec, - &kiocb->ki_iovec,1 ); + &kiocb->ki_iovec);if (ret <0 ) goto out;
fs/compat.c +3 -3
@@ -539,7 +539,7 @@ out:ssize_t compat_rw_copy_check_uvector (int type,const struct compat_iovec __user *uvector,unsigned long nr_segs,unsigned long fast_segs,struct iovec *fast_pointer, -struct iovec **ret_pointer,int check_access) +struct iovec **ret_pointer) {compat_ssize_t tot_len;struct iovec *iov = *ret_pointer = fast_pointer;
@@ -586,7 +586,7 @@ ssize_t compat_rw_copy_check_uvector(int type, }if (len <0 )/* size_t not fitting in compat_ssize_t .. */ goto out; -if (check_access && +if (type >=0 && !access_ok (vrfy_dir (type),compat_ptr (buf), len)) { ret = -EFAULT ; goto out;
@@ -1101,7 +1101,7 @@ static ssize_t compat_do_readv_writev(int type, struct file *file, goto out; tot_len =compat_rw_copy_check_uvector (type, uvector, nr_segs, -UIO_FASTIOV , iovstack, &iov,1 ); +UIO_FASTIOV , iovstack, &iov);if (tot_len ==0 ) { ret =0 ; goto out;
fs/read_write.c +3 -4
@@ -633,8 +633,7 @@ ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,ssize_t rw_copy_check_uvector (int type,const struct iovec __user * uvector,unsigned long nr_segs,unsigned long fast_segs,struct iovec *fast_pointer, -struct iovec **ret_pointer, -int check_access) +struct iovec **ret_pointer) {unsigned long seg;ssize_t ret;
@@ -690,7 +689,7 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector, ret = -EINVAL ; goto out; } -if (check_access +if (type >=0 &&unlikely (!access_ok (vrfy_dir (type), buf, len))) { ret = -EFAULT ; goto out;
@@ -723,7 +722,7 @@ static ssize_t do_readv_writev(int type, struct file *file, } ret =rw_copy_check_uvector (type, uvector, nr_segs, -ARRAY_SIZE (iovstack), iovstack, &iov,1 ); +ARRAY_SIZE (iovstack), iovstack, &iov);if (ret <=0 ) goto out;
include/linux/compat.h +1 -2
@@ -547,8 +547,7 @@ extern ssize_t compat_rw_copy_check_uvector(int type,const struct compat_iovec __user *uvector, unsigned long nr_segs, unsigned long fast_segs, struct iovec *fast_pointer, - struct iovec **ret_pointer, - int check_access); + struct iovec **ret_pointer);extern void __user *compat_alloc_user_space (unsigned long len);
include/linux/fs.h +8 -2
@@ -169,6 +169,13 @@ struct inodes_stat_t {#define WRITE_FUA (WRITE | REQ_SYNC | REQ_NOIDLE | REQ_FUA)#define WRITE_FLUSH_FUA (WRITE | REQ_SYNC | REQ_NOIDLE | REQ_FLUSH | REQ_FUA) + +/* Flag for rw_copy_check_uvector and compat_rw_copy_check_uvector +that indicates that they should check the contents of the iovec are +valid, but not check the memory that the iovec elements +points too */ +#define CHECK_IOVEC_ONLY -1 +#define SEL_IN 1#define SEL_OUT 2#define SEL_EX 4
@@ -1660,8 +1667,7 @@ struct seq_file;ssize_t rw_copy_check_uvector (int type,const struct iovec __user * uvector,unsigned long nr_segs,unsigned long fast_segs,struct iovec *fast_pointer, -struct iovec **ret_pointer, -int check_access); +struct iovec **ret_pointer);extern ssize_t vfs_read (struct file *,char __user *,size_t ,loff_t *);extern ssize_t vfs_write (struct file *,const char __user *,size_t ,loff_t *);
mm/process_vm_access.c +8 -8
@@ -368,15 +368,15 @@ static ssize_t process_vm_rw(pid_t pid,/* Check iovecs */ if (vm_write) rc =rw_copy_check_uvector (WRITE , lvec, liovcnt,UIO_FASTIOV , - iovstack_l, &iov_l,1 ); + iovstack_l, &iov_l);else rc =rw_copy_check_uvector (READ , lvec, liovcnt,UIO_FASTIOV , - iovstack_l, &iov_l,1 ); + iovstack_l, &iov_l);if (rc <=0 ) goto free_iovecs; - rc =rw_copy_check_uvector (READ , rvec, riovcnt,UIO_FASTIOV , - iovstack_r, &iov_r,0 ); + rc =rw_copy_check_uvector (CHECK_IOVEC_ONLY , rvec, riovcnt,UIO_FASTIOV , + iovstack_r, &iov_r);if (rc <=0 ) goto free_iovecs;
@@ -435,16 +435,16 @@ compat_process_vm_rw(compat_pid_t pid,if (vm_write) rc =compat_rw_copy_check_uvector (WRITE , lvec, liovcnt,UIO_FASTIOV , iovstack_l, - &iov_l,1 ); + &iov_l);else rc =compat_rw_copy_check_uvector (READ , lvec, liovcnt,UIO_FASTIOV , iovstack_l, - &iov_l,1 ); + &iov_l);if (rc <=0 ) goto free_iovecs; - rc =compat_rw_copy_check_uvector (READ , rvec, riovcnt, + rc =compat_rw_copy_check_uvector (CHECK_IOVEC_ONLY , rvec, riovcnt,UIO_FASTIOV , iovstack_r, - &iov_r,0 ); + &iov_r);if (rc <=0 ) goto free_iovecs;
security/keys/compat.c +1 -1
@@ -38,7 +38,7 @@ long compat_keyctl_instantiate_key_iov( ret =compat_rw_copy_check_uvector (WRITE , _payload_iov, ioc,ARRAY_SIZE (iovstack), - iovstack, &iov,1 ); + iovstack, &iov);if (ret <0 )return ret;if (ret ==0 )
security/keys/keyctl.c +1 -1
@@ -1065,7 +1065,7 @@ long keyctl_instantiate_key_iov(key_serial_t id, goto no_payload; ret =rw_copy_check_uvector (WRITE , _payload_iov, ioc, -ARRAY_SIZE (iovstack), iovstack, &iov,1 ); +ARRAY_SIZE (iovstack), iovstack, &iov);if (ret <0 )return ret;if (ret ==0 )
Quoted 10 lines · Christopher Yeoh
ssize_tcompat_rw_copy_check_uvector(int type,fs/compat.c +1
-1const struct compat_iovec __user *uvector, unsigned long nr_segs, unsigned long fast_segs, struct iovec *fast_pointer, - struct iovec **ret_pointer, int check_access) + struct iovec **ret_pointer) { compat_ssize_t tot_len;struct iovec *iov = *ret_pointer = fast_pointer;
fs/compat.c ssize_t compat_rw_copy_check_uvector(int type, · line 589
Quoted 5 lines
fs/compat.c +1
-1@@ -586,7 +586,7 @@ ssize_t compat_rw_copy_check_uvector(int type, }if (len <0 )/* size_t not fitting in compat_ssize_t .. */ goto out; - if (check_access && + if (type >=0 &&
I bet checkpatch.pl will complain, this needs the space after '>' ;)
Otherwise this is nice cleanup, imho.
Christopher, this is up to Andrew but perhaps you should update the changelog. It should explain what this patch does (overload "int type", remove the unnecessary "check_access", etc). It should not simply mention the previous discussion.
Oleg.
Andrew Morton
maintainerQuoted 19 lines · On Mon, 30 Jan 2012 18:23:37 +0100 Oleg Nesterov <[email protected]> wrote:
Quoted 10 lines · Christopher Yeoh
ssize_tcompat_rw_copy_check_uvector(int type,fs/compat.c +1
-1const struct compat_iovec __user *uvector, unsigned long nr_segs, unsigned long fast_segs, struct iovec *fast_pointer, - struct iovec **ret_pointer, int check_access) + struct iovec **ret_pointer) { compat_ssize_t tot_len;struct iovec *iov = *ret_pointer = fast_pointer;fs/compat.c ssize_t compat_rw_copy_check_uvector(int type, · line 589
Quoted 5 lines
fs/compat.c +1
-1@@ -586,7 +586,7 @@ ssize_t compat_rw_copy_check_uvector(int type, }if (len <0 )/* size_t not fitting in compat_ssize_t .. */ goto out; - if (check_access && + if (type >=0 &&I bet checkpatch.pl will complain, this needs the space after '>' ;)
Quoted 7 lines
Otherwise this is nice cleanup, imho.
Christopher, this is up to Andrew but perhaps you should update the changelog. It should explain what this patch does (overload "int type", remove the unnecessary "
check_access", etc). It should not simply mention the previous discussion.
Yes please. Put oneself in the position of a developer reading the changelog two years from now.
Also, please use our conventional comment layout style in fs.h.
Quoted 15 lines · On Mon, 30 Jan 2012 14:01:28 -0800 Andrew Morton <[email protected]>…
Quoted 12 lines · On Mon, 30 Jan 2012 18:23:37 +0100 Oleg Nesterov <[email protected]> wrote:
Quoted 11 lines · Christopher Yeoh
ssize_tcompat_rw_copy_check_uvector(int type,
const structcompat_iovec__user*uvector,
unsigned longnr_segs, unsigned longfast_segs, struct iovec*fast_pointer,(quoted diff) +1
-1- struct iovec **ret_pointer, int check_access) + struct iovec **ret_pointer) { compat_ssize_t tot_len; struct iovec *iov = *ret_pointer = fast_pointer;@@ -586,7 +586,7 @@ ssize_t compat_rw_copy_check_uvector(int type,
Quoted 8 lines
Quoted 8 lines
Review ssize_t compat_rw_copy_check_uvector(int type, · line 589
Quoted 5 lines
(quoted diff) +1
-1} if (len < 0) /* size_t not fitting in compat_ssize_t .. */ goto out; - if (check_access && + if (type >=0 &&I bet checkpatch.pl will complain, this needs the space after '>' ;)
Oops!
Quoted 8 lines
Quoted 7 lines
Otherwise this is nice cleanup, imho.
Christopher, this is up to Andrew but perhaps you should update the changelog. It should explain what this patch does (overload "int type", remove the unnecessary "
check_access", etc). It should not simply mention the previous discussion.
Quoted 4 lines
Yes please. Put oneself in the position of a developer reading the changelog two years from now.
Also, please use our conventional comment layout style in
fs.h.
Updated changelog and patch below. Differences from last patch
- fixes formatting problem
- fixes comment layout style for changes in fs.h
This is the cleanup of rw_copy_check_uvector and compat_rw_copy_check_uvector after changes made to support CMA in an earlier patch. Rather than having an additional check_access parameter to these functions, the first paramater type is overloaded to allow the caller to specify CHECK_IOVEC_ONLY which means check that the contents of the iovec are valid, but do not check the memory that they point to. This is used by process_vm_readv/writev where we need to validate that a iovec passed to the syscall is valid but do not want to check the memory that it points to at this point because it refers to an address space in another process.
Signature · 12 lines
[email protected] [email protected] Signed-off-by: Chris Yeoh <[email protected]> fs/aio.c | 4 ++-- fs/compat.c | 6 +++--- fs/read_write.c | 7 +++---- include/linux/compat.h | 3 +-- include/linux/fs.h | 12 ++++++++++-- mm/process_vm_access.c | 16 ++++++++-------- security/keys/compat.c | 2 +- security/keys/keyctl.c | 2 +- 8 files changed, 29 insertions(+), 23 deletions(-)
fs/aio.c +2 -2
@@ -1467,13 +1467,13 @@ static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb, bool compat) ret =compat_rw_copy_check_uvector (type, (struct compat_iovec __user *)kiocb->ki_buf, kiocb->ki_nbytes,1 , &kiocb->ki_inline_vec, - &kiocb->ki_iovec,1 ); + &kiocb->ki_iovec); else#endif ret =rw_copy_check_uvector (type, (struct iovec __user *)kiocb->ki_buf, kiocb->ki_nbytes,1 , &kiocb->ki_inline_vec, - &kiocb->ki_iovec,1 ); + &kiocb->ki_iovec);if (ret <0 ) goto out;
fs/compat.c +3 -3
@@ -539,7 +539,7 @@ out:ssize_t compat_rw_copy_check_uvector (int type,const struct compat_iovec __user *uvector,unsigned long nr_segs,unsigned long fast_segs,struct iovec *fast_pointer, -struct iovec **ret_pointer,int check_access) +struct iovec **ret_pointer) {compat_ssize_t tot_len;struct iovec *iov = *ret_pointer = fast_pointer;
@@ -586,7 +586,7 @@ ssize_t compat_rw_copy_check_uvector(int type, }if (len <0 )/* size_t not fitting in compat_ssize_t .. */ goto out; -if (check_access && +if (type >=0 && !access_ok (vrfy_dir (type),compat_ptr (buf), len)) { ret = -EFAULT ; goto out;
@@ -1101,7 +1101,7 @@ static ssize_t compat_do_readv_writev(int type, struct file *file, goto out; tot_len =compat_rw_copy_check_uvector (type, uvector, nr_segs, -UIO_FASTIOV , iovstack, &iov,1 ); +UIO_FASTIOV , iovstack, &iov);if (tot_len ==0 ) { ret =0 ; goto out;
fs/read_write.c +3 -4
@@ -633,8 +633,7 @@ ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,ssize_t rw_copy_check_uvector (int type,const struct iovec __user * uvector,unsigned long nr_segs,unsigned long fast_segs,struct iovec *fast_pointer, -struct iovec **ret_pointer, -int check_access) +struct iovec **ret_pointer) {unsigned long seg;ssize_t ret;
@@ -690,7 +689,7 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector, ret = -EINVAL ; goto out; } -if (check_access +if (type >=0 &&unlikely (!access_ok (vrfy_dir (type), buf, len))) { ret = -EFAULT ; goto out;
@@ -723,7 +722,7 @@ static ssize_t do_readv_writev(int type, struct file *file, } ret =rw_copy_check_uvector (type, uvector, nr_segs, -ARRAY_SIZE (iovstack), iovstack, &iov,1 ); +ARRAY_SIZE (iovstack), iovstack, &iov);if (ret <=0 ) goto out;
include/linux/compat.h +1 -2
@@ -547,8 +547,7 @@ extern ssize_t compat_rw_copy_check_uvector(int type,const struct compat_iovec __user *uvector, unsigned long nr_segs, unsigned long fast_segs, struct iovec *fast_pointer, - struct iovec **ret_pointer, - int check_access); + struct iovec **ret_pointer);extern void __user *compat_alloc_user_space (unsigned long len);
include/linux/fs.h +10 -2
@@ -169,6 +169,15 @@ struct inodes_stat_t {#define WRITE_FUA (WRITE | REQ_SYNC | REQ_NOIDLE | REQ_FUA)#define WRITE_FLUSH_FUA (WRITE | REQ_SYNC | REQ_NOIDLE | REQ_FLUSH | REQ_FUA) + +/* +* Flag for rw_copy_check_uvector and compat_rw_copy_check_uvector +* that indicates that they should check the contents of the iovec are +* valid, but not check the memory that the iovec elements +* points too. +*/ +#define CHECK_IOVEC_ONLY -1 +#define SEL_IN 1#define SEL_OUT 2#define SEL_EX 4
@@ -1660,8 +1669,7 @@ struct seq_file;ssize_t rw_copy_check_uvector (int type,const struct iovec __user * uvector,unsigned long nr_segs,unsigned long fast_segs,struct iovec *fast_pointer, -struct iovec **ret_pointer, -int check_access); +struct iovec **ret_pointer);extern ssize_t vfs_read (struct file *,char __user *,size_t ,loff_t *);extern ssize_t vfs_write (struct file *,const char __user *,size_t ,loff_t *);
mm/process_vm_access.c +8 -8
@@ -376,15 +376,15 @@ static ssize_t process_vm_rw(pid_t pid,/* Check iovecs */ if (vm_write) rc =rw_copy_check_uvector (WRITE , lvec, liovcnt,UIO_FASTIOV , - iovstack_l, &iov_l,1 ); + iovstack_l, &iov_l);else rc =rw_copy_check_uvector (READ , lvec, liovcnt,UIO_FASTIOV , - iovstack_l, &iov_l,1 ); + iovstack_l, &iov_l);if (rc <=0 ) goto free_iovecs; - rc =rw_copy_check_uvector (READ , rvec, riovcnt,UIO_FASTIOV , - iovstack_r, &iov_r,0 ); + rc =rw_copy_check_uvector (CHECK_IOVEC_ONLY , rvec, riovcnt,UIO_FASTIOV , + iovstack_r, &iov_r);if (rc <=0 ) goto free_iovecs;
@@ -443,16 +443,16 @@ compat_process_vm_rw(compat_pid_t pid,if (vm_write) rc =compat_rw_copy_check_uvector (WRITE , lvec, liovcnt,UIO_FASTIOV , iovstack_l, - &iov_l,1 ); + &iov_l);else rc =compat_rw_copy_check_uvector (READ , lvec, liovcnt,UIO_FASTIOV , iovstack_l, - &iov_l,1 ); + &iov_l);if (rc <=0 ) goto free_iovecs; - rc =compat_rw_copy_check_uvector (READ , rvec, riovcnt, + rc =compat_rw_copy_check_uvector (CHECK_IOVEC_ONLY , rvec, riovcnt,UIO_FASTIOV , iovstack_r, - &iov_r,0 ); + &iov_r);if (rc <=0 ) goto free_iovecs;
security/keys/compat.c +1 -1
@@ -38,7 +38,7 @@ long compat_keyctl_instantiate_key_iov( ret =compat_rw_copy_check_uvector (WRITE , _payload_iov, ioc,ARRAY_SIZE (iovstack), - iovstack, &iov,1 ); + iovstack, &iov);if (ret <0 )return ret;if (ret ==0 )
security/keys/keyctl.c +1 -1
@@ -1065,7 +1065,7 @@ long keyctl_instantiate_key_iov(key_serial_t id, goto no_payload; ret =rw_copy_check_uvector (WRITE , _payload_iov, ioc, -ARRAY_SIZE (iovstack), iovstack, &iov,1 ); +ARRAY_SIZE (iovstack), iovstack, &iov);if (ret <0 )return ret;if (ret ==0 )
Quoted 10 lines · Christopher Yeoh
Quoted 5 lines · On Mon, 30 Jan 2012 14:01:28 -0800 Andrew Morton <[email protected]>…
Yes please. Put oneself in the position of a developer reading the changelog two years from now.
Also, please use our conventional comment layout style in
fs.h.Updated changelog and patch below. Differences from last patch
Looks like this (imho good) cleanup was forgotten.
Christopher, perhaps you can re-send?
Oleg.