Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 Red Hat, Inc. |
| 4 | * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com> |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/efi.h> |
Luck, Tony | bef3efb | 2018-02-22 09:15:06 -0800 | [diff] [blame] | 8 | #include <linux/delay.h> |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 9 | #include <linux/fs.h> |
Linus Torvalds | 20b4fb4 | 2013-05-01 17:51:54 -0700 | [diff] [blame] | 10 | #include <linux/slab.h> |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 11 | #include <linux/mount.h> |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 12 | |
| 13 | #include "internal.h" |
| 14 | |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 15 | static ssize_t efivarfs_file_write(struct file *file, |
| 16 | const char __user *userbuf, size_t count, loff_t *ppos) |
| 17 | { |
| 18 | struct efivar_entry *var = file->private_data; |
| 19 | void *data; |
| 20 | u32 attributes; |
| 21 | struct inode *inode = file->f_mapping->host; |
| 22 | unsigned long datasize = count - sizeof(attributes); |
Geyslan G. Bem | aca32b5 | 2013-10-30 15:57:41 -0300 | [diff] [blame] | 23 | ssize_t bytes; |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 24 | bool set = false; |
| 25 | |
| 26 | if (count < sizeof(attributes)) |
| 27 | return -EINVAL; |
| 28 | |
| 29 | if (copy_from_user(&attributes, userbuf, sizeof(attributes))) |
| 30 | return -EFAULT; |
| 31 | |
| 32 | if (attributes & ~(EFI_VARIABLE_MASK)) |
| 33 | return -EINVAL; |
| 34 | |
Geyslan G. Bem | aca32b5 | 2013-10-30 15:57:41 -0300 | [diff] [blame] | 35 | data = memdup_user(userbuf + sizeof(attributes), datasize); |
| 36 | if (IS_ERR(data)) |
| 37 | return PTR_ERR(data); |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 38 | |
| 39 | bytes = efivar_entry_set_get_size(var, attributes, &datasize, |
| 40 | data, &set); |
Lingzhu Xiang | 3fab70c | 2013-05-10 18:29:21 +0800 | [diff] [blame] | 41 | if (!set && bytes) { |
| 42 | if (bytes == -ENOENT) |
| 43 | bytes = -EIO; |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 44 | goto out; |
Lingzhu Xiang | 3fab70c | 2013-05-10 18:29:21 +0800 | [diff] [blame] | 45 | } |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 46 | |
| 47 | if (bytes == -ENOENT) { |
| 48 | drop_nlink(inode); |
Al Viro | b583043 | 2014-10-31 01:22:04 -0400 | [diff] [blame] | 49 | d_delete(file->f_path.dentry); |
| 50 | dput(file->f_path.dentry); |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 51 | } else { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 52 | inode_lock(inode); |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 53 | i_size_write(inode, datasize + sizeof(attributes)); |
Tony Luck | 2096721f | 2020-05-28 12:49:04 -0700 | [diff] [blame] | 54 | inode->i_mtime = current_time(inode); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 55 | inode_unlock(inode); |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | bytes = count; |
| 59 | |
| 60 | out: |
| 61 | kfree(data); |
| 62 | |
| 63 | return bytes; |
| 64 | } |
| 65 | |
| 66 | static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf, |
| 67 | size_t count, loff_t *ppos) |
| 68 | { |
| 69 | struct efivar_entry *var = file->private_data; |
| 70 | unsigned long datasize = 0; |
| 71 | u32 attributes; |
| 72 | void *data; |
| 73 | ssize_t size = 0; |
| 74 | int err; |
| 75 | |
Tony Luck | 4353f03 | 2020-05-28 12:49:05 -0700 | [diff] [blame] | 76 | while (!__ratelimit(&file->f_cred->user->ratelimit)) |
| 77 | msleep(50); |
Luck, Tony | bef3efb | 2018-02-22 09:15:06 -0800 | [diff] [blame] | 78 | |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 79 | err = efivar_entry_size(var, &datasize); |
Lingzhu Xiang | 3fab70c | 2013-05-10 18:29:21 +0800 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | * efivarfs represents uncommitted variables with |
| 83 | * zero-length files. Reading them should return EOF. |
| 84 | */ |
| 85 | if (err == -ENOENT) |
| 86 | return 0; |
| 87 | else if (err) |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 88 | return err; |
| 89 | |
| 90 | data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL); |
| 91 | |
| 92 | if (!data) |
| 93 | return -ENOMEM; |
| 94 | |
| 95 | size = efivar_entry_get(var, &attributes, &datasize, |
| 96 | data + sizeof(attributes)); |
| 97 | if (size) |
| 98 | goto out_free; |
| 99 | |
| 100 | memcpy(data, &attributes, sizeof(attributes)); |
| 101 | size = simple_read_from_buffer(userbuf, count, ppos, |
| 102 | data, datasize + sizeof(attributes)); |
| 103 | out_free: |
| 104 | kfree(data); |
| 105 | |
| 106 | return size; |
| 107 | } |
| 108 | |
Darrick J. Wong | 5aca284 | 2019-07-01 08:25:34 -0700 | [diff] [blame] | 109 | static inline unsigned int efivarfs_getflags(struct inode *inode) |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 110 | { |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 111 | unsigned int i_flags; |
| 112 | unsigned int flags = 0; |
| 113 | |
| 114 | i_flags = inode->i_flags; |
| 115 | if (i_flags & S_IMMUTABLE) |
| 116 | flags |= FS_IMMUTABLE_FL; |
Darrick J. Wong | 5aca284 | 2019-07-01 08:25:34 -0700 | [diff] [blame] | 117 | return flags; |
| 118 | } |
| 119 | |
| 120 | static int |
| 121 | efivarfs_ioc_getxflags(struct file *file, void __user *arg) |
| 122 | { |
| 123 | struct inode *inode = file->f_mapping->host; |
| 124 | unsigned int flags = efivarfs_getflags(inode); |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 125 | |
| 126 | if (copy_to_user(arg, &flags, sizeof(flags))) |
| 127 | return -EFAULT; |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static int |
| 132 | efivarfs_ioc_setxflags(struct file *file, void __user *arg) |
| 133 | { |
| 134 | struct inode *inode = file->f_mapping->host; |
| 135 | unsigned int flags; |
| 136 | unsigned int i_flags = 0; |
Darrick J. Wong | 5aca284 | 2019-07-01 08:25:34 -0700 | [diff] [blame] | 137 | unsigned int oldflags = efivarfs_getflags(inode); |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 138 | int error; |
| 139 | |
| 140 | if (!inode_owner_or_capable(inode)) |
| 141 | return -EACCES; |
| 142 | |
| 143 | if (copy_from_user(&flags, arg, sizeof(flags))) |
| 144 | return -EFAULT; |
| 145 | |
| 146 | if (flags & ~FS_IMMUTABLE_FL) |
| 147 | return -EOPNOTSUPP; |
| 148 | |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 149 | if (flags & FS_IMMUTABLE_FL) |
| 150 | i_flags |= S_IMMUTABLE; |
| 151 | |
| 152 | |
| 153 | error = mnt_want_write_file(file); |
| 154 | if (error) |
| 155 | return error; |
| 156 | |
| 157 | inode_lock(inode); |
Darrick J. Wong | 5aca284 | 2019-07-01 08:25:34 -0700 | [diff] [blame] | 158 | |
| 159 | error = vfs_ioc_setflags_prepare(inode, oldflags, flags); |
| 160 | if (error) |
| 161 | goto out; |
| 162 | |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 163 | inode_set_flags(inode, i_flags, S_IMMUTABLE); |
Darrick J. Wong | 5aca284 | 2019-07-01 08:25:34 -0700 | [diff] [blame] | 164 | out: |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 165 | inode_unlock(inode); |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 166 | mnt_drop_write_file(file); |
Darrick J. Wong | 5aca284 | 2019-07-01 08:25:34 -0700 | [diff] [blame] | 167 | return error; |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 168 | } |
| 169 | |
Peter Jones | 6c5450e | 2016-05-06 22:39:31 +0100 | [diff] [blame] | 170 | static long |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 171 | efivarfs_file_ioctl(struct file *file, unsigned int cmd, unsigned long p) |
| 172 | { |
| 173 | void __user *arg = (void __user *)p; |
| 174 | |
| 175 | switch (cmd) { |
| 176 | case FS_IOC_GETFLAGS: |
| 177 | return efivarfs_ioc_getxflags(file, arg); |
| 178 | case FS_IOC_SETFLAGS: |
| 179 | return efivarfs_ioc_setxflags(file, arg); |
| 180 | } |
| 181 | |
| 182 | return -ENOTTY; |
| 183 | } |
| 184 | |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 185 | const struct file_operations efivarfs_file_operations = { |
H. Peter Anvin | f53f292 | 2013-04-20 09:16:44 -0700 | [diff] [blame] | 186 | .open = simple_open, |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 187 | .read = efivarfs_file_read, |
| 188 | .write = efivarfs_file_write, |
| 189 | .llseek = no_llseek, |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 190 | .unlocked_ioctl = efivarfs_file_ioctl, |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 191 | }; |