Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) 2020 Bootlin |
| 4 | * |
| 5 | * Author: Joao Marcos Costa <joaomarcos.costa@bootlin.com> |
| 6 | * |
| 7 | * sqfs.c: SquashFS filesystem implementation |
| 8 | */ |
| 9 | |
| 10 | #include <asm/unaligned.h> |
| 11 | #include <errno.h> |
| 12 | #include <fs.h> |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/byteorder/little_endian.h> |
| 15 | #include <linux/byteorder/generic.h> |
| 16 | #include <memalign.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <squashfs.h> |
| 20 | #include <part.h> |
| 21 | |
| 22 | #include "sqfs_decompressor.h" |
| 23 | #include "sqfs_filesystem.h" |
| 24 | #include "sqfs_utils.h" |
| 25 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 26 | static struct squashfs_ctxt ctxt; |
| 27 | |
| 28 | static int sqfs_disk_read(__u32 block, __u32 nr_blocks, void *buf) |
| 29 | { |
| 30 | ulong ret; |
| 31 | |
| 32 | if (!ctxt.cur_dev) |
| 33 | return -1; |
| 34 | |
| 35 | ret = blk_dread(ctxt.cur_dev, ctxt.cur_part_info.start + block, |
| 36 | nr_blocks, buf); |
| 37 | |
| 38 | if (ret != nr_blocks) |
| 39 | return -1; |
| 40 | |
| 41 | return ret; |
| 42 | } |
| 43 | |
| 44 | static int sqfs_read_sblk(struct squashfs_super_block **sblk) |
| 45 | { |
| 46 | *sblk = malloc_cache_aligned(ctxt.cur_dev->blksz); |
| 47 | if (!*sblk) |
| 48 | return -ENOMEM; |
| 49 | |
| 50 | if (sqfs_disk_read(0, 1, *sblk) != 1) { |
| 51 | free(*sblk); |
| 52 | return -EINVAL; |
| 53 | } |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int sqfs_count_tokens(const char *filename) |
| 59 | { |
| 60 | int token_count = 1, l; |
| 61 | |
| 62 | for (l = 1; l < strlen(filename); l++) { |
| 63 | if (filename[l] == '/') |
| 64 | token_count++; |
| 65 | } |
| 66 | |
| 67 | /* Ignore trailing '/' in path */ |
| 68 | if (filename[strlen(filename) - 1] == '/') |
| 69 | token_count--; |
| 70 | |
| 71 | if (!token_count) |
| 72 | token_count = 1; |
| 73 | |
| 74 | return token_count; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Calculates how many blocks are needed for the buffer used in sqfs_disk_read. |
| 79 | * The memory section (e.g. inode table) start offset and its end (i.e. the next |
| 80 | * table start) must be specified. It also calculates the offset from which to |
| 81 | * start reading the buffer. |
| 82 | */ |
| 83 | static int sqfs_calc_n_blks(__le64 start, __le64 end, u64 *offset) |
| 84 | { |
| 85 | u64 start_, table_size; |
| 86 | |
| 87 | table_size = le64_to_cpu(end) - le64_to_cpu(start); |
| 88 | start_ = le64_to_cpu(start) / ctxt.cur_dev->blksz; |
| 89 | *offset = le64_to_cpu(start) - (start_ * ctxt.cur_dev->blksz); |
| 90 | |
| 91 | return DIV_ROUND_UP(table_size + *offset, ctxt.cur_dev->blksz); |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Retrieves fragment block entry and returns true if the fragment block is |
| 96 | * compressed |
| 97 | */ |
| 98 | static int sqfs_frag_lookup(u32 inode_fragment_index, |
| 99 | struct squashfs_fragment_block_entry *e) |
| 100 | { |
| 101 | u64 start, n_blks, src_len, table_offset, start_block; |
| 102 | unsigned char *metadata_buffer, *metadata, *table; |
| 103 | struct squashfs_fragment_block_entry *entries; |
| 104 | struct squashfs_super_block *sblk = ctxt.sblk; |
| 105 | unsigned long dest_len; |
| 106 | int block, offset, ret; |
Joao Marcos Costa | b87fd01 | 2020-08-18 17:17:22 +0200 | [diff] [blame] | 107 | u16 header; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 108 | |
Richard Genoud | 07726e1 | 2020-11-03 12:11:15 +0100 | [diff] [blame] | 109 | metadata_buffer = NULL; |
| 110 | entries = NULL; |
| 111 | table = NULL; |
| 112 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 113 | if (inode_fragment_index >= get_unaligned_le32(&sblk->fragments)) |
| 114 | return -EINVAL; |
| 115 | |
| 116 | start = get_unaligned_le64(&sblk->fragment_table_start) / |
| 117 | ctxt.cur_dev->blksz; |
| 118 | n_blks = sqfs_calc_n_blks(sblk->fragment_table_start, |
| 119 | sblk->export_table_start, |
| 120 | &table_offset); |
| 121 | |
| 122 | /* Allocate a proper sized buffer to store the fragment index table */ |
| 123 | table = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz); |
Richard Genoud | 07726e1 | 2020-11-03 12:11:15 +0100 | [diff] [blame] | 124 | if (!table) { |
| 125 | ret = -ENOMEM; |
| 126 | goto out; |
| 127 | } |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 128 | |
| 129 | if (sqfs_disk_read(start, n_blks, table) < 0) { |
Richard Genoud | 07726e1 | 2020-11-03 12:11:15 +0100 | [diff] [blame] | 130 | ret = -EINVAL; |
| 131 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | block = SQFS_FRAGMENT_INDEX(inode_fragment_index); |
| 135 | offset = SQFS_FRAGMENT_INDEX_OFFSET(inode_fragment_index); |
| 136 | |
| 137 | /* |
| 138 | * Get the start offset of the metadata block that contains the right |
| 139 | * fragment block entry |
| 140 | */ |
| 141 | start_block = get_unaligned_le64(table + table_offset + block * |
| 142 | sizeof(u64)); |
| 143 | |
| 144 | start = start_block / ctxt.cur_dev->blksz; |
| 145 | n_blks = sqfs_calc_n_blks(cpu_to_le64(start_block), |
| 146 | sblk->fragment_table_start, &table_offset); |
| 147 | |
| 148 | metadata_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz); |
| 149 | if (!metadata_buffer) { |
| 150 | ret = -ENOMEM; |
Richard Genoud | 07726e1 | 2020-11-03 12:11:15 +0100 | [diff] [blame] | 151 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | if (sqfs_disk_read(start, n_blks, metadata_buffer) < 0) { |
| 155 | ret = -EINVAL; |
Richard Genoud | 07726e1 | 2020-11-03 12:11:15 +0100 | [diff] [blame] | 156 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /* Every metadata block starts with a 16-bit header */ |
| 160 | header = get_unaligned_le16(metadata_buffer + table_offset); |
| 161 | metadata = metadata_buffer + table_offset + SQFS_HEADER_SIZE; |
| 162 | |
Joao Marcos Costa | f1dfe4a | 2020-09-11 12:21:06 +0200 | [diff] [blame] | 163 | if (!metadata || !header) { |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 164 | ret = -ENOMEM; |
Richard Genoud | 07726e1 | 2020-11-03 12:11:15 +0100 | [diff] [blame] | 165 | goto out; |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 168 | entries = malloc(SQFS_METADATA_BLOCK_SIZE); |
| 169 | if (!entries) { |
| 170 | ret = -ENOMEM; |
Richard Genoud | 07726e1 | 2020-11-03 12:11:15 +0100 | [diff] [blame] | 171 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | if (SQFS_COMPRESSED_METADATA(header)) { |
| 175 | src_len = SQFS_METADATA_SIZE(header); |
| 176 | dest_len = SQFS_METADATA_BLOCK_SIZE; |
Joao Marcos Costa | b87fd01 | 2020-08-18 17:17:22 +0200 | [diff] [blame] | 177 | ret = sqfs_decompress(&ctxt, entries, &dest_len, metadata, |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 178 | src_len); |
| 179 | if (ret) { |
| 180 | ret = -EINVAL; |
Richard Genoud | 07726e1 | 2020-11-03 12:11:15 +0100 | [diff] [blame] | 181 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 182 | } |
| 183 | } else { |
| 184 | memcpy(entries, metadata, SQFS_METADATA_SIZE(header)); |
| 185 | } |
| 186 | |
| 187 | *e = entries[offset]; |
| 188 | ret = SQFS_COMPRESSED_BLOCK(e->size); |
| 189 | |
Richard Genoud | 07726e1 | 2020-11-03 12:11:15 +0100 | [diff] [blame] | 190 | out: |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 191 | free(entries); |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 192 | free(metadata_buffer); |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 193 | free(table); |
| 194 | |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * The entry name is a flexible array member, and we don't know its size before |
| 200 | * actually reading the entry. So we need a first copy to retrieve this size so |
| 201 | * we can finally copy the whole struct. |
| 202 | */ |
| 203 | static int sqfs_read_entry(struct squashfs_directory_entry **dest, void *src) |
| 204 | { |
| 205 | struct squashfs_directory_entry *tmp; |
| 206 | u16 sz; |
| 207 | |
| 208 | tmp = src; |
| 209 | sz = get_unaligned_le16(src + sizeof(*tmp) - sizeof(u16)); |
| 210 | /* |
| 211 | * 'src' points to the begin of a directory entry, and 'sz' gets its |
| 212 | * 'name_size' member's value. name_size is actually the string |
| 213 | * length - 1, so adding 2 compensates this difference and adds space |
| 214 | * for the trailling null byte. |
| 215 | */ |
| 216 | *dest = malloc(sizeof(*tmp) + sz + 2); |
| 217 | if (!*dest) |
| 218 | return -ENOMEM; |
| 219 | |
| 220 | memcpy(*dest, src, sizeof(*tmp) + sz + 1); |
| 221 | (*dest)->name[sz + 1] = '\0'; |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static int sqfs_get_tokens_length(char **tokens, int count) |
| 227 | { |
| 228 | int length = 0, i; |
| 229 | |
| 230 | /* |
| 231 | * 1 is added to the result of strlen to consider the slash separator |
| 232 | * between the tokens. |
| 233 | */ |
| 234 | for (i = 0; i < count; i++) |
| 235 | length += strlen(tokens[i]) + 1; |
| 236 | |
| 237 | return length; |
| 238 | } |
| 239 | |
| 240 | /* Takes a token list and returns a single string with '/' as separator. */ |
| 241 | static char *sqfs_concat_tokens(char **token_list, int token_count) |
| 242 | { |
| 243 | char *result; |
| 244 | int i, length = 0, offset = 0; |
| 245 | |
| 246 | length = sqfs_get_tokens_length(token_list, token_count); |
| 247 | |
| 248 | result = malloc(length + 1); |
Richard Genoud | 489e7ae | 2020-11-03 12:11:08 +0100 | [diff] [blame] | 249 | if (!result) |
| 250 | return NULL; |
| 251 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 252 | result[length] = '\0'; |
| 253 | |
| 254 | for (i = 0; i < token_count; i++) { |
| 255 | strcpy(result + offset, token_list[i]); |
| 256 | offset += strlen(token_list[i]); |
| 257 | result[offset++] = '/'; |
| 258 | } |
| 259 | |
| 260 | return result; |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Differently from sqfs_concat_tokens, sqfs_join writes the result into a |
| 265 | * previously allocated string, and returns the number of bytes written. |
| 266 | */ |
| 267 | static int sqfs_join(char **strings, char *dest, int start, int end, |
| 268 | char separator) |
| 269 | { |
| 270 | int i, offset = 0; |
| 271 | |
| 272 | for (i = start; i < end; i++) { |
| 273 | strcpy(dest + offset, strings[i]); |
| 274 | offset += strlen(strings[i]); |
| 275 | if (i < end - 1) |
| 276 | dest[offset++] = separator; |
| 277 | } |
| 278 | |
| 279 | return offset; |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * Fills the given token list using its size (count) and a source string (str) |
| 284 | */ |
| 285 | static int sqfs_tokenize(char **tokens, int count, const char *str) |
| 286 | { |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 287 | int i, j, ret = 0; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 288 | char *aux, *strc; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 289 | |
| 290 | strc = strdup(str); |
| 291 | if (!strc) |
| 292 | return -ENOMEM; |
| 293 | |
| 294 | if (!strcmp(strc, "/")) { |
| 295 | tokens[0] = strdup(strc); |
| 296 | if (!tokens[0]) { |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 297 | ret = -ENOMEM; |
| 298 | goto free_strc; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 299 | } |
| 300 | } else { |
| 301 | for (j = 0; j < count; j++) { |
| 302 | aux = strtok(!j ? strc : NULL, "/"); |
| 303 | tokens[j] = strdup(aux); |
| 304 | if (!tokens[j]) { |
| 305 | for (i = 0; i < j; i++) |
| 306 | free(tokens[i]); |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 307 | ret = -ENOMEM; |
| 308 | goto free_strc; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 313 | free_strc: |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 314 | free(strc); |
| 315 | |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 316 | return ret; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /* |
| 320 | * Remove last 'updir + 1' tokens from the base path tokens list. This leaves us |
| 321 | * with a token list containing only the tokens needed to form the resolved |
| 322 | * path, and returns the decremented size of the token list. |
| 323 | */ |
| 324 | static int sqfs_clean_base_path(char **base, int count, int updir) |
| 325 | { |
| 326 | int i; |
| 327 | |
| 328 | for (i = count - updir - 1; i < count; i++) |
| 329 | free(base[i]); |
| 330 | |
| 331 | return count - updir - 1; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * Given the base ("current dir.") path and the relative one, generate the |
| 336 | * absolute path. |
| 337 | */ |
| 338 | static char *sqfs_get_abs_path(const char *base, const char *rel) |
| 339 | { |
| 340 | char **base_tokens, **rel_tokens, *resolved = NULL; |
| 341 | int ret, bc, rc, i, updir = 0, resolved_size = 0, offset = 0; |
| 342 | |
| 343 | /* Memory allocation for the token lists */ |
| 344 | bc = sqfs_count_tokens(base); |
| 345 | rc = sqfs_count_tokens(rel); |
| 346 | if (bc < 1 || rc < 1) |
| 347 | return NULL; |
| 348 | |
| 349 | base_tokens = malloc(bc * sizeof(char *)); |
| 350 | if (!base_tokens) |
| 351 | return NULL; |
| 352 | |
| 353 | rel_tokens = malloc(rc * sizeof(char *)); |
| 354 | if (!rel_tokens) |
| 355 | goto free_b_tokens; |
| 356 | |
| 357 | /* Fill token lists */ |
| 358 | ret = sqfs_tokenize(base_tokens, bc, base); |
| 359 | if (ret) |
| 360 | goto free_r_tokens; |
| 361 | |
Richard Genoud | 9cb3036 | 2020-11-03 12:11:16 +0100 | [diff] [blame^] | 362 | ret = sqfs_tokenize(rel_tokens, rc, rel); |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 363 | if (ret) |
| 364 | goto free_r_tokens; |
| 365 | |
| 366 | /* count '..' occurrences in target path */ |
| 367 | for (i = 0; i < rc; i++) { |
| 368 | if (!strcmp(rel_tokens[i], "..")) |
| 369 | updir++; |
| 370 | } |
| 371 | |
| 372 | /* Remove the last token and the '..' occurrences */ |
| 373 | bc = sqfs_clean_base_path(base_tokens, bc, updir); |
| 374 | if (bc < 0) |
| 375 | goto free_r_tokens; |
| 376 | |
| 377 | /* Calculate resolved path size */ |
| 378 | if (!bc) |
| 379 | resolved_size++; |
| 380 | |
| 381 | resolved_size += sqfs_get_tokens_length(base_tokens, bc) + |
| 382 | sqfs_get_tokens_length(rel_tokens, rc); |
| 383 | |
| 384 | resolved = malloc(resolved_size + 1); |
| 385 | if (!resolved) |
| 386 | goto free_r_tokens_loop; |
| 387 | |
| 388 | /* Set resolved path */ |
| 389 | memset(resolved, '\0', resolved_size + 1); |
| 390 | offset += sqfs_join(base_tokens, resolved + offset, 0, bc, '/'); |
| 391 | resolved[offset++] = '/'; |
| 392 | offset += sqfs_join(rel_tokens, resolved + offset, updir, rc, '/'); |
| 393 | |
| 394 | free_r_tokens_loop: |
| 395 | for (i = 0; i < rc; i++) |
| 396 | free(rel_tokens[i]); |
| 397 | for (i = 0; i < bc; i++) |
| 398 | free(base_tokens[i]); |
| 399 | free_r_tokens: |
| 400 | free(rel_tokens); |
| 401 | free_b_tokens: |
| 402 | free(base_tokens); |
| 403 | |
| 404 | return resolved; |
| 405 | } |
| 406 | |
| 407 | static char *sqfs_resolve_symlink(struct squashfs_symlink_inode *sym, |
| 408 | const char *base_path) |
| 409 | { |
| 410 | char *resolved, *target; |
| 411 | u32 sz; |
| 412 | |
| 413 | sz = get_unaligned_le32(&sym->symlink_size); |
| 414 | target = malloc(sz + 1); |
| 415 | if (!target) |
| 416 | return NULL; |
| 417 | |
| 418 | /* |
| 419 | * There is no trailling null byte in the symlink's target path, so a |
| 420 | * copy is made and a '\0' is added at its end. |
| 421 | */ |
| 422 | target[sz] = '\0'; |
| 423 | /* Get target name (relative path) */ |
| 424 | strncpy(target, sym->symlink, sz); |
| 425 | |
| 426 | /* Relative -> absolute path conversion */ |
| 427 | resolved = sqfs_get_abs_path(base_path, target); |
| 428 | |
| 429 | free(target); |
| 430 | |
| 431 | return resolved; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * m_list contains each metadata block's position, and m_count is the number of |
| 436 | * elements of m_list. Those metadata blocks come from the compressed directory |
| 437 | * table. |
| 438 | */ |
| 439 | static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list, |
| 440 | int token_count, u32 *m_list, int m_count) |
| 441 | { |
| 442 | struct squashfs_super_block *sblk = ctxt.sblk; |
| 443 | char *path, *target, **sym_tokens, *res, *rem; |
Richard Genoud | 2762f65 | 2020-11-03 12:11:06 +0100 | [diff] [blame] | 444 | int j, ret = 0, new_inode_number, offset; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 445 | struct squashfs_symlink_inode *sym; |
Joao Marcos Costa | f1dfe4a | 2020-09-11 12:21:06 +0200 | [diff] [blame] | 446 | struct squashfs_ldir_inode *ldir; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 447 | struct squashfs_dir_inode *dir; |
| 448 | struct fs_dir_stream *dirsp; |
| 449 | struct fs_dirent *dent; |
| 450 | unsigned char *table; |
| 451 | |
Richard Genoud | 2762f65 | 2020-11-03 12:11:06 +0100 | [diff] [blame] | 452 | res = NULL; |
| 453 | rem = NULL; |
| 454 | path = NULL; |
| 455 | target = NULL; |
| 456 | sym_tokens = NULL; |
| 457 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 458 | dirsp = (struct fs_dir_stream *)dirs; |
| 459 | |
| 460 | /* Start by root inode */ |
| 461 | table = sqfs_find_inode(dirs->inode_table, le32_to_cpu(sblk->inodes), |
| 462 | sblk->inodes, sblk->block_size); |
| 463 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 464 | dir = (struct squashfs_dir_inode *)table; |
Joao Marcos Costa | f1dfe4a | 2020-09-11 12:21:06 +0200 | [diff] [blame] | 465 | ldir = (struct squashfs_ldir_inode *)table; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 466 | |
| 467 | /* get directory offset in directory table */ |
| 468 | offset = sqfs_dir_offset(table, m_list, m_count); |
| 469 | dirs->table = &dirs->dir_table[offset]; |
| 470 | |
| 471 | /* Setup directory header */ |
| 472 | dirs->dir_header = malloc(SQFS_DIR_HEADER_SIZE); |
| 473 | if (!dirs->dir_header) |
| 474 | return -ENOMEM; |
| 475 | |
| 476 | memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE); |
| 477 | |
| 478 | /* Initialize squashfs_dir_stream members */ |
| 479 | dirs->table += SQFS_DIR_HEADER_SIZE; |
| 480 | dirs->size = get_unaligned_le16(&dir->file_size) - SQFS_DIR_HEADER_SIZE; |
| 481 | dirs->entry_count = dirs->dir_header->count + 1; |
| 482 | |
| 483 | /* No path given -> root directory */ |
| 484 | if (!strcmp(token_list[0], "/")) { |
| 485 | dirs->table = &dirs->dir_table[offset]; |
| 486 | memcpy(&dirs->i_dir, dir, sizeof(*dir)); |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | for (j = 0; j < token_count; j++) { |
| 491 | if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) { |
| 492 | printf("** Cannot find directory. **\n"); |
Richard Genoud | 2762f65 | 2020-11-03 12:11:06 +0100 | [diff] [blame] | 493 | ret = -EINVAL; |
| 494 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | while (!sqfs_readdir(dirsp, &dent)) { |
| 498 | ret = strcmp(dent->name, token_list[j]); |
| 499 | if (!ret) |
| 500 | break; |
| 501 | free(dirs->entry); |
Richard Genoud | e405fc4 | 2020-11-03 12:11:05 +0100 | [diff] [blame] | 502 | dirs->entry = NULL; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | if (ret) { |
| 506 | printf("** Cannot find directory. **\n"); |
Richard Genoud | 2762f65 | 2020-11-03 12:11:06 +0100 | [diff] [blame] | 507 | ret = -EINVAL; |
| 508 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /* Redefine inode as the found token */ |
| 512 | new_inode_number = dirs->entry->inode_offset + |
| 513 | dirs->dir_header->inode_number; |
| 514 | |
| 515 | /* Get reference to inode in the inode table */ |
| 516 | table = sqfs_find_inode(dirs->inode_table, new_inode_number, |
| 517 | sblk->inodes, sblk->block_size); |
| 518 | dir = (struct squashfs_dir_inode *)table; |
| 519 | |
| 520 | /* Check for symbolic link and inode type sanity */ |
| 521 | if (get_unaligned_le16(&dir->inode_type) == SQFS_SYMLINK_TYPE) { |
| 522 | sym = (struct squashfs_symlink_inode *)table; |
| 523 | /* Get first j + 1 tokens */ |
| 524 | path = sqfs_concat_tokens(token_list, j + 1); |
Richard Genoud | 2762f65 | 2020-11-03 12:11:06 +0100 | [diff] [blame] | 525 | if (!path) { |
| 526 | ret = -ENOMEM; |
| 527 | goto out; |
| 528 | } |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 529 | /* Resolve for these tokens */ |
| 530 | target = sqfs_resolve_symlink(sym, path); |
Richard Genoud | 2762f65 | 2020-11-03 12:11:06 +0100 | [diff] [blame] | 531 | if (!target) { |
| 532 | ret = -ENOMEM; |
| 533 | goto out; |
| 534 | } |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 535 | /* Join remaining tokens */ |
| 536 | rem = sqfs_concat_tokens(token_list + j + 1, token_count - |
| 537 | j - 1); |
Richard Genoud | 2762f65 | 2020-11-03 12:11:06 +0100 | [diff] [blame] | 538 | if (!rem) { |
| 539 | ret = -ENOMEM; |
| 540 | goto out; |
| 541 | } |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 542 | /* Concatenate remaining tokens and symlink's target */ |
| 543 | res = malloc(strlen(rem) + strlen(target) + 1); |
Richard Genoud | 2762f65 | 2020-11-03 12:11:06 +0100 | [diff] [blame] | 544 | if (!res) { |
| 545 | ret = -ENOMEM; |
| 546 | goto out; |
| 547 | } |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 548 | strcpy(res, target); |
| 549 | res[strlen(target)] = '/'; |
| 550 | strcpy(res + strlen(target) + 1, rem); |
| 551 | token_count = sqfs_count_tokens(res); |
| 552 | |
Richard Genoud | 2762f65 | 2020-11-03 12:11:06 +0100 | [diff] [blame] | 553 | if (token_count < 0) { |
| 554 | ret = -EINVAL; |
| 555 | goto out; |
| 556 | } |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 557 | |
| 558 | sym_tokens = malloc(token_count * sizeof(char *)); |
Richard Genoud | 2762f65 | 2020-11-03 12:11:06 +0100 | [diff] [blame] | 559 | if (!sym_tokens) { |
| 560 | ret = -EINVAL; |
| 561 | goto out; |
| 562 | } |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 563 | |
| 564 | /* Fill tokens list */ |
| 565 | ret = sqfs_tokenize(sym_tokens, token_count, res); |
Richard Genoud | 2762f65 | 2020-11-03 12:11:06 +0100 | [diff] [blame] | 566 | if (ret) { |
| 567 | ret = -EINVAL; |
| 568 | goto out; |
| 569 | } |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 570 | free(dirs->entry); |
Richard Genoud | e405fc4 | 2020-11-03 12:11:05 +0100 | [diff] [blame] | 571 | dirs->entry = NULL; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 572 | |
| 573 | ret = sqfs_search_dir(dirs, sym_tokens, token_count, |
| 574 | m_list, m_count); |
Richard Genoud | 2762f65 | 2020-11-03 12:11:06 +0100 | [diff] [blame] | 575 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 576 | } else if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) { |
| 577 | printf("** Cannot find directory. **\n"); |
| 578 | free(dirs->entry); |
Richard Genoud | e405fc4 | 2020-11-03 12:11:05 +0100 | [diff] [blame] | 579 | dirs->entry = NULL; |
Richard Genoud | 2762f65 | 2020-11-03 12:11:06 +0100 | [diff] [blame] | 580 | ret = -EINVAL; |
| 581 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | /* Check if it is an extended dir. */ |
| 585 | if (get_unaligned_le16(&dir->inode_type) == SQFS_LDIR_TYPE) |
| 586 | ldir = (struct squashfs_ldir_inode *)table; |
| 587 | |
| 588 | /* Get dir. offset into the directory table */ |
| 589 | offset = sqfs_dir_offset(table, m_list, m_count); |
| 590 | dirs->table = &dirs->dir_table[offset]; |
| 591 | |
| 592 | /* Copy directory header */ |
| 593 | memcpy(dirs->dir_header, &dirs->dir_table[offset], |
| 594 | SQFS_DIR_HEADER_SIZE); |
| 595 | |
| 596 | /* Check for empty directory */ |
| 597 | if (sqfs_is_empty_dir(table)) { |
| 598 | printf("Empty directory.\n"); |
| 599 | free(dirs->entry); |
Richard Genoud | e405fc4 | 2020-11-03 12:11:05 +0100 | [diff] [blame] | 600 | dirs->entry = NULL; |
Richard Genoud | 2762f65 | 2020-11-03 12:11:06 +0100 | [diff] [blame] | 601 | ret = SQFS_EMPTY_DIR; |
| 602 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | dirs->table += SQFS_DIR_HEADER_SIZE; |
| 606 | dirs->size = get_unaligned_le16(&dir->file_size); |
| 607 | dirs->entry_count = dirs->dir_header->count + 1; |
| 608 | dirs->size -= SQFS_DIR_HEADER_SIZE; |
| 609 | free(dirs->entry); |
Richard Genoud | e405fc4 | 2020-11-03 12:11:05 +0100 | [diff] [blame] | 610 | dirs->entry = NULL; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | offset = sqfs_dir_offset(table, m_list, m_count); |
| 614 | dirs->table = &dirs->dir_table[offset]; |
| 615 | |
| 616 | if (get_unaligned_le16(&dir->inode_type) == SQFS_DIR_TYPE) |
| 617 | memcpy(&dirs->i_dir, dir, sizeof(*dir)); |
| 618 | else |
| 619 | memcpy(&dirs->i_ldir, ldir, sizeof(*ldir)); |
| 620 | |
Richard Genoud | 2762f65 | 2020-11-03 12:11:06 +0100 | [diff] [blame] | 621 | out: |
| 622 | free(res); |
| 623 | free(rem); |
| 624 | free(path); |
| 625 | free(target); |
| 626 | free(sym_tokens); |
| 627 | return ret; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | /* |
| 631 | * Inode and directory tables are stored as a series of metadata blocks, and |
| 632 | * given the compressed size of this table, we can calculate how much metadata |
| 633 | * blocks are needed to store the result of the decompression, since a |
| 634 | * decompressed metadata block should have a size of 8KiB. |
| 635 | */ |
| 636 | static int sqfs_count_metablks(void *table, u32 offset, int table_size) |
| 637 | { |
| 638 | int count = 0, cur_size = 0, ret; |
| 639 | u32 data_size; |
| 640 | bool comp; |
| 641 | |
| 642 | do { |
| 643 | ret = sqfs_read_metablock(table, offset + cur_size, &comp, |
| 644 | &data_size); |
| 645 | if (ret) |
| 646 | return -EINVAL; |
| 647 | cur_size += data_size + SQFS_HEADER_SIZE; |
| 648 | count++; |
| 649 | } while (cur_size < table_size); |
| 650 | |
| 651 | return count; |
| 652 | } |
| 653 | |
| 654 | /* |
| 655 | * Storing the metadata blocks header's positions will be useful while looking |
| 656 | * for an entry in the directory table, using the reference (index and offset) |
| 657 | * given by its inode. |
| 658 | */ |
| 659 | static int sqfs_get_metablk_pos(u32 *pos_list, void *table, u32 offset, |
| 660 | int metablks_count) |
| 661 | { |
| 662 | u32 data_size, cur_size = 0; |
| 663 | int j, ret = 0; |
| 664 | bool comp; |
| 665 | |
| 666 | if (!metablks_count) |
| 667 | return -EINVAL; |
| 668 | |
| 669 | for (j = 0; j < metablks_count; j++) { |
| 670 | ret = sqfs_read_metablock(table, offset + cur_size, &comp, |
| 671 | &data_size); |
| 672 | if (ret) |
| 673 | return -EINVAL; |
| 674 | |
| 675 | cur_size += data_size + SQFS_HEADER_SIZE; |
| 676 | pos_list[j] = cur_size; |
| 677 | } |
| 678 | |
| 679 | return ret; |
| 680 | } |
| 681 | |
| 682 | static int sqfs_read_inode_table(unsigned char **inode_table) |
| 683 | { |
| 684 | struct squashfs_super_block *sblk = ctxt.sblk; |
| 685 | u64 start, n_blks, table_offset, table_size; |
Joao Marcos Costa | b87fd01 | 2020-08-18 17:17:22 +0200 | [diff] [blame] | 686 | int j, ret = 0, metablks_count; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 687 | unsigned char *src_table, *itb; |
| 688 | u32 src_len, dest_offset = 0; |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 689 | unsigned long dest_len = 0; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 690 | bool compressed; |
| 691 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 692 | table_size = get_unaligned_le64(&sblk->directory_table_start) - |
| 693 | get_unaligned_le64(&sblk->inode_table_start); |
| 694 | start = get_unaligned_le64(&sblk->inode_table_start) / |
| 695 | ctxt.cur_dev->blksz; |
| 696 | n_blks = sqfs_calc_n_blks(sblk->inode_table_start, |
| 697 | sblk->directory_table_start, &table_offset); |
| 698 | |
| 699 | /* Allocate a proper sized buffer (itb) to store the inode table */ |
| 700 | itb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz); |
| 701 | if (!itb) |
| 702 | return -ENOMEM; |
| 703 | |
| 704 | if (sqfs_disk_read(start, n_blks, itb) < 0) { |
| 705 | ret = -EINVAL; |
| 706 | goto free_itb; |
| 707 | } |
| 708 | |
| 709 | /* Parse inode table (metadata block) header */ |
| 710 | ret = sqfs_read_metablock(itb, table_offset, &compressed, &src_len); |
| 711 | if (ret) { |
| 712 | ret = -EINVAL; |
| 713 | goto free_itb; |
| 714 | } |
| 715 | |
| 716 | /* Calculate size to store the whole decompressed table */ |
| 717 | metablks_count = sqfs_count_metablks(itb, table_offset, table_size); |
| 718 | if (metablks_count < 1) { |
| 719 | ret = -EINVAL; |
| 720 | goto free_itb; |
| 721 | } |
| 722 | |
| 723 | *inode_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE); |
| 724 | if (!*inode_table) { |
| 725 | ret = -ENOMEM; |
| 726 | goto free_itb; |
| 727 | } |
| 728 | |
| 729 | src_table = itb + table_offset + SQFS_HEADER_SIZE; |
| 730 | |
| 731 | /* Extract compressed Inode table */ |
| 732 | for (j = 0; j < metablks_count; j++) { |
| 733 | sqfs_read_metablock(itb, table_offset, &compressed, &src_len); |
| 734 | if (compressed) { |
| 735 | dest_len = SQFS_METADATA_BLOCK_SIZE; |
Joao Marcos Costa | b87fd01 | 2020-08-18 17:17:22 +0200 | [diff] [blame] | 736 | ret = sqfs_decompress(&ctxt, *inode_table + |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 737 | dest_offset, &dest_len, |
| 738 | src_table, src_len); |
| 739 | if (ret) { |
| 740 | free(*inode_table); |
Richard Genoud | a62528d | 2020-11-03 12:11:07 +0100 | [diff] [blame] | 741 | *inode_table = NULL; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 742 | goto free_itb; |
| 743 | } |
| 744 | |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 745 | dest_offset += dest_len; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 746 | } else { |
| 747 | memcpy(*inode_table + (j * SQFS_METADATA_BLOCK_SIZE), |
| 748 | src_table, src_len); |
| 749 | } |
| 750 | |
| 751 | /* |
| 752 | * Offsets to the decompression destination, to the metadata |
| 753 | * buffer 'itb' and to the decompression source, respectively. |
| 754 | */ |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 755 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 756 | table_offset += src_len + SQFS_HEADER_SIZE; |
| 757 | src_table += src_len + SQFS_HEADER_SIZE; |
| 758 | } |
| 759 | |
| 760 | free_itb: |
| 761 | free(itb); |
| 762 | |
| 763 | return ret; |
| 764 | } |
| 765 | |
| 766 | static int sqfs_read_directory_table(unsigned char **dir_table, u32 **pos_list) |
| 767 | { |
| 768 | u64 start, n_blks, table_offset, table_size; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 769 | struct squashfs_super_block *sblk = ctxt.sblk; |
Joao Marcos Costa | b87fd01 | 2020-08-18 17:17:22 +0200 | [diff] [blame] | 770 | int j, ret = 0, metablks_count = -1; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 771 | unsigned char *src_table, *dtb; |
| 772 | u32 src_len, dest_offset = 0; |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 773 | unsigned long dest_len = 0; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 774 | bool compressed; |
| 775 | |
Richard Genoud | 2f9c985 | 2020-11-03 12:11:04 +0100 | [diff] [blame] | 776 | *dir_table = NULL; |
| 777 | *pos_list = NULL; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 778 | /* DIRECTORY TABLE */ |
| 779 | table_size = get_unaligned_le64(&sblk->fragment_table_start) - |
| 780 | get_unaligned_le64(&sblk->directory_table_start); |
| 781 | start = get_unaligned_le64(&sblk->directory_table_start) / |
| 782 | ctxt.cur_dev->blksz; |
| 783 | n_blks = sqfs_calc_n_blks(sblk->directory_table_start, |
| 784 | sblk->fragment_table_start, &table_offset); |
| 785 | |
| 786 | /* Allocate a proper sized buffer (dtb) to store the directory table */ |
| 787 | dtb = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz); |
| 788 | if (!dtb) |
| 789 | return -ENOMEM; |
| 790 | |
| 791 | if (sqfs_disk_read(start, n_blks, dtb) < 0) |
Richard Genoud | 2f9c985 | 2020-11-03 12:11:04 +0100 | [diff] [blame] | 792 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 793 | |
| 794 | /* Parse directory table (metadata block) header */ |
| 795 | ret = sqfs_read_metablock(dtb, table_offset, &compressed, &src_len); |
| 796 | if (ret) |
Richard Genoud | 2f9c985 | 2020-11-03 12:11:04 +0100 | [diff] [blame] | 797 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 798 | |
| 799 | /* Calculate total size to store the whole decompressed table */ |
| 800 | metablks_count = sqfs_count_metablks(dtb, table_offset, table_size); |
| 801 | if (metablks_count < 1) |
Richard Genoud | 2f9c985 | 2020-11-03 12:11:04 +0100 | [diff] [blame] | 802 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 803 | |
| 804 | *dir_table = malloc(metablks_count * SQFS_METADATA_BLOCK_SIZE); |
| 805 | if (!*dir_table) |
Richard Genoud | 2f9c985 | 2020-11-03 12:11:04 +0100 | [diff] [blame] | 806 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 807 | |
| 808 | *pos_list = malloc(metablks_count * sizeof(u32)); |
Richard Genoud | 2f9c985 | 2020-11-03 12:11:04 +0100 | [diff] [blame] | 809 | if (!*pos_list) |
| 810 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 811 | |
| 812 | ret = sqfs_get_metablk_pos(*pos_list, dtb, table_offset, |
| 813 | metablks_count); |
| 814 | if (ret) { |
| 815 | metablks_count = -1; |
Richard Genoud | 2f9c985 | 2020-11-03 12:11:04 +0100 | [diff] [blame] | 816 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | src_table = dtb + table_offset + SQFS_HEADER_SIZE; |
| 820 | |
| 821 | /* Extract compressed Directory table */ |
| 822 | dest_offset = 0; |
| 823 | for (j = 0; j < metablks_count; j++) { |
| 824 | sqfs_read_metablock(dtb, table_offset, &compressed, &src_len); |
| 825 | if (compressed) { |
| 826 | dest_len = SQFS_METADATA_BLOCK_SIZE; |
Joao Marcos Costa | b87fd01 | 2020-08-18 17:17:22 +0200 | [diff] [blame] | 827 | ret = sqfs_decompress(&ctxt, *dir_table + |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 828 | (j * SQFS_METADATA_BLOCK_SIZE), |
| 829 | &dest_len, src_table, src_len); |
| 830 | if (ret) { |
| 831 | metablks_count = -1; |
Richard Genoud | 2f9c985 | 2020-11-03 12:11:04 +0100 | [diff] [blame] | 832 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | if (dest_len < SQFS_METADATA_BLOCK_SIZE) { |
| 836 | dest_offset += dest_len; |
| 837 | break; |
| 838 | } |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 839 | |
| 840 | dest_offset += dest_len; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 841 | } else { |
| 842 | memcpy(*dir_table + (j * SQFS_METADATA_BLOCK_SIZE), |
| 843 | src_table, src_len); |
| 844 | } |
| 845 | |
| 846 | /* |
| 847 | * Offsets to the decompression destination, to the metadata |
| 848 | * buffer 'dtb' and to the decompression source, respectively. |
| 849 | */ |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 850 | table_offset += src_len + SQFS_HEADER_SIZE; |
| 851 | src_table += src_len + SQFS_HEADER_SIZE; |
| 852 | } |
| 853 | |
Richard Genoud | 2f9c985 | 2020-11-03 12:11:04 +0100 | [diff] [blame] | 854 | out: |
| 855 | if (metablks_count < 1) { |
| 856 | free(*dir_table); |
| 857 | free(*pos_list); |
| 858 | *dir_table = NULL; |
| 859 | *pos_list = NULL; |
| 860 | } |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 861 | free(dtb); |
| 862 | |
| 863 | return metablks_count; |
| 864 | } |
| 865 | |
| 866 | int sqfs_opendir(const char *filename, struct fs_dir_stream **dirsp) |
| 867 | { |
| 868 | unsigned char *inode_table = NULL, *dir_table = NULL; |
Richard Genoud | 32bea5b | 2020-11-03 12:11:01 +0100 | [diff] [blame] | 869 | int j, token_count = 0, ret = 0, metablks_count; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 870 | struct squashfs_dir_stream *dirs; |
Richard Genoud | 32bea5b | 2020-11-03 12:11:01 +0100 | [diff] [blame] | 871 | char **token_list = NULL, *path = NULL; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 872 | u32 *pos_list = NULL; |
| 873 | |
| 874 | dirs = malloc(sizeof(*dirs)); |
| 875 | if (!dirs) |
| 876 | return -EINVAL; |
| 877 | |
Richard Genoud | 557f08f | 2020-11-03 12:11:00 +0100 | [diff] [blame] | 878 | /* these should be set to NULL to prevent dangling pointers */ |
| 879 | dirs->dir_header = NULL; |
| 880 | dirs->entry = NULL; |
| 881 | dirs->table = NULL; |
| 882 | dirs->inode_table = NULL; |
| 883 | dirs->dir_table = NULL; |
| 884 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 885 | ret = sqfs_read_inode_table(&inode_table); |
Richard Genoud | 557f08f | 2020-11-03 12:11:00 +0100 | [diff] [blame] | 886 | if (ret) { |
| 887 | ret = -EINVAL; |
Richard Genoud | 32bea5b | 2020-11-03 12:11:01 +0100 | [diff] [blame] | 888 | goto out; |
Richard Genoud | 557f08f | 2020-11-03 12:11:00 +0100 | [diff] [blame] | 889 | } |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 890 | |
| 891 | metablks_count = sqfs_read_directory_table(&dir_table, &pos_list); |
Richard Genoud | 557f08f | 2020-11-03 12:11:00 +0100 | [diff] [blame] | 892 | if (metablks_count < 1) { |
| 893 | ret = -EINVAL; |
Richard Genoud | 32bea5b | 2020-11-03 12:11:01 +0100 | [diff] [blame] | 894 | goto out; |
Richard Genoud | 557f08f | 2020-11-03 12:11:00 +0100 | [diff] [blame] | 895 | } |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 896 | |
| 897 | /* Tokenize filename */ |
| 898 | token_count = sqfs_count_tokens(filename); |
Richard Genoud | 557f08f | 2020-11-03 12:11:00 +0100 | [diff] [blame] | 899 | if (token_count < 0) { |
| 900 | ret = -EINVAL; |
Richard Genoud | 32bea5b | 2020-11-03 12:11:01 +0100 | [diff] [blame] | 901 | goto out; |
Richard Genoud | 557f08f | 2020-11-03 12:11:00 +0100 | [diff] [blame] | 902 | } |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 903 | |
| 904 | path = strdup(filename); |
Richard Genoud | 557f08f | 2020-11-03 12:11:00 +0100 | [diff] [blame] | 905 | if (!path) { |
| 906 | ret = -EINVAL; |
Richard Genoud | 32bea5b | 2020-11-03 12:11:01 +0100 | [diff] [blame] | 907 | goto out; |
Richard Genoud | 557f08f | 2020-11-03 12:11:00 +0100 | [diff] [blame] | 908 | } |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 909 | |
| 910 | token_list = malloc(token_count * sizeof(char *)); |
| 911 | if (!token_list) { |
| 912 | ret = -EINVAL; |
Richard Genoud | 32bea5b | 2020-11-03 12:11:01 +0100 | [diff] [blame] | 913 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | /* Fill tokens list */ |
| 917 | ret = sqfs_tokenize(token_list, token_count, path); |
| 918 | if (ret) |
Richard Genoud | 32bea5b | 2020-11-03 12:11:01 +0100 | [diff] [blame] | 919 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 920 | /* |
| 921 | * ldir's (extended directory) size is greater than dir, so it works as |
| 922 | * a general solution for the malloc size, since 'i' is a union. |
| 923 | */ |
| 924 | dirs->inode_table = inode_table; |
| 925 | dirs->dir_table = dir_table; |
| 926 | ret = sqfs_search_dir(dirs, token_list, token_count, pos_list, |
| 927 | metablks_count); |
| 928 | if (ret) |
Richard Genoud | 32bea5b | 2020-11-03 12:11:01 +0100 | [diff] [blame] | 929 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 930 | |
| 931 | if (le16_to_cpu(dirs->i_dir.inode_type) == SQFS_DIR_TYPE) |
| 932 | dirs->size = le16_to_cpu(dirs->i_dir.file_size); |
| 933 | else |
| 934 | dirs->size = le32_to_cpu(dirs->i_ldir.file_size); |
| 935 | |
| 936 | /* Setup directory header */ |
| 937 | memcpy(dirs->dir_header, dirs->table, SQFS_DIR_HEADER_SIZE); |
| 938 | dirs->entry_count = dirs->dir_header->count + 1; |
| 939 | dirs->size -= SQFS_DIR_HEADER_SIZE; |
| 940 | |
| 941 | /* Setup entry */ |
| 942 | dirs->entry = NULL; |
| 943 | dirs->table += SQFS_DIR_HEADER_SIZE; |
| 944 | |
| 945 | *dirsp = (struct fs_dir_stream *)dirs; |
| 946 | |
Richard Genoud | 32bea5b | 2020-11-03 12:11:01 +0100 | [diff] [blame] | 947 | out: |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 948 | for (j = 0; j < token_count; j++) |
| 949 | free(token_list[j]); |
| 950 | free(token_list); |
| 951 | free(pos_list); |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 952 | free(path); |
Richard Genoud | 32bea5b | 2020-11-03 12:11:01 +0100 | [diff] [blame] | 953 | if (ret) { |
Richard Genoud | 557f08f | 2020-11-03 12:11:00 +0100 | [diff] [blame] | 954 | free(inode_table); |
Richard Genoud | 557f08f | 2020-11-03 12:11:00 +0100 | [diff] [blame] | 955 | free(dirs); |
Richard Genoud | 32bea5b | 2020-11-03 12:11:01 +0100 | [diff] [blame] | 956 | } |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 957 | |
| 958 | return ret; |
| 959 | } |
| 960 | |
| 961 | int sqfs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp) |
| 962 | { |
| 963 | struct squashfs_super_block *sblk = ctxt.sblk; |
| 964 | struct squashfs_dir_stream *dirs; |
| 965 | struct squashfs_lreg_inode *lreg; |
| 966 | struct squashfs_base_inode *base; |
| 967 | struct squashfs_reg_inode *reg; |
| 968 | int i_number, offset = 0, ret; |
| 969 | struct fs_dirent *dent; |
| 970 | unsigned char *ipos; |
| 971 | |
| 972 | dirs = (struct squashfs_dir_stream *)fs_dirs; |
| 973 | if (!dirs->size) { |
| 974 | *dentp = NULL; |
| 975 | return -SQFS_STOP_READDIR; |
| 976 | } |
| 977 | |
| 978 | dent = &dirs->dentp; |
| 979 | |
| 980 | if (!dirs->entry_count) { |
| 981 | if (dirs->size > SQFS_DIR_HEADER_SIZE) { |
| 982 | dirs->size -= SQFS_DIR_HEADER_SIZE; |
| 983 | } else { |
| 984 | *dentp = NULL; |
| 985 | dirs->size = 0; |
| 986 | return -SQFS_STOP_READDIR; |
| 987 | } |
| 988 | |
| 989 | if (dirs->size > SQFS_EMPTY_FILE_SIZE) { |
| 990 | /* Read follow-up (emitted) dir. header */ |
| 991 | memcpy(dirs->dir_header, dirs->table, |
| 992 | SQFS_DIR_HEADER_SIZE); |
| 993 | dirs->entry_count = dirs->dir_header->count + 1; |
| 994 | ret = sqfs_read_entry(&dirs->entry, dirs->table + |
| 995 | SQFS_DIR_HEADER_SIZE); |
| 996 | if (ret) |
| 997 | return -SQFS_STOP_READDIR; |
| 998 | |
| 999 | dirs->table += SQFS_DIR_HEADER_SIZE; |
| 1000 | } |
| 1001 | } else { |
| 1002 | ret = sqfs_read_entry(&dirs->entry, dirs->table); |
| 1003 | if (ret) |
| 1004 | return -SQFS_STOP_READDIR; |
| 1005 | } |
| 1006 | |
| 1007 | i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset; |
| 1008 | ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes, |
| 1009 | sblk->block_size); |
| 1010 | |
| 1011 | base = (struct squashfs_base_inode *)ipos; |
| 1012 | |
| 1013 | /* Set entry type and size */ |
| 1014 | switch (dirs->entry->type) { |
| 1015 | case SQFS_DIR_TYPE: |
| 1016 | case SQFS_LDIR_TYPE: |
| 1017 | dent->type = FS_DT_DIR; |
| 1018 | break; |
| 1019 | case SQFS_REG_TYPE: |
| 1020 | case SQFS_LREG_TYPE: |
| 1021 | /* |
| 1022 | * Entries do not differentiate extended from regular types, so |
| 1023 | * it needs to be verified manually. |
| 1024 | */ |
| 1025 | if (get_unaligned_le16(&base->inode_type) == SQFS_LREG_TYPE) { |
| 1026 | lreg = (struct squashfs_lreg_inode *)ipos; |
| 1027 | dent->size = get_unaligned_le64(&lreg->file_size); |
| 1028 | } else { |
| 1029 | reg = (struct squashfs_reg_inode *)ipos; |
| 1030 | dent->size = get_unaligned_le32(®->file_size); |
| 1031 | } |
| 1032 | |
| 1033 | dent->type = FS_DT_REG; |
| 1034 | break; |
| 1035 | case SQFS_BLKDEV_TYPE: |
| 1036 | case SQFS_CHRDEV_TYPE: |
| 1037 | case SQFS_LBLKDEV_TYPE: |
| 1038 | case SQFS_LCHRDEV_TYPE: |
| 1039 | case SQFS_FIFO_TYPE: |
| 1040 | case SQFS_SOCKET_TYPE: |
| 1041 | case SQFS_LFIFO_TYPE: |
| 1042 | case SQFS_LSOCKET_TYPE: |
| 1043 | dent->type = SQFS_MISC_ENTRY_TYPE; |
| 1044 | break; |
| 1045 | case SQFS_SYMLINK_TYPE: |
| 1046 | case SQFS_LSYMLINK_TYPE: |
| 1047 | dent->type = FS_DT_LNK; |
| 1048 | break; |
| 1049 | default: |
| 1050 | return -SQFS_STOP_READDIR; |
| 1051 | } |
| 1052 | |
| 1053 | /* Set entry name */ |
| 1054 | strncpy(dent->name, dirs->entry->name, dirs->entry->name_size + 1); |
| 1055 | dent->name[dirs->entry->name_size + 1] = '\0'; |
| 1056 | |
| 1057 | offset = dirs->entry->name_size + 1 + SQFS_ENTRY_BASE_LENGTH; |
| 1058 | dirs->entry_count--; |
| 1059 | |
| 1060 | /* Decrement size to be read */ |
| 1061 | if (dirs->size > offset) |
| 1062 | dirs->size -= offset; |
| 1063 | else |
| 1064 | dirs->size = 0; |
| 1065 | |
| 1066 | /* Keep a reference to the current entry before incrementing it */ |
| 1067 | dirs->table += offset; |
| 1068 | |
| 1069 | *dentp = dent; |
| 1070 | |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
| 1074 | int sqfs_probe(struct blk_desc *fs_dev_desc, struct disk_partition *fs_partition) |
| 1075 | { |
| 1076 | struct squashfs_super_block *sblk; |
| 1077 | int ret; |
| 1078 | |
| 1079 | ctxt.cur_dev = fs_dev_desc; |
| 1080 | ctxt.cur_part_info = *fs_partition; |
| 1081 | |
| 1082 | ret = sqfs_read_sblk(&sblk); |
| 1083 | if (ret) |
| 1084 | return ret; |
| 1085 | |
| 1086 | /* Make sure it has a valid SquashFS magic number*/ |
| 1087 | if (get_unaligned_le32(&sblk->s_magic) != SQFS_MAGIC_NUMBER) { |
| 1088 | printf("Bad magic number for SquashFS image.\n"); |
| 1089 | ctxt.cur_dev = NULL; |
| 1090 | return -EINVAL; |
| 1091 | } |
| 1092 | |
| 1093 | ctxt.sblk = sblk; |
| 1094 | |
Joao Marcos Costa | 877576c | 2020-08-18 17:17:21 +0200 | [diff] [blame] | 1095 | ret = sqfs_decompressor_init(&ctxt); |
| 1096 | |
| 1097 | if (ret) { |
| 1098 | ctxt.cur_dev = NULL; |
| 1099 | free(ctxt.sblk); |
| 1100 | return -EINVAL; |
| 1101 | } |
| 1102 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1103 | return 0; |
| 1104 | } |
| 1105 | |
| 1106 | static char *sqfs_basename(char *path) |
| 1107 | { |
| 1108 | char *fname; |
| 1109 | |
| 1110 | fname = path + strlen(path) - 1; |
| 1111 | while (fname >= path) { |
| 1112 | if (*fname == '/') { |
| 1113 | fname++; |
| 1114 | break; |
| 1115 | } |
| 1116 | |
| 1117 | fname--; |
| 1118 | } |
| 1119 | |
| 1120 | return fname; |
| 1121 | } |
| 1122 | |
| 1123 | static char *sqfs_dirname(char *path) |
| 1124 | { |
| 1125 | char *fname; |
| 1126 | |
| 1127 | fname = sqfs_basename(path); |
| 1128 | --fname; |
| 1129 | *fname = '\0'; |
| 1130 | |
| 1131 | return path; |
| 1132 | } |
| 1133 | |
| 1134 | /* |
| 1135 | * Takes a path to file and splits it in two parts: the filename itself and the |
| 1136 | * directory's path, e.g.: |
| 1137 | * path: /path/to/file.txt |
| 1138 | * file: file.txt |
| 1139 | * dir: /path/to |
| 1140 | */ |
| 1141 | static int sqfs_split_path(char **file, char **dir, const char *path) |
| 1142 | { |
| 1143 | char *dirc, *basec, *bname, *dname, *tmp_path; |
| 1144 | int ret = 0; |
| 1145 | |
Richard Genoud | 8cf2f02 | 2020-11-03 12:11:03 +0100 | [diff] [blame] | 1146 | *file = NULL; |
| 1147 | *dir = NULL; |
| 1148 | dirc = NULL; |
| 1149 | basec = NULL; |
| 1150 | bname = NULL; |
| 1151 | dname = NULL; |
| 1152 | tmp_path = NULL; |
| 1153 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1154 | /* check for first slash in path*/ |
| 1155 | if (path[0] == '/') { |
| 1156 | tmp_path = strdup(path); |
Richard Genoud | 8cf2f02 | 2020-11-03 12:11:03 +0100 | [diff] [blame] | 1157 | if (!tmp_path) { |
| 1158 | ret = -ENOMEM; |
| 1159 | goto out; |
| 1160 | } |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1161 | } else { |
| 1162 | tmp_path = malloc(strlen(path) + 2); |
Richard Genoud | 8cf2f02 | 2020-11-03 12:11:03 +0100 | [diff] [blame] | 1163 | if (!tmp_path) { |
| 1164 | ret = -ENOMEM; |
| 1165 | goto out; |
| 1166 | } |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1167 | tmp_path[0] = '/'; |
| 1168 | strcpy(tmp_path + 1, path); |
| 1169 | } |
| 1170 | |
| 1171 | /* String duplicates */ |
| 1172 | dirc = strdup(tmp_path); |
| 1173 | if (!dirc) { |
| 1174 | ret = -ENOMEM; |
Richard Genoud | 8cf2f02 | 2020-11-03 12:11:03 +0100 | [diff] [blame] | 1175 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | basec = strdup(tmp_path); |
| 1179 | if (!basec) { |
| 1180 | ret = -ENOMEM; |
Richard Genoud | 8cf2f02 | 2020-11-03 12:11:03 +0100 | [diff] [blame] | 1181 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | dname = sqfs_dirname(dirc); |
| 1185 | bname = sqfs_basename(basec); |
| 1186 | |
| 1187 | *file = strdup(bname); |
| 1188 | |
| 1189 | if (!*file) { |
| 1190 | ret = -ENOMEM; |
Richard Genoud | 8cf2f02 | 2020-11-03 12:11:03 +0100 | [diff] [blame] | 1191 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | if (*dname == '\0') { |
| 1195 | *dir = malloc(2); |
| 1196 | if (!*dir) { |
| 1197 | ret = -ENOMEM; |
Richard Genoud | 8cf2f02 | 2020-11-03 12:11:03 +0100 | [diff] [blame] | 1198 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | (*dir)[0] = '/'; |
| 1202 | (*dir)[1] = '\0'; |
| 1203 | } else { |
| 1204 | *dir = strdup(dname); |
| 1205 | if (!*dir) { |
| 1206 | ret = -ENOMEM; |
Richard Genoud | 8cf2f02 | 2020-11-03 12:11:03 +0100 | [diff] [blame] | 1207 | goto out; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1208 | } |
| 1209 | } |
| 1210 | |
Richard Genoud | 8cf2f02 | 2020-11-03 12:11:03 +0100 | [diff] [blame] | 1211 | out: |
| 1212 | if (ret) { |
| 1213 | free(*file); |
| 1214 | free(*dir); |
| 1215 | *dir = NULL; |
| 1216 | *file = NULL; |
| 1217 | } |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1218 | free(basec); |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1219 | free(dirc); |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1220 | free(tmp_path); |
| 1221 | |
| 1222 | return ret; |
| 1223 | } |
| 1224 | |
| 1225 | static int sqfs_get_regfile_info(struct squashfs_reg_inode *reg, |
| 1226 | struct squashfs_file_info *finfo, |
| 1227 | struct squashfs_fragment_block_entry *fentry, |
| 1228 | __le32 blksz) |
| 1229 | { |
| 1230 | int datablk_count = 0, ret; |
| 1231 | |
| 1232 | finfo->size = get_unaligned_le32(®->file_size); |
| 1233 | finfo->offset = get_unaligned_le32(®->offset); |
| 1234 | finfo->start = get_unaligned_le32(®->start_block); |
| 1235 | finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(®->fragment)); |
| 1236 | |
Joao Marcos Costa | f1dfe4a | 2020-09-11 12:21:06 +0200 | [diff] [blame] | 1237 | if (finfo->frag && finfo->offset == 0xFFFFFFFF) |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 1238 | return -EINVAL; |
| 1239 | |
Joao Marcos Costa | f1dfe4a | 2020-09-11 12:21:06 +0200 | [diff] [blame] | 1240 | if (finfo->size < 1 || finfo->start == 0xFFFFFFFF) |
| 1241 | return -EINVAL; |
| 1242 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1243 | if (finfo->frag) { |
| 1244 | datablk_count = finfo->size / le32_to_cpu(blksz); |
| 1245 | ret = sqfs_frag_lookup(get_unaligned_le32(®->fragment), |
| 1246 | fentry); |
| 1247 | if (ret < 0) |
| 1248 | return -EINVAL; |
| 1249 | finfo->comp = true; |
Joao Marcos Costa | f1dfe4a | 2020-09-11 12:21:06 +0200 | [diff] [blame] | 1250 | if (fentry->size < 1 || fentry->start == 0x7FFFFFFF) |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 1251 | return -EINVAL; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1252 | } else { |
| 1253 | datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz)); |
| 1254 | } |
| 1255 | |
| 1256 | finfo->blk_sizes = malloc(datablk_count * sizeof(u32)); |
| 1257 | if (!finfo->blk_sizes) |
| 1258 | return -ENOMEM; |
| 1259 | |
| 1260 | return datablk_count; |
| 1261 | } |
| 1262 | |
| 1263 | static int sqfs_get_lregfile_info(struct squashfs_lreg_inode *lreg, |
| 1264 | struct squashfs_file_info *finfo, |
| 1265 | struct squashfs_fragment_block_entry *fentry, |
| 1266 | __le32 blksz) |
| 1267 | { |
| 1268 | int datablk_count = 0, ret; |
| 1269 | |
| 1270 | finfo->size = get_unaligned_le64(&lreg->file_size); |
| 1271 | finfo->offset = get_unaligned_le32(&lreg->offset); |
| 1272 | finfo->start = get_unaligned_le64(&lreg->start_block); |
| 1273 | finfo->frag = SQFS_IS_FRAGMENTED(get_unaligned_le32(&lreg->fragment)); |
| 1274 | |
Joao Marcos Costa | f1dfe4a | 2020-09-11 12:21:06 +0200 | [diff] [blame] | 1275 | if (finfo->frag && finfo->offset == 0xFFFFFFFF) |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 1276 | return -EINVAL; |
| 1277 | |
Joao Marcos Costa | f1dfe4a | 2020-09-11 12:21:06 +0200 | [diff] [blame] | 1278 | if (finfo->size < 1 || finfo->start == 0x7FFFFFFF) |
| 1279 | return -EINVAL; |
| 1280 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1281 | if (finfo->frag) { |
| 1282 | datablk_count = finfo->size / le32_to_cpu(blksz); |
| 1283 | ret = sqfs_frag_lookup(get_unaligned_le32(&lreg->fragment), |
| 1284 | fentry); |
| 1285 | if (ret < 0) |
| 1286 | return -EINVAL; |
| 1287 | finfo->comp = true; |
Joao Marcos Costa | f1dfe4a | 2020-09-11 12:21:06 +0200 | [diff] [blame] | 1288 | if (fentry->size < 1 || fentry->start == 0x7FFFFFFF) |
Joao Marcos Costa | b7fb3c3 | 2020-08-19 18:28:41 +0200 | [diff] [blame] | 1289 | return -EINVAL; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1290 | } else { |
| 1291 | datablk_count = DIV_ROUND_UP(finfo->size, le32_to_cpu(blksz)); |
| 1292 | } |
| 1293 | |
| 1294 | finfo->blk_sizes = malloc(datablk_count * sizeof(u32)); |
| 1295 | if (!finfo->blk_sizes) |
| 1296 | return -ENOMEM; |
| 1297 | |
| 1298 | return datablk_count; |
| 1299 | } |
| 1300 | |
| 1301 | int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len, |
| 1302 | loff_t *actread) |
| 1303 | { |
| 1304 | char *dir, *fragment_block, *datablock = NULL, *data_buffer = NULL; |
| 1305 | char *fragment, *file, *resolved, *data; |
| 1306 | u64 start, n_blks, table_size, data_offset, table_offset; |
Joao Marcos Costa | b87fd01 | 2020-08-18 17:17:22 +0200 | [diff] [blame] | 1307 | int ret, j, i_number, datablk_count = 0; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1308 | struct squashfs_super_block *sblk = ctxt.sblk; |
| 1309 | struct squashfs_fragment_block_entry frag_entry; |
| 1310 | struct squashfs_file_info finfo = {0}; |
| 1311 | struct squashfs_symlink_inode *symlink; |
| 1312 | struct fs_dir_stream *dirsp = NULL; |
| 1313 | struct squashfs_dir_stream *dirs; |
| 1314 | struct squashfs_lreg_inode *lreg; |
| 1315 | struct squashfs_base_inode *base; |
| 1316 | struct squashfs_reg_inode *reg; |
| 1317 | unsigned long dest_len; |
| 1318 | struct fs_dirent *dent; |
| 1319 | unsigned char *ipos; |
| 1320 | |
| 1321 | *actread = 0; |
| 1322 | |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1323 | /* |
| 1324 | * sqfs_opendir will uncompress inode and directory tables, and will |
| 1325 | * return a pointer to the directory that contains the requested file. |
| 1326 | */ |
| 1327 | sqfs_split_path(&file, &dir, filename); |
| 1328 | ret = sqfs_opendir(dir, &dirsp); |
| 1329 | if (ret) { |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1330 | goto free_paths; |
| 1331 | } |
| 1332 | |
| 1333 | dirs = (struct squashfs_dir_stream *)dirsp; |
| 1334 | |
| 1335 | /* For now, only regular files are able to be loaded */ |
| 1336 | while (!sqfs_readdir(dirsp, &dent)) { |
| 1337 | ret = strcmp(dent->name, file); |
| 1338 | if (!ret) |
| 1339 | break; |
| 1340 | |
| 1341 | free(dirs->entry); |
Richard Genoud | 0b8aa99 | 2020-11-03 12:11:11 +0100 | [diff] [blame] | 1342 | dirs->entry = NULL; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | if (ret) { |
| 1346 | printf("File not found.\n"); |
| 1347 | *actread = 0; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1348 | ret = -ENOENT; |
| 1349 | goto free_paths; |
| 1350 | } |
| 1351 | |
| 1352 | i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset; |
| 1353 | ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes, |
| 1354 | sblk->block_size); |
| 1355 | |
| 1356 | base = (struct squashfs_base_inode *)ipos; |
| 1357 | switch (get_unaligned_le16(&base->inode_type)) { |
| 1358 | case SQFS_REG_TYPE: |
| 1359 | reg = (struct squashfs_reg_inode *)ipos; |
| 1360 | datablk_count = sqfs_get_regfile_info(reg, &finfo, &frag_entry, |
| 1361 | sblk->block_size); |
| 1362 | if (datablk_count < 0) { |
| 1363 | ret = -EINVAL; |
| 1364 | goto free_paths; |
| 1365 | } |
| 1366 | |
| 1367 | memcpy(finfo.blk_sizes, ipos + sizeof(*reg), |
| 1368 | datablk_count * sizeof(u32)); |
| 1369 | break; |
| 1370 | case SQFS_LREG_TYPE: |
| 1371 | lreg = (struct squashfs_lreg_inode *)ipos; |
| 1372 | datablk_count = sqfs_get_lregfile_info(lreg, &finfo, |
| 1373 | &frag_entry, |
| 1374 | sblk->block_size); |
| 1375 | if (datablk_count < 0) { |
| 1376 | ret = -EINVAL; |
| 1377 | goto free_paths; |
| 1378 | } |
| 1379 | |
| 1380 | memcpy(finfo.blk_sizes, ipos + sizeof(*lreg), |
| 1381 | datablk_count * sizeof(u32)); |
| 1382 | break; |
| 1383 | case SQFS_SYMLINK_TYPE: |
| 1384 | case SQFS_LSYMLINK_TYPE: |
| 1385 | symlink = (struct squashfs_symlink_inode *)ipos; |
| 1386 | resolved = sqfs_resolve_symlink(symlink, filename); |
| 1387 | ret = sqfs_read(resolved, buf, offset, len, actread); |
| 1388 | free(resolved); |
| 1389 | goto free_paths; |
| 1390 | case SQFS_BLKDEV_TYPE: |
| 1391 | case SQFS_CHRDEV_TYPE: |
| 1392 | case SQFS_LBLKDEV_TYPE: |
| 1393 | case SQFS_LCHRDEV_TYPE: |
| 1394 | case SQFS_FIFO_TYPE: |
| 1395 | case SQFS_SOCKET_TYPE: |
| 1396 | case SQFS_LFIFO_TYPE: |
| 1397 | case SQFS_LSOCKET_TYPE: |
| 1398 | default: |
| 1399 | printf("Unsupported entry type\n"); |
| 1400 | ret = -EINVAL; |
| 1401 | goto free_paths; |
| 1402 | } |
| 1403 | |
| 1404 | /* If the user specifies a length, check its sanity */ |
| 1405 | if (len) { |
| 1406 | if (len > finfo.size) { |
| 1407 | ret = -EINVAL; |
| 1408 | goto free_paths; |
| 1409 | } |
| 1410 | |
| 1411 | finfo.size = len; |
| 1412 | } |
| 1413 | |
| 1414 | if (datablk_count) { |
| 1415 | data_offset = finfo.start; |
| 1416 | datablock = malloc(get_unaligned_le32(&sblk->block_size)); |
| 1417 | if (!datablock) { |
| 1418 | ret = -ENOMEM; |
| 1419 | goto free_paths; |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | for (j = 0; j < datablk_count; j++) { |
| 1424 | start = data_offset / ctxt.cur_dev->blksz; |
| 1425 | table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]); |
| 1426 | table_offset = data_offset - (start * ctxt.cur_dev->blksz); |
| 1427 | n_blks = DIV_ROUND_UP(table_size + table_offset, |
| 1428 | ctxt.cur_dev->blksz); |
| 1429 | |
| 1430 | data_buffer = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz); |
| 1431 | |
| 1432 | if (!data_buffer) { |
| 1433 | ret = -ENOMEM; |
| 1434 | goto free_datablk; |
| 1435 | } |
| 1436 | |
| 1437 | ret = sqfs_disk_read(start, n_blks, data_buffer); |
| 1438 | if (ret < 0) { |
| 1439 | /* |
| 1440 | * Possible causes: too many data blocks or too large |
| 1441 | * SquashFS block size. Tip: re-compile the SquashFS |
| 1442 | * image with mksquashfs's -b <block_size> option. |
| 1443 | */ |
| 1444 | printf("Error: too many data blocks to be read.\n"); |
| 1445 | goto free_buffer; |
| 1446 | } |
| 1447 | |
| 1448 | data = data_buffer + table_offset; |
| 1449 | |
| 1450 | /* Load the data */ |
| 1451 | if (SQFS_COMPRESSED_BLOCK(finfo.blk_sizes[j])) { |
| 1452 | dest_len = get_unaligned_le32(&sblk->block_size); |
Joao Marcos Costa | b87fd01 | 2020-08-18 17:17:22 +0200 | [diff] [blame] | 1453 | ret = sqfs_decompress(&ctxt, datablock, &dest_len, |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1454 | data, table_size); |
| 1455 | if (ret) |
| 1456 | goto free_buffer; |
| 1457 | |
| 1458 | memcpy(buf + offset + *actread, datablock, dest_len); |
| 1459 | *actread += dest_len; |
| 1460 | } else { |
| 1461 | memcpy(buf + offset + *actread, data, table_size); |
| 1462 | *actread += table_size; |
| 1463 | } |
| 1464 | |
| 1465 | data_offset += table_size; |
Richard Genoud | 7584465 | 2020-11-03 12:11:14 +0100 | [diff] [blame] | 1466 | free(data_buffer); |
| 1467 | data_buffer = NULL; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | free(finfo.blk_sizes); |
| 1471 | |
| 1472 | /* |
| 1473 | * There is no need to continue if the file is not fragmented. |
| 1474 | */ |
| 1475 | if (!finfo.frag) { |
| 1476 | ret = 0; |
| 1477 | goto free_buffer; |
| 1478 | } |
| 1479 | |
| 1480 | start = frag_entry.start / ctxt.cur_dev->blksz; |
| 1481 | table_size = SQFS_BLOCK_SIZE(frag_entry.size); |
| 1482 | table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz); |
| 1483 | n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz); |
| 1484 | |
| 1485 | fragment = malloc_cache_aligned(n_blks * ctxt.cur_dev->blksz); |
| 1486 | |
| 1487 | if (!fragment) { |
| 1488 | ret = -ENOMEM; |
| 1489 | goto free_buffer; |
| 1490 | } |
| 1491 | |
| 1492 | ret = sqfs_disk_read(start, n_blks, fragment); |
| 1493 | if (ret < 0) |
| 1494 | goto free_fragment; |
| 1495 | |
| 1496 | /* File compressed and fragmented */ |
| 1497 | if (finfo.frag && finfo.comp) { |
| 1498 | dest_len = get_unaligned_le32(&sblk->block_size); |
| 1499 | fragment_block = malloc(dest_len); |
| 1500 | if (!fragment_block) { |
| 1501 | ret = -ENOMEM; |
| 1502 | goto free_fragment; |
| 1503 | } |
| 1504 | |
Joao Marcos Costa | b87fd01 | 2020-08-18 17:17:22 +0200 | [diff] [blame] | 1505 | ret = sqfs_decompress(&ctxt, fragment_block, &dest_len, |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1506 | (void *)fragment + table_offset, |
| 1507 | frag_entry.size); |
| 1508 | if (ret) { |
| 1509 | free(fragment_block); |
| 1510 | goto free_fragment; |
| 1511 | } |
| 1512 | |
| 1513 | for (j = offset + *actread; j < finfo.size; j++) { |
| 1514 | memcpy(buf + j, &fragment_block[finfo.offset + j], 1); |
| 1515 | (*actread)++; |
| 1516 | } |
| 1517 | |
| 1518 | free(fragment_block); |
| 1519 | |
| 1520 | } else if (finfo.frag && !finfo.comp) { |
| 1521 | fragment_block = (void *)fragment + table_offset; |
| 1522 | |
| 1523 | for (j = offset + *actread; j < finfo.size; j++) { |
| 1524 | memcpy(buf + j, &fragment_block[finfo.offset + j], 1); |
| 1525 | (*actread)++; |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | free_fragment: |
| 1530 | free(fragment); |
| 1531 | free_buffer: |
| 1532 | if (datablk_count) |
| 1533 | free(data_buffer); |
| 1534 | free_datablk: |
| 1535 | if (datablk_count) |
| 1536 | free(datablock); |
| 1537 | free_paths: |
| 1538 | free(file); |
| 1539 | free(dir); |
Richard Genoud | 84a5f57 | 2020-11-03 12:11:13 +0100 | [diff] [blame] | 1540 | sqfs_closedir(dirsp); |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1541 | |
| 1542 | return ret; |
| 1543 | } |
| 1544 | |
| 1545 | int sqfs_size(const char *filename, loff_t *size) |
| 1546 | { |
| 1547 | struct squashfs_super_block *sblk = ctxt.sblk; |
| 1548 | struct squashfs_symlink_inode *symlink; |
| 1549 | struct fs_dir_stream *dirsp = NULL; |
| 1550 | struct squashfs_base_inode *base; |
| 1551 | struct squashfs_dir_stream *dirs; |
| 1552 | struct squashfs_lreg_inode *lreg; |
| 1553 | struct squashfs_reg_inode *reg; |
| 1554 | char *dir, *file, *resolved; |
| 1555 | struct fs_dirent *dent; |
| 1556 | unsigned char *ipos; |
| 1557 | int ret, i_number; |
| 1558 | |
| 1559 | sqfs_split_path(&file, &dir, filename); |
| 1560 | /* |
| 1561 | * sqfs_opendir will uncompress inode and directory tables, and will |
| 1562 | * return a pointer to the directory that contains the requested file. |
| 1563 | */ |
| 1564 | ret = sqfs_opendir(dir, &dirsp); |
| 1565 | if (ret) { |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1566 | ret = -EINVAL; |
| 1567 | goto free_strings; |
| 1568 | } |
| 1569 | |
| 1570 | dirs = (struct squashfs_dir_stream *)dirsp; |
| 1571 | |
| 1572 | while (!sqfs_readdir(dirsp, &dent)) { |
| 1573 | ret = strcmp(dent->name, file); |
| 1574 | if (!ret) |
| 1575 | break; |
| 1576 | free(dirs->entry); |
Richard Genoud | c3dadcb | 2020-11-03 12:11:09 +0100 | [diff] [blame] | 1577 | dirs->entry = NULL; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | if (ret) { |
| 1581 | printf("File not found.\n"); |
| 1582 | *size = 0; |
| 1583 | ret = -EINVAL; |
| 1584 | goto free_strings; |
| 1585 | } |
| 1586 | |
| 1587 | i_number = dirs->dir_header->inode_number + dirs->entry->inode_offset; |
| 1588 | ipos = sqfs_find_inode(dirs->inode_table, i_number, sblk->inodes, |
| 1589 | sblk->block_size); |
| 1590 | free(dirs->entry); |
Richard Genoud | c3dadcb | 2020-11-03 12:11:09 +0100 | [diff] [blame] | 1591 | dirs->entry = NULL; |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1592 | |
| 1593 | base = (struct squashfs_base_inode *)ipos; |
| 1594 | switch (get_unaligned_le16(&base->inode_type)) { |
| 1595 | case SQFS_REG_TYPE: |
| 1596 | reg = (struct squashfs_reg_inode *)ipos; |
| 1597 | *size = get_unaligned_le32(®->file_size); |
| 1598 | break; |
| 1599 | case SQFS_LREG_TYPE: |
| 1600 | lreg = (struct squashfs_lreg_inode *)ipos; |
| 1601 | *size = get_unaligned_le64(&lreg->file_size); |
| 1602 | break; |
| 1603 | case SQFS_SYMLINK_TYPE: |
| 1604 | case SQFS_LSYMLINK_TYPE: |
| 1605 | symlink = (struct squashfs_symlink_inode *)ipos; |
| 1606 | resolved = sqfs_resolve_symlink(symlink, filename); |
| 1607 | ret = sqfs_size(resolved, size); |
| 1608 | free(resolved); |
| 1609 | break; |
| 1610 | case SQFS_BLKDEV_TYPE: |
| 1611 | case SQFS_CHRDEV_TYPE: |
| 1612 | case SQFS_LBLKDEV_TYPE: |
| 1613 | case SQFS_LCHRDEV_TYPE: |
| 1614 | case SQFS_FIFO_TYPE: |
| 1615 | case SQFS_SOCKET_TYPE: |
| 1616 | case SQFS_LFIFO_TYPE: |
| 1617 | case SQFS_LSOCKET_TYPE: |
| 1618 | default: |
| 1619 | printf("Unable to recover entry's size.\n"); |
| 1620 | *size = 0; |
| 1621 | ret = -EINVAL; |
| 1622 | break; |
| 1623 | } |
| 1624 | |
| 1625 | free_strings: |
| 1626 | free(dir); |
| 1627 | free(file); |
| 1628 | |
| 1629 | sqfs_closedir(dirsp); |
| 1630 | |
| 1631 | return ret; |
| 1632 | } |
| 1633 | |
| 1634 | void sqfs_close(void) |
| 1635 | { |
| 1636 | free(ctxt.sblk); |
| 1637 | ctxt.cur_dev = NULL; |
Joao Marcos Costa | 877576c | 2020-08-18 17:17:21 +0200 | [diff] [blame] | 1638 | sqfs_decompressor_cleanup(&ctxt); |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | void sqfs_closedir(struct fs_dir_stream *dirs) |
| 1642 | { |
| 1643 | struct squashfs_dir_stream *sqfs_dirs; |
| 1644 | |
| 1645 | sqfs_dirs = (struct squashfs_dir_stream *)dirs; |
| 1646 | free(sqfs_dirs->inode_table); |
| 1647 | free(sqfs_dirs->dir_table); |
| 1648 | free(sqfs_dirs->dir_header); |
Richard Genoud | 2575eb2 | 2020-11-03 12:11:02 +0100 | [diff] [blame] | 1649 | free(sqfs_dirs); |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 1650 | } |