Skip to content

Commit fb041b5

Browse files
david-laightAl Viro
authored andcommitted
iov_iter: move rw_copy_check_uvector() into lib/iov_iter.c
This lets the compiler inline it into import_iovec() generating much better code. Signed-off-by: David Laight <david.laight@aculab.com> Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 576d070 commit fb041b5

2 files changed

Lines changed: 176 additions & 179 deletions

File tree

‎fs/read_write.c‎

Lines changed: 0 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -752,185 +752,6 @@ static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
752752
return ret;
753753
}
754754

755-
/**
756-
* rw_copy_check_uvector() - Copy an array of &struct iovec from userspace
757-
* into the kernel and check that it is valid.
758-
*
759-
* @type: One of %CHECK_IOVEC_ONLY, %READ, or %WRITE.
760-
* @uvector: Pointer to the userspace array.
761-
* @nr_segs: Number of elements in userspace array.
762-
* @fast_segs: Number of elements in @fast_pointer.
763-
* @fast_pointer: Pointer to (usually small on-stack) kernel array.
764-
* @ret_pointer: (output parameter) Pointer to a variable that will point to
765-
* either @fast_pointer, a newly allocated kernel array, or NULL,
766-
* depending on which array was used.
767-
*
768-
* This function copies an array of &struct iovec of @nr_segs from
769-
* userspace into the kernel and checks that each element is valid (e.g.
770-
* it does not point to a kernel address or cause overflow by being too
771-
* large, etc.).
772-
*
773-
* As an optimization, the caller may provide a pointer to a small
774-
* on-stack array in @fast_pointer, typically %UIO_FASTIOV elements long
775-
* (the size of this array, or 0 if unused, should be given in @fast_segs).
776-
*
777-
* @ret_pointer will always point to the array that was used, so the
778-
* caller must take care not to call kfree() on it e.g. in case the
779-
* @fast_pointer array was used and it was allocated on the stack.
780-
*
781-
* Return: The total number of bytes covered by the iovec array on success
782-
* or a negative error code on error.
783-
*/
784-
ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
785-
unsigned long nr_segs, unsigned long fast_segs,
786-
struct iovec *fast_pointer,
787-
struct iovec **ret_pointer)
788-
{
789-
unsigned long seg;
790-
ssize_t ret;
791-
struct iovec *iov = fast_pointer;
792-
793-
/*
794-
* SuS says "The readv() function *may* fail if the iovcnt argument
795-
* was less than or equal to 0, or greater than {IOV_MAX}. Linux has
796-
* traditionally returned zero for zero segments, so...
797-
*/
798-
if (nr_segs == 0) {
799-
ret = 0;
800-
goto out;
801-
}
802-
803-
/*
804-
* First get the "struct iovec" from user memory and
805-
* verify all the pointers
806-
*/
807-
if (nr_segs > UIO_MAXIOV) {
808-
ret = -EINVAL;
809-
goto out;
810-
}
811-
if (nr_segs > fast_segs) {
812-
iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
813-
if (iov == NULL) {
814-
ret = -ENOMEM;
815-
goto out;
816-
}
817-
}
818-
if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
819-
ret = -EFAULT;
820-
goto out;
821-
}
822-
823-
/*
824-
* According to the Single Unix Specification we should return EINVAL
825-
* if an element length is < 0 when cast to ssize_t or if the
826-
* total length would overflow the ssize_t return value of the
827-
* system call.
828-
*
829-
* Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
830-
* overflow case.
831-
*/
832-
ret = 0;
833-
for (seg = 0; seg < nr_segs; seg++) {
834-
void __user *buf = iov[seg].iov_base;
835-
ssize_t len = (ssize_t)iov[seg].iov_len;
836-
837-
/* see if we we're about to use an invalid len or if
838-
* it's about to overflow ssize_t */
839-
if (len < 0) {
840-
ret = -EINVAL;
841-
goto out;
842-
}
843-
if (type >= 0
844-
&& unlikely(!access_ok(buf, len))) {
845-
ret = -EFAULT;
846-
goto out;
847-
}
848-
if (len > MAX_RW_COUNT - ret) {
849-
len = MAX_RW_COUNT - ret;
850-
iov[seg].iov_len = len;
851-
}
852-
ret += len;
853-
}
854-
out:
855-
*ret_pointer = iov;
856-
return ret;
857-
}
858-
859-
#ifdef CONFIG_COMPAT
860-
ssize_t compat_rw_copy_check_uvector(int type,
861-
const struct compat_iovec __user *uvector, unsigned long nr_segs,
862-
unsigned long fast_segs, struct iovec *fast_pointer,
863-
struct iovec **ret_pointer)
864-
{
865-
compat_ssize_t tot_len;
866-
struct iovec *iov = *ret_pointer = fast_pointer;
867-
ssize_t ret = 0;
868-
int seg;
869-
870-
/*
871-
* SuS says "The readv() function *may* fail if the iovcnt argument
872-
* was less than or equal to 0, or greater than {IOV_MAX}. Linux has
873-
* traditionally returned zero for zero segments, so...
874-
*/
875-
if (nr_segs == 0)
876-
goto out;
877-
878-
ret = -EINVAL;
879-
if (nr_segs > UIO_MAXIOV)
880-
goto out;
881-
if (nr_segs > fast_segs) {
882-
ret = -ENOMEM;
883-
iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
884-
if (iov == NULL)
885-
goto out;
886-
}
887-
*ret_pointer = iov;
888-
889-
ret = -EFAULT;
890-
if (!access_ok(uvector, nr_segs*sizeof(*uvector)))
891-
goto out;
892-
893-
/*
894-
* Single unix specification:
895-
* We should -EINVAL if an element length is not >= 0 and fitting an
896-
* ssize_t.
897-
*
898-
* In Linux, the total length is limited to MAX_RW_COUNT, there is
899-
* no overflow possibility.
900-
*/
901-
tot_len = 0;
902-
ret = -EINVAL;
903-
for (seg = 0; seg < nr_segs; seg++) {
904-
compat_uptr_t buf;
905-
compat_ssize_t len;
906-
907-
if (__get_user(len, &uvector->iov_len) ||
908-
__get_user(buf, &uvector->iov_base)) {
909-
ret = -EFAULT;
910-
goto out;
911-
}
912-
if (len < 0) /* size_t not fitting in compat_ssize_t .. */
913-
goto out;
914-
if (type >= 0 &&
915-
!access_ok(compat_ptr(buf), len)) {
916-
ret = -EFAULT;
917-
goto out;
918-
}
919-
if (len > MAX_RW_COUNT - tot_len)
920-
len = MAX_RW_COUNT - tot_len;
921-
tot_len += len;
922-
iov->iov_base = compat_ptr(buf);
923-
iov->iov_len = (compat_size_t) len;
924-
uvector++;
925-
iov++;
926-
}
927-
ret = tot_len;
928-
929-
out:
930-
return ret;
931-
}
932-
#endif
933-
934755
static ssize_t do_iter_read(struct file *file, struct iov_iter *iter,
935756
loff_t *pos, rwf_t flags)
936757
{

‎lib/iov_iter.c‎

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,109 @@ const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
16501650
}
16511651
EXPORT_SYMBOL(dup_iter);
16521652

1653+
/**
1654+
* rw_copy_check_uvector() - Copy an array of &struct iovec from userspace
1655+
* into the kernel and check that it is valid.
1656+
*
1657+
* @type: One of %CHECK_IOVEC_ONLY, %READ, or %WRITE.
1658+
* @uvector: Pointer to the userspace array.
1659+
* @nr_segs: Number of elements in userspace array.
1660+
* @fast_segs: Number of elements in @fast_pointer.
1661+
* @fast_pointer: Pointer to (usually small on-stack) kernel array.
1662+
* @ret_pointer: (output parameter) Pointer to a variable that will point to
1663+
* either @fast_pointer, a newly allocated kernel array, or NULL,
1664+
* depending on which array was used.
1665+
*
1666+
* This function copies an array of &struct iovec of @nr_segs from
1667+
* userspace into the kernel and checks that each element is valid (e.g.
1668+
* it does not point to a kernel address or cause overflow by being too
1669+
* large, etc.).
1670+
*
1671+
* As an optimization, the caller may provide a pointer to a small
1672+
* on-stack array in @fast_pointer, typically %UIO_FASTIOV elements long
1673+
* (the size of this array, or 0 if unused, should be given in @fast_segs).
1674+
*
1675+
* @ret_pointer will always point to the array that was used, so the
1676+
* caller must take care not to call kfree() on it e.g. in case the
1677+
* @fast_pointer array was used and it was allocated on the stack.
1678+
*
1679+
* Return: The total number of bytes covered by the iovec array on success
1680+
* or a negative error code on error.
1681+
*/
1682+
ssize_t rw_copy_check_uvector(int type, const struct iovec __user *uvector,
1683+
unsigned long nr_segs, unsigned long fast_segs,
1684+
struct iovec *fast_pointer, struct iovec **ret_pointer)
1685+
{
1686+
unsigned long seg;
1687+
ssize_t ret;
1688+
struct iovec *iov = fast_pointer;
1689+
1690+
/*
1691+
* SuS says "The readv() function *may* fail if the iovcnt argument
1692+
* was less than or equal to 0, or greater than {IOV_MAX}. Linux has
1693+
* traditionally returned zero for zero segments, so...
1694+
*/
1695+
if (nr_segs == 0) {
1696+
ret = 0;
1697+
goto out;
1698+
}
1699+
1700+
/*
1701+
* First get the "struct iovec" from user memory and
1702+
* verify all the pointers
1703+
*/
1704+
if (nr_segs > UIO_MAXIOV) {
1705+
ret = -EINVAL;
1706+
goto out;
1707+
}
1708+
if (nr_segs > fast_segs) {
1709+
iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
1710+
if (iov == NULL) {
1711+
ret = -ENOMEM;
1712+
goto out;
1713+
}
1714+
}
1715+
if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
1716+
ret = -EFAULT;
1717+
goto out;
1718+
}
1719+
1720+
/*
1721+
* According to the Single Unix Specification we should return EINVAL
1722+
* if an element length is < 0 when cast to ssize_t or if the
1723+
* total length would overflow the ssize_t return value of the
1724+
* system call.
1725+
*
1726+
* Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
1727+
* overflow case.
1728+
*/
1729+
ret = 0;
1730+
for (seg = 0; seg < nr_segs; seg++) {
1731+
void __user *buf = iov[seg].iov_base;
1732+
ssize_t len = (ssize_t)iov[seg].iov_len;
1733+
1734+
/* see if we we're about to use an invalid len or if
1735+
* it's about to overflow ssize_t */
1736+
if (len < 0) {
1737+
ret = -EINVAL;
1738+
goto out;
1739+
}
1740+
if (type >= 0
1741+
&& unlikely(!access_ok(buf, len))) {
1742+
ret = -EFAULT;
1743+
goto out;
1744+
}
1745+
if (len > MAX_RW_COUNT - ret) {
1746+
len = MAX_RW_COUNT - ret;
1747+
iov[seg].iov_len = len;
1748+
}
1749+
ret += len;
1750+
}
1751+
out:
1752+
*ret_pointer = iov;
1753+
return ret;
1754+
}
1755+
16531756
/**
16541757
* import_iovec() - Copy an array of &struct iovec from userspace
16551758
* into the kernel, check that it is valid, and initialize a new
@@ -1695,6 +1798,79 @@ EXPORT_SYMBOL(import_iovec);
16951798
#ifdef CONFIG_COMPAT
16961799
#include <linux/compat.h>
16971800

1801+
ssize_t compat_rw_copy_check_uvector(int type,
1802+
const struct compat_iovec __user *uvector,
1803+
unsigned long nr_segs, unsigned long fast_segs,
1804+
struct iovec *fast_pointer, struct iovec **ret_pointer)
1805+
{
1806+
compat_ssize_t tot_len;
1807+
struct iovec *iov = *ret_pointer = fast_pointer;
1808+
ssize_t ret = 0;
1809+
int seg;
1810+
1811+
/*
1812+
* SuS says "The readv() function *may* fail if the iovcnt argument
1813+
* was less than or equal to 0, or greater than {IOV_MAX}. Linux has
1814+
* traditionally returned zero for zero segments, so...
1815+
*/
1816+
if (nr_segs == 0)
1817+
goto out;
1818+
1819+
ret = -EINVAL;
1820+
if (nr_segs > UIO_MAXIOV)
1821+
goto out;
1822+
if (nr_segs > fast_segs) {
1823+
ret = -ENOMEM;
1824+
iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
1825+
if (iov == NULL)
1826+
goto out;
1827+
}
1828+
*ret_pointer = iov;
1829+
1830+
ret = -EFAULT;
1831+
if (!access_ok(uvector, nr_segs*sizeof(*uvector)))
1832+
goto out;
1833+
1834+
/*
1835+
* Single unix specification:
1836+
* We should -EINVAL if an element length is not >= 0 and fitting an
1837+
* ssize_t.
1838+
*
1839+
* In Linux, the total length is limited to MAX_RW_COUNT, there is
1840+
* no overflow possibility.
1841+
*/
1842+
tot_len = 0;
1843+
ret = -EINVAL;
1844+
for (seg = 0; seg < nr_segs; seg++) {
1845+
compat_uptr_t buf;
1846+
compat_ssize_t len;
1847+
1848+
if (__get_user(len, &uvector->iov_len) ||
1849+
__get_user(buf, &uvector->iov_base)) {
1850+
ret = -EFAULT;
1851+
goto out;
1852+
}
1853+
if (len < 0) /* size_t not fitting in compat_ssize_t .. */
1854+
goto out;
1855+
if (type >= 0 &&
1856+
!access_ok(compat_ptr(buf), len)) {
1857+
ret = -EFAULT;
1858+
goto out;
1859+
}
1860+
if (len > MAX_RW_COUNT - tot_len)
1861+
len = MAX_RW_COUNT - tot_len;
1862+
tot_len += len;
1863+
iov->iov_base = compat_ptr(buf);
1864+
iov->iov_len = (compat_size_t) len;
1865+
uvector++;
1866+
iov++;
1867+
}
1868+
ret = tot_len;
1869+
1870+
out:
1871+
return ret;
1872+
}
1873+
16981874
ssize_t compat_import_iovec(int type,
16991875
const struct compat_iovec __user * uvector,
17001876
unsigned nr_segs, unsigned fast_segs,

0 commit comments

Comments
 (0)