Received: by 2002:ab2:69cc:0:b0:1f4:be93:e15a with SMTP id n12csp1469978lqp; Mon, 15 Apr 2024 07:29:18 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXVai4fO5uBqrnBZjvfFGfhkNKgUX0SApENXb6OELhrxe93YSrPWrwRWU5oFFCX2FePfq8Gi9RmgMtG9ieyN3L44olmuW0AKpMqi83xLA== X-Google-Smtp-Source: AGHT+IHVcJeQNjKOYN0H6rPEShso7g70oKJ2ujNTNlxnWt2M6oWAVa2ePn0mLu+xW5qXxO8I0A3e X-Received: by 2002:a17:90a:d187:b0:2a2:bcbf:562c with SMTP id fu7-20020a17090ad18700b002a2bcbf562cmr8674348pjb.30.1713191358618; Mon, 15 Apr 2024 07:29:18 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713191358; cv=pass; d=google.com; s=arc-20160816; b=vD71GP5PSuXBzifkvhJctoG3e++qwqLl9UV+lNNQ7r3WtiPoTV7kHAqX5u4zji3NUd BYFTinvlXZX3aJdoF3xiWHZ9TPJwbs/OuwG6MEs190w+0PnRhYFmYJrMrpslElmLBYdf Lc9hSxzlVCH5ZOUVlhimvXTSwjI4nnTHWzXIawWuFU/qggeDDLiifDyg/AzL7RlQ+U3q M/hgJFS6kt41M6Ar1u1bqn6n1/bVINnNRFDLYElVu11P2+Z7ygPK+I8GlDMPIVuIr4H5 IiPAOQeRu+dvesJnE/dbwrTDm1avkel2K0GzGcHSBUIsWByQPMNg7VFXQaEegd60TwcV bghQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=F2TjPk4yJU/VTAmq5mHBmWs2KHWf0wXBW6x844jtGqc=; fh=opLLVHUCSmbPIVm05XF/t7xLEeTehUWm/fnjcsDhNr8=; b=IYNcxL16EDGjlP3icxiQHvoWgS17WWZnE+BVDvkAhjlUD14LTxRUC9w08wXd05fl3w idI8uVGRHxEZ1Cyo28QWuSFm0sZojc+Z1iE5AaUhkvUre+hX4KAeAoo/FIMF/GhrxaBE rNZ+6aJTQjZUJ233UzToZb4eZGYIS88u54+mT4uyjhT8A2noz5OeECOWmP03yLEPeIHF oDA3tfZ4oVnLlKNbpz9++HnwbKDxxkDUHuTSAQmQP6jgKv6tS2HBZ2JIcokKml8LqF7j XHGhFQZ1iDSKWjhclRw7+Pz2spfm3JMutoH521hHDtzfc9gSHhQHbWy4hn33y/RyLn2a gH1Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-145345-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-145345-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id s1-20020a17090a1c0100b0029c7692a095si9998017pjs.120.2024.04.15.07.29.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Apr 2024 07:29:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-145345-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-145345-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-145345-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 630ACB224AB for ; Mon, 15 Apr 2024 14:26:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6BCB678B4E; Mon, 15 Apr 2024 14:26:02 +0000 (UTC) Received: from frasgout12.his.huawei.com (frasgout12.his.huawei.com [14.137.139.154]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 648CF757E1; Mon, 15 Apr 2024 14:25:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=14.137.139.154 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713191161; cv=none; b=sbq9ifUfVWvd3KF26gumdwta68Erhi3MBERns5tzeRB0ow3UyTQmncVjqBCdhT1vari5w9PXKE4iIfTaywuNDNaPp+nGFYDi8oyJ8e9r2tu3yZxWBO9UF/PxEBurGVCrEkW7lMhQ1VTZyMLMjHXftorpSFZGMF/igWEIQHYRVbs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713191161; c=relaxed/simple; bh=4YogMc3bBBFZcievjqeeR/1jzuCv2Qh7C5E3wQz7ArE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eJwwxRLE8rNCgz3Fi/2a/W4rfc2AC5c/P1iXJIHE4CvfA2NZt/ZGEpxlhLGmr2mQisMtE8OUdLKjnMWUVjCJ2y4K4Aw+HGsDMmIz9o6vIR9EyO+Tvc1Yag1kb9xfmCcRvgqMqnIQ7c11Vstz+adE2+ILPjgke1TfJjKxU6euckk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=14.137.139.154 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.18.186.29]) by frasgout12.his.huawei.com (SkyGuard) with ESMTP id 4VJ87g1L1zz9xGWd; Mon, 15 Apr 2024 22:05:03 +0800 (CST) Received: from mail02.huawei.com (unknown [7.182.16.27]) by mail.maildlp.com (Postfix) with ESMTP id 9B365140854; Mon, 15 Apr 2024 22:25:50 +0800 (CST) Received: from huaweicloud.com (unknown [10.204.63.22]) by APP2 (Coremail) with SMTP id GxC2BwBnoSWrOB1myEJGBg--.9473S5; Mon, 15 Apr 2024 15:25:49 +0100 (CET) From: Roberto Sassu To: corbet@lwn.net, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, akpm@linux-foundation.org, shuah@kernel.org, mcoquelin.stm32@gmail.com, alexandre.torgue@foss.st.com, mic@digikod.net Cc: linux-security-module@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, bpf@vger.kernel.org, zohar@linux.ibm.com, dmitry.kasatkin@gmail.com, linux-integrity@vger.kernel.org, wufan@linux.microsoft.com, pbrobinson@gmail.com, zbyszek@in.waw.pl, hch@lst.de, mjg59@srcf.ucam.org, pmatilai@redhat.com, jannh@google.com, dhowells@redhat.com, jikos@kernel.org, mkoutny@suse.com, ppavlu@suse.com, petr.vorel@gmail.com, mzerqung@0pointer.de, kgold@linux.ibm.com, Roberto Sassu Subject: [PATCH v4 03/14] digest_cache: Add securityfs interface Date: Mon, 15 Apr 2024 16:24:25 +0200 Message-Id: <20240415142436.2545003-4-roberto.sassu@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240415142436.2545003-1-roberto.sassu@huaweicloud.com> References: <20240415142436.2545003-1-roberto.sassu@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-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Ys7xG6r1j6r1xMIIF0xvEx4A2jsIE14 v26r4j6F4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr1j6F4UJbIYCTnIWIevJa73UjIFyTuY vjxUIUUUUUUUU X-CM-SenderInfo: purev21wro2thvvxqx5xdzvxpfor3voofrz/1tbiAgAOBF1jj5h0rgAAs7 From: Roberto Sassu Add the digest_cache_path file in securityfs, to let root change/read the default path (file or directory) from where digest lists are looked up. An RW semaphore prevents the default path from changing while digest_list_new() and read_default_path() are executed, so that those read a stable value. Multiple digest_list_new() and read_default_path() calls, instead, can be done in parallel, since they are the readers. Changing the default path does not affect digest caches created with the old path. Signed-off-by: Roberto Sassu --- security/digest_cache/Kconfig | 4 ++ security/digest_cache/Makefile | 2 +- security/digest_cache/internal.h | 1 + security/digest_cache/main.c | 10 +++- security/digest_cache/secfs.c | 87 ++++++++++++++++++++++++++++++++ 5 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 security/digest_cache/secfs.c diff --git a/security/digest_cache/Kconfig b/security/digest_cache/Kconfig index e53fbf0779d6..dfabe5d6e3ca 100644 --- a/security/digest_cache/Kconfig +++ b/security/digest_cache/Kconfig @@ -14,3 +14,7 @@ config DIGEST_LIST_DEFAULT_PATH default "/etc/digest_lists" help Default directory where digest_cache LSM expects to find digest lists. + + It can be changed at run-time, by writing the new path to the + securityfs interface. Digest caches created with the old path are + not affected by the change. diff --git a/security/digest_cache/Makefile b/security/digest_cache/Makefile index 48848c41253e..1330655e33b1 100644 --- a/security/digest_cache/Makefile +++ b/security/digest_cache/Makefile @@ -4,4 +4,4 @@ obj-$(CONFIG_SECURITY_DIGEST_CACHE) += digest_cache.o -digest_cache-y := main.o +digest_cache-y := main.o secfs.o diff --git a/security/digest_cache/internal.h b/security/digest_cache/internal.h index 5f04844af3a5..bbf5eefe5c82 100644 --- a/security/digest_cache/internal.h +++ b/security/digest_cache/internal.h @@ -49,6 +49,7 @@ struct digest_cache_security { extern struct lsm_blob_sizes digest_cache_blob_sizes; extern char *default_path_str; +extern struct rw_semaphore default_path_sem; static inline struct digest_cache_security * digest_cache_get_security(const struct inode *inode) diff --git a/security/digest_cache/main.c b/security/digest_cache/main.c index 14dba8915e99..661c8d106791 100644 --- a/security/digest_cache/main.c +++ b/security/digest_cache/main.c @@ -18,6 +18,9 @@ static struct kmem_cache *digest_cache_cache __read_mostly; char *default_path_str = CONFIG_DIGEST_LIST_DEFAULT_PATH; +/* Protects default_path_str. */ +struct rw_semaphore default_path_sem; + /** * digest_cache_alloc_init - Allocate and initialize a new digest cache * @path_str: Path string of the digest list @@ -274,9 +277,12 @@ struct digest_cache *digest_cache_get(struct dentry *dentry) /* Serialize accesses to inode for which the digest cache is used. */ mutex_lock(&dig_sec->dig_user_mutex); - if (!dig_sec->dig_user) + if (!dig_sec->dig_user) { + down_read(&default_path_sem); /* Consume extra reference from digest_cache_create(). */ dig_sec->dig_user = digest_cache_new(dentry); + up_read(&default_path_sem); + } if (dig_sec->dig_user) /* Increment ref. count for reference returned to the caller. */ @@ -386,6 +392,8 @@ static const struct lsm_id digest_cache_lsmid = { */ static int __init digest_cache_init(void) { + init_rwsem(&default_path_sem); + digest_cache_cache = kmem_cache_create("digest_cache_cache", sizeof(struct digest_cache), 0, SLAB_PANIC, diff --git a/security/digest_cache/secfs.c b/security/digest_cache/secfs.c new file mode 100644 index 000000000000..d3a37bf3588e --- /dev/null +++ b/security/digest_cache/secfs.c @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2023-2024 Huawei Technologies Duesseldorf GmbH + * + * Author: Roberto Sassu + * + * Implement the securityfs interface of the digest_cache LSM. + */ + +#define pr_fmt(fmt) "DIGEST CACHE: "fmt +#include + +#include "internal.h" + +static struct dentry *default_path_dentry; + +/** + * write_default_path - Write default path + * @file: File descriptor of the securityfs file + * @buf: User space buffer + * @datalen: Amount of data to write + * @ppos: Current position in the file + * + * This function sets the new default path where digest lists can be found. + * Can be either a regular file or a directory. + * + * Return: Length of path written on success, a POSIX error code otherwise. + */ +static ssize_t write_default_path(struct file *file, const char __user *buf, + size_t datalen, loff_t *ppos) +{ + char *new_default_path_str; + + new_default_path_str = memdup_user_nul(buf, datalen); + if (IS_ERR(new_default_path_str)) + return PTR_ERR(new_default_path_str); + + down_write(&default_path_sem); + kfree_const(default_path_str); + default_path_str = new_default_path_str; + up_write(&default_path_sem); + return datalen; +} + +/** + * read_default_path - Read default path + * @file: File descriptor of the securityfs file + * @buf: User space buffer + * @datalen: Amount of data to read + * @ppos: Current position in the file + * + * This function returns the current default path where digest lists can be + * found. Can be either a regular file or a directory. + * + * Return: Length of path read on success, a POSIX error code otherwise. + */ +static ssize_t read_default_path(struct file *file, char __user *buf, + size_t datalen, loff_t *ppos) +{ + int ret; + + down_read(&default_path_sem); + ret = simple_read_from_buffer(buf, datalen, ppos, default_path_str, + strlen(default_path_str) + 1); + up_read(&default_path_sem); + return ret; +} + +static const struct file_operations default_path_ops = { + .open = generic_file_open, + .write = write_default_path, + .read = read_default_path, + .llseek = generic_file_llseek, +}; + +static int __init digest_cache_path_init(void) +{ + default_path_dentry = securityfs_create_file("digest_cache_path", 0660, + NULL, NULL, + &default_path_ops); + if (IS_ERR(default_path_dentry)) + return -EFAULT; + + return 0; +} + +late_initcall(digest_cache_path_init); -- 2.34.1