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