Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp1693513pxb; Fri, 20 Nov 2020 16:52:40 -0800 (PST) X-Google-Smtp-Source: ABdhPJyIn3azXQJzsNv9pZBF5KZnYxpB2lbAo8ZPh0BJtCsWn4uEOPYTVcl8zlYcBi631vAUce6x X-Received: by 2002:a17:907:40c7:: with SMTP id nv7mr12354617ejb.325.1605919960391; Fri, 20 Nov 2020 16:52:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605919960; cv=none; d=google.com; s=arc-20160816; b=jnFHZHCfmHvJ2Y7n62So6rCxUSQQ8G/MKeYUqFpfM6MDitDh7SMAxjHmGLMdim9Xen ftZYJNNhjBQXbZWASemRkKqwh23/6TIEgQpZagowvp/WaEOvSe1MOKKkQhOdvMCasdz7 f7/2U3eVVD0KTvJAafOpiKyuRxizayIu5sRico829MSDjnGiRTLzmFyuRaAtNKSX/i/i isxIgI9w9sQAv+rn3IXn+gjJbj4va64VpKpu1bzcQ740oeUihFcjNG2DrZtOLgNdsT56 W6rhwuK3q2q/jyMImNaUbDwbzZVKS+IxjBvJvsOO25FA0QjE6CclK5iUZdJ5108lwXug Q9FA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=4EcEA95lNa5ZZ+4yTLKOmHZRpqe2owrp9tTkgwACEiM=; b=PiAYW9DiblAmErgLD9YxdpgaFAx6f1tIFwc9RpbsTD2MkDk8uxiRyTnD9b/41QWDW6 zhQiAR+e0060l3gjp/33or/ENVJpfsfHxmr/Ik2wixILFaBwrjCcDGxWxHBfpfHmTZ4R rPn1o3oDiI86u8F8+aMyjfPVwM1jo/L+BERHyxm/wdsz9DfJr+ZaOjB8SWys1yHNNvXr 8u1DZggqB44LyFEiUoGzB0I9pgmm7F1gRZiyvZXQkBEq29mtj45U26ioaxZnTTHse+mH c+DU25JmuFASTzblJ6x/AAWW45agOlDxzi6P1Iu6IdUh2ZsZ3XfhApEIBMI774x2Lt2u qdPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=HDfVcfah; 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 n22si2659311ejo.705.2020.11.20.16.52.16; Fri, 20 Nov 2020 16:52:40 -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=HDfVcfah; 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 S1728341AbgKUAvA (ORCPT + 99 others); Fri, 20 Nov 2020 19:51:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44074 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726587AbgKUAu6 (ORCPT ); Fri, 20 Nov 2020 19:50:58 -0500 Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47219C061A47 for ; Fri, 20 Nov 2020 16:50:58 -0800 (PST) Received: by mail-wr1-x444.google.com with SMTP id 23so12666129wrc.8 for ; Fri, 20 Nov 2020 16:50:58 -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=4EcEA95lNa5ZZ+4yTLKOmHZRpqe2owrp9tTkgwACEiM=; b=HDfVcfahM3bKl9+qUQ17zfP7VPqPF47VMw+BHus3Ay4VZMn3CaP64kl4ysBf8B662Z 8iy2knqgDfzfRBSuxTRSuVI/mM9QU6Xl3uvF+LmivdARc+EhYceK73AoEr5+4YBtgdwx H6MZ1Xw9f4RCyLGtwXbNlRLg4+6a4ugdGEerA= 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=4EcEA95lNa5ZZ+4yTLKOmHZRpqe2owrp9tTkgwACEiM=; b=qgz9LX4gECGYS0vqmmd/xj8rORi7pFjI2zb/X+K5A0l/SKFYqmgNbCy70vbYfGsFZL HFIqbabwVpOwLKjzhP69klqqBk3pO28IJyv+Wbq8BfbV9t5o4HMPnNFEy7IEoEoAgzBj QVAmZQyLk/VRr75+2stdEQ266v7ckcAai7ZN7UrmxEx0CZipiYW9XvCBZgNimLGv+itG NbIKFQJwLTYOTRPem2BhDOPw4/lfYoCk48o89ge5hEzfNMoy+hQWZMLNGgKakfapnR+X NKubXn+AdWzPZGxGh6XLYoZhEfmShREviH04V5lmVN00GIet9VFC2nBv4hn8tyRIXaB/ WY0Q== X-Gm-Message-State: AOAM530f+O/8qlIjX/T68mcAWAxkZ9bVTeo3WpXM0LnClIaVlaLitzkp UCyT/CJzCVyCoqT7HwhOCoDPTQ== X-Received: by 2002:adf:ead1:: with SMTP id o17mr18872821wrn.396.1605919856713; Fri, 20 Nov 2020 16:50:56 -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 s8sm7133607wrn.33.2020.11.20.16.50.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Nov 2020 16:50:56 -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 v2 1/3] ima: Implement ima_inode_hash Date: Sat, 21 Nov 2020 00:50:52 +0000 Message-Id: <20201121005054.3467947-1-kpsingh@chromium.org> X-Mailer: git-send-email 2.29.2.454.gaff20da3a2-goog 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