fs/ntfs3: Add more attributes checks in mi_enum_attr()
[linux-block.git] / fs / ntfs3 / record.c
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
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  */
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
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
95 void mi_put(struct mft_inode *mi)
96 {
97         mi_clear(mi);
98         kfree(mi);
99 }
100
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  */
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 struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr)
193 {
194         const struct MFT_REC *rec = mi->mrec;
195         u32 used = le32_to_cpu(rec->used);
196         u32 t32, off, asize, prev_type;
197         u16 t16;
198         u64 data_size, alloc_size, tot_size;
199
200         if (!attr) {
201                 u32 total = le32_to_cpu(rec->total);
202
203                 off = le16_to_cpu(rec->attr_off);
204
205                 if (used > total)
206                         return NULL;
207
208                 if (off >= used || off < MFTRECORD_FIXUP_OFFSET_1 ||
209                     !IS_ALIGNED(off, 4)) {
210                         return NULL;
211                 }
212
213                 /* Skip non-resident records. */
214                 if (!is_rec_inuse(rec))
215                         return NULL;
216
217                 prev_type = 0;
218                 attr = Add2Ptr(rec, off);
219         } else {
220                 /* Check if input attr inside record. */
221                 off = PtrOffset(rec, attr);
222                 if (off >= used)
223                         return NULL;
224
225                 asize = le32_to_cpu(attr->size);
226                 if (asize < SIZEOF_RESIDENT) {
227                         /* Impossible 'cause we should not return such attribute. */
228                         return NULL;
229                 }
230
231                 /* Overflow check. */
232                 if (off + asize < off)
233                         return NULL;
234
235                 prev_type = le32_to_cpu(attr->type);
236                 attr = Add2Ptr(attr, asize);
237                 off += asize;
238         }
239
240         asize = le32_to_cpu(attr->size);
241
242         /* Can we use the first field (attr->type). */
243         if (off + 8 > used) {
244                 static_assert(ALIGN(sizeof(enum ATTR_TYPE), 8) == 8);
245                 return NULL;
246         }
247
248         if (attr->type == ATTR_END) {
249                 /* End of enumeration. */
250                 return NULL;
251         }
252
253         /* 0x100 is last known attribute for now. */
254         t32 = le32_to_cpu(attr->type);
255         if (!t32 || (t32 & 0xf) || (t32 > 0x100))
256                 return NULL;
257
258         /* attributes in record must be ordered by type */
259         if (t32 < prev_type)
260                 return NULL;
261
262         /* Check overflow and boundary. */
263         if (off + asize < off || off + asize > used)
264                 return NULL;
265
266         /* Check size of attribute. */
267         if (!attr->non_res) {
268                 /* Check resident fields. */
269                 if (asize < SIZEOF_RESIDENT)
270                         return NULL;
271
272                 t16 = le16_to_cpu(attr->res.data_off);
273                 if (t16 > asize)
274                         return NULL;
275
276                 if (t16 + le32_to_cpu(attr->res.data_size) > asize)
277                         return NULL;
278
279                 t32 = sizeof(short) * attr->name_len;
280                 if (t32 && le16_to_cpu(attr->name_off) + t32 > t16)
281                         return NULL;
282
283                 return attr;
284         }
285
286         /* Check nonresident fields. */
287         if (attr->non_res != 1)
288                 return NULL;
289
290         t16 = le16_to_cpu(attr->nres.run_off);
291         if (t16 > asize)
292                 return NULL;
293
294         t32 = sizeof(short) * attr->name_len;
295         if (t32 && le16_to_cpu(attr->name_off) + t32 > t16)
296                 return NULL;
297
298         /* Check start/end vcn. */
299         if (le64_to_cpu(attr->nres.svcn) > le64_to_cpu(attr->nres.evcn) + 1)
300                 return NULL;
301
302         data_size = le64_to_cpu(attr->nres.data_size);
303         if (le64_to_cpu(attr->nres.valid_size) > data_size)
304                 return NULL;
305
306         alloc_size = le64_to_cpu(attr->nres.alloc_size);
307         if (data_size > alloc_size)
308                 return NULL;
309
310         t32 = mi->sbi->cluster_mask;
311         if (alloc_size & t32)
312                 return NULL;
313
314         if (!attr->nres.svcn && is_attr_ext(attr)) {
315                 /* First segment of sparse/compressed attribute */
316                 if (asize + 8 < SIZEOF_NONRESIDENT_EX)
317                         return NULL;
318
319                 tot_size = le64_to_cpu(attr->nres.total_size);
320                 if (tot_size & t32)
321                         return NULL;
322
323                 if (tot_size > alloc_size)
324                         return NULL;
325         } else {
326                 if (asize + 8 < SIZEOF_NONRESIDENT)
327                         return NULL;
328
329                 if (attr->nres.c_unit)
330                         return NULL;
331         }
332
333         return attr;
334 }
335
336 /*
337  * mi_find_attr - Find the attribute by type and name and id.
338  */
339 struct ATTRIB *mi_find_attr(struct mft_inode *mi, struct ATTRIB *attr,
340                             enum ATTR_TYPE type, const __le16 *name,
341                             u8 name_len, const __le16 *id)
342 {
343         u32 type_in = le32_to_cpu(type);
344         u32 atype;
345
346 next_attr:
347         attr = mi_enum_attr(mi, attr);
348         if (!attr)
349                 return NULL;
350
351         atype = le32_to_cpu(attr->type);
352         if (atype > type_in)
353                 return NULL;
354
355         if (atype < type_in)
356                 goto next_attr;
357
358         if (attr->name_len != name_len)
359                 goto next_attr;
360
361         if (name_len && memcmp(attr_name(attr), name, name_len * sizeof(short)))
362                 goto next_attr;
363
364         if (id && *id != attr->id)
365                 goto next_attr;
366
367         return attr;
368 }
369
370 int mi_write(struct mft_inode *mi, int wait)
371 {
372         struct MFT_REC *rec;
373         int err;
374         struct ntfs_sb_info *sbi;
375
376         if (!mi->dirty)
377                 return 0;
378
379         sbi = mi->sbi;
380         rec = mi->mrec;
381
382         err = ntfs_write_bh(sbi, &rec->rhdr, &mi->nb, wait);
383         if (err)
384                 return err;
385
386         if (mi->rno < sbi->mft.recs_mirr)
387                 sbi->flags |= NTFS_FLAGS_MFTMIRR;
388
389         mi->dirty = false;
390
391         return 0;
392 }
393
394 int mi_format_new(struct mft_inode *mi, struct ntfs_sb_info *sbi, CLST rno,
395                   __le16 flags, bool is_mft)
396 {
397         int err;
398         u16 seq = 1;
399         struct MFT_REC *rec;
400         u64 vbo = (u64)rno << sbi->record_bits;
401
402         err = mi_init(mi, sbi, rno);
403         if (err)
404                 return err;
405
406         rec = mi->mrec;
407
408         if (rno == MFT_REC_MFT) {
409                 ;
410         } else if (rno < MFT_REC_FREE) {
411                 seq = rno;
412         } else if (rno >= sbi->mft.used) {
413                 ;
414         } else if (mi_read(mi, is_mft)) {
415                 ;
416         } else if (rec->rhdr.sign == NTFS_FILE_SIGNATURE) {
417                 /* Record is reused. Update its sequence number. */
418                 seq = le16_to_cpu(rec->seq) + 1;
419                 if (!seq)
420                         seq = 1;
421         }
422
423         memcpy(rec, sbi->new_rec, sbi->record_size);
424
425         rec->seq = cpu_to_le16(seq);
426         rec->flags = RECORD_FLAG_IN_USE | flags;
427         if (MFTRECORD_FIXUP_OFFSET == MFTRECORD_FIXUP_OFFSET_3)
428                 rec->mft_record = cpu_to_le32(rno);
429
430         mi->dirty = true;
431
432         if (!mi->nb.nbufs) {
433                 struct ntfs_inode *ni = sbi->mft.ni;
434                 bool lock = false;
435
436                 if (is_mounted(sbi) && !is_mft) {
437                         down_read(&ni->file.run_lock);
438                         lock = true;
439                 }
440
441                 err = ntfs_get_bh(sbi, &ni->file.run, vbo, sbi->record_size,
442                                   &mi->nb);
443                 if (lock)
444                         up_read(&ni->file.run_lock);
445         }
446
447         return err;
448 }
449
450 /*
451  * mi_insert_attr - Reserve space for new attribute.
452  *
453  * Return: Not full constructed attribute or NULL if not possible to create.
454  */
455 struct ATTRIB *mi_insert_attr(struct mft_inode *mi, enum ATTR_TYPE type,
456                               const __le16 *name, u8 name_len, u32 asize,
457                               u16 name_off)
458 {
459         size_t tail;
460         struct ATTRIB *attr;
461         __le16 id;
462         struct MFT_REC *rec = mi->mrec;
463         struct ntfs_sb_info *sbi = mi->sbi;
464         u32 used = le32_to_cpu(rec->used);
465         const u16 *upcase = sbi->upcase;
466
467         /* Can we insert mi attribute? */
468         if (used + asize > sbi->record_size)
469                 return NULL;
470
471         /*
472          * Scan through the list of attributes to find the point
473          * at which we should insert it.
474          */
475         attr = NULL;
476         while ((attr = mi_enum_attr(mi, attr))) {
477                 int diff = compare_attr(attr, type, name, name_len, upcase);
478
479                 if (diff < 0)
480                         continue;
481
482                 if (!diff && !is_attr_indexed(attr))
483                         return NULL;
484                 break;
485         }
486
487         if (!attr) {
488                 /* Append. */
489                 tail = 8;
490                 attr = Add2Ptr(rec, used - 8);
491         } else {
492                 /* Insert before 'attr'. */
493                 tail = used - PtrOffset(rec, attr);
494         }
495
496         id = mi_new_attt_id(mi);
497
498         memmove(Add2Ptr(attr, asize), attr, tail);
499         memset(attr, 0, asize);
500
501         attr->type = type;
502         attr->size = cpu_to_le32(asize);
503         attr->name_len = name_len;
504         attr->name_off = cpu_to_le16(name_off);
505         attr->id = id;
506
507         memmove(Add2Ptr(attr, name_off), name, name_len * sizeof(short));
508         rec->used = cpu_to_le32(used + asize);
509
510         mi->dirty = true;
511
512         return attr;
513 }
514
515 /*
516  * mi_remove_attr - Remove the attribute from record.
517  *
518  * NOTE: The source attr will point to next attribute.
519  */
520 bool mi_remove_attr(struct ntfs_inode *ni, struct mft_inode *mi,
521                     struct ATTRIB *attr)
522 {
523         struct MFT_REC *rec = mi->mrec;
524         u32 aoff = PtrOffset(rec, attr);
525         u32 used = le32_to_cpu(rec->used);
526         u32 asize = le32_to_cpu(attr->size);
527
528         if (aoff + asize > used)
529                 return false;
530
531         if (ni && is_attr_indexed(attr)) {
532                 le16_add_cpu(&ni->mi.mrec->hard_links, -1);
533                 ni->mi.dirty = true;
534         }
535
536         used -= asize;
537         memmove(attr, Add2Ptr(attr, asize), used - aoff);
538         rec->used = cpu_to_le32(used);
539         mi->dirty = true;
540
541         return true;
542 }
543
544 /* bytes = "new attribute size" - "old attribute size" */
545 bool mi_resize_attr(struct mft_inode *mi, struct ATTRIB *attr, int bytes)
546 {
547         struct MFT_REC *rec = mi->mrec;
548         u32 aoff = PtrOffset(rec, attr);
549         u32 total, used = le32_to_cpu(rec->used);
550         u32 nsize, asize = le32_to_cpu(attr->size);
551         u32 rsize = le32_to_cpu(attr->res.data_size);
552         int tail = (int)(used - aoff - asize);
553         int dsize;
554         char *next;
555
556         if (tail < 0 || aoff >= used)
557                 return false;
558
559         if (!bytes)
560                 return true;
561
562         total = le32_to_cpu(rec->total);
563         next = Add2Ptr(attr, asize);
564
565         if (bytes > 0) {
566                 dsize = ALIGN(bytes, 8);
567                 if (used + dsize > total)
568                         return false;
569                 nsize = asize + dsize;
570                 /* Move tail */
571                 memmove(next + dsize, next, tail);
572                 memset(next, 0, dsize);
573                 used += dsize;
574                 rsize += dsize;
575         } else {
576                 dsize = ALIGN(-bytes, 8);
577                 if (dsize > asize)
578                         return false;
579                 nsize = asize - dsize;
580                 memmove(next - dsize, next, tail);
581                 used -= dsize;
582                 rsize -= dsize;
583         }
584
585         rec->used = cpu_to_le32(used);
586         attr->size = cpu_to_le32(nsize);
587         if (!attr->non_res)
588                 attr->res.data_size = cpu_to_le32(rsize);
589         mi->dirty = true;
590
591         return true;
592 }
593
594 /*
595  * Pack runs in MFT record.
596  * If failed record is not changed.
597  */
598 int mi_pack_runs(struct mft_inode *mi, struct ATTRIB *attr,
599                  struct runs_tree *run, CLST len)
600 {
601         int err = 0;
602         struct ntfs_sb_info *sbi = mi->sbi;
603         u32 new_run_size;
604         CLST plen;
605         struct MFT_REC *rec = mi->mrec;
606         CLST svcn = le64_to_cpu(attr->nres.svcn);
607         u32 used = le32_to_cpu(rec->used);
608         u32 aoff = PtrOffset(rec, attr);
609         u32 asize = le32_to_cpu(attr->size);
610         char *next = Add2Ptr(attr, asize);
611         u16 run_off = le16_to_cpu(attr->nres.run_off);
612         u32 run_size = asize - run_off;
613         u32 tail = used - aoff - asize;
614         u32 dsize = sbi->record_size - used;
615
616         /* Make a maximum gap in current record. */
617         memmove(next + dsize, next, tail);
618
619         /* Pack as much as possible. */
620         err = run_pack(run, svcn, len, Add2Ptr(attr, run_off), run_size + dsize,
621                        &plen);
622         if (err < 0) {
623                 memmove(next, next + dsize, tail);
624                 return err;
625         }
626
627         new_run_size = ALIGN(err, 8);
628
629         memmove(next + new_run_size - run_size, next + dsize, tail);
630
631         attr->size = cpu_to_le32(asize + new_run_size - run_size);
632         attr->nres.evcn = cpu_to_le64(svcn + plen - 1);
633         rec->used = cpu_to_le32(used + new_run_size - run_size);
634         mi->dirty = true;
635
636         return 0;
637 }