file.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #include <linux/efi.h>
  10. #include <linux/fs.h>
  11. #include <linux/slab.h>
  12. #include <linux/mount.h>
  13. #include "internal.h"
  14. static ssize_t efivarfs_file_write(struct file *file,
  15. const char __user *userbuf, size_t count, loff_t *ppos)
  16. {
  17. struct efivar_entry *var = file->private_data;
  18. void *data;
  19. u32 attributes;
  20. struct inode *inode = file->f_mapping->host;
  21. unsigned long datasize = count - sizeof(attributes);
  22. ssize_t bytes;
  23. bool set = false;
  24. if (count < sizeof(attributes))
  25. return -EINVAL;
  26. if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
  27. return -EFAULT;
  28. if (attributes & ~(EFI_VARIABLE_MASK))
  29. return -EINVAL;
  30. data = memdup_user(userbuf + sizeof(attributes), datasize);
  31. if (IS_ERR(data))
  32. return PTR_ERR(data);
  33. bytes = efivar_entry_set_get_size(var, attributes, &datasize,
  34. data, &set);
  35. if (!set && bytes) {
  36. if (bytes == -ENOENT)
  37. bytes = -EIO;
  38. goto out;
  39. }
  40. if (bytes == -ENOENT) {
  41. drop_nlink(inode);
  42. d_delete(file->f_path.dentry);
  43. dput(file->f_path.dentry);
  44. } else {
  45. inode_lock(inode);
  46. i_size_write(inode, datasize + sizeof(attributes));
  47. inode_unlock(inode);
  48. }
  49. bytes = count;
  50. out:
  51. kfree(data);
  52. return bytes;
  53. }
  54. static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
  55. size_t count, loff_t *ppos)
  56. {
  57. struct efivar_entry *var = file->private_data;
  58. unsigned long datasize = 0;
  59. u32 attributes;
  60. void *data;
  61. ssize_t size = 0;
  62. int err;
  63. err = efivar_entry_size(var, &datasize);
  64. /*
  65. * efivarfs represents uncommitted variables with
  66. * zero-length files. Reading them should return EOF.
  67. */
  68. if (err == -ENOENT)
  69. return 0;
  70. else if (err)
  71. return err;
  72. data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
  73. if (!data)
  74. return -ENOMEM;
  75. size = efivar_entry_get(var, &attributes, &datasize,
  76. data + sizeof(attributes));
  77. if (size)
  78. goto out_free;
  79. memcpy(data, &attributes, sizeof(attributes));
  80. size = simple_read_from_buffer(userbuf, count, ppos,
  81. data, datasize + sizeof(attributes));
  82. out_free:
  83. kfree(data);
  84. return size;
  85. }
  86. static int
  87. efivarfs_ioc_getxflags(struct file *file, void __user *arg)
  88. {
  89. struct inode *inode = file->f_mapping->host;
  90. unsigned int i_flags;
  91. unsigned int flags = 0;
  92. i_flags = inode->i_flags;
  93. if (i_flags & S_IMMUTABLE)
  94. flags |= FS_IMMUTABLE_FL;
  95. if (copy_to_user(arg, &flags, sizeof(flags)))
  96. return -EFAULT;
  97. return 0;
  98. }
  99. static int
  100. efivarfs_ioc_setxflags(struct file *file, void __user *arg)
  101. {
  102. struct inode *inode = file->f_mapping->host;
  103. unsigned int flags;
  104. unsigned int i_flags = 0;
  105. int error;
  106. if (!inode_owner_or_capable(inode))
  107. return -EACCES;
  108. if (copy_from_user(&flags, arg, sizeof(flags)))
  109. return -EFAULT;
  110. if (flags & ~FS_IMMUTABLE_FL)
  111. return -EOPNOTSUPP;
  112. if (!capable(CAP_LINUX_IMMUTABLE))
  113. return -EPERM;
  114. if (flags & FS_IMMUTABLE_FL)
  115. i_flags |= S_IMMUTABLE;
  116. error = mnt_want_write_file(file);
  117. if (error)
  118. return error;
  119. inode_lock(inode);
  120. inode_set_flags(inode, i_flags, S_IMMUTABLE);
  121. inode_unlock(inode);
  122. mnt_drop_write_file(file);
  123. return 0;
  124. }
  125. static long
  126. efivarfs_file_ioctl(struct file *file, unsigned int cmd, unsigned long p)
  127. {
  128. void __user *arg = (void __user *)p;
  129. switch (cmd) {
  130. case FS_IOC_GETFLAGS:
  131. return efivarfs_ioc_getxflags(file, arg);
  132. case FS_IOC_SETFLAGS:
  133. return efivarfs_ioc_setxflags(file, arg);
  134. }
  135. return -ENOTTY;
  136. }
  137. const struct file_operations efivarfs_file_operations = {
  138. .open = simple_open,
  139. .read = efivarfs_file_read,
  140. .write = efivarfs_file_write,
  141. .llseek = no_llseek,
  142. .unlocked_ioctl = efivarfs_file_ioctl,
  143. };