Debian Codegraph @

compress.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2020 Corellium LLC
4 */
5
6 #include <linux/slab.h>
7 #include <linux/zlib.h>
8 #include <linux/mutex.h>
9
10 #include "apfs.h"
11 #include "libzbitmap.h"
12 #include "lzfse/lzfse.h"
13 #include "lzfse/lzvn_decode_base.h"
14
15 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 12, 0)
16
17 #include <linux/vmalloc.h>
18
19 static inline void *kvmalloc(size_t size, gfp_t flags)
20 {
21 gfp_t kmalloc_flags = flags;
22 void *ret;
23
24 if ((flags & GFP_KERNEL) != GFP_KERNEL)
25 return kmalloc(size, flags);
26
27 if (size > PAGE_SIZE)
28 kmalloc_flags |= __GFP_NOWARN | __GFP_NORETRY;
29
30 ret = kmalloc(size, flags);
31 if (ret || size < PAGE_SIZE)
32 return ret;
33
34 return vmalloc(size);
35 }
36
37 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 12, 0) */
38
39 struct apfs_compress_file_data {
40 struct apfs_compress_hdr hdr;
41 u8 *buf;
42 ssize_t bufblk;
43 size_t bufsize;
44 struct mutex mtx;
45 struct super_block *sb;
46 struct apfs_compressed_data cdata;
47 };
48
49 static inline int apfs_compress_is_rsrc(u32 algo)
50 {
51 return (algo & 1) == 0;
52 }
53
54 static inline bool apfs_compress_is_supported(u32 algo)
55 {
56 switch (algo) {
57 case APFS_COMPRESS_ZLIB_RSRC:
58 case APFS_COMPRESS_ZLIB_ATTR:
59 case APFS_COMPRESS_LZVN_RSRC:
60 case APFS_COMPRESS_LZVN_ATTR:
61 case APFS_COMPRESS_PLAIN_RSRC:
62 case APFS_COMPRESS_PLAIN_ATTR:
63 case APFS_COMPRESS_LZFSE_RSRC:
64 case APFS_COMPRESS_LZFSE_ATTR:
65 case APFS_COMPRESS_LZBITMAP_RSRC:
66 case APFS_COMPRESS_LZBITMAP_ATTR:
67 return true;
68 default:
69 /* Once will usually be enough, don't flood the console */
70 pr_err_once("APFS: unsupported compression algorithm (%u)\n", algo);
71 return false;
72 }
73 }
74
75 static int apfs_compress_file_open(struct inode *inode, struct file *filp)
76 {
77 struct super_block *sb = inode->i_sb;
78 struct apfs_nxsb_info *nxi = APFS_NXI(sb);
79 struct apfs_compress_file_data *fd;
80 ssize_t res;
81 bool is_rsrc;
82
83 /*
84 * The official implementation seems to transparently decompress files
85 * when you write to them. Doing that atomically inside the kernel is
86 * probably a chore, so for now I'll just leave it to the user to make
87 * an uncompressed copy themselves and replace the original. I might
88 * fix this in the future, but only if people complain (TODO).
89 */
90 if (filp->f_mode & FMODE_WRITE) {
91 apfs_warn(sb, "writes to compressed files are not supported");
92 apfs_warn(sb, "you can work with a copy of the file instead");
93 return -EOPNOTSUPP;
94 }
95
96 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
97 return -EOVERFLOW;
98
99 fd = kzalloc(sizeof(*fd), GFP_KERNEL);
100 if (!fd)
101 return -ENOMEM;
102 mutex_init(&fd->mtx);
103 fd->sb = sb;
104
105 down_read(&nxi->nx_big_sem);
106
107 res = ____apfs_xattr_get(inode, APFS_XATTR_NAME_COMPRESSED, &fd->hdr, sizeof(fd->hdr), 0);
108 if (res != sizeof(fd->hdr)) {
109 apfs_err(sb, "decmpfs header read failed");
110 goto fail;
111 }
112
113 if (!apfs_compress_is_supported(le32_to_cpu(fd->hdr.algo))) {
114 res = -EOPNOTSUPP;
115 goto fail;
116 }
117
118 fd->buf = kvmalloc(APFS_COMPRESS_BLOCK, GFP_KERNEL);
119 if (!fd->buf) {
120 res = -ENOMEM;
121 goto fail;
122 }
123 fd->bufblk = -1;
124
125 is_rsrc = apfs_compress_is_rsrc(le32_to_cpu(fd->hdr.algo));
126 res = apfs_xattr_get_compressed_data(inode, is_rsrc ? APFS_XATTR_NAME_RSRC_FORK : APFS_XATTR_NAME_COMPRESSED, &fd->cdata);
127 if (res) {
128 apfs_err(sb, "failed to get compressed data");
129 goto fail;
130 }
131
132 up_read(&nxi->nx_big_sem);
133
134 filp->private_data = fd;
135 return 0;
136
137 fail:
138 apfs_release_compressed_data(&fd->cdata);
139 if (fd->buf)
140 kvfree(fd->buf);
141 up_read(&nxi->nx_big_sem);
142 kfree(fd);
143 if (res > 0)
144 res = -EINVAL;
145 return res;
146 }
147
148 static int apfs_compress_file_read_block(struct apfs_compress_file_data *fd, loff_t block)
149 {
150 struct super_block *sb = fd->sb;
151 struct apfs_compressed_data *comp_data = &fd->cdata;
152 u8 *cdata = NULL;
153 u8 *tmp = fd->buf;
154 u32 doffs = 0, coffs;
155 size_t csize, bsize;
156 int res = 0;
157
158 if (apfs_compress_is_rsrc(le32_to_cpu(fd->hdr.algo)) &&
159 le32_to_cpu(fd->hdr.algo) != APFS_COMPRESS_LZBITMAP_RSRC &&
160 le32_to_cpu(fd->hdr.algo) != APFS_COMPRESS_LZVN_RSRC &&
161 le32_to_cpu(fd->hdr.algo) != APFS_COMPRESS_LZFSE_RSRC) {
162 struct apfs_compress_rsrc_hdr hdr = {0};
163 struct apfs_compress_rsrc_data cd = {0};
164 struct apfs_compress_rsrc_block blk = {0};
165 u32 blk_off;
166
167 res = apfs_compressed_data_read(comp_data, &hdr, sizeof(hdr), 0 /* offset */);
168 if (res) {
169 apfs_err(sb, "failed to read resource header");
170 return res;
171 }
172
173 doffs = be32_to_cpu(hdr.data_offs);
174 res = apfs_compressed_data_read(comp_data, &cd, sizeof(cd), doffs);
175 if (res) {
176 apfs_err(sb, "failed to read resource data header");
177 return res;
178 }
179 if (block >= le32_to_cpu(cd.num))
180 return 0;
181
182 blk_off = doffs + sizeof(cd) + sizeof(blk) * block;
183 res = apfs_compressed_data_read(comp_data, &blk, sizeof(blk), blk_off);
184 if (res) {
185 apfs_err(sb, "failed to read resource block metadata");
186 return res;
187 }
188
189 bsize = le64_to_cpu(fd->hdr.size) - block * APFS_COMPRESS_BLOCK;
190 if (bsize > APFS_COMPRESS_BLOCK)
191 bsize = APFS_COMPRESS_BLOCK;
192
193 csize = le32_to_cpu(blk.size);
194 coffs = le32_to_cpu(blk.offs) + 4;
195 } else if (apfs_compress_is_rsrc(le32_to_cpu(fd->hdr.algo))) {
196 __le32 blks[2];
197 u32 blk_off;
198
199 blk_off = doffs + sizeof(__le32) * block;
200 res = apfs_compressed_data_read(comp_data, blks, sizeof(blks), blk_off);
201 if (res) {
202 apfs_err(sb, "failed to read resource block metadata");
203 return res;
204 }
205
206 bsize = le64_to_cpu(fd->hdr.size) - block * APFS_COMPRESS_BLOCK;
207 if (bsize > APFS_COMPRESS_BLOCK)
208 bsize = APFS_COMPRESS_BLOCK;
209
210 coffs = le32_to_cpu(blks[0]);
211 csize = le32_to_cpu(blks[1]) - coffs;
212 } else {
213 /*
214 * I think attr compression is only for single-block files, in
215 * fact none of these files ever seem to decompress to more than
216 * 2048 bytes.
217 */
218 bsize = le64_to_cpu(fd->hdr.size);
219 if (block != 0 || bsize > APFS_COMPRESS_BLOCK) {
220 apfs_err(sb, "file too big for inline compression");
221 return -EFSCORRUPTED;
222 }
223
224 /* The first few bytes are the decmpfs header */
225 coffs = sizeof(struct apfs_compress_hdr);
226 csize = comp_data->size - sizeof(struct apfs_compress_hdr);
227 }
228
229 cdata = kvmalloc(csize, GFP_KERNEL);
230 if (!cdata)
231 return -ENOMEM;
232 res = apfs_compressed_data_read(comp_data, cdata, csize, doffs + coffs);
233 if (res) {
234 apfs_err(sb, "failed to read compressed block");
235 goto fail;
236 }
237
238 switch (le32_to_cpu(fd->hdr.algo)) {
239 case APFS_COMPRESS_ZLIB_RSRC:
240 case APFS_COMPRESS_ZLIB_ATTR:
241 if (cdata[0] == 0x78 && csize >= 2) {
242 res = zlib_inflate_blob(tmp, bsize, cdata + 2, csize - 2);
243 if (res <= 0) {
244 apfs_err(sb, "zlib decompression failed");
245 goto fail;
246 }
247 bsize = res;
248 res = 0;
249 } else if ((cdata[0] & 0x0F) == 0x0F) {
250 memcpy(tmp, &cdata[1], csize - 1);
251 bsize = csize - 1;
252 } else {
253 apfs_err(sb, "zlib decompression failed");
254 res = -EINVAL;
255 goto fail;
256 }
257 break;
258 case APFS_COMPRESS_LZVN_RSRC:
259 case APFS_COMPRESS_LZVN_ATTR:
260 if (cdata[0] == 0x06) {
261 memcpy(tmp, &cdata[1], csize - 1);
262 bsize = csize - 1;
263 } else {
264 lzvn_decoder_state dstate = {0};
265
266 dstate.src = cdata;
267 dstate.src_end = dstate.src + csize;
268 dstate.dst = dstate.dst_begin = tmp;
269 dstate.dst_end = dstate.dst + bsize;
270 lzvn_decode(&dstate);
271 bsize = dstate.dst - tmp;
272 }
273 break;
274 case APFS_COMPRESS_LZBITMAP_RSRC:
275 case APFS_COMPRESS_LZBITMAP_ATTR:
276 if (cdata[0] == 0x5a) {
277 res = zbm_decompress(tmp, bsize, cdata, csize, &bsize);
278 if (res < 0) {
279 apfs_err(sb, "lzbitmap decompression failed");
280 goto fail;
281 }
282 res = 0;
283 } else if ((cdata[0] & 0x0F) == 0x0F) {
284 memcpy(tmp, &cdata[1], csize - 1);
285 bsize = csize - 1;
286 } else {
287 apfs_err(sb, "lzbitmap decompression failed");
288 res = -EINVAL;
289 goto fail;
290 }
291 break;
292 case APFS_COMPRESS_LZFSE_RSRC:
293 case APFS_COMPRESS_LZFSE_ATTR:
294 if (cdata[0] == 0x62 && csize >= 2) {
295 res = lzfse_decode_buffer(tmp, bsize, cdata, csize, NULL);
296 if (res == 0) {
297 apfs_err(sb, "lzfse decompression failed");
298 /* Could be ENOMEM too... */
299 res = -EINVAL;
300 goto fail;
301 }
302 bsize = res;
303 res = 0;
304 } else {
305 /* cdata[0] == 0xff, apparently */
306 memcpy(tmp, &cdata[1], csize - 1);
307 bsize = csize - 1;
308 }
309 break;
310 case APFS_COMPRESS_PLAIN_RSRC:
311 case APFS_COMPRESS_PLAIN_ATTR:
312 memcpy(tmp, &cdata[1], csize - 1);
313 bsize = csize - 1;
314 break;
315 default:
316 res = -EINVAL;
317 goto fail;
318 }
319 fd->bufblk = block;
320 fd->bufsize = bsize;
321 fail:
322 kvfree(cdata);
323 return res;
324 }
325
326 static int apfs_compress_file_release(struct inode *inode, struct file *filp)
327 {
328 struct apfs_compress_file_data *fd = filp->private_data;
329
330 apfs_release_compressed_data(&fd->cdata);
331 if (fd->buf)
332 kvfree(fd->buf);
333 kfree(fd);
334 return 0;
335 }
336
337 static ssize_t apfs_compress_file_read_from_block(struct apfs_compress_file_data *fd, char *buf, size_t size, loff_t off)
338 {
339 struct super_block *sb = fd->sb;
340 struct apfs_nxsb_info *nxi = APFS_NXI(sb);
341 struct apfs_compressed_data cdata = fd->cdata;
342 loff_t block;
343 size_t bsize;
344 ssize_t res;
345
346 /*
347 * Request reads of all blocks before actually working with any of them.
348 * The compressed data is typically small enough that this is effective.
349 * It would be much better to make an inode for the xattr dstream and
350 * work with readahead as usual, but I'm not confident I can get that
351 * right (TODO).
352 */
353 if (cdata.has_dstream && off == 0) {
354 down_read(&nxi->nx_big_sem);
355 apfs_nonsparse_dstream_preread(cdata.dstream);
356 up_read(&nxi->nx_big_sem);
357 }
358
359 if (off >= le64_to_cpu(fd->hdr.size))
360 return 0;
361 if (size > le64_to_cpu(fd->hdr.size) - off)
362 size = le64_to_cpu(fd->hdr.size) - off;
363
364 block = off / APFS_COMPRESS_BLOCK;
365 off -= block * APFS_COMPRESS_BLOCK;
366 if (block != fd->bufblk) {
367 down_read(&nxi->nx_big_sem);
368 res = apfs_compress_file_read_block(fd, block);
369 up_read(&nxi->nx_big_sem);
370 if (res) {
371 apfs_err(sb, "failed to read block into buffer");
372 return res;
373 }
374 }
375 bsize = fd->bufsize;
376
377 if (bsize < off)
378 return 0;
379 bsize -= off;
380 if (size > bsize)
381 size = bsize;
382 memcpy(buf, fd->buf + off, size);
383 return size;
384 }
385
386 static ssize_t apfs_compress_file_read_page(struct file *filp, char *buf, loff_t off)
387 {
388 struct apfs_compress_file_data *fd = filp->private_data;
389 loff_t step;
390 ssize_t block, res;
391 size_t size = PAGE_SIZE;
392
393 step = 0;
394 while (step < size) {
395 block = APFS_COMPRESS_BLOCK - ((off + step) & (APFS_COMPRESS_BLOCK - 1));
396 if (block > size - step)
397 block = size - step;
398 mutex_lock(&fd->mtx);
399 res = apfs_compress_file_read_from_block(fd, buf + step, block, off + step);
400 mutex_unlock(&fd->mtx);
401 if (res < block) {
402 if (res < 0 && !step)
403 return res;
404 step += res > 0 ? res : 0;
405 break;
406 }
407 step += block;
408 }
409 return step;
410 }
411
412 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0) || RHEL_VERSION_GE(9, 3)
413 static int apfs_compress_read_folio(struct file *filp, struct folio *folio)
414 {
415 struct page *page = &folio->page;
416 #else
417 static int apfs_compress_readpage(struct file *filp, struct page *page)
418 {
419 #endif
420 char *addr = NULL;
421 ssize_t ret;
422 loff_t off;
423
424 /* Mostly copied from ext4_read_inline_page() */
425 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 16, 0)
426 off = page_folio(page)->index << PAGE_SHIFT;
427 #else
428 off = page->index << PAGE_SHIFT;
429 #endif
430 addr = kmap(page);
431 ret = apfs_compress_file_read_page(filp, addr, off);
432 flush_dcache_page(page);
433 kunmap(page);
434 if (ret >= 0) {
435 zero_user_segment(page, ret, PAGE_SIZE);
436 SetPageUptodate(page);
437 ret = 0;
438 }
439
440 unlock_page(page);
441 return ret;
442 }
443
444 const struct address_space_operations apfs_compress_aops = {
445 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0) || RHEL_VERSION_GE(9, 3)
446 .read_folio = apfs_compress_read_folio,
447 #else
448 .readpage = apfs_compress_readpage,
449 #endif
450 };
451
452 /* TODO: these operations are all happening without proper locks */
453 const struct file_operations apfs_compress_file_operations = {
454 .open = apfs_compress_file_open,
455 .llseek = generic_file_llseek,
456 .read_iter = generic_file_read_iter,
457 .release = apfs_compress_file_release,
458 .mmap = apfs_file_mmap,
459 };
460
461 int apfs_compress_get_size(struct inode *inode, loff_t *size)
462 {
463 struct apfs_compress_hdr hdr;
464 int res = ____apfs_xattr_get(inode, APFS_XATTR_NAME_COMPRESSED, &hdr, sizeof(hdr), 0);
465
466 if (res < 0)
467 return res;
468 if (res != sizeof(hdr)) {
469 apfs_err(inode->i_sb, "decmpfs header read failed");
470 return 1;
471 }
472
473 if (!apfs_compress_is_supported(le32_to_cpu(hdr.algo)))
474 return 1;
475
476 *size = le64_to_cpu(hdr.size);
477 return 0;
478 }