]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - SQUASHFS/squashfs-tools-4.4/squashfs-tools/unsquash-1.c
Fix the bug when booting ALT Linux in UEFI mode. (#1645)
[Ventoy.git] / SQUASHFS / squashfs-tools-4.4 / squashfs-tools / unsquash-1.c
1 /*
2 * Unsquash a squashfs filesystem. This is a highly compressed read only
3 * filesystem.
4 *
5 * Copyright (c) 2009, 2010, 2011, 2012, 2019
6 * Phillip Lougher <phillip@squashfs.org.uk>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2,
11 * or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 *
22 * unsquash-1.c
23 */
24
25 #include "unsquashfs.h"
26 #include "squashfs_compat.h"
27
28 static unsigned int *uid_table, *guid_table;
29 static char *inode_table, *directory_table;
30 static squashfs_operations ops;
31
32 static void read_block_list(unsigned int *block_list, char *block_ptr, int blocks)
33 {
34 unsigned short block_size;
35 int i;
36
37 TRACE("read_block_list: blocks %d\n", blocks);
38
39 for(i = 0; i < blocks; i++, block_ptr += 2) {
40 if(swap) {
41 unsigned short sblock_size;
42 memcpy(&sblock_size, block_ptr, sizeof(unsigned short));
43 SQUASHFS_SWAP_SHORTS_3((&block_size), &sblock_size, 1);
44 } else
45 memcpy(&block_size, block_ptr, sizeof(unsigned short));
46 block_list[i] = SQUASHFS_COMPRESSED_SIZE(block_size) |
47 (SQUASHFS_COMPRESSED(block_size) ? 0 :
48 SQUASHFS_COMPRESSED_BIT_BLOCK);
49 }
50 }
51
52
53 static struct inode *read_inode(unsigned int start_block, unsigned int offset)
54 {
55 static union squashfs_inode_header_1 header;
56 long long start = sBlk.s.inode_table_start + start_block;
57 int bytes = lookup_entry(inode_table_hash, start);
58 char *block_ptr = inode_table + bytes + offset;
59 static struct inode i;
60
61 TRACE("read_inode: reading inode [%d:%d]\n", start_block, offset);
62
63 if(bytes == -1)
64 EXIT_UNSQUASH("read_inode: inode table block %lld not found\n",
65 start);
66
67 if(swap) {
68 squashfs_base_inode_header_1 sinode;
69 memcpy(&sinode, block_ptr, sizeof(header.base));
70 SQUASHFS_SWAP_BASE_INODE_HEADER_1(&header.base, &sinode,
71 sizeof(squashfs_base_inode_header_1));
72 } else
73 memcpy(&header.base, block_ptr, sizeof(header.base));
74
75 i.uid = (uid_t) uid_table[(header.base.inode_type - 1) /
76 SQUASHFS_TYPES * 16 + header.base.uid];
77 if(header.base.inode_type == SQUASHFS_IPC_TYPE) {
78 squashfs_ipc_inode_header_1 *inodep = &header.ipc;
79
80 if(swap) {
81 squashfs_ipc_inode_header_1 sinodep;
82 memcpy(&sinodep, block_ptr, sizeof(sinodep));
83 SQUASHFS_SWAP_IPC_INODE_HEADER_1(inodep, &sinodep);
84 } else
85 memcpy(inodep, block_ptr, sizeof(*inodep));
86
87 if(inodep->type == SQUASHFS_SOCKET_TYPE) {
88 i.mode = S_IFSOCK | header.base.mode;
89 i.type = SQUASHFS_SOCKET_TYPE;
90 } else {
91 i.mode = S_IFIFO | header.base.mode;
92 i.type = SQUASHFS_FIFO_TYPE;
93 }
94 i.uid = (uid_t) uid_table[inodep->offset * 16 + inodep->uid];
95 } else {
96 i.mode = lookup_type[(header.base.inode_type - 1) %
97 SQUASHFS_TYPES + 1] | header.base.mode;
98 i.type = (header.base.inode_type - 1) % SQUASHFS_TYPES + 1;
99 }
100
101 i.xattr = SQUASHFS_INVALID_XATTR;
102 i.gid = header.base.guid == 15 ? i.uid :
103 (uid_t) guid_table[header.base.guid];
104 i.time = sBlk.s.mkfs_time;
105 i.inode_number = inode_number ++;
106
107 switch(i.type) {
108 case SQUASHFS_DIR_TYPE: {
109 squashfs_dir_inode_header_1 *inode = &header.dir;
110
111 if(swap) {
112 squashfs_dir_inode_header_1 sinode;
113 memcpy(&sinode, block_ptr, sizeof(header.dir));
114 SQUASHFS_SWAP_DIR_INODE_HEADER_1(inode,
115 &sinode);
116 } else
117 memcpy(inode, block_ptr, sizeof(header.dir));
118
119 i.data = inode->file_size;
120 i.offset = inode->offset;
121 i.start = inode->start_block;
122 i.time = inode->mtime;
123 break;
124 }
125 case SQUASHFS_FILE_TYPE: {
126 squashfs_reg_inode_header_1 *inode = &header.reg;
127
128 if(swap) {
129 squashfs_reg_inode_header_1 sinode;
130 memcpy(&sinode, block_ptr, sizeof(sinode));
131 SQUASHFS_SWAP_REG_INODE_HEADER_1(inode,
132 &sinode);
133 } else
134 memcpy(inode, block_ptr, sizeof(*inode));
135
136 i.data = inode->file_size;
137 i.time = inode->mtime;
138 i.blocks = (i.data + sBlk.s.block_size - 1) >>
139 sBlk.s.block_log;
140 i.start = inode->start_block;
141 i.block_ptr = block_ptr + sizeof(*inode);
142 i.fragment = 0;
143 i.frag_bytes = 0;
144 i.offset = 0;
145 i.sparse = 0;
146 break;
147 }
148 case SQUASHFS_SYMLINK_TYPE: {
149 squashfs_symlink_inode_header_1 *inodep =
150 &header.symlink;
151
152 if(swap) {
153 squashfs_symlink_inode_header_1 sinodep;
154 memcpy(&sinodep, block_ptr, sizeof(sinodep));
155 SQUASHFS_SWAP_SYMLINK_INODE_HEADER_1(inodep,
156 &sinodep);
157 } else
158 memcpy(inodep, block_ptr, sizeof(*inodep));
159
160 i.symlink = malloc(inodep->symlink_size + 1);
161 if(i.symlink == NULL)
162 EXIT_UNSQUASH("read_inode: failed to malloc "
163 "symlink data\n");
164 strncpy(i.symlink, block_ptr +
165 sizeof(squashfs_symlink_inode_header_1),
166 inodep->symlink_size);
167 i.symlink[inodep->symlink_size] = '\0';
168 i.data = inodep->symlink_size;
169 break;
170 }
171 case SQUASHFS_BLKDEV_TYPE:
172 case SQUASHFS_CHRDEV_TYPE: {
173 squashfs_dev_inode_header_1 *inodep = &header.dev;
174
175 if(swap) {
176 squashfs_dev_inode_header_1 sinodep;
177 memcpy(&sinodep, block_ptr, sizeof(sinodep));
178 SQUASHFS_SWAP_DEV_INODE_HEADER_1(inodep,
179 &sinodep);
180 } else
181 memcpy(inodep, block_ptr, sizeof(*inodep));
182
183 i.data = inodep->rdev;
184 break;
185 }
186 case SQUASHFS_FIFO_TYPE:
187 case SQUASHFS_SOCKET_TYPE: {
188 i.data = 0;
189 break;
190 }
191 default:
192 EXIT_UNSQUASH("Unknown inode type %d in "
193 " read_inode_header_1!\n",
194 header.base.inode_type);
195 }
196 return &i;
197 }
198
199
200 static struct dir *squashfs_opendir(unsigned int block_start, unsigned int offset,
201 struct inode **i)
202 {
203 squashfs_dir_header_2 dirh;
204 char buffer[sizeof(squashfs_dir_entry_2) + SQUASHFS_NAME_LEN + 1]
205 __attribute__((aligned));
206 squashfs_dir_entry_2 *dire = (squashfs_dir_entry_2 *) buffer;
207 long long start;
208 int bytes;
209 int dir_count, size;
210 struct dir_ent *new_dir;
211 struct dir *dir;
212
213 TRACE("squashfs_opendir: inode start block %d, offset %d\n",
214 block_start, offset);
215
216 *i = read_inode(block_start, offset);
217
218 dir = malloc(sizeof(struct dir));
219 if(dir == NULL)
220 EXIT_UNSQUASH("squashfs_opendir: malloc failed!\n");
221
222 dir->dir_count = 0;
223 dir->cur_entry = 0;
224 dir->mode = (*i)->mode;
225 dir->uid = (*i)->uid;
226 dir->guid = (*i)->gid;
227 dir->mtime = (*i)->time;
228 dir->xattr = (*i)->xattr;
229 dir->dirs = NULL;
230
231 if ((*i)->data == 0)
232 /*
233 * if the directory is empty, skip the unnecessary
234 * lookup_entry, this fixes the corner case with
235 * completely empty filesystems where lookup_entry correctly
236 * returning -1 is incorrectly treated as an error
237 */
238 return dir;
239
240 start = sBlk.s.directory_table_start + (*i)->start;
241 bytes = lookup_entry(directory_table_hash, start);
242 if(bytes == -1)
243 EXIT_UNSQUASH("squashfs_opendir: directory block %d not "
244 "found!\n", block_start);
245
246 bytes += (*i)->offset;
247 size = (*i)->data + bytes;
248
249 while(bytes < size) {
250 if(swap) {
251 squashfs_dir_header_2 sdirh;
252 memcpy(&sdirh, directory_table + bytes, sizeof(sdirh));
253 SQUASHFS_SWAP_DIR_HEADER_2(&dirh, &sdirh);
254 } else
255 memcpy(&dirh, directory_table + bytes, sizeof(dirh));
256
257 dir_count = dirh.count + 1;
258 TRACE("squashfs_opendir: Read directory header @ byte position "
259 "%d, %d directory entries\n", bytes, dir_count);
260 bytes += sizeof(dirh);
261
262 /* dir_count should never be larger than SQUASHFS_DIR_COUNT */
263 if(dir_count > SQUASHFS_DIR_COUNT) {
264 ERROR("File system corrupted: too many entries in directory\n");
265 goto corrupted;
266 }
267
268 while(dir_count--) {
269 if(swap) {
270 squashfs_dir_entry_2 sdire;
271 memcpy(&sdire, directory_table + bytes,
272 sizeof(sdire));
273 SQUASHFS_SWAP_DIR_ENTRY_2(dire, &sdire);
274 } else
275 memcpy(dire, directory_table + bytes,
276 sizeof(*dire));
277 bytes += sizeof(*dire);
278
279 /* size should never be SQUASHFS_NAME_LEN or larger */
280 if(dire->size >= SQUASHFS_NAME_LEN) {
281 ERROR("File system corrupted: filename too long\n");
282 goto corrupted;
283 }
284
285 memcpy(dire->name, directory_table + bytes,
286 dire->size + 1);
287 dire->name[dire->size + 1] = '\0';
288 TRACE("squashfs_opendir: directory entry %s, inode "
289 "%d:%d, type %d\n", dire->name,
290 dirh.start_block, dire->offset, dire->type);
291 if((dir->dir_count % DIR_ENT_SIZE) == 0) {
292 new_dir = realloc(dir->dirs, (dir->dir_count +
293 DIR_ENT_SIZE) * sizeof(struct dir_ent));
294 if(new_dir == NULL)
295 EXIT_UNSQUASH("squashfs_opendir: "
296 "realloc failed!\n");
297 dir->dirs = new_dir;
298 }
299 strcpy(dir->dirs[dir->dir_count].name, dire->name);
300 dir->dirs[dir->dir_count].start_block =
301 dirh.start_block;
302 dir->dirs[dir->dir_count].offset = dire->offset;
303 dir->dirs[dir->dir_count].type = dire->type;
304 dir->dir_count ++;
305 bytes += dire->size + 1;
306 }
307 }
308
309 return dir;
310
311 corrupted:
312 free(dir->dirs);
313 free(dir);
314 return NULL;
315 }
316
317
318 squashfs_operations *read_filesystem_tables_1()
319 {
320 long long table_start;
321
322 /* Read uid and gid lookup tables */
323
324 /* Sanity check super block contents */
325 if(sBlk.no_guids) {
326 if(sBlk.guid_start >= sBlk.s.bytes_used) {
327 ERROR("read_filesystem_tables: gid start too large in super block\n");
328 goto corrupted;
329 }
330
331 /* In 1.x filesystems, there should never be more than 15 gids */
332 if(sBlk.no_guids > 15) {
333 ERROR("read_filesystem_tables: gids too large in super block\n");
334 goto corrupted;
335 }
336
337 if(read_ids(sBlk.no_guids, sBlk.guid_start, sBlk.s.bytes_used, &guid_table) == FALSE)
338 goto corrupted;
339
340 table_start = sBlk.guid_start;
341 } else {
342 /* no guids, guid_start should be 0 */
343 if(sBlk.guid_start != 0) {
344 ERROR("read_filesystem_tables: gid start too large in super block\n");
345 goto corrupted;
346 }
347
348 table_start = sBlk.s.bytes_used;
349 }
350
351 if(sBlk.uid_start >= table_start) {
352 ERROR("read_filesystem_tables: uid start too large in super block\n");
353 goto corrupted;
354 }
355
356 /* There should be at least one uid */
357 if(sBlk.no_uids == 0) {
358 ERROR("read_filesystem_tables: uid count bad in super block\n");
359 goto corrupted;
360 }
361
362 /* In 1.x filesystems, there should never be more than 48 uids */
363 if(sBlk.no_uids > 48) {
364 ERROR("read_filesystem_tables: uids too large in super block\n");
365 goto corrupted;
366 }
367
368 if(read_ids(sBlk.no_uids, sBlk.uid_start, table_start, &uid_table) == FALSE)
369 goto corrupted;
370
371 table_start = sBlk.uid_start;
372
373 /* Read directory table */
374
375 /* Sanity check super block contents */
376 if(sBlk.s.directory_table_start > table_start) {
377 ERROR("read_filesystem_tables: directory table start too large in super block\n");
378 goto corrupted;
379 }
380
381 directory_table = read_directory_table(sBlk.s.directory_table_start,
382 table_start);
383 if(directory_table == NULL)
384 goto corrupted;
385
386 /* Read inode table */
387
388 /* Sanity check super block contents */
389 if(sBlk.s.inode_table_start >= sBlk.s.directory_table_start) {
390 ERROR("read_filesystem_tables: inode table start too large in super block\n");
391 goto corrupted;
392 }
393
394 inode_table = read_inode_table(sBlk.s.inode_table_start,
395 sBlk.s.directory_table_start);
396 if(inode_table == NULL)
397 goto corrupted;
398
399 return &ops;
400
401 corrupted:
402 ERROR("File system corruption detected\n");
403 return NULL;
404 }
405
406
407 static squashfs_operations ops = {
408 .opendir = squashfs_opendir,
409 .read_block_list = read_block_list,
410 .read_inode = read_inode
411 };