From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A029BC48BE8 for ; Fri, 18 Jun 2021 22:32:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8828B61260 for ; Fri, 18 Jun 2021 22:32:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235039AbhFRWfI (ORCPT ); Fri, 18 Jun 2021 18:35:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229466AbhFRWfH (ORCPT ); Fri, 18 Jun 2021 18:35:07 -0400 Received: from zeniv-ca.linux.org.uk (zeniv-ca.linux.org.uk [IPv6:2607:5300:60:148a::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 358ABC061574; Fri, 18 Jun 2021 15:32:58 -0700 (PDT) Received: from viro by zeniv-ca.linux.org.uk with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1luN2k-009lrM-FL; Fri, 18 Jun 2021 22:32:54 +0000 Date: Fri, 18 Jun 2021 22:32:54 +0000 From: Al Viro To: Omar Sandoval Cc: Linus Torvalds , linux-fsdevel , linux-btrfs , Linux API , Kernel Team Subject: Re: [PATCH RESEND x3 v9 1/9] iov_iter: add copy_struct_from_iter() Message-ID: References: <6caae597eb20da5ea23e53e8e64ce0c4f4d9c6d2.1623972519.git.osandov@fb.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: Al Viro Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org On Fri, Jun 18, 2021 at 03:10:03PM -0700, Omar Sandoval wrote: > Or do the same reverting thing that Al did, but with copy_from_iter() > instead of copy_from_iter_full() and being careful with the copied count > (which I'm not 100% sure I got correct here): > > size_t copied = copy_from_iter(&encoded, sizeof(encoded), &i); > if (copied < offsetofend(struct encoded_iov, size)) > return -EFAULT; > if (encoded.size > PAGE_SIZE) > return -E2BIG; > if (encoded.size < ENCODED_IOV_SIZE_VER0) > return -EINVAL; > if (encoded.size > sizeof(encoded)) { > if (copied < sizeof(encoded) > return -EFAULT; > if (!iov_iter_check_zeroes(&i, encoded.size - sizeof(encoded)) > return -EINVAL; > } else if (encoded.size < sizeof(encoded)) { > // older than what we expect > if (copied < encoded.size) > return -EFAULT; > iov_iter_revert(&i, copied - encoded.size); > memset((void *)&encoded + encoded.size, 0, sizeof(encoded) - encoded.size); > } simpler than that, actually - copied = copy_from_iter(&encoded, sizeof(encoded), &i); if (unlikely(copied < sizeof(encoded))) { if (copied < offsetofend(struct encoded_iov, size) || copied < encoded.size) return iov_iter_count(i) ? -EFAULT : -EINVAL; } if (encoded.size > sizeof(encoded)) { if (!iov_iter_check_zeroes(&i, encoded.size - sizeof(encoded)) return -EINVAL; } else if (encoded.size < sizeof(encoded)) { // copied can't be less than encoded.size here - otherwise // we'd have copied < sizeof(encoded) and the check above // would've buggered off iov_iter_revert(&i, copied - encoded.size); memset((void *)&encoded + encoded.size, 0, sizeof(encoded) - encoded.size); } should do it.