Received: by 2002:a05:7412:8d08:b0:f9:2d0a:d759 with SMTP id bj8csp109650rdb; Sun, 17 Dec 2023 04:41:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IH87ttZCpgaZhFrhXe0Kixg76iEKZXLdApLvHjTb23OlIZ7o2PLqrDKVcSTPDfWi+UzBvaL X-Received: by 2002:a05:6808:1a1c:b0:3b9:eec3:925b with SMTP id bk28-20020a0568081a1c00b003b9eec3925bmr18338394oib.77.1702816878334; Sun, 17 Dec 2023 04:41:18 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702816878; cv=pass; d=google.com; s=arc-20160816; b=Qgk9DN544lfIhQBEtXqeqHPUJ3WC9qPd8V2c15snohqn931s0gdIlj9KA1feySzdW1 sa3EFDurqr9u4LX+/AOYFhAPHKkpNfHFkRJHfGmHkEvwRMQ1DIFgleIhbj1bSPmXZQ6c UrFDCKqi/kLRe8wzCesEnCGUpTATkP3zsCKK+BNS1CIOW8+fZK8eQoKXPUAjradUaTL0 8srZcHL7BjhUFh5rse2Rqb5ZAIGXDBHrTIckY2iABGkfPS54A1J97TS+cmmrylWjkdxK uBd59cKpQSi9OnIzF9hIABT81DOSrh/oR7m3IVF8auHre9OMrMG2eUdWDFxAjexUyNkN A7XQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language:subject :references:cc:to:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=9HifBpJc5qMXAFdtxZd4YRtuBh2XKnXg7W7+1B+UIHk=; fh=aLN5cCJBmn/kcL95mZ/t142iNLeBPa3jJQ0pDLduHeU=; b=I7KfqCPfcuGfnKOWY2bxQH9uqkHKk7Go6yjVygkRYTMcOCceU6rZ5yf2I2/qXUrG7l eB/F8OlvjLyCJPS/HNxxloqLo+wDiFJsprWzn8pxLfinLVWm1K1od4+TaLhfDZeY9XEP 6Vr0MQ7FIgWpdUBja3jOsbcmW5stchjrVtSl3Oly6vPTIABHHKvb1ldWKeqSxUr+eorY pNMiCO1iYvCL0LxVijHPeJg72T8AtbvNIctCNrHpS1ygAi1lyridJZ+4GSE0QKnIzFBu AfmHCS5B3TCAh0l217qbZ1co86IJnoRVzG66QhssWuKQzD+a6G98Tf3L67uDNLg1si9h Dhww== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@siddh.me header.s=zmail header.b="JP/H+aKz"; arc=pass (i=1 spf=pass spfdomain=siddh.me dkim=pass dkdomain=siddh.me dmarc=pass fromdomain=siddh.me>); spf=pass (google.com: domain of linux-kernel+bounces-2579-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2579-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=siddh.me Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id bq5-20020a056a000e0500b006cb901a87c4si16337540pfb.376.2023.12.17.04.41.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Dec 2023 04:41:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2579-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@siddh.me header.s=zmail header.b="JP/H+aKz"; arc=pass (i=1 spf=pass spfdomain=siddh.me dkim=pass dkdomain=siddh.me dmarc=pass fromdomain=siddh.me>); spf=pass (google.com: domain of linux-kernel+bounces-2579-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2579-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=siddh.me Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id D34CC2831E2 for ; Sun, 17 Dec 2023 12:41:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8958F43AB2; Sun, 17 Dec 2023 12:41:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=siddh.me header.i=code@siddh.me header.b="JP/H+aKz" X-Original-To: linux-kernel@vger.kernel.org Received: from sender-of-o51.zoho.in (sender-of-o51.zoho.in [103.117.158.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 14B8B43147 for ; Sun, 17 Dec 2023 12:41:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=siddh.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=siddh.me ARC-Seal: i=1; a=rsa-sha256; t=1702816849; cv=none; d=zohomail.in; s=zohoarc; b=dfxZeRVksCEl17zXDa/6l/V+twMV+S2HR0wZhv96jMHOd3UFaUOZo7jBW79uo1/4IJv+egbBNfyhHYKr+rrTsBXFKx+WwXKDBbxwq4In0TAatX/tGLAWlyMx44pA2vQXAEH7Cybc+xxijo1mpW3gPrOcjW18hEhqZ+I6RvaaTiA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.in; s=zohoarc; t=1702816849; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=9HifBpJc5qMXAFdtxZd4YRtuBh2XKnXg7W7+1B+UIHk=; b=Cb6p7dJYVSDh29kwk6niFDLCKr1BsajvZNilG9q3xuIr5AyHQmL/y8waRrA3IzkN72FbV22VUsXQEwdS0diNUFliRsDmm65SexQG65wrOf3hkN6JAMM3rsYsoBM1qEQYZaSOSyuPgAkbmpdAqmVAGjub8XePOIr3SLsW3nrn170= ARC-Authentication-Results: i=1; mx.zohomail.in; dkim=pass header.i=siddh.me; spf=pass smtp.mailfrom=code@siddh.me; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1702816849; s=zmail; d=siddh.me; i=code@siddh.me; h=Message-ID:Date:Date:MIME-Version:To:To:Cc:Cc:References:Subject:Subject:From:From:In-Reply-To:Content-Type:Content-Transfer-Encoding:Message-Id:Reply-To; bh=9HifBpJc5qMXAFdtxZd4YRtuBh2XKnXg7W7+1B+UIHk=; b=JP/H+aKz2/vUELaM924vEWuU+GVuPaAmBomNUy8rud6CsH8/IL1W7aKsRBiDOMWw IsxZKEBj3QBB7KUZz/zszFeHzG2pRad7/FduTumiEye9hUdvltKFm7dlPwnWumyGLAB b0EnihznvmOfe3m2BN1WRjPnRSufAh3Rrnwns2co= Received: from [192.168.1.12] (122.170.167.40 [122.170.167.40]) by mx.zoho.in with SMTPS id 1702816847757899.9693718007204; Sun, 17 Dec 2023 18:10:47 +0530 (IST) Message-ID: Date: Sun, 17 Dec 2023 18:10:43 +0530 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: syzbot+bbe84a4010eeea00982d@syzkaller.appspotmail.com Cc: linux-kernel@vger.kernel.org, syzkaller-bugs@googlegroups.com References: <000000000000cb112e0609b419d3@google.com> Subject: Re: [syzbot] [net?] [nfc?] KASAN: slab-use-after-free Read in nfc_alloc_send_skb Content-Language: en-US, en-GB, hi-IN From: Siddh Raman Pant In-Reply-To: <000000000000cb112e0609b419d3@google.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-ZohoMailClient: External #syz test https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git main --- net/nfc/llcp_core.c | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/net/nfc/llcp_core.c b/net/nfc/llcp_core.c index 1dac28136e6a..fadc8a9ec4df 100644 --- a/net/nfc/llcp_core.c +++ b/net/nfc/llcp_core.c @@ -145,6 +145,13 @@ static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device, static struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local) { + /* Since using nfc_llcp_local may result in usage of nfc_dev, whenever + * we hold a reference to local, we also need to hold a reference to + * the device to avoid UAF. + */ + if (!nfc_get_device(local->dev->idx)) + return NULL; + kref_get(&local->ref); return local; @@ -177,10 +184,18 @@ static void local_release(struct kref *ref) int nfc_llcp_local_put(struct nfc_llcp_local *local) { + struct nfc_dev *dev; + int ret; + if (local == NULL) return 0; - return kref_put(&local->ref, local_release); + dev = local->dev; + + ret = kref_put(&local->ref, local_release); + nfc_put_device(dev); + + return ret; } static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local, @@ -959,8 +974,18 @@ static void nfc_llcp_recv_connect(struct nfc_llcp_local *local, } new_sock = nfc_llcp_sock(new_sk); - new_sock->dev = local->dev; + new_sock->local = nfc_llcp_local_get(local); + if (!new_sock->local) { + reason = LLCP_DM_REJ; + release_sock(&sock->sk); + sock_put(&sock->sk); + sock_put(&new_sock->sk); + nfc_llcp_sock_free(new_sock); + goto fail; + } + + new_sock->dev = local->dev; new_sock->rw = sock->rw; new_sock->miux = sock->miux; new_sock->nfc_protocol = sock->nfc_protocol; @@ -1597,7 +1622,16 @@ int nfc_llcp_register_device(struct nfc_dev *ndev) if (local == NULL) return -ENOMEM; - local->dev = ndev; + /* As we are going to initialize local's refcount, we need to get the + * nfc_dev to avoid UAF, otherwise there is no point in continuing. + * See nfc_llcp_local_get(). + */ + local->dev = nfc_get_device(ndev->idx); + if (!local->dev) { + kfree(local); + return -ENODEV; + } + INIT_LIST_HEAD(&local->list); kref_init(&local->ref); mutex_init(&local->sdp_lock); -- 2.42.0