blob: e9e27a271af0b003e2c2c3e01b2423504ffa72f5 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Matt Flemingd68772b2013-02-08 16:27:24 +00002/*
3 * Copyright (C) 2012 Red Hat, Inc.
4 * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
Matt Flemingd68772b2013-02-08 16:27:24 +00005 */
6
7#include <linux/efi.h>
Luck, Tonybef3efb2018-02-22 09:15:06 -08008#include <linux/delay.h>
Matt Flemingd68772b2013-02-08 16:27:24 +00009#include <linux/fs.h>
Linus Torvalds20b4fb42013-05-01 17:51:54 -070010#include <linux/slab.h>
Peter Jonesed8b0de2016-02-08 14:48:15 -050011#include <linux/mount.h>
Matt Flemingd68772b2013-02-08 16:27:24 +000012
13#include "internal.h"
14
Matt Flemingd68772b2013-02-08 16:27:24 +000015static 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. Bemaca32b52013-10-30 15:57:41 -030023 ssize_t bytes;
Matt Flemingd68772b2013-02-08 16:27:24 +000024 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. Bemaca32b52013-10-30 15:57:41 -030035 data = memdup_user(userbuf + sizeof(attributes), datasize);
36 if (IS_ERR(data))
37 return PTR_ERR(data);
Matt Flemingd68772b2013-02-08 16:27:24 +000038
39 bytes = efivar_entry_set_get_size(var, attributes, &datasize,
40 data, &set);
Lingzhu Xiang3fab70c2013-05-10 18:29:21 +080041 if (!set && bytes) {
42 if (bytes == -ENOENT)
43 bytes = -EIO;
Matt Flemingd68772b2013-02-08 16:27:24 +000044 goto out;
Lingzhu Xiang3fab70c2013-05-10 18:29:21 +080045 }
Matt Flemingd68772b2013-02-08 16:27:24 +000046
47 if (bytes == -ENOENT) {
48 drop_nlink(inode);
Al Virob5830432014-10-31 01:22:04 -040049 d_delete(file->f_path.dentry);
50 dput(file->f_path.dentry);
Matt Flemingd68772b2013-02-08 16:27:24 +000051 } else {
Al Viro59551022016-01-22 15:40:57 -050052 inode_lock(inode);
Matt Flemingd68772b2013-02-08 16:27:24 +000053 i_size_write(inode, datasize + sizeof(attributes));
Al Viro59551022016-01-22 15:40:57 -050054 inode_unlock(inode);
Matt Flemingd68772b2013-02-08 16:27:24 +000055 }
56
57 bytes = count;
58
59out:
60 kfree(data);
61
62 return bytes;
63}
64
65static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
66 size_t count, loff_t *ppos)
67{
68 struct efivar_entry *var = file->private_data;
69 unsigned long datasize = 0;
70 u32 attributes;
71 void *data;
72 ssize_t size = 0;
73 int err;
74
Luck, Tonybef3efb2018-02-22 09:15:06 -080075 while (!__ratelimit(&file->f_cred->user->ratelimit)) {
76 if (!msleep_interruptible(50))
77 return -EINTR;
78 }
79
Matt Flemingd68772b2013-02-08 16:27:24 +000080 err = efivar_entry_size(var, &datasize);
Lingzhu Xiang3fab70c2013-05-10 18:29:21 +080081
82 /*
83 * efivarfs represents uncommitted variables with
84 * zero-length files. Reading them should return EOF.
85 */
86 if (err == -ENOENT)
87 return 0;
88 else if (err)
Matt Flemingd68772b2013-02-08 16:27:24 +000089 return err;
90
91 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
92
93 if (!data)
94 return -ENOMEM;
95
96 size = efivar_entry_get(var, &attributes, &datasize,
97 data + sizeof(attributes));
98 if (size)
99 goto out_free;
100
101 memcpy(data, &attributes, sizeof(attributes));
102 size = simple_read_from_buffer(userbuf, count, ppos,
103 data, datasize + sizeof(attributes));
104out_free:
105 kfree(data);
106
107 return size;
108}
109
Darrick J. Wong5aca2842019-07-01 08:25:34 -0700110static inline unsigned int efivarfs_getflags(struct inode *inode)
Peter Jonesed8b0de2016-02-08 14:48:15 -0500111{
Peter Jonesed8b0de2016-02-08 14:48:15 -0500112 unsigned int i_flags;
113 unsigned int flags = 0;
114
115 i_flags = inode->i_flags;
116 if (i_flags & S_IMMUTABLE)
117 flags |= FS_IMMUTABLE_FL;
Darrick J. Wong5aca2842019-07-01 08:25:34 -0700118 return flags;
119}
120
121static int
122efivarfs_ioc_getxflags(struct file *file, void __user *arg)
123{
124 struct inode *inode = file->f_mapping->host;
125 unsigned int flags = efivarfs_getflags(inode);
Peter Jonesed8b0de2016-02-08 14:48:15 -0500126
127 if (copy_to_user(arg, &flags, sizeof(flags)))
128 return -EFAULT;
129 return 0;
130}
131
132static int
133efivarfs_ioc_setxflags(struct file *file, void __user *arg)
134{
135 struct inode *inode = file->f_mapping->host;
136 unsigned int flags;
137 unsigned int i_flags = 0;
Darrick J. Wong5aca2842019-07-01 08:25:34 -0700138 unsigned int oldflags = efivarfs_getflags(inode);
Peter Jonesed8b0de2016-02-08 14:48:15 -0500139 int error;
140
141 if (!inode_owner_or_capable(inode))
142 return -EACCES;
143
144 if (copy_from_user(&flags, arg, sizeof(flags)))
145 return -EFAULT;
146
147 if (flags & ~FS_IMMUTABLE_FL)
148 return -EOPNOTSUPP;
149
Peter Jonesed8b0de2016-02-08 14:48:15 -0500150 if (flags & FS_IMMUTABLE_FL)
151 i_flags |= S_IMMUTABLE;
152
153
154 error = mnt_want_write_file(file);
155 if (error)
156 return error;
157
158 inode_lock(inode);
Darrick J. Wong5aca2842019-07-01 08:25:34 -0700159
160 error = vfs_ioc_setflags_prepare(inode, oldflags, flags);
161 if (error)
162 goto out;
163
Peter Jonesed8b0de2016-02-08 14:48:15 -0500164 inode_set_flags(inode, i_flags, S_IMMUTABLE);
Darrick J. Wong5aca2842019-07-01 08:25:34 -0700165out:
Peter Jonesed8b0de2016-02-08 14:48:15 -0500166 inode_unlock(inode);
Peter Jonesed8b0de2016-02-08 14:48:15 -0500167 mnt_drop_write_file(file);
Darrick J. Wong5aca2842019-07-01 08:25:34 -0700168 return error;
Peter Jonesed8b0de2016-02-08 14:48:15 -0500169}
170
Peter Jones6c5450e2016-05-06 22:39:31 +0100171static long
Peter Jonesed8b0de2016-02-08 14:48:15 -0500172efivarfs_file_ioctl(struct file *file, unsigned int cmd, unsigned long p)
173{
174 void __user *arg = (void __user *)p;
175
176 switch (cmd) {
177 case FS_IOC_GETFLAGS:
178 return efivarfs_ioc_getxflags(file, arg);
179 case FS_IOC_SETFLAGS:
180 return efivarfs_ioc_setxflags(file, arg);
181 }
182
183 return -ENOTTY;
184}
185
Matt Flemingd68772b2013-02-08 16:27:24 +0000186const struct file_operations efivarfs_file_operations = {
H. Peter Anvinf53f2922013-04-20 09:16:44 -0700187 .open = simple_open,
Matt Flemingd68772b2013-02-08 16:27:24 +0000188 .read = efivarfs_file_read,
189 .write = efivarfs_file_write,
190 .llseek = no_llseek,
Peter Jonesed8b0de2016-02-08 14:48:15 -0500191 .unlocked_ioctl = efivarfs_file_ioctl,
Matt Flemingd68772b2013-02-08 16:27:24 +0000192};