Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp2137406lqb; Mon, 27 May 2024 09:01:30 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWRc2J8TYpurViCygZHF2l9JMhmIdCCN9bUQ1A9sQ5yZ7iEQDABb5sVAnYPuraWvmucAdy8CsTrH6PcHomec45HRpaveEv6AnHYrmul+A== X-Google-Smtp-Source: AGHT+IGAbIgCernV0vPjfCSv3XqmGqnFLHJ6yuIZVGJ6o7vb25/HSsTkPccGDNa1+3jUtKDr1f1V X-Received: by 2002:a05:622a:3d0:b0:437:bae3:abe3 with SMTP id d75a77b69052e-43fb0e7edecmr115470461cf.18.1716825690003; Mon, 27 May 2024 09:01:30 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716825689; cv=pass; d=google.com; s=arc-20160816; b=wFIhwgX/Kt7Ddr6O8NhK6InO6175LmLHK0i3IBegKml/LcNhYCaOC+IMglKbgwcZJI X2xaeBYJcnfOpJmCODFc3fNXKc46Psixd4NZGS5Le9L+fpcWTl7Bbf37kYLJVX/uqumE wBRrvMYEVesib3jkOAopK6DTEuHn7YAn68hemOVEd+tVtPvbMxBoO7s/HoW0QUMWEeiI TB/3SVY1CeXQ9X/3xBYDm7XOJBt5mmVaiyTCCyLs5onmERI9dan5m2ocoFbRfwpvlbmz 8a9tz4V73AndlwM05wCcQEDEvXWIzh9eXKEfIzHqV8YXwDRmbrSZMEt5DiFvyot90MPy DSVQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Gqw2uA/RvdeCj/bGhXUiNCv7ipe0ruDnamhwpRlX884=; fh=djcyi5/l3TQaO6DyforyfQ5BFxyyqWeZOGyyk1JW1Jo=; b=wGkjlibUpUoHn2l6ojy1yW9l1L04OlbSGGW2JTYfdwfuigzWstXsj4cmIlj/LV1nV+ VnifQ515BEtIo4Ij7mV/utr+D09JazmczgqD6HCacFsJ/HfM/6g4MpP0k5AsMw7tAlY6 RFJRxb5aH3CnYBIDfP5plP4M4hjKoHNuQx8BQKb0EyPCOb/VsI+P0U2/CTVj7x33r/6P Iu/S1YyHN9fVi7m4ibMwNOH0ocQ1Ryb8HXuszvVm7JKrGtMQ1Pxw7HRkKlTDDzrmcIxs lZ2wEikDx23l0AW2t6kIwMhWqNKv4k4wxktOcmTBDW1wSXaQ7i4BXnjoyzepzS4s0iK4 Yg1w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WhRzO8L+; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-191144-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-191144-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d75a77b69052e-43fb17bb148si84382981cf.96.2024.05.27.09.01.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 May 2024 09:01:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-191144-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WhRzO8L+; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-191144-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-191144-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 847581C225F7 for ; Mon, 27 May 2024 16:01:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 33847160860; Mon, 27 May 2024 15:55:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WhRzO8L+" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 434D9155CA7; Mon, 27 May 2024 15:55:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716825358; cv=none; b=jqvUbfSbQOEVPIUJBY4cPmvepOkllkfHXX05AUBCSGtyNA3Hi1ZTUAlhcqHIQZjaAxqCYBpp/OF+Yrgu3VfFw+ez2OERvQXgIGpx3HjAG999D8xn702OZWTsuKYsGIqU3B+R0eBmk5ik3j3H8PRhuVixZoPuGE5bILIw4H3hsHk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716825358; c=relaxed/simple; bh=t69fSFn4QBXxAEyTZrdB5V0gHZ/18lUkY7exkJBwoPI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cTEt7G+X/NfzeHOQcj76cPdW6Wqd5EAqMcP7E6nfjQqjIE8XHmqvMvIcR44RH4eys8p7VhnMGS9Kxhg4YlGZTxcoMDTLPrhP6N2AyOINOU/Z1yFv6f2RDxb4trN/DSTWOCtW7/jTUJXoI3qZb9VQCkQFZM7mF447qSLcR/lOJJs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WhRzO8L+; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id AE567C32781; Mon, 27 May 2024 15:55:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1716825358; bh=t69fSFn4QBXxAEyTZrdB5V0gHZ/18lUkY7exkJBwoPI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WhRzO8L+hw0WPgyoi/I9f6AB7w4lPdNNY6fxIxHjYfbQpfsvoxRiApS8wAPxWRHSw F4+/n51/6yoL1vZ7yzKajQOlvinAPKWIuaug0Py3NSFuHwzAPl7y4VlFiYHPM1t3sU WfzHCVj7aG2uqfdQl5KTSiSlM5KKLmVAQj8iLjP5KDDu9MF3NFG1OnK0WVcXyTrIRZ 6cC5RtNRa6++OsZVzzZIud14XN8Uv8r5BQSvI+Lc9bPIy2eDsf8ZwOfaUepNBpy3ft ztODGQ11vX8cG9qHKR25AMBBXVQXL807IhHbT7t2sKSq17aWHyXSjL+njUfgrfPv9u KvvzvkEYKTDpQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Stefan Berger , Al Viro , Mimi Zohar , Sasha Levin , roberto.sassu@huawei.com, dmitry.kasatkin@gmail.com, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH AUTOSEL 6.6 02/16] ima: Fix use-after-free on a dentry's dname.name Date: Mon, 27 May 2024 11:54:53 -0400 Message-ID: <20240527155541.3865428-2-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240527155541.3865428-1-sashal@kernel.org> References: <20240527155541.3865428-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.6.32 Content-Transfer-Encoding: 8bit From: Stefan Berger [ Upstream commit be84f32bb2c981ca670922e047cdde1488b233de ] ->d_name.name can change on rename and the earlier value can be freed; there are conditions sufficient to stabilize it (->d_lock on dentry, ->d_lock on its parent, ->i_rwsem exclusive on the parent's inode, rename_lock), but none of those are met at any of the sites. Take a stable snapshot of the name instead. Link: https://lore.kernel.org/all/20240202182732.GE2087318@ZenIV/ Signed-off-by: Al Viro Signed-off-by: Stefan Berger Signed-off-by: Mimi Zohar Signed-off-by: Sasha Levin --- security/integrity/ima/ima_api.c | 16 ++++++++++++---- security/integrity/ima/ima_template_lib.c | 17 ++++++++++++++--- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/security/integrity/ima/ima_api.c b/security/integrity/ima/ima_api.c index 597ea0c4d72f7..44b8161746fec 100644 --- a/security/integrity/ima/ima_api.c +++ b/security/integrity/ima/ima_api.c @@ -244,8 +244,8 @@ int ima_collect_measurement(struct integrity_iint_cache *iint, const char *audit_cause = "failed"; struct inode *inode = file_inode(file); struct inode *real_inode = d_real_inode(file_dentry(file)); - const char *filename = file->f_path.dentry->d_name.name; struct ima_max_digest_data hash; + struct name_snapshot filename; struct kstat stat; int result = 0; int length; @@ -316,9 +316,13 @@ int ima_collect_measurement(struct integrity_iint_cache *iint, if (file->f_flags & O_DIRECT) audit_cause = "failed(directio)"; + take_dentry_name_snapshot(&filename, file->f_path.dentry); + integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, - filename, "collect_data", audit_cause, - result, 0); + filename.name.name, "collect_data", + audit_cause, result, 0); + + release_dentry_name_snapshot(&filename); } return result; } @@ -431,6 +435,7 @@ void ima_audit_measurement(struct integrity_iint_cache *iint, */ const char *ima_d_path(const struct path *path, char **pathbuf, char *namebuf) { + struct name_snapshot filename; char *pathname = NULL; *pathbuf = __getname(); @@ -444,7 +449,10 @@ const char *ima_d_path(const struct path *path, char **pathbuf, char *namebuf) } if (!pathname) { - strscpy(namebuf, path->dentry->d_name.name, NAME_MAX); + take_dentry_name_snapshot(&filename, path->dentry); + strscpy(namebuf, filename.name.name, NAME_MAX); + release_dentry_name_snapshot(&filename); + pathname = namebuf; } diff --git a/security/integrity/ima/ima_template_lib.c b/security/integrity/ima/ima_template_lib.c index 6cd0add524cdc..3b2cb8f1002e6 100644 --- a/security/integrity/ima/ima_template_lib.c +++ b/security/integrity/ima/ima_template_lib.c @@ -483,7 +483,10 @@ static int ima_eventname_init_common(struct ima_event_data *event_data, bool size_limit) { const char *cur_filename = NULL; + struct name_snapshot filename; u32 cur_filename_len = 0; + bool snapshot = false; + int ret; BUG_ON(event_data->filename == NULL && event_data->file == NULL); @@ -496,7 +499,10 @@ static int ima_eventname_init_common(struct ima_event_data *event_data, } if (event_data->file) { - cur_filename = event_data->file->f_path.dentry->d_name.name; + take_dentry_name_snapshot(&filename, + event_data->file->f_path.dentry); + snapshot = true; + cur_filename = filename.name.name; cur_filename_len = strlen(cur_filename); } else /* @@ -505,8 +511,13 @@ static int ima_eventname_init_common(struct ima_event_data *event_data, */ cur_filename_len = IMA_EVENT_NAME_LEN_MAX; out: - return ima_write_template_field_data(cur_filename, cur_filename_len, - DATA_FMT_STRING, field_data); + ret = ima_write_template_field_data(cur_filename, cur_filename_len, + DATA_FMT_STRING, field_data); + + if (snapshot) + release_dentry_name_snapshot(&filename); + + return ret; } /* -- 2.43.0