From patchwork Fri Aug 21 07:38:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 11728421 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8A120739 for ; Fri, 21 Aug 2020 07:39:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6F26A207DF for ; Fri, 21 Aug 2020 07:39:07 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=osandov-com.20150623.gappssmtp.com header.i=@osandov-com.20150623.gappssmtp.com header.b="EiDDS+Rl" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727779AbgHUHjE (ORCPT ); Fri, 21 Aug 2020 03:39:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728086AbgHUHiz (ORCPT ); Fri, 21 Aug 2020 03:38:55 -0400 Received: from mail-pf1-x444.google.com (mail-pf1-x444.google.com [IPv6:2607:f8b0:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E2D0C061342 for ; Fri, 21 Aug 2020 00:38:54 -0700 (PDT) Received: by mail-pf1-x444.google.com with SMTP id m8so669988pfh.3 for ; Fri, 21 Aug 2020 00:38:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=osandov-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NbNZE5EUMG3BjXEiJ181XCt1kROeQ09EehF/ZeJbJZc=; b=EiDDS+RlL7NRC3vJsn68m59XKvD9RaJJMZrqSCQ8d5QGhjaAgm31pzaitQG5/0cf/3 4xA0/3PSDviWS44gfeuiVlsrj2LceKZdmWnEvcPF7lgLif7o8QyV+eSFBOCsiWBpCbVr EpRzRiKJ4hxiyyRwSGk+zCSFHsHJbPXdoXWK5Dcjux1OoK6jtfSVqNKxdVXeBHQZyZMO 1x5Vxd0xcX2l8gV0DpabmXwJ5BqKSphyyjyIHNTq6uOX78QeHLWo91hehp2T9RUnonVR MZacudvfNUVFx5B0FMQIkqLMnoDIaSZoQdQW6S0ishOQOsJU3hqRzb3nCJ6gLBagrpwX cX0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NbNZE5EUMG3BjXEiJ181XCt1kROeQ09EehF/ZeJbJZc=; b=Tb2o/SOfrZEz4DvfJKjDLS8xCzfdLbxW8H9h49ltJnPo42B8oIHKONA43/2Fb3qqMS rYKCBbuDkZgQRE48p3Ql/giKzSxrufGoI/46zGXTkIoKeY/YcE7bNBsP+rqMaWum5ety HFYvMWn7wI00dUNOfxEtrdtxyPD+1RKsU3qXh96+Ln/fkdEA95OmUy3AVvZ4dipwhkeD wWgSsUrXbc8Qe5XDoctMrMJDQUGphebiz3QnUiW0WEAm2lAmJZ6aQTPkczWT8x5/s55H 3XIX5fJfUdBQ26r1LqApWjed4L+p+I7qtcejKhUfVAnanP+LiXPu89T7YEqYoKxv5W6A cqag== X-Gm-Message-State: AOAM530zqGYfGVQFiTQzQlayUuHgvN89ztQab6Svl3+TFo0QdcbDXKQK RSyuTL2plg9HfFLNmqU8AlWwQUp6BFM5oA== X-Google-Smtp-Source: ABdhPJyVLFv3sJdKcYXujphx1VB63apU83QnLh3+ScdVNhU0JvrvFb7x8jdnjjhwwqfAbMlU9PbiIw== X-Received: by 2002:a63:cc49:: with SMTP id q9mr1382045pgi.390.1597995533642; Fri, 21 Aug 2020 00:38:53 -0700 (PDT) Received: from exodia.tfbnw.net ([2620:10d:c090:400::5:f2a4]) by smtp.gmail.com with ESMTPSA id t10sm1220867pgp.15.2020.08.21.00.38.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Aug 2020 00:38:52 -0700 (PDT) From: Omar Sandoval To: linux-fsdevel@vger.kernel.org, linux-btrfs@vger.kernel.org, Al Viro , Christoph Hellwig Cc: Dave Chinner , Jann Horn , Amir Goldstein , Aleksa Sarai , 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 Message-Id: <8010f8862ec494c631b1d7681a6c5886d12f60df.1597993855.git.osandov@osandov.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: References: MIME-Version: 1.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org From: Omar Sandoval This is essentially copy_struct_from_user() but for an iov_iter. Suggested-by: Aleksa Sarai Signed-off-by: Omar Sandoval Reviewed-by: Josef Bacik --- 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;