Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3195424imu; Fri, 23 Nov 2018 23:50:34 -0800 (PST) X-Google-Smtp-Source: AFSGD/U+PZN3F7wo0VGBIVTL0e1qCBSUoyD703fO6R5m3MiUY8Qt/E/Hazfo3XFeSeevxwYbgVE2 X-Received: by 2002:a63:e40c:: with SMTP id a12mr17226255pgi.28.1543045834685; Fri, 23 Nov 2018 23:50:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543045834; cv=none; d=google.com; s=arc-20160816; b=hqNqHp5dhiCjVr+vzPC/ctYCPu2fZQ0KYz8fluYF1Xt82z+wFNu6/5jLMjfyFMPIc/ ao49AAHkOEbPxHxYaUwCbe+eP7nMJ8vYixdQYtSZgTPzuep8Cf6WWHHQ2VnJWXfyRJL7 iIuSQAX0dsCf9q/UyklFdCWJeXX3LbiqeW4pIbv74Xo5B/4Y+vlhLQEweGRrCx9bg2X6 dpNVn4m1/07Ze/gSW2BQ6yv89Ctdw16SQTBZxaAWjkv/9CNn77v94PFQ6QG85tvwS4tt APVAbbJc+jTI+UvCMu8c7+zcHbrXbRKb02klf4ShAlcZw8nlEdmAm+EEcwd0DE8WAbF6 SCJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=FVnCKiRPOtTHhR3R2VXIEQAgOV0yCduGKgfYTIdPMGA=; b=Md3IklVHzh77sLIKf38RGdHy1KdQiPAWigu8MmcsYBjn4PjtYspCxQYbNmALUplBKU x6bQOwvQgdlJMyyO/UFehxxxsC9utu+ukiPqoaPouab2PckZSM5xB0+TAvZNKWzs6/kT OMg2sOXrEEhS7zNMOcy3lSdwTkYcN6w2Q8i/Y3eQSJueElG3ICTHdzxr077ae8uwwixx DNX3RQb0ZNdJhjolyu48kOEFxHiw7iAMAkgRceycjZnho7IHF5mt4mMvVRMyjYoLZsxd iiiePAip/npbZdBPAecZqVaOJpOb5JCKM5MXjLZ8m01O0pSbgtZjcEaSGHWAuW+Nhl37 /kaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=A5mJscS6; 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 az5si35675872plb.166.2018.11.23.23.50.20; Fri, 23 Nov 2018 23:50:34 -0800 (PST) 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=@163.com header.s=s110527 header.b=A5mJscS6; 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 S2392800AbeKWLsN (ORCPT + 99 others); Fri, 23 Nov 2018 06:48:13 -0500 Received: from m12-12.163.com ([220.181.12.12]:33205 "EHLO m12-12.163.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726954AbeKWLsM (ORCPT ); Fri, 23 Nov 2018 06:48:12 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=Date:From:Subject:Message-ID:MIME-Version; bh=FVnCK iRPOtTHhR3R2VXIEQAgOV0yCduGKgfYTIdPMGA=; b=A5mJscS6TPbcS6rQG4clZ Pgrub6Be22qZpgoFsaTlMSLL2L8IyqNt1aAP26WE4rMh0rne4s1uFUTWFAwTRLKh TqbzoBQCEb8LpC3Ex083OnpDmzZBNPo8hCdS+esSc7ylFaXyqhQlkszeWKbuhVuB yZQOAy3Q7PgK7RG+vSb9u4= Received: from bp (unknown [106.120.213.96]) by smtp8 (Coremail) with SMTP id DMCowABX5nJwUvdbvHbqBQ--.22330S2; Fri, 23 Nov 2018 09:05:53 +0800 (CST) Date: Fri, 23 Nov 2018 09:05:55 +0800 From: PanBian To: Herbert Xu Cc: "David S. Miller" , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] crypto: do not free algorithm before using Message-ID: <20181123010555.GA83154@bp> Reply-To: PanBian References: <1542880816-63838-1-git-send-email-bianpan2016@163.com> <20181122144441.tkfmrq3lzibq2g3y@gondor.apana.org.au> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20181122144441.tkfmrq3lzibq2g3y@gondor.apana.org.au> User-Agent: Mutt/1.5.24 (2015-08-30) X-CM-TRANSID: DMCowABX5nJwUvdbvHbqBQ--.22330S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7AF15Cr4UAryrAw1rurW5Awb_yoW8Cr4fpr WrJFW8JFWktrZ0kFZ29a1rXry8W3yI9r15GrWUCryIvwnxXr1kJr9Fyr47XF42yF1kJryr JrZ7Wr97Z3WjkaDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jNvtAUUUUU= X-Originating-IP: [106.120.213.96] X-CM-SenderInfo: held01tdqsiiqw6rljoofrz/xtbBzxEIclaD0YLuDwAAsD Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Nov 22, 2018 at 10:44:41PM +0800, Herbert Xu wrote: > On Thu, Nov 22, 2018 at 06:00:16PM +0800, Pan Bian wrote: > > In multiple functions, the algorithm fields are read after its reference > > is dropped through crypto_mod_put. In this case, the algorithm memory > > may be freed, resulting in use-after-free bugs. This patch delays the > > put operation until the algorithm is never used. > > > > Signed-off-by: Pan Bian > > I don't think this patch is needed. > > > --- > > crypto/cbc.c | 6 ++++-- > > crypto/cfb.c | 6 ++++-- > > crypto/pcbc.c | 6 ++++-- > > 3 files changed, 12 insertions(+), 6 deletions(-) > > > > diff --git a/crypto/cbc.c b/crypto/cbc.c > > index b761b1f..dd5f332 100644 > > --- a/crypto/cbc.c > > +++ b/crypto/cbc.c > > @@ -140,9 +140,8 @@ static int crypto_cbc_create(struct crypto_template *tmpl, struct rtattr **tb) > > spawn = skcipher_instance_ctx(inst); > > err = crypto_init_spawn(spawn, alg, skcipher_crypto_instance(inst), > > CRYPTO_ALG_TYPE_MASK); > > - crypto_mod_put(alg); > > if (err) > > - goto err_free_inst; > > + goto err_put_alg; > > We can safely drop the reference to the algorithm because the spawn > is now meant to hold a reference to it. As long as the spawn is > alive so will the algorithm. Thanks for your explanation! But I find that the function crypto_init_spawn just lets spawn->alg point to the algorithm without increasing the reference count, i.e., alg->cra_refcnt. So I am confused about how this can protect the algorithm from being freed. Maybe I missed some key points. Could you please explain it in more details? Thank you! Best regards, Pan Bian > > Cheers, > -- > Email: Herbert Xu > Home Page: http://gondor.apana.org.au/~herbert/ > PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt