Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* user_defined.c: user defined key type
0003  *
0004  * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
0005  * Written by David Howells (dhowells@redhat.com)
0006  */
0007 
0008 #include <linux/export.h>
0009 #include <linux/init.h>
0010 #include <linux/slab.h>
0011 #include <linux/seq_file.h>
0012 #include <linux/err.h>
0013 #include <keys/user-type.h>
0014 #include <linux/uaccess.h>
0015 #include "internal.h"
0016 
0017 static int logon_vet_description(const char *desc);
0018 
0019 /*
0020  * user defined keys take an arbitrary string as the description and an
0021  * arbitrary blob of data as the payload
0022  */
0023 struct key_type key_type_user = {
0024     .name           = "user",
0025     .preparse       = user_preparse,
0026     .free_preparse      = user_free_preparse,
0027     .instantiate        = generic_key_instantiate,
0028     .update         = user_update,
0029     .revoke         = user_revoke,
0030     .destroy        = user_destroy,
0031     .describe       = user_describe,
0032     .read           = user_read,
0033 };
0034 
0035 EXPORT_SYMBOL_GPL(key_type_user);
0036 
0037 /*
0038  * This key type is essentially the same as key_type_user, but it does
0039  * not define a .read op. This is suitable for storing username and
0040  * password pairs in the keyring that you do not want to be readable
0041  * from userspace.
0042  */
0043 struct key_type key_type_logon = {
0044     .name           = "logon",
0045     .preparse       = user_preparse,
0046     .free_preparse      = user_free_preparse,
0047     .instantiate        = generic_key_instantiate,
0048     .update         = user_update,
0049     .revoke         = user_revoke,
0050     .destroy        = user_destroy,
0051     .describe       = user_describe,
0052     .vet_description    = logon_vet_description,
0053 };
0054 EXPORT_SYMBOL_GPL(key_type_logon);
0055 
0056 /*
0057  * Preparse a user defined key payload
0058  */
0059 int user_preparse(struct key_preparsed_payload *prep)
0060 {
0061     struct user_key_payload *upayload;
0062     size_t datalen = prep->datalen;
0063 
0064     if (datalen <= 0 || datalen > 32767 || !prep->data)
0065         return -EINVAL;
0066 
0067     upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
0068     if (!upayload)
0069         return -ENOMEM;
0070 
0071     /* attach the data */
0072     prep->quotalen = datalen;
0073     prep->payload.data[0] = upayload;
0074     upayload->datalen = datalen;
0075     memcpy(upayload->data, prep->data, datalen);
0076     return 0;
0077 }
0078 EXPORT_SYMBOL_GPL(user_preparse);
0079 
0080 /*
0081  * Free a preparse of a user defined key payload
0082  */
0083 void user_free_preparse(struct key_preparsed_payload *prep)
0084 {
0085     kfree_sensitive(prep->payload.data[0]);
0086 }
0087 EXPORT_SYMBOL_GPL(user_free_preparse);
0088 
0089 static void user_free_payload_rcu(struct rcu_head *head)
0090 {
0091     struct user_key_payload *payload;
0092 
0093     payload = container_of(head, struct user_key_payload, rcu);
0094     kfree_sensitive(payload);
0095 }
0096 
0097 /*
0098  * update a user defined key
0099  * - the key's semaphore is write-locked
0100  */
0101 int user_update(struct key *key, struct key_preparsed_payload *prep)
0102 {
0103     struct user_key_payload *zap = NULL;
0104     int ret;
0105 
0106     /* check the quota and attach the new data */
0107     ret = key_payload_reserve(key, prep->datalen);
0108     if (ret < 0)
0109         return ret;
0110 
0111     /* attach the new data, displacing the old */
0112     key->expiry = prep->expiry;
0113     if (key_is_positive(key))
0114         zap = dereference_key_locked(key);
0115     rcu_assign_keypointer(key, prep->payload.data[0]);
0116     prep->payload.data[0] = NULL;
0117 
0118     if (zap)
0119         call_rcu(&zap->rcu, user_free_payload_rcu);
0120     return ret;
0121 }
0122 EXPORT_SYMBOL_GPL(user_update);
0123 
0124 /*
0125  * dispose of the links from a revoked keyring
0126  * - called with the key sem write-locked
0127  */
0128 void user_revoke(struct key *key)
0129 {
0130     struct user_key_payload *upayload = user_key_payload_locked(key);
0131 
0132     /* clear the quota */
0133     key_payload_reserve(key, 0);
0134 
0135     if (upayload) {
0136         rcu_assign_keypointer(key, NULL);
0137         call_rcu(&upayload->rcu, user_free_payload_rcu);
0138     }
0139 }
0140 
0141 EXPORT_SYMBOL(user_revoke);
0142 
0143 /*
0144  * dispose of the data dangling from the corpse of a user key
0145  */
0146 void user_destroy(struct key *key)
0147 {
0148     struct user_key_payload *upayload = key->payload.data[0];
0149 
0150     kfree_sensitive(upayload);
0151 }
0152 
0153 EXPORT_SYMBOL_GPL(user_destroy);
0154 
0155 /*
0156  * describe the user key
0157  */
0158 void user_describe(const struct key *key, struct seq_file *m)
0159 {
0160     seq_puts(m, key->description);
0161     if (key_is_positive(key))
0162         seq_printf(m, ": %u", key->datalen);
0163 }
0164 
0165 EXPORT_SYMBOL_GPL(user_describe);
0166 
0167 /*
0168  * read the key data
0169  * - the key's semaphore is read-locked
0170  */
0171 long user_read(const struct key *key, char *buffer, size_t buflen)
0172 {
0173     const struct user_key_payload *upayload;
0174     long ret;
0175 
0176     upayload = user_key_payload_locked(key);
0177     ret = upayload->datalen;
0178 
0179     /* we can return the data as is */
0180     if (buffer && buflen > 0) {
0181         if (buflen > upayload->datalen)
0182             buflen = upayload->datalen;
0183 
0184         memcpy(buffer, upayload->data, buflen);
0185     }
0186 
0187     return ret;
0188 }
0189 
0190 EXPORT_SYMBOL_GPL(user_read);
0191 
0192 /* Vet the description for a "logon" key */
0193 static int logon_vet_description(const char *desc)
0194 {
0195     char *p;
0196 
0197     /* require a "qualified" description string */
0198     p = strchr(desc, ':');
0199     if (!p)
0200         return -EINVAL;
0201 
0202     /* also reject description with ':' as first char */
0203     if (p == desc)
0204         return -EINVAL;
0205 
0206     return 0;
0207 }