Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Greybus Component Authentication Protocol (CAP) Driver.
0004  *
0005  * Copyright 2016 Google Inc.
0006  * Copyright 2016 Linaro Ltd.
0007  */
0008 
0009 #include <linux/greybus.h>
0010 #include <linux/cdev.h>
0011 #include <linux/fs.h>
0012 #include <linux/ioctl.h>
0013 #include <linux/uaccess.h>
0014 
0015 #include "greybus_authentication.h"
0016 #include "firmware.h"
0017 
0018 #define CAP_TIMEOUT_MS      1000
0019 
0020 /*
0021  * Number of minor devices this driver supports.
0022  * There will be exactly one required per Interface.
0023  */
0024 #define NUM_MINORS      U8_MAX
0025 
0026 struct gb_cap {
0027     struct device       *parent;
0028     struct gb_connection    *connection;
0029     struct kref     kref;
0030     struct list_head    node;
0031     bool            disabled; /* connection getting disabled */
0032 
0033     struct mutex        mutex;
0034     struct cdev     cdev;
0035     struct device       *class_device;
0036     dev_t           dev_num;
0037 };
0038 
0039 static struct class *cap_class;
0040 static dev_t cap_dev_num;
0041 static DEFINE_IDA(cap_minors_map);
0042 static LIST_HEAD(cap_list);
0043 static DEFINE_MUTEX(list_mutex);
0044 
0045 static void cap_kref_release(struct kref *kref)
0046 {
0047     struct gb_cap *cap = container_of(kref, struct gb_cap, kref);
0048 
0049     kfree(cap);
0050 }
0051 
0052 /*
0053  * All users of cap take a reference (from within list_mutex lock), before
0054  * they get a pointer to play with. And the structure will be freed only after
0055  * the last user has put the reference to it.
0056  */
0057 static void put_cap(struct gb_cap *cap)
0058 {
0059     kref_put(&cap->kref, cap_kref_release);
0060 }
0061 
0062 /* Caller must call put_cap() after using struct gb_cap */
0063 static struct gb_cap *get_cap(struct cdev *cdev)
0064 {
0065     struct gb_cap *cap;
0066 
0067     mutex_lock(&list_mutex);
0068 
0069     list_for_each_entry(cap, &cap_list, node) {
0070         if (&cap->cdev == cdev) {
0071             kref_get(&cap->kref);
0072             goto unlock;
0073         }
0074     }
0075 
0076     cap = NULL;
0077 
0078 unlock:
0079     mutex_unlock(&list_mutex);
0080 
0081     return cap;
0082 }
0083 
0084 static int cap_get_endpoint_uid(struct gb_cap *cap, u8 *euid)
0085 {
0086     struct gb_connection *connection = cap->connection;
0087     struct gb_cap_get_endpoint_uid_response response;
0088     int ret;
0089 
0090     ret = gb_operation_sync(connection, GB_CAP_TYPE_GET_ENDPOINT_UID, NULL,
0091                 0, &response, sizeof(response));
0092     if (ret) {
0093         dev_err(cap->parent, "failed to get endpoint uid (%d)\n", ret);
0094         return ret;
0095     }
0096 
0097     memcpy(euid, response.uid, sizeof(response.uid));
0098 
0099     return 0;
0100 }
0101 
0102 static int cap_get_ims_certificate(struct gb_cap *cap, u32 class, u32 id,
0103                    u8 *certificate, u32 *size, u8 *result)
0104 {
0105     struct gb_connection *connection = cap->connection;
0106     struct gb_cap_get_ims_certificate_request *request;
0107     struct gb_cap_get_ims_certificate_response *response;
0108     size_t max_size = gb_operation_get_payload_size_max(connection);
0109     struct gb_operation *op;
0110     int ret;
0111 
0112     op = gb_operation_create_flags(connection,
0113                        GB_CAP_TYPE_GET_IMS_CERTIFICATE,
0114                        sizeof(*request), max_size,
0115                        GB_OPERATION_FLAG_SHORT_RESPONSE,
0116                        GFP_KERNEL);
0117     if (!op)
0118         return -ENOMEM;
0119 
0120     request = op->request->payload;
0121     request->certificate_class = cpu_to_le32(class);
0122     request->certificate_id = cpu_to_le32(id);
0123 
0124     ret = gb_operation_request_send_sync(op);
0125     if (ret) {
0126         dev_err(cap->parent, "failed to get certificate (%d)\n", ret);
0127         goto done;
0128     }
0129 
0130     response = op->response->payload;
0131     *result = response->result_code;
0132     *size = op->response->payload_size - sizeof(*response);
0133     memcpy(certificate, response->certificate, *size);
0134 
0135 done:
0136     gb_operation_put(op);
0137     return ret;
0138 }
0139 
0140 static int cap_authenticate(struct gb_cap *cap, u32 auth_type, u8 *uid,
0141                 u8 *challenge, u8 *result, u8 *auth_response,
0142                 u32 *signature_size, u8 *signature)
0143 {
0144     struct gb_connection *connection = cap->connection;
0145     struct gb_cap_authenticate_request *request;
0146     struct gb_cap_authenticate_response *response;
0147     size_t max_size = gb_operation_get_payload_size_max(connection);
0148     struct gb_operation *op;
0149     int ret;
0150 
0151     op = gb_operation_create_flags(connection, GB_CAP_TYPE_AUTHENTICATE,
0152                        sizeof(*request), max_size,
0153                        GB_OPERATION_FLAG_SHORT_RESPONSE,
0154                        GFP_KERNEL);
0155     if (!op)
0156         return -ENOMEM;
0157 
0158     request = op->request->payload;
0159     request->auth_type = cpu_to_le32(auth_type);
0160     memcpy(request->uid, uid, sizeof(request->uid));
0161     memcpy(request->challenge, challenge, sizeof(request->challenge));
0162 
0163     ret = gb_operation_request_send_sync(op);
0164     if (ret) {
0165         dev_err(cap->parent, "failed to authenticate (%d)\n", ret);
0166         goto done;
0167     }
0168 
0169     response = op->response->payload;
0170     *result = response->result_code;
0171     *signature_size = op->response->payload_size - sizeof(*response);
0172     memcpy(auth_response, response->response, sizeof(response->response));
0173     memcpy(signature, response->signature, *signature_size);
0174 
0175 done:
0176     gb_operation_put(op);
0177     return ret;
0178 }
0179 
0180 /* Char device fops */
0181 
0182 static int cap_open(struct inode *inode, struct file *file)
0183 {
0184     struct gb_cap *cap = get_cap(inode->i_cdev);
0185 
0186     /* cap structure can't get freed until file descriptor is closed */
0187     if (cap) {
0188         file->private_data = cap;
0189         return 0;
0190     }
0191 
0192     return -ENODEV;
0193 }
0194 
0195 static int cap_release(struct inode *inode, struct file *file)
0196 {
0197     struct gb_cap *cap = file->private_data;
0198 
0199     put_cap(cap);
0200     return 0;
0201 }
0202 
0203 static int cap_ioctl(struct gb_cap *cap, unsigned int cmd,
0204              void __user *buf)
0205 {
0206     struct cap_ioc_get_endpoint_uid endpoint_uid;
0207     struct cap_ioc_get_ims_certificate *ims_cert;
0208     struct cap_ioc_authenticate *authenticate;
0209     size_t size;
0210     int ret;
0211 
0212     switch (cmd) {
0213     case CAP_IOC_GET_ENDPOINT_UID:
0214         ret = cap_get_endpoint_uid(cap, endpoint_uid.uid);
0215         if (ret)
0216             return ret;
0217 
0218         if (copy_to_user(buf, &endpoint_uid, sizeof(endpoint_uid)))
0219             return -EFAULT;
0220 
0221         return 0;
0222     case CAP_IOC_GET_IMS_CERTIFICATE:
0223         size = sizeof(*ims_cert);
0224         ims_cert = memdup_user(buf, size);
0225         if (IS_ERR(ims_cert))
0226             return PTR_ERR(ims_cert);
0227 
0228         ret = cap_get_ims_certificate(cap, ims_cert->certificate_class,
0229                           ims_cert->certificate_id,
0230                           ims_cert->certificate,
0231                           &ims_cert->cert_size,
0232                           &ims_cert->result_code);
0233         if (!ret && copy_to_user(buf, ims_cert, size))
0234             ret = -EFAULT;
0235         kfree(ims_cert);
0236 
0237         return ret;
0238     case CAP_IOC_AUTHENTICATE:
0239         size = sizeof(*authenticate);
0240         authenticate = memdup_user(buf, size);
0241         if (IS_ERR(authenticate))
0242             return PTR_ERR(authenticate);
0243 
0244         ret = cap_authenticate(cap, authenticate->auth_type,
0245                        authenticate->uid,
0246                        authenticate->challenge,
0247                        &authenticate->result_code,
0248                        authenticate->response,
0249                        &authenticate->signature_size,
0250                        authenticate->signature);
0251         if (!ret && copy_to_user(buf, authenticate, size))
0252             ret = -EFAULT;
0253         kfree(authenticate);
0254 
0255         return ret;
0256     default:
0257         return -ENOTTY;
0258     }
0259 }
0260 
0261 static long cap_ioctl_unlocked(struct file *file, unsigned int cmd,
0262                    unsigned long arg)
0263 {
0264     struct gb_cap *cap = file->private_data;
0265     struct gb_bundle *bundle = cap->connection->bundle;
0266     int ret = -ENODEV;
0267 
0268     /*
0269      * Serialize ioctls.
0270      *
0271      * We don't want the user to do multiple authentication operations in
0272      * parallel.
0273      *
0274      * This is also used to protect ->disabled, which is used to check if
0275      * the connection is getting disconnected, so that we don't start any
0276      * new operations.
0277      */
0278     mutex_lock(&cap->mutex);
0279     if (!cap->disabled) {
0280         ret = gb_pm_runtime_get_sync(bundle);
0281         if (!ret) {
0282             ret = cap_ioctl(cap, cmd, (void __user *)arg);
0283             gb_pm_runtime_put_autosuspend(bundle);
0284         }
0285     }
0286     mutex_unlock(&cap->mutex);
0287 
0288     return ret;
0289 }
0290 
0291 static const struct file_operations cap_fops = {
0292     .owner      = THIS_MODULE,
0293     .open       = cap_open,
0294     .release    = cap_release,
0295     .unlocked_ioctl = cap_ioctl_unlocked,
0296 };
0297 
0298 int gb_cap_connection_init(struct gb_connection *connection)
0299 {
0300     struct gb_cap *cap;
0301     int ret, minor;
0302 
0303     if (!connection)
0304         return 0;
0305 
0306     cap = kzalloc(sizeof(*cap), GFP_KERNEL);
0307     if (!cap)
0308         return -ENOMEM;
0309 
0310     cap->parent = &connection->bundle->dev;
0311     cap->connection = connection;
0312     mutex_init(&cap->mutex);
0313     gb_connection_set_data(connection, cap);
0314     kref_init(&cap->kref);
0315 
0316     mutex_lock(&list_mutex);
0317     list_add(&cap->node, &cap_list);
0318     mutex_unlock(&list_mutex);
0319 
0320     ret = gb_connection_enable(connection);
0321     if (ret)
0322         goto err_list_del;
0323 
0324     minor = ida_simple_get(&cap_minors_map, 0, NUM_MINORS, GFP_KERNEL);
0325     if (minor < 0) {
0326         ret = minor;
0327         goto err_connection_disable;
0328     }
0329 
0330     /* Add a char device to allow userspace to interact with cap */
0331     cap->dev_num = MKDEV(MAJOR(cap_dev_num), minor);
0332     cdev_init(&cap->cdev, &cap_fops);
0333 
0334     ret = cdev_add(&cap->cdev, cap->dev_num, 1);
0335     if (ret)
0336         goto err_remove_ida;
0337 
0338     /* Add a soft link to the previously added char-dev within the bundle */
0339     cap->class_device = device_create(cap_class, cap->parent, cap->dev_num,
0340                       NULL, "gb-authenticate-%d", minor);
0341     if (IS_ERR(cap->class_device)) {
0342         ret = PTR_ERR(cap->class_device);
0343         goto err_del_cdev;
0344     }
0345 
0346     return 0;
0347 
0348 err_del_cdev:
0349     cdev_del(&cap->cdev);
0350 err_remove_ida:
0351     ida_simple_remove(&cap_minors_map, minor);
0352 err_connection_disable:
0353     gb_connection_disable(connection);
0354 err_list_del:
0355     mutex_lock(&list_mutex);
0356     list_del(&cap->node);
0357     mutex_unlock(&list_mutex);
0358 
0359     put_cap(cap);
0360 
0361     return ret;
0362 }
0363 
0364 void gb_cap_connection_exit(struct gb_connection *connection)
0365 {
0366     struct gb_cap *cap;
0367 
0368     if (!connection)
0369         return;
0370 
0371     cap = gb_connection_get_data(connection);
0372 
0373     device_destroy(cap_class, cap->dev_num);
0374     cdev_del(&cap->cdev);
0375     ida_simple_remove(&cap_minors_map, MINOR(cap->dev_num));
0376 
0377     /*
0378      * Disallow any new ioctl operations on the char device and wait for
0379      * existing ones to finish.
0380      */
0381     mutex_lock(&cap->mutex);
0382     cap->disabled = true;
0383     mutex_unlock(&cap->mutex);
0384 
0385     /* All pending greybus operations should have finished by now */
0386     gb_connection_disable(cap->connection);
0387 
0388     /* Disallow new users to get access to the cap structure */
0389     mutex_lock(&list_mutex);
0390     list_del(&cap->node);
0391     mutex_unlock(&list_mutex);
0392 
0393     /*
0394      * All current users of cap would have taken a reference to it by
0395      * now, we can drop our reference and wait the last user will get
0396      * cap freed.
0397      */
0398     put_cap(cap);
0399 }
0400 
0401 int cap_init(void)
0402 {
0403     int ret;
0404 
0405     cap_class = class_create(THIS_MODULE, "gb_authenticate");
0406     if (IS_ERR(cap_class))
0407         return PTR_ERR(cap_class);
0408 
0409     ret = alloc_chrdev_region(&cap_dev_num, 0, NUM_MINORS,
0410                   "gb_authenticate");
0411     if (ret)
0412         goto err_remove_class;
0413 
0414     return 0;
0415 
0416 err_remove_class:
0417     class_destroy(cap_class);
0418     return ret;
0419 }
0420 
0421 void cap_exit(void)
0422 {
0423     unregister_chrdev_region(cap_dev_num, NUM_MINORS);
0424     class_destroy(cap_class);
0425     ida_destroy(&cap_minors_map);
0426 }