Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp412667rdb; Tue, 5 Dec 2023 08:43:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IFHhtx2P3sFqpgO1V+7qB95H/Ph2Xpl8r0U+77OkoRA0vwI45XQlOJxltFNyM7uAbHkVLMX X-Received: by 2002:a17:902:e802:b0:1d0:acf7:833c with SMTP id u2-20020a170902e80200b001d0acf7833cmr3100624plg.26.1701794612100; Tue, 05 Dec 2023 08:43:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701794612; cv=none; d=google.com; s=arc-20160816; b=Og9mMvfKESJuXPwuoINRqylQNzTrsHicCfvh8BPA6EXCfNrWb0hctN05Q0+lUjIUhk Uqomu2xMyXpLtPcefvSnJH/k9shMXV78T9+RimUulbp41d0Lr6l/sbs+GpfSGHp5HNvC YkvU6eimCFJ7v+jQKGBN6gJpIk+VsJ/2NAMP4XzVFsFSohWf3z3Ku81nWpwW/rn8SnMh c6v041DgSwAAozXOrAJkhsfH0pAh0OgpXj1ZqaZcqjnqHkn9IUAhHI/b/exrxBROUV4N y5w89xZoZfGuV2ctjvM6MjriZhLg6SYW1/DhxSOl4esyYr6/6yQCwTs3b2xbkcaD6VOF gB4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:autocrypt :from:references:cc:to:content-language:subject:user-agent :mime-version:date:message-id:dkim-signature; bh=a8TfjwRcKypybBxsw7XwMuabdDZTIwUX7pnwmTTnOxQ=; fh=wzMFJR73Nrh2T0mnQfDDYy6EqJs8gFPpUb/MuCNRfcs=; b=V5ZOV22NIjHEXf96G3Hr6IpHEIY1XE27W06p9p2XtMf9nzFQEoDkXtkS0X1II7pPEV yXwCQe/yeB5GyVDVeYabjGB2a35+hpwRIkWAuY/BBGljENBvxerJ2dntCghMbVj5t6Q1 82j4M8qmKe6wfm5vx4RtzRf1uz+ff2eSK22XLvF8JqeJJuojH0olAwlka1xcBUDbPpnG JQbxtEgWhvAN40LIjjiv2JJW2wJ4iR6idGu/v2ky+zgtctAq4ovP9IiyS/0GZ9zNLAo6 TfGhIfrA367pY8TZaJhzobQPm9aULVp3u7dr4j/dqRPLCVfGmHmQeqJW7Qp29brmBVzr TX9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="u2gkL5k/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id c8-20020a170903234800b001d0a084b012si3700827plh.558.2023.12.05.08.43.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 08:43:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="u2gkL5k/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id CC6C8807F176; Tue, 5 Dec 2023 08:40:24 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229710AbjLEQkB (ORCPT + 99 others); Tue, 5 Dec 2023 11:40:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229462AbjLEQj7 (ORCPT ); Tue, 5 Dec 2023 11:39:59 -0500 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E99B812C for ; Tue, 5 Dec 2023 08:40:03 -0800 (PST) Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-33350fcb2c7so1391119f8f.2 for ; Tue, 05 Dec 2023 08:40:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1701794402; x=1702399202; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:autocrypt:from:references:cc :to:content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=a8TfjwRcKypybBxsw7XwMuabdDZTIwUX7pnwmTTnOxQ=; b=u2gkL5k/RUlcpJ13U/gn1nFMeY48+7tUcVmkJsqGlZjfYPam4AVlffCvutuG18Tg+y /cpisIwd18d7VCKEcwnM2uJVUvNZ5BhDFQmDEtRFwMEdiLa2QELPt0ih5k663QF6htaP JVIV59l91bIVUesTd0AwScW5AQMYLWV/G7rQclEnVbvj3j8FZ7aHPo+pb4wFqCOrJZLU SCQ2OjeEMNFleiw9wbYfN5Q9CkIZje0XxqmKCEBbpQfMtxgFgN1Y+Vo5samLlaQzg82e JoaNtpDJtbTQ1nHfDi/Dv12eH6rU3VMwk4r13OK0pIdUeU0aINF386/1cI/TQrbrMxhM I0DA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701794402; x=1702399202; h=content-transfer-encoding:in-reply-to:autocrypt:from:references:cc :to:content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=a8TfjwRcKypybBxsw7XwMuabdDZTIwUX7pnwmTTnOxQ=; b=uz/WHZ67orpslDfZvahPJJJd3csLkQXOx5AdyWl8nl3mM0dCC1F+mAKVEOBywShoPj iFSlFF+6WA3VuL/6eX0IhYIgKZTqVZP/6QxPBLJ0CYdVez857VazcXyVnlka67x97awr 7+0VfhHQiTqCP3LUP4UNTyIMivn+WIPm6Ov/CSgJF/y6l7b4J4R2VrveZ7bTFSBQsJti dQ4ytVPY/nWFEUtzK6325zD02FaRJX7/dAqFx7eeUOZzYSjxeEucxhmxKhjd7qK+GhRt Ekal2z8LWNoDE2kSao2J4tVAMIhWqgOjp0exmNrRg30tLD5cXGdFKIcvnSBbx/jtBn0y TSBw== X-Gm-Message-State: AOJu0YxPP3DiZ05yDswtgp/+I59v0qg88dl0qRFXpRPF2MHOFuwsp76c 1yfu93jSmsQMe7CITuI/ciboig== X-Received: by 2002:a05:6000:4ef:b0:333:3f7c:b2bd with SMTP id cr15-20020a05600004ef00b003333f7cb2bdmr3281889wrb.108.1701794402365; Tue, 05 Dec 2023 08:40:02 -0800 (PST) Received: from [192.168.1.20] ([178.197.218.27]) by smtp.gmail.com with ESMTPSA id dn6-20020a0560000c0600b003333b0d603bsm2306072wrb.42.2023.12.05.08.40.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 05 Dec 2023 08:40:01 -0800 (PST) Message-ID: Date: Tue, 5 Dec 2023 17:40:00 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH net-next v3 1/2] nfc: llcp_core: Hold a ref to llcp_local->dev when holding a ref to llcp_local Content-Language: en-US To: Siddh Raman Pant , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Suman Ghosh Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+bbe84a4010eeea00982d@syzkaller.appspotmail.com References: <4143dc4398aa4940a76d3f375ec7984e98891a11.1701627492.git.code@siddh.me> From: Krzysztof Kozlowski Autocrypt: addr=krzysztof.kozlowski@linaro.org; keydata= xsFNBFVDQq4BEAC6KeLOfFsAvFMBsrCrJ2bCalhPv5+KQ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6pAIXZH3qFW+4TKPMGZ2rZ6zITrMip+12jgw4mGjy 5y06JZvA02rZT2k9aa7i9dUUFggaanI09jNGbRA/ABEBAAHCwXwEGAEKACYCGwwWIQSb0H4O DFH41ZZ3t1Qbk0N9O0FimwUCYDzvagUJFF+UtgAKCRAbk0N9O0Fim9JzD/0auoGtUu4mgnna oEEpQEOjgT7l9TVuO3Qa/SeH+E0m55y5Fjpp6ZToc481za3xAcxK/BtIX5Wn1mQ6+szfrJQ6 59y2io437BeuWIRjQniSxHz1kgtFECiV30yHRgOoQlzUea7FgsnuWdstgfWi6LxstswEzxLZ Sj1EqpXYZE4uLjh6dW292sO+j4LEqPYr53hyV4I2LPmptPE9Rb9yCTAbSUlzgjiyyjuXhcwM qf3lzsm02y7Ooq+ERVKiJzlvLd9tSe4jRx6Z6LMXhB21fa5DGs/tHAcUF35hSJrvMJzPT/+u /oVmYDFZkbLlqs2XpWaVCo2jv8+iHxZZ9FL7F6AHFzqEFdqGnJQqmEApiRqH6b4jRBOgJ+cY qc+rJggwMQcJL9F+oDm3wX47nr6jIsEB5ZftdybIzpMZ5V9v45lUwmdnMrSzZVgC4jRGXzsU EViBQt2CopXtHtYfPAO5nAkIvKSNp3jmGxZw4aTc5xoAZBLo0OV+Ezo71pg3AYvq0a3/oGRG KQ06ztUMRrj8eVtpImjsWCd0bDWRaaR4vqhCHvAG9iWXZu4qh3ipie2Y0oSJygcZT7H3UZxq fyYKiqEmRuqsvv6dcbblD8ZLkz1EVZL6djImH5zc5x8qpVxlA0A0i23v5QvN00m6G9NFF0Le D2GYIS41Kv4Isx2dEFh+/Q== In-Reply-To: <4143dc4398aa4940a76d3f375ec7984e98891a11.1701627492.git.code@siddh.me> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 05 Dec 2023 08:40:24 -0800 (PST) On 04/12/2023 14:08, Siddh Raman Pant wrote: > llcp_sock_sendmsg() calls nfc_llcp_send_ui_frame() which in turn calls > nfc_alloc_send_skb(), which accesses the nfc_dev from the llcp_sock for > getting the headroom and tailroom needed for skb allocation. > > Parallelly the nfc_dev can be freed, as the refcount is decreased via > nfc_free_device(), leading to a UAF reported by Syzkaller, which can > be summarized as follows: > > (1) llcp_sock_sendmsg() -> nfc_llcp_send_ui_frame() > -> nfc_alloc_send_skb() -> Dereference *nfc_dev > (2) virtual_ncidev_close() -> nci_free_device() -> nfc_free_device() > -> put_device() -> nfc_release() -> Free *nfc_dev > > When a reference to llcp_local is acquired, we do not acquire the same > for the nfc_dev. This leads to freeing even when the llcp_local is in > use, and this is the case with the UAF described above too. > > Thus, when we acquire a reference to llcp_local, we should acquire a > reference to nfc_dev, and release the references appropriately later. > > References for llcp_local is initialized in nfc_llcp_register_device() > (which is called by nfc_register_device()). Thus, we should acquire a > reference to nfc_dev there. > > nfc_unregister_device() calls nfc_llcp_unregister_device() which in > turn calls nfc_llcp_local_put(). Thus, the reference to nfc_dev is > appropriately released later. > > Reported-and-tested-by: syzbot+bbe84a4010eeea00982d@syzkaller.appspotmail.com > Closes: https://syzkaller.appspot.com/bug?extid=bbe84a4010eeea00982d > Fixes: c7aa12252f51 ("NFC: Take a reference on the LLCP local pointer when creating a socket") > Signed-off-by: Siddh Raman Pant > --- > net/nfc/llcp_core.c | 24 ++++++++++++++++++++++-- > 1 file changed, 22 insertions(+), 2 deletions(-) > > diff --git a/net/nfc/llcp_core.c b/net/nfc/llcp_core.c > index 1dac28136e6a..9d45ce6dcdca 100644 > --- a/net/nfc/llcp_core.c > +++ b/net/nfc/llcp_core.c > @@ -145,6 +145,9 @@ 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) > { > + if (!nfc_get_device(local->dev->idx)) > + return NULL; > + > kref_get(&local->ref); > > return local; > @@ -180,6 +183,7 @@ int nfc_llcp_local_put(struct nfc_llcp_local *local) > if (local == NULL) > return 0; > > + nfc_put_device(local->dev); Mismatched order with get. Unwinding is always in reversed order. Or maybe other order is here on purpose? Then it needs to be explained. > return kref_put(&local->ref, local_release); > } > > @@ -959,8 +963,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) { There is already an cleanup path/label, so extend it. Existing code needs some improvements in that matter as well. > + 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 +1611,13 @@ int nfc_llcp_register_device(struct nfc_dev *ndev) > if (local == NULL) > return -ENOMEM; > > - local->dev = ndev; > + /* Hold a reference to the device. */ That's obvious. Instead write something not obvious - why you call nfc_get_device() while not incrementing reference to llcp_local. > + local->dev = nfc_get_device(ndev->idx); This looks confusing. If you can access ndev->idx, then ndev reference was already increased. In such case iterating through all devices to find it, is unnecessary and confusing. Best regards, Krzysztof