Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp664838pxk; Wed, 16 Sep 2020 13:47:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy4TYu8tWmpHSGWFfAfSdqVusXhBq2kW7fI0ywwJ5GhW7scRG1d/z6sTlZ93Xr5IZbbqNs7 X-Received: by 2002:a05:6402:1641:: with SMTP id s1mr30622736edx.66.1600289241278; Wed, 16 Sep 2020 13:47:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600289241; cv=none; d=google.com; s=arc-20160816; b=KzTs6/t1IHszM7OJc1yjM6aNc6e02V9U4n2B10fYCMEBk04hhFDjozR8qYQfxdLWjP a1n9HrwQ2YckV1Cm7mQYHyXRLP8CBpewuQBqJ88hMLLochMJSFx8HaXzOMn64yFzzFWB X1GiyBQLTYKKAxKmT5ava3fv12DR5dOIYthKkOJ10VtJAO4lb+sX5ugClhNM39ibZ7Hw TsLlXlN7qJkQ6088kQIZkgeX1RKdMnyMiOnd8Pm/vE84EB5OTg079kYfK0DUk97HLT/j DyRlIzhMZgUnUXUdrRxqZbI5i2kDbMzsy65g6PvhCS/CtIe2tD6tgMIxPjCKva2AezKW Zsxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=0Ak62nesCbOREDTTzpc8X9wLA7URHRevPKbppowyS5I=; b=XXEidM4q8M4esCWTi55owJgGu4W6zfcs0FtLnT8/tO/1mzL+BXfiSbwPlNphQ9C5gK JLd+TPTxUWOulvjr94i/vIK5iLncDTu8h4Mwre+uIsvVD6y0sozLqXTM2PLo61Fyz7ke 5EsX6sAfwq47JkqS+UM4sp6y2/JVoQQdFEoUQeR7pOy23jKziLFy+8rhAquwM97SS1j2 b5buUXh4WM4Tu994Jr5ctvthYYmmim/HlnZOK+3lhXdrGYRMkZQDGYPgEjx9D+oGSXb/ nHLh85994mE04pxKmIv4U3EWpNasgaUMTPG77QE9UCXWMJzdMcts21aY9zONLYbxSW5l dl+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="F+8NDuo/"; 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 j22si13012623edq.572.2020.09.16.13.46.57; Wed, 16 Sep 2020 13:47:21 -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="F+8NDuo/"; 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 S1728382AbgIPUn4 (ORCPT + 99 others); Wed, 16 Sep 2020 16:43:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726671AbgIPRD1 (ORCPT ); Wed, 16 Sep 2020 13:03:27 -0400 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C354EC0A888E for ; Wed, 16 Sep 2020 05:48:00 -0700 (PDT) Received: by mail-wm1-x344.google.com with SMTP id q9so2667173wmj.2 for ; Wed, 16 Sep 2020 05:48:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=0Ak62nesCbOREDTTzpc8X9wLA7URHRevPKbppowyS5I=; b=F+8NDuo/1RWe7wgG2WhonViKgW4VIrYKG0fuQMdu+s7s5fBfnou/S/twaeShYbWsMb SpuEoGcFcKnM/hRqmfdKUbO45bKl7Q1S7ixO2QN1hVzhYdN/ZC6KlkmpSJBXli3nrJC4 C8wZha5ZkpFJr1yLUzyj/IveBKWMSQSs7Tpis= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=0Ak62nesCbOREDTTzpc8X9wLA7URHRevPKbppowyS5I=; b=nsFyvUH3ehJspeG5ucMhqD8BmTL2zO0bn5vPYPRCUGDyn8aNI7tW+G9gAhijc9kytV bMc0OsFwJbbWFysTxgDGhidHiHkt5UVkkQtzlCQYQeRGlg2W49AkQ1q+442Z+n5g/HEQ tMWYwOrKrXLKLfgNkh/zZiLi92jqxJj3/XTh+0/vmTDutLDmUIdlUPb+yhKLNDlohoEA V9cFt0GYQLSEUR1ub+SpIFH6NAK2TPV3fFavl7/WiHXU1CoINONZXiJ7R0EdtWklz1b6 eIRI+P8loWkxzckBF0qjj85LQFQqcd5tHFMxJDrxDSD8/beixk+lFPlcU0BiyyvatyUj QR8w== X-Gm-Message-State: AOAM5337P2y6EtgzGiUSXKJeyZut1HZaQm41zG1Rb/Hp3TAgRS1ORIaX UX5dBbDIn1sQrp2Wp7vLPlLcftEr+WAx86BSdXc8Ag== X-Received: by 2002:a1c:1902:: with SMTP id 2mr4874239wmz.26.1600260478858; Wed, 16 Sep 2020 05:47:58 -0700 (PDT) MIME-Version: 1.0 References: <20200916120548.364892-1-kpsingh@chromium.org> <5ab256148e8ad5f9882e888dac809bc72cd3fe66.camel@chromium.org> In-Reply-To: <5ab256148e8ad5f9882e888dac809bc72cd3fe66.camel@chromium.org> From: KP Singh Date: Wed, 16 Sep 2020 14:47:48 +0200 Message-ID: Subject: Re: [PATCH] ima: Fix NULL pointer dereference in ima_file_hash To: Florent Revest Cc: open list , Linux Security Module list , Mimi Zohar , James Morris , Kees Cook , Lakshmi Ramasubramanian , Jann Horn Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Somehow this patch does not seem to have been picked up by any of the mailing list archives and I am not sure if this was delivered to the lists. I will send a v2 and add Florent's Reviewed-by tag. On Wed, Sep 16, 2020 at 2:41 PM Florent Revest wrote: > > Reviewed-by: Florent Revest > > On Wed, 2020-09-16 at 12:05 +0000, KP Singh wrote: > > 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; > > >