|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +#include <keys/user-type.h> |
| 3 | +#include <linux/crash_dump.h> |
| 4 | +#include <linux/configfs.h> |
| 5 | +#include <linux/module.h> |
| 6 | + |
| 7 | +#define KEY_NUM_MAX 128 /* maximum dm crypt keys */ |
| 8 | +#define KEY_DESC_MAX_LEN 128 /* maximum dm crypt key description size */ |
| 9 | + |
| 10 | +static unsigned int key_count; |
| 11 | + |
| 12 | +struct config_key { |
| 13 | + struct config_item item; |
| 14 | + const char *description; |
| 15 | +}; |
| 16 | + |
| 17 | +static inline struct config_key *to_config_key(struct config_item *item) |
| 18 | +{ |
| 19 | + return container_of(item, struct config_key, item); |
| 20 | +} |
| 21 | + |
| 22 | +static ssize_t config_key_description_show(struct config_item *item, char *page) |
| 23 | +{ |
| 24 | + return sprintf(page, "%s\n", to_config_key(item)->description); |
| 25 | +} |
| 26 | + |
| 27 | +static ssize_t config_key_description_store(struct config_item *item, |
| 28 | + const char *page, size_t count) |
| 29 | +{ |
| 30 | + struct config_key *config_key = to_config_key(item); |
| 31 | + size_t len; |
| 32 | + int ret; |
| 33 | + |
| 34 | + ret = -EINVAL; |
| 35 | + len = strcspn(page, "\n"); |
| 36 | + |
| 37 | + if (len > KEY_DESC_MAX_LEN) { |
| 38 | + pr_err("The key description shouldn't exceed %u characters", KEY_DESC_MAX_LEN); |
| 39 | + return ret; |
| 40 | + } |
| 41 | + |
| 42 | + if (!len) |
| 43 | + return ret; |
| 44 | + |
| 45 | + kfree(config_key->description); |
| 46 | + ret = -ENOMEM; |
| 47 | + config_key->description = kmemdup_nul(page, len, GFP_KERNEL); |
| 48 | + if (!config_key->description) |
| 49 | + return ret; |
| 50 | + |
| 51 | + return count; |
| 52 | +} |
| 53 | + |
| 54 | +CONFIGFS_ATTR(config_key_, description); |
| 55 | + |
| 56 | +static struct configfs_attribute *config_key_attrs[] = { |
| 57 | + &config_key_attr_description, |
| 58 | + NULL, |
| 59 | +}; |
| 60 | + |
| 61 | +static void config_key_release(struct config_item *item) |
| 62 | +{ |
| 63 | + kfree(to_config_key(item)); |
| 64 | + key_count--; |
| 65 | +} |
| 66 | + |
| 67 | +static struct configfs_item_operations config_key_item_ops = { |
| 68 | + .release = config_key_release, |
| 69 | +}; |
| 70 | + |
| 71 | +static const struct config_item_type config_key_type = { |
| 72 | + .ct_item_ops = &config_key_item_ops, |
| 73 | + .ct_attrs = config_key_attrs, |
| 74 | + .ct_owner = THIS_MODULE, |
| 75 | +}; |
| 76 | + |
| 77 | +static struct config_item *config_keys_make_item(struct config_group *group, |
| 78 | + const char *name) |
| 79 | +{ |
| 80 | + struct config_key *config_key; |
| 81 | + |
| 82 | + if (key_count > KEY_NUM_MAX) { |
| 83 | + pr_err("Only %u keys at maximum to be created\n", KEY_NUM_MAX); |
| 84 | + return ERR_PTR(-EINVAL); |
| 85 | + } |
| 86 | + |
| 87 | + config_key = kzalloc(sizeof(struct config_key), GFP_KERNEL); |
| 88 | + if (!config_key) |
| 89 | + return ERR_PTR(-ENOMEM); |
| 90 | + |
| 91 | + config_item_init_type_name(&config_key->item, name, &config_key_type); |
| 92 | + |
| 93 | + key_count++; |
| 94 | + |
| 95 | + return &config_key->item; |
| 96 | +} |
| 97 | + |
| 98 | +static ssize_t config_keys_count_show(struct config_item *item, char *page) |
| 99 | +{ |
| 100 | + return sprintf(page, "%d\n", key_count); |
| 101 | +} |
| 102 | + |
| 103 | +CONFIGFS_ATTR_RO(config_keys_, count); |
| 104 | + |
| 105 | +static struct configfs_attribute *config_keys_attrs[] = { |
| 106 | + &config_keys_attr_count, |
| 107 | + NULL, |
| 108 | +}; |
| 109 | + |
| 110 | +/* |
| 111 | + * Note that, since no extra work is required on ->drop_item(), |
| 112 | + * no ->drop_item() is provided. |
| 113 | + */ |
| 114 | +static struct configfs_group_operations config_keys_group_ops = { |
| 115 | + .make_item = config_keys_make_item, |
| 116 | +}; |
| 117 | + |
| 118 | +static const struct config_item_type config_keys_type = { |
| 119 | + .ct_group_ops = &config_keys_group_ops, |
| 120 | + .ct_attrs = config_keys_attrs, |
| 121 | + .ct_owner = THIS_MODULE, |
| 122 | +}; |
| 123 | + |
| 124 | +static struct configfs_subsystem config_keys_subsys = { |
| 125 | + .su_group = { |
| 126 | + .cg_item = { |
| 127 | + .ci_namebuf = "crash_dm_crypt_keys", |
| 128 | + .ci_type = &config_keys_type, |
| 129 | + }, |
| 130 | + }, |
| 131 | +}; |
| 132 | + |
| 133 | +static int __init configfs_dmcrypt_keys_init(void) |
| 134 | +{ |
| 135 | + int ret; |
| 136 | + |
| 137 | + config_group_init(&config_keys_subsys.su_group); |
| 138 | + mutex_init(&config_keys_subsys.su_mutex); |
| 139 | + ret = configfs_register_subsystem(&config_keys_subsys); |
| 140 | + if (ret) { |
| 141 | + pr_err("Error %d while registering subsystem %s\n", ret, |
| 142 | + config_keys_subsys.su_group.cg_item.ci_namebuf); |
| 143 | + goto out_unregister; |
| 144 | + } |
| 145 | + |
| 146 | + return 0; |
| 147 | + |
| 148 | +out_unregister: |
| 149 | + configfs_unregister_subsystem(&config_keys_subsys); |
| 150 | + |
| 151 | + return ret; |
| 152 | +} |
| 153 | + |
| 154 | +module_init(configfs_dmcrypt_keys_init); |
0 commit comments