Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp1829544ybn; Thu, 26 Sep 2019 02:53:19 -0700 (PDT) X-Google-Smtp-Source: APXvYqyzuudUNRqkQrhkKx6sQp5+ArZOv7jFPIlFjb6vdS7n3kGIhH/3p31YbLVRFi0M+TKqYArp X-Received: by 2002:a17:906:4e8f:: with SMTP id v15mr2312083eju.57.1569491599233; Thu, 26 Sep 2019 02:53:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569491599; cv=none; d=google.com; s=arc-20160816; b=TuUFBdcNuHWxUqyKaiBn2y+f5QUt3f3ygNMRdib3flOwf+YhEBeZUIU05fcV3/y8LU eU5Rw7Z1sZj6vsJHmBRhyvxyB2wW49r2V+ne4MNrjxEKqyp7Ke1rtMs/hW8bcd23COCn ugldMK8mFFVZV4CnhWHwUVVFCvdgAu1mExLL8UGgSXv/UubM793gFElsmNpsLZnS6dL1 lrGXadh8/Hd+URbTIQJUW//iVSjs1CMWsyTQ6JhpzhldwxxhxIkvq7BzBGg6Z1SuXe4g LTq53y69HSUw9xgvZhLzF54pgH548c8vT3ckMiYibaHlGhb50Hx17LNI60hH/rV0iXpS RTOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=Ubp3hpdHZYFHNsbBzmSC09eT5vsyQ7Zrdz0nsHZMrVo=; b=FvR+qy0oM+NfUGduDz+AY8KSAieRDYxIGVCtvFDhJwzkFPuxacTYHNGFGO2O7ORqzL hM4xbkFnnIGljoycxmdwWzCsJ1fNGbM8RchfIc4j2YpVi6CkO9fHDOXPwUyX3wbnO4S9 gIQLLnABTlqOqsGvui+zYQqdIEJKhzje9PyIbGZON4DaJTra9eIfi9wo2o4ZOCBM9QGI tnlurP8FKwlu86nhyUtEW7ZktNdFiIGEr2ebjWfPH2AN3c2efOX/EPfd8pqvyjZJDBc1 TwPPx8Ru/Z0gyab19OjxzgI1yPnPUe9XA1jsTxpx2hQ6rSRfYLeHl9d0w5wkWHeIz/9N qDpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=gGKQ4rsB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w55si1001993eda.15.2019.09.26.02.52.56; Thu, 26 Sep 2019 02:53:19 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=gGKQ4rsB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728280AbfIYRsv (ORCPT + 99 others); Wed, 25 Sep 2019 13:48:51 -0400 Received: from mail-lf1-f66.google.com ([209.85.167.66]:38368 "EHLO mail-lf1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727050AbfIYRsv (ORCPT ); Wed, 25 Sep 2019 13:48:51 -0400 Received: by mail-lf1-f66.google.com with SMTP id u28so4875060lfc.5 for ; Wed, 25 Sep 2019 10:48:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Ubp3hpdHZYFHNsbBzmSC09eT5vsyQ7Zrdz0nsHZMrVo=; b=gGKQ4rsBGdlqZUFMO6XNkNge3CFyTMn8oZRzwJk/rovTd8qu09JoOmMh4pWLsauLni Au3XxhldtMZLJwfaqs4pnNxXd0RGKaYu+YhlrWEfuThVz+0g8mr0CzD/B99CmJ3YtiNy rZkgmAhinraPdfB5emwIyaHr2/x37VdH3uCq4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Ubp3hpdHZYFHNsbBzmSC09eT5vsyQ7Zrdz0nsHZMrVo=; b=QA/M+R9Rvno8Z9BAC5yYltDZrIkMEy0hUPIVIdURK3B+9OZRwQHm14npdp7wCvpA6e szUSSMvuUvMK+e6Qc0weiQUzIhGycJjJbzN1cvy/6sD9lK+VJN1Wzn4SLT4KYoU2lO49 xJ6YKVNjcGvJ84WBDssMRLSdfisAB4ldUQRnwiLj+Y5VgYHBj9CuHbY6tyslCmCmZYhc U7YDxnh5wWuyjEKs5Y1RsoGhz+k0FiHt4n278BJnu//33lv2dJaK8zjga/D7cInf4MQV Tx3/PyM+z4yL5eVGnNsfAQyx2454pLFrya+udkM+tPdvt/+IZaWi7n0x+dz/8uR817E8 TdFw== X-Gm-Message-State: APjAAAUo/tEdmYJKUA3vH1EKhFBMrUV3rrPBvs45f/oCTdKcQHYWWFpW 0yhieSVBvegC8RqGNRfEa7AHwlfOe94= X-Received: by 2002:a19:c3d3:: with SMTP id t202mr7116590lff.48.1569433729076; Wed, 25 Sep 2019 10:48:49 -0700 (PDT) Received: from mail-lf1-f48.google.com (mail-lf1-f48.google.com. [209.85.167.48]) by smtp.gmail.com with ESMTPSA id w16sm1361515lji.42.2019.09.25.10.48.48 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 25 Sep 2019 10:48:48 -0700 (PDT) Received: by mail-lf1-f48.google.com with SMTP id u28so4875003lfc.5 for ; Wed, 25 Sep 2019 10:48:48 -0700 (PDT) X-Received: by 2002:ac2:5c11:: with SMTP id r17mr7038976lfp.61.1569433727828; Wed, 25 Sep 2019 10:48:47 -0700 (PDT) MIME-Version: 1.0 References: <20190925165915.8135-1-cyphar@cyphar.com> <20190925165915.8135-2-cyphar@cyphar.com> <20190925172049.skm6ohnnxpofdkzv@yavin> In-Reply-To: <20190925172049.skm6ohnnxpofdkzv@yavin> From: Linus Torvalds Date: Wed, 25 Sep 2019 10:48:31 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v1 1/4] lib: introduce copy_struct_from_user() helper To: Aleksa Sarai Cc: Ingo Molnar , Peter Zijlstra , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Christian Brauner , Rasmus Villemoes , Al Viro , GNU C Library , Linux API , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Sep 25, 2019 at 10:21 AM Aleksa Sarai wrote: > > Just to make sure I understand, the following diff would this solve the > problem? If so, I'll apply it, and re-send in a few hours. Actually, looking at it more, it's still buggy. That final "size smaller than unsigned long" doesn't correctly handle the case of (say) a single byte in the middle of a 8-byte word. So you need to do something like this: int is_zeroed_user(const void __user *from, size_t size) { unsigned long val, mask, align; if (unlikely(!size)) return true; if (!user_access_begin(from, size)) return -EFAULT; align = (uintptr_t) from % sizeof(unsigned long); from -= align; size += align; mask = ~aligned_byte_mask(align); while (size >= sizeof(unsigned long)) { unsafe_get_user(val, (unsigned long __user *) from, err_fault); val &= mask; if (unlikely(val)) goto done; mask = ~0ul; from += sizeof(unsigned long); size -= sizeof(unsigned long); } if (size) { /* (@from + @size) is unaligned. */ unsafe_get_user(val, (unsigned long __user *) from, err_fault); mask &= aligned_byte_mask(size); val &= mask; } done: user_access_end(); return (val == 0); err_fault: user_access_end(); return -EFAULT; } note how "mask" carries around from the very beginning all the way to the end, and "align" itself is no longer used after mask has been calculated. That's required because of say a 2-byte read at offset 5. You end up with "align=5, size=7" at the beginning, and mask needs to be 0x00ffff0000000000 (on little-endian) for that final access. Anyway, I checked, and the above seems to generate ok code quality too. Sadly "unsafe_get_user()" cannot use "asm goto" because of a gcc limitation (no asm goto with outputs), so it's not _perfect_, but that's literally a compiler limitation. But I didn't actually _test_ the end result. You should probably verify that it gets the right behavior exactly for those interesting cases where we mask both the beginning and the end. Linus