blob: 8e568428c88be059299b5ada7eb48e7d289a04fe [file] [log] [blame]
Matt Flemingd68772b2013-02-08 16:27:24 +00001/*
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>
Luck, Tonybef3efb2018-02-22 09:15:06 -080011#include <linux/delay.h>
Matt Flemingd68772b2013-02-08 16:27:24 +000012#include <linux/fs.h>
Linus Torvalds20b4fb42013-05-01 17:51:54 -070013#include <linux/slab.h>
Peter Jonesed8b0de2016-02-08 14:48:15 -050014#include <linux/mount.h>
Matt Flemingd68772b2013-02-08 16:27:24 +000015
16#include "internal.h"
17
Matt Flemingd68772b2013-02-08 16:27:24 +000018static ssize_t efivarfs_file_write(struct file *file,
19 const char __user *userbuf, size_t count, loff_t *ppos)
20{
21 struct efivar_entry *var = file->private_data;
22 void *data;
23 u32 attributes;
24 struct inode *inode = file->f_mapping->host;
25 unsigned long datasize = count - sizeof(attributes);
Geyslan G. Bemaca32b52013-10-30 15:57:41 -030026 ssize_t bytes;
Matt Flemingd68772b2013-02-08 16:27:24 +000027 bool set = false;
28
29 if (count < sizeof(attributes))
30 return -EINVAL;
31
32 if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
33 return -EFAULT;
34
35 if (attributes & ~(EFI_VARIABLE_MASK))
36 return -EINVAL;
37
Geyslan G. Bemaca32b52013-10-30 15:57:41 -030038 data = memdup_user(userbuf + sizeof(attributes), datasize);
39 if (IS_ERR(data))
40 return PTR_ERR(data);
Matt Flemingd68772b2013-02-08 16:27:24 +000041
42 bytes = efivar_entry_set_get_size(var, attributes, &datasize,
43 data, &set);
Lingzhu Xiang3fab70c2013-05-10 18:29:21 +080044 if (!set && bytes) {
45 if (bytes == -ENOENT)
46 bytes = -EIO;
Matt Flemingd68772b2013-02-08 16:27:24 +000047 goto out;
Lingzhu Xiang3fab70c2013-05-10 18:29:21 +080048 }
Matt Flemingd68772b2013-02-08 16:27:24 +000049
50 if (bytes == -ENOENT) {
51 drop_nlink(inode);
Al Virob5830432014-10-31 01:22:04 -040052 d_delete(file->f_path.dentry);
53 dput(file->f_path.dentry);
Matt Flemingd68772b2013-02-08 16:27:24 +000054 } else {
Al Viro59551022016-01-22 15:40:57 -050055 inode_lock(inode);
Matt Flemingd68772b2013-02-08 16:27:24 +000056 i_size_write(inode, datasize + sizeof(attributes));
Al Viro59551022016-01-22 15:40:57 -050057 inode_unlock(inode);
Matt Flemingd68772b2013-02-08 16:27:24 +000058 }
59
60 bytes = count;
61
62out:
63 kfree(data);
64
65 return bytes;
66}
67
68static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
69 size_t count, loff_t *ppos)
70{
71 struct efivar_entry *var = file->private_data;
72 unsigned long datasize = 0;
73 u32 attributes;
74 void *data;
75 ssize_t size = 0;
76 int err;
77
Luck, Tonybef3efb2018-02-22 09:15:06 -080078 while (!__ratelimit(&file->f_cred->user->ratelimit)) {
79 if (!msleep_interruptible(50))
80 return -EINTR;
81 }
82
Matt Flemingd68772b2013-02-08 16:27:24 +000083 err = efivar_entry_size(var, &datasize);
Lingzhu Xiang3fab70c2013-05-10 18:29:21 +080084
85 /*
86 * efivarfs represents uncommitted variables with
87 * zero-length files. Reading them should return EOF.
88 */
89 if (err == -ENOENT)
90 return 0;
91 else if (err)
Matt Flemingd68772b2013-02-08 16:27:24 +000092 return err;
93
94 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
95
96 if (!data)
97 return -ENOMEM;
98
99 size = efivar_entry_get(var, &attributes, &datasize,
100 data + sizeof(attributes));
101 if (size)
102 goto out_free;
103
104 memcpy(data, &attributes, sizeof(attributes));
105 size = simple_read_from_buffer(userbuf, count, ppos,
106 data, datasize + sizeof(attributes));
107out_free:
108 kfree(data);
109
110 return size;
111}
112
Peter Jonesed8b0de2016-02-08 14:48:15 -0500113static int
114efivarfs_ioc_getxflags(struct file *file, void __user *arg)
115{
116 struct inode *inode = file->f_mapping->host;
117 unsigned int i_flags;
118 unsigned int flags = 0;
119
120 i_flags = inode->i_flags;
121 if (i_flags & S_IMMUTABLE)
122 flags |= FS_IMMUTABLE_FL;
123
124 if (copy_to_user(arg, &flags, sizeof(flags)))
125 return -EFAULT;
126 return 0;
127}
128
129static int
130efivarfs_ioc_setxflags(struct file *file, void __user *arg)
131{
132 struct inode *inode = file->f_mapping->host;
133 unsigned int flags;
134 unsigned int i_flags = 0;
135 int error;
136
137 if (!inode_owner_or_capable(inode))
138 return -EACCES;
139
140 if (copy_from_user(&flags, arg, sizeof(flags)))
141 return -EFAULT;
142
143 if (flags & ~FS_IMMUTABLE_FL)
144 return -EOPNOTSUPP;
145
146 if (!capable(CAP_LINUX_IMMUTABLE))
147 return -EPERM;
148
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);
158 inode_set_flags(inode, i_flags, S_IMMUTABLE);
159 inode_unlock(inode);
160
161 mnt_drop_write_file(file);
162
163 return 0;
164}
165
Peter Jones6c5450e2016-05-06 22:39:31 +0100166static long
Peter Jonesed8b0de2016-02-08 14:48:15 -0500167efivarfs_file_ioctl(struct file *file, unsigned int cmd, unsigned long p)
168{
169 void __user *arg = (void __user *)p;
170
171 switch (cmd) {
172 case FS_IOC_GETFLAGS:
173 return efivarfs_ioc_getxflags(file, arg);
174 case FS_IOC_SETFLAGS:
175 return efivarfs_ioc_setxflags(file, arg);
176 }
177
178 return -ENOTTY;
179}
180
Matt Flemingd68772b2013-02-08 16:27:24 +0000181const struct file_operations efivarfs_file_operations = {
H. Peter Anvinf53f2922013-04-20 09:16:44 -0700182 .open = simple_open,
Matt Flemingd68772b2013-02-08 16:27:24 +0000183 .read = efivarfs_file_read,
184 .write = efivarfs_file_write,
185 .llseek = no_llseek,
Peter Jonesed8b0de2016-02-08 14:48:15 -0500186 .unlocked_ioctl = efivarfs_file_ioctl,
Matt Flemingd68772b2013-02-08 16:27:24 +0000187};