Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3128107pxf; Mon, 5 Apr 2021 04:07:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwDJZNnO0HGgLfUhCje3cbEK9/1GJ9THOybBcR/4rABVHrNlZFjmNr42jUu8BcqrsFj76aD X-Received: by 2002:a6b:3b12:: with SMTP id i18mr19347169ioa.131.1617620875804; Mon, 05 Apr 2021 04:07:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617620875; cv=none; d=google.com; s=arc-20160816; b=vHQy0ay82FDJUxc2gU+1A3whUoNXhMgInkWRKtMvtnviVvV2zHtJZ2kCUvMJdkVZIm D+QRWf3ljm+5hdGOode3VpxjdSaOvh1F9PqfVEbDrH3Nm599VqRLOKTNG8BsNiwLI3J7 Eb/5h52D81QS58ejVYn3JPokjiFjWTgd6JSvB8HUL7gQ6Sowe9mmIC1vn+5XYM27aAHS fH/fvod20ZAso10stgigWDA2rWUEWBHVNtBV43dtvVGth5GhIE/eEMYf8I+DF/qngpjO S6u23GecbIAxc7Dk18gwdBMy6TDu0MccPiX+UCntDL8fqVljnS3bgZzBnciblTwP2F8p 7GIg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=frbsnZc3bCOy4Itgl+QJmftg4huJnZIWNdWqfUh1XvM=; b=e516ggDKwUJfkW2yXC9G411BiFbneRJlaNsR/9J9T1v+68VZec7m6fdX2VC0+L80/2 Ej0O61SmwN/Cp6QtbM4qQBjTfMm5j1q5WRrWAr0vyL6v4+hvFGkhCZ6dC2/wiNZI/4yx 5x1sJXd0W/HU2IIDtbw0UdoVjEu6jsdgguduFBXI/Sd4/xJ3diNOa4S2OiSampfkgh/M nUtARoNtIUHaxUUGvKiX/TCNy/YxEQrEDt/3fjVC0MucrBIIPk9AGBHyUBPCW5YnDA7i MVXrkrc2gxoIyoTZEs7EljMawaqDwIPIklWyMsnIl/UHFnoR6VMcY8cBwUaqAsxrIv9C 2Wow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="t/34lefz"; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x6si11377875ill.58.2021.04.05.04.07.44; Mon, 05 Apr 2021 04:07:55 -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=@linuxfoundation.org header.s=korg header.b="t/34lefz"; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233745AbhDEI61 (ORCPT + 99 others); Mon, 5 Apr 2021 04:58:27 -0400 Received: from mail.kernel.org ([198.145.29.99]:37544 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233340AbhDEI5v (ORCPT ); Mon, 5 Apr 2021 04:57:51 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 301B8610E8; Mon, 5 Apr 2021 08:57:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1617613065; bh=3SHKbbEXoPErrAsIDb2nUkQQYB67JgIL0oeUYVPoEH0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=t/34lefzYaWzQABKVu5g0Ih0n6EniawWAJbiU2XnYoLqajt8pVNLlDMog5AOCksBV uAGRV9L0PYnWkClI8aMpvPoMueyN8qc5FWMPWjQv4YXvhHJHlAlxyMbL37+FfBsfyv 5gbtiynfBzjvnEew8HpHogx7NTNw4HLcvcA0EX34= 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 , Wen Yang Subject: [PATCH 4.9 34/35] audit: fix a net reference leak in audit_send_reply() Date: Mon, 5 Apr 2021 10:54:09 +0200 Message-Id: <20210405085019.951555935@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210405085018.871387942@linuxfoundation.org> References: <20210405085018.871387942@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Paul Moore commit a48b284b403a4a073d8beb72d2bb33e54df67fb6 upstream. 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 Cc: # 4.9.x Signed-off-by: Wen Yang Signed-off-by: Greg Kroah-Hartman --- kernel/audit.c | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) --- a/kernel/audit.c +++ b/kernel/audit.c @@ -580,6 +580,18 @@ 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; @@ -592,8 +604,8 @@ static int audit_send_reply_thread(void /* Ignore failure. It'll only happen if the sender goes away, because our timeout is set to infinite. */ netlink_unicast(aunet->nlsk , reply->skb, reply->portid, 0); - put_net(net); - kfree(reply); + reply->skb = NULL; + audit_free_reply(reply); return 0; } /** @@ -606,36 +618,34 @@ static int audit_send_reply_thread(void * @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) { u32 portid = NETLINK_CB(request_skb).portid; - 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(portid, seq, type, done, multi, payload, size); - if (!skb) - goto out; + reply->skb = audit_make_reply(portid, seq, type, done, multi, payload, size); + if (!reply->skb) + goto err; - reply->net = get_net(net); + reply->net = get_net(sock_net(NETLINK_CB(request_skb).sk)); reply->portid = 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); } /*