Received: by 10.223.185.116 with SMTP id b49csp6347777wrg; Wed, 28 Feb 2018 07:59:41 -0800 (PST) X-Google-Smtp-Source: AH8x225wge4ufMaRPaxPkFImnTjxFKDdoRnwvp5RokeHE1xv/weaOkki6eWPtUw9rPpjZ6wswAs/ X-Received: by 10.98.211.1 with SMTP id q1mr18271582pfg.199.1519833581778; Wed, 28 Feb 2018 07:59:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519833581; cv=none; d=google.com; s=arc-20160816; b=bT1fr7MFwQy4uAlrZeIOGGmR/53R8SYTp7MAfWKvuj+XPwkdvQmgudwNwTPnuzTbnm VzGw6WeT/qhw+4jRsrQBmKh3ByRfp1oZpvXgZW1g+896iXHt0Hk92hQ4ZK2ijD4TN8B6 +tVHBTk4oR2uGXobMVhCfjvHnoW9E2fUXP9vgz9ji1B747cv11rItOnrDVnrkwHUsDtD UbE1WGOqd77PNCtShJfaX91ST180pecWbnJZf6T3BpLWW90mvCsJ3HfDl2bwjW91z4Qi sEUxtfX8zaMkFTting7CkQD2vbSA1puGlnGhNt6AfXPFE7QaXY22dwDiOr+Qn4Zp/p93 xAew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition :arc-authentication-results; bh=tJuOYHWoAlkH2pfoWqvPzTUXb/6em9vyz0rqJFnTFVk=; b=bGB69UqUYAGB+4Z8pXu26InrjXHkJS2ag5RcOTGGBbKmIj88IyhwjxaGcv4QzMwYA0 JikIX4jVhv+mNRctpQF/+rNZUiD80rfOUik2Z2A+wHe6kDnQG9gBdYbeiSaof1SyXC6U k+l+OH0l2k3iqQYH1ODU5/uZ95fkmrCVkKXhtWohigT3oEVfdxXZ+L6w5+OxBTF5IuQt VRNH2Ug68ywGW1VBl8ahlzrOGaNjmY0rhM1jY1HFI3xyV94rPlO8mpPFUY72/4cthpLg gg+r4FkLutSI/OqVToz2Ge54xHKUMmkByBSntM46R8QpFuxk7oaC2oYQK+vwWxXC8OQU YaFA== ARC-Authentication-Results: i=1; mx.google.com; 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 f1-v6si1450083plb.73.2018.02.28.07.59.27; Wed, 28 Feb 2018 07:59:41 -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; 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 S934108AbeB1P6X (ORCPT + 99 others); Wed, 28 Feb 2018 10:58:23 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:34554 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753002AbeB1P6T (ORCPT ); Wed, 28 Feb 2018 10:58:19 -0500 Received: from [2a02:8011:400e:2:6f00:88c8:c921:d332] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1er3Yj-0006Xe-L7; Wed, 28 Feb 2018 15:22:22 +0000 Received: from ben by deadeye with local (Exim 4.90_1) (envelope-from ) id 1er3Yi-0000C7-Jb; Wed, 28 Feb 2018 15:22:20 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "syzbot" , "Herbert Xu" , "Eric Biggers" Date: Wed, 28 Feb 2018 15:20:18 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 171/254] crypto: algapi - fix NULL dereference in crypto_remove_spawns() In-Reply-To: X-SA-Exim-Connect-IP: 2a02:8011:400e:2:6f00:88c8:c921:d332 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.55-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Eric Biggers commit 9a00674213a3f00394f4e3221b88f2d21fc05789 upstream. syzkaller triggered a NULL pointer dereference in crypto_remove_spawns() via a program that repeatedly and concurrently requests AEADs "authenc(cmac(des3_ede-asm),pcbc-aes-aesni)" and hashes "cmac(des3_ede)" through AF_ALG, where the hashes are requested as "untested" (CRYPTO_ALG_TESTED is set in ->salg_mask but clear in ->salg_feat; this causes the template to be instantiated for every request). Although AF_ALG users really shouldn't be able to request an "untested" algorithm, the NULL pointer dereference is actually caused by a longstanding race condition where crypto_remove_spawns() can encounter an instance which has had spawn(s) "grabbed" but hasn't yet been registered, resulting in ->cra_users still being NULL. We probably should properly initialize ->cra_users earlier, but that would require updating many templates individually. For now just fix the bug in a simple way that can easily be backported: make crypto_remove_spawns() treat a NULL ->cra_users list as empty. Reported-by: syzbot Signed-off-by: Eric Biggers Signed-off-by: Herbert Xu Signed-off-by: Ben Hutchings --- crypto/algapi.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) --- a/crypto/algapi.c +++ b/crypto/algapi.c @@ -147,6 +147,18 @@ void crypto_remove_spawns(struct crypto_ spawn->alg = NULL; spawns = &inst->alg.cra_users; + + /* + * We may encounter an unregistered instance here, since + * an instance's spawns are set up prior to the instance + * being registered. An unregistered instance will have + * NULL ->cra_users.next, since ->cra_users isn't + * properly initialized until registration. But an + * unregistered instance cannot have any users, so treat + * it the same as ->cra_users being empty. + */ + if (spawns->next == NULL) + break; } } while ((spawns = crypto_more_spawns(alg, &stack, &top, &secondary_spawns)));