Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp774442ybt; Fri, 19 Jun 2020 13:26:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxFJIfy7km+xnQHf11cCQZrwNQhFipe9OY/3RNtEP0Jre6utS1JbyisJOMlxi9f/l3BckH9 X-Received: by 2002:a05:6402:1a48:: with SMTP id bf8mr5225363edb.133.1592598373960; Fri, 19 Jun 2020 13:26:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592598373; cv=none; d=google.com; s=arc-20160816; b=m/R4EF7yekIxtfzn5krY6CPxXgNE5Q3xTG2chPZ+BtTqOYo4Z+mt7VCzyH1dLcJKBJ k+exsjpbpPB0QY9WqIDkF3WhCz9sMgADYvcAPI5v7q5N70vTk3svmtFT3llFBvRTd8+g 5Sv0tIg8wOnbf43W8+mJOvCJz/IQH3ZGT8S5m+oKwf9HSfLoody9QgtijraXGZIU0Juw FXcNU7FishiGzct0RDqc33iMXHA26bfh8AKMAcpw1lDKFbat2uuk9sinrWXz8qJKVOWr Jo4eaRS5n7ZnyK3AhKvKraRUofDSlO4dlHtSi/3tNBU4moHqqpJT0Sfbk8CZ2u0MY4U1 5Gcg== 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=g0LR53jO16mZJVZSqpU6MFbPH9GRXbbrpNkbPlZhgSU=; b=ZFxoVH+t9LDCI0NrP2WYTyBl+2IykYUGEdsSfllNH8nMEyoVcBgxJwCwOSxfpjWc8h 3PVgt4nGK8u9OtDPF55uFzdpM5wiviol4zY1g4SSOwJzBRk1bAFBApAQbWh8zfmW3wuF KyYfsONks17v4yY685i/XD9/BfRBg50XNU+jKAEZ1ETgaK0EpepoWzNgxESP2j3UG3mA lHG0zaWmdfj6Rt2fWBdlgKCm6QMVn9OQ7iWeD/PeQM7ZHEGZdiJYRDUIcJdN1kCHrKqG gCXegToMuQZ/bUDjQAEW26JO28dlIb9Y0N8nITN6sczZDu/LAwyyerymxaqA/g7bqUCS sq6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=E1scWhiL; 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 d16si4399674edx.546.2020.06.19.13.25.51; Fri, 19 Jun 2020 13:26:13 -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=E1scWhiL; 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 S2390575AbgFSO7z (ORCPT + 99 others); Fri, 19 Jun 2020 10:59:55 -0400 Received: from mail.kernel.org ([198.145.29.99]:56000 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390553AbgFSO7p (ORCPT ); Fri, 19 Jun 2020 10:59:45 -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 8FB0121BE5; Fri, 19 Jun 2020 14:59:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1592578785; bh=svkCY4kcX35RFxdXiTu9EV5sl1nHP7BzH9zbGc10ERc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=E1scWhiLwFlKIkYnBfzvVrWblKgaadkftRv5HNanvWZNjGMAIIptXZmiL9j9x5CWX Yr25jZRSavLEx8DMTVFAqCUfuynltzjPYi7QKM5d90h7rWGP9aHgRbUQbgpn3TccLt XmBGNA48GUwATMfjgdiTYTlgleGiMras1nw/e4/o= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, teroincn@gmail.com, Richard Guy Briggs , Paul Moore , Sasha Levin Subject: [PATCH 4.19 125/267] audit: fix a net reference leak in audit_send_reply() Date: Fri, 19 Jun 2020 16:31:50 +0200 Message-Id: <20200619141654.831201283@linuxfoundation.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200619141648.840376470@linuxfoundation.org> References: <20200619141648.840376470@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: Paul Moore [ Upstream commit a48b284b403a4a073d8beb72d2bb33e54df67fb6 ] If audit_send_reply() fails when trying to create a new thread to send the reply it also fails to cleanup properly, leaking a reference to a net structure. This patch fixes the error path and makes a handful of other cleanups that came up while fixing the code. Reported-by: teroincn@gmail.com Reviewed-by: Richard Guy Briggs Signed-off-by: Paul Moore Signed-off-by: Sasha Levin --- kernel/audit.c | 50 +++++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/kernel/audit.c b/kernel/audit.c index 7afec5f43c63..20c78480d632 100644 --- a/kernel/audit.c +++ b/kernel/audit.c @@ -937,19 +937,30 @@ out_kfree_skb: return NULL; } +static void audit_free_reply(struct audit_reply *reply) +{ + if (!reply) + return; + + if (reply->skb) + kfree_skb(reply->skb); + if (reply->net) + put_net(reply->net); + kfree(reply); +} + static int audit_send_reply_thread(void *arg) { struct audit_reply *reply = (struct audit_reply *)arg; - struct sock *sk = audit_get_sk(reply->net); audit_ctl_lock(); audit_ctl_unlock(); /* Ignore failure. It'll only happen if the sender goes away, because our timeout is set to infinite. */ - netlink_unicast(sk, reply->skb, reply->portid, 0); - put_net(reply->net); - kfree(reply); + netlink_unicast(audit_get_sk(reply->net), reply->skb, reply->portid, 0); + reply->skb = NULL; + audit_free_reply(reply); return 0; } @@ -963,35 +974,32 @@ static int audit_send_reply_thread(void *arg) * @payload: payload data * @size: payload size * - * Allocates an skb, builds the netlink message, and sends it to the port id. - * No failure notifications. + * Allocates a skb, builds the netlink message, and sends it to the port id. */ static void audit_send_reply(struct sk_buff *request_skb, int seq, int type, int done, int multi, const void *payload, int size) { - struct net *net = sock_net(NETLINK_CB(request_skb).sk); - struct sk_buff *skb; struct task_struct *tsk; - struct audit_reply *reply = kmalloc(sizeof(struct audit_reply), - GFP_KERNEL); + struct audit_reply *reply; + reply = kzalloc(sizeof(*reply), GFP_KERNEL); if (!reply) return; - skb = audit_make_reply(seq, type, done, multi, payload, size); - if (!skb) - goto out; - - reply->net = get_net(net); + reply->skb = audit_make_reply(seq, type, done, multi, payload, size); + if (!reply->skb) + goto err; + reply->net = get_net(sock_net(NETLINK_CB(request_skb).sk)); reply->portid = NETLINK_CB(request_skb).portid; - reply->skb = skb; tsk = kthread_run(audit_send_reply_thread, reply, "audit_send_reply"); - if (!IS_ERR(tsk)) - return; - kfree_skb(skb); -out: - kfree(reply); + if (IS_ERR(tsk)) + goto err; + + return; + +err: + audit_free_reply(reply); } /* -- 2.25.1