kernel_read_file.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/fs.h>
  3. #include <linux/fs_struct.h>
  4. #include <linux/kernel_read_file.h>
  5. #include <linux/security.h>
  6. #include <linux/vmalloc.h>
  7. /**
  8. * kernel_read_file() - read file contents into a kernel buffer
  9. *
  10. * @file file to read from
  11. * @offset where to start reading from (see below).
  12. * @buf pointer to a "void *" buffer for reading into (if
  13. * *@buf is NULL, a buffer will be allocated, and
  14. * @buf_size will be ignored)
  15. * @buf_size size of buf, if already allocated. If @buf not
  16. * allocated, this is the largest size to allocate.
  17. * @file_size if non-NULL, the full size of @file will be
  18. * written here.
  19. * @id the kernel_read_file_id identifying the type of
  20. * file contents being read (for LSMs to examine)
  21. *
  22. * @offset must be 0 unless both @buf and @file_size are non-NULL
  23. * (i.e. the caller must be expecting to read partial file contents
  24. * via an already-allocated @buf, in at most @buf_size chunks, and
  25. * will be able to determine when the entire file was read by
  26. * checking @file_size). This isn't a recommended way to read a
  27. * file, though, since it is possible that the contents might
  28. * change between calls to kernel_read_file().
  29. *
  30. * Returns number of bytes read (no single read will be bigger
  31. * than INT_MAX), or negative on error.
  32. *
  33. */
  34. int kernel_read_file(struct file *file, loff_t offset, void **buf,
  35. size_t buf_size, size_t *file_size,
  36. enum kernel_read_file_id id)
  37. {
  38. loff_t i_size, pos;
  39. size_t copied;
  40. void *allocated = NULL;
  41. bool whole_file;
  42. int ret;
  43. if (offset != 0 && (!*buf || !file_size))
  44. return -EINVAL;
  45. if (!S_ISREG(file_inode(file)->i_mode))
  46. return -EINVAL;
  47. ret = deny_write_access(file);
  48. if (ret)
  49. return ret;
  50. i_size = i_size_read(file_inode(file));
  51. if (i_size <= 0) {
  52. ret = -EINVAL;
  53. goto out;
  54. }
  55. /* The file is too big for sane activities. */
  56. if (i_size > INT_MAX) {
  57. ret = -EFBIG;
  58. goto out;
  59. }
  60. /* The entire file cannot be read in one buffer. */
  61. if (!file_size && offset == 0 && i_size > buf_size) {
  62. ret = -EFBIG;
  63. goto out;
  64. }
  65. whole_file = (offset == 0 && i_size <= buf_size);
  66. ret = security_kernel_read_file(file, id, whole_file);
  67. if (ret)
  68. goto out;
  69. if (file_size)
  70. *file_size = i_size;
  71. if (!*buf)
  72. *buf = allocated = vmalloc(i_size);
  73. if (!*buf) {
  74. ret = -ENOMEM;
  75. goto out;
  76. }
  77. pos = offset;
  78. copied = 0;
  79. while (copied < buf_size) {
  80. ssize_t bytes;
  81. size_t wanted = min_t(size_t, buf_size - copied,
  82. i_size - pos);
  83. bytes = kernel_read(file, *buf + copied, wanted, &pos);
  84. if (bytes < 0) {
  85. ret = bytes;
  86. goto out_free;
  87. }
  88. if (bytes == 0)
  89. break;
  90. copied += bytes;
  91. }
  92. if (whole_file) {
  93. if (pos != i_size) {
  94. ret = -EIO;
  95. goto out_free;
  96. }
  97. ret = security_kernel_post_read_file(file, *buf, i_size, id);
  98. }
  99. out_free:
  100. if (ret < 0) {
  101. if (allocated) {
  102. vfree(*buf);
  103. *buf = NULL;
  104. }
  105. }
  106. out:
  107. allow_write_access(file);
  108. return ret == 0 ? copied : ret;
  109. }
  110. EXPORT_SYMBOL_GPL(kernel_read_file);
  111. int kernel_read_file_from_path(const char *path, loff_t offset, void **buf,
  112. size_t buf_size, size_t *file_size,
  113. enum kernel_read_file_id id)
  114. {
  115. struct file *file;
  116. int ret;
  117. if (!path || !*path)
  118. return -EINVAL;
  119. file = filp_open(path, O_RDONLY, 0);
  120. if (IS_ERR(file))
  121. return PTR_ERR(file);
  122. ret = kernel_read_file(file, offset, buf, buf_size, file_size, id);
  123. fput(file);
  124. return ret;
  125. }
  126. EXPORT_SYMBOL_GPL(kernel_read_file_from_path);
  127. int kernel_read_file_from_path_initns(const char *path, loff_t offset,
  128. void **buf, size_t buf_size,
  129. size_t *file_size,
  130. enum kernel_read_file_id id)
  131. {
  132. struct file *file;
  133. struct path root;
  134. int ret;
  135. if (!path || !*path)
  136. return -EINVAL;
  137. task_lock(&init_task);
  138. get_fs_root(init_task.fs, &root);
  139. task_unlock(&init_task);
  140. file = file_open_root(root.dentry, root.mnt, path, O_RDONLY, 0);
  141. path_put(&root);
  142. if (IS_ERR(file))
  143. return PTR_ERR(file);
  144. ret = kernel_read_file(file, offset, buf, buf_size, file_size, id);
  145. fput(file);
  146. return ret;
  147. }
  148. EXPORT_SYMBOL_GPL(kernel_read_file_from_path_initns);
  149. int kernel_read_file_from_fd(int fd, loff_t offset, void **buf,
  150. size_t buf_size, size_t *file_size,
  151. enum kernel_read_file_id id)
  152. {
  153. struct fd f = fdget(fd);
  154. int ret = -EBADF;
  155. if (!f.file || !(f.file->f_mode & FMODE_READ))
  156. goto out;
  157. ret = kernel_read_file(f.file, offset, buf, buf_size, file_size, id);
  158. out:
  159. fdput(f);
  160. return ret;
  161. }
  162. EXPORT_SYMBOL_GPL(kernel_read_file_from_fd);