1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/fs_struct.h>
4 #include <linux/kernel_read_file.h>
5 #include <linux/security.h>
6 #include <linux/vmalloc.h>
9 * kernel_read_file() - read file contents into a kernel buffer
11 * @file file to read from
12 * @offset where to start reading from (see below).
13 * @buf pointer to a "void *" buffer for reading into (if
14 * *@buf is NULL, a buffer will be allocated, and
15 * @buf_size will be ignored)
16 * @buf_size size of buf, if already allocated. If @buf not
17 * allocated, this is the largest size to allocate.
18 * @file_size if non-NULL, the full size of @file will be
20 * @id the kernel_read_file_id identifying the type of
21 * file contents being read (for LSMs to examine)
23 * @offset must be 0 unless both @buf and @file_size are non-NULL
24 * (i.e. the caller must be expecting to read partial file contents
25 * via an already-allocated @buf, in at most @buf_size chunks, and
26 * will be able to determine when the entire file was read by
27 * checking @file_size). This isn't a recommended way to read a
28 * file, though, since it is possible that the contents might
29 * change between calls to kernel_read_file().
31 * Returns number of bytes read (no single read will be bigger
32 * than SSIZE_MAX), or negative on error.
35 ssize_t kernel_read_file(struct file *file, loff_t offset, void **buf,
36 size_t buf_size, size_t *file_size,
37 enum kernel_read_file_id id)
41 void *allocated = NULL;
45 if (offset != 0 && (!*buf || !file_size))
48 if (!S_ISREG(file_inode(file)->i_mode))
51 ret = deny_write_access(file);
55 i_size = i_size_read(file_inode(file));
60 /* The file is too big for sane activities. */
61 if (i_size > SSIZE_MAX) {
65 /* The entire file cannot be read in one buffer. */
66 if (!file_size && offset == 0 && i_size > buf_size) {
71 whole_file = (offset == 0 && i_size <= buf_size);
72 ret = security_kernel_read_file(file, id, whole_file);
80 *buf = allocated = vmalloc(i_size);
88 while (copied < buf_size) {
90 size_t wanted = min_t(size_t, buf_size - copied,
93 bytes = kernel_read(file, *buf + copied, wanted, &pos);
110 ret = security_kernel_post_read_file(file, *buf, i_size, id);
122 allow_write_access(file);
123 return ret == 0 ? copied : ret;
125 EXPORT_SYMBOL_GPL(kernel_read_file);
127 ssize_t kernel_read_file_from_path(const char *path, loff_t offset, void **buf,
128 size_t buf_size, size_t *file_size,
129 enum kernel_read_file_id id)
137 file = filp_open(path, O_RDONLY, 0);
139 return PTR_ERR(file);
141 ret = kernel_read_file(file, offset, buf, buf_size, file_size, id);
145 EXPORT_SYMBOL_GPL(kernel_read_file_from_path);
147 ssize_t kernel_read_file_from_path_initns(const char *path, loff_t offset,
148 void **buf, size_t buf_size,
150 enum kernel_read_file_id id)
159 task_lock(&init_task);
160 get_fs_root(init_task.fs, &root);
161 task_unlock(&init_task);
163 file = file_open_root(&root, path, O_RDONLY, 0);
166 return PTR_ERR(file);
168 ret = kernel_read_file(file, offset, buf, buf_size, file_size, id);
172 EXPORT_SYMBOL_GPL(kernel_read_file_from_path_initns);
174 ssize_t kernel_read_file_from_fd(int fd, loff_t offset, void **buf,
175 size_t buf_size, size_t *file_size,
176 enum kernel_read_file_id id)
178 struct fd f = fdget(fd);
179 ssize_t ret = -EBADF;
181 if (!f.file || !(f.file->f_mode & FMODE_READ))
184 ret = kernel_read_file(f.file, offset, buf, buf_size, file_size, id);
189 EXPORT_SYMBOL_GPL(kernel_read_file_from_fd);