From patchwork Fri Feb 26 11:44:08 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Milan Broz X-Patchwork-Id: 8436091 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Original-To: patchwork-linux-crypto@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 98DAE9F52D for ; Fri, 26 Feb 2016 11:44:30 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id A42A72037C for ; Fri, 26 Feb 2016 11:44:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A267720397 for ; Fri, 26 Feb 2016 11:44:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751555AbcBZLo0 (ORCPT ); Fri, 26 Feb 2016 06:44:26 -0500 Received: from mail-wm0-f68.google.com ([74.125.82.68]:34731 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751124AbcBZLoZ (ORCPT ); Fri, 26 Feb 2016 06:44:25 -0500 Received: by mail-wm0-f68.google.com with SMTP id b205so8631733wmb.1; Fri, 26 Feb 2016 03:44:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=1mwRIShkhqAswtZwc/dYXq1CSQTY59sVd1iOADRehGU=; b=omd52DpqTayP0gG3pbTrXOON1om3k4NYY1of8p6BZex8IfzjFqATwfNbq/ER4DOhRM 9uYJjTMhEogsHJyZS9DLWVL9MskgMIRDr2jn1ClTQanfRjzMnhr9oFy4x1/8dIyY9/wN H9s0ReF1qJLm7OrJ/6tcVLWNGMeQbwqj6YkoPZqxcl3kJNr99E4T13ZLiK+1jwoMKqRU oyf+9isUuzwzRZ+SRz5FSo2gEPE8MtTifA8oSYeU5Td9uHfsEjhq//FCa6DZr8loacHe vXymnPHO5uqwsmIt5H4jzYdT7o68yfXu9WESkCdcN1GwRFtkDT6HFYwUseg3BAde13Eh OZmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=1mwRIShkhqAswtZwc/dYXq1CSQTY59sVd1iOADRehGU=; b=GYaaQUUyzKIube6oIhKfqJHArnU17xmtR2W0FiRPvJyXygwRbKkA6LldzHJ+JgEH57 YHJdu6m9igLEZrjs4Dac+M7JUElbbEbLLgbU9L0nRM8Yv6a8j5AypHseUrd1mUPW9wRa 19svTsE0eQBiOMNARihdZjyI0D6hIkhEs+h5ujKnCjwd/iWXZcHixyaJE5Dn9SPWyyQd 5sR0UK7bkz8b3ajA8ta1HIqVPI8VViDI1GzxfJQGIC22KH50puUU2rvncQdwhA0pMQj9 7s6pPb9tyUbD/uQ5IqW5vgbA349vuI6ImGVEuwufD7j/kYs3aAHgCsOW1wHjeTVLzqlG 1BLQ== X-Gm-Message-State: AD7BkJJ3CeV9BvVg2CP6NNrNEZ0/dDTFaPoFHjrFCui03loBMERyf0tz/rognSd0m+9ZAA== X-Received: by 10.28.99.136 with SMTP id x130mr2881811wmb.12.1456487063666; Fri, 26 Feb 2016 03:44:23 -0800 (PST) Received: from merlot.mazyland.net (hector.fi.muni.cz. [147.251.42.24]) by smtp.gmail.com with ESMTPSA id j10sm12058768wjb.46.2016.02.26.03.44.23 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 26 Feb 2016 03:44:23 -0800 (PST) From: Milan Broz To: linux-crypto@vger.kernel.org Cc: stable@vger.kernel.org, Herbert Xu Subject: [PATCH 1/4] crypto: algif_skcipher - Require setkey before accept(2) Date: Fri, 26 Feb 2016 12:44:08 +0100 Message-Id: <1456487051-14652-1-git-send-email-gmazyland@gmail.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <56D0361B.8040301@gmail.com> References: <56D0361B.8040301@gmail.com> Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Herbert Xu commit dd504589577d8e8e70f51f997ad487a4cb6c026f upstream. Some cipher implementations will crash if you try to use them without calling setkey first. This patch adds a check so that the accept(2) call will fail with -ENOKEY if setkey hasn't been done on the socket yet. Cc: stable@vger.kernel.org Reported-by: Dmitry Vyukov Signed-off-by: Herbert Xu Tested-by: Dmitry Vyukov [backported to 4.1 by Milan Broz ] Signed-off-by: Milan Broz --- crypto/algif_skcipher.c | 48 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/crypto/algif_skcipher.c b/crypto/algif_skcipher.c index 5bc42f9..1c9879d 100644 --- a/crypto/algif_skcipher.c +++ b/crypto/algif_skcipher.c @@ -31,6 +31,11 @@ struct skcipher_sg_list { struct scatterlist sg[0]; }; +struct skcipher_tfm { + struct crypto_ablkcipher *skcipher; + bool has_key; +}; + struct skcipher_ctx { struct list_head tsgl; struct af_alg_sgl rsgl; @@ -752,17 +757,41 @@ static struct proto_ops algif_skcipher_ops = { static void *skcipher_bind(const char *name, u32 type, u32 mask) { - return crypto_alloc_ablkcipher(name, type, mask); + struct skcipher_tfm *tfm; + struct crypto_ablkcipher *skcipher; + + tfm = kzalloc(sizeof(*tfm), GFP_KERNEL); + if (!tfm) + return ERR_PTR(-ENOMEM); + + skcipher = crypto_alloc_ablkcipher(name, type, mask); + if (IS_ERR(skcipher)) { + kfree(tfm); + return ERR_CAST(skcipher); + } + + tfm->skcipher = skcipher; + + return tfm; } static void skcipher_release(void *private) { - crypto_free_ablkcipher(private); + struct skcipher_tfm *tfm = private; + + crypto_free_ablkcipher(tfm->skcipher); + kfree(tfm); } static int skcipher_setkey(void *private, const u8 *key, unsigned int keylen) { - return crypto_ablkcipher_setkey(private, key, keylen); + struct skcipher_tfm *tfm = private; + int err; + + err = crypto_ablkcipher_setkey(tfm->skcipher, key, keylen); + tfm->has_key = !err; + + return err; } static void skcipher_wait(struct sock *sk) @@ -794,20 +823,25 @@ static int skcipher_accept_parent(void *private, struct sock *sk) { struct skcipher_ctx *ctx; struct alg_sock *ask = alg_sk(sk); - unsigned int len = sizeof(*ctx) + crypto_ablkcipher_reqsize(private); + struct skcipher_tfm *tfm = private; + struct crypto_ablkcipher *skcipher = tfm->skcipher; + unsigned int len = sizeof(*ctx) + crypto_ablkcipher_reqsize(skcipher); + + if (!tfm->has_key) + return -ENOKEY; ctx = sock_kmalloc(sk, len, GFP_KERNEL); if (!ctx) return -ENOMEM; - ctx->iv = sock_kmalloc(sk, crypto_ablkcipher_ivsize(private), + ctx->iv = sock_kmalloc(sk, crypto_ablkcipher_ivsize(skcipher), GFP_KERNEL); if (!ctx->iv) { sock_kfree_s(sk, ctx, len); return -ENOMEM; } - memset(ctx->iv, 0, crypto_ablkcipher_ivsize(private)); + memset(ctx->iv, 0, crypto_ablkcipher_ivsize(skcipher)); INIT_LIST_HEAD(&ctx->tsgl); ctx->len = len; @@ -820,7 +854,7 @@ static int skcipher_accept_parent(void *private, struct sock *sk) ask->private = ctx; - ablkcipher_request_set_tfm(&ctx->req, private); + ablkcipher_request_set_tfm(&ctx->req, skcipher); ablkcipher_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG, af_alg_complete, &ctx->completion);