Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3392190ybl; Fri, 20 Dec 2019 08:32:50 -0800 (PST) X-Google-Smtp-Source: APXvYqwieE0WTCnB+KCyAP6tHNxhx9e5TtT+NST4W0IzUqNOLtcqc+CqMoqL5MrPjTbHd5dPg8v1 X-Received: by 2002:aca:cdd6:: with SMTP id d205mr4072675oig.90.1576859569986; Fri, 20 Dec 2019 08:32:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576859569; cv=none; d=google.com; s=arc-20160816; b=w54+ioNnahVNo/fH5N/vLU8Fou5/9rQopJ5Y86UOM2sqNJvv8TS5HAxR8+aUmooGaM hHhS/Oz0DLIgIqsVz45D8mPe+yaV+gdOSd5l99/AfThh3cvTAgMrR9CoeG9KTV71mufq rX6rWJv3bxETpD6kli0IXZ4HK/r14ieatS3P+0fclSwp70sd/a4YO/kTk0E1Rq3hPKxX 4kUsigb656sJDEE0x68OfYCRw9FpGbS8cumVGa736OOruWyaYcZXdNgqbkA+TpO0rxI2 +ujevlndduon0PuFduylmyylx3BLkJqpPwDRLDtRsLlQYpiodx1jw62BN6NogNX7BMsP g0kw== 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=JEPKTWFBgqyz+hVI4T+K0RwVC4SXQz3XQWTyqv52qe8=; b=yhhyGJeMFz6Gl+rflCrxVp0vfT0FLcmodKxKOcPOg+cXZnDpNsLnVyiY2G+LD90chK 5Gn4THgraiU+V+tZBY1+LJEqOKxgVnB13YfA+IAjo379byo/AYSFW37KoRkSs6lZ4eIj M3NRPKVNofjY4S/N0R5EWfOmIEamUU4Qo7BYa0VUXQkQgm1ohd/5sEYB2mUJRl43chWl JsGxYEGj9L5Xtgs0G0R0tgmd1+ZalJgs2OlTUpqJO4URlcW8oedkiZ9i+cXtuSd+rlaQ a9sjZia/CN4wERyurkFbQ7P3bty5Tk96U8JrYfMu0sKNtITYV5RULMCm2HHLV0ONiXgJ bLTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=W5IZsAhY; 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 d5si5073760oij.139.2019.12.20.08.32.37; Fri, 20 Dec 2019 08:32:49 -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=W5IZsAhY; 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 S1727421AbfLTQbm (ORCPT + 99 others); Fri, 20 Dec 2019 11:31:42 -0500 Received: from mail-wr1-f66.google.com ([209.85.221.66]:40527 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726808AbfLTQbm (ORCPT ); Fri, 20 Dec 2019 11:31:42 -0500 Received: by mail-wr1-f66.google.com with SMTP id c14so9985540wrn.7 for ; Fri, 20 Dec 2019 08:31:40 -0800 (PST) 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=JEPKTWFBgqyz+hVI4T+K0RwVC4SXQz3XQWTyqv52qe8=; b=W5IZsAhYOcgC9qWUZYKC01gRCHi+E98qMCuJ/gpkCo6A1zL/IlfRHzDjgbgPxE32VK Lj18G/l5Tmlan2jDE+DlrH3dYb0dLa0PyZvI0Yny3B0a14NKr7NBKVoBIs7ldxNFnYBk 4A9x3WSvrnyIiB+NFpEEfVCPUEA0cPNQjfP24= 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=JEPKTWFBgqyz+hVI4T+K0RwVC4SXQz3XQWTyqv52qe8=; b=ZKgoEbrySdDrDVSeQHpEUqPo82BfpdNvOoB9sd9RHrg7XlmMu2NaBrVvPpJgOwbock AtIp311O9yRC7Gqs7P0y6enhqHHxNT4Q8YEgDj3znlYJTL7GZIyjJ4ptm4BJGjKrGIDS bZRF9JgJVn/754WErtrkrf2AFXMUmxVjiWu68mD6LU6O/u53/i+iappsTK6FyRSDv+7N bZDiDDYx1Tfcpquw79biDQz491VJFeBmLzqAEiwK8ELl9sqmC61rTJ5G0lEyCqktrUE2 rKFdjO8SwgJ+XPFKUn6ymTwd1sXWe9nF3Nu+I81N05Q73SY5MGpl+SsnST5kfis9Usww ty3A== X-Gm-Message-State: APjAAAUdM2eK3zaP6x9MkMHhxIlqhNA2/QDJROyUqgPRO6+hiB7k32pY /5gZPU1kzmrKF9Zan8uvzUmQ7w== X-Received: by 2002:adf:ea0f:: with SMTP id q15mr15795873wrm.324.1576859499726; Fri, 20 Dec 2019 08:31:39 -0800 (PST) Received: from revest.zrh.corp.google.com ([2a00:79e0:42:204:51d1:d96e:f72e:c8c0]) by smtp.gmail.com with ESMTPSA id e6sm10165522wru.44.2019.12.20.08.31.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Dec 2019 08:31:39 -0800 (PST) From: Florent Revest To: 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 Subject: [PATCH] ima: add the ability to query ima for the hash of a given file. Date: Fri, 20 Dec 2019 17:31:36 +0100 Message-Id: <20191220163136.25010-1-revest@chromium.org> X-Mailer: git-send-email 2.24.1.735.g03f4e72817-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: 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/ [1] https://lkml.org/lkml/2019/9/10/393 Signed-off-by: Florent Revest --- include/linux/ima.h | 6 +++++ security/integrity/ima/ima_main.c | 41 +++++++++++++++++++++++++++++++ 2 files changed, 47 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..f054ddf4364e 100644 --- a/security/integrity/ima/ima_main.c +++ b/security/integrity/ima/ima_main.c @@ -445,6 +445,47 @@ 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, 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 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; +} +EXPORT_SYMBOL_GPL(ima_file_hash); + /** * ima_post_create_tmpfile - mark newly created tmpfile as new * @file : newly created tmpfile -- 2.24.1.735.g03f4e72817-goog