Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3744201ybl; Mon, 13 Jan 2020 01:45:28 -0800 (PST) X-Google-Smtp-Source: APXvYqxFbDTOgHwsqS38Ahkoq8CX+xGgtd1nstotRkwwEesebaGBIS32UdSQUhdvWqjBuMtYTWXB X-Received: by 2002:aca:b183:: with SMTP id a125mr12070615oif.83.1578908728682; Mon, 13 Jan 2020 01:45:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578908728; cv=none; d=google.com; s=arc-20160816; b=Mdb3z5g2bMbngdmpNgL8tvSnm/UEpF5Ro5h3SZQzaotR+BSE6DbLiWvPoafGWkJ/41 AkCN/MCIPu5pffCnWMOZ43HicB3MwMOH+683/T61jDpr7n31kvvhokY/QFSTQsQ9Izfd hzlRfhXNzD3K2UkAtnI6VsrYtUlj+LlZFpc5u1ANwSfJWF+57KQ0RSjK4wqBQw7b6Tbx CNvi6Vk7nIitkMYOuyyhDSYdVxJHi4KrVcPmOAqelO/nNZE9HzMOkFs4dPdCJfGKAwlY ydGHKoLLIn1V+KMo3+5Cl7WaKf+tadslYKEE14QSYKoMnTUiZssy/oHKHE4LyK8NKcIp zC1A== 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=PHx6WmDbUjJ36Jl+g3c5bGGBL+IPquArbpNqxaiK16s=; b=FudqzalieVBHCG6GsZJi+jsdBelM/+3DstCdFv/AQBan33a23H8d+0hLtBcl5fgLXJ AgMf1LF5LsFY4k0WEcMsy6NvoblrTXSiOmJ20n3Fs9Y35X9fNELG9NDhuQMIXCR3JEsZ kauhdfkLGKfrFfLOPNeau7z67rBQTn1GL1CmpXica54RoubFn9NiZDHhZahQrT2aaotP XKrrZKLDT3n+E+ivY+uocC0WkYC8QtmxSxFIB7kJkYjL67DVpvOOZ+mQGM5hpAmUtfAZ Uj8Qua/BMFsWqnOv13HBXK/zx7CcguTI92DwcNKLlUspGAK9DD1tT0zJiyCcnwIMu/Rx /MuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=EfnB1nq4; 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 l5si6411390otn.194.2020.01.13.01.45.16; Mon, 13 Jan 2020 01:45:28 -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=EfnB1nq4; 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 S1725992AbgAMJnV (ORCPT + 99 others); Mon, 13 Jan 2020 04:43:21 -0500 Received: from mail-wm1-f68.google.com ([209.85.128.68]:51760 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726127AbgAMJnV (ORCPT ); Mon, 13 Jan 2020 04:43:21 -0500 Received: by mail-wm1-f68.google.com with SMTP id d73so8844304wmd.1 for ; Mon, 13 Jan 2020 01:43:19 -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=PHx6WmDbUjJ36Jl+g3c5bGGBL+IPquArbpNqxaiK16s=; b=EfnB1nq4V68FEsTjQCUQnWpZE9rQyBa/cUPWKvQou1+R2njQCVYQzJbBt1ZPRu1rAe wY+lQNDa01pAkV0/0BAX3kRz187MIvZdfkqw3tk8tZzczQwXgbjSa0GpVYhJmh5zp6cG 2XIrvqlhqgnReKjnEqvDRvXR7EsmpHi/NFzwE= 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=PHx6WmDbUjJ36Jl+g3c5bGGBL+IPquArbpNqxaiK16s=; b=BdwVG+oluDHGfJvE1KuD1WlKEP04dg+clac1J8bodlVL9ptD/DhFzrKK+iBwxdn83D A1b6Y3YXFeXiwImj5dhWLglli339IFfFlSny7PMPLtBJiwD4B99C6TMktw0FhOzCVo6l JpPmoT4aDyt0HiKGKQyNgDkccc8JwySuLGreDTkP5wgEZrLyWdVoiKA0JaIzGcazSEgY ocF89YHLOTvTRk3R1jdX+zeRHVaqNGz0vTelcy4ts1GW9+350UnG5t51Nivo/1E/f/Gk DPqYHtGQuVmiO2Fp4ENld7mbOjl+rohZX60XuVwh1qe+bZ6bTiC2arzSu4s5v4jTyRJr CyPA== X-Gm-Message-State: APjAAAUcPlUUx7mz3hxs+6vDpNpVNc7/SdpYl8uhiTHhB9gUZJf104FT vmrvhLS+gI7zH30j4O+E7Me84g== X-Received: by 2002:a1c:f30e:: with SMTP id q14mr18991444wmq.65.1578908599336; Mon, 13 Jan 2020 01:43:19 -0800 (PST) Received: from revest.zrh.corp.google.com ([2a00:79e0:42:204:51d1:d96e:f72e:c8c0]) by smtp.gmail.com with ESMTPSA id m126sm13919276wmf.7.2020.01.13.01.43.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jan 2020 01:43:18 -0800 (PST) From: Florent Revest To: linux-integrity@vger.kernel.org Cc: 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: [PATCH v3] ima: add the ability to query the cached hash of a given file Date: Mon, 13 Jan 2020 10:42:44 +0100 Message-Id: <20200113094244.26678-1-revest@chromium.org> X-Mailer: git-send-email 2.25.0.rc1.283.g88dfdc4193-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. 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 --- 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