Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3798996ybl; Mon, 13 Jan 2020 02:52:12 -0800 (PST) X-Google-Smtp-Source: APXvYqygz1qjwoc27zxYtcewa7x/z4SY8YGX+eLwLuO2AB7duKndfaiw3aq23+0mgWHp/gw5Dd0O X-Received: by 2002:a05:6830:160c:: with SMTP id g12mr12141189otr.82.1578912732704; Mon, 13 Jan 2020 02:52:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578912732; cv=none; d=google.com; s=arc-20160816; b=r4zPMAWlbe7dxIrCr3LROoKE9LxbsgJAAbEArqr2iG4L18C78WF4OxQiXoDumYjIXW yiFEXXNe5oxIKX+vqY7w56M1PrGtByrAfoaDf2SlwTjfsWdiXpwyWuhL8rQs9pePSal/ g8BhRp5BmK/5g70WpuGhIXc/htzZVtITGOHx58iIpxDRS8dQ5WsKjnlVhO4YvI11tA2h zzjK32uLcTBXtNP8/hMUdgYP5udEqwtcmIJ21tCkkioypf+dxPTMVVzw3xPSRTaK5Be4 oCriclPkUhq9Ls1CibwVfnr+3EVCOfC2RxRJaX6sGjXisLA5kFW8uLiUn87bzangbhbw dahA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:date:from:dkim-signature; bh=rX6hYkQTcPkxFSQTp22iI/GTbRzDTSn2mT2N78MPltg=; b=slrF65Mb2saYXfSlUh8dsjTxf0m28U7Z0XeD5+4tR69Dc/q0lowgRWw5ILbGKKISS3 Jc1OU3zLSTH9L2LoDXeqjRqxzC6cUC7WgaiUep8uIglZKkPVlqHN4b7eDYBwg5miTVUy HZnX8vt/KNxT4M2eraTLq76RqgjbZshzno1aWDqsAP/R33bARbjGsnsAEj4aA0AmqDX1 5GjC5vs+hcX7pMilPKhBwiJ1xwsJDR0r05U1a87mEOC0uh6heXihAlqxqIb2REb6tsxq togJDgTESSSXqPzdUiHnkPV64o/W2kKmoDzHgPKFKJ84KoSNnwnNazp9zfpsWD8ko+Is GDiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=oKkpYKtv; 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 q1si6711488oti.234.2020.01.13.02.52.00; Mon, 13 Jan 2020 02:52:12 -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=oKkpYKtv; 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 S1726523AbgAMKvF (ORCPT + 99 others); Mon, 13 Jan 2020 05:51:05 -0500 Received: from mail-wr1-f68.google.com ([209.85.221.68]:34980 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726109AbgAMKvE (ORCPT ); Mon, 13 Jan 2020 05:51:04 -0500 Received: by mail-wr1-f68.google.com with SMTP id g17so8038110wro.2 for ; Mon, 13 Jan 2020 02:51:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:date:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=rX6hYkQTcPkxFSQTp22iI/GTbRzDTSn2mT2N78MPltg=; b=oKkpYKtvG22DswPrnQPe4mDphOZzPeWS9Fm/gBtPFXIi/eh1IzHY8X22AlrD5VKaYE s+qOTZJe21UPh7e2M2/VVXjI1lW86ypWRIgklVRlnZXV2m3TNbBYjdeCk1bOikndC3V+ aTL2Zqv9kyPeC8epB3SLemTlwjlE2X2XmGN7A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:date:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=rX6hYkQTcPkxFSQTp22iI/GTbRzDTSn2mT2N78MPltg=; b=lNocnEXp6Q3E9QAsmhk+MF5EuzZGOvP5MhyUXwSJrrQ5vnY6Mm0BlRfWz47puiKnpu tTYizR3z0UGU+BNaqrxNdyosfL9b1wy92P2ECKM+KZpihnyG0mG5yr6I5NrIQrQoXMP3 iEKNg61y3PUS6UjkkRepqC7U8C+FpDx6RlNLS31NGBGXpUGMPl9HLKKwku4ODXf1k4KH h8HHt99AdjLI9UN5HA0cpiEH03RZ7BG0IuNwXH/DNjdRrV9EeqL/8FJZLlachLO+tAS3 4JGtKlWAc0CfbqNkv/UyhrTzUCLMjdd6uM3/FaJjHMIez91Gjma8s6rIe4iBpDJO5TEK XeRg== X-Gm-Message-State: APjAAAWJIS9IeAMU0Vu5ELiKaCwl7AP/QIY9K+J+2u1mE2LLmLLlRuuG pKzcoxQqwjjVOjISpjC0ab8yyQ== X-Received: by 2002:adf:ef8b:: with SMTP id d11mr12548851wro.45.1578912662627; Mon, 13 Jan 2020 02:51:02 -0800 (PST) Received: from google.com ([2a00:79e0:42:204:8a21:ba0c:bb42:75ec]) by smtp.gmail.com with ESMTPSA id s3sm13791867wmh.25.2020.01.13.02.51.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jan 2020 02:51:02 -0800 (PST) From: KP Singh X-Google-Original-From: KP Singh Date: Mon, 13 Jan 2020 11:51:00 +0100 To: Florent Revest Cc: linux-integrity@vger.kernel.org, kpsingh@chromium.org, mjg59@google.com, zohar@linux.ibm.com, nramas@linux.microsoft.com, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, Florent Revest Subject: Re: [PATCH v3] ima: add the ability to query the cached hash of a given file Message-ID: <20200113105100.GA31000@google.com> References: <20200113094244.26678-1-revest@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200113094244.26678-1-revest@chromium.org> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 13-Jan 10:42, Florent Revest wrote: > From: Florent Revest > > This allows other parts of the kernel (perhaps a stacked LSM allowing > system monitoring, eg. the proposed KRSI LSM [1]) to retrieve the hash > of a given file from IMA if it's present in the iint cache. > > It's true that the existence of the hash means that it's also in the > audit logs or in /sys/kernel/security/ima/ascii_runtime_measurements, > but it can be difficult to pull that information out for every > subsequent exec. This is especially true if a given host has been up > for a long time and the file was first measured a long time ago. > > It should be kept in mind that this function gives access to cached > entries which can be removed, for instance on security_inode_free(). > > This is based on Peter Moody's patch: > https://sourceforge.net/p/linux-ima/mailman/message/33036180/ > > [1] https://lkml.org/lkml/2019/9/10/393 > > Signed-off-by: Florent Revest Reviewed-by: KP Singh > --- > include/linux/ima.h | 6 ++++ > security/integrity/ima/ima_main.c | 49 +++++++++++++++++++++++++++++++ > 2 files changed, 55 insertions(+) > > diff --git a/include/linux/ima.h b/include/linux/ima.h > index f4644c54f648..1659217e9b60 100644 > --- a/include/linux/ima.h > +++ b/include/linux/ima.h > @@ -23,6 +23,7 @@ extern int ima_read_file(struct file *file, enum kernel_read_file_id id); > extern int ima_post_read_file(struct file *file, void *buf, loff_t size, > enum kernel_read_file_id id); > extern void ima_post_path_mknod(struct dentry *dentry); > +extern int ima_file_hash(struct file *file, char *buf, size_t buf_size); > extern void ima_kexec_cmdline(const void *buf, int size); > > #ifdef CONFIG_IMA_KEXEC > @@ -91,6 +92,11 @@ static inline void ima_post_path_mknod(struct dentry *dentry) > return; > } > > +static inline int ima_file_hash(struct file *file, char *buf, size_t buf_size) > +{ > + return -EOPNOTSUPP; > +} > + > static inline void ima_kexec_cmdline(const void *buf, int size) {} > #endif /* CONFIG_IMA */ > > diff --git a/security/integrity/ima/ima_main.c b/security/integrity/ima/ima_main.c > index 2272c3255c7d..9fe949c6a530 100644 > --- a/security/integrity/ima/ima_main.c > +++ b/security/integrity/ima/ima_main.c > @@ -445,6 +445,55 @@ int ima_file_check(struct file *file, int mask) > } > EXPORT_SYMBOL_GPL(ima_file_check); > > +/** > + * ima_file_hash - return the stored measurement if a file has been hashed and > + * is in the iint cache. > + * @file: pointer to the file > + * @buf: buffer in which to store the hash > + * @buf_size: length of the buffer > + * > + * On success, return the hash algorithm (as defined in the enum hash_algo). > + * If buf is not NULL, this function also outputs the hash into buf. > + * If the hash is larger than buf_size, then only buf_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. > + * The file hash returned is based on the entire file, including the appended > + * signature. > + * > + * 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; > + int hash_algo; > + > + if (!file) > + 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); > + if (buf) { > + size_t copied_size; > + > + copied_size = min_t(size_t, iint->ima_hash->length, buf_size); > + memcpy(buf, iint->ima_hash->digest, copied_size); > + } > + hash_algo = iint->ima_hash->algo; > + mutex_unlock(&iint->mutex); > + > + return hash_algo; > +} > +EXPORT_SYMBOL_GPL(ima_file_hash); > + > /** > * ima_post_create_tmpfile - mark newly created tmpfile as new > * @file : newly created tmpfile > -- > 2.25.0.rc1.283.g88dfdc4193-goog >