blob: 4a18c6aa26551beb5a1e33e686a5bece31d1d20d [file] [log] [blame]
Victor Hsiehdde17902021-02-26 12:35:31 -08001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use libc::EIO;
18use std::io;
19
20use super::common::{build_fsverity_digest, merkle_tree_height, FsverityError};
21use super::sys::{FS_VERITY_HASH_ALG_SHA256, FS_VERITY_MAGIC};
22use crate::auth::Authenticator;
23use crate::common::{divide_roundup, CHUNK_SIZE};
24use crate::crypto::{CryptoError, Sha256Hasher};
Victor Hsiehd0bb5d32021-03-19 12:48:03 -070025use crate::file::{ChunkBuffer, ReadByChunk};
Victor Hsiehdde17902021-02-26 12:35:31 -080026
27const ZEROS: [u8; CHUNK_SIZE as usize] = [0u8; CHUNK_SIZE as usize];
28
29// The size of `struct fsverity_formatted_digest` in Linux with SHA-256.
30const SIZE_OF_FSVERITY_FORMATTED_DIGEST_SHA256: usize = 12 + Sha256Hasher::HASH_SIZE;
31
32type HashBuffer = [u8; Sha256Hasher::HASH_SIZE];
33
34fn hash_with_padding(chunk: &[u8], pad_to: usize) -> Result<HashBuffer, CryptoError> {
35 let padding_size = pad_to - chunk.len();
Chris Wailes68c39f82021-07-27 16:03:44 -070036 Sha256Hasher::new()?.update(chunk)?.update(&ZEROS[..padding_size])?.finalize()
Victor Hsiehdde17902021-02-26 12:35:31 -080037}
38
Victor Hsiehd0bb5d32021-03-19 12:48:03 -070039fn verity_check<T: ReadByChunk>(
Victor Hsiehdde17902021-02-26 12:35:31 -080040 chunk: &[u8],
41 chunk_index: u64,
42 file_size: u64,
43 merkle_tree: &T,
44) -> Result<HashBuffer, FsverityError> {
45 // The caller should not be able to produce a chunk at the first place if `file_size` is 0. The
Victor Hsiehd0bb5d32021-03-19 12:48:03 -070046 // current implementation expects to crash when a `ReadByChunk` implementation reads
Victor Hsiehdde17902021-02-26 12:35:31 -080047 // beyond the file size, including empty file.
48 assert_ne!(file_size, 0);
49
Chris Wailes68c39f82021-07-27 16:03:44 -070050 let chunk_hash = hash_with_padding(chunk, CHUNK_SIZE as usize)?;
Victor Hsiehdde17902021-02-26 12:35:31 -080051
52 fsverity_walk(chunk_index, file_size, merkle_tree)?.try_fold(
53 chunk_hash,
54 |actual_hash, result| {
55 let (merkle_chunk, hash_offset_in_chunk) = result?;
56 let expected_hash =
57 &merkle_chunk[hash_offset_in_chunk..hash_offset_in_chunk + Sha256Hasher::HASH_SIZE];
58 if actual_hash != expected_hash {
59 return Err(FsverityError::CannotVerify);
60 }
61 Ok(hash_with_padding(&merkle_chunk, CHUNK_SIZE as usize)?)
62 },
63 )
64}
65
66/// Given a chunk index and the size of the file, returns an iterator that walks the Merkle tree
67/// from the leaf to the root. The iterator carries the slice of the chunk/node as well as the
68/// offset of the child node's hash. It is up to the iterator user to use the node and hash,
69/// e.g. for the actual verification.
70#[allow(clippy::needless_collect)]
Victor Hsiehd0bb5d32021-03-19 12:48:03 -070071fn fsverity_walk<T: ReadByChunk>(
Victor Hsiehdde17902021-02-26 12:35:31 -080072 chunk_index: u64,
73 file_size: u64,
74 merkle_tree: &T,
75) -> Result<impl Iterator<Item = Result<([u8; 4096], usize), FsverityError>> + '_, FsverityError> {
76 let hashes_per_node = CHUNK_SIZE / Sha256Hasher::HASH_SIZE as u64;
77 debug_assert_eq!(hashes_per_node, 128u64);
78 let max_level = merkle_tree_height(file_size).expect("file should not be empty") as u32;
79 let root_to_leaf_steps = (0..=max_level)
80 .rev()
81 .map(|x| {
82 let leaves_per_hash = hashes_per_node.pow(x);
83 let leaves_size_per_hash = CHUNK_SIZE * leaves_per_hash;
84 let leaves_size_per_node = leaves_size_per_hash * hashes_per_node;
85 let nodes_at_level = divide_roundup(file_size, leaves_size_per_node);
86 let level_size = nodes_at_level * CHUNK_SIZE;
87 let offset_in_level = (chunk_index / leaves_per_hash) * Sha256Hasher::HASH_SIZE as u64;
88 (level_size, offset_in_level)
89 })
90 .scan(0, |level_offset, (level_size, offset_in_level)| {
91 let this_level_offset = *level_offset;
92 *level_offset += level_size;
93 let global_hash_offset = this_level_offset + offset_in_level;
94 Some(global_hash_offset)
95 })
96 .map(|global_hash_offset| {
97 let chunk_index = global_hash_offset / CHUNK_SIZE;
98 let hash_offset_in_chunk = (global_hash_offset % CHUNK_SIZE) as usize;
99 (chunk_index, hash_offset_in_chunk)
100 })
101 .collect::<Vec<_>>(); // Needs to collect first to be able to reverse below.
102
103 Ok(root_to_leaf_steps.into_iter().rev().map(move |(chunk_index, hash_offset_in_chunk)| {
104 let mut merkle_chunk = [0u8; 4096];
105 // read_chunk is supposed to return a full chunk, or an incomplete one at the end of the
106 // file. In the incomplete case, the hash is calculated with 0-padding to the chunk size.
107 // Therefore, we don't need to check the returned size here.
108 let _ = merkle_tree.read_chunk(chunk_index, &mut merkle_chunk)?;
109 Ok((merkle_chunk, hash_offset_in_chunk))
110 }))
111}
112
113fn build_fsverity_formatted_digest(
114 root_hash: &HashBuffer,
115 file_size: u64,
116) -> Result<[u8; SIZE_OF_FSVERITY_FORMATTED_DIGEST_SHA256], CryptoError> {
117 let digest = build_fsverity_digest(root_hash, file_size)?;
118 // Little-endian byte representation of fsverity_formatted_digest from linux/fsverity.h
119 // Not FFI-ed as it seems easier to deal with the raw bytes manually.
120 let mut formatted_digest = [0u8; SIZE_OF_FSVERITY_FORMATTED_DIGEST_SHA256];
121 formatted_digest[0..8].copy_from_slice(FS_VERITY_MAGIC);
122 formatted_digest[8..10].copy_from_slice(&(FS_VERITY_HASH_ALG_SHA256 as u16).to_le_bytes());
123 formatted_digest[10..12].copy_from_slice(&(Sha256Hasher::HASH_SIZE as u16).to_le_bytes());
124 formatted_digest[12..].copy_from_slice(&digest);
125 Ok(formatted_digest)
126}
127
Victor Hsiehd0bb5d32021-03-19 12:48:03 -0700128pub struct VerifiedFileReader<F: ReadByChunk, M: ReadByChunk> {
Victor Hsiehdde17902021-02-26 12:35:31 -0800129 chunked_file: F,
130 file_size: u64,
131 merkle_tree: M,
132 root_hash: HashBuffer,
133}
134
Victor Hsiehd0bb5d32021-03-19 12:48:03 -0700135impl<F: ReadByChunk, M: ReadByChunk> VerifiedFileReader<F, M> {
Victor Hsiehdde17902021-02-26 12:35:31 -0800136 pub fn new<A: Authenticator>(
137 authenticator: &A,
138 chunked_file: F,
139 file_size: u64,
140 sig: Vec<u8>,
141 merkle_tree: M,
Victor Hsieh09e26262021-03-03 16:00:55 -0800142 ) -> Result<VerifiedFileReader<F, M>, FsverityError> {
Victor Hsiehdde17902021-02-26 12:35:31 -0800143 let mut buf = [0u8; CHUNK_SIZE as usize];
144 let size = merkle_tree.read_chunk(0, &mut buf)?;
145 if buf.len() != size {
146 return Err(FsverityError::InsufficientData(size));
147 }
148 let root_hash = Sha256Hasher::new()?.update(&buf[..])?.finalize()?;
149 let formatted_digest = build_fsverity_formatted_digest(&root_hash, file_size)?;
150 let valid = authenticator.verify(&sig, &formatted_digest)?;
151 if valid {
Victor Hsieh09e26262021-03-03 16:00:55 -0800152 Ok(VerifiedFileReader { chunked_file, file_size, merkle_tree, root_hash })
Victor Hsiehdde17902021-02-26 12:35:31 -0800153 } else {
154 Err(FsverityError::BadSignature)
155 }
156 }
157}
158
Victor Hsiehd0bb5d32021-03-19 12:48:03 -0700159impl<F: ReadByChunk, M: ReadByChunk> ReadByChunk for VerifiedFileReader<F, M> {
160 fn read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize> {
Victor Hsiehdde17902021-02-26 12:35:31 -0800161 let size = self.chunked_file.read_chunk(chunk_index, buf)?;
162 let root_hash = verity_check(&buf[..size], chunk_index, self.file_size, &self.merkle_tree)
163 .map_err(|_| io::Error::from_raw_os_error(EIO))?;
164 if root_hash != self.root_hash {
165 Err(io::Error::from_raw_os_error(EIO))
166 } else {
167 Ok(size)
168 }
169 }
170}
171
172#[cfg(test)]
173mod tests {
174 use super::*;
175 use crate::auth::FakeAuthenticator;
Victor Hsieh88e50172021-10-15 13:27:13 -0700176 use crate::file::ReadByChunk;
Victor Hsiehdde17902021-02-26 12:35:31 -0800177 use anyhow::Result;
Victor Hsieh88e50172021-10-15 13:27:13 -0700178 use std::cmp::min;
Victor Hsieh85b4f732021-03-09 16:02:14 -0800179 use std::fs::{self, File};
Victor Hsiehdde17902021-02-26 12:35:31 -0800180 use std::io::Read;
Victor Hsieh88e50172021-10-15 13:27:13 -0700181 use std::os::unix::fs::FileExt;
182
183 struct LocalFileReader {
184 file: File,
185 size: u64,
186 }
187
188 impl LocalFileReader {
189 fn new(file: File) -> io::Result<LocalFileReader> {
190 let size = file.metadata()?.len();
191 Ok(LocalFileReader { file, size })
192 }
193
194 fn len(&self) -> u64 {
195 self.size
196 }
197 }
198
199 impl ReadByChunk for LocalFileReader {
200 fn read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize> {
201 let start = chunk_index * CHUNK_SIZE;
202 if start >= self.size {
203 return Ok(0);
204 }
205 let end = min(self.size, start + CHUNK_SIZE);
206 let read_size = (end - start) as usize;
207 debug_assert!(read_size <= buf.len());
208 self.file.read_exact_at(&mut buf[..read_size], start)?;
209 Ok(read_size)
210 }
211 }
Victor Hsiehdde17902021-02-26 12:35:31 -0800212
Victor Hsieh09e26262021-03-03 16:00:55 -0800213 type LocalVerifiedFileReader = VerifiedFileReader<LocalFileReader, LocalFileReader>;
Victor Hsiehdde17902021-02-26 12:35:31 -0800214
215 fn total_chunk_number(file_size: u64) -> u64 {
216 (file_size + 4095) / 4096
217 }
218
219 // Returns a reader with fs-verity verification and the file size.
220 fn new_reader_with_fsverity(
221 content_path: &str,
222 merkle_tree_path: &str,
223 signature_path: &str,
Victor Hsieh09e26262021-03-03 16:00:55 -0800224 ) -> Result<(LocalVerifiedFileReader, u64)> {
225 let file_reader = LocalFileReader::new(File::open(content_path)?)?;
Victor Hsiehdde17902021-02-26 12:35:31 -0800226 let file_size = file_reader.len();
Victor Hsieh09e26262021-03-03 16:00:55 -0800227 let merkle_tree = LocalFileReader::new(File::open(merkle_tree_path)?)?;
Victor Hsiehdde17902021-02-26 12:35:31 -0800228 let mut sig = Vec::new();
229 let _ = File::open(signature_path)?.read_to_end(&mut sig)?;
230 let authenticator = FakeAuthenticator::always_succeed();
231 Ok((
Victor Hsieh09e26262021-03-03 16:00:55 -0800232 VerifiedFileReader::new(&authenticator, file_reader, file_size, sig, merkle_tree)?,
Victor Hsiehdde17902021-02-26 12:35:31 -0800233 file_size,
234 ))
235 }
236
237 #[test]
238 fn fsverity_verify_full_read_4k() -> Result<()> {
239 let (file_reader, file_size) = new_reader_with_fsverity(
240 "testdata/input.4k",
241 "testdata/input.4k.merkle_dump",
242 "testdata/input.4k.fsv_sig",
243 )?;
244
245 for i in 0..total_chunk_number(file_size) {
246 let mut buf = [0u8; 4096];
Victor Hsiehd0bb5d32021-03-19 12:48:03 -0700247 assert!(file_reader.read_chunk(i, &mut buf).is_ok());
Victor Hsiehdde17902021-02-26 12:35:31 -0800248 }
249 Ok(())
250 }
251
252 #[test]
253 fn fsverity_verify_full_read_4k1() -> Result<()> {
254 let (file_reader, file_size) = new_reader_with_fsverity(
255 "testdata/input.4k1",
256 "testdata/input.4k1.merkle_dump",
257 "testdata/input.4k1.fsv_sig",
258 )?;
259
260 for i in 0..total_chunk_number(file_size) {
261 let mut buf = [0u8; 4096];
Victor Hsiehd0bb5d32021-03-19 12:48:03 -0700262 assert!(file_reader.read_chunk(i, &mut buf).is_ok());
Victor Hsiehdde17902021-02-26 12:35:31 -0800263 }
264 Ok(())
265 }
266
267 #[test]
268 fn fsverity_verify_full_read_4m() -> Result<()> {
269 let (file_reader, file_size) = new_reader_with_fsverity(
270 "testdata/input.4m",
271 "testdata/input.4m.merkle_dump",
272 "testdata/input.4m.fsv_sig",
273 )?;
274
275 for i in 0..total_chunk_number(file_size) {
276 let mut buf = [0u8; 4096];
Victor Hsiehd0bb5d32021-03-19 12:48:03 -0700277 assert!(file_reader.read_chunk(i, &mut buf).is_ok());
Victor Hsiehdde17902021-02-26 12:35:31 -0800278 }
279 Ok(())
280 }
281
282 #[test]
283 fn fsverity_verify_bad_merkle_tree() -> Result<()> {
284 let (file_reader, _) = new_reader_with_fsverity(
285 "testdata/input.4m",
286 "testdata/input.4m.merkle_dump.bad", // First leaf node is corrupted.
287 "testdata/input.4m.fsv_sig",
288 )?;
289
290 // A lowest broken node (a 4K chunk that contains 128 sha256 hashes) will fail the read
291 // failure of the underlying chunks, but not before or after.
292 let mut buf = [0u8; 4096];
293 let num_hashes = 4096 / 32;
294 let last_index = num_hashes;
295 for i in 0..last_index {
Victor Hsiehd0bb5d32021-03-19 12:48:03 -0700296 assert!(file_reader.read_chunk(i, &mut buf).is_err());
Victor Hsiehdde17902021-02-26 12:35:31 -0800297 }
Victor Hsiehd0bb5d32021-03-19 12:48:03 -0700298 assert!(file_reader.read_chunk(last_index, &mut buf).is_ok());
Victor Hsiehdde17902021-02-26 12:35:31 -0800299 Ok(())
300 }
301
302 #[test]
303 fn invalid_signature() -> Result<()> {
304 let authenticator = FakeAuthenticator::always_fail();
Victor Hsieh09e26262021-03-03 16:00:55 -0800305 let file_reader = LocalFileReader::new(File::open("testdata/input.4m")?)?;
Victor Hsiehdde17902021-02-26 12:35:31 -0800306 let file_size = file_reader.len();
Victor Hsieh09e26262021-03-03 16:00:55 -0800307 let merkle_tree = LocalFileReader::new(File::open("testdata/input.4m.merkle_dump")?)?;
Victor Hsieh85b4f732021-03-09 16:02:14 -0800308 let sig = fs::read("testdata/input.4m.fsv_sig")?;
Victor Hsieh09e26262021-03-03 16:00:55 -0800309 assert!(VerifiedFileReader::new(&authenticator, file_reader, file_size, sig, merkle_tree)
310 .is_err());
Victor Hsiehdde17902021-02-26 12:35:31 -0800311 Ok(())
312 }
313}