Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2511031imm; Mon, 24 Sep 2018 05:42:12 -0700 (PDT) X-Google-Smtp-Source: ACcGV60L1jaNjTnNsAB5M5FrXsLgqZ7Fn5ZV+v7otSAd+BjLju8eLhGZzVxS/hF+ZBDPUluaAzQc X-Received: by 2002:a63:9809:: with SMTP id q9-v6mr9295737pgd.58.1537792932775; Mon, 24 Sep 2018 05:42:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537792932; cv=none; d=google.com; s=arc-20160816; b=gZkF5OU3qLEy7/LA5kyY8PtpDdVeNLqFqBeXMPoh1i2MuKqHkRq2WhFCGHGCq1BkBg thycAoTV7+VLTnMygN5aVKkEdGnTYtm6sFKzJjHPSDaCHyLRz3cer1PcStSisDC9tJoq uxkcTq+OH3ojTx6OYPU+aaoe6AKDEvAeWSQl94lR15qFUhjgOm0llRYm2uVK/lQYDmNb nXmcpjH74AixGWp83EyYlPXYxkAAvVnlJVzaa+YarAi0/qhezxXphIbaroFbyJ9LCTa4 rYTfNplG8d8XEgB6bDN8f4ms7Hr8fcDfq1AG5NHtiqecwjtsAD7FpnsWAgeP6/EiO21P +Y1Q== 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; bh=KsCXY3o61dDUT5aeU7sQvKn2tdUwuGMupvFyLg8qDas=; b=OQfUTEIqmRCBn5vUzOlj27u/m2VmNowUPR+m2WXjnLQ1fZCVlfBvBh6A3AsBHICfI3 Ua7fTl07hS6KD40reakjttcmhU+mnWtXgzYFPuJcXUcVDH8rvou0zyblhqk5LIoz9UzV 80Qe69dGnCeP3/3hI60HTHqfrfPAMenEJiQKdVNcnssU8TZGr3jHaw2bu4RcZX/m+VFp IkyfKTS8haAGw8ZYszbGla9DHiAT8bWOCO/7v833UBRmpI7b2ZR48kSSZr4ZNLUTevdx k4/EkwW8qhRAR3AlBBXKVK0IrADLiqQnZmX1tzXmEneFKj00ddoecZeXKw8Zy9I3kfO4 Isgg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 98-v6si6976752pla.59.2018.09.24.05.41.57; Mon, 24 Sep 2018 05:42:12 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388435AbeIXSlj (ORCPT + 99 others); Mon, 24 Sep 2018 14:41:39 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:58872 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731070AbeIXSli (ORCPT ); Mon, 24 Sep 2018 14:41:38 -0400 Received: from localhost (ip-213-127-77-73.ip.prioritytelecom.net [213.127.77.73]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 5123E1099; Mon, 24 Sep 2018 12:39:40 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ronny Chevalier , Richard Guy Briggs , Paul Moore , Sasha Levin Subject: [PATCH 4.18 149/235] audit: fix use-after-free in audit_add_watch Date: Mon, 24 Sep 2018 13:52:15 +0200 Message-Id: <20180924113120.346534159@linuxfoundation.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20180924113103.999624566@linuxfoundation.org> References: <20180924113103.999624566@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review 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 4.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Ronny Chevalier [ Upstream commit baa2a4fdd525c8c4b0f704d20457195b29437839 ] audit_add_watch stores locally krule->watch without taking a reference on watch. Then, it calls audit_add_to_parent, and uses the watch stored locally. Unfortunately, it is possible that audit_add_to_parent updates krule->watch. When it happens, it also drops a reference of watch which could free the watch. How to reproduce (with KASAN enabled): auditctl -w /etc/passwd -F success=0 -k test_passwd auditctl -w /etc/passwd -F success=1 -k test_passwd2 The second call to auditctl triggers the use-after-free, because audit_to_parent updates krule->watch to use a previous existing watch and drops the reference to the newly created watch. To fix the issue, we grab a reference of watch and we release it at the end of the function. Signed-off-by: Ronny Chevalier Reviewed-by: Richard Guy Briggs Signed-off-by: Paul Moore Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- kernel/audit_watch.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) --- a/kernel/audit_watch.c +++ b/kernel/audit_watch.c @@ -419,6 +419,13 @@ int audit_add_watch(struct audit_krule * struct path parent_path; int h, ret = 0; + /* + * When we will be calling audit_add_to_parent, krule->watch might have + * been updated and watch might have been freed. + * So we need to keep a reference of watch. + */ + audit_get_watch(watch); + mutex_unlock(&audit_filter_mutex); /* Avoid calling path_lookup under audit_filter_mutex. */ @@ -427,8 +434,10 @@ int audit_add_watch(struct audit_krule * /* caller expects mutex locked */ mutex_lock(&audit_filter_mutex); - if (ret) + if (ret) { + audit_put_watch(watch); return ret; + } /* either find an old parent or attach a new one */ parent = audit_find_parent(d_backing_inode(parent_path.dentry)); @@ -446,6 +455,7 @@ int audit_add_watch(struct audit_krule * *list = &audit_inode_hash[h]; error: path_put(&parent_path); + audit_put_watch(watch); return ret; }