Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp640998img; Fri, 22 Mar 2019 05:44:17 -0700 (PDT) X-Google-Smtp-Source: APXvYqyyNfjJeOcAmpJvyrflZn2OHJKGaA+OkgTA19/HxlN6a/0aoyKhSOkSdNmW4b+8Qru7+XGE X-Received: by 2002:a63:c34a:: with SMTP id e10mr8722582pgd.194.1553258657324; Fri, 22 Mar 2019 05:44:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553258657; cv=none; d=google.com; s=arc-20160816; b=uLte1cTn8f+dVZYTJfgiRrjdkOiFYuqweqnTuMMj7z19gewEoEM4vbcz5e6VP9Ky1J x+PwHwQ7f+0+pNiGfWHZVdYGrdc31ARWUMS8X2GRS4kBbinOuJ7E5lKMWhMaJcJosm33 8g+tPPPm5ULQqFokWb+5KeMnHLLPnCoQtnjgrjYJkzHhXcjDEXmZ+MnGno4Nw3ujc4ts iGJc3vWhilRjd00P8ZNmKW4K+yGdAO4gZBvCuBzOq3lD5coon+KsmbeB2mdtSpSi0BQu usRIpUyrn7htNpVUqq2R1zvj4CUN0KCchIrZili3a/RfL8Cmd74rXbz1RcC5EBJs9odA cUlw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=oKnKAm7RU9MzV+v82WmMeeSnNc3M4f9dITa6Tcke5Hs=; b=OX0nZkVBaN6C365k7C+wOgiwm0unfCfDVaaaqIS2Kz4hZfSQHlVszLj48vp8NESsrO rNUlLfMV2MEsV9ABSyufeCevTb8yNDdhJICGhHLKz+tPPKSQZpUc5BHNt/8oj8MjU/4y gC0V7vacKTKWJSZA2dUZuAt++FI40uAkA61TXA2t/UTgbHdtYD7y8lEAEzoH1Q2E2kNs d25VMxagdAzLDDwxrFBhnVrE5JC+Zzx7uPzWn812/F6OeEW7OVaIefhWqM/YhdL7kMB9 rzJVyWR+iY1rQHG04j+UET7GcrUo4JuBmtYM+yurwANHGeKQIqYzI8rXg0ACOEbjE/Eg UnUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Qp5g176d; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n8si6454583pgq.119.2019.03.22.05.44.02; Fri, 22 Mar 2019 05:44:17 -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=@kernel.org header.s=default header.b=Qp5g176d; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388452AbfCVMDD (ORCPT + 99 others); Fri, 22 Mar 2019 08:03:03 -0400 Received: from mail.kernel.org ([198.145.29.99]:41126 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388117AbfCVMCx (ORCPT ); Fri, 22 Mar 2019 08:02:53 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2E740204FD; Fri, 22 Mar 2019 12:02:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553256171; bh=Smy/IecndsygcsHXX3JxixQW0zEoBex/87Gu0amPEVI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Qp5g176d9u2/ceRTou8V70/FT3+sr2W++PVj42JkzHHV3tJMa1n2rfNazBjDZ26pR swu+g7xFw4dT/iKfIMdXSqX8XxcL7AS8f+CesOythRQP0CEo7D8w1kiMHZCmjj1+QU 32DNykY9Xc5Lb5T9TvA3PavubCKSJrgB44wiIOLc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Biggers , Herbert Xu Subject: [PATCH 4.19 130/280] crypto: skcipher - set CRYPTO_TFM_NEED_KEY if ->setkey() fails Date: Fri, 22 Mar 2019 12:14:43 +0100 Message-Id: <20190322111316.531680044@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190322111306.356185024@linuxfoundation.org> References: <20190322111306.356185024@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Eric Biggers commit b1f6b4bf416b49f00f3abc49c639371cdecaaad1 upstream. Some algorithms have a ->setkey() method that is not atomic, in the sense that setting a key can fail after changes were already made to the tfm context. In this case, if a key was already set the tfm can end up in a state that corresponds to neither the old key nor the new key. For example, in lrw.c, if gf128mul_init_64k_bbe() fails due to lack of memory, then priv::table will be left NULL. After that, encryption with that tfm will cause a NULL pointer dereference. It's not feasible to make all ->setkey() methods atomic, especially ones that have to key multiple sub-tfms. Therefore, make the crypto API set CRYPTO_TFM_NEED_KEY if ->setkey() fails and the algorithm requires a key, to prevent the tfm from being used until a new key is set. [Cc stable mainly because when introducing the NEED_KEY flag I changed AF_ALG to rely on it; and unlike in-kernel crypto API users, AF_ALG previously didn't have this problem. So these "incompletely keyed" states became theoretically accessible via AF_ALG -- though, the opportunities for causing real mischief seem pretty limited.] Fixes: f8d33fac8480 ("crypto: skcipher - prevent using skciphers without setting key") Cc: # v4.16+ Signed-off-by: Eric Biggers Signed-off-by: Herbert Xu Signed-off-by: Greg Kroah-Hartman --- crypto/skcipher.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) --- a/crypto/skcipher.c +++ b/crypto/skcipher.c @@ -584,6 +584,12 @@ static unsigned int crypto_skcipher_exts return crypto_alg_extsize(alg); } +static void skcipher_set_needkey(struct crypto_skcipher *tfm) +{ + if (tfm->keysize) + crypto_skcipher_set_flags(tfm, CRYPTO_TFM_NEED_KEY); +} + static int skcipher_setkey_blkcipher(struct crypto_skcipher *tfm, const u8 *key, unsigned int keylen) { @@ -597,8 +603,10 @@ static int skcipher_setkey_blkcipher(str err = crypto_blkcipher_setkey(blkcipher, key, keylen); crypto_skcipher_set_flags(tfm, crypto_blkcipher_get_flags(blkcipher) & CRYPTO_TFM_RES_MASK); - if (err) + if (unlikely(err)) { + skcipher_set_needkey(tfm); return err; + } crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY); return 0; @@ -676,8 +684,7 @@ static int crypto_init_skcipher_ops_blkc skcipher->ivsize = crypto_blkcipher_ivsize(blkcipher); skcipher->keysize = calg->cra_blkcipher.max_keysize; - if (skcipher->keysize) - crypto_skcipher_set_flags(skcipher, CRYPTO_TFM_NEED_KEY); + skcipher_set_needkey(skcipher); return 0; } @@ -697,8 +704,10 @@ static int skcipher_setkey_ablkcipher(st crypto_skcipher_set_flags(tfm, crypto_ablkcipher_get_flags(ablkcipher) & CRYPTO_TFM_RES_MASK); - if (err) + if (unlikely(err)) { + skcipher_set_needkey(tfm); return err; + } crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY); return 0; @@ -775,8 +784,7 @@ static int crypto_init_skcipher_ops_ablk sizeof(struct ablkcipher_request); skcipher->keysize = calg->cra_ablkcipher.max_keysize; - if (skcipher->keysize) - crypto_skcipher_set_flags(skcipher, CRYPTO_TFM_NEED_KEY); + skcipher_set_needkey(skcipher); return 0; } @@ -819,8 +827,10 @@ static int skcipher_setkey(struct crypto else err = cipher->setkey(tfm, key, keylen); - if (err) + if (unlikely(err)) { + skcipher_set_needkey(tfm); return err; + } crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY); return 0; @@ -852,8 +862,7 @@ static int crypto_skcipher_init_tfm(stru skcipher->ivsize = alg->ivsize; skcipher->keysize = alg->max_keysize; - if (skcipher->keysize) - crypto_skcipher_set_flags(skcipher, CRYPTO_TFM_NEED_KEY); + skcipher_set_needkey(skcipher); if (alg->exit) skcipher->base.exit = crypto_skcipher_exit_tfm;