Received: by 10.223.185.116 with SMTP id b49csp4412470wrg; Tue, 6 Mar 2018 15:35:25 -0800 (PST) X-Google-Smtp-Source: AG47ELsjjt5azAaWyJMX0bwxSjTsxbqzWDHGUZCS8Yli1wkNP+PLvCTGy5/r9AGaGPyYujdbhTIK X-Received: by 10.99.167.66 with SMTP id w2mr16377316pgo.357.1520379325644; Tue, 06 Mar 2018 15:35:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520379325; cv=none; d=google.com; s=arc-20160816; b=zW2PQ21WjEbwrg8lxelcYCDCfuARghOy3JmJCn3d32ThYafAPek7AyvJbKYIMHSn51 GFnYxe0s11C8U4QmsKmXgzmjvTeYn7Ysaf89zh/eAe/xJgW2Cgcvbtme5Z4bX0yQecqZ PcrTRKCCynb23M8pytFJ9DbTotS9LeUh1O/iemvmjtk0hPIQRYVvweIZ18No47IRTs7U YSkRprtxCbnT3slV+hcpOd42a8ho6GRuEsUXB5mSQjaf9+6UpwH0GVscces2Tmob3R5V Uuq+xWhMGL5z/9/s9rhGNoIef8cDgL0K+B9q9gOmA+3XzGIOSr1KZWPp6RgejYLU9j1M jz6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=hsCOsz4fqYISnhu1co+ZJyT7GaPXowbIvX6m24ZwdKU=; b=w0MplEW8/Gh7v4+fTGYROlpMir1oTBnzUW+OGDmcjKlzS5BQ5VkXT1K6XaRoTakUfD Xax86O6NkSN8dQhxtzwbB1+mEPH2xZFd7QyAilZwPtbaxnqVYSUEv/uOfjiZUdEuYN3h lyxuclQcCj/bw4wq+d9guOFp9bOHiuvDqA8w7F2V0b8Aytsh+EX99qhZdPIo8AH5nDTY 4XhHdF74A8tmJitLtuf7uqUGQy9rhJBFbTzvwK2a1mT8fl8psE51leWV9wmDLnm6YHrf ujGX/vEqAcDQyKJ243vJPOg+O12oGXY0EEo9idA2+cYOmIRYERH3DI9n5DVFokf+FaMa OyoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ReU9iKQE; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w20-v6si8989789plq.486.2018.03.06.15.35.10; Tue, 06 Mar 2018 15:35:25 -0800 (PST) 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=@google.com header.s=20161025 header.b=ReU9iKQE; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754185AbeCFXPN (ORCPT + 99 others); Tue, 6 Mar 2018 18:15:13 -0500 Received: from mail-pl0-f65.google.com ([209.85.160.65]:44272 "EHLO mail-pl0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753996AbeCFXPM (ORCPT ); Tue, 6 Mar 2018 18:15:12 -0500 Received: by mail-pl0-f65.google.com with SMTP id 9-v6so208293ple.11 for ; Tue, 06 Mar 2018 15:15:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=hsCOsz4fqYISnhu1co+ZJyT7GaPXowbIvX6m24ZwdKU=; b=ReU9iKQEf3R6GlvouU1oK9lXscjF27TAJLcBIruSVLEMVz7a4iE84Sih3VlpjuwQ2O Nky1+VUDg8PXcY+vV4eqvoOrFLNp0lLMK1SaKAPYcTwUYRGpoZwAe425Yu/m5UgF4LMw DLYPxQH3B+yh8Rbfi7mL2V+JNq9CahGsAS1vS8ORnwlBv8YcTJVsdoXbMxzeRcbWXxVB jYXoSlnAfuGG6Bm+G3psWXyJuc7L82NTGlxQiVWk3VA9UiuzkIyxFNHGqY8+oJ57cmjp dfa3w88JRJSf1g05+3WTBGPGXBaP74JSuHhHftt0uy6G5XJYxBLj5S+TXMnAVwhOVif2 E+4Q== 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; bh=hsCOsz4fqYISnhu1co+ZJyT7GaPXowbIvX6m24ZwdKU=; b=qI08dHDMLaCiBm/TvQLwcH9xORNjcYFHjhM+1ItiCiMj7ngiX4djgNlrdEXH9NYvL3 3pL+aM96h/ZozBIe4VdV+sDndm2azA0//sMBtNi9t1L/FY/m3wLDU9EtSlj7vJFOMRxp Rh0oq55to4mEyqz0uYDQGdt3HdekocCNIOcvCqH+3Ak/gOjwrsAq6eN3BXZm1iR0x+vk RIUx5iW4auilZEWLo8zL6jtLj68CsfIdtbH8tpOLKBWD195rn+rmB6AadlNjSCAK2jT0 IMZwErL2SYxpj2UL/RY7PT+l0yu3QM+3F2PvkCValehB7VNxAHzumyjuz4P/3ehIVO3a MomA== X-Gm-Message-State: AElRT7E4O26lJsHz7Bt/VDVhgCEVy1OxHKZ6f5bDzVBWv4/z1XwJZ7Mp lfDNH6s8ChmZ/iKKcxK8qrzv4Q== X-Received: by 2002:a17:902:a985:: with SMTP id bh5-v6mr12482787plb.35.1520378111146; Tue, 06 Mar 2018 15:15:11 -0800 (PST) Received: from totte1.mtv.corp.google.com ([2620:0:1000:1601:b5a1:c7a1:e416:5491]) by smtp.gmail.com with ESMTPSA id r78sm33817782pfb.24.2018.03.06.15.15.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Mar 2018 15:15:10 -0800 (PST) From: Patrik Torstensson To: Alasdair Kergon , Mike Snitzer , dm-devel@redhat.com Cc: linux-kernel@vger.kernel.org, ebiggers@google.com, samitolvanen@google.com, gkaiser@google.com, paulcrowley@google.com, Patrik Torstensson Subject: [PATCH] Add an option to dm-verity to validate hashes at most once Date: Tue, 6 Mar 2018 15:14:56 -0800 Message-Id: <20180306231456.210504-1-totte@google.com> X-Mailer: git-send-email 2.16.2.395.g2e18187dfd-goog Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add an option to dm-verity to validate hashes at most once to allow platforms that is CPU/memory contraint to be protected by dm-verity against offline attacks. The option introduces a bitset that is used to check if a block has been validated before or not. A block can be validated more than once as there is no thread protection for the bitset. This patch has been developed and tested on entry-level Android Go devices. Signed-off-by: Patrik Torstensson --- drivers/md/dm-verity-target.c | 58 +++++++++++++++++++++++++++++++++-- drivers/md/dm-verity.h | 1 + 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c index aedb8222836b..479d0af212bf 100644 --- a/drivers/md/dm-verity-target.c +++ b/drivers/md/dm-verity-target.c @@ -32,6 +32,7 @@ #define DM_VERITY_OPT_LOGGING "ignore_corruption" #define DM_VERITY_OPT_RESTART "restart_on_corruption" #define DM_VERITY_OPT_IGN_ZEROES "ignore_zero_blocks" +#define DM_VERITY_OPT_AT_MOST_ONCE "check_at_most_once" #define DM_VERITY_OPTS_MAX (2 + DM_VERITY_OPTS_FEC) @@ -432,6 +433,19 @@ static int verity_bv_zero(struct dm_verity *v, struct dm_verity_io *io, return 0; } +/* + * Moves the bio iter one data block forward. + */ +static inline void verity_bv_skip_block(struct dm_verity *v, + struct dm_verity_io *io, + struct bvec_iter *iter) +{ + struct bio *bio = dm_bio_from_per_bio_data(io, + v->ti->per_io_data_size); + + bio_advance_iter(bio, iter, 1 << v->data_dev_block_bits); +} + /* * Verify one "dm_verity_io" structure. */ @@ -445,8 +459,15 @@ static int verity_verify_io(struct dm_verity_io *io) for (b = 0; b < io->n_blocks; b++) { int r; + sector_t cur_block = io->block + b; struct ahash_request *req = verity_io_hash_req(v, io); + if (v->validated_blocks && + likely(test_bit(cur_block, v->validated_blocks))) { + verity_bv_skip_block(v, io, &io->iter); + continue; + } + r = verity_hash_for_block(v, io, io->block + b, verity_io_want_digest(v, io), &is_zero); @@ -481,13 +502,17 @@ static int verity_verify_io(struct dm_verity_io *io) return r; if (likely(memcmp(verity_io_real_digest(v, io), - verity_io_want_digest(v, io), v->digest_size) == 0)) + verity_io_want_digest(v, io), + v->digest_size) == 0)) { + if (v->validated_blocks) + set_bit(cur_block, v->validated_blocks); continue; + } else if (verity_fec_decode(v, io, DM_VERITY_BLOCK_TYPE_DATA, - io->block + b, NULL, &start) == 0) + cur_block, NULL, &start) == 0) continue; else if (verity_handle_err(v, DM_VERITY_BLOCK_TYPE_DATA, - io->block + b)) + cur_block)) return -EIO; } @@ -740,6 +765,7 @@ static void verity_dtr(struct dm_target *ti) if (v->bufio) dm_bufio_client_destroy(v->bufio); + kvfree(v->validated_blocks); kfree(v->salt); kfree(v->root_digest); kfree(v->zero_digest); @@ -760,6 +786,26 @@ static void verity_dtr(struct dm_target *ti) kfree(v); } +static int verity_alloc_most_once(struct dm_verity *v) +{ + struct dm_target *ti = v->ti; + + /* the bitset can only handle INT_MAX blocks */ + if (v->data_blocks > INT_MAX) { + ti->error = "device too large to use check_at_most_once"; + return -E2BIG; + } + + v->validated_blocks = kvzalloc(BITS_TO_LONGS(v->data_blocks) + * sizeof(unsigned long), GFP_KERNEL); + if (!v->validated_blocks) { + ti->error = "failed to allocate bitset for check_at_most_once"; + return -ENOMEM; + } + + return 0; +} + static int verity_alloc_zero_digest(struct dm_verity *v) { int r = -ENOMEM; @@ -829,6 +875,12 @@ static int verity_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v) } continue; + } else if (!strcasecmp(arg_name, DM_VERITY_OPT_AT_MOST_ONCE)) { + r = verity_alloc_most_once(v); + if (r) + return r; + continue; + } else if (verity_is_fec_opt_arg(arg_name)) { r = verity_fec_parse_opt_args(as, v, &argc, arg_name); if (r) diff --git a/drivers/md/dm-verity.h b/drivers/md/dm-verity.h index b675bc015512..ace5ec781b5f 100644 --- a/drivers/md/dm-verity.h +++ b/drivers/md/dm-verity.h @@ -63,6 +63,7 @@ struct dm_verity { sector_t hash_level_block[DM_VERITY_MAX_LEVELS]; struct dm_verity_fec *fec; /* forward error correction */ + unsigned long *validated_blocks; /* bitset blocks validated */ }; struct dm_verity_io { -- 2.16.2.395.g2e18187dfd-goog