Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1716579pxu; Tue, 24 Nov 2020 07:17:21 -0800 (PST) X-Google-Smtp-Source: ABdhPJyv6fWtNRHhLFdirG1mwPAzK5HbDBnDwDZ5Kk2mPPk6Zh5BiBECo0SehZpvI5frlRGRbZlp X-Received: by 2002:a17:906:f2cc:: with SMTP id gz12mr4491869ejb.181.1606231041658; Tue, 24 Nov 2020 07:17:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606231041; cv=none; d=google.com; s=arc-20160816; b=sOLcSp57LRAO3cuxuQ1DpCaBWyvfnD6p80ZK0H3QhMp2Xm0f9jCJe/YMbuIVX04u4+ kMKXR54dHWszUxm/dlF6v7dPqXs3cpjbTGbJ/FRH4IIIa+hLdSYi9/pC1YdKb+e6gJeZ b5hy2toXbJQ5F9xB4/X0o7CEphpylaMzisMzfdmb5JqLFcmlK4yW39KTFoaejSgjY5Wh e57LLr825uS1AL9VZEpdLb/d7yu7rpC05zuExzWNqVIFz6RE8mgOd2vDshlBBcUEjYQS Un1rNLhxC++1CugKbKN8tQCdY9i9VVzmXDGjjaiG4zhP7S7phAcp4A1zM1XYbUeUcn6Y pYcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4EcEA95lNa5ZZ+4yTLKOmHZRpqe2owrp9tTkgwACEiM=; b=j6F0UE4GWcfGMmWZoyNP3iR8rj8qjUTBh4dx+PrWtxEFFfyb4nOFTAwGSAF/Mnol8x y0NF3V5Xj9iMHup67EP06iU8MI84J/7wg9D6+W8sTpN59kok77XsfbmezDSNzkcWKcON oFmMy9CHbpJ61yASWNkFR1ju10m6Y3kWPK/X8XTKq82xGQwgXAFKRzuoB6RINrNTzsF0 oKLlgo9yP/WO+Dli/jQoS0KBIlb1V6334BeUrdpyTSk/Mkd+nU8qDBRfJjVEsKZYo4dD 8DoXVJ+s6s0NCaJBMrj55QPfFlL3Q7TOzoPk03/SubeLuzVsLpj0s24Wj7KlIComS3V2 KkPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ibCi5X8J; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id sb25si2699097ejb.638.2020.11.24.07.16.57; Tue, 24 Nov 2020 07:17:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ibCi5X8J; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S2388191AbgKXPMP (ORCPT + 99 others); Tue, 24 Nov 2020 10:12:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49824 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728372AbgKXPMO (ORCPT ); Tue, 24 Nov 2020 10:12:14 -0500 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A5E71C0613D6 for ; Tue, 24 Nov 2020 07:12:14 -0800 (PST) Received: by mail-wr1-x443.google.com with SMTP id g14so7481280wrm.13 for ; Tue, 24 Nov 2020 07:12:14 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=4EcEA95lNa5ZZ+4yTLKOmHZRpqe2owrp9tTkgwACEiM=; b=ibCi5X8Jr3TncJq/YRtxicrO73AYf22K1o+IetPSD2hZZqoLFnXj4rZ9EU7AI5GcfD p1hLH3tYLKfG4l2BLazi3w4HwIoqZeSG5jYi/WeR1uV8A4j6SVqDNyS5S4L/yExAP05U Z0KANTbHysLXx2RsH0ZNKTICnEWqjOVyfxb6A= 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=4EcEA95lNa5ZZ+4yTLKOmHZRpqe2owrp9tTkgwACEiM=; b=l48acPzMJZ6qEU3HU8zcDKUkDzkb/NpuQL+Bnr/HszsF058IiwD+s1szmJ0U+739o/ 3i8DWTorWk2iBzRVtt0DdJpY+1vpHaCA4USWx626B6ESeXwMzXegmJTHGTRSsPfFXdah 2nDXyeWp8V9p6gHiGpgV04P44cFexNPmYvahmz6iOA//MbpmzxoVDivSWafxtoOWt6kp iGZvYKi1nBmaJpZQGHR451ZmWaKq1m2wbvyx/puD/XoZTsKbA9LMXdV/LBcxqlX8HH+9 C5WStdFFHwuHY9q9TiNemKz3saSqQzQfgBpWbh1hIuH46owMYvPqxu2JFwSuT0r0/nLY UA3A== X-Gm-Message-State: AOAM530U9Xh2cFqo2nPuhSmYLZ83n0lQhF4mF8/nkNuxL/sWivGdsGt8 Dfr6dGhIgtBnpNpxKcH/T2I50g== X-Received: by 2002:a5d:690a:: with SMTP id t10mr5955851wru.203.1606230733342; Tue, 24 Nov 2020 07:12:13 -0800 (PST) Received: from kpsingh.c.googlers.com.com (203.75.199.104.bc.googleusercontent.com. [104.199.75.203]) by smtp.gmail.com with ESMTPSA id g131sm6353127wma.35.2020.11.24.07.12.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Nov 2020 07:12:12 -0800 (PST) From: KP Singh To: James Morris , linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-security-module@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Florent Revest , Brendan Jackman , Mimi Zohar Subject: [PATCH bpf-next v3 1/3] ima: Implement ima_inode_hash Date: Tue, 24 Nov 2020 15:12:08 +0000 Message-Id: <20201124151210.1081188-2-kpsingh@chromium.org> X-Mailer: git-send-email 2.29.2.454.gaff20da3a2-goog In-Reply-To: <20201124151210.1081188-1-kpsingh@chromium.org> References: <20201124151210.1081188-1-kpsingh@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: KP Singh This is in preparation to add a helper for BPF LSM programs to use IMA hashes when attached to LSM hooks. There are LSM hooks like inode_unlink which do not have a struct file * argument and cannot use the existing ima_file_hash API. An inode based API is, therefore, useful in LSM based detections like an executable trying to delete itself which rely on the inode_unlink LSM hook. Moreover, the ima_file_hash function does nothing with the struct file pointer apart from calling file_inode on it and converting it to an inode. Signed-off-by: KP Singh --- include/linux/ima.h | 6 +++ security/integrity/ima/ima_main.c | 78 +++++++++++++++++++++---------- 2 files changed, 60 insertions(+), 24 deletions(-) diff --git a/include/linux/ima.h b/include/linux/ima.h index 8fa7bcfb2da2..7233a2751754 100644 --- a/include/linux/ima.h +++ b/include/linux/ima.h @@ -29,6 +29,7 @@ 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 int ima_inode_hash(struct inode *inode, char *buf, size_t buf_size); extern void ima_kexec_cmdline(int kernel_fd, const void *buf, int size); #ifdef CONFIG_IMA_KEXEC @@ -115,6 +116,11 @@ static inline int ima_file_hash(struct file *file, char *buf, size_t buf_size) return -EOPNOTSUPP; } +static inline int ima_inode_hash(struct inode *inode, char *buf, size_t buf_size) +{ + return -EOPNOTSUPP; +} + static inline void ima_kexec_cmdline(int kernel_fd, 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 2d1af8899cab..cb2deaa188e7 100644 --- a/security/integrity/ima/ima_main.c +++ b/security/integrity/ima/ima_main.c @@ -501,37 +501,14 @@ 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) +static int __ima_inode_hash(struct inode *inode, 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; @@ -558,8 +535,61 @@ int ima_file_hash(struct file *file, char *buf, size_t buf_size) return hash_algo; } + +/** + * 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) +{ + if (!file) + return -EINVAL; + + return __ima_inode_hash(file_inode(file), buf, buf_size); +} EXPORT_SYMBOL_GPL(ima_file_hash); +/** + * ima_inode_hash - return the stored measurement if the inode has been hashed + * and is in the iint cache. + * @inode: pointer to the inode + * @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 hash returned is based on the entire contents, 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_inode_hash(struct inode *inode, char *buf, size_t buf_size) +{ + if (!inode) + return -EINVAL; + + return __ima_inode_hash(inode, buf, buf_size); +} +EXPORT_SYMBOL_GPL(ima_inode_hash); + /** * ima_post_create_tmpfile - mark newly created tmpfile as new * @file : newly created tmpfile -- 2.29.2.454.gaff20da3a2-goog