xref: /linux/fs/ntfs3/record.c (revision 1fdae000a3db8569430aa9189a30f8a3b7480c58)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *
4  * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
5  *
6  */
7 
8 #include <linux/fs.h>
9 
10 #include "debug.h"
11 #include "ntfs.h"
12 #include "ntfs_fs.h"
13 
compare_attr(const struct ATTRIB * left,enum ATTR_TYPE type,const __le16 * name,u8 name_len,const u16 * upcase)14 static inline int compare_attr(const struct ATTRIB *left, enum ATTR_TYPE type,
15 			       const __le16 *name, u8 name_len,
16 			       const u16 *upcase)
17 {
18 	/* First, compare the type codes. */
19 	int diff = le32_to_cpu(left->type) - le32_to_cpu(type);
20 
21 	if (diff)
22 		return diff;
23 
24 	/* They have the same type code, so we have to compare the names. */
25 	return ntfs_cmp_names(attr_name(left), left->name_len, name, name_len,
26 			      upcase, true);
27 }
28 
29 /*
30  * mi_new_attt_id
31  *
32  * Return: Unused attribute id that is less than mrec->next_attr_id.
33  */
mi_new_attt_id(struct mft_inode * mi)34 static __le16 mi_new_attt_id(struct mft_inode *mi)
35 {
36 	u16 free_id, max_id, t16;
37 	struct MFT_REC *rec = mi->mrec;
38 	struct ATTRIB *attr;
39 	__le16 id;
40 
41 	id = rec->next_attr_id;
42 	free_id = le16_to_cpu(id);
43 	if (free_id < 0x7FFF) {
44 		rec->next_attr_id = cpu_to_le16(free_id + 1);
45 		return id;
46 	}
47 
48 	/* One record can store up to 1024/24 ~= 42 attributes. */
49 	free_id = 0;
50 	max_id = 0;
51 
52 	attr = NULL;
53 
54 	for (;;) {
55 		attr = mi_enum_attr(mi, attr);
56 		if (!attr) {
57 			rec->next_attr_id = cpu_to_le16(max_id + 1);
58 			mi->dirty = true;
59 			return cpu_to_le16(free_id);
60 		}
61 
62 		t16 = le16_to_cpu(attr->id);
63 		if (t16 == free_id) {
64 			free_id += 1;
65 			attr = NULL;
66 		} else if (max_id < t16)
67 			max_id = t16;
68 	}
69 }
70 
mi_get(struct ntfs_sb_info * sbi,CLST rno,struct mft_inode ** mi)71 int mi_get(struct ntfs_sb_info *sbi, CLST rno, struct mft_inode **mi)
72 {
73 	int err;
74 	struct mft_inode *m = kzalloc(sizeof(struct mft_inode), GFP_NOFS);
75 
76 	if (!m)
77 		return -ENOMEM;
78 
79 	err = mi_init(m, sbi, rno);
80 	if (err) {
81 		kfree(m);
82 		return err;
83 	}
84 
85 	err = mi_read(m, false);
86 	if (err) {
87 		mi_put(m);
88 		return err;
89 	}
90 
91 	*mi = m;
92 	return 0;
93 }
94 
mi_put(struct mft_inode * mi)95 void mi_put(struct mft_inode *mi)
96 {
97 	mi_clear(mi);
98 	kfree(mi);
99 }
100 
mi_init(struct mft_inode * mi,struct ntfs_sb_info * sbi,CLST rno)101 int mi_init(struct mft_inode *mi, struct ntfs_sb_info *sbi, CLST rno)
102 {
103 	mi->sbi = sbi;
104 	mi->rno = rno;
105 	mi->mrec = kmalloc(sbi->record_size, GFP_NOFS);
106 	if (!mi->mrec)
107 		return -ENOMEM;
108 
109 	return 0;
110 }
111 
112 /*
113  * mi_read - Read MFT data.
114  */
mi_read(struct mft_inode * mi,bool is_mft)115 int mi_read(struct mft_inode *mi, bool is_mft)
116 {
117 	int err;
118 	struct MFT_REC *rec = mi->mrec;
119 	struct ntfs_sb_info *sbi = mi->sbi;
120 	u32 bpr = sbi->record_size;
121 	u64 vbo = (u64)mi->rno << sbi->record_bits;
122 	struct ntfs_inode *mft_ni = sbi->mft.ni;
123 	struct runs_tree *run = mft_ni ? &mft_ni->file.run : NULL;
124 	struct rw_semaphore *rw_lock = NULL;
125 
126 	if (is_mounted(sbi)) {
127 		if (!is_mft && mft_ni) {
128 			rw_lock = &mft_ni->file.run_lock;
129 			down_read(rw_lock);
130 		}
131 	}
132 
133 	err = ntfs_read_bh(sbi, run, vbo, &rec->rhdr, bpr, &mi->nb);
134 	if (rw_lock)
135 		up_read(rw_lock);
136 	if (!err)
137 		goto ok;
138 
139 	if (err == -E_NTFS_FIXUP) {
140 		mi->dirty = true;
141 		goto ok;
142 	}
143 
144 	if (err != -ENOENT)
145 		goto out;
146 
147 	if (rw_lock) {
148 		ni_lock(mft_ni);
149 		down_write(rw_lock);
150 	}
151 	err = attr_load_runs_vcn(mft_ni, ATTR_DATA, NULL, 0, run,
152 				 vbo >> sbi->cluster_bits);
153 	if (rw_lock) {
154 		up_write(rw_lock);
155 		ni_unlock(mft_ni);
156 	}
157 	if (err)
158 		goto out;
159 
160 	if (rw_lock)
161 		down_read(rw_lock);
162 	err = ntfs_read_bh(sbi, run, vbo, &rec->rhdr, bpr, &mi->nb);
163 	if (rw_lock)
164 		up_read(rw_lock);
165 
166 	if (err == -E_NTFS_FIXUP) {
167 		mi->dirty = true;
168 		goto ok;
169 	}
170 	if (err)
171 		goto out;
172 
173 ok:
174 	/* Check field 'total' only here. */
175 	if (le32_to_cpu(rec->total) != bpr) {
176 		err = -EINVAL;
177 		goto out;
178 	}
179 
180 	return 0;
181 
182 out:
183 	if (err == -E_NTFS_CORRUPT) {
184 		ntfs_err(sbi->sb, "mft corrupted");
185 		ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
186 		err = -EINVAL;
187 	}
188 
189 	return err;
190 }
191 
192 /*
193  * mi_enum_attr - start/continue attributes enumeration in record.
194  *
195  * NOTE: mi->mrec - memory of size sbi->record_size
196  * here we sure that mi->mrec->total == sbi->record_size (see mi_read)
197  */
mi_enum_attr(struct mft_inode * mi,struct ATTRIB * attr)198 struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr)
199 {
200 	const struct MFT_REC *rec = mi->mrec;
201 	u32 used = le32_to_cpu(rec->used);
202 	u32 t32, off, asize, prev_type;
203 	u16 t16;
204 	u64 data_size, alloc_size, tot_size;
205 
206 	if (!attr) {
207 		u32 total = le32_to_cpu(rec->total);
208 
209 		off = le16_to_cpu(rec->attr_off);
210 
211 		if (used > total)
212 			return NULL;
213 
214 		if (off >= used || off < MFTRECORD_FIXUP_OFFSET_1 ||
215 		    !IS_ALIGNED(off, 8)) {
216 			return NULL;
217 		}
218 
219 		/* Skip non-resident records. */
220 		if (!is_rec_inuse(rec))
221 			return NULL;
222 
223 		prev_type = 0;
224 		attr = Add2Ptr(rec, off);
225 	} else {
226 		/*
227 		 * We don't need to check previous attr here. There is
228 		 * a bounds checking in the previous round.
229 		 */
230 		off = PtrOffset(rec, attr);
231 
232 		asize = le32_to_cpu(attr->size);
233 
234 		prev_type = le32_to_cpu(attr->type);
235 		attr = Add2Ptr(attr, asize);
236 		off += asize;
237 	}
238 
239 	/*
240 	 * Can we use the first fields:
241 	 * attr->type,
242 	 * attr->size
243 	 */
244 	if (off + 8 > used) {
245 		static_assert(ALIGN(sizeof(enum ATTR_TYPE), 8) == 8);
246 		return NULL;
247 	}
248 
249 	if (attr->type == ATTR_END) {
250 		/* End of enumeration. */
251 		return NULL;
252 	}
253 
254 	/* 0x100 is last known attribute for now. */
255 	t32 = le32_to_cpu(attr->type);
256 	if (!t32 || (t32 & 0xf) || (t32 > 0x100))
257 		return NULL;
258 
259 	/* attributes in record must be ordered by type */
260 	if (t32 < prev_type)
261 		return NULL;
262 
263 	asize = le32_to_cpu(attr->size);
264 
265 	if (!IS_ALIGNED(asize, 8))
266 		return NULL;
267 
268 	/* Check overflow and boundary. */
269 	if (off + asize < off || off + asize > used)
270 		return NULL;
271 
272 	/* Can we use the field attr->non_res. */
273 	if (off + 9 > used)
274 		return NULL;
275 
276 	/* Check size of attribute. */
277 	if (!attr->non_res) {
278 		/* Check resident fields. */
279 		if (asize < SIZEOF_RESIDENT)
280 			return NULL;
281 
282 		t16 = le16_to_cpu(attr->res.data_off);
283 		if (t16 > asize)
284 			return NULL;
285 
286 		if (le32_to_cpu(attr->res.data_size) > asize - t16)
287 			return NULL;
288 
289 		t32 = sizeof(short) * attr->name_len;
290 		if (t32 && le16_to_cpu(attr->name_off) + t32 > t16)
291 			return NULL;
292 
293 		return attr;
294 	}
295 
296 	/* Check nonresident fields. */
297 	if (attr->non_res != 1)
298 		return NULL;
299 
300 	/* Can we use memory including attr->nres.valid_size? */
301 	if (asize < SIZEOF_NONRESIDENT)
302 		return NULL;
303 
304 	t16 = le16_to_cpu(attr->nres.run_off);
305 	if (t16 > asize)
306 		return NULL;
307 
308 	t32 = sizeof(short) * attr->name_len;
309 	if (t32 && le16_to_cpu(attr->name_off) + t32 > t16)
310 		return NULL;
311 
312 	/* Check start/end vcn. */
313 	if (le64_to_cpu(attr->nres.svcn) > le64_to_cpu(attr->nres.evcn) + 1)
314 		return NULL;
315 
316 	data_size = le64_to_cpu(attr->nres.data_size);
317 	if (le64_to_cpu(attr->nres.valid_size) > data_size)
318 		return NULL;
319 
320 	alloc_size = le64_to_cpu(attr->nres.alloc_size);
321 	if (data_size > alloc_size)
322 		return NULL;
323 
324 	t32 = mi->sbi->cluster_mask;
325 	if (alloc_size & t32)
326 		return NULL;
327 
328 	if (!attr->nres.svcn && is_attr_ext(attr)) {
329 		/* First segment of sparse/compressed attribute */
330 		/* Can we use memory including attr->nres.total_size? */
331 		if (asize < SIZEOF_NONRESIDENT_EX)
332 			return NULL;
333 
334 		tot_size = le64_to_cpu(attr->nres.total_size);
335 		if (tot_size & t32)
336 			return NULL;
337 
338 		if (tot_size > alloc_size)
339 			return NULL;
340 	} else {
341 		if (attr->nres.c_unit)
342 			return NULL;
343 
344 		if (alloc_size > mi->sbi->volume.size)
345 			return NULL;
346 	}
347 
348 	return attr;
349 }
350 
351 /*
352  * mi_find_attr - Find the attribute by type and name and id.
353  */
mi_find_attr(struct mft_inode * mi,struct ATTRIB * attr,enum ATTR_TYPE type,const __le16 * name,u8 name_len,const __le16 * id)354 struct ATTRIB *mi_find_attr(struct mft_inode *mi, struct ATTRIB *attr,
355 			    enum ATTR_TYPE type, const __le16 *name,
356 			    u8 name_len, const __le16 *id)
357 {
358 	u32 type_in = le32_to_cpu(type);
359 	u32 atype;
360 
361 next_attr:
362 	attr = mi_enum_attr(mi, attr);
363 	if (!attr)
364 		return NULL;
365 
366 	atype = le32_to_cpu(attr->type);
367 	if (atype > type_in)
368 		return NULL;
369 
370 	if (atype < type_in)
371 		goto next_attr;
372 
373 	if (attr->name_len != name_len)
374 		goto next_attr;
375 
376 	if (name_len && memcmp(attr_name(attr), name, name_len * sizeof(short)))
377 		goto next_attr;
378 
379 	if (id && *id != attr->id)
380 		goto next_attr;
381 
382 	return attr;
383 }
384 
mi_write(struct mft_inode * mi,int wait)385 int mi_write(struct mft_inode *mi, int wait)
386 {
387 	struct MFT_REC *rec;
388 	int err;
389 	struct ntfs_sb_info *sbi;
390 
391 	if (!mi->dirty)
392 		return 0;
393 
394 	sbi = mi->sbi;
395 	rec = mi->mrec;
396 
397 	err = ntfs_write_bh(sbi, &rec->rhdr, &mi->nb, wait);
398 	if (err)
399 		return err;
400 
401 	if (mi->rno < sbi->mft.recs_mirr)
402 		sbi->flags |= NTFS_FLAGS_MFTMIRR;
403 
404 	mi->dirty = false;
405 
406 	return 0;
407 }
408 
mi_format_new(struct mft_inode * mi,struct ntfs_sb_info * sbi,CLST rno,__le16 flags,bool is_mft)409 int mi_format_new(struct mft_inode *mi, struct ntfs_sb_info *sbi, CLST rno,
410 		  __le16 flags, bool is_mft)
411 {
412 	int err;
413 	u16 seq = 1;
414 	struct MFT_REC *rec;
415 	u64 vbo = (u64)rno << sbi->record_bits;
416 
417 	err = mi_init(mi, sbi, rno);
418 	if (err)
419 		return err;
420 
421 	rec = mi->mrec;
422 
423 	if (rno == MFT_REC_MFT) {
424 		;
425 	} else if (rno < MFT_REC_FREE) {
426 		seq = rno;
427 	} else if (rno >= sbi->mft.used) {
428 		;
429 	} else if (mi_read(mi, is_mft)) {
430 		;
431 	} else if (rec->rhdr.sign == NTFS_FILE_SIGNATURE) {
432 		/* Record is reused. Update its sequence number. */
433 		seq = le16_to_cpu(rec->seq) + 1;
434 		if (!seq)
435 			seq = 1;
436 	}
437 
438 	memcpy(rec, sbi->new_rec, sbi->record_size);
439 
440 	rec->seq = cpu_to_le16(seq);
441 	rec->flags = RECORD_FLAG_IN_USE | flags;
442 	if (MFTRECORD_FIXUP_OFFSET == MFTRECORD_FIXUP_OFFSET_3)
443 		rec->mft_record = cpu_to_le32(rno);
444 
445 	mi->dirty = true;
446 
447 	if (!mi->nb.nbufs) {
448 		struct ntfs_inode *ni = sbi->mft.ni;
449 		bool lock = false;
450 
451 		if (is_mounted(sbi) && !is_mft) {
452 			down_read(&ni->file.run_lock);
453 			lock = true;
454 		}
455 
456 		err = ntfs_get_bh(sbi, &ni->file.run, vbo, sbi->record_size,
457 				  &mi->nb);
458 		if (lock)
459 			up_read(&ni->file.run_lock);
460 	}
461 
462 	return err;
463 }
464 
465 /*
466  * mi_insert_attr - Reserve space for new attribute.
467  *
468  * Return: Not full constructed attribute or NULL if not possible to create.
469  */
mi_insert_attr(struct mft_inode * mi,enum ATTR_TYPE type,const __le16 * name,u8 name_len,u32 asize,u16 name_off)470 struct ATTRIB *mi_insert_attr(struct mft_inode *mi, enum ATTR_TYPE type,
471 			      const __le16 *name, u8 name_len, u32 asize,
472 			      u16 name_off)
473 {
474 	size_t tail;
475 	struct ATTRIB *attr;
476 	__le16 id;
477 	struct MFT_REC *rec = mi->mrec;
478 	struct ntfs_sb_info *sbi = mi->sbi;
479 	u32 used = le32_to_cpu(rec->used);
480 	const u16 *upcase = sbi->upcase;
481 
482 	/* Can we insert mi attribute? */
483 	if (used + asize > sbi->record_size)
484 		return NULL;
485 
486 	/*
487 	 * Scan through the list of attributes to find the point
488 	 * at which we should insert it.
489 	 */
490 	attr = NULL;
491 	while ((attr = mi_enum_attr(mi, attr))) {
492 		int diff = compare_attr(attr, type, name, name_len, upcase);
493 
494 		if (diff < 0)
495 			continue;
496 
497 		if (!diff && !is_attr_indexed(attr))
498 			return NULL;
499 		break;
500 	}
501 
502 	if (!attr) {
503 		/* Append. */
504 		tail = 8;
505 		attr = Add2Ptr(rec, used - 8);
506 	} else {
507 		/* Insert before 'attr'. */
508 		tail = used - PtrOffset(rec, attr);
509 	}
510 
511 	id = mi_new_attt_id(mi);
512 
513 	memmove(Add2Ptr(attr, asize), attr, tail);
514 	memset(attr, 0, asize);
515 
516 	attr->type = type;
517 	attr->size = cpu_to_le32(asize);
518 	attr->name_len = name_len;
519 	attr->name_off = cpu_to_le16(name_off);
520 	attr->id = id;
521 
522 	memmove(Add2Ptr(attr, name_off), name, name_len * sizeof(short));
523 	rec->used = cpu_to_le32(used + asize);
524 
525 	mi->dirty = true;
526 
527 	return attr;
528 }
529 
530 /*
531  * mi_remove_attr - Remove the attribute from record.
532  *
533  * NOTE: The source attr will point to next attribute.
534  */
mi_remove_attr(struct ntfs_inode * ni,struct mft_inode * mi,struct ATTRIB * attr)535 bool mi_remove_attr(struct ntfs_inode *ni, struct mft_inode *mi,
536 		    struct ATTRIB *attr)
537 {
538 	struct MFT_REC *rec = mi->mrec;
539 	u32 aoff = PtrOffset(rec, attr);
540 	u32 used = le32_to_cpu(rec->used);
541 	u32 asize = le32_to_cpu(attr->size);
542 
543 	if (aoff + asize > used)
544 		return false;
545 
546 	if (ni && is_attr_indexed(attr) && attr->type == ATTR_NAME) {
547 		u16 links = le16_to_cpu(ni->mi.mrec->hard_links);
548 		if (!links) {
549 			/* minor error. Not critical. */
550 		} else {
551 			ni->mi.mrec->hard_links = cpu_to_le16(links - 1);
552 			ni->mi.dirty = true;
553 		}
554 	}
555 
556 	used -= asize;
557 	memmove(attr, Add2Ptr(attr, asize), used - aoff);
558 	rec->used = cpu_to_le32(used);
559 	mi->dirty = true;
560 
561 	return true;
562 }
563 
564 /* bytes = "new attribute size" - "old attribute size" */
mi_resize_attr(struct mft_inode * mi,struct ATTRIB * attr,int bytes)565 bool mi_resize_attr(struct mft_inode *mi, struct ATTRIB *attr, int bytes)
566 {
567 	struct MFT_REC *rec = mi->mrec;
568 	u32 aoff = PtrOffset(rec, attr);
569 	u32 total, used = le32_to_cpu(rec->used);
570 	u32 nsize, asize = le32_to_cpu(attr->size);
571 	u32 rsize = le32_to_cpu(attr->res.data_size);
572 	int tail = (int)(used - aoff - asize);
573 	int dsize;
574 	char *next;
575 
576 	if (tail < 0 || aoff >= used)
577 		return false;
578 
579 	if (!bytes)
580 		return true;
581 
582 	total = le32_to_cpu(rec->total);
583 	next = Add2Ptr(attr, asize);
584 
585 	if (bytes > 0) {
586 		dsize = ALIGN(bytes, 8);
587 		if (used + dsize > total)
588 			return false;
589 		nsize = asize + dsize;
590 		/* Move tail */
591 		memmove(next + dsize, next, tail);
592 		memset(next, 0, dsize);
593 		used += dsize;
594 		rsize += dsize;
595 	} else {
596 		dsize = ALIGN(-bytes, 8);
597 		if (dsize > asize)
598 			return false;
599 		nsize = asize - dsize;
600 		memmove(next - dsize, next, tail);
601 		used -= dsize;
602 		rsize -= dsize;
603 	}
604 
605 	rec->used = cpu_to_le32(used);
606 	attr->size = cpu_to_le32(nsize);
607 	if (!attr->non_res)
608 		attr->res.data_size = cpu_to_le32(rsize);
609 	mi->dirty = true;
610 
611 	return true;
612 }
613 
614 /*
615  * Pack runs in MFT record.
616  * If failed record is not changed.
617  */
mi_pack_runs(struct mft_inode * mi,struct ATTRIB * attr,struct runs_tree * run,CLST len)618 int mi_pack_runs(struct mft_inode *mi, struct ATTRIB *attr,
619 		 struct runs_tree *run, CLST len)
620 {
621 	int err = 0;
622 	struct ntfs_sb_info *sbi = mi->sbi;
623 	u32 new_run_size;
624 	CLST plen;
625 	struct MFT_REC *rec = mi->mrec;
626 	CLST svcn = le64_to_cpu(attr->nres.svcn);
627 	u32 used = le32_to_cpu(rec->used);
628 	u32 aoff = PtrOffset(rec, attr);
629 	u32 asize = le32_to_cpu(attr->size);
630 	char *next = Add2Ptr(attr, asize);
631 	u16 run_off = le16_to_cpu(attr->nres.run_off);
632 	u32 run_size = asize - run_off;
633 	u32 tail = used - aoff - asize;
634 	u32 dsize = sbi->record_size - used;
635 
636 	/* Make a maximum gap in current record. */
637 	memmove(next + dsize, next, tail);
638 
639 	/* Pack as much as possible. */
640 	err = run_pack(run, svcn, len, Add2Ptr(attr, run_off), run_size + dsize,
641 		       &plen);
642 	if (err < 0) {
643 		memmove(next, next + dsize, tail);
644 		return err;
645 	}
646 
647 	new_run_size = ALIGN(err, 8);
648 
649 	memmove(next + new_run_size - run_size, next + dsize, tail);
650 
651 	attr->size = cpu_to_le32(asize + new_run_size - run_size);
652 	attr->nres.evcn = cpu_to_le64(svcn + plen - 1);
653 	rec->used = cpu_to_le32(used + new_run_size - run_size);
654 	mi->dirty = true;
655 
656 	return 0;
657 }
658