]> Pileus Git - ~andy/linux/blob - fs/isofs/rock.c
[PATCH] rock.c: handle corrupted directories
[~andy/linux] / fs / isofs / rock.c
1 /*
2  *  linux/fs/isofs/rock.c
3  *
4  *  (C) 1992, 1993  Eric Youngdale
5  *
6  *  Rock Ridge Extensions to iso9660
7  */
8
9 #include <linux/slab.h>
10 #include <linux/pagemap.h>
11 #include <linux/smp_lock.h>
12
13 #include "isofs.h"
14 #include "rock.h"
15
16 /*
17  * These functions are designed to read the system areas of a directory record
18  * and extract relevant information.  There are different functions provided
19  * depending upon what information we need at the time.  One function fills
20  * out an inode structure, a second one extracts a filename, a third one
21  * returns a symbolic link name, and a fourth one returns the extent number
22  * for the file.
23  */
24
25 #define SIG(A,B) ((A) | ((B) << 8))     /* isonum_721() */
26
27 struct rock_state {
28         void *buffer;
29         unsigned char *chr;
30         int len;
31         int cont_size;
32         int cont_extent;
33         int cont_offset;
34         struct inode *inode;
35 };
36
37 /*
38  * This is a way of ensuring that we have something in the system
39  * use fields that is compatible with Rock Ridge.  Return zero on success.
40  */
41
42 static int check_sp(struct rock_ridge *rr, struct inode *inode)
43 {
44         if (rr->u.SP.magic[0] != 0xbe)
45                 return -1;
46         if (rr->u.SP.magic[1] != 0xef)
47                 return -1;
48         ISOFS_SB(inode->i_sb)->s_rock_offset = rr->u.SP.skip;
49         return 0;
50 }
51
52 static void setup_rock_ridge(struct iso_directory_record *de,
53                         struct inode *inode, struct rock_state *rs)
54 {
55         rs->len = sizeof(struct iso_directory_record) + de->name_len[0];
56         if (rs->len & 1)
57                 (rs->len)++;
58         rs->chr = (unsigned char *)de + rs->len;
59         rs->len = *((unsigned char *)de) - rs->len;
60         if (rs->len < 0)
61                 rs->len = 0;
62
63         if (ISOFS_SB(inode->i_sb)->s_rock_offset != -1) {
64                 rs->len -= ISOFS_SB(inode->i_sb)->s_rock_offset;
65                 rs->chr += ISOFS_SB(inode->i_sb)->s_rock_offset;
66                 if (rs->len < 0)
67                         rs->len = 0;
68         }
69 }
70
71 static void init_rock_state(struct rock_state *rs, struct inode *inode)
72 {
73         memset(rs, 0, sizeof(*rs));
74         rs->inode = inode;
75 }
76
77 /*
78  * Returns 0 if the caller should continue scanning, 1 if the scan must end
79  * and -ve on error.
80  */
81 static int rock_continue(struct rock_state *rs)
82 {
83         int ret = 1;
84         int blocksize = 1 << rs->inode->i_blkbits;
85         const int min_de_size = offsetof(struct rock_ridge, u);
86
87         kfree(rs->buffer);
88         rs->buffer = NULL;
89
90         if ((unsigned)rs->cont_offset > blocksize - min_de_size ||
91             (unsigned)rs->cont_size > blocksize ||
92             (unsigned)(rs->cont_offset + rs->cont_size) > blocksize) {
93                 printk(KERN_NOTICE "rock: corrupted directory entry. "
94                         "extent=%d, offset=%d, size=%d\n",
95                         rs->cont_extent, rs->cont_offset, rs->cont_size);
96                 ret = -EIO;
97                 goto out;
98         }
99
100         if (rs->cont_extent) {
101                 struct buffer_head *bh;
102
103                 rs->buffer = kmalloc(rs->cont_size, GFP_KERNEL);
104                 if (!rs->buffer) {
105                         ret = -ENOMEM;
106                         goto out;
107                 }
108                 ret = -EIO;
109                 bh = sb_bread(rs->inode->i_sb, rs->cont_extent);
110                 if (bh) {
111                         memcpy(rs->buffer, bh->b_data + rs->cont_offset,
112                                         rs->cont_size);
113                         put_bh(bh);
114                         rs->chr = rs->buffer;
115                         rs->len = rs->cont_size;
116                         rs->cont_extent = 0;
117                         rs->cont_size = 0;
118                         rs->cont_offset = 0;
119                         return 0;
120                 }
121                 printk("Unable to read rock-ridge attributes\n");
122         }
123 out:
124         kfree(rs->buffer);
125         rs->buffer = NULL;
126         return ret;
127 }
128
129 /*
130  * return length of name field; 0: not found, -1: to be ignored
131  */
132 int get_rock_ridge_filename(struct iso_directory_record *de,
133                             char *retname, struct inode *inode)
134 {
135         struct rock_state rs;
136         struct rock_ridge *rr;
137         int sig;
138         int retnamlen = 0;
139         int truncate = 0;
140         int ret = 0;
141
142         if (!ISOFS_SB(inode->i_sb)->s_rock)
143                 return 0;
144         *retname = 0;
145
146         init_rock_state(&rs, inode);
147         setup_rock_ridge(de, inode, &rs);
148 repeat:
149
150         while (rs.len > 2) { /* There may be one byte for padding somewhere */
151                 rr = (struct rock_ridge *)rs.chr;
152                 if (rr->len < 3)
153                         goto out;       /* Something got screwed up here */
154                 sig = isonum_721(rs.chr);
155                 rs.chr += rr->len;
156                 rs.len -= rr->len;
157                 if (rs.len < 0)
158                         goto out;       /* corrupted isofs */
159
160                 switch (sig) {
161                 case SIG('R', 'R'):
162                         if ((rr->u.RR.flags[0] & RR_NM) == 0)
163                                 goto out;
164                         break;
165                 case SIG('S', 'P'):
166                         if (check_sp(rr, inode))
167                                 goto out;
168                         break;
169                 case SIG('C', 'E'):
170                         rs.cont_extent = isonum_733(rr->u.CE.extent);
171                         rs.cont_offset = isonum_733(rr->u.CE.offset);
172                         rs.cont_size = isonum_733(rr->u.CE.size);
173                         break;
174                 case SIG('N', 'M'):
175                         if (truncate)
176                                 break;
177                         if (rr->len < 5)
178                                 break;
179                         /*
180                          * If the flags are 2 or 4, this indicates '.' or '..'.
181                          * We don't want to do anything with this, because it
182                          * screws up the code that calls us.  We don't really
183                          * care anyways, since we can just use the non-RR
184                          * name.
185                          */
186                         if (rr->u.NM.flags & 6)
187                                 break;
188
189                         if (rr->u.NM.flags & ~1) {
190                                 printk("Unsupported NM flag settings (%d)\n",
191                                         rr->u.NM.flags);
192                                 break;
193                         }
194                         if ((strlen(retname) + rr->len - 5) >= 254) {
195                                 truncate = 1;
196                                 break;
197                         }
198                         strncat(retname, rr->u.NM.name, rr->len - 5);
199                         retnamlen += rr->len - 5;
200                         break;
201                 case SIG('R', 'E'):
202                         kfree(rs.buffer);
203                         return -1;
204                 default:
205                         break;
206                 }
207         }
208         ret = rock_continue(&rs);
209         if (ret == 0)
210                 goto repeat;
211         if (ret == 1)
212                 return retnamlen; /* If 0, this file did not have a NM field */
213 out:
214         kfree(rs.buffer);
215         return ret;
216 }
217
218 static int
219 parse_rock_ridge_inode_internal(struct iso_directory_record *de,
220                                 struct inode *inode, int regard_xa)
221 {
222         int symlink_len = 0;
223         int cnt, sig;
224         struct inode *reloc;
225         struct rock_ridge *rr;
226         int rootflag;
227         struct rock_state rs;
228         int ret = 0;
229
230         if (!ISOFS_SB(inode->i_sb)->s_rock)
231                 return 0;
232
233         init_rock_state(&rs, inode);
234         setup_rock_ridge(de, inode, &rs);
235         if (regard_xa) {
236                 rs.chr += 14;
237                 rs.len -= 14;
238                 if (rs.len < 0)
239                         rs.len = 0;
240         }
241
242 repeat:
243         while (rs.len > 2) { /* There may be one byte for padding somewhere */
244                 rr = (struct rock_ridge *)rs.chr;
245                 if (rr->len < 3)
246                         goto out;       /* Something got screwed up here */
247                 sig = isonum_721(rs.chr);
248                 rs.chr += rr->len;
249                 rs.len -= rr->len;
250                 if (rs.len < 0)
251                         goto out;       /* corrupted isofs */
252
253                 switch (sig) {
254 #ifndef CONFIG_ZISOFS           /* No flag for SF or ZF */
255                 case SIG('R', 'R'):
256                         if ((rr->u.RR.flags[0] &
257                              (RR_PX | RR_TF | RR_SL | RR_CL)) == 0)
258                                 goto out;
259                         break;
260 #endif
261                 case SIG('S', 'P'):
262                         if (check_sp(rr, inode))
263                                 goto out;
264                         break;
265                 case SIG('C', 'E'):
266                         rs.cont_extent = isonum_733(rr->u.CE.extent);
267                         rs.cont_offset = isonum_733(rr->u.CE.offset);
268                         rs.cont_size = isonum_733(rr->u.CE.size);
269                         break;
270                 case SIG('E', 'R'):
271                         ISOFS_SB(inode->i_sb)->s_rock = 1;
272                         printk(KERN_DEBUG "ISO 9660 Extensions: ");
273                         {
274                                 int p;
275                                 for (p = 0; p < rr->u.ER.len_id; p++)
276                                         printk("%c", rr->u.ER.data[p]);
277                         }
278                         printk("\n");
279                         break;
280                 case SIG('P', 'X'):
281                         inode->i_mode = isonum_733(rr->u.PX.mode);
282                         inode->i_nlink = isonum_733(rr->u.PX.n_links);
283                         inode->i_uid = isonum_733(rr->u.PX.uid);
284                         inode->i_gid = isonum_733(rr->u.PX.gid);
285                         break;
286                 case SIG('P', 'N'):
287                         {
288                                 int high, low;
289                                 high = isonum_733(rr->u.PN.dev_high);
290                                 low = isonum_733(rr->u.PN.dev_low);
291                                 /*
292                                  * The Rock Ridge standard specifies that if
293                                  * sizeof(dev_t) <= 4, then the high field is
294                                  * unused, and the device number is completely
295                                  * stored in the low field.  Some writers may
296                                  * ignore this subtlety,
297                                  * and as a result we test to see if the entire
298                                  * device number is
299                                  * stored in the low field, and use that.
300                                  */
301                                 if ((low & ~0xff) && high == 0) {
302                                         inode->i_rdev =
303                                             MKDEV(low >> 8, low & 0xff);
304                                 } else {
305                                         inode->i_rdev =
306                                             MKDEV(high, low);
307                                 }
308                         }
309                         break;
310                 case SIG('T', 'F'):
311                         /*
312                          * Some RRIP writers incorrectly place ctime in the
313                          * TF_CREATE field. Try to handle this correctly for
314                          * either case.
315                          */
316                         /* Rock ridge never appears on a High Sierra disk */
317                         cnt = 0;
318                         if (rr->u.TF.flags & TF_CREATE) {
319                                 inode->i_ctime.tv_sec =
320                                     iso_date(rr->u.TF.times[cnt++].time,
321                                              0);
322                                 inode->i_ctime.tv_nsec = 0;
323                         }
324                         if (rr->u.TF.flags & TF_MODIFY) {
325                                 inode->i_mtime.tv_sec =
326                                     iso_date(rr->u.TF.times[cnt++].time,
327                                              0);
328                                 inode->i_mtime.tv_nsec = 0;
329                         }
330                         if (rr->u.TF.flags & TF_ACCESS) {
331                                 inode->i_atime.tv_sec =
332                                     iso_date(rr->u.TF.times[cnt++].time,
333                                              0);
334                                 inode->i_atime.tv_nsec = 0;
335                         }
336                         if (rr->u.TF.flags & TF_ATTRIBUTES) {
337                                 inode->i_ctime.tv_sec =
338                                     iso_date(rr->u.TF.times[cnt++].time,
339                                              0);
340                                 inode->i_ctime.tv_nsec = 0;
341                         }
342                         break;
343                 case SIG('S', 'L'):
344                         {
345                                 int slen;
346                                 struct SL_component *slp;
347                                 struct SL_component *oldslp;
348                                 slen = rr->len - 5;
349                                 slp = &rr->u.SL.link;
350                                 inode->i_size = symlink_len;
351                                 while (slen > 1) {
352                                         rootflag = 0;
353                                         switch (slp->flags & ~1) {
354                                         case 0:
355                                                 inode->i_size +=
356                                                     slp->len;
357                                                 break;
358                                         case 2:
359                                                 inode->i_size += 1;
360                                                 break;
361                                         case 4:
362                                                 inode->i_size += 2;
363                                                 break;
364                                         case 8:
365                                                 rootflag = 1;
366                                                 inode->i_size += 1;
367                                                 break;
368                                         default:
369                                                 printk("Symlink component flag "
370                                                         "not implemented\n");
371                                         }
372                                         slen -= slp->len + 2;
373                                         oldslp = slp;
374                                         slp = (struct SL_component *)
375                                                 (((char *)slp) + slp->len + 2);
376
377                                         if (slen < 2) {
378                                                 if (((rr->u.SL.
379                                                       flags & 1) != 0)
380                                                     &&
381                                                     ((oldslp->
382                                                       flags & 1) == 0))
383                                                         inode->i_size +=
384                                                             1;
385                                                 break;
386                                         }
387
388                                         /*
389                                          * If this component record isn't
390                                          * continued, then append a '/'.
391                                          */
392                                         if (!rootflag
393                                             && (oldslp->flags & 1) == 0)
394                                                 inode->i_size += 1;
395                                 }
396                         }
397                         symlink_len = inode->i_size;
398                         break;
399                 case SIG('R', 'E'):
400                         printk(KERN_WARNING "Attempt to read inode for "
401                                         "relocated directory\n");
402                         goto out;
403                 case SIG('C', 'L'):
404                         ISOFS_I(inode)->i_first_extent =
405                             isonum_733(rr->u.CL.location);
406                         reloc =
407                             isofs_iget(inode->i_sb,
408                                        ISOFS_I(inode)->i_first_extent,
409                                        0);
410                         if (!reloc)
411                                 goto out;
412                         inode->i_mode = reloc->i_mode;
413                         inode->i_nlink = reloc->i_nlink;
414                         inode->i_uid = reloc->i_uid;
415                         inode->i_gid = reloc->i_gid;
416                         inode->i_rdev = reloc->i_rdev;
417                         inode->i_size = reloc->i_size;
418                         inode->i_blocks = reloc->i_blocks;
419                         inode->i_atime = reloc->i_atime;
420                         inode->i_ctime = reloc->i_ctime;
421                         inode->i_mtime = reloc->i_mtime;
422                         iput(reloc);
423                         break;
424 #ifdef CONFIG_ZISOFS
425                 case SIG('Z', 'F'): {
426                         int algo;
427
428                         if (ISOFS_SB(inode->i_sb)->s_nocompress)
429                                 break;
430                         algo = isonum_721(rr->u.ZF.algorithm);
431                         if (algo == SIG('p', 'z')) {
432                                 int block_shift =
433                                         isonum_711(&rr->u.ZF.parms[1]);
434                                 if (block_shift < PAGE_CACHE_SHIFT
435                                                 || block_shift > 17) {
436                                         printk(KERN_WARNING "isofs: "
437                                                 "Can't handle ZF block "
438                                                 "size of 2^%d\n",
439                                                 block_shift);
440                                 } else {
441                                         /*
442                                          * Note: we don't change
443                                          * i_blocks here
444                                          */
445                                         ISOFS_I(inode)->i_file_format =
446                                                 isofs_file_compressed;
447                                         /*
448                                          * Parameters to compression
449                                          * algorithm (header size,
450                                          * block size)
451                                          */
452                                         ISOFS_I(inode)->i_format_parm[0] =
453                                                 isonum_711(&rr->u.ZF.parms[0]);
454                                         ISOFS_I(inode)->i_format_parm[1] =
455                                                 isonum_711(&rr->u.ZF.parms[1]);
456                                         inode->i_size =
457                                             isonum_733(rr->u.ZF.
458                                                        real_size);
459                                 }
460                         } else {
461                                 printk(KERN_WARNING
462                                        "isofs: Unknown ZF compression "
463                                                 "algorithm: %c%c\n",
464                                        rr->u.ZF.algorithm[0],
465                                        rr->u.ZF.algorithm[1]);
466                         }
467                         break;
468                 }
469 #endif
470                 default:
471                         break;
472                 }
473         }
474         ret = rock_continue(&rs);
475         if (ret == 0)
476                 goto repeat;
477         if (ret == 1)
478                 ret = 0;
479 out:
480         kfree(rs.buffer);
481         return ret;
482 }
483
484 static char *get_symlink_chunk(char *rpnt, struct rock_ridge *rr, char *plimit)
485 {
486         int slen;
487         int rootflag;
488         struct SL_component *oldslp;
489         struct SL_component *slp;
490         slen = rr->len - 5;
491         slp = &rr->u.SL.link;
492         while (slen > 1) {
493                 rootflag = 0;
494                 switch (slp->flags & ~1) {
495                 case 0:
496                         if (slp->len > plimit - rpnt)
497                                 return NULL;
498                         memcpy(rpnt, slp->text, slp->len);
499                         rpnt += slp->len;
500                         break;
501                 case 2:
502                         if (rpnt >= plimit)
503                                 return NULL;
504                         *rpnt++ = '.';
505                         break;
506                 case 4:
507                         if (2 > plimit - rpnt)
508                                 return NULL;
509                         *rpnt++ = '.';
510                         *rpnt++ = '.';
511                         break;
512                 case 8:
513                         if (rpnt >= plimit)
514                                 return NULL;
515                         rootflag = 1;
516                         *rpnt++ = '/';
517                         break;
518                 default:
519                         printk("Symlink component flag not implemented (%d)\n",
520                                slp->flags);
521                 }
522                 slen -= slp->len + 2;
523                 oldslp = slp;
524                 slp = (struct SL_component *)((char *)slp + slp->len + 2);
525
526                 if (slen < 2) {
527                         /*
528                          * If there is another SL record, and this component
529                          * record isn't continued, then add a slash.
530                          */
531                         if ((!rootflag) && (rr->u.SL.flags & 1) &&
532                             !(oldslp->flags & 1)) {
533                                 if (rpnt >= plimit)
534                                         return NULL;
535                                 *rpnt++ = '/';
536                         }
537                         break;
538                 }
539
540                 /*
541                  * If this component record isn't continued, then append a '/'.
542                  */
543                 if (!rootflag && !(oldslp->flags & 1)) {
544                         if (rpnt >= plimit)
545                                 return NULL;
546                         *rpnt++ = '/';
547                 }
548         }
549         return rpnt;
550 }
551
552 int parse_rock_ridge_inode(struct iso_directory_record *de, struct inode *inode)
553 {
554         int result = parse_rock_ridge_inode_internal(de, inode, 0);
555
556         /*
557          * if rockridge flag was reset and we didn't look for attributes
558          * behind eventual XA attributes, have a look there
559          */
560         if ((ISOFS_SB(inode->i_sb)->s_rock_offset == -1)
561             && (ISOFS_SB(inode->i_sb)->s_rock == 2)) {
562                 result = parse_rock_ridge_inode_internal(de, inode, 14);
563         }
564         return result;
565 }
566
567 /*
568  * readpage() for symlinks: reads symlink contents into the page and either
569  * makes it uptodate and returns 0 or returns error (-EIO)
570  */
571 static int rock_ridge_symlink_readpage(struct file *file, struct page *page)
572 {
573         struct inode *inode = page->mapping->host;
574         struct iso_inode_info *ei = ISOFS_I(inode);
575         char *link = kmap(page);
576         unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
577         struct buffer_head *bh;
578         char *rpnt = link;
579         unsigned char *pnt;
580         struct iso_directory_record *raw_de;
581         unsigned long block, offset;
582         int sig;
583         struct rock_ridge *rr;
584         struct rock_state rs;
585         int ret;
586
587         if (!ISOFS_SB(inode->i_sb)->s_rock)
588                 goto error;
589
590         init_rock_state(&rs, inode);
591         block = ei->i_iget5_block;
592         lock_kernel();
593         bh = sb_bread(inode->i_sb, block);
594         if (!bh)
595                 goto out_noread;
596
597         offset = ei->i_iget5_offset;
598         pnt = (unsigned char *)bh->b_data + offset;
599
600         raw_de = (struct iso_directory_record *)pnt;
601
602         /*
603          * If we go past the end of the buffer, there is some sort of error.
604          */
605         if (offset + *pnt > bufsize)
606                 goto out_bad_span;
607
608         /*
609          * Now test for possible Rock Ridge extensions which will override
610          * some of these numbers in the inode structure.
611          */
612
613         setup_rock_ridge(raw_de, inode, &rs);
614
615 repeat:
616         while (rs.len > 2) { /* There may be one byte for padding somewhere */
617                 rr = (struct rock_ridge *)rs.chr;
618                 if (rr->len < 3)
619                         goto out;       /* Something got screwed up here */
620                 sig = isonum_721(rs.chr);
621                 rs.chr += rr->len;
622                 rs.len -= rr->len;
623                 if (rs.len < 0)
624                         goto out;       /* corrupted isofs */
625
626                 switch (sig) {
627                 case SIG('R', 'R'):
628                         if ((rr->u.RR.flags[0] & RR_SL) == 0)
629                                 goto out;
630                         break;
631                 case SIG('S', 'P'):
632                         if (check_sp(rr, inode))
633                                 goto out;
634                         break;
635                 case SIG('S', 'L'):
636                         rpnt = get_symlink_chunk(rpnt, rr,
637                                                  link + (PAGE_SIZE - 1));
638                         if (rpnt == NULL)
639                                 goto out;
640                         break;
641                 case SIG('C', 'E'):
642                         /* This tells is if there is a continuation record */
643                         rs.cont_extent = isonum_733(rr->u.CE.extent);
644                         rs.cont_offset = isonum_733(rr->u.CE.offset);
645                         rs.cont_size = isonum_733(rr->u.CE.size);
646                 default:
647                         break;
648                 }
649         }
650         ret = rock_continue(&rs);
651         if (ret == 0)
652                 goto repeat;
653         if (ret < 0)
654                 goto fail;
655
656         if (rpnt == link)
657                 goto fail;
658         brelse(bh);
659         *rpnt = '\0';
660         unlock_kernel();
661         SetPageUptodate(page);
662         kunmap(page);
663         unlock_page(page);
664         return 0;
665
666         /* error exit from macro */
667 out:
668         kfree(rs.buffer);
669         goto fail;
670 out_noread:
671         printk("unable to read i-node block");
672         goto fail;
673 out_bad_span:
674         printk("symlink spans iso9660 blocks\n");
675 fail:
676         brelse(bh);
677         unlock_kernel();
678 error:
679         SetPageError(page);
680         kunmap(page);
681         unlock_page(page);
682         return -EIO;
683 }
684
685 struct address_space_operations isofs_symlink_aops = {
686         .readpage = rock_ridge_symlink_readpage
687 };