Mark Salyzyn | 9d5438f | 2015-01-16 16:01:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google, Inc. |
| 3 | * |
| 4 | * This software is licensed under the terms of the GNU General Public |
| 5 | * License version 2, as published by the Free Software Foundation, and |
| 6 | * may be copied, distributed, and modified under those terms. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/cdev.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/fs.h> |
| 17 | #include <linux/uaccess.h> |
| 18 | #include <linux/vmalloc.h> |
| 19 | #include "internal.h" |
| 20 | |
| 21 | static DEFINE_MUTEX(pmsg_lock); |
Mark Salyzyn | 9d5438f | 2015-01-16 16:01:10 -0800 | [diff] [blame] | 22 | |
| 23 | static ssize_t write_pmsg(struct file *file, const char __user *buf, |
| 24 | size_t count, loff_t *ppos) |
| 25 | { |
Kees Cook | fdd0311 | 2017-03-05 00:56:38 -0800 | [diff] [blame] | 26 | struct pstore_record record = { |
| 27 | .type = PSTORE_TYPE_PMSG, |
| 28 | .size = count, |
| 29 | .psi = psinfo, |
| 30 | }; |
Mark Salyzyn | 5bf6d1b | 2016-09-01 08:13:46 -0700 | [diff] [blame] | 31 | int ret; |
Mark Salyzyn | 9d5438f | 2015-01-16 16:01:10 -0800 | [diff] [blame] | 32 | |
| 33 | if (!count) |
| 34 | return 0; |
| 35 | |
Kees Cook | 4c9ec21 | 2017-03-05 22:41:10 -0800 | [diff] [blame^] | 36 | /* check outside lock, page in any data. write_user also checks */ |
Mark Salyzyn | 9d5438f | 2015-01-16 16:01:10 -0800 | [diff] [blame] | 37 | if (!access_ok(VERIFY_READ, buf, count)) |
| 38 | return -EFAULT; |
| 39 | |
Mark Salyzyn | 9d5438f | 2015-01-16 16:01:10 -0800 | [diff] [blame] | 40 | mutex_lock(&pmsg_lock); |
Kees Cook | 4c9ec21 | 2017-03-05 22:41:10 -0800 | [diff] [blame^] | 41 | ret = psinfo->write_user(&record, buf); |
Mark Salyzyn | 9d5438f | 2015-01-16 16:01:10 -0800 | [diff] [blame] | 42 | mutex_unlock(&pmsg_lock); |
Mark Salyzyn | 5bf6d1b | 2016-09-01 08:13:46 -0700 | [diff] [blame] | 43 | return ret ? ret : count; |
Mark Salyzyn | 9d5438f | 2015-01-16 16:01:10 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | static const struct file_operations pmsg_fops = { |
| 47 | .owner = THIS_MODULE, |
| 48 | .llseek = noop_llseek, |
| 49 | .write = write_pmsg, |
| 50 | }; |
| 51 | |
| 52 | static struct class *pmsg_class; |
| 53 | static int pmsg_major; |
| 54 | #define PMSG_NAME "pmsg" |
| 55 | #undef pr_fmt |
| 56 | #define pr_fmt(fmt) PMSG_NAME ": " fmt |
| 57 | |
| 58 | static char *pmsg_devnode(struct device *dev, umode_t *mode) |
| 59 | { |
| 60 | if (mode) |
| 61 | *mode = 0220; |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | void pstore_register_pmsg(void) |
| 66 | { |
| 67 | struct device *pmsg_device; |
| 68 | |
| 69 | pmsg_major = register_chrdev(0, PMSG_NAME, &pmsg_fops); |
| 70 | if (pmsg_major < 0) { |
| 71 | pr_err("register_chrdev failed\n"); |
| 72 | goto err; |
| 73 | } |
| 74 | |
| 75 | pmsg_class = class_create(THIS_MODULE, PMSG_NAME); |
| 76 | if (IS_ERR(pmsg_class)) { |
| 77 | pr_err("device class file already in use\n"); |
| 78 | goto err_class; |
| 79 | } |
| 80 | pmsg_class->devnode = pmsg_devnode; |
| 81 | |
| 82 | pmsg_device = device_create(pmsg_class, NULL, MKDEV(pmsg_major, 0), |
| 83 | NULL, "%s%d", PMSG_NAME, 0); |
| 84 | if (IS_ERR(pmsg_device)) { |
| 85 | pr_err("failed to create device\n"); |
| 86 | goto err_device; |
| 87 | } |
| 88 | return; |
| 89 | |
| 90 | err_device: |
| 91 | class_destroy(pmsg_class); |
| 92 | err_class: |
| 93 | unregister_chrdev(pmsg_major, PMSG_NAME); |
| 94 | err: |
| 95 | return; |
| 96 | } |
Geliang Tang | ee1d267 | 2015-10-20 00:39:03 -0700 | [diff] [blame] | 97 | |
| 98 | void pstore_unregister_pmsg(void) |
| 99 | { |
| 100 | device_destroy(pmsg_class, MKDEV(pmsg_major, 0)); |
| 101 | class_destroy(pmsg_class); |
| 102 | unregister_chrdev(pmsg_major, PMSG_NAME); |
| 103 | } |