Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3743789ybl; Mon, 13 Jan 2020 01:44:55 -0800 (PST) X-Google-Smtp-Source: APXvYqyHxal/ToDXO1dbd+WWBxfzgUKR77T4VDtAfw5P/Nt1XmHxwdOxSylibnD3KMrTE9x2cJYw X-Received: by 2002:aca:814:: with SMTP id 20mr12258789oii.159.1578908695082; Mon, 13 Jan 2020 01:44:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578908695; cv=none; d=google.com; s=arc-20160816; b=SmnvBjsWERmZdiXG2mpVovbvxxiacfiUvG4++U8JtXwsS8rqP/uT6mT5qXPVq1CiMN GkSTEILBx9tyois3MZjDlc7xbm/fZLu+FRPj5k93ortckqJhQEco/YQMkzo7tcGOzq9n 2+VA4C8IyqhB51eWmona4Db1A4S9U2k1SlYKO+RljJgZppaFvM90m58OY6RDWZx9Z1NA cUyQpW42d2h9qHsBYSl/G1GOcJCZpG5IMNDZrSP/gXTvnMVaMONF34NYVZc4QMEnEWLg QrBXGBb7Zy1Jj1YKMnjPYq3bFTaJDEUWn/TWjO1PoHKN6fRv4InktXVUSB14sTWvzuNS P/9A== 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=/AnYXrXWYvNhwGCXjTJAreL3RYx7ZKU0okhiknvPq1k=; b=tDd+XZiXni+R1lXEPldOQhlwPsDnDl/RPF2L1PeSZ7sxQc7bLTX3MXrrfvml/YrHhg xZV2UM4y3T6cLejURw0OOopq6FpnuWpt7goRMM511a6OxQmgZcLxt0PUhi5paz3lRQdW JC7pPDyUpGYis1IcqQT3O2BbBcVh2PTtgVuYN2+CDRpoHAInEe5E/AwmEzsrYDGNUuaR TA7x4b4KbBEbrgTCpy3x5oXbK0Sknrp8ncFUzU9YgzIwjLjDfpHk/2wROTY3ZlXWNy6u 2RIgd37lQlyECIKwgaeuzp+qK2Tta7G26i87Kvu/Fzb7dSnZ6+Kl7wcbvTN2EcrpAMz1 FPPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=LONkt8qU; 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 y127si5191466oiy.250.2020.01.13.01.44.43; Mon, 13 Jan 2020 01:44:55 -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=LONkt8qU; 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 S1728760AbgAMJmW (ORCPT + 99 others); Mon, 13 Jan 2020 04:42:22 -0500 Received: from mail-wr1-f68.google.com ([209.85.221.68]:37234 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728746AbgAMJmV (ORCPT ); Mon, 13 Jan 2020 04:42:21 -0500 Received: by mail-wr1-f68.google.com with SMTP id w15so7814483wru.4 for ; Mon, 13 Jan 2020 01:42:20 -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=/AnYXrXWYvNhwGCXjTJAreL3RYx7ZKU0okhiknvPq1k=; b=LONkt8qUSVzgSpJ3vYRz5tdEdUTiIp7lBSpk8CgEbr+mU66cFuYcwYwY0Gt33ed63d Za4k2QokkwkSZrYzZ458SxLbyR6U5j4nYTd1ZGpkw/SI17H4TkFXcEXQ4D3KzgxNTSn+ bxLdeS1EG1MKd28ac5FKeVtvQ/x1eACkxMXNg= 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=/AnYXrXWYvNhwGCXjTJAreL3RYx7ZKU0okhiknvPq1k=; b=FiBRcR0Gmkh2q65ZGTafRp7YuhtLB49B81/VjKsSwbr87YzdrvU6A32dr2JHx9oLPr wOuYpl+ZJgYolf8c3yZQoBYuLp4lKhsr4JSxzw29pTCo17BI+3Au+gjHf7WeBBjiBZaZ SbEQff9gqT/v1vBO4+nUQotIwmVi1Vo5rf05BQlnQUlKkaWrYYgzGzZphciGECsq8jke dPh7WmnLKoAvOSHvh5CFZg8ugneIl+GpsmCcfbpu/zXV38wkmW5FtdGFM2RfY3JnQe3G fXBPTUA3TqjRiR2oqCDuuTnQibwBb0FjjWa0mhm5fBD7lVLnxT/33SmAlGnGbQO0DtMX 7UMA== X-Gm-Message-State: APjAAAXU7tPBjTR6VJNsYbLivh5naZ73vhThytE9OjL7o0XGPqCaxqjy jhhyGEUeoOiK3J2QLByPm7QOfQ== X-Received: by 2002:a5d:608a:: with SMTP id w10mr16362892wrt.136.1578908539527; Mon, 13 Jan 2020 01:42:19 -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 e18sm14474206wrr.95.2020.01.13.01.42.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jan 2020 01:42:19 -0800 (PST) Message-ID: Subject: Re: [PATCH v2] ima: add the ability to query the hash of a given file. From: Florent Revest To: Mimi Zohar , linux-integrity@vger.kernel.org Cc: kpsingh@chromium.org, mjg59@google.com, nramas@linux.microsoft.com, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, Florent Revest Date: Mon, 13 Jan 2020 10:42:18 +0100 In-Reply-To: <1578499556.5222.157.camel@linux.ibm.com> References: <20200106162524.164650-1-revest@chromium.org> <1578499556.5222.157.camel@linux.ibm.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 Wed, 2020-01-08 at 11:05 -0500, Mimi Zohar wrote: > On Mon, 2020-01-06 at 17:25 +0100, 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. > > > > This is based on Peter Moody's patch: > > https://sourceforge.net/p/linux-ima/mailman/message/33036180/ > > FYI, but unlike the audit log/IMA measurement list, the iint cache > entries can be removed. Refer to security_inode_free(). Perhaps > mention of this difference should be included, here, in the patch > description. Sure, I added a comment about this in a v3. > > [1] https://lkml.org/lkml/2019/9/10/393 > > > > Signed-off-by: Florent Revest > > Assuming, with the above difference, you're still interested in > having this feature upstreamed and addressing the comments above and > below: > > Reviewed-by: Mimi Zohar Thank you. Yes we are still interested in this feature! > > --- > > include/linux/ima.h | 6 ++++ > > security/integrity/ima/ima_main.c | 46 > > +++++++++++++++++++++++++++++++ > > 2 files changed, 52 insertions(+) > > > > diff --git a/include/linux/ima.h b/include/linux/ima.h > > index 6d904754d858..d621c65ba9a5 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 d7e987baf127..3799b6c6c3b8 100644 > > --- a/security/integrity/ima/ima_main.c > > +++ b/security/integrity/ima/ima_main.c > > @@ -445,6 +445,52 @@ 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. > > + * @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. > > As of Linux 5.4.y, IMA support for appended file signatures was > added. Should we indicate that the file hash returned is based on the > entire file, including the appended signature? > > Mimi Of course it never hurts to add a comment. :) I'll send a v3 with this added. > > > + * 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 > > + * > > + * 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