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