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