From patchwork Tue Mar 6 23:14:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrik Torstensson X-Patchwork-Id: 10263681 X-Patchwork-Delegate: snitzer@redhat.com Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 82E976016D for ; Wed, 7 Mar 2018 09:19:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 73BE428771 for ; Wed, 7 Mar 2018 09:19:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 68161293F1; Wed, 7 Mar 2018 09:19:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id F0E17293F0 for ; Wed, 7 Mar 2018 09:19:12 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EA93636809; Wed, 7 Mar 2018 09:19:11 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B08345D9CA; Wed, 7 Mar 2018 09:19:11 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id B1C394A46E; Wed, 7 Mar 2018 09:19:10 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w26NFIwt030759 for ; Tue, 6 Mar 2018 18:15:18 -0500 Received: by smtp.corp.redhat.com (Postfix) id 0A5AF5D75A; Tue, 6 Mar 2018 23:15:18 +0000 (UTC) Delivered-To: dm-devel@redhat.com Received: from mx1.redhat.com (ext-mx01.extmail.prod.ext.phx2.redhat.com [10.5.110.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0507F61F55 for ; Tue, 6 Mar 2018 23:15:13 +0000 (UTC) Received: from mail-pl0-f65.google.com (mail-pl0-f65.google.com [209.85.160.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4623A81DED for ; Tue, 6 Mar 2018 23:15:12 +0000 (UTC) Received: by mail-pl0-f65.google.com with SMTP id c11-v6so222841plo.0 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=Lf62xSU5vAl1eiiBCc2Fy3W6UUmfL3GOzTwnHvUItJZK5QrxMJhgs3NwS0h3jiYLIo KUdZwrz6D4p1ZqeGt/QsMl3CRZ0XANnERE4OX2AOYv0rialZ6HkGGXqVknDgp1FLb1Dn yop4u6y9RFJDktNTlkT8C2PBg3CT93ea6Tsw2LQNpMUOlGTJZUnjDw9lVKKpVVm6p0gn 7hKFlZgOzp8hxoUl8sdiK6ZRZj3sKMwhSgdMunol07b98A8MsCl7PqxNK73bF24w+pOn LWQl71jXP+6g9kYlkrxQnsiunx6pPbgJxXIAGtiiOrv2eT8V6rN7mE9/eWkEbQv4x6ST 2JvA== X-Gm-Message-State: AElRT7FaB55BL5LmuH8phlu6ScY0WIr/Ppl1SRfw9CUHDEVbtlEeGasl /hDmYBTxYkfwLVAow83sNRTWSA== X-Google-Smtp-Source: AG47ELut4A8ZPK+RwSa7PeaK6doJCPM8f1pqEqNLHWbKVtklGTp+NfyEmeoVL1txRDMgEawEnN9gHw== 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 Date: Tue, 6 Mar 2018 15:14:56 -0800 Message-Id: <20180306231456.210504-1-totte@google.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 06 Mar 2018 23:15:12 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 06 Mar 2018 23:15:12 +0000 (UTC) for IP:'209.85.160.65' DOMAIN:'mail-pl0-f65.google.com' HELO:'mail-pl0-f65.google.com' FROM:'totte@google.com' RCPT:'' X-RedHat-Spam-Score: -0.141 (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_PASS, T_RP_MATCHES_RCVD) 209.85.160.65 mail-pl0-f65.google.com 209.85.160.65 mail-pl0-f65.google.com X-Scanned-By: MIMEDefang 2.83 on 10.5.110.25 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: dm-devel@redhat.com X-Mailman-Approved-At: Wed, 07 Mar 2018 04:19:09 -0500 Cc: gkaiser@google.com, ebiggers@google.com, Patrik Torstensson , linux-kernel@vger.kernel.org, samitolvanen@google.com, paulcrowley@google.com Subject: [dm-devel] [PATCH] Add an option to dm-verity to validate hashes at most once X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Wed, 07 Mar 2018 09:19:12 +0000 (UTC) X-Virus-Scanned: ClamAV using ClamSMTP 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 {