From patchwork Mon Oct 23 13:06:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Daniel_Gr=C3=B6ber?= X-Patchwork-Id: 156891 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp1306930vqx; Mon, 23 Oct 2023 06:44:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHVVCC1qO6Egz+MEZKkWPiJiXjT+xoWOrP+Ji8T+G4yLghYW6xqXg5RtGIGw9PDhlGcWlOF X-Received: by 2002:a05:6830:2b0b:b0:6b9:4155:7dbd with SMTP id l11-20020a0568302b0b00b006b941557dbdmr10975374otv.0.1698068671795; Mon, 23 Oct 2023 06:44:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698068671; cv=none; d=google.com; s=arc-20160816; b=KK43LgJIrgQuoMaOe52iJXCzctAY9BYeaa17fspZTuqI/iWUmlumaIHzHrUgKN5oXm SBAX/EU2lE950sr/2XUxe8CpJX0T3nJd2AhrBQ1jzRJLh1cFDtBmDINy01KZhcYQ2D6+ ylM9jwcvUnX1l12TlSucpkUtiWGxgF6lEk4P24K6k9lc6FZXlAqGNtVmwI9v1yaqtG6q utJ6gPdLDBe1X6PsFkQ+UwlxPopURkP2xl0Py9peGzsQblA79GiY2QjWgJgCZ95gMhix SqzQGBY6pAkP2Z9Ih3Vjx1AuUooaqleljmwYEa2f5TwLCfplCO5iwJZn6yxDPt2Yjg3c VdPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=oKWxyzDXJ5GHvMffXyKRFdndvr9vNZQahDsj7EHisiw=; fh=Z92tCVzgeX4gNGyyutHFrxDTE5M9ZxLJgb2EBIJEmCs=; b=hCMr1dxJRRIqte0gzzxqSWK8qBiA8owHeDPRo9DPFIyawoYlLItbdFGnrTLfVO84Mu miaW3i7ahxXkoRuGSEUUPnDkb5HAHjUnkQpkxDeED3aUreeIB4lvZuigZ+SIrPl2SoX0 IRHdlJLuk5cFloFPtTrwWGshZZDUexTsVd/SVQUXkKWrIxAHPEUE0Ex+Qq0xsnjl60Xg oFeDejlsaHmgCgV7JVqe5JNFzC2f3u+JT71YQbdH+Ws9Cn3SKLuHXKp0tiniqFMqxjtW t2wJbo9TkVXwL+sAYxhEZJReuT6dSPfJe/QKaO2VbDwiy78Mtx2hO/9U8WwRbDUb2qg+ DvyA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=darkboxed.org Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id f2-20020ad442c2000000b0065b21f1b691si5661610qvr.59.2023.10.23.06.44.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 06:44:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=darkboxed.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 7B7D880ADF03; Mon, 23 Oct 2023 06:44:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229865AbjJWNoX (ORCPT + 27 others); Mon, 23 Oct 2023 09:44:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229452AbjJWNoW (ORCPT ); Mon, 23 Oct 2023 09:44:22 -0400 X-Greylist: delayed 2264 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Mon, 23 Oct 2023 06:44:19 PDT Received: from janet.servers.dxld.at (mail.servers.dxld.at [IPv6:2001:678:4d8:200::1a57]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0321891 for ; Mon, 23 Oct 2023 06:44:18 -0700 (PDT) Received: janet.servers.dxld.at; Mon, 23 Oct 2023 15:06:26 +0200 From: =?utf-8?q?Daniel_Gr=C3=B6ber?= To: "Jason A. Donenfeld" , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , wireguard@lists.zx2c4.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: =?utf-8?q?Daniel_Gr=C3=B6ber?= Subject: [PATCH] wireguard: Fix leaking sockets in wg_socket_init error paths Date: Mon, 23 Oct 2023 15:06:09 +0200 Message-Id: <20231023130609.595122-1-dxld@darkboxed.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Mon, 23 Oct 2023 06:44:29 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780554055491406140 X-GMAIL-MSGID: 1780554055491406140 This doesn't seem to be reachable normally, but while working on a patch for the address binding code I ended up triggering this leak and had to reboot to get rid of the leaking wg sockets. --- drivers/net/wireguard/socket.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/drivers/net/wireguard/socket.c b/drivers/net/wireguard/socket.c index 0414d7a6ce74..c35163f503e7 100644 --- a/drivers/net/wireguard/socket.c +++ b/drivers/net/wireguard/socket.c @@ -387,7 +387,7 @@ int wg_socket_init(struct wg_device *wg, u16 port) ret = udp_sock_create(net, &port4, &new4); if (ret < 0) { pr_err("%s: Could not create IPv4 socket\n", wg->dev->name); - goto out; + goto err; } set_sock_opts(new4); setup_udp_tunnel_sock(net, new4, &cfg); @@ -402,7 +402,7 @@ int wg_socket_init(struct wg_device *wg, u16 port) goto retry; pr_err("%s: Could not create IPv6 socket\n", wg->dev->name); - goto out; + goto err; } set_sock_opts(new6); setup_udp_tunnel_sock(net, new6, &cfg); @@ -414,6 +414,11 @@ int wg_socket_init(struct wg_device *wg, u16 port) out: put_net(net); return ret; + +err: + sock_free(new4 ? new4->sk : NULL); + sock_free(new6 ? new6->sk : NULL); + goto out; } void wg_socket_reinit(struct wg_device *wg, struct sock *new4,