Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp1784815imi; Sat, 23 Jul 2022 18:06:20 -0700 (PDT) X-Google-Smtp-Source: AGRyM1ulYV+87BRqyq0dTN64wn8WxREG4B127KCOR9Wq37pwl8VcI1ZEmCZMzfo+v1oeS47jVehJ X-Received: by 2002:a17:906:58d0:b0:72e:e25a:46e7 with SMTP id e16-20020a17090658d000b0072ee25a46e7mr4986590ejs.459.1658624779928; Sat, 23 Jul 2022 18:06:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658624779; cv=none; d=google.com; s=arc-20160816; b=DSh6D8jVP1NDF5MtJxj44hNs+v4crI1I4RGXeMDfCBPCMCHjJibogpCSsjKB2Y9Drm rXjOsIpR5UNAwyqIA6axInAfdHd+BPGbf6/iCCFCj6m2KXGtOAuzx/Yl35UNNsafZNKI jjFOfBYY5EHnhANRlkQBPFG76iDrnNC04kkNIZSWS9xvaX/OWECEWyhoIvtU8iRPcd6U Gg6Q2EQ7c1y3A5Ow/UKG3Y2+uDbG4gLsPYAdmtnGHfxUi+ZyU8POdkijOZ4+c+fOAFHE BZji+Z4swrACUnThc49nKOYVxtJa+2JWIvzUKnvIXchpbBEI1Fw9Dz3A8mLglWESbp0Q ssNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=8nYP/mighbY7HkK+3pThpyKfJWuBvQJ5ESTc7PzJJ0s=; b=MDhfBuHp6/5LK0/oj7FV9HTfy2zvTQnb/libsKPQF+AeGsOS1I7Z/MCPaWEGT9gUK9 /jJhVyc4Fx5yoBYRQaSY5DRpHKiQyKBnt5fS9DQyY0oRFkeaojGhscaV1Lg6Cy0ljBHD yIUapuHUPvVKTVbnstoQyeWnPMpWeoS1OWEV1ArSSM6vvimV1RE7oL5K/bVog525a47s YP4AiN3WOSLSP+Qs+b2rfWYRN42AH5o/tyG11179HW1OZmg8YFh9OMMxH+WniRxLezI+ UWQ/I1II/zL+/JzYRHwMu4M83w9FaFVdafpRjV5C0VkIQ24W8soWQy8MGQJ63/LWujh+ azrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (bad format) header.i=@dorminy.me header.s=mail header.b=WzMJJA4a; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=dorminy.me Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i14-20020a0564020f0e00b0043bce7d8325si6205334eda.543.2022.07.23.18.05.55; Sat, 23 Jul 2022 18:06:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=neutral (bad format) header.i=@dorminy.me header.s=mail header.b=WzMJJA4a; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=dorminy.me Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239620AbiGXAzj (ORCPT + 99 others); Sat, 23 Jul 2022 20:55:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239497AbiGXAzS (ORCPT ); Sat, 23 Jul 2022 20:55:18 -0400 Received: from box.fidei.email (box.fidei.email [71.19.144.250]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A03391ADB7; Sat, 23 Jul 2022 17:55:00 -0700 (PDT) Received: from authenticated-user (box.fidei.email [71.19.144.250]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by box.fidei.email (Postfix) with ESMTPSA id CC84680BB8; Sat, 23 Jul 2022 20:54:58 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=dorminy.me; s=mail; t=1658624099; bh=otwv5bBYWXt+KfSPuzurzl6xT3sEMYqE9TpfS6zqEu8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WzMJJA4aJYHeCCWtTN23YDurbKSqUSsLgFKE68zMur9y0mrdzHdFnnJdAc5CYFiVH 1vfxUDc2YDI8OufkQN/kCp4VGM77Wxl5cfjyn9+7O5m13azSpOSO3CMoQCa0WVz0sv OofCCum+pK1gdU11dN53Q6d2qrwpFLP/+kdBWa/LBKzltyb9eqGj6cVKOac77IgbLn A7tjBdYMSuHyjfDluePImvNcXgaeMwK50vHsmc/tisbODCpGQ4rUGRgvQ1L4bQ5Gdj O6FBRn9BFmkHAVRp6pbW6joDMyDeW+5jk7ymm/3+iVa+lkn61anOb24Lkompv6RG1e T9lii1mNR+3FQ== From: Sweet Tea Dorminy To: Chris Mason , Josef Bacik , David Sterba , linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org, osandov@osandov.com, kernel-team@fb.com Cc: Sweet Tea Dorminy Subject: [PATCH RFC v2 15/16] btrfs: encrypt normal file extent data if appropriate Date: Sat, 23 Jul 2022 20:54:00 -0400 Message-Id: <8685022a83df3cb910d1e78003650a263dfe862d.1658623319.git.sweettea-kernel@dorminy.me> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Omar Sandoval Add in the necessary calls to encrypt and decrypt data to achieve encryption of normal data. Signed-off-by: Omar Sandoval Signed-off-by: Sweet Tea Dorminy --- fs/btrfs/extent_io.c | 56 ++++++++++++++++++++++++++++++++++++----- fs/btrfs/file-item.c | 9 +++++-- fs/btrfs/fscrypt.c | 27 ++++++++++++++++---- fs/btrfs/tree-checker.c | 11 +++++--- 4 files changed, 87 insertions(+), 16 deletions(-) diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c index d9b924d6f73f..7e1be83ccabf 100644 --- a/fs/btrfs/extent_io.c +++ b/fs/btrfs/extent_io.c @@ -184,6 +184,7 @@ static void submit_one_bio(struct btrfs_bio_ctrl *bio_ctrl) { struct bio *bio; struct bio_vec *bv; + struct page *first_page; struct inode *inode; int mirror_num; @@ -192,13 +193,17 @@ static void submit_one_bio(struct btrfs_bio_ctrl *bio_ctrl) bio = bio_ctrl->bio; bv = bio_first_bvec_all(bio); - inode = bv->bv_page->mapping->host; + first_page = bio_first_page_all(bio); + if (fscrypt_is_bounce_page(first_page)) + inode = fscrypt_pagecache_page(first_page)->mapping->host; + else + inode = first_page->mapping->host; mirror_num = bio_ctrl->mirror_num; /* Caller should ensure the bio has at least some range added */ ASSERT(bio->bi_iter.bi_size); - btrfs_bio(bio)->file_offset = page_offset(bv->bv_page) + bv->bv_offset; + btrfs_bio(bio)->file_offset = page_offset(first_page) + bv->bv_offset; if (!is_data_inode(inode)) btrfs_submit_metadata_bio(inode, bio, mirror_num); @@ -2836,9 +2841,19 @@ static void end_bio_extent_writepage(struct bio *bio) ASSERT(!bio_flagged(bio, BIO_CLONED)); bio_for_each_segment_all(bvec, bio, iter_all) { struct page *page = bvec->bv_page; - struct inode *inode = page->mapping->host; - struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); - const u32 sectorsize = fs_info->sectorsize; + struct inode *inode; + struct btrfs_fs_info *fs_info; + u32 sectorsize; + struct page *bounce_page = NULL; + + if (fscrypt_is_bounce_page(page)) { + bounce_page = page; + page = fscrypt_pagecache_page(bounce_page); + } + + inode = page->mapping->host; + fs_info = btrfs_sb(inode->i_sb); + sectorsize = fs_info->sectorsize; /* Our read/write should always be sector aligned. */ if (!IS_ALIGNED(bvec->bv_offset, sectorsize)) @@ -2859,7 +2874,7 @@ static void end_bio_extent_writepage(struct bio *bio) } end_extent_writepage(page, error, start, end); - + fscrypt_free_bounce_page(bounce_page); btrfs_page_clear_writeback(fs_info, page, start, bvec->bv_len); } @@ -3058,6 +3073,17 @@ static void end_bio_extent_readpage(struct bio *bio) } } + if (likely(uptodate)) { + if (fscrypt_inode_uses_fs_layer_crypto(inode)) { + int ret = fscrypt_decrypt_pagecache_blocks(page, + bvec->bv_len, + bvec->bv_offset); + if (ret) { + error_bitmap = (unsigned int) -1; + uptodate = false; + } + } + } if (likely(uptodate)) { loff_t i_size = i_size_read(inode); pgoff_t end_index = i_size >> PAGE_SHIFT; @@ -3415,11 +3441,29 @@ static int submit_extent_page(unsigned int opf, bool force_bio_submit) { int ret = 0; + struct page *bounce_page = NULL; struct btrfs_inode *inode = BTRFS_I(page->mapping->host); unsigned int cur = pg_offset; ASSERT(bio_ctrl); + if ((opf & REQ_OP_MASK) == REQ_OP_WRITE && + fscrypt_inode_uses_fs_layer_crypto(&inode->vfs_inode)) { + gfp_t gfp_flags = GFP_NOFS; + + if (bio_ctrl->bio) + gfp_flags = GFP_NOWAIT | __GFP_NOWARN; + else + gfp_flags = GFP_NOFS; + bounce_page = fscrypt_encrypt_pagecache_blocks(page, size, + pg_offset, + gfp_flags); + if (IS_ERR(bounce_page)) + return PTR_ERR(bounce_page); + page = bounce_page; + pg_offset = 0; + } + ASSERT(pg_offset < PAGE_SIZE && size <= PAGE_SIZE && pg_offset + size <= PAGE_SIZE); if (force_bio_submit) diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c index 066d59707408..c3780eacdd35 100644 --- a/fs/btrfs/file-item.c +++ b/fs/btrfs/file-item.c @@ -663,8 +663,13 @@ blk_status_t btrfs_csum_one_bio(struct btrfs_inode *inode, struct bio *bio, shash->tfm = fs_info->csum_shash; bio_for_each_segment(bvec, bio, iter) { - if (use_page_offsets) - offset = page_offset(bvec.bv_page) + bvec.bv_offset; + if (use_page_offsets) { + struct page *page = bvec.bv_page; + + if (fscrypt_is_bounce_page(page)) + page = fscrypt_pagecache_page(page); + offset = page_offset(page) + bvec.bv_offset; + } if (!ordered) { ordered = btrfs_lookup_ordered_extent(inode, offset); diff --git a/fs/btrfs/fscrypt.c b/fs/btrfs/fscrypt.c index a11bf78c2a33..d4fba4c581c7 100644 --- a/fs/btrfs/fscrypt.c +++ b/fs/btrfs/fscrypt.c @@ -190,11 +190,28 @@ static bool btrfs_fscrypt_empty_dir(struct inode *inode) static void btrfs_fscrypt_get_iv(u8 *iv, int ivsize, struct inode *inode, u64 lblk_num) { - /* - * For encryption that doesn't involve extent data, juse use the - * nonce already loaded into the iv buffer. - */ - return; + u64 offset = lblk_num << inode->i_blkbits; + struct extent_map *em; + + if (lblk_num == 0) { + /* Must be a filename or a symlink. Just use the nonce. */ + return; + } + + /* Since IO must be in progress on this extent, this must succeed */ + em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, offset, PAGE_SIZE); + ASSERT(!IS_ERR(em) && em); + if (em) { + __le64 *iv_64 = (__le64 *)iv; + memcpy(iv, em->iv, ivsize); + /* + * Add the lblk_num to the low bits of the IV to ensure + * the IV changes for every page + */ + *iv_64 = cpu_to_le64(le64_to_cpu(*iv_64) + lblk_num); + free_extent_map(em); + return; + } } const struct fscrypt_operations btrfs_fscrypt_ops = { diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c index 458877442ce5..6908dcb5d737 100644 --- a/fs/btrfs/tree-checker.c +++ b/fs/btrfs/tree-checker.c @@ -273,9 +273,14 @@ static int check_extent_data_item(struct extent_buffer *leaf, return -EUCLEAN; } - /* Compressed inline extent has no on-disk size, skip it */ - if (btrfs_file_extent_compression(leaf, fi) != - BTRFS_COMPRESS_NONE) + /* + * Compressed inline extent has no on-disk size; encrypted has + * variable size; skip them + */ + if ((btrfs_file_extent_compression(leaf, fi) != + BTRFS_COMPRESS_NONE) || + (btrfs_file_extent_encryption(leaf, fi) != + BTRFS_ENCRYPTION_NONE)) return 0; /* Uncompressed inline extent size must match item size */ -- 2.35.1