Linux-Fsdevel Archive on lore.kernel.org help / color / mirror / Atom feed
From: Omar Sandoval <osandov@osandov.com> To: linux-fsdevel@vger.kernel.org, linux-btrfs@vger.kernel.org, Al Viro <viro@zeniv.linux.org.uk>, Christoph Hellwig <hch@infradead.org> Cc: Dave Chinner <david@fromorbit.com>, Jann Horn <jannh@google.com>, Amir Goldstein <amir73il@gmail.com>, Aleksa Sarai <cyphar@cyphar.com>, linux-api@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v5 1/9] iov_iter: add copy_struct_from_iter() Date: Fri, 21 Aug 2020 00:38:32 -0700 [thread overview] Message-ID: <8010f8862ec494c631b1d7681a6c5886d12f60df.1597993855.git.osandov@osandov.com> (raw) In-Reply-To: <cover.1597993855.git.osandov@osandov.com> From: Omar Sandoval <osandov@fb.com> This is essentially copy_struct_from_user() but for an iov_iter. Suggested-by: Aleksa Sarai <cyphar@cyphar.com> Signed-off-by: Omar Sandoval <osandov@fb.com> --- include/linux/uio.h | 2 ++ lib/iov_iter.c | 82 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/include/linux/uio.h b/include/linux/uio.h index 3835a8a8e9ea..2b80cd073d83 100644 --- a/include/linux/uio.h +++ b/include/linux/uio.h @@ -121,6 +121,8 @@ size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes, struct iov_iter *i); size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes, struct iov_iter *i); +int copy_struct_from_iter(void *dst, size_t ksize, struct iov_iter *i, + size_t usize); size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i); size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i); diff --git a/lib/iov_iter.c b/lib/iov_iter.c index 5e40786c8f12..cb320f7f6628 100644 --- a/lib/iov_iter.c +++ b/lib/iov_iter.c @@ -944,6 +944,88 @@ size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes, } EXPORT_SYMBOL(copy_page_from_iter); +/** + * copy_struct_from_iter - copy a struct from an iov_iter + * @dst: Destination buffer. + * @ksize: Size of @dst struct. + * @i: Source iterator. + * @usize: (Alleged) size of struct in @i. + * + * Copies a struct from an iov_iter in a way that guarantees + * backwards-compatibility for struct arguments in an iovec (as long as the + * rules for copy_struct_from_user() are followed). + * + * The recommended usage is that @usize be taken from the current segment: + * + * int do_foo(struct iov_iter *i) + * { + * size_t usize = iov_iter_single_seg_count(i); + * struct foo karg; + * int err; + * + * if (usize > PAGE_SIZE) + * return -E2BIG; + * if (usize < FOO_SIZE_VER0) + * return -EINVAL; + * err = copy_struct_from_iter(&karg, sizeof(karg), i, usize); + * if (err) + * return err; + * + * // ... + * } + * + * Return: 0 on success, -errno on error (see copy_struct_from_user()). + * + * On success, the iterator is advanced @usize bytes. On error, the iterator is + * not advanced. + */ +int copy_struct_from_iter(void *dst, size_t ksize, struct iov_iter *i, + size_t usize) +{ + if (usize <= ksize) { + if (!copy_from_iter_full(dst, usize, i)) + return -EFAULT; + memset(dst + usize, 0, ksize - usize); + } else { + size_t copied = 0, copy; + int ret; + + if (WARN_ON(iov_iter_is_pipe(i)) || unlikely(i->count < usize)) + return -EFAULT; + if (iter_is_iovec(i)) + might_fault(); + iterate_all_kinds(i, usize, v, ({ + copy = min(ksize - copied, v.iov_len); + if (copy && copyin(dst + copied, v.iov_base, copy)) + return -EFAULT; + copied += copy; + ret = check_zeroed_user(v.iov_base + copy, + v.iov_len - copy); + if (ret <= 0) + return ret ?: -E2BIG; + 0;}), ({ + char *addr = kmap_atomic(v.bv_page); + copy = min_t(size_t, ksize - copied, v.bv_len); + memcpy(dst + copied, addr + v.bv_offset, copy); + copied += copy; + ret = memchr_inv(addr + v.bv_offset + copy, 0, + v.bv_len - copy) ? -E2BIG : 0; + kunmap_atomic(addr); + if (ret) + return ret; + }), ({ + copy = min(ksize - copied, v.iov_len); + memcpy(dst + copied, v.iov_base, copy); + if (memchr_inv(v.iov_base, 0, v.iov_len)) + return -E2BIG; + }) + ) + iov_iter_advance(i, usize); + } + return 0; +} +EXPORT_SYMBOL_GPL(copy_struct_from_iter); + static size_t pipe_zero(size_t bytes, struct iov_iter *i) { struct pipe_inode_info *pipe = i->pipe; -- 2.28.0
next prev parent reply other threads:[~2020-08-21 7:39 UTC|newest] Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-08-21 7:38 [PATCH v5 0/9] fs: interface for directly reading/writing compressed data Omar Sandoval 2020-08-21 7:38 ` [PATCH man-pages v5] Document encoded I/O Omar Sandoval 2020-08-21 9:24 ` Amir Goldstein 2020-08-24 18:15 ` Omar Sandoval 2020-08-21 7:38 ` Omar Sandoval [this message] 2020-08-24 18:52 ` [PATCH v5 1/9] iov_iter: add copy_struct_from_iter() Josef Bacik 2020-08-24 21:09 ` Omar Sandoval 2020-08-21 7:38 ` [PATCH v5 2/9] fs: add O_ALLOW_ENCODED open flag Omar Sandoval 2020-08-24 18:28 ` Josef Bacik 2020-08-24 21:11 ` Omar Sandoval 2020-08-21 7:38 ` [PATCH v5 3/9] fs: add RWF_ENCODED for reading/writing compressed data Omar Sandoval 2020-08-21 8:47 ` Amir Goldstein 2020-08-24 23:49 ` Omar Sandoval 2020-08-25 8:25 ` Amir Goldstein 2020-08-25 17:20 ` Omar Sandoval 2020-08-24 19:07 ` Josef Bacik 2020-08-21 7:38 ` [PATCH v5 4/9] btrfs: don't advance offset for compressed bios in btrfs_csum_one_bio() Omar Sandoval 2020-08-24 19:17 ` Josef Bacik 2020-08-21 7:38 ` [PATCH v5 5/9] btrfs: add ram_bytes and offset to btrfs_ordered_extent Omar Sandoval 2020-08-24 19:23 ` Josef Bacik 2020-08-21 7:38 ` [PATCH v5 6/9] btrfs: support different disk extent size for delalloc Omar Sandoval 2020-08-24 19:26 ` Josef Bacik 2020-08-21 7:38 ` [PATCH v5 7/9] btrfs: optionally extend i_size in cow_file_range_inline() Omar Sandoval 2020-08-24 19:33 ` Josef Bacik 2020-08-21 7:38 ` [PATCH v5 8/9] btrfs: implement RWF_ENCODED reads Omar Sandoval 2020-08-24 19:54 ` Josef Bacik 2020-08-24 21:23 ` Omar Sandoval 2020-08-21 7:38 ` [PATCH v5 9/9] btrfs: implement RWF_ENCODED writes Omar Sandoval 2020-08-24 20:30 ` Josef Bacik 2020-08-24 21:30 ` Omar Sandoval
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=8010f8862ec494c631b1d7681a6c5886d12f60df.1597993855.git.osandov@osandov.com \ --to=osandov@osandov.com \ --cc=amir73il@gmail.com \ --cc=cyphar@cyphar.com \ --cc=david@fromorbit.com \ --cc=hch@infradead.org \ --cc=jannh@google.com \ --cc=kernel-team@fb.com \ --cc=linux-api@vger.kernel.org \ --cc=linux-btrfs@vger.kernel.org \ --cc=linux-fsdevel@vger.kernel.org \ --cc=viro@zeniv.linux.org.uk \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).