Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp237036imm; Mon, 2 Jul 2018 10:37:22 -0700 (PDT) X-Google-Smtp-Source: AAOMgpeqkFeou6H7svu73l397cYaySrawn3oeCf9m1HqXN+3emxAqUeNI10IyJ4cWRVaytwRRwvb X-Received: by 2002:a62:c699:: with SMTP id x25-v6mr22650281pfk.16.1530553042236; Mon, 02 Jul 2018 10:37:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530553042; cv=none; d=google.com; s=arc-20160816; b=Uel22I2WUvO7ma7cJuqqbiaiLV49+ec5280CMjI48HqaP8e0ZBh3qIfnYZDC6HrE9m RwP3S/RaWtLOJguH91eTAxetVSB9cc2y/m3n+wHpnh1rFTt+BA8aFFDrq36v7ddYsU47 F+FwfyjSJ5f6JTnJBpX/Y0lu9+TlG9bS0cm4/hIO/mqYbEYdogMCznL6BQMfjMxq4G3p 4Oj5B+238tiqDyUoJz4teKW78WtoEAPe9rpdYXs5/ixuQ1YbwSX+LghYHy4EUZFvlnmv DMSkqbN0g6iItL7znlB0LfbQ2rkIgPlul6TkaC5Gru3e2Nkv7JVpxOyF+y62eHGzBCdd TRhg== 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 :references:in-reply-to:mime-version:dkim-signature:dkim-signature :arc-authentication-results; bh=2Yqe4porHSYWUjvrFBZ245/CUtjFWO7UZyqf+k+Iml4=; b=AxYhaETGvnGcePSDoWTH8UkdA0UC4Sti9fA92qx3hFvrliD2WRuWmym175CTHWqdIy hoxUiR+e08/cRbwd31/BeMT+JR+/VqQD5yeiZVqvP5iWxqCV0Vurc/vl1APiix9YQ8AH mq8vjBrZtkvTJrRlAHzfz2MacHXabgswYxlrmSz9UboNQ5o14SjOuQTXmjTlM82eNGD9 E8fbHaTwZaWdxU0LotZ1RUV+N23fA60zdqmBYKhuwc0oLtQmKXDXabpvJpyDeiJPg4UQ LGE8WhYGhoUQeLg6WMij1GDA0W5iulRG0ofdhbP9TX5eZogesjV9QwmXa0x6TwO4pdjb mYow== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@google.com header.s=20161025 header.b="QRvBZA/O"; dkim=fail header.i=@chromium.org header.s=google header.b=NGYOkMq6; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o3-v6si17394343pld.50.2018.07.02.10.37.07; Mon, 02 Jul 2018 10:37:22 -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=fail header.i=@google.com header.s=20161025 header.b="QRvBZA/O"; dkim=fail header.i=@chromium.org header.s=google header.b=NGYOkMq6; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753179AbeGBRew (ORCPT + 99 others); Mon, 2 Jul 2018 13:34:52 -0400 Received: from mail-yw0-f194.google.com ([209.85.161.194]:44926 "EHLO mail-yw0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753163AbeGBRet (ORCPT ); Mon, 2 Jul 2018 13:34:49 -0400 Received: by mail-yw0-f194.google.com with SMTP id k18-v6so6935553ywm.11 for ; Mon, 02 Jul 2018 10:34:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=2Yqe4porHSYWUjvrFBZ245/CUtjFWO7UZyqf+k+Iml4=; b=QRvBZA/OctClp28NfUzI/RMMu3hOpq1yfC2JdfDhOOTSEMY29ONLRxzxB/qUZ53AEq ZMApbctfpOp2LhjElHvnpV/3Q4PA7Kywge7z4MqD+D9ut+whELG/MkWCX9o/xQs0vNwK Ib1jWVpzfsxPlar6i26H5jEnObI7zKzYm6R9YUCwXuhekRD7GMWL4msEh3Kt6sxFQFZ6 cMLBx1wqrI2nsgFJfG0x7PpGkdE5e0UaomiwWTaE/cUnWPm5cDjOvddF4DOPvnOet4gS 0CPCRw2JMhRdblZ0bSISZwElQKAeprN1KBW6a8JHbBUHaDPfjzHykysTvGIXX/NlyQFL EGcA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=2Yqe4porHSYWUjvrFBZ245/CUtjFWO7UZyqf+k+Iml4=; b=NGYOkMq6YG+WB97k6Tp+6blrPI1LCdVSJdryv1WaU1HoNoHt4atcw68f+FSVxsgLBo rhP48zMUr0C/FP4Cg9Pkjf+eWpPo8vTBsQEGiD/3iyopUFUPueb0OW91slhC1SU8SjsX CmMnNHha75sEpUKfylA/26eQ4uNf0n8VHPGGE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=2Yqe4porHSYWUjvrFBZ245/CUtjFWO7UZyqf+k+Iml4=; b=SZE+dxrqsNxwB2ubXXZyjt3Nn+B84V6mLuZ1YDQLAHZwFSiuGiwYn8eV0GCqED6b3N 7AE9RKv3kAi+xaP/YfxI1WTaMtNNfQjeaAWlvmHf/gyQQmDN1tR6MY60C0sOp0sVuh60 4TxEJXq9KFHgZR/6aAFOF0JaZEW5efRZSutGRawYzwlinrBRxpESgjJ8m5hulIVVjLjG W2ETufGd6mbxU4t7/Thnvh23ZHrJwJgQrA8Qub4QD866PHGUMxJVTKEO5+foIvJUN3Fe GpGsQ+mAUwZClrB4oWTRpcSqVPh3aT542w/YnV1YIiz+DxSZp1LiXcC3QW9AVNHWYFkr lvZA== X-Gm-Message-State: APt69E1oJda6zOmq/maEjPtDQfJ+ipOsMMjtrL55nqB10Z0TeqBUrMOi USVzNxddYAV7wr2alKSnUD7HH/XxdHuNR5DvfNmQ/w== X-Received: by 2002:a81:2706:: with SMTP id n6-v6mr12616593ywn.88.1530552887462; Mon, 02 Jul 2018 10:34:47 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a25:5f51:0:0:0:0:0 with HTTP; Mon, 2 Jul 2018 10:34:46 -0700 (PDT) In-Reply-To: <20180701172058.GA26715@sol.localdomain> References: <20180629002843.31095-1-keescook@chromium.org> <20180629002843.31095-10-keescook@chromium.org> <20180630070301.GA1706@sol.localdomain> <20180701172058.GA26715@sol.localdomain> From: Kees Cook Date: Mon, 2 Jul 2018 10:34:46 -0700 X-Google-Sender-Auth: HOMQMXN8KG2a7815P-Gq9mC4clk Message-ID: Subject: Re: [dm-devel] [PATCH v3 9/9] crypto: shash: Remove VLA usage in unaligned hashing To: Eric Biggers Cc: Herbert Xu , Giovanni Cabiddu , Arnd Bergmann , Eric Biggers , Mike Snitzer , "Gustavo A. R. Silva" , qat-linux@intel.com, LKML , dm-devel@redhat.com, linux-crypto , Lars Persson , Tim Chen , "David S. Miller" , Alasdair Kergon , Rabin Vincent 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 Sun, Jul 1, 2018 at 10:20 AM, Eric Biggers wrote: > On Sun, Jul 01, 2018 at 10:04:59AM -0700, Kees Cook wrote: >> On Sat, Jun 30, 2018 at 12:03 AM, Eric Biggers wrote: >> > On Thu, Jun 28, 2018 at 05:28:43PM -0700, Kees Cook wrote: >> >> @@ -88,11 +81,13 @@ static int shash_update_unaligned(struct shash_desc *desc, const u8 *data, >> >> unsigned long alignmask = crypto_shash_alignmask(tfm); >> >> unsigned int unaligned_len = alignmask + 1 - >> >> ((unsigned long)data & alignmask); >> >> - u8 ubuf[shash_align_buffer_size(unaligned_len, alignmask)] >> >> - __aligned_largest; >> >> + u8 ubuf[MAX_ALGAPI_ALIGNMASK + 1]; >> >> u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1); >> >> int err; >> >> >> >> + if (WARN_ON(buf + unaligned_len > ubuf + sizeof(ubuf))) >> >> + return -EINVAL; >> >> + >> > >> > How is 'ubuf' guaranteed to be large enough? You removed the __aligned >> > attribute, so 'ubuf' can have any alignment. So the aligned pointer 'buf' may >> > be as high as '&ubuf[alignmask]'. Then, up to 'alignmask' bytes of data will be >> > copied into 'buf'... resulting in up to '2 * alignmask' bytes needed in 'ubuf'. >> > But you've only guaranteed 'alignmask + 1' bytes. >> >> Hm, good point. Adding __aligned(MAX_ALGAPI_ALIGNMASK + 1) looks to >> fix this, yes? >> >> Also, if __aligned() is used here, can't PTR_ALIGN() be dropped? (I >> think you pointed this out earlier.) > > Sure, I'm just not sure whether __aligned() with such a large alignment is > guaranteed to work on stack variables on all architectures. See e.g. > https://patchwork.kernel.org/patch/9507697/. That's terrible. :( That seems like a compiler bug, but okay. >> Also, is "unaligned_len" being calculated correctly? Let's say >> alignmask is 63. If data is binary ...111111, then unaligned_len will >> be 64 - 63 == 1, which is fine: we copy 1 byte out, bump the address >> by 1, and we're happily aligned to ...000000. If data is ...000000, >> then unaligned_len will be 64. But it should be 0. Shouldn't this be: >> >> unsigned int unaligned_len; >> >> unaligned_len = (unsigned long)data & alignmask; >> if (unaligned_len) >> unaligned_len = alignmask + 1 - unaligned_len; >> >> And then ubuf only needs to be MAX_ALGAPI_ALIGNMASK, without the +1? > > shash_update_unaligned() is only called when 'data & alignmask'. > Similarly with shash_final_unaligned(). Ah! I see that now. > Though, calculating 'unaligned_len' could be simplified to > > unsigned int unaligned_len = -(unsigned long)data & alignmask; > > which works either way. So, since we can't depend on __aligned() working, I'll just keep the PTR_ALIGN and add MAX_ALGAPI_ALIGNMASK to each array. That'll be less memory-efficient, but it'll actually get aligned correctly. -Kees -- Kees Cook Pixel Security