Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1121353ybt; Tue, 7 Jul 2020 08:14:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy1MhTnqnNO0RqFr6quQYFfb96sZN6BpgXEhtUOQpS0PPo4mEQCbsRMNtW/I3nttcBXBSmT X-Received: by 2002:a50:8f83:: with SMTP id y3mr19729427edy.257.1594134894002; Tue, 07 Jul 2020 08:14:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594134893; cv=none; d=google.com; s=arc-20160816; b=bAQxawNconsE0MSU7FIna1d+fX5O5penaQWuniYz025SDlZEVvcpT34b9t2tQ9upd+ Cw1R51arLZJFCPJCkw2RSwA9HGunM1dpDnPZi8m4RgsfIdRKzVoubQY2L0ocLGBKxxHb xTCw8YR9nkm+TFc97H4N7nt63tH1l0bjUD4Hvw813AAgSijO/VEepBtL1N+fUVEKv1C6 VnBcllTdp7QvpTwbDtzcT6F12bxxkVo7CrVV/EjKFkT6ERKZe0+nSGvLJolHOPYfkBI4 sgEzPcgB99r1TkwFudI8vYy5W1P9QRI4KNDPB5lZPgHXcamkR/w3cuF4huIW+5i5ey+g iEUg== 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=VfAxEzCJMITLT2Sp+IYkTs0K+d/9iZ2+Qrm4/vDa7+Y=; b=qISqS6nmDJRy5vu2wx6YbkYOUUoblEQr3aBlo1Uldf3ulGWdR/Do7qIuy+xYdCtztU fpOgT+nOuGwp9dRHPrfzuKeJ5g5ymHnZkSLCFalmGvrCxvRLLLY7/wlPZmPQUA9fKdoF 9Hw22Doh4/BMHfkG27Q7TcZdWri0dF+HKejaJ9SQXjYWvo5T53/UjUlCnrGrob/MIwsn 6fzeUDy8mPBIFTG2UQGeyBHqU+PgBcDiALdImSXpPA5XfiW6vh8gjqdW+tJkdhXRBCMW zM3jaygmh72/rPTt1MScUrX3rZbMdiW7uJkfbSxfrqwkjsZMd+L17FkFBxlegudIlTY6 /tIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=mtmnqYK3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g19si16553737edv.174.2020.07.07.08.14.30; Tue, 07 Jul 2020 08:14:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=mtmnqYK3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728421AbgGGPOK (ORCPT + 99 others); Tue, 7 Jul 2020 11:14:10 -0400 Received: from mail.kernel.org ([198.145.29.99]:53330 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728243AbgGGPOK (ORCPT ); Tue, 7 Jul 2020 11:14:10 -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 A043820674; Tue, 7 Jul 2020 15:14:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1594134849; bh=lYEr1dtF+6HbgexYBM0gBCUjUJD67Z+A66WNai15G1g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mtmnqYK36dggXbVHnBjVxsqrrAj8G77ulPXW9mMqIRISpytaAYYZB0QdlK6sx2wZ5 W8ZWnM5dzYSf5190SJCJMpgW6qJnqfqNyUQtPYst+yfJIglTgAhF/9H7XLbNI64tll Wfld8FAVOjs6KVV8KhOC91bmS8rl/wX+CAYTEhNg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Brian Moyles , Mauricio Faria de Oliveira , Herbert Xu Subject: [PATCH 4.9 11/24] crypto: af_alg - fix use-after-free in af_alg_accept() due to bh_lock_sock() Date: Tue, 7 Jul 2020 17:13:43 +0200 Message-Id: <20200707145749.517115346@linuxfoundation.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200707145748.952502272@linuxfoundation.org> References: <20200707145748.952502272@linuxfoundation.org> User-Agent: quilt/0.66 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 From: Herbert Xu commit 34c86f4c4a7be3b3e35aa48bd18299d4c756064d upstream. The locking in af_alg_release_parent is broken as the BH socket lock can only be taken if there is a code-path to handle the case where the lock is owned by process-context. Instead of adding such handling, we can fix this by changing the ref counts to atomic_t. This patch also modifies the main refcnt to include both normal and nokey sockets. This way we don't have to fudge the nokey ref count when a socket changes from nokey to normal. Credits go to Mauricio Faria de Oliveira who diagnosed this bug and sent a patch for it: https://lore.kernel.org/linux-crypto/20200605161657.535043-1-mfo@canonical.com/ Reported-by: Brian Moyles Reported-by: Mauricio Faria de Oliveira Fixes: 37f96694cf73 ("crypto: af_alg - Use bh_lock_sock in...") Cc: Signed-off-by: Herbert Xu Signed-off-by: Greg Kroah-Hartman --- crypto/af_alg.c | 26 +++++++++++--------------- crypto/algif_aead.c | 9 +++------ crypto/algif_hash.c | 9 +++------ crypto/algif_skcipher.c | 9 +++------ include/crypto/if_alg.h | 4 ++-- 5 files changed, 22 insertions(+), 35 deletions(-) --- a/crypto/af_alg.c +++ b/crypto/af_alg.c @@ -130,21 +130,15 @@ EXPORT_SYMBOL_GPL(af_alg_release); void af_alg_release_parent(struct sock *sk) { struct alg_sock *ask = alg_sk(sk); - unsigned int nokey = ask->nokey_refcnt; - bool last = nokey && !ask->refcnt; + unsigned int nokey = atomic_read(&ask->nokey_refcnt); sk = ask->parent; ask = alg_sk(sk); - local_bh_disable(); - bh_lock_sock(sk); - ask->nokey_refcnt -= nokey; - if (!last) - last = !--ask->refcnt; - bh_unlock_sock(sk); - local_bh_enable(); + if (nokey) + atomic_dec(&ask->nokey_refcnt); - if (last) + if (atomic_dec_and_test(&ask->refcnt)) sock_put(sk); } EXPORT_SYMBOL_GPL(af_alg_release_parent); @@ -189,7 +183,7 @@ static int alg_bind(struct socket *sock, err = -EBUSY; lock_sock(sk); - if (ask->refcnt | ask->nokey_refcnt) + if (atomic_read(&ask->refcnt)) goto unlock; swap(ask->type, type); @@ -238,7 +232,7 @@ static int alg_setsockopt(struct socket int err = -EBUSY; lock_sock(sk); - if (ask->refcnt) + if (atomic_read(&ask->refcnt) != atomic_read(&ask->nokey_refcnt)) goto unlock; type = ask->type; @@ -305,12 +299,14 @@ int af_alg_accept(struct sock *sk, struc sk2->sk_family = PF_ALG; - if (nokey || !ask->refcnt++) + if (atomic_inc_return_relaxed(&ask->refcnt) == 1) sock_hold(sk); - ask->nokey_refcnt += nokey; + if (nokey) { + atomic_inc(&ask->nokey_refcnt); + atomic_set(&alg_sk(sk2)->nokey_refcnt, 1); + } alg_sk(sk2)->parent = sk; alg_sk(sk2)->type = type; - alg_sk(sk2)->nokey_refcnt = nokey; newsock->ops = type->ops; newsock->state = SS_CONNECTED; --- a/crypto/algif_aead.c +++ b/crypto/algif_aead.c @@ -747,7 +747,7 @@ static int aead_check_key(struct socket struct alg_sock *ask = alg_sk(sk); lock_sock(sk); - if (ask->refcnt) + if (!atomic_read(&ask->nokey_refcnt)) goto unlock_child; psk = ask->parent; @@ -759,11 +759,8 @@ static int aead_check_key(struct socket if (!tfm->has_key) goto unlock; - if (!pask->refcnt++) - sock_hold(psk); - - ask->refcnt = 1; - sock_put(psk); + atomic_dec(&pask->nokey_refcnt); + atomic_set(&ask->nokey_refcnt, 0); err = 0; --- a/crypto/algif_hash.c +++ b/crypto/algif_hash.c @@ -308,7 +308,7 @@ static int hash_check_key(struct socket struct alg_sock *ask = alg_sk(sk); lock_sock(sk); - if (ask->refcnt) + if (!atomic_read(&ask->nokey_refcnt)) goto unlock_child; psk = ask->parent; @@ -320,11 +320,8 @@ static int hash_check_key(struct socket if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) goto unlock; - if (!pask->refcnt++) - sock_hold(psk); - - ask->refcnt = 1; - sock_put(psk); + atomic_dec(&pask->nokey_refcnt); + atomic_set(&ask->nokey_refcnt, 0); err = 0; --- a/crypto/algif_skcipher.c +++ b/crypto/algif_skcipher.c @@ -774,7 +774,7 @@ static int skcipher_check_key(struct soc struct alg_sock *ask = alg_sk(sk); lock_sock(sk); - if (ask->refcnt) + if (!atomic_read(&ask->nokey_refcnt)) goto unlock_child; psk = ask->parent; @@ -786,11 +786,8 @@ static int skcipher_check_key(struct soc if (!tfm->has_key) goto unlock; - if (!pask->refcnt++) - sock_hold(psk); - - ask->refcnt = 1; - sock_put(psk); + atomic_dec(&pask->nokey_refcnt); + atomic_set(&ask->nokey_refcnt, 0); err = 0; --- a/include/crypto/if_alg.h +++ b/include/crypto/if_alg.h @@ -30,8 +30,8 @@ struct alg_sock { struct sock *parent; - unsigned int refcnt; - unsigned int nokey_refcnt; + atomic_t refcnt; + atomic_t nokey_refcnt; const struct af_alg_type *type; void *private;