1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Ioctl to read verity metadata
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright 2021 Google LLC
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include "fsverity_private.h"
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/backing-dev.h>
11*4882a593Smuzhiyun #include <linux/highmem.h>
12*4882a593Smuzhiyun #include <linux/sched/signal.h>
13*4882a593Smuzhiyun #include <linux/uaccess.h>
14*4882a593Smuzhiyun
fsverity_read_merkle_tree(struct inode * inode,const struct fsverity_info * vi,void __user * buf,u64 offset,int length)15*4882a593Smuzhiyun static int fsverity_read_merkle_tree(struct inode *inode,
16*4882a593Smuzhiyun const struct fsverity_info *vi,
17*4882a593Smuzhiyun void __user *buf, u64 offset, int length)
18*4882a593Smuzhiyun {
19*4882a593Smuzhiyun const struct fsverity_operations *vops = inode->i_sb->s_vop;
20*4882a593Smuzhiyun u64 end_offset;
21*4882a593Smuzhiyun unsigned int offs_in_page;
22*4882a593Smuzhiyun pgoff_t index, last_index;
23*4882a593Smuzhiyun int retval = 0;
24*4882a593Smuzhiyun int err = 0;
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun end_offset = min(offset + length, vi->tree_params.tree_size);
27*4882a593Smuzhiyun if (offset >= end_offset)
28*4882a593Smuzhiyun return 0;
29*4882a593Smuzhiyun offs_in_page = offset_in_page(offset);
30*4882a593Smuzhiyun last_index = (end_offset - 1) >> PAGE_SHIFT;
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun /*
33*4882a593Smuzhiyun * Iterate through each Merkle tree page in the requested range and copy
34*4882a593Smuzhiyun * the requested portion to userspace. Note that the Merkle tree block
35*4882a593Smuzhiyun * size isn't important here, as we are returning a byte stream; i.e.,
36*4882a593Smuzhiyun * we can just work with pages even if the tree block size != PAGE_SIZE.
37*4882a593Smuzhiyun */
38*4882a593Smuzhiyun for (index = offset >> PAGE_SHIFT; index <= last_index; index++) {
39*4882a593Smuzhiyun unsigned long num_ra_pages =
40*4882a593Smuzhiyun min_t(unsigned long, last_index - index + 1,
41*4882a593Smuzhiyun inode->i_sb->s_bdi->io_pages);
42*4882a593Smuzhiyun unsigned int bytes_to_copy = min_t(u64, end_offset - offset,
43*4882a593Smuzhiyun PAGE_SIZE - offs_in_page);
44*4882a593Smuzhiyun struct page *page;
45*4882a593Smuzhiyun const void *virt;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun page = vops->read_merkle_tree_page(inode, index, num_ra_pages);
48*4882a593Smuzhiyun if (IS_ERR(page)) {
49*4882a593Smuzhiyun err = PTR_ERR(page);
50*4882a593Smuzhiyun fsverity_err(inode,
51*4882a593Smuzhiyun "Error %d reading Merkle tree page %lu",
52*4882a593Smuzhiyun err, index);
53*4882a593Smuzhiyun break;
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun virt = kmap(page);
57*4882a593Smuzhiyun if (copy_to_user(buf, virt + offs_in_page, bytes_to_copy)) {
58*4882a593Smuzhiyun kunmap(page);
59*4882a593Smuzhiyun put_page(page);
60*4882a593Smuzhiyun err = -EFAULT;
61*4882a593Smuzhiyun break;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun kunmap(page);
64*4882a593Smuzhiyun put_page(page);
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun retval += bytes_to_copy;
67*4882a593Smuzhiyun buf += bytes_to_copy;
68*4882a593Smuzhiyun offset += bytes_to_copy;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun if (fatal_signal_pending(current)) {
71*4882a593Smuzhiyun err = -EINTR;
72*4882a593Smuzhiyun break;
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun cond_resched();
75*4882a593Smuzhiyun offs_in_page = 0;
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun return retval ? retval : err;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /* Copy the requested portion of the buffer to userspace. */
fsverity_read_buffer(void __user * dst,u64 offset,int length,const void * src,size_t src_length)81*4882a593Smuzhiyun static int fsverity_read_buffer(void __user *dst, u64 offset, int length,
82*4882a593Smuzhiyun const void *src, size_t src_length)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun if (offset >= src_length)
85*4882a593Smuzhiyun return 0;
86*4882a593Smuzhiyun src += offset;
87*4882a593Smuzhiyun src_length -= offset;
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun length = min_t(size_t, length, src_length);
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun if (copy_to_user(dst, src, length))
92*4882a593Smuzhiyun return -EFAULT;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun return length;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun
fsverity_read_descriptor(struct inode * inode,void __user * buf,u64 offset,int length)97*4882a593Smuzhiyun static int fsverity_read_descriptor(struct inode *inode,
98*4882a593Smuzhiyun void __user *buf, u64 offset, int length)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun struct fsverity_descriptor *desc;
101*4882a593Smuzhiyun size_t desc_size;
102*4882a593Smuzhiyun int res;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun res = fsverity_get_descriptor(inode, &desc, &desc_size);
105*4882a593Smuzhiyun if (res)
106*4882a593Smuzhiyun return res;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /* don't include the signature */
109*4882a593Smuzhiyun desc_size = offsetof(struct fsverity_descriptor, signature);
110*4882a593Smuzhiyun desc->sig_size = 0;
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun res = fsverity_read_buffer(buf, offset, length, desc, desc_size);
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun kfree(desc);
115*4882a593Smuzhiyun return res;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
fsverity_read_signature(struct inode * inode,void __user * buf,u64 offset,int length)118*4882a593Smuzhiyun static int fsverity_read_signature(struct inode *inode,
119*4882a593Smuzhiyun void __user *buf, u64 offset, int length)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun struct fsverity_descriptor *desc;
122*4882a593Smuzhiyun size_t desc_size;
123*4882a593Smuzhiyun int res;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun res = fsverity_get_descriptor(inode, &desc, &desc_size);
126*4882a593Smuzhiyun if (res)
127*4882a593Smuzhiyun return res;
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun if (desc->sig_size == 0) {
130*4882a593Smuzhiyun res = -ENODATA;
131*4882a593Smuzhiyun goto out;
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun /*
135*4882a593Smuzhiyun * Include only the signature. Note that fsverity_get_descriptor()
136*4882a593Smuzhiyun * already verified that sig_size is in-bounds.
137*4882a593Smuzhiyun */
138*4882a593Smuzhiyun res = fsverity_read_buffer(buf, offset, length, desc->signature,
139*4882a593Smuzhiyun le32_to_cpu(desc->sig_size));
140*4882a593Smuzhiyun out:
141*4882a593Smuzhiyun kfree(desc);
142*4882a593Smuzhiyun return res;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun /**
146*4882a593Smuzhiyun * fsverity_ioctl_read_metadata() - read verity metadata from a file
147*4882a593Smuzhiyun * @filp: file to read the metadata from
148*4882a593Smuzhiyun * @uarg: user pointer to fsverity_read_metadata_arg
149*4882a593Smuzhiyun *
150*4882a593Smuzhiyun * Return: length read on success, 0 on EOF, -errno on failure
151*4882a593Smuzhiyun */
fsverity_ioctl_read_metadata(struct file * filp,const void __user * uarg)152*4882a593Smuzhiyun int fsverity_ioctl_read_metadata(struct file *filp, const void __user *uarg)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun struct inode *inode = file_inode(filp);
155*4882a593Smuzhiyun const struct fsverity_info *vi;
156*4882a593Smuzhiyun struct fsverity_read_metadata_arg arg;
157*4882a593Smuzhiyun int length;
158*4882a593Smuzhiyun void __user *buf;
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun vi = fsverity_get_info(inode);
161*4882a593Smuzhiyun if (!vi)
162*4882a593Smuzhiyun return -ENODATA; /* not a verity file */
163*4882a593Smuzhiyun /*
164*4882a593Smuzhiyun * Note that we don't have to explicitly check that the file is open for
165*4882a593Smuzhiyun * reading, since verity files can only be opened for reading.
166*4882a593Smuzhiyun */
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun if (copy_from_user(&arg, uarg, sizeof(arg)))
169*4882a593Smuzhiyun return -EFAULT;
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun if (arg.__reserved)
172*4882a593Smuzhiyun return -EINVAL;
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun /* offset + length must not overflow. */
175*4882a593Smuzhiyun if (arg.offset + arg.length < arg.offset)
176*4882a593Smuzhiyun return -EINVAL;
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun /* Ensure that the return value will fit in INT_MAX. */
179*4882a593Smuzhiyun length = min_t(u64, arg.length, INT_MAX);
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun buf = u64_to_user_ptr(arg.buf_ptr);
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun switch (arg.metadata_type) {
184*4882a593Smuzhiyun case FS_VERITY_METADATA_TYPE_MERKLE_TREE:
185*4882a593Smuzhiyun return fsverity_read_merkle_tree(inode, vi, buf, arg.offset,
186*4882a593Smuzhiyun length);
187*4882a593Smuzhiyun case FS_VERITY_METADATA_TYPE_DESCRIPTOR:
188*4882a593Smuzhiyun return fsverity_read_descriptor(inode, buf, arg.offset, length);
189*4882a593Smuzhiyun case FS_VERITY_METADATA_TYPE_SIGNATURE:
190*4882a593Smuzhiyun return fsverity_read_signature(inode, buf, arg.offset, length);
191*4882a593Smuzhiyun default:
192*4882a593Smuzhiyun return -EINVAL;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fsverity_ioctl_read_metadata);
196