Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp7191786imm; Tue, 24 Jul 2018 09:53:18 -0700 (PDT) X-Google-Smtp-Source: AAOMgpeBFmPHIus24EF7Xc4RST6oZ6cqda70f8czGskiE2O1FztIVXj6qOYIXAIbTDbDJe8pQBMC X-Received: by 2002:a63:1b5c:: with SMTP id b28-v6mr17481981pgm.204.1532451198029; Tue, 24 Jul 2018 09:53:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532451198; cv=none; d=google.com; s=arc-20160816; b=j8g3k7RPCe3dbwVVbaEoRNbjVCxo37L7gYyXSy5xVkCHqdT9bUrBYdiAg589R+jt0m CD6uF7yEm+/Dmh2CrbfYvhwy9JR0KguBCn67hP6FSdwI+/5fbpv8wnI6LJiPjjp7NLAE xB/bMsHbmqaIUEnnvVqrvUqbZzmXWHuBOdSPxNf0F0bz26zXuOIkvA1cu/1C9NPMBhwN GkDI5BK/mRKsXH9/ef6nFW2W+9ij0LKZZ51ACRUB3ysPBq+8XEpI6HsCc2Ggv8G3dJ/1 b26yp9rkBhK4m4ZBO2oLl3OSKNbMWmyKmHvMWTWba3C6WpNVoCJqykigGZLjkv4zCCvL IM5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=Ok95ycGnZuw2CZpO4KgRNxj4QX0cXIlEQlrBt5ewdsw=; b=mvglpKMBjqVVlUchTS5ctemdmeZS5TyqexIEK2idVdo+Z62C9kRX1z+06Etcfpiy/u X7lKQqcPPXGL56h2qK/zxYMsy9dsukVkr5Pz4UoGFe0ogkb4TQoGVm2Vs5y6NDl2Lqj7 sp+YE4ukw4y8XnXjX1btaOOd3hcCXx3TRaFs598BsiapKaGdjfhh8qfx8nEV0WsQ+u/C WO4cVCHdeJy7KT7zf6MdwyXmJkIocIPBKita2O174xrBYlYmEJqAfmQgU3c/7jY2xPab 7u8dBqpsqJT63VH2qso32cTxpkw0NHhqTehApwOw/RBYyHOoOQh48sEFJZ3Bp7OpWbqh zTVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=jQ5y83FY; 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=pass (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 s15-v6si10907727pgm.56.2018.07.24.09.53.03; Tue, 24 Jul 2018 09:53:17 -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=@chromium.org header.s=google header.b=jQ5y83FY; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388642AbeGXR5O (ORCPT + 99 others); Tue, 24 Jul 2018 13:57:14 -0400 Received: from mail-pg1-f195.google.com ([209.85.215.195]:43359 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388598AbeGXR5N (ORCPT ); Tue, 24 Jul 2018 13:57:13 -0400 Received: by mail-pg1-f195.google.com with SMTP id v13-v6so3294519pgr.10 for ; Tue, 24 Jul 2018 09:49:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Ok95ycGnZuw2CZpO4KgRNxj4QX0cXIlEQlrBt5ewdsw=; b=jQ5y83FYcXRBUM77JJA0nyUNgrejZPE5lJ0JT0iizxWJfPIBVNOrdtpeO7rT+b2lsv +bjOyNg4sn32L5cfKvZIdvHZhJj8DFiyCgt6UyipV5eZBsz7wNOkN0Bulbb9Gj/WzKGr ZuhcetVCR/7gfa6Kfm6K+hpLj/2AObwWbUbIA= 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; bh=Ok95ycGnZuw2CZpO4KgRNxj4QX0cXIlEQlrBt5ewdsw=; b=kbQjKenFmxQFby2cCAWv7rrb5rk0opB5hZ0VTRsZpt01hQar8ylBS6D3DSjRgo4wTi fRijJqnasEkBLWFj2CJHO6R/FP/S1YGVxqTHHZY5AmpuX/q8LECpjMujm/NTQNL5yvsq DpdlLjGwmBj+jGyWooKbRnuTlc7Xu0CdY6cN2T/631XEPD3d0B/CEY/hW1e6HQ4ETAVS l08lfnuxJ3wH8opDppgtYcTZG8n1ycEptPHaibE0eS/+P7OxGbiOpry2QGTliM9mx6x5 KjLiWn31oOez0z42GcTFqZ2A3HsdL0fttzZ4GLvwBMFarB3yagenDipRhkIf+wZ80ja7 +tDg== X-Gm-Message-State: AOUpUlFZdNUpcKQkB5AkxnN1pqH5aeaQqqSyyn9bXZQNpiqELO8kADGr U4sGtC9eIyG96CVw0izM8LYBUg== X-Received: by 2002:a65:4384:: with SMTP id m4-v6mr5659689pgp.265.1532450992012; Tue, 24 Jul 2018 09:49:52 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id x65-v6sm6984082pfk.140.2018.07.24.09.49.45 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 24 Jul 2018 09:49:47 -0700 (PDT) From: Kees Cook To: Herbert Xu Cc: Kees Cook , Arnd Bergmann , Eric Biggers , "Gustavo A. R. Silva" , Alasdair Kergon , Rabin Vincent , Tim Chen , "Rafael J. Wysocki" , Pavel Machek , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , x86@kernel.org, Philipp Reisner , Lars Ellenberg , Jens Axboe , Giovanni Cabiddu , Mike Snitzer , Paul Mackerras , Greg Kroah-Hartman , David Howells , Johannes Berg , Tudor-Dan Ambarus , Jia-Ju Bai , Andrew Morton , Geert Uytterhoeven , Josh Poimboeuf , David Woodhouse , Will Deacon , dm-devel@redhat.com, linux-pm@vger.kernel.org, linux-crypto@vger.kernel.org, drbd-dev@lists.linbit.com, linux-block@vger.kernel.org, qat-linux@intel.com, linux-ppp@vger.kernel.org, netdev@vger.kernel.org, devel@driverdev.osuosl.org, linux-afs@lists.infradead.org, linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 07/18] crypto: shash: Remove VLA usage in unaligned hashing Date: Tue, 24 Jul 2018 09:49:25 -0700 Message-Id: <20180724164936.37477-8-keescook@chromium.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180724164936.37477-1-keescook@chromium.org> References: <20180724164936.37477-1-keescook@chromium.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In the quest to remove all stack VLA usage from the kernel[1], this uses the newly defined max alignment to perform unaligned hashing to avoid VLAs, and drops the helper function while adding sanity checks on the resulting buffer sizes. Additionally, the __aligned_largest macro is removed since this helper was the only user. [1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com Signed-off-by: Kees Cook --- crypto/shash.c | 27 ++++++++++++++++----------- include/linux/compiler-gcc.h | 1 - 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/crypto/shash.c b/crypto/shash.c index 86d76b5c626c..d21f04d70dce 100644 --- a/crypto/shash.c +++ b/crypto/shash.c @@ -73,13 +73,6 @@ int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key, } EXPORT_SYMBOL_GPL(crypto_shash_setkey); -static inline unsigned int shash_align_buffer_size(unsigned len, - unsigned long mask) -{ - typedef u8 __aligned_largest u8_aligned; - return len + (mask & ~(__alignof__(u8_aligned) - 1)); -} - static int shash_update_unaligned(struct shash_desc *desc, const u8 *data, unsigned int len) { @@ -88,11 +81,17 @@ 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; + /* + * We cannot count on __aligned() working for large values: + * https://patchwork.kernel.org/patch/9507697/ + */ + u8 ubuf[MAX_ALGAPI_ALIGNMASK * 2]; u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1); int err; + if (WARN_ON(buf + unaligned_len > ubuf + sizeof(ubuf))) + return -EINVAL; + if (unaligned_len > len) unaligned_len = len; @@ -124,11 +123,17 @@ static int shash_final_unaligned(struct shash_desc *desc, u8 *out) unsigned long alignmask = crypto_shash_alignmask(tfm); struct shash_alg *shash = crypto_shash_alg(tfm); unsigned int ds = crypto_shash_digestsize(tfm); - u8 ubuf[shash_align_buffer_size(ds, alignmask)] - __aligned_largest; + /* + * We cannot count on __aligned() working for large values: + * https://patchwork.kernel.org/patch/9507697/ + */ + u8 ubuf[MAX_ALGAPI_ALIGNMASK + HASH_MAX_DIGESTSIZE]; u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1); int err; + if (WARN_ON(buf + ds > ubuf + sizeof(ubuf))) + return -EINVAL; + err = shash->final(desc, buf); if (err) goto out; diff --git a/include/linux/compiler-gcc.h b/include/linux/compiler-gcc.h index f1a7492a5cc8..1f1cdef36a82 100644 --- a/include/linux/compiler-gcc.h +++ b/include/linux/compiler-gcc.h @@ -125,7 +125,6 @@ */ #define __pure __attribute__((pure)) #define __aligned(x) __attribute__((aligned(x))) -#define __aligned_largest __attribute__((aligned)) #define __printf(a, b) __attribute__((format(printf, a, b))) #define __scanf(a, b) __attribute__((format(scanf, a, b))) #define __attribute_const__ __attribute__((__const__)) -- 2.17.1