Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp496585pxx; Wed, 28 Oct 2020 09:38:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyRoJKNElMSY6MOYYdpchBFtjKh7qI8Q0DhCu6SomCsaojQOprwtIC/wRDtKw2gN+D5ELC/ X-Received: by 2002:aa7:cfc1:: with SMTP id r1mr8921851edy.138.1603903100286; Wed, 28 Oct 2020 09:38:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603903100; cv=none; d=google.com; s=arc-20160816; b=fQzLud7AH071czCfL9tzsLIlbsoMeXKNvbpJsTprdHj/Hoiu8Nz1CeVs/PZJBxjgz5 0ClNCOy8eqIq3HdI0bHJ4rBpjOwyYg38T1TLxb+egTK5IvSMtpoxamFWTDCy2qslr/tc OSOLud36wXslXr3NnL5j8pNqfcGi8w2J192rW8r7w3fMR0QpIxO1uaKZKdnyf2Lomd67 2M8wALUVFS199nqkMPF96jv76H72uW9aL4DkC3Gs7y3qSjoIOA58aDDQ7rhQKXzuFJmH UBwF51t0A7Ve8CnZpT+5wUIq8j+hF9mPyn1FJOBa+cFAU7tbJeAkpb5DFFT7sGo+noeY npHQ== 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=dZIy9QkpPCnlKUKMhkwM4GfEkrI1dA0WZSrWDldkfnM=; b=B/mlxdAPALeSW8yOHuTrDt/sOnVggT3RTHE6vhmO6U6zT6kpNMNiBB67x/Q9hqnn7d FB3YvPzqJB+ur97MWXZoAbCmOJWQvumZ8IqbTxEiD2JOaPR9qa1ltkKTkcSstXrf0CKN SifJJ9xIoCmizvqc9wZDZr2tqsRWs/csuiKnk87Xy0FXyQhjXKVT8SndDqhP35j/3G8M 2Ei8QXku/hLJJLFbx1kcEQvsJVfUs5poYDFKkdw5hdfIpD+Wz0+BBARKWgzQZDbmKau8 eCN9ZRduCrc/+D/3UrHw2faBj9UU3vmSx/K+i12zEYjANiptd2ztRO4EGZyu50rNq7g9 c5gg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=D+TRTMvE; 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=fail (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 u21si3820982ejb.621.2020.10.28.09.37.57; Wed, 28 Oct 2020 09:38:20 -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=D+TRTMvE; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1818085AbgJ0RRS (ORCPT + 99 others); Tue, 27 Oct 2020 13:17:18 -0400 Received: from mail.kernel.org ([198.145.29.99]:59126 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1783176AbgJ0O56 (ORCPT ); Tue, 27 Oct 2020 10:57:58 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (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 EAD2E206F4; Tue, 27 Oct 2020 14:57:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603810677; bh=UnlBI+gDGXaX3+z9SxabEFOzl99bud+KBkDUDoIRtFU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=D+TRTMvEsjekrrbXqV8FQMfmU6hYSO1XZL760I2JtkfNvYOYxHX0x05102FRs8VAX fTZvvKtO2K3Z/DDEvnVnDB79IHJkpwRklT307Rqsdh0uvqLXWsdwfbZ9dvLmtBqkAH 5LQj+9bqcFELCbw+Kb1AkRK12DjG5b/Et7wyAv4E= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, KP Singh , Florent Revest , Mimi Zohar , Sasha Levin Subject: [PATCH 5.8 225/633] ima: Fix NULL pointer dereference in ima_file_hash Date: Tue, 27 Oct 2020 14:49:28 +0100 Message-Id: <20201027135533.237888356@linuxfoundation.org> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201027135522.655719020@linuxfoundation.org> References: <20201027135522.655719020@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: KP Singh [ Upstream commit aa662fc04f5b290b3979332588bf8d812b189962 ] ima_file_hash can be called when there is no iint->ima_hash available even though the inode exists in the integrity cache. It is fairly common for a file to not have a hash. (e.g. an mknodat, prior to the file being closed). Another example where this can happen (suggested by Jann Horn): Process A does: while(1) { unlink("/tmp/imafoo"); fd = open("/tmp/imafoo", O_RDWR|O_CREAT|O_TRUNC, 0700); if (fd == -1) { perror("open"); continue; } write(fd, "A", 1); close(fd); } and Process B does: while (1) { int fd = open("/tmp/imafoo", O_RDONLY); if (fd == -1) continue; char *mapping = mmap(NULL, 0x1000, PROT_READ|PROT_EXEC, MAP_PRIVATE, fd, 0); if (mapping != MAP_FAILED) munmap(mapping, 0x1000); close(fd); } Due to the race to get the iint->mutex between ima_file_hash and process_measurement iint->ima_hash could still be NULL. Fixes: 6beea7afcc72 ("ima: add the ability to query the cached hash of a given file") Signed-off-by: KP Singh Reviewed-by: Florent Revest Signed-off-by: Mimi Zohar Signed-off-by: Sasha Levin --- security/integrity/ima/ima_main.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/security/integrity/ima/ima_main.c b/security/integrity/ima/ima_main.c index c1583d98c5e50..0b8f17570f210 100644 --- a/security/integrity/ima/ima_main.c +++ b/security/integrity/ima/ima_main.c @@ -531,6 +531,16 @@ int ima_file_hash(struct file *file, char *buf, size_t buf_size) return -EOPNOTSUPP; mutex_lock(&iint->mutex); + + /* + * ima_file_hash can be called when ima_collect_measurement has still + * not been called, we might not always have a hash. + */ + if (!iint->ima_hash) { + mutex_unlock(&iint->mutex); + return -EOPNOTSUPP; + } + if (buf) { size_t copied_size; -- 2.25.1