Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp21627102ybl; Mon, 6 Jan 2020 08:12:35 -0800 (PST) X-Google-Smtp-Source: APXvYqwpA/zFLjjgYF9Y55aPEfIeECf31XjXdN0gY0R6YdqO6wozFJvmxJySOlnTK6xPDFYpN6Wi X-Received: by 2002:aca:a811:: with SMTP id r17mr5883876oie.59.1578327154917; Mon, 06 Jan 2020 08:12:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578327154; cv=none; d=google.com; s=arc-20160816; b=PVL3HappNPixGRK/DcJAeBolgv0AAnZXLl1y79hoQ8OjY8n7zo8C0DDk507Po0XMet h8pTTzhVXDsue954IlFLMoGJ29gB6C1426+Q2I2urC9q5RsAWvy7GQynjzFaqa8TLIW4 JlMEQHBWQUiWpafNDAEXIK9iSVgzzSVvgn7G9dO8iu7Y/Omq8QZgz1ANcm06AHZ4JuJe nUIxXa2NYJjG9Kg2jDWLRoQuW02znwDqX7i9lhC/brejDcaKE2+kaJ8UIuBnnhhsEu7F 0BJ2CMtB/E0x1URPIPxORsFuxpugT+7lGd2T686M10VMnlOM+Z9wDTVsOIDBq/klu7VL MIIg== 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:date:cc:to:from:subject :message-id:dkim-signature; bh=lKFIQHaKUHD2UmFMf44CL03yjerDzDCte0G5za7Hzaw=; b=v4JL3+OLTpEFNAhvK/XiWiorapNmrg7Y3NA/VFRJwpqk9fSNuxfk8ypk4MV5y3jfXg 56lUXTcflPSfJY7kNiz7/5YSLDHQ4wUWYEwKhE838rncAgpaJycDAY8ao30rwqBzUuL2 KMiZqZs5/YImTzfPssTynkvxHyVVgjJQ3xrtdV6tKzMlBDsL+/yfvMjkutwFrVbniPzo zJf2T2T/nLU137jlbC363Zv403DWdOBLZSBMQiLFCkYIEOjOYRmv2u3g4UInudWBJWY9 MiLy8XrQz7EFq1PA13tOeIpyzjqazJB76X1X+if5eW+yIqbS1Eoew0bcPynT6346uofV W83A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=gYN8qm9g; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k7si32038080oib.115.2020.01.06.08.12.22; Mon, 06 Jan 2020 08:12:34 -0800 (PST) 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; dkim=pass header.i=@chromium.org header.s=google header.b=gYN8qm9g; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726659AbgAFQKj (ORCPT + 99 others); Mon, 6 Jan 2020 11:10:39 -0500 Received: from mail-wm1-f66.google.com ([209.85.128.66]:40259 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726448AbgAFQKj (ORCPT ); Mon, 6 Jan 2020 11:10:39 -0500 Received: by mail-wm1-f66.google.com with SMTP id t14so15810305wmi.5 for ; Mon, 06 Jan 2020 08:10:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=message-id:subject:from:to:cc:date:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=lKFIQHaKUHD2UmFMf44CL03yjerDzDCte0G5za7Hzaw=; b=gYN8qm9gfI4NDVj7y1aSRpVDDuB6bfgMDg0dGrfKkOZz8EwVE2DIR3ZiHKZ/Vb2/81 ocxy5ZzV7c/CbFC5W7RV0kU8jLCvya4o9+5hG83RRMHLv6LX+6j3xtvhwmUyHdLyvYzF Rr7XG6qC/KXrU7sht5+bHIyepl50iz8aoEQKc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=lKFIQHaKUHD2UmFMf44CL03yjerDzDCte0G5za7Hzaw=; b=VNimUOQGURdz59SjoxoYkTueJtvA1jWFLM23WcfSEbKvAIbKeUv7suDe+euHVd3G35 rMpS0h5aW0SvTfc9wmyn5q/AZtF7ZuCNI0mFsnC2kRqZOLexrtInR3DQrR6kRLSz/1GD 3xoe+oj4ms5z1RRNXt138BBQA1i/LdUo69rQ64/qKl8MjX81EAk2DMGi9w63w9lKV8ny AYC0BSdb/0OZsNO3SpOrcBFn3DxXNkIHGYe/cb7SLi550repH7enHOzTNVJTm68raPQd 99FLJV+cDhJogXSMZpVz/Ptcn3JzO6Q3z4lawcTQslPJ/SKsFgz69UBg3UcEpGCpMpjR NJLw== X-Gm-Message-State: APjAAAU5aJKqU+gtivTjKdC7whco+n6j7MUCOgFrrvECh4Y40A+r7j3/ OWWGC78LB+a0lH+P3o8/v7asKA== X-Received: by 2002:a7b:cbc8:: with SMTP id n8mr35083327wmi.35.1578327037092; Mon, 06 Jan 2020 08:10:37 -0800 (PST) Received: from ?IPv6:2a00:79e0:42:204:51d1:d96e:f72e:c8c0? ([2a00:79e0:42:204:51d1:d96e:f72e:c8c0]) by smtp.gmail.com with ESMTPSA id i5sm72940135wrv.34.2020.01.06.08.10.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jan 2020 08:10:36 -0800 (PST) Message-ID: Subject: Re: [PATCH] ima: add the ability to query ima for the hash of a given file. From: Florent Revest To: Lakshmi Ramasubramanian , linux-integrity@vger.kernel.org Cc: kpsingh@chromium.org, mjg59@google.com, zohar@linux.ibm.com, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, Florent Revest Date: Mon, 06 Jan 2020 17:10:35 +0100 In-Reply-To: <8f4d9c4e-735d-8ba9-b84a-4f341030e0cf@linux.microsoft.com> References: <20191220163136.25010-1-revest@chromium.org> <8f4d9c4e-735d-8ba9-b84a-4f341030e0cf@linux.microsoft.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.30.5-1.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 2019-12-20 at 08:48 -0800, Lakshmi Ramasubramanian wrote: > On 12/20/2019 8:31 AM, Florent Revest wrote: > > > > > +/** > > + * ima_file_hash - return the stored measurement if a file has > > been hashed. > > + * @file: pointer to the file > > + * @buf: buffer in which to store the hash > > + * @buf_size: length of the buffer > > + * > > + * On success, output the hash into buf and return the hash > > algorithm (as > > + * defined in the enum hash_algo). > > + * If the hash is larger than buf, then only size bytes will be > > copied. It > > + * generally just makes sense to pass a buffer capable of holding > > the largest > > + * possible hash: IMA_MAX_DIGEST_SIZE > > If the given buffer is smaller than the hash length, wouldn't it be > better to return the required size and a status indicating the buffer > is not enough. The caller can then call back with the required > buffer. > > If the hash is truncated the caller may not know if the hash is > partial or not. I agree with Mimi's answer that the caller would know based on the returned hash algorithm. > > + * > > + * If IMA is disabled or if no measurement is available, return > > -EOPNOTSUPP. > > + * If the parameters are incorrect, return -EINVAL. > > + */ > > +int ima_file_hash(struct file *file, char *buf, size_t buf_size) > > +{ > > + struct inode *inode; > > + struct integrity_iint_cache *iint; > > + size_t copied_size; > > + > > + if (!file || !buf) > > + return -EINVAL; > > + > > + if (!ima_policy_flag) > > + return -EOPNOTSUPP; > > + > > + inode = file_inode(file); > > + iint = integrity_iint_find(inode); > > + if (!iint) > > + return -EOPNOTSUPP; > > + > > + mutex_lock(&iint->mutex); > > + copied_size = min_t(size_t, iint->ima_hash->length, buf_size); > > + memcpy(buf, iint->ima_hash->digest, copied_size); > > + mutex_unlock(&iint->mutex); > > + > > + return iint->ima_hash->algo; > > Should the hash algorithm be copied from iinit->ima_hash to a local > variable while holding the mutex and that one returned? > > I assume iinit->mutex is taken to ensure iinit->ima_hash is not > removed while this function is accessing it. Ah! Good catch, thank you :)