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