@@ -47,6 +47,10 @@ bool digest_cache_opened_fd(struct file *file);
struct digest_cache *digest_cache_lookup(struct dentry *dentry,
struct digest_cache *digest_cache,
u8 *digest, enum hash_algo algo);
+int digest_cache_verif_set(struct file *file, const char *verif_id, void *data,
+ size_t size);
+void *digest_cache_verif_get(struct digest_cache *digest_cache,
+ const char *verif_id);
/* Parser API */
int digest_cache_htable_init(struct digest_cache *digest_cache, u64 num_digests,
@@ -81,6 +85,19 @@ digest_cache_lookup(struct dentry *dentry, struct digest_cache *digest_cache,
return NULL;
}
+static inline int digest_cache_verif_set(struct file *file,
+ const char *verif_id, void *data,
+ size_t size)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline void *digest_cache_verif_get(struct digest_cache *digest_cache,
+ const char *verif_id)
+{
+ return NULL;
+}
+
static inline int digest_cache_htable_init(struct digest_cache *digest_cache,
u64 num_digests, enum hash_algo algo)
{
@@ -5,6 +5,7 @@
obj-$(CONFIG_INTEGRITY_DIGEST_CACHE) += digest_cache.o
obj-$(CONFIG_DIGEST_CACHE_TLV_PARSER) += parsers/tlv.o
-digest_cache-y := main.o secfs.o htable.o parsers.o populate.o modsig.o
+digest_cache-y := main.o secfs.o htable.o parsers.o populate.o modsig.o \
+ verif.o
CFLAGS_parsers.o += -DPARSERS_DIR=\"$(MODLIB)/kernel/security/integrity/digest_cache/parsers\"
@@ -18,6 +18,21 @@
#define INIT_STARTED 1 /* Digest cache init started. */
#define INVALID 2 /* Digest cache marked as invalid. */
+/**
+ * struct digest_cache_verif
+ * @list: Linked list
+ * @verif_id: Identifier of who verified the digest list
+ * @data: Opaque data set by the digest list verifier
+ *
+ * This structure contains opaque data containing the result of verification
+ * of the digest list by a verifier.
+ */
+struct digest_cache_verif {
+ struct list_head list;
+ char *verif_id;
+ void *data;
+};
+
/**
* struct read_work - Structure to schedule reading a digest list
* @work: Work structure
@@ -72,6 +87,8 @@ struct htable {
* @ref_count: Number of references to the digest cache
* @path_str: Path of the digest list the digest cache was created from
* @flags: Control flags
+ * @verif_data: Verification data regarding the digest list
+ * @verif_data_lock: Protects verification data modifications
*
* This structure represents a cache of digests extracted from a digest list.
*/
@@ -80,6 +97,8 @@ struct digest_cache {
atomic_t ref_count;
char *path_str;
unsigned long flags;
+ struct list_head verif_data;
+ spinlock_t verif_data_lock;
};
/**
@@ -191,4 +210,7 @@ int digest_cache_populate(struct dentry *dentry,
/* modsig.c */
size_t digest_cache_strip_modsig(__u8 *data, size_t data_len);
+/* verif.c */
+void digest_cache_verif_free(struct digest_cache *digest_cache);
+
#endif /* _DIGEST_CACHE_INTERNAL_H */
@@ -52,6 +52,8 @@ static struct digest_cache *digest_cache_alloc_init(char *path_str,
atomic_set(&digest_cache->ref_count, 1);
digest_cache->flags = 0UL;
INIT_LIST_HEAD(&digest_cache->htables);
+ INIT_LIST_HEAD(&digest_cache->verif_data);
+ spin_lock_init(&digest_cache->verif_data_lock);
pr_debug("New digest cache %s (ref count: %d)\n",
digest_cache->path_str, atomic_read(&digest_cache->ref_count));
@@ -68,6 +70,7 @@ static struct digest_cache *digest_cache_alloc_init(char *path_str,
static void digest_cache_free(struct digest_cache *digest_cache)
{
digest_cache_htable_free(digest_cache);
+ digest_cache_verif_free(digest_cache);
pr_debug("Freed digest cache %s\n", digest_cache->path_str);
kfree(digest_cache->path_str);
new file mode 100644
@@ -0,0 +1,131 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2023-2024 Huawei Technologies Duesseldorf GmbH
+ *
+ * Author: Roberto Sassu <roberto.sassu@huawei.com>
+ *
+ * Manage verification data regarding digest lists.
+ */
+
+#define pr_fmt(fmt) "digest_cache: "fmt
+#include "internal.h"
+
+/**
+ * free_verif - Free a digest_cache_verif structure
+ * @verif: digest_cache_verif structure
+ *
+ * Free the space allocated for a digest_cache_verif structure.
+ */
+static void free_verif(struct digest_cache_verif *verif)
+{
+ kfree(verif->data);
+ kfree(verif->verif_id);
+ kfree(verif);
+}
+
+/**
+ * digest_cache_verif_set - Set digest cache verification data
+ * @file: File descriptor of the digest list being read to populate digest cache
+ * @verif_id: Verifier ID
+ * @data: Verification data (opaque)
+ * @size: Size of @data
+ *
+ * This function lets a verifier supply verification data about a digest list
+ * being read to populate the digest cache. Verifier ID must be unique.
+ *
+ * Return: Zero on success, a POSIX error code otherwise.
+ */
+int digest_cache_verif_set(struct file *file, const char *verif_id, void *data,
+ size_t size)
+{
+ struct digest_cache *digest_cache = digest_cache_from_file_sec(file);
+ struct digest_cache_verif *new_verif, *verif;
+ /* All allocations done by kprobe must be atomic (non-sleepable). */
+ gfp_t flags = !strncmp(verif_id, "kprobe", 6) ? GFP_ATOMIC : GFP_KERNEL;
+ int ret = 0;
+
+ /*
+ * Zero the data, so that we can always call free_verif() to free a
+ * partially filled structure (if a pointer is NULL, will not be freed).
+ */
+ new_verif = kzalloc(sizeof(*new_verif), flags);
+ if (!new_verif)
+ return -ENOMEM;
+
+ new_verif->verif_id = kstrdup(verif_id, flags);
+ if (!new_verif->verif_id) {
+ free_verif(new_verif);
+ return -ENOMEM;
+ }
+
+ new_verif->data = kmemdup(data, size, flags);
+ if (!new_verif->data) {
+ free_verif(new_verif);
+ return -ENOMEM;
+ }
+
+ spin_lock(&digest_cache->verif_data_lock);
+ list_for_each_entry(verif, &digest_cache->verif_data, list) {
+ if (!strcmp(verif->verif_id, verif_id)) {
+ ret = -EEXIST;
+ goto out;
+ }
+ }
+
+ list_add_tail_rcu(&new_verif->list, &digest_cache->verif_data);
+out:
+ spin_unlock(&digest_cache->verif_data_lock);
+
+ if (ret < 0)
+ free_verif(new_verif);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(digest_cache_verif_set);
+
+/**
+ * digest_cache_verif_get - Get digest cache verification data
+ * @digest_cache: Digest cache
+ * @verif_id: Verifier ID
+ *
+ * This function returns the verification data previously set by a verifier
+ * with digest_cache_verif_set().
+ *
+ * Return: Verification data if found, NULL otherwise.
+ */
+void *digest_cache_verif_get(struct digest_cache *digest_cache,
+ const char *verif_id)
+{
+ struct digest_cache_verif *verif;
+ void *verif_data = NULL;
+
+ rcu_read_lock();
+ list_for_each_entry_rcu(verif, &digest_cache->verif_data, list) {
+ if (!strcmp(verif->verif_id, verif_id)) {
+ verif_data = verif->data;
+ break;
+ }
+ }
+ rcu_read_unlock();
+
+ return verif_data;
+}
+EXPORT_SYMBOL_GPL(digest_cache_verif_get);
+
+/**
+ * digest_cache_verif_free - Free all digest_cache_verif structures
+ * @digest_cache: Digest cache
+ *
+ * This function frees the space allocated for all digest_cache_verif
+ * structures in the digest cache.
+ */
+void digest_cache_verif_free(struct digest_cache *digest_cache)
+{
+ struct digest_cache_verif *p, *q;
+
+ /* No need to lock, called when nobody else has a digest cache ref. */
+ list_for_each_entry_safe(p, q, &digest_cache->verif_data, list) {
+ list_del(&p->list);
+ free_verif(p);
+ }
+}