Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp52887ybg; Mon, 8 Jun 2020 16:12:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzdKEE7qAqGQ4QjB8MJZzyPJNAEudpTOEqQTWyj0AVuBHirE5nQFoyseZ0W2sq5+caKamLA X-Received: by 2002:a17:906:4716:: with SMTP id y22mr23720415ejq.125.1591657971007; Mon, 08 Jun 2020 16:12:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591657970; cv=none; d=google.com; s=arc-20160816; b=I/hAy0Y+eLemGz9v5spONWxHtACHZ1y0ydJa8Oqf8Ka9oXaMalpuwgNJVpAgUxWqWz 1swqWf4EDb8+B5wtJphYe4YpgZVCI2KpVozXWJ265jlyViQQw+Sl3lIPwixQEqO62Bbv Yg1cn3/n7/wL8PeY9w7jaZfClKJ325LOFSVQF3MvS7knZm3ytTVRcDE56GwA31TTGZnZ f8926xJcKsC8LDRvoQI8QLf+2fVfunhc8+QuLHDydLgybcafO6FEoDJqW6FEeb5h9k5j YlDpTx7SDmvollscGyzf4Si6PXQuAtETDEaG2sSBSMI7qCOCbBUNzpM+66oFJQN3JU8I S+4A== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=h81lNSiL3JiP4v3YuymVueZFjDOP0bwLCjSf5uzrgFg=; b=e8tcybTgn9xWVUmTjKuB+70b7avIgT/QBYHIMeCYdQ8v+wOoX6CD7tOtjwnjp+Sk3G 2jg2TReVDyq81yWuNhUwEJXtZsLew3UeWIEFamMDGHfPhXKxzHf6XgciCl7vRhxf+R38 +V6nUYtAlI+aqOn2lRUpGqr3gYGOe8ekOvjbs2nPseNKV+q0TKWwWFEI91JA52yH8Cmo xm/VByC0k/SePHZqGdgVTTegF1+hEXni0sbhMDY95cU3MLMzLcyroGf2Ppk/eyLbvnix bvcXVCAHqChWxMiFbdf5auD+Yr8Lc7lqboHBKTMLCmhGQOkbPe77xz5Ds0mZyTTbzO4j gpCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Pq9U9VwP; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f24si9639575ejx.91.2020.06.08.16.12.28; Mon, 08 Jun 2020 16:12:50 -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=Pq9U9VwP; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728460AbgFHXJi (ORCPT + 99 others); Mon, 8 Jun 2020 19:09:38 -0400 Received: from mail.kernel.org ([198.145.29.99]:53876 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728185AbgFHXIc (ORCPT ); Mon, 8 Jun 2020 19:08:32 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id BCAF0208B3; Mon, 8 Jun 2020 23:08:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1591657711; bh=grvurrTZx3QwnOzhXNCbI8odxJzQ/tew0Ej4W8RRl6w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Pq9U9VwPnDQHdbKhFQ0zOd3KWcclX55nI1ghEtveU3ZTMfNc2DzgBu+zrqL02ZsFr SxvlvO3j8wTfwLTfZ85UUeCcOkI01v/eYAkKBRNYkjI1Ofnv67a7DovColhoJu5zMC w67CsH/doGz6aaqXMBpeYGYHCqFp2VqS5az4+uQc= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Paul Moore , teroincn@gmail.com, Richard Guy Briggs , Sasha Levin , linux-audit@redhat.com Subject: [PATCH AUTOSEL 5.7 107/274] audit: fix a net reference leak in audit_send_reply() Date: Mon, 8 Jun 2020 19:03:20 -0400 Message-Id: <20200608230607.3361041-107-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200608230607.3361041-1-sashal@kernel.org> References: <20200608230607.3361041-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore 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 87f31bf1f0a0..033b14712340 100644 --- a/kernel/audit.c +++ b/kernel/audit.c @@ -924,19 +924,30 @@ struct sk_buff *audit_make_reply(int seq, int type, int done, 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; } @@ -950,35 +961,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