Skip to contentSkip to conversation

lib/iov_iter: Improved function for importing iovec[] from userpace.

PATCH In review David Laight 2 participants 3 messages Sep 15, 2020 – Sep 21, 2020

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 3/9 lib/iov_iter: Improved function for importing iovec[] from userpace.

import_iovec() has a 'pointer by reference' parameter to pass in the (on-stack) iov[] cache and return the address of a larger copy that the caller must free.
This is non-intuitive, faffy to setup, and not that efficient. Instead just pass in the address of the cache and return the address to free (on success) or PTR_ERR() (on error).

Additionally the size of the 'cache' is nominally variable but is often specified in a different source file to the actual cache passed. Use a structure for the 'cache' so that the compiler checks its size.

To avoid having to change everything at once the 'struct iov_iter *' is passed to rw_copy_check_uvector() which is renamed iovec_import() and returns the malloced address on success.

import_iovec() is then implemented using iovec_import().

The optimisation for zero length iov[] is removed (they'll now do a zero length copy_from_user() before returning success).

The check for oversize iov[] is moved inside the check for iov[] larger than the supplied cache.

The same changes have been made to the compat versions.

---
 include/linux/uio.h |  14 ++++
 lib/iov_iter.c      | 194 ++++++++++++++++++++------------------------
 2 files changed, 100 insertions(+), 108 deletions(-)
include/linux/uio.h +14 -0
index 3835a8a8e9ea..d26482d348f3 100644
@@ -266,15 +266,29 @@ bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum, struct
 size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
 		struct iov_iter *i);
 
+struct iovec_cache {
+	struct iovec  iov[UIO_FASTIOV];
+};
+
 ssize_t import_iovec(int type, const struct iovec __user * uvector,
 		 unsigned nr_segs, unsigned fast_segs,
 		 struct iovec **iov, struct iov_iter *i);
 
+struct iovec *iovec_import(int type, const struct iovec __user * uvector,
+		unsigned int nr_segs, struct iovec_cache *cache,
+		struct iov_iter *i);
+
 #ifdef CONFIG_COMPAT
 struct compat_iovec;
 ssize_t compat_import_iovec(int type, const struct compat_iovec __user * uvector,
 		 unsigned nr_segs, unsigned fast_segs,
 		 struct iovec **iov, struct iov_iter *i);
+
+struct iovec *compat_iovec_import(int type,
+		const struct compat_iovec __user * uvector,
+		unsigned int nr_segs, struct iovec_cache *cache,
+		struct iov_iter *i);
+
 #endif
 
 int import_single_range(int type, void __user *buf, size_t len,
lib/iov_iter.c +86 -108

288 lines not on this page · raw message

Signature · 5 lines
2.25.1

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
#1 lore raw
Quoted 7 lines · David Laight

import_iovec() has a 'pointer by reference' parameter to pass in the (on-stack) iov[] cache and return the address of a larger copy that the caller must free.
This is non-intuitive, faffy to setup, and not that efficient. Instead just pass in the address of the cache and return the address to free (on success) or PTR_ERR() (on error).

To me it seems pretty sensible, and in fact the conversions to your new API seem to add more lines than they remove.

#2 lore raw

From: Christoph Hellwig

Quoted 11 lines

Sent: 21 September 2020 15:11

Quoted 7 lines · David Laight

import_iovec() has a 'pointer by reference' parameter to pass in the (on-stack) iov[] cache and return the address of a larger copy that the caller must free.
This is non-intuitive, faffy to setup, and not that efficient. Instead just pass in the address of the cache and return the address to free (on success) or PTR_ERR() (on error).

Quoted 2 lines

To me it seems pretty sensible, and in fact the conversions to your new API seem to add more lines than they remove.

They probably add a line because the two variables get defined on separate lines.

The problem is the inefficiency of passing the addresses by 'double reference'.
Although your suggestion of putting the 'address to free' in the same structure as the cache does resolve that.
It also gets rid of all the PTR_ERR() faffing.
Still probably best to return 0 on success.
Plenty of code was converting the +ve to 0.

I might to a v2 on top of your compat iovec changes - once they hit Linus's tree.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)