Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp6591708yba; Wed, 1 May 2019 15:47:12 -0700 (PDT) X-Google-Smtp-Source: APXvYqzktv2fDdI8CjW5FQcbaQs8fmkx+4TWxwzSxc+I9RQ9mGMMAOdpwJc1GznnFYGgfUJfETk8 X-Received: by 2002:a62:125c:: with SMTP id a89mr474629pfj.93.1556750831988; Wed, 01 May 2019 15:47:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556750831; cv=none; d=google.com; s=arc-20160816; b=HwfQeTcelP0AqqZ8XsqmCxci2B43NO+w5oCJ9q+pHuTNnsrGSJ11xG3I0+Gs92ROzy Z1LgpkeyPAThVURjVox4EtQR4QmFFyESEJqK++fW5n2uvKfnPLslDxkf80W7sbusW4r5 CrTJD6ahc2jQv6dDQkCJYgRS4dv5WvcBSMUDiwx7ntV10iNxTXcI4zsywpa7JsHez+7h 3R3C+XovlnsKGFcu4fazhqLXbisiYhLa4dJa4nb2Iejly944WB33HuvprVzZewno6a2Z rM4PBqcnbCST7tjVynX1WpD0i75sPTuwW7opKyiIkeiPHemnQ7fsbGRECMMlveV8YCWZ wkeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=0Ua8yEx4J1MHDUUhLX15IpMY7YKZoGeM7Ti6Q1var/s=; b=C3Y4MoLSikkOm/3z/8QGdTdFAwjo845qUkuC1rUYSkNH7LgmJGbVgiw5OREQhFxjZX HXYZKTwQIHJz+oC7v4+pn5zGuDWKA7wo/YgO3yepcjthqArPLNWMkiejQIKzBKaFgiP5 1wpyNBiPAosAuph/RjiVnoutRz2vX46Y8bsszGsIldUEGQoElEOcE18Wcnh2vxOBlIFJ nuOeaGHnklD8OXQ4i6dvSOFoWGDrzY+YvVsNcdNMC5hGwPPY/WBrj3+CjP0eLijxV9th 7ZNzlwcHJ+zeBN1Itih0pNhujjQWDiXDXevMMk9H45W57Yvna50oSO2J5qtuok/lQVAt CKXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=bqxdbS5n; spf=pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g191si27847313pgc.12.2019.05.01.15.46.57; Wed, 01 May 2019 15:47:11 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-ext4-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=@kernel.org header.s=default header.b=bqxdbS5n; spf=pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726321AbfEAWqH (ORCPT + 99 others); Wed, 1 May 2019 18:46:07 -0400 Received: from mail.kernel.org ([198.145.29.99]:47212 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726126AbfEAWqG (ORCPT ); Wed, 1 May 2019 18:46:06 -0400 Received: from ebiggers-linuxstation.mtv.corp.google.com (unknown [104.132.1.77]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 1B5A1208E4; Wed, 1 May 2019 22:46:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1556750766; bh=+RQ61D5kWmWko+0G8uvpI5+1qaC74xgMofSqQ5OY21Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bqxdbS5n/64wpTomiYr8G1Mx4WqHQOmGjZmn7Gmzh/TrhJ6VjAlo0K4m80kLlWo63 EUl2omT42JJf/J61mscI5nkC46Ekpw4lxwfznm/5qu0j9xV6sR4LaKEEBAtI2VUga0 vZxH1KDYMQT/uYjNx/5wG1RcrC4kLMNdHOIXUiFk= From: Eric Biggers To: linux-fscrypt@vger.kernel.org Cc: linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-mtd@lists.infradead.org, linux-fsdevel@vger.kernel.org, Chandan Rajendra Subject: [PATCH 04/13] fscrypt: clean up some BUG_ON()s in block encryption/decryption Date: Wed, 1 May 2019 15:45:06 -0700 Message-Id: <20190501224515.43059-5-ebiggers@kernel.org> X-Mailer: git-send-email 2.21.0.593.g511ec345e18-goog In-Reply-To: <20190501224515.43059-1-ebiggers@kernel.org> References: <20190501224515.43059-1-ebiggers@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org From: Eric Biggers Replace some BUG_ON()s with WARN_ON_ONCE() and returning an error code, and move the check for len divisible by FS_CRYPTO_BLOCK_SIZE into fscrypt_crypt_block() so that it's done for both encryption and decryption, not just encryption. Signed-off-by: Eric Biggers --- fs/crypto/crypto.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/fs/crypto/crypto.c b/fs/crypto/crypto.c index e6802d7aca3c7..9cda0147fca95 100644 --- a/fs/crypto/crypto.c +++ b/fs/crypto/crypto.c @@ -161,7 +161,10 @@ int fscrypt_crypt_block(const struct inode *inode, fscrypt_direction_t rw, struct crypto_skcipher *tfm = ci->ci_ctfm; int res = 0; - BUG_ON(len == 0); + if (WARN_ON_ONCE(len <= 0)) + return -EINVAL; + if (WARN_ON_ONCE(len % FS_CRYPTO_BLOCK_SIZE != 0)) + return -EINVAL; fscrypt_generate_iv(&iv, lblk_num, ci); @@ -224,8 +227,6 @@ struct page *fscrypt_encrypt_page(const struct inode *inode, struct page *ciphertext_page = page; int err; - BUG_ON(len % FS_CRYPTO_BLOCK_SIZE != 0); - if (inode->i_sb->s_cop->flags & FS_CFLG_OWN_PAGES) { /* with inplace-encryption we just encrypt the page */ err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk_num, page, @@ -237,7 +238,8 @@ struct page *fscrypt_encrypt_page(const struct inode *inode, return ciphertext_page; } - BUG_ON(!PageLocked(page)); + if (WARN_ON_ONCE(!PageLocked(page))) + return ERR_PTR(-EINVAL); /* The encryption operation will require a bounce page. */ ciphertext_page = fscrypt_alloc_bounce_page(gfp_flags); @@ -274,8 +276,9 @@ EXPORT_SYMBOL(fscrypt_encrypt_page); int fscrypt_decrypt_page(const struct inode *inode, struct page *page, unsigned int len, unsigned int offs, u64 lblk_num) { - if (!(inode->i_sb->s_cop->flags & FS_CFLG_OWN_PAGES)) - BUG_ON(!PageLocked(page)); + if (WARN_ON_ONCE(!PageLocked(page) && + !(inode->i_sb->s_cop->flags & FS_CFLG_OWN_PAGES))) + return -EINVAL; return fscrypt_crypt_block(inode, FS_DECRYPT, lblk_num, page, page, len, offs, GFP_NOFS); -- 2.21.0.593.g511ec345e18-goog