Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp7192674imm; Tue, 24 Jul 2018 09:54:17 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcAx/ljt6EYYnQvtyZAd9hJBh6xN0QZRyhp2VRoaOAD033jgjxD8+v05/IRyiS8hwsAh1do X-Received: by 2002:a63:f919:: with SMTP id h25-v6mr16608222pgi.401.1532451257018; Tue, 24 Jul 2018 09:54:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532451256; cv=none; d=google.com; s=arc-20160816; b=Z5TfDOBgQKvTEBKHW/Rta74INKShySx0H7biw+imXyOnKmLU/BSX8zv9BCeVpd70qH dNjyWg1nJ2/Mkz9yL326jYm6DEoJUA3zYUCz7wP777E5lx4q87nOav+G/mFWSzcsqbI+ AA2zAV9S/QJcuXPd0tkFlCNau7F423/5OTukaFfRNxHcldXuhMRdlvDooda8nH+yfLnS f/tKCIAH8MUL3h4XtiiDQMwo0WRYRqQjZR8KRYlimcvlBwrxOBUelOAT8AE7KFKvGOhL 1aV3TvSq9QFbIrXreUvgqAfmv89OLk5qSmp0cwKWs0bx43LRtZo1hYat4lvhsp6zg08k hyig== 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=XEthQhO55UFajYXolwRxBcBomydbdSRnD/7XsWAlazI=; b=yig0WJ3Q8KX8GVm5nhYLa2n99u9GSJK1TTl44Ql8MUUAA1Yjd68/R5CvdZ+xwIxqoG rx0PhLhv5pSkuGo7oY8uIqewut36wm/2iuKLWITH41vIvwjiZjNoZAkQjE9b+XG1ESnj b1TZXmc5LrQ1/kJ7eRkvM/kf7QfmBhSKh+r4QDMxkFtcnFXZ+8Bu1ufPBfKcHZUJ2ntQ Ed4ArjiDAD1uR1omTEzl2/MZ2k9l2gpoA1ehLl1r90evH2zZ1oqv8CCV00jMVzs6W875 LYganjiHwdR9i2ygXws9d5dLMGMCYtA0zLg+kdkK9pOPEs/9sUbMPpY83Kimwhz9ME7d E0oA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ixxOdkMi; 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 2-v6si635967pla.509.2018.07.24.09.54.01; Tue, 24 Jul 2018 09:54:16 -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=ixxOdkMi; 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 S2388872AbeGXR76 (ORCPT + 99 others); Tue, 24 Jul 2018 13:59:58 -0400 Received: from mail-pg1-f193.google.com ([209.85.215.193]:45101 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388472AbeGXR5I (ORCPT ); Tue, 24 Jul 2018 13:57:08 -0400 Received: by mail-pg1-f193.google.com with SMTP id f1-v6so3290451pgq.12 for ; Tue, 24 Jul 2018 09:49:47 -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=XEthQhO55UFajYXolwRxBcBomydbdSRnD/7XsWAlazI=; b=ixxOdkMi9vZPN9eRiFZDAkxfO2bTTNYRzxZYzn4NSWx6QOYFVL6oePm0o1WULyMlG3 +xbfQ2qCdDwQV6/w1qUfpWYXJaH6ouXgwNpjDmjX/SnKwBqOvIhpY2e3BSqFrg8aUM8j W4y0jfjC4dAG5XOfOH3+UjWHqNXo1eRaVlSuo= 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=XEthQhO55UFajYXolwRxBcBomydbdSRnD/7XsWAlazI=; b=KDxQ2G9oVvU0sPi5/NRG71zoV63RJ4JxAmj926Sw32HGTiAaV1beg9ciBjBI9tUfXN FzwxneilYd/9FZMA/DuEBiDSd51RVr3XrOK3reea5fAq3ta/2ZDQhjmRgE6keNPtYLFj GULsPdBHyYo6l5MzRVGJGyGWPwvcRLSU4bYyUOx0BLIFYpq/pYVYkmNgbDGzHlN3TdLJ 1z5Ld3ZtzFtc3uOzKulIm2OxBQ9t1VN2AZoN7Dms/9Qgx4BTmekp8iURjfFWbPf0bZcT qLUdQyp4LMavKkWY1oToDm5p6kTbdap5nC3i/hZut7PeOOXuuLsBR0GZ376Fc07Lh1lR or0Q== X-Gm-Message-State: AOUpUlH6FDsMCsINMJSVAxgi4U+3hLjh/JwY6wW3rU4UUn6NUSbhBvDi HePuB6aEBnWaRGJcufmdcR/HSQ== X-Received: by 2002:a63:e452:: with SMTP id i18-v6mr16893596pgk.185.1532450986562; Tue, 24 Jul 2018 09:49:46 -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 12-v6sm16652474pfx.128.2018.07.24.09.49.42 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 24 Jul 2018 09:49:42 -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 04/18] dm: Remove VLA usage from hashes Date: Tue, 24 Jul 2018 09:49:22 -0700 Message-Id: <20180724164936.37477-5-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 new HASH_MAX_DIGESTSIZE from the crypto layer to allocate the upper bounds on stack usage. [1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com Signed-off-by: Kees Cook --- drivers/md/dm-integrity.c | 23 +++++++++++++++++------ drivers/md/dm-verity-fec.c | 5 ++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/drivers/md/dm-integrity.c b/drivers/md/dm-integrity.c index 86438b2f10dd..884edd7cf1d0 100644 --- a/drivers/md/dm-integrity.c +++ b/drivers/md/dm-integrity.c @@ -521,7 +521,12 @@ static void section_mac(struct dm_integrity_c *ic, unsigned section, __u8 result } memset(result + size, 0, JOURNAL_MAC_SIZE - size); } else { - __u8 digest[size]; + __u8 digest[HASH_MAX_DIGESTSIZE]; + + if (WARN_ON(size > sizeof(digest))) { + dm_integrity_io_error(ic, "digest_size", -EINVAL); + goto err; + } r = crypto_shash_final(desc, digest); if (unlikely(r)) { dm_integrity_io_error(ic, "crypto_shash_final", r); @@ -1244,7 +1249,7 @@ static void integrity_metadata(struct work_struct *w) struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io)); char *checksums; unsigned extra_space = unlikely(digest_size > ic->tag_size) ? digest_size - ic->tag_size : 0; - char checksums_onstack[ic->tag_size + extra_space]; + char checksums_onstack[HASH_MAX_DIGESTSIZE]; unsigned sectors_to_process = dio->range.n_sectors; sector_t sector = dio->range.logical_sector; @@ -1253,8 +1258,14 @@ static void integrity_metadata(struct work_struct *w) checksums = kmalloc((PAGE_SIZE >> SECTOR_SHIFT >> ic->sb->log2_sectors_per_block) * ic->tag_size + extra_space, GFP_NOIO | __GFP_NORETRY | __GFP_NOWARN); - if (!checksums) + if (!checksums) { checksums = checksums_onstack; + if (WARN_ON(extra_space && + digest_size > sizeof(checksums_onstack))) { + r = -EINVAL; + goto error; + } + } __bio_for_each_segment(bv, bio, iter, dio->orig_bi_iter) { unsigned pos; @@ -1466,7 +1477,7 @@ static bool __journal_read_write(struct dm_integrity_io *dio, struct bio *bio, } while (++s < ic->sectors_per_block); #ifdef INTERNAL_VERIFY if (ic->internal_hash) { - char checksums_onstack[max(crypto_shash_digestsize(ic->internal_hash), ic->tag_size)]; + char checksums_onstack[max(HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)]; integrity_sector_checksum(ic, logical_sector, mem + bv.bv_offset, checksums_onstack); if (unlikely(memcmp(checksums_onstack, journal_entry_tag(ic, je), ic->tag_size))) { @@ -1516,7 +1527,7 @@ static bool __journal_read_write(struct dm_integrity_io *dio, struct bio *bio, if (ic->internal_hash) { unsigned digest_size = crypto_shash_digestsize(ic->internal_hash); if (unlikely(digest_size > ic->tag_size)) { - char checksums_onstack[digest_size]; + char checksums_onstack[HASH_MAX_DIGESTSIZE]; integrity_sector_checksum(ic, logical_sector, (char *)js, checksums_onstack); memcpy(journal_entry_tag(ic, je), checksums_onstack, ic->tag_size); } else @@ -1937,7 +1948,7 @@ static void do_journal_write(struct dm_integrity_c *ic, unsigned write_start, unlikely(from_replay) && #endif ic->internal_hash) { - char test_tag[max(crypto_shash_digestsize(ic->internal_hash), ic->tag_size)]; + char test_tag[max_t(size_t, HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)]; integrity_sector_checksum(ic, sec + ((l - j) << ic->sb->log2_sectors_per_block), (char *)access_journal_data(ic, i, l), test_tag); diff --git a/drivers/md/dm-verity-fec.c b/drivers/md/dm-verity-fec.c index 684af08d0747..0ce04e5b4afb 100644 --- a/drivers/md/dm-verity-fec.c +++ b/drivers/md/dm-verity-fec.c @@ -212,12 +212,15 @@ static int fec_read_bufs(struct dm_verity *v, struct dm_verity_io *io, struct dm_verity_fec_io *fio = fec_io(io); u64 block, ileaved; u8 *bbuf, *rs_block; - u8 want_digest[v->digest_size]; + u8 want_digest[HASH_MAX_DIGESTSIZE]; unsigned n, k; if (neras) *neras = 0; + if (WARN_ON(v->digest_size > sizeof(want_digest))) + return -EINVAL; + /* * read each of the rsn data blocks that are part of the RS block, and * interleave contents to available bufs -- 2.17.1