Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp593729pxk; Wed, 16 Sep 2020 11:40:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxhI88RWvkJz0rjGMfz/09pQWn+0kNvli1GsDTeyu1jwSRf33hpHVCUJJI2GAfpxwuQy0kM X-Received: by 2002:a17:906:5611:: with SMTP id f17mr28391550ejq.427.1600281656378; Wed, 16 Sep 2020 11:40:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600281656; cv=none; d=google.com; s=arc-20160816; b=n6wCCOjjqXK8MvfBkwT5uhCCu5M78P2atvIBOooHOPgmJ3Kz0gvn0qnha5ZYPFlLqW BRLi2mMTlJp7Liyh8w1+udbcAKYye/OMsELBMUTzGI/0e3xKWy2/xVicbF/sIJ2P+7nF 4lKz2LhArl5Wasmzp0EsYpjgKNVmtpST2ujqB/AWvS75zzxwLISqAtG6TFgYAd4Jyf3O LyBN9w7NfK+dM6MikGEar3viCuZGhf+O4AhNk8k57GCvgSikPTpYhrC8SyAn3aaLE8Q8 mUQJRbl1q3RyUjAA0Qf2KDxhAKdouJdjwEBzZS2ZdNvp4uDKgdtFh1U9xQLxpY16Ic3z O6Jg== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=Rp7Xhs/bBt9D8tUMlvjIoqHMOsBqD8IYyxiK/2FKKG8=; b=NJpBY7u07vM0W5KEgIyWfjWOW2m1JEvPGNyZVrAUCzZ/3hN1Y8zS7fvSd05UkdX4KX 7kOyhI11GotMRpoiTmd2VOhJPJcbmzTURtCgcCTEmh+e1VwQ4VYmW3JLc6J59n6Vh3S5 X6HB5BNgpcrjyObeX013+oqIAKq52h29v831EILKodJ5KZAkza2jmw/i1k+g0pywJDoD hNwKXq+dgCywWPL2b7Gf5GAo4ItDbla7q60vNqp73psKNkBovm/vw1bBFZoKE49Yly9L uhKw/7tZJzgW9+4wWkbq4Zn8Bfg7Slh4211mRLgCyxojkSKZkhBlmLO16nAKrMLDK59p Ko+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=NzuEqJEa; 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=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id md14si11955512ejb.548.2020.09.16.11.40.34; Wed, 16 Sep 2020 11:40:56 -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=@chromium.org header.s=google header.b=NzuEqJEa; 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=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728142AbgIPSiM (ORCPT + 99 others); Wed, 16 Sep 2020 14:38:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727694AbgIPS27 (ORCPT ); Wed, 16 Sep 2020 14:28:59 -0400 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A4BD8C035437 for ; Wed, 16 Sep 2020 05:06:59 -0700 (PDT) Received: by mail-wr1-x443.google.com with SMTP id e16so6673178wrm.2 for ; Wed, 16 Sep 2020 05:06:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Rp7Xhs/bBt9D8tUMlvjIoqHMOsBqD8IYyxiK/2FKKG8=; b=NzuEqJEaQL8fg8E+ugG0EvQ+ISnYy96VOTeyqJ2H4egEbK1//gM37ymiqQDoNLVucw ZHKgF8zoZS4sE+eFUY5nU3d0PGYihU3kowLoGaEPHhj2oy4IPrVBPyh1462yd5rznFvZ 2PtEI+LhLapu6mgrPLWGZVRDWX7X4PLtZb42I= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Rp7Xhs/bBt9D8tUMlvjIoqHMOsBqD8IYyxiK/2FKKG8=; b=dyFNWWL9kPJ5h4oOIs5e/pBu78q4lTpRVn1ftUK3/z7/DrCYnm4b2C5g35eUsxL68H V5mIeg0c3SS0psmybokm0MpmyPTLqhrKzlukozugGNVp5Tn/1Cw2/rj+rTCgH3BL8yKr H6GJKLjj43H/DOIBLKDFHM4YiBAaAAyVnkqXuOCegiFlPGTOd8Fs3G+RZ4PMEp27Ergx UYAJWZ0IcJ9eod4N1ahIPTIdK1hnBqcOJGIfKpPjA6aJ5uNfp1RP7eTvwmaugRGSza13 gOEMqq1eRDteqVFYE9lLYNdNy2cDLX/3RE4QciN7tn5ylsfjnZxGeN9NEMsniwc5+BA5 HlLg== X-Gm-Message-State: AOAM530h6MBU/citmgdjiVsurPHZB2FpDIo+nIHNfYhN5SPHd1cPk2Lb FMhM8wCUHnt/yttOckv7oX/mLxKqCiGS/1Jd/xE= X-Received: by 2002:adf:dd0b:: with SMTP id a11mr25833458wrm.422.1600258018090; Wed, 16 Sep 2020 05:06:58 -0700 (PDT) Received: from kpsingh.c.googlers.com.com (203.75.199.104.bc.googleusercontent.com. [104.199.75.203]) by smtp.gmail.com with ESMTPSA id h8sm32042888wrw.68.2020.09.16.05.06.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Sep 2020 05:06:57 -0700 (PDT) From: KP Singh To: linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org Cc: Mimi Zohar , James Morris , Kees Cook , Lakshmi Ramasubramanian , Jann Horn , Florent Revest Subject: [PATCH] ima: Fix NULL pointer dereference in ima_file_hash Date: Wed, 16 Sep 2020 12:05:48 +0000 Message-Id: <20200916120548.364892-1-kpsingh@chromium.org> X-Mailer: git-send-email 2.28.0.618.gf4bc123cb7-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: KP Singh ima_file_hash can be called when there is no iint->ima_hash available even though the inode exists in the integrity cache. An 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 --- 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 8a91711ca79b..4c86cd4eece0 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.28.0.618.gf4bc123cb7-goog